1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the friends-of-phpspec/phpspec-code-coverage package. |
5
|
|
|
* |
6
|
|
|
* @author ek9 <[email protected]> |
7
|
|
|
* @license MIT |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please see the LICENSE file |
10
|
|
|
* that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace FriendsOfPhpSpec\PhpSpec\CodeCoverage\Listener; |
16
|
|
|
|
17
|
|
|
use PhpSpec\Console\ConsoleIO; |
18
|
|
|
use PhpSpec\Event\SuiteEvent; |
19
|
|
|
use SebastianBergmann\CodeCoverage\CodeCoverage; |
20
|
|
|
use SebastianBergmann\CodeCoverage\Report; |
21
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Henrik Bjornskov |
25
|
|
|
*/ |
26
|
|
|
class CodeCoverageListener implements EventSubscriberInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var CodeCoverage |
30
|
|
|
*/ |
31
|
|
|
private $coverage; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ConsoleIO |
35
|
|
|
*/ |
36
|
|
|
private $io; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private $options; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private $reports; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
private $skipCoverage; |
52
|
|
|
|
53
|
|
|
public function __construct(ConsoleIO $io, CodeCoverage $coverage, array $reports, bool $skipCoverage = false) |
54
|
1 |
|
{ |
55
|
|
|
$this->io = $io; |
56
|
1 |
|
$this->coverage = $coverage; |
57
|
1 |
|
$this->reports = $reports; |
58
|
1 |
|
$this->options = [ |
59
|
1 |
|
'whitelist' => ['src', 'lib'], |
60
|
|
|
'blacklist' => ['test', 'vendor', 'spec'], |
61
|
|
|
'whitelist_files' => [], |
62
|
|
|
'blacklist_files' => [], |
63
|
|
|
'output' => ['html' => 'coverage'], |
64
|
|
|
'format' => ['html'], |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
$this->skipCoverage = $skipCoverage; |
68
|
1 |
|
} |
69
|
1 |
|
|
70
|
|
|
public function afterSuite(SuiteEvent $event): void |
|
|
|
|
71
|
6 |
|
{ |
72
|
|
|
if ($this->skipCoverage) { |
73
|
6 |
|
if ($this->io->isVerbose()) { |
74
|
|
|
$this->io->writeln('Skipping code coverage generation'); |
75
|
|
|
} |
76
|
|
|
|
77
|
6 |
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($this->io->isVerbose()) { |
81
|
|
|
$this->io->writeln(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
foreach ($this->reports as $format => $report) { |
85
|
|
|
if ($this->io->isVerbose()) { |
86
|
|
|
$this->io->writeln(sprintf('Generating code coverage report in %s format ...', $format)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($report instanceof Report\Text) { |
90
|
|
|
$this->io->writeln( |
91
|
|
|
$report->process($this->coverage, $this->io->isDecorated()) |
92
|
|
|
); |
93
|
|
|
} else { |
94
|
|
|
$report->process($this->coverage, $this->options['output'][$format]); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Note: We use array_map() instead of array_walk() because the latter expects |
101
|
|
|
* the callback to take the value as the first and the index as the seconds parameter. |
102
|
|
|
* |
103
|
|
|
* @param SuiteEvent $event |
104
|
|
|
*/ |
105
|
|
|
public function beforeSuite(SuiteEvent $event): void |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
if ($this->skipCoverage) { |
108
|
|
|
return; |
109
|
6 |
|
} |
110
|
|
|
|
111
|
|
|
$filter = $this->coverage->filter(); |
112
|
|
|
|
113
|
|
|
foreach ($this->options['whitelist'] as $option) { |
114
|
|
|
$filter->addDirectoryToWhitelist($option); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
foreach ($this->options['blacklist'] as $option) { |
118
|
|
|
$filter->removeDirectoryFromWhitelist($option); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
foreach ($this->options['whitelist_files'] as $option) { |
122
|
|
|
$filter->addFilesToWhitelist($option); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
foreach ($this->options['blacklist_files'] as $option) { |
126
|
|
|
$filter->removeFileFromWhitelist($option); |
127
|
|
|
} |
128
|
|
|
} |
129
|
6 |
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
|
|
public static function getSubscribedEvents(): array |
134
|
|
|
{ |
135
|
|
|
return [ |
136
|
|
|
'beforeSuite' => ['beforeSuite', -10], |
137
|
|
|
'afterSuite' => ['afterSuite', -10], |
138
|
|
|
]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function setOptions(array $options): void |
142
|
|
|
{ |
143
|
|
|
$this->options = $options + $this->options; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.