Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Snippet / Cli / SnippetsController.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\Snippet\Cli;
12
13 use Behat\Behat\EventDispatcher\Event\AfterStepTested;
14 use Behat\Behat\EventDispatcher\Event\StepTested;
15 use Behat\Behat\Snippet\Printer\ConsoleSnippetPrinter;
16 use Behat\Behat\Snippet\SnippetRegistry;
17 use Behat\Behat\Snippet\SnippetWriter;
18 use Behat\Behat\Tester\Result\StepResult;
19 use Behat\Testwork\Cli\Controller;
20 use Behat\Testwork\EventDispatcher\Event\ExerciseCompleted;
21 use Symfony\Component\Console\Command\Command;
22 use Symfony\Component\Console\Input\InputInterface;
23 use Symfony\Component\Console\Input\InputOption;
24 use Symfony\Component\Console\Output\OutputInterface;
25 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
26
27 /**
28  * Appends and prints snippets.
29  *
30  * @author Konstantin Kudryashov <ever.zet@gmail.com>
31  */
32 final class SnippetsController implements Controller
33 {
34     /**
35      * @var SnippetRegistry
36      */
37     private $registry;
38     /**
39      * @var SnippetWriter
40      */
41     private $writer;
42     /**
43      * @var ConsoleSnippetPrinter
44      */
45     private $printer;
46     /**
47      * @var EventDispatcherInterface
48      */
49     private $eventDispatcher;
50     /**
51      * @var OutputInterface
52      */
53     private $output;
54
55     /**
56      * Initializes controller.
57      *
58      * @param SnippetRegistry          $registry
59      * @param SnippetWriter            $writer
60      * @param ConsoleSnippetPrinter    $printer
61      * @param EventDispatcherInterface $eventDispatcher
62      */
63     public function __construct(
64         SnippetRegistry $registry,
65         SnippetWriter $writer,
66         ConsoleSnippetPrinter $printer,
67         EventDispatcherInterface $eventDispatcher
68     ) {
69         $this->registry = $registry;
70         $this->writer = $writer;
71         $this->printer = $printer;
72         $this->eventDispatcher = $eventDispatcher;
73     }
74
75     /**
76      * Configures command to be executable by the controller.
77      *
78      * @param Command $command
79      */
80     public function configure(Command $command)
81     {
82         $command
83             ->addOption(
84                 '--append-snippets', null, InputOption::VALUE_NONE,
85                 "Appends snippets for undefined steps into main context."
86             )
87             ->addOption(
88                 '--no-snippets', null, InputOption::VALUE_NONE,
89                 "Do not print snippets for undefined steps after stats."
90             );
91     }
92
93     /**
94      * Executes controller.
95      *
96      * @param InputInterface  $input
97      * @param OutputInterface $output
98      *
99      * @return null|integer
100      */
101     public function execute(InputInterface $input, OutputInterface $output)
102     {
103         $this->eventDispatcher->addListener(StepTested::AFTER, array($this, 'registerUndefinedStep'), -999);
104         $this->output = $output;
105
106         if ($input->getOption('append-snippets')) {
107             $this->eventDispatcher->addListener(ExerciseCompleted::AFTER, array($this, 'appendAllSnippets'), -999);
108         }
109
110         if (!$input->getOption('no-snippets') && !$input->getOption('append-snippets')) {
111             $this->eventDispatcher->addListener(ExerciseCompleted::AFTER, array($this, 'printAllSnippets'), -999);
112         }
113
114         if (!$input->getOption('no-snippets')) {
115             $this->eventDispatcher->addListener(ExerciseCompleted::AFTER, array($this, 'printUndefinedSteps'), -995);
116         }
117     }
118
119     /**
120      * Registers undefined step.
121      *
122      * @param AfterStepTested $event
123      */
124     public function registerUndefinedStep(AfterStepTested $event)
125     {
126         if (StepResult::UNDEFINED === $event->getTestResult()->getResultCode()) {
127             $this->registry->registerUndefinedStep($event->getEnvironment(), $event->getStep());
128         }
129     }
130
131     /**
132      * Appends all snippets to corresponding targets.
133      */
134     public function appendAllSnippets()
135     {
136         $snippets = $this->registry->getSnippets();
137         count($snippets) && $this->output->writeln('');
138
139         $this->writer->appendSnippets($snippets);
140     }
141
142     /**
143      * Prints all snippets.
144      */
145     public function printAllSnippets()
146     {
147         $snippets = $this->registry->getSnippets();
148         count($snippets) && $this->output->writeln('');
149
150         $this->writer->printSnippets($this->printer, $snippets);
151     }
152
153     /**
154      * Prints all undefined steps.
155      */
156     public function printUndefinedSteps()
157     {
158         $undefined = $this->registry->getUndefinedSteps();
159         count($undefined) && $this->output->writeln('');
160
161         $this->writer->printUndefinedSteps($this->printer, $undefined);
162     }
163 }