1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the doyo/behat-code-coverage project. |
5
|
|
|
* |
6
|
|
|
* (c) Anthonius Munthi <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Doyo\Behat\Coverage\Listener; |
15
|
|
|
|
16
|
|
|
use Behat\Behat\EventDispatcher\Event\ExampleTested; |
17
|
|
|
use Behat\Behat\EventDispatcher\Event\ScenarioTested; |
18
|
|
|
use Behat\Testwork\EventDispatcher\Event\AfterTested; |
19
|
|
|
use Behat\Testwork\EventDispatcher\Event\ExerciseCompleted; |
20
|
|
|
use Behat\Testwork\Tester\Result\TestResult; |
21
|
|
|
use Doyo\Behat\Coverage\Console\ConsoleIO; |
22
|
|
|
use Doyo\Behat\Coverage\Event\CoverageEvent; |
23
|
|
|
use Doyo\Behat\Coverage\Event\ReportEvent; |
24
|
|
|
use Doyo\Bridge\CodeCoverage\ProcessorInterface; |
25
|
|
|
use Doyo\Bridge\CodeCoverage\TestCase; |
26
|
|
|
use Doyo\Symfony\Bridge\EventDispatcher\EventDispatcher; |
27
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
28
|
|
|
|
29
|
|
|
class BehatEventListener implements EventSubscriberInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var EventDispatcher |
33
|
|
|
*/ |
34
|
|
|
private $dispatcher; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ProcessorInterface |
38
|
|
|
*/ |
39
|
|
|
private $processor; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var ConsoleIO |
43
|
|
|
*/ |
44
|
|
|
private $consoleIO; |
45
|
|
|
|
46
|
10 |
|
public function __construct( |
47
|
|
|
EventDispatcher $dispatcher, |
48
|
|
|
ProcessorInterface $processor, |
49
|
|
|
ConsoleIO $consoleIO |
50
|
|
|
) { |
51
|
10 |
|
$this->dispatcher = $dispatcher; |
52
|
10 |
|
$this->processor = $processor; |
53
|
10 |
|
$this->consoleIO = $consoleIO; |
54
|
|
|
} |
55
|
|
|
|
56
|
4 |
|
public static function getSubscribedEvents() |
57
|
|
|
{ |
58
|
|
|
return [ |
59
|
4 |
|
ExerciseCompleted::BEFORE => 'refreshCoverage', |
60
|
1 |
|
ScenarioTested::BEFORE => 'startCoverage', |
61
|
1 |
|
ExampleTested::BEFORE => 'startCoverage', |
62
|
1 |
|
ScenarioTested::AFTER => 'stopCoverage', |
63
|
1 |
|
ExampleTested::AFTER => 'stopCoverage', |
64
|
1 |
|
ExerciseCompleted::AFTER => 'generateReport', |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
public function refreshCoverage() |
69
|
|
|
{ |
70
|
4 |
|
$dispatcher = $this->dispatcher; |
71
|
4 |
|
$event = new CoverageEvent($this->processor, $this->consoleIO); |
72
|
|
|
|
73
|
4 |
|
$this->processor->clear(); |
74
|
4 |
|
$dispatcher->dispatch($event, CoverageEvent::BEFORE_REFRESH); |
75
|
4 |
|
$dispatcher->dispatch($event, CoverageEvent::REFRESH); |
76
|
|
|
} |
77
|
|
|
|
78
|
8 |
|
public function startCoverage($scope) |
79
|
|
|
{ |
80
|
4 |
|
$scenario = $scope->getScenario(); |
81
|
4 |
|
$id = $scope->getFeature()->getFile().':'.$scenario->getLine(); |
82
|
4 |
|
$dispatcher = $this->dispatcher; |
83
|
4 |
|
$testCase = new TestCase($id); |
84
|
4 |
|
$processor = $this->processor; |
85
|
4 |
|
$consoleIO = $this->consoleIO; |
86
|
|
|
|
87
|
4 |
|
$coverageEvent = new CoverageEvent($processor, $consoleIO, $testCase); |
88
|
4 |
|
$processor->start($testCase); |
89
|
8 |
|
$dispatcher->dispatch($coverageEvent, CoverageEvent::BEFORE_START); |
90
|
8 |
|
$dispatcher->dispatch($coverageEvent, CoverageEvent::START); |
91
|
|
|
} |
92
|
|
|
|
93
|
8 |
|
public function stopCoverage(AfterTested $testedEvent) |
94
|
|
|
{ |
95
|
8 |
|
$dispatcher = $this->dispatcher; |
96
|
8 |
|
$processor = $this->processor; |
97
|
8 |
|
$testCase = $processor->getCurrentTestCase(); |
98
|
8 |
|
$result = $testedEvent->getTestResult(); |
99
|
8 |
|
$consoleIO = $this->consoleIO; |
100
|
|
|
$map = [ |
101
|
8 |
|
TestResult::PASSED => TestCase::RESULT_PASSED, |
102
|
8 |
|
TestResult::FAILED => TestCase::RESULT_FAILED, |
103
|
8 |
|
TestResult::SKIPPED => TestCase::RESULT_SKIPPED, |
104
|
8 |
|
TestResult::PENDING => TestCase::RESULT_SKIPPED, |
105
|
|
|
]; |
106
|
8 |
|
$result = $map[$result->getResultCode()]; |
107
|
|
|
|
108
|
8 |
|
$testCase->setResult($result); |
109
|
8 |
|
$processor->stop(); |
110
|
4 |
|
$coverageEvent = new CoverageEvent($processor, $consoleIO, $testCase); |
111
|
4 |
|
$dispatcher->dispatch($coverageEvent, CoverageEvent::BEFORE_STOP); |
112
|
4 |
|
$dispatcher->dispatch($coverageEvent, CoverageEvent::STOP); |
113
|
|
|
} |
114
|
|
|
|
115
|
4 |
|
public function generateReport() |
116
|
|
|
{ |
117
|
4 |
|
$dispatcher = $this->dispatcher; |
118
|
4 |
|
$processor = $this->processor; |
119
|
4 |
|
$consoleIO = $this->consoleIO; |
120
|
|
|
|
121
|
4 |
|
$consoleIO->setHasError(false); |
122
|
|
|
|
123
|
4 |
|
$coverageEvent = new CoverageEvent($processor, $consoleIO); |
124
|
4 |
|
$dispatcher->dispatch($coverageEvent, CoverageEvent::COMPLETED); |
125
|
4 |
|
$processor->complete(); |
126
|
|
|
|
127
|
4 |
|
$reportEvent = new ReportEvent($processor, $consoleIO); |
128
|
4 |
|
$dispatcher->dispatch($reportEvent, ReportEvent::BEFORE_PROCESS); |
129
|
4 |
|
$dispatcher->dispatch($reportEvent, ReportEvent::PROCESS); |
130
|
4 |
|
$dispatcher->dispatch($reportEvent, ReportEvent::AFTER_PROCESS); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|