Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Output / ServiceContainer / Formatter / PrettyFormatterFactory.php
1 <?php
2
3 /*
4  * This file is part of the Behat.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Behat\Output\ServiceContainer\Formatter;
12
13 use Behat\Behat\Definition\ServiceContainer\DefinitionExtension;
14 use Behat\Behat\EventDispatcher\Event\BackgroundTested;
15 use Behat\Behat\EventDispatcher\Event\OutlineTested;
16 use Behat\Behat\EventDispatcher\Event\ScenarioTested;
17 use Behat\Testwork\Exception\ServiceContainer\ExceptionExtension;
18 use Behat\Testwork\Output\ServiceContainer\Formatter\FormatterFactory;
19 use Behat\Testwork\Output\ServiceContainer\OutputExtension;
20 use Behat\Testwork\ServiceContainer\ServiceProcessor;
21 use Behat\Testwork\Translator\ServiceContainer\TranslatorExtension;
22 use Symfony\Component\DependencyInjection\ContainerBuilder;
23 use Symfony\Component\DependencyInjection\Definition;
24 use Symfony\Component\DependencyInjection\Reference;
25
26 /**
27  * Behat pretty formatter factory.
28  *
29  * @author Konstantin Kudryashov <ever.zet@gmail.com>
30  */
31 class PrettyFormatterFactory implements FormatterFactory
32 {
33     /**
34      * @var ServiceProcessor
35      */
36     private $processor;
37
38     /*
39      * Available services
40      */
41     const ROOT_LISTENER_ID = 'output.node.listener.pretty';
42     const RESULT_TO_STRING_CONVERTER_ID = 'output.node.printer.result_to_string';
43
44     /*
45      * Available extension points
46      */
47     const ROOT_LISTENER_WRAPPER_TAG = 'output.node.listener.pretty.wrapper';
48
49     /**
50      * Initializes extension.
51      *
52      * @param null|ServiceProcessor $processor
53      */
54     public function __construct(ServiceProcessor $processor = null)
55     {
56         $this->processor = $processor ? : new ServiceProcessor();
57     }
58
59     /**
60      * {@inheritdoc}
61      */
62     public function buildFormatter(ContainerBuilder $container)
63     {
64         $this->loadRootNodeListener($container);
65
66         $this->loadCorePrinters($container);
67         $this->loadTableOutlinePrinter($container);
68         $this->loadExpandedOutlinePrinter($container);
69         $this->loadHookPrinters($container);
70         $this->loadStatisticsPrinter($container);
71         $this->loadPrinterHelpers($container);
72
73         $this->loadFormatter($container);
74     }
75
76     /**
77      * {@inheritdoc}
78      */
79     public function processFormatter(ContainerBuilder $container)
80     {
81         $this->processListenerWrappers($container);
82     }
83
84     /**
85      * Loads pretty formatter node event listener.
86      *
87      * @param ContainerBuilder $container
88      */
89     protected function loadRootNodeListener(ContainerBuilder $container)
90     {
91         $definition = new Definition('Behat\Testwork\Output\Node\EventListener\ChainEventListener', array(
92             array(
93                 new Definition('Behat\Behat\Output\Node\EventListener\AST\SuiteListener', array(
94                     new Reference('output.node.printer.pretty.suite_setup')
95                 )),
96                 new Definition('Behat\Behat\Output\Node\EventListener\AST\FeatureListener', array(
97                     new Reference('output.node.printer.pretty.feature'),
98                     new Reference('output.node.printer.pretty.feature_setup')
99                 )),
100                 $this->proxySiblingEvents(
101                     BackgroundTested::BEFORE,
102                     BackgroundTested::AFTER,
103                     array(
104                         new Definition('Behat\Behat\Output\Node\EventListener\AST\ScenarioNodeListener', array(
105                             BackgroundTested::AFTER_SETUP,
106                             BackgroundTested::AFTER,
107                             new Reference('output.node.printer.pretty.scenario')
108                         )),
109                         new Definition('Behat\Behat\Output\Node\EventListener\AST\StepListener', array(
110                             new Reference('output.node.printer.pretty.step'),
111                             new Reference('output.node.printer.pretty.step_setup')
112                         )),
113                     )
114                 ),
115                 $this->proxySiblingEvents(
116                     ScenarioTested::BEFORE,
117                     ScenarioTested::AFTER,
118                     array(
119                         new Definition('Behat\Behat\Output\Node\EventListener\AST\ScenarioNodeListener', array(
120                             ScenarioTested::AFTER_SETUP,
121                             ScenarioTested::AFTER,
122                             new Reference('output.node.printer.pretty.scenario'),
123                             new Reference('output.node.printer.pretty.scenario_setup')
124                         )),
125                         new Definition('Behat\Behat\Output\Node\EventListener\AST\StepListener', array(
126                             new Reference('output.node.printer.pretty.step'),
127                             new Reference('output.node.printer.pretty.step_setup')
128                         )),
129                     )
130                 ),
131                 $this->proxySiblingEvents(
132                     OutlineTested::BEFORE,
133                     OutlineTested::AFTER,
134                     array(
135                         $this->proxyEventsIfParameterIsSet(
136                             'expand',
137                             false,
138                             new Definition('Behat\Behat\Output\Node\EventListener\AST\OutlineTableListener', array(
139                                 new Reference('output.node.printer.pretty.outline_table'),
140                                 new Reference('output.node.printer.pretty.example_row'),
141                                 new Reference('output.node.printer.pretty.example_setup'),
142                                 new Reference('output.node.printer.pretty.example_step_setup')
143                             ))
144                         ),
145                         $this->proxyEventsIfParameterIsSet(
146                             'expand',
147                             true,
148                             new Definition('Behat\Behat\Output\Node\EventListener\AST\OutlineListener', array(
149                                 new Reference('output.node.printer.pretty.outline'),
150                                 new Reference('output.node.printer.pretty.example'),
151                                 new Reference('output.node.printer.pretty.example_step'),
152                                 new Reference('output.node.printer.pretty.example_setup'),
153                                 new Reference('output.node.printer.pretty.example_step_setup')
154                             ))
155                         )
156                     )
157                 ),
158             )
159         ));
160         $container->setDefinition(self::ROOT_LISTENER_ID, $definition);
161     }
162
163     /**
164      * Loads formatter itself.
165      *
166      * @param ContainerBuilder $container
167      */
168     protected function loadFormatter(ContainerBuilder $container)
169     {
170         $definition = new Definition('Behat\Behat\Output\Statistics\TotalStatistics');
171         $container->setDefinition('output.pretty.statistics', $definition);
172
173         $definition = new Definition('Behat\Testwork\Output\NodeEventListeningFormatter', array(
174             'pretty',
175             'Prints the feature as is.',
176             array(
177                 'timer'     => true,
178                 'expand'    => false,
179                 'paths'     => true,
180                 'multiline' => true,
181             ),
182             $this->createOutputPrinterDefinition(),
183             new Definition('Behat\Testwork\Output\Node\EventListener\ChainEventListener', array(
184                     array(
185                         $this->rearrangeBackgroundEvents(
186                             new Reference(self::ROOT_LISTENER_ID)
187                         ),
188                         new Definition('Behat\Behat\Output\Node\EventListener\Statistics\StatisticsListener', array(
189                             new Reference('output.pretty.statistics'),
190                             new Reference('output.node.printer.pretty.statistics')
191                         )),
192                         new Definition('Behat\Behat\Output\Node\EventListener\Statistics\ScenarioStatsListener', array(
193                             new Reference('output.pretty.statistics')
194                         )),
195                         new Definition('Behat\Behat\Output\Node\EventListener\Statistics\StepStatsListener', array(
196                             new Reference('output.pretty.statistics'),
197                             new Reference(ExceptionExtension::PRESENTER_ID)
198                         )),
199                     )
200                 )
201             )
202         ));
203         $definition->addTag(OutputExtension::FORMATTER_TAG, array('priority' => 100));
204         $container->setDefinition(OutputExtension::FORMATTER_TAG . '.pretty', $definition);
205     }
206
207     /**
208      * Loads feature, scenario and step printers.
209      *
210      * @param ContainerBuilder $container
211      */
212     protected function loadCorePrinters(ContainerBuilder $container)
213     {
214         $definition = new Definition('Behat\Behat\Output\Node\Printer\Pretty\PrettyFeaturePrinter');
215         $container->setDefinition('output.node.printer.pretty.feature', $definition);
216
217         $definition = new Definition('Behat\Behat\Output\Node\Printer\Pretty\PrettyPathPrinter', array(
218             new Reference('output.node.printer.pretty.width_calculator'),
219             '%paths.base%'
220         ));
221         $container->setDefinition('output.node.printer.pretty.path', $definition);
222
223         $definition = new Definition('Behat\Behat\Output\Node\Printer\Pretty\PrettyScenarioPrinter', array(
224             new Reference('output.node.printer.pretty.path'),
225         ));
226         $container->setDefinition('output.node.printer.pretty.scenario', $definition);
227
228         $definition = new Definition('Behat\Behat\Output\Node\Printer\Pretty\PrettyStepPrinter', array(
229             new Reference('output.node.printer.pretty.step_text_painter'),
230             new Reference(self::RESULT_TO_STRING_CONVERTER_ID),
231             new Reference('output.node.printer.pretty.path'),
232             new Reference(ExceptionExtension::PRESENTER_ID)
233         ));
234         $container->setDefinition('output.node.printer.pretty.step', $definition);
235
236         $definition = new Definition('Behat\Behat\Output\Node\Printer\Pretty\PrettySkippedStepPrinter', array(
237             new Reference('output.node.printer.pretty.step_text_painter'),
238             new Reference(self::RESULT_TO_STRING_CONVERTER_ID),
239             new Reference('output.node.printer.pretty.path'),
240         ));
241         $container->setDefinition('output.node.printer.pretty.skipped_step', $definition);
242     }
243
244     /**
245      * Loads table outline printer.
246      *
247      * @param ContainerBuilder $container
248      */
249     protected function loadTableOutlinePrinter(ContainerBuilder $container)
250     {
251         $definition = new Definition('Behat\Behat\Output\Node\Printer\Pretty\PrettyOutlineTablePrinter', array(
252             new Reference('output.node.printer.pretty.scenario'),
253             new Reference('output.node.printer.pretty.skipped_step'),
254             new Reference(self::RESULT_TO_STRING_CONVERTER_ID)
255         ));
256         $container->setDefinition('output.node.printer.pretty.outline_table', $definition);
257
258         $definition = new Definition('Behat\Behat\Output\Node\Printer\Pretty\PrettyExampleRowPrinter', array(
259             new Reference(self::RESULT_TO_STRING_CONVERTER_ID),
260             new Reference(ExceptionExtension::PRESENTER_ID)
261         ));
262         $container->setDefinition('output.node.printer.pretty.example_row', $definition);
263     }
264
265     /**
266      * Loads expanded outline printer.
267      *
268      * @param ContainerBuilder $container
269      */
270     protected function loadExpandedOutlinePrinter(ContainerBuilder $container)
271     {
272         $definition = new Definition('Behat\Behat\Output\Node\Printer\Pretty\PrettyOutlinePrinter', array(
273             new Reference('output.node.printer.pretty.scenario'),
274             new Reference('output.node.printer.pretty.skipped_step'),
275             new Reference(self::RESULT_TO_STRING_CONVERTER_ID)
276         ));
277         $container->setDefinition('output.node.printer.pretty.outline', $definition);
278
279         $definition = new Definition('Behat\Behat\Output\Node\Printer\Pretty\PrettyExamplePrinter', array(
280             new Reference('output.node.printer.pretty.path'),
281         ));
282         $container->setDefinition('output.node.printer.pretty.example', $definition);
283
284         $definition = new Definition('Behat\Behat\Output\Node\Printer\Pretty\PrettyStepPrinter', array(
285             new Reference('output.node.printer.pretty.step_text_painter'),
286             new Reference(self::RESULT_TO_STRING_CONVERTER_ID),
287             new Reference('output.node.printer.pretty.path'),
288             new Reference(ExceptionExtension::PRESENTER_ID),
289             8
290         ));
291         $container->setDefinition('output.node.printer.pretty.example_step', $definition);
292     }
293
294     /**
295      * Loads hook printers.
296      *
297      * @param ContainerBuilder $container
298      */
299     protected function loadHookPrinters(ContainerBuilder $container)
300     {
301         $definition = new Definition('Behat\Behat\Output\Node\Printer\Pretty\PrettySetupPrinter', array(
302             new Reference(self::RESULT_TO_STRING_CONVERTER_ID),
303             new Reference(ExceptionExtension::PRESENTER_ID),
304             0,
305             true,
306             true
307         ));
308         $container->setDefinition('output.node.printer.pretty.suite_setup', $definition);
309
310         $definition = new Definition('Behat\Behat\Output\Node\Printer\Pretty\PrettySetupPrinter', array(
311             new Reference(self::RESULT_TO_STRING_CONVERTER_ID),
312             new Reference(ExceptionExtension::PRESENTER_ID),
313             0,
314             false,
315             true
316         ));
317         $container->setDefinition('output.node.printer.pretty.feature_setup', $definition);
318
319         $definition = new Definition('Behat\Behat\Output\Node\Printer\Pretty\PrettySetupPrinter', array(
320             new Reference(self::RESULT_TO_STRING_CONVERTER_ID),
321             new Reference(ExceptionExtension::PRESENTER_ID),
322             2
323         ));
324         $container->setDefinition('output.node.printer.pretty.scenario_setup', $definition);
325
326         $definition = new Definition('Behat\Behat\Output\Node\Printer\Pretty\PrettySetupPrinter', array(
327             new Reference(self::RESULT_TO_STRING_CONVERTER_ID),
328             new Reference(ExceptionExtension::PRESENTER_ID),
329             4
330         ));
331         $container->setDefinition('output.node.printer.pretty.step_setup', $definition);
332
333         $definition = new Definition('Behat\Behat\Output\Node\Printer\Pretty\PrettySetupPrinter', array(
334             new Reference(self::RESULT_TO_STRING_CONVERTER_ID),
335             new Reference(ExceptionExtension::PRESENTER_ID),
336             8
337         ));
338         $container->setDefinition('output.node.printer.pretty.example_step_setup', $definition);
339
340         $definition = new Definition('Behat\Behat\Output\Node\Printer\Pretty\PrettySetupPrinter', array(
341             new Reference(self::RESULT_TO_STRING_CONVERTER_ID),
342             new Reference(ExceptionExtension::PRESENTER_ID),
343             6
344         ));
345         $container->setDefinition('output.node.printer.pretty.example_setup', $definition);
346     }
347
348     /**
349      * Loads statistics printer.
350      *
351      * @param ContainerBuilder $container
352      */
353     protected function loadStatisticsPrinter(ContainerBuilder $container)
354     {
355         $definition = new Definition('Behat\Behat\Output\Node\Printer\CounterPrinter', array(
356             new Reference(self::RESULT_TO_STRING_CONVERTER_ID),
357             new Reference(TranslatorExtension::TRANSLATOR_ID),
358         ));
359         $container->setDefinition('output.node.printer.counter', $definition);
360
361         $definition = new Definition('Behat\Behat\Output\Node\Printer\ListPrinter', array(
362             new Reference(self::RESULT_TO_STRING_CONVERTER_ID),
363             new Reference(ExceptionExtension::PRESENTER_ID),
364             new Reference(TranslatorExtension::TRANSLATOR_ID),
365             '%paths.base%'
366         ));
367         $container->setDefinition('output.node.printer.list', $definition);
368
369         $definition = new Definition('Behat\Behat\Output\Node\Printer\Pretty\PrettyStatisticsPrinter', array(
370             new Reference('output.node.printer.counter'),
371             new Reference('output.node.printer.list')
372         ));
373         $container->setDefinition('output.node.printer.pretty.statistics', $definition);
374     }
375
376     /**
377      * Loads printer helpers.
378      *
379      * @param ContainerBuilder $container
380      */
381     protected function loadPrinterHelpers(ContainerBuilder $container)
382     {
383         $definition = new Definition('Behat\Behat\Output\Node\Printer\Helper\WidthCalculator');
384         $container->setDefinition('output.node.printer.pretty.width_calculator', $definition);
385
386         $definition = new Definition('Behat\Behat\Output\Node\Printer\Helper\StepTextPainter', array(
387             new Reference(DefinitionExtension::PATTERN_TRANSFORMER_ID),
388             new Reference(self::RESULT_TO_STRING_CONVERTER_ID)
389         ));
390         $container->setDefinition('output.node.printer.pretty.step_text_painter', $definition);
391
392         $definition = new Definition('Behat\Behat\Output\Node\Printer\Helper\ResultToStringConverter');
393         $container->setDefinition(self::RESULT_TO_STRING_CONVERTER_ID, $definition);
394     }
395
396     /**
397      * Creates output printer definition.
398      *
399      * @return Definition
400      */
401     protected function createOutputPrinterDefinition()
402     {
403         return new Definition('Behat\Testwork\Output\Printer\StreamOutputPrinter', array(
404             new Definition('Behat\Behat\Output\Printer\ConsoleOutputFactory'),
405         ));
406     }
407
408     /**
409      * Creates root listener definition.
410      *
411      * @param mixed $listener
412      *
413      * @return Definition
414      */
415     protected function rearrangeBackgroundEvents($listener)
416     {
417         return new Definition('Behat\Behat\Output\Node\EventListener\Flow\FirstBackgroundFiresFirstListener', array(
418             new Definition('Behat\Behat\Output\Node\EventListener\Flow\OnlyFirstBackgroundFiresListener', array(
419                 $listener
420             ))
421         ));
422     }
423
424     /**
425      * Creates contextual proxy listener.
426      *
427      * @param string       $beforeEventName
428      * @param string       $afterEventName
429      * @param Definition[] $listeners
430      *
431      * @return Definition
432      */
433     protected function proxySiblingEvents($beforeEventName, $afterEventName, array $listeners)
434     {
435         return new Definition('Behat\Behat\Output\Node\EventListener\Flow\FireOnlySiblingsListener',
436             array(
437                 $beforeEventName,
438                 $afterEventName,
439                 new Definition('Behat\Testwork\Output\Node\EventListener\ChainEventListener', array($listeners))
440             )
441         );
442     }
443
444     /**
445      * Creates contextual proxy listener.
446      *
447      * @param string $name
448      * @param mixed  $value
449      * @param mixed  $listener
450      *
451      * @return Definition
452      */
453     protected function proxyEventsIfParameterIsSet($name, $value, Definition $listener)
454     {
455         return new Definition('Behat\Testwork\Output\Node\EventListener\Flow\FireOnlyIfFormatterParameterListener',
456             array($name, $value, $listener)
457         );
458     }
459
460     /**
461      * Processes all registered pretty formatter node listener wrappers.
462      *
463      * @param ContainerBuilder $container
464      */
465     protected function processListenerWrappers(ContainerBuilder $container)
466     {
467         $this->processor->processWrapperServices($container, self::ROOT_LISTENER_ID, self::ROOT_LISTENER_WRAPPER_TAG);
468     }
469 }