1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the doyo/code-coverage project. |
5
|
|
|
* |
6
|
|
|
* (c) Anthonius Munthi <https://itstoni.com> |
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\CodeCoverage\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 Behat\Testwork\Tester\Result\TestResults; |
22
|
|
|
use Doyo\Bridge\CodeCoverage\CodeCoverageInterface; |
23
|
|
|
use Doyo\Bridge\CodeCoverage\TestCase; |
24
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
25
|
|
|
|
26
|
|
|
class CoverageListener implements EventSubscriberInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
private $enabled = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var CodeCoverageInterface |
35
|
|
|
*/ |
36
|
|
|
private $coverage; |
37
|
|
|
|
38
|
5 |
|
public function __construct( |
39
|
|
|
CodeCoverageInterface $coverage, |
40
|
|
|
bool $enabled |
41
|
|
|
) { |
42
|
5 |
|
$this->coverage = $coverage; |
43
|
5 |
|
$this->enabled = $enabled; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function getSubscribedEvents() |
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
|
|
ExerciseCompleted::BEFORE => 'refresh', |
50
|
|
|
ScenarioTested::BEFORE => 'start', |
51
|
|
|
ExampleTested::BEFORE => 'start', |
52
|
|
|
ScenarioTested::AFTER => 'stop', |
53
|
|
|
ExampleTested::AFTER => 'stop', |
54
|
|
|
ExerciseCompleted::AFTER => 'complete', |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
public function refresh() |
59
|
|
|
{ |
60
|
1 |
|
if (!$this->enabled) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
$this->coverage->refresh(); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
public function start(ScenarioTested $scenarioTested) |
68
|
|
|
{ |
69
|
1 |
|
if (!$this->enabled) { |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
$scenario = $scenarioTested->getScenario(); |
74
|
1 |
|
$id = $scenarioTested->getFeature()->getFile().':'.$scenario->getLine(); |
75
|
1 |
|
$testCase = new TestCase($id); |
76
|
|
|
|
77
|
1 |
|
$this->coverage->start($testCase); |
78
|
|
|
} |
79
|
|
|
|
80
|
17 |
|
public function stop(AfterTested $tested) |
81
|
|
|
{ |
82
|
17 |
|
if (!$this->enabled || null === $tested->getTestResult()) { |
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$map = [ |
87
|
17 |
|
TestResult::PASSED => TestCase::RESULT_PASSED, |
88
|
17 |
|
TestResult::FAILED => TestCase::RESULT_FAILED, |
89
|
17 |
|
TestResult::SKIPPED => TestCase::RESULT_SKIPPED, |
90
|
17 |
|
TestResult::PENDING => TestCase::RESULT_SKIPPED, |
91
|
17 |
|
TestResults::NO_TESTS => TestCase::RESULT_SKIPPED, |
92
|
|
|
]; |
93
|
|
|
|
94
|
17 |
|
$result = $map[$tested->getTestResult()->getResultCode()]; |
95
|
17 |
|
if (null !== $result) { |
|
|
|
|
96
|
17 |
|
$coverage = $this->coverage; |
97
|
17 |
|
$coverage->setResult($result); |
98
|
17 |
|
$coverage->stop(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
public function complete() |
103
|
|
|
{ |
104
|
1 |
|
if (!$this->enabled) { |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
$this->coverage->complete(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|