4 * This file is part of the Behat.
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\Behat\Snippet\Cli;
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;
28 * Appends and prints snippets.
30 * @author Konstantin Kudryashov <ever.zet@gmail.com>
32 final class SnippetsController implements Controller
35 * @var SnippetRegistry
43 * @var ConsoleSnippetPrinter
47 * @var EventDispatcherInterface
49 private $eventDispatcher;
51 * @var OutputInterface
56 * Initializes controller.
58 * @param SnippetRegistry $registry
59 * @param SnippetWriter $writer
60 * @param ConsoleSnippetPrinter $printer
61 * @param EventDispatcherInterface $eventDispatcher
63 public function __construct(
64 SnippetRegistry $registry,
65 SnippetWriter $writer,
66 ConsoleSnippetPrinter $printer,
67 EventDispatcherInterface $eventDispatcher
69 $this->registry = $registry;
70 $this->writer = $writer;
71 $this->printer = $printer;
72 $this->eventDispatcher = $eventDispatcher;
76 * Configures command to be executable by the controller.
78 * @param Command $command
80 public function configure(Command $command)
84 '--append-snippets', null, InputOption::VALUE_NONE,
85 "Appends snippets for undefined steps into main context."
88 '--no-snippets', null, InputOption::VALUE_NONE,
89 "Do not print snippets for undefined steps after stats."
94 * Executes controller.
96 * @param InputInterface $input
97 * @param OutputInterface $output
99 * @return null|integer
101 public function execute(InputInterface $input, OutputInterface $output)
103 $this->eventDispatcher->addListener(StepTested::AFTER, array($this, 'registerUndefinedStep'), -999);
104 $this->output = $output;
106 if ($input->getOption('append-snippets')) {
107 $this->eventDispatcher->addListener(ExerciseCompleted::AFTER, array($this, 'appendAllSnippets'), -999);
110 if (!$input->getOption('no-snippets') && !$input->getOption('append-snippets')) {
111 $this->eventDispatcher->addListener(ExerciseCompleted::AFTER, array($this, 'printAllSnippets'), -999);
114 if (!$input->getOption('no-snippets')) {
115 $this->eventDispatcher->addListener(ExerciseCompleted::AFTER, array($this, 'printUndefinedSteps'), -995);
120 * Registers undefined step.
122 * @param AfterStepTested $event
124 public function registerUndefinedStep(AfterStepTested $event)
126 if (StepResult::UNDEFINED === $event->getTestResult()->getResultCode()) {
127 $this->registry->registerUndefinedStep($event->getEnvironment(), $event->getStep());
132 * Appends all snippets to corresponding targets.
134 public function appendAllSnippets()
136 $snippets = $this->registry->getSnippets();
137 count($snippets) && $this->output->writeln('');
139 $this->writer->appendSnippets($snippets);
143 * Prints all snippets.
145 public function printAllSnippets()
147 $snippets = $this->registry->getSnippets();
148 count($snippets) && $this->output->writeln('');
150 $this->writer->printSnippets($this->printer, $snippets);
154 * Prints all undefined steps.
156 public function printUndefinedSteps()
158 $undefined = $this->registry->getUndefinedSteps();
159 count($undefined) && $this->output->writeln('');
161 $this->writer->printUndefinedSteps($this->printer, $undefined);