4 * This file is part of the Behat Testwork.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\Testwork\Tester\ServiceContainer;
13 use Behat\Testwork\Cli\ServiceContainer\CliExtension;
14 use Behat\Testwork\Environment\ServiceContainer\EnvironmentExtension;
15 use Behat\Testwork\ServiceContainer\Extension;
16 use Behat\Testwork\ServiceContainer\ExtensionManager;
17 use Behat\Testwork\ServiceContainer\ServiceProcessor;
18 use Behat\Testwork\Specification\ServiceContainer\SpecificationExtension;
19 use Behat\Testwork\Suite\ServiceContainer\SuiteExtension;
20 use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
21 use Symfony\Component\DependencyInjection\ContainerBuilder;
22 use Symfony\Component\DependencyInjection\Definition;
23 use Symfony\Component\DependencyInjection\Reference;
26 * Provides tester services.
28 * @author Konstantin Kudryashov <ever.zet@gmail.com>
30 abstract class TesterExtension implements Extension
35 const EXERCISE_ID = 'tester.exercise';
36 const SUITE_TESTER_ID = 'tester.suite';
37 const SPECIFICATION_TESTER_ID = 'tester.specification';
38 const RESULT_INTERPRETER_ID = 'tester.result.interpreter';
41 * Available extension points
43 const EXERCISE_WRAPPER_TAG = 'tester.exercise.wrapper';
44 const SUITE_TESTER_WRAPPER_TAG = 'tester.suite.wrapper';
45 const SPECIFICATION_TESTER_WRAPPER_TAG = 'tester.specification.wrapper';
46 const RESULT_INTERPRETATION_TAG = 'test.result.interpretation';
49 * @var ServiceProcessor
54 * Initializes extension.
56 * @param null|ServiceProcessor $processor
58 public function __construct(ServiceProcessor $processor = null)
60 $this->processor = $processor ? : new ServiceProcessor();
66 public function getConfigKey()
74 public function initialize(ExtensionManager $extensionManager)
81 public function configure(ArrayNodeDefinition $builder)
84 ->addDefaultsIfNotSet()
86 ->booleanNode('strict')
87 ->info('Sets the strict mode for result interpretation')
91 ->info('Tells tester to skip all tests')
101 public function load(ContainerBuilder $container, array $config)
103 $this->loadExerciseController($container, $config['skip']);
104 $this->loadStrictController($container, $config['strict']);
105 $this->loadResultInterpreter($container);
106 $this->loadExercise($container);
107 $this->loadSuiteTester($container);
108 $this->loadSpecificationTester($container);
114 public function process(ContainerBuilder $container)
116 $this->processExerciseWrappers($container);
117 $this->processSuiteTesterWrappers($container);
118 $this->processSpecificationTesterWrappers($container);
119 $this->processResultInterpretations($container);
123 * Loads exercise cli controllers.
125 * @param ContainerBuilder $container
126 * @param Boolean $skip
128 protected function loadExerciseController(ContainerBuilder $container, $skip = false)
130 $definition = new Definition('Behat\Testwork\Tester\Cli\ExerciseController', array(
131 new Reference(SuiteExtension::REGISTRY_ID),
132 new Reference(SpecificationExtension::FINDER_ID),
133 new Reference(self::EXERCISE_ID),
134 new Reference(self::RESULT_INTERPRETER_ID),
137 $definition->addTag(CliExtension::CONTROLLER_TAG, array('priority' => 0));
138 $container->setDefinition(CliExtension::CONTROLLER_TAG . '.exercise', $definition);
142 * Loads exercise cli controllers.
144 * @param ContainerBuilder $container
145 * @param Boolean $strict
147 protected function loadStrictController(ContainerBuilder $container, $strict = false)
149 $definition = new Definition('Behat\Testwork\Tester\Cli\StrictController', array(
150 new Reference(self::RESULT_INTERPRETER_ID),
153 $definition->addTag(CliExtension::CONTROLLER_TAG, array('priority' => 300));
154 $container->setDefinition(CliExtension::CONTROLLER_TAG . '.strict', $definition);
158 * Loads result interpreter controller
160 * @param ContainerBuilder $container
162 protected function loadResultInterpreter(ContainerBuilder $container)
164 $definition = new Definition('Behat\Testwork\Tester\Result\ResultInterpreter');
165 $container->setDefinition(self::RESULT_INTERPRETER_ID, $definition);
167 $definition = new Definition('Behat\Testwork\Tester\Result\Interpretation\SoftInterpretation');
168 $definition->addTag(self::RESULT_INTERPRETATION_TAG);
169 $container->setDefinition(self::RESULT_INTERPRETATION_TAG . '.soft', $definition);
173 * Loads exercise tester.
175 * @param ContainerBuilder $container
177 protected function loadExercise(ContainerBuilder $container)
179 $definition = new Definition('Behat\Testwork\Tester\Runtime\RuntimeExercise', array(
180 new Reference(EnvironmentExtension::MANAGER_ID),
181 new Reference(self::SUITE_TESTER_ID)
183 $container->setDefinition(self::EXERCISE_ID, $definition);
187 * Loads suite tester.
189 * @param ContainerBuilder $container
191 protected function loadSuiteTester(ContainerBuilder $container)
193 $definition = new Definition('Behat\Testwork\Tester\Runtime\RuntimeSuiteTester', array(
194 new Reference(self::SPECIFICATION_TESTER_ID)
196 $container->setDefinition(self::SUITE_TESTER_ID, $definition);
200 * Loads specification tester.
202 * @param ContainerBuilder $container
204 abstract protected function loadSpecificationTester(ContainerBuilder $container);
207 * Processes all registered exercise wrappers.
209 * @param ContainerBuilder $container
211 protected function processExerciseWrappers(ContainerBuilder $container)
213 $this->processor->processWrapperServices($container, self::EXERCISE_ID, self::EXERCISE_WRAPPER_TAG);
217 * Processes all registered suite tester wrappers.
219 * @param ContainerBuilder $container
221 protected function processSuiteTesterWrappers(ContainerBuilder $container)
223 $this->processor->processWrapperServices($container, self::SUITE_TESTER_ID, self::SUITE_TESTER_WRAPPER_TAG);
227 * Processes all registered specification tester wrappers.
229 * @param ContainerBuilder $container
231 protected function processSpecificationTesterWrappers(ContainerBuilder $container)
233 $this->processor->processWrapperServices($container, self::SPECIFICATION_TESTER_ID, self::SPECIFICATION_TESTER_WRAPPER_TAG);
237 * Processes all registered result interpretations.
239 * @param ContainerBuilder $container
241 protected function processResultInterpretations(ContainerBuilder $container)
243 $references = $this->processor->findAndSortTaggedServices($container, self::RESULT_INTERPRETATION_TAG);
244 $definition = $container->getDefinition(self::RESULT_INTERPRETER_ID);
246 foreach ($references as $reference) {
247 $definition->addMethodCall('registerResultInterpretation', array($reference));