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\EventDispatcher\Cli;
13 use Behat\Behat\EventDispatcher\Event\AfterScenarioTested;
14 use Behat\Behat\EventDispatcher\Event\ExampleTested;
15 use Behat\Behat\EventDispatcher\Event\ScenarioTested;
16 use Behat\Testwork\Cli\Controller;
17 use Behat\Testwork\EventDispatcher\Event\AfterExerciseAborted;
18 use Behat\Testwork\EventDispatcher\Event\AfterSuiteAborted;
19 use Behat\Testwork\EventDispatcher\Event\ExerciseCompleted;
20 use Behat\Testwork\EventDispatcher\Event\SuiteTested;
21 use Behat\Testwork\Tester\Result\Interpretation\ResultInterpretation;
22 use Behat\Testwork\Tester\Result\Interpretation\SoftInterpretation;
23 use Behat\Testwork\Tester\Result\Interpretation\StrictInterpretation;
24 use Symfony\Component\Console\Command\Command;
25 use Symfony\Component\Console\Input\InputInterface;
26 use Symfony\Component\Console\Input\InputOption;
27 use Symfony\Component\Console\Output\OutputInterface;
28 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
31 * Stops tests on first scenario failure.
33 * @author Konstantin Kudryashov <ever.zet@gmail.com>
35 final class StopOnFailureController implements Controller
38 * @var EventDispatcherInterface
40 private $eventDispatcher;
43 * @var ResultInterpretation
45 private $resultInterpretation;
48 * Initializes controller.
50 * @param EventDispatcherInterface $eventDispatcher
52 public function __construct(EventDispatcherInterface $eventDispatcher)
54 $this->eventDispatcher = $eventDispatcher;
55 $this->resultInterpretation = new SoftInterpretation();
59 * Configures command to be executable by the controller.
61 * @param Command $command
63 public function configure(Command $command)
65 $command->addOption('--stop-on-failure', null, InputOption::VALUE_NONE,
66 'Stop processing on first failed scenario.'
71 * Executes controller.
73 * @param InputInterface $input
74 * @param OutputInterface $output
76 * @return null|integer
78 public function execute(InputInterface $input, OutputInterface $output)
80 if (!$input->getOption('stop-on-failure')) {
84 if ($input->getOption('strict')) {
85 $this->resultInterpretation = new StrictInterpretation();
88 $this->eventDispatcher->addListener(ScenarioTested::AFTER, array($this, 'exitOnFailure'), -100);
89 $this->eventDispatcher->addListener(ExampleTested::AFTER, array($this, 'exitOnFailure'), -100);
93 * Exits if scenario is a failure and if stopper is enabled.
95 * @param AfterScenarioTested $event
97 public function exitOnFailure(AfterScenarioTested $event)
99 if (!$this->resultInterpretation->isFailure($event->getTestResult())) {
103 $this->eventDispatcher->dispatch(SuiteTested::AFTER, new AfterSuiteAborted($event->getEnvironment()));
104 $this->eventDispatcher->dispatch(ExerciseCompleted::AFTER, new AfterExerciseAborted());