Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / EventDispatcher / Cli / StopOnFailureController.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\EventDispatcher\Cli;
12
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;
29
30 /**
31  * Stops tests on first scenario failure.
32  *
33  * @author Konstantin Kudryashov <ever.zet@gmail.com>
34  */
35 final class StopOnFailureController implements Controller
36 {
37     /**
38      * @var EventDispatcherInterface
39      */
40     private $eventDispatcher;
41
42     /**
43      * @var ResultInterpretation
44      */
45     private $resultInterpretation;
46
47     /**
48      * Initializes controller.
49      *
50      * @param EventDispatcherInterface $eventDispatcher
51      */
52     public function __construct(EventDispatcherInterface $eventDispatcher)
53     {
54         $this->eventDispatcher = $eventDispatcher;
55         $this->resultInterpretation = new SoftInterpretation();
56     }
57
58     /**
59      * Configures command to be executable by the controller.
60      *
61      * @param Command $command
62      */
63     public function configure(Command $command)
64     {
65         $command->addOption('--stop-on-failure', null, InputOption::VALUE_NONE,
66             'Stop processing on first failed scenario.'
67         );
68     }
69
70     /**
71      * Executes controller.
72      *
73      * @param InputInterface  $input
74      * @param OutputInterface $output
75      *
76      * @return null|integer
77      */
78     public function execute(InputInterface $input, OutputInterface $output)
79     {
80         if (!$input->getOption('stop-on-failure')) {
81             return null;
82         }
83
84         if ($input->getOption('strict')) {
85             $this->resultInterpretation = new StrictInterpretation();
86         }
87
88         $this->eventDispatcher->addListener(ScenarioTested::AFTER, array($this, 'exitOnFailure'), -100);
89         $this->eventDispatcher->addListener(ExampleTested::AFTER, array($this, 'exitOnFailure'), -100);
90     }
91
92     /**
93      * Exits if scenario is a failure and if stopper is enabled.
94      *
95      * @param AfterScenarioTested $event
96      */
97     public function exitOnFailure(AfterScenarioTested $event)
98     {
99         if (!$this->resultInterpretation->isFailure($event->getTestResult())) {
100             return;
101         }
102
103         $this->eventDispatcher->dispatch(SuiteTested::AFTER, new AfterSuiteAborted($event->getEnvironment()));
104         $this->eventDispatcher->dispatch(ExerciseCompleted::AFTER, new AfterExerciseAborted());
105
106         exit(1);
107     }
108 }