1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Doyo\PhpSpec\CodeCoverage\Listener; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Doyo\Bridge\CodeCoverage\ProcessorInterface; |
8
|
|
|
use Doyo\Bridge\CodeCoverage\TestCase; |
9
|
|
|
use Doyo\PhpSpec\CodeCoverage\Event\CoverageEvent; |
10
|
|
|
use Doyo\Symfony\Bridge\EventDispatcher\EventDispatcher; |
11
|
|
|
use Doyo\Symfony\Bridge\EventDispatcher\EventDispatcherInterface; |
12
|
|
|
use PhpSpec\Console\ConsoleIO; |
13
|
|
|
use PhpSpec\Event\ExampleEvent; |
14
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
15
|
|
|
|
16
|
|
|
class CoverageListener implements EventSubscriberInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ProcessorInterface $processor |
20
|
|
|
*/ |
21
|
|
|
private $processor; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ConsoleIO |
25
|
|
|
*/ |
26
|
|
|
private $consoleIO; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var EventDispatcherInterface |
30
|
|
|
*/ |
31
|
|
|
private $dispatcher; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
private $canCollectCoverage; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* CoverageListener constructor. |
40
|
|
|
* @param ProcessorInterface $processor |
41
|
|
|
* @param ConsoleIO $consoleIO |
42
|
|
|
*/ |
43
|
6 |
|
public function __construct( |
44
|
|
|
EventDispatcher $dispatcher, |
45
|
|
|
ProcessorInterface $processor, |
46
|
|
|
ConsoleIO $consoleIO, |
47
|
|
|
bool $canCollectCoverage |
48
|
|
|
) |
49
|
|
|
{ |
50
|
6 |
|
$this->dispatcher = $dispatcher; |
|
|
|
|
51
|
6 |
|
$this->processor = $processor; |
52
|
6 |
|
$this->consoleIO = $consoleIO; |
53
|
6 |
|
$this->canCollectCoverage = $canCollectCoverage; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
public static function getSubscribedEvents() |
57
|
|
|
{ |
58
|
|
|
return [ |
59
|
1 |
|
'beforeExample' => ['beforeExample', -10], |
60
|
|
|
'afterExample' => ['afterExample', -10], |
61
|
|
|
'afterSuite' => ['afterSuite', -10] |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
public function beforeExample(ExampleEvent $suiteEvent) |
66
|
|
|
{ |
67
|
1 |
|
if(!$this->canCollectCoverage){ |
68
|
|
|
return; |
69
|
|
|
} |
70
|
1 |
|
$example = $suiteEvent->getExample(); |
71
|
1 |
|
$processor = $this->processor; |
72
|
|
|
|
73
|
1 |
|
$name = strtr('%spec%::%example%', [ |
74
|
1 |
|
'%spec%' => $example->getSpecification()->getTitle(), |
75
|
1 |
|
'%example%' => $example->getFunctionReflection()->getName(), |
76
|
|
|
]); |
77
|
1 |
|
$testCase = new TestCase($name); |
78
|
1 |
|
$processor->setCurrentTestCase($testCase); |
79
|
1 |
|
$processor->start($testCase); |
80
|
|
|
} |
81
|
|
|
|
82
|
15 |
|
public function afterExample(ExampleEvent $exampleEvent) |
83
|
|
|
{ |
84
|
15 |
|
if(!$this->canCollectCoverage){ |
85
|
|
|
return; |
86
|
|
|
} |
87
|
15 |
|
$processor = $this->processor; |
88
|
15 |
|
$result = $exampleEvent->getResult(); |
89
|
15 |
|
$testCase = $processor->getCurrentTestCase(); |
90
|
|
|
|
91
|
|
|
$map = [ |
92
|
15 |
|
ExampleEvent::PASSED => TestCase::RESULT_PASSED, |
93
|
15 |
|
ExampleEvent::SKIPPED => TestCase::RESULT_SKIPPED, |
94
|
15 |
|
ExampleEvent::FAILED => TestCase::RESULT_FAILED, |
95
|
15 |
|
ExampleEvent::BROKEN => TestCase::RESULT_ERROR, |
96
|
15 |
|
ExampleEvent::PENDING => TestCase::RESULT_SKIPPED, |
97
|
|
|
]; |
98
|
|
|
|
99
|
15 |
|
$result = $map[$result]; |
100
|
15 |
|
$testCase->setResult($result); |
101
|
15 |
|
$processor->addTestCase($testCase); |
102
|
15 |
|
$processor->stop(); |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
public function afterSuite() |
106
|
|
|
{ |
107
|
1 |
|
if(!$this->canCollectCoverage){ |
108
|
|
|
return; |
109
|
|
|
} |
110
|
1 |
|
$processor = $this->processor; |
111
|
1 |
|
$consoleIO = $this->consoleIO; |
112
|
1 |
|
$dispatcher = $this->dispatcher; |
113
|
1 |
|
$event = new CoverageEvent($processor, $consoleIO); |
114
|
|
|
|
115
|
1 |
|
$dispatcher->dispatch($event, CoverageEvent::REPORT); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..