1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Behat Code Coverage |
4
|
|
|
*/ |
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace DVDoug\Behat\CodeCoverage; |
8
|
|
|
|
9
|
|
|
use Behat\Testwork\Cli\ServiceContainer\CliExtension; |
10
|
|
|
use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface; |
11
|
|
|
use Behat\Testwork\ServiceContainer\ExtensionManager; |
12
|
|
|
use Composer\InstalledVersions; |
13
|
|
|
use Composer\Semver\VersionParser; |
14
|
|
|
use DVDoug\Behat\CodeCoverage\Subscriber\EventSubscriber; |
15
|
|
|
use SebastianBergmann\CodeCoverage\CodeCoverage; |
16
|
|
|
use SebastianBergmann\CodeCoverage\Driver\Selector; |
17
|
|
|
use SebastianBergmann\CodeCoverage\Driver\Xdebug2NotEnabledException; |
|
|
|
|
18
|
|
|
use SebastianBergmann\CodeCoverage\Driver\Xdebug3NotEnabledException; |
|
|
|
|
19
|
|
|
use SebastianBergmann\CodeCoverage\Driver\XdebugNotAvailableException; |
20
|
|
|
use SebastianBergmann\CodeCoverage\Driver\XdebugNotEnabledException; |
21
|
|
|
use SebastianBergmann\CodeCoverage\Filter; |
22
|
|
|
use SebastianBergmann\CodeCoverage\NoCodeCoverageDriverAvailableException; |
23
|
|
|
use SebastianBergmann\CodeCoverage\NoCodeCoverageDriverWithPathCoverageSupportAvailableException; |
24
|
|
|
use SebastianBergmann\FileIterator\Facade as FileIteratorFacade; |
25
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
26
|
|
|
use Symfony\Component\Config\FileLocator; |
27
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
28
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
29
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
|
|
|
30
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
|
|
|
31
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
32
|
|
|
|
33
|
|
|
use function sprintf; |
34
|
|
|
use function sys_get_temp_dir; |
35
|
|
|
|
36
|
|
|
class Extension implements ExtensionInterface |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
30 |
|
public function initialize(ExtensionManager $extensionManager): void |
42
|
|
|
{ |
43
|
30 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
36 |
|
public function load(ContainerBuilder $container, array $config): void |
49
|
|
|
{ |
50
|
36 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/Resources/config')); |
51
|
|
|
|
52
|
36 |
|
$servicesFile = 'services.xml'; |
53
|
36 |
|
$loader->load($servicesFile); |
54
|
|
|
|
55
|
36 |
|
$container->setParameter('behat.code_coverage.config.filter', $config['filter']); |
56
|
36 |
|
$container->setParameter('behat.code_coverage.config.branchAndPathCoverage', $config['branchAndPathCoverage']); |
57
|
36 |
|
$container->setParameter('behat.code_coverage.config.reports', $config['reports'] ?? []); |
58
|
36 |
|
$container->setParameter('behat.code_coverage.config.cache', $config['cache']); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
72 |
|
public function configure(ArrayNodeDefinition $builder): void |
65
|
|
|
{ |
66
|
60 |
|
$builder |
67
|
72 |
|
->children() |
68
|
72 |
|
->scalarNode('cache') |
69
|
72 |
|
->defaultValue(sys_get_temp_dir() . '/behat-code-coverage-cache') |
70
|
72 |
|
->end() |
71
|
72 |
|
->booleanNode('branchAndPathCoverage') |
|
|
|
|
72
|
72 |
|
->defaultNull() // use null to mean auto |
73
|
72 |
|
->end() |
74
|
72 |
|
->arrayNode('filter') |
75
|
72 |
|
->addDefaultsIfNotSet() |
76
|
72 |
|
->children() |
77
|
72 |
|
->scalarNode('includeUncoveredFiles') |
78
|
72 |
|
->defaultTrue() |
79
|
72 |
|
->end() |
80
|
72 |
|
->scalarNode('processUncoveredFiles') |
81
|
72 |
|
->defaultFalse() |
82
|
72 |
|
->setDeprecated('dvdoug/behat-code-coverage', '5.3', 'the processUncoveredFiles setting is deprecated, it has been removed from php-code-coverage v10') |
83
|
72 |
|
->end() |
84
|
72 |
|
->arrayNode('include') |
85
|
72 |
|
->addDefaultsIfNotSet() |
86
|
72 |
|
->children() |
87
|
72 |
|
->arrayNode('directories') |
88
|
72 |
|
->useAttributeAsKey('name') |
89
|
72 |
|
->normalizeKeys(false) |
90
|
72 |
|
->prototype('array') |
91
|
72 |
|
->children() |
92
|
72 |
|
->scalarNode('prefix')->defaultValue('')->end() |
93
|
72 |
|
->scalarNode('suffix')->defaultValue('.php')->end() |
94
|
72 |
|
->end() |
95
|
72 |
|
->end() |
96
|
72 |
|
->end() |
97
|
72 |
|
->arrayNode('files') |
98
|
72 |
|
->prototype('scalar')->end() |
99
|
72 |
|
->end() |
100
|
72 |
|
->end() |
101
|
72 |
|
->end() |
102
|
72 |
|
->arrayNode('exclude') |
103
|
72 |
|
->addDefaultsIfNotSet() |
104
|
72 |
|
->children() |
105
|
72 |
|
->arrayNode('directories') |
106
|
72 |
|
->useAttributeAsKey('name') |
107
|
72 |
|
->normalizeKeys(false) |
108
|
72 |
|
->prototype('array') |
109
|
72 |
|
->children() |
110
|
72 |
|
->scalarNode('prefix')->defaultValue('')->end() |
111
|
72 |
|
->scalarNode('suffix')->defaultValue('.php')->end() |
112
|
72 |
|
->end() |
113
|
72 |
|
->end() |
114
|
72 |
|
->end() |
115
|
72 |
|
->arrayNode('files') |
116
|
72 |
|
->prototype('scalar')->end() |
117
|
72 |
|
->end() |
118
|
72 |
|
->end() |
119
|
72 |
|
->end() |
120
|
72 |
|
->end() |
121
|
72 |
|
->end() |
122
|
72 |
|
->arrayNode('reports') |
123
|
72 |
|
->children() |
124
|
72 |
|
->arrayNode('cobertura') |
125
|
72 |
|
->children() |
126
|
72 |
|
->scalarNode('name')->defaultNull()->end() |
127
|
72 |
|
->scalarNode('target')->isRequired()->cannotBeEmpty()->end() |
128
|
72 |
|
->end() |
129
|
72 |
|
->end() |
130
|
72 |
|
->arrayNode('clover') |
131
|
72 |
|
->children() |
132
|
72 |
|
->scalarNode('name')->defaultNull()->end() |
133
|
72 |
|
->scalarNode('target')->isRequired()->cannotBeEmpty()->end() |
134
|
72 |
|
->end() |
135
|
72 |
|
->end() |
136
|
72 |
|
->arrayNode('crap4j') |
137
|
72 |
|
->children() |
138
|
72 |
|
->scalarNode('name')->defaultNull()->end() |
139
|
72 |
|
->scalarNode('target')->isRequired()->cannotBeEmpty()->end() |
140
|
72 |
|
->end() |
141
|
72 |
|
->end() |
142
|
72 |
|
->arrayNode('html') |
143
|
72 |
|
->children() |
144
|
72 |
|
->scalarNode('target')->isRequired()->cannotBeEmpty()->end() |
145
|
72 |
|
->scalarNode('lowUpperBound')->defaultValue(50)->end() |
146
|
72 |
|
->scalarNode('highLowerBound')->defaultValue(90)->end() |
147
|
72 |
|
->arrayNode('colors') |
148
|
72 |
|
->addDefaultsIfNotSet() |
149
|
72 |
|
->children() |
150
|
72 |
|
->scalarNode('successLow')->defaultValue('#dff0d8')->end() |
151
|
72 |
|
->scalarNode('successMedium')->defaultValue('#c3e3b5')->end() |
152
|
72 |
|
->scalarNode('successHigh')->defaultValue('#99cb84')->end() |
153
|
72 |
|
->scalarNode('warning')->defaultValue('#fcf8e3')->end() |
154
|
72 |
|
->scalarNode('danger')->defaultValue('#f2dede')->end() |
155
|
72 |
|
->end() |
156
|
72 |
|
->end() |
157
|
72 |
|
->scalarNode('customCSSFile')->defaultNull()->end() |
158
|
72 |
|
->end() |
159
|
72 |
|
->end() |
160
|
72 |
|
->arrayNode('php') |
161
|
72 |
|
->children() |
162
|
72 |
|
->scalarNode('target')->isRequired()->cannotBeEmpty()->end() |
163
|
72 |
|
->end() |
164
|
72 |
|
->end() |
165
|
72 |
|
->arrayNode('text') |
166
|
72 |
|
->children() |
167
|
72 |
|
->booleanNode('showColors')->defaultValue(false)->end() |
168
|
72 |
|
->scalarNode('lowUpperBound')->defaultValue(50)->end() |
169
|
72 |
|
->scalarNode('highLowerBound')->defaultValue(90)->end() |
170
|
72 |
|
->booleanNode('showOnlySummary')->defaultValue(false)->end() |
171
|
72 |
|
->booleanNode('showUncoveredFiles')->defaultValue(false)->end() |
172
|
72 |
|
->end() |
173
|
72 |
|
->end() |
174
|
72 |
|
->arrayNode('xml') |
175
|
72 |
|
->children() |
176
|
72 |
|
->scalarNode('target')->isRequired()->cannotBeEmpty()->end() |
177
|
72 |
|
->end() |
178
|
72 |
|
->end() |
179
|
72 |
|
->end() |
180
|
72 |
|
->end() |
181
|
72 |
|
->end() |
182
|
72 |
|
->end(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritdoc} |
187
|
|
|
*/ |
188
|
72 |
|
public function getConfigKey() |
189
|
|
|
{ |
190
|
72 |
|
return 'code_coverage'; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* {@inheritdoc} |
195
|
|
|
*/ |
196
|
144 |
|
public function process(ContainerBuilder $container): void |
197
|
|
|
{ |
198
|
|
|
/** @var InputInterface $input */ |
199
|
144 |
|
$input = $container->get(CliExtension::INPUT_ID); |
200
|
|
|
|
201
|
|
|
/** @var OutputInterface $output */ |
202
|
144 |
|
$output = $container->get(CliExtension::OUTPUT_ID); |
203
|
|
|
|
204
|
144 |
|
$filterConfig = $container->getParameter('behat.code_coverage.config.filter'); |
205
|
144 |
|
$branchPathConfig = $container->getParameter('behat.code_coverage.config.branchAndPathCoverage'); |
206
|
144 |
|
$cacheDir = $container->getParameter('behat.code_coverage.config.cache'); |
207
|
|
|
|
208
|
144 |
|
$canCollectCodeCoverage = true; |
209
|
|
|
try { |
210
|
144 |
|
$this->initCodeCoverage(new Filter(), $filterConfig, null, $cacheDir, $output); |
211
|
|
|
|
212
|
108 |
|
$codeCoverageDefinition = $container->getDefinition(CodeCoverage::class); |
213
|
108 |
|
$filterDefinition = $container->getDefinition(Filter::class); |
214
|
108 |
|
$codeCoverageDefinition->setFactory([new Reference(self::class), 'initCodeCoverage']); |
215
|
108 |
|
$codeCoverageDefinition->setArguments([$filterDefinition, $filterConfig, $branchPathConfig, $cacheDir, $output]); |
216
|
36 |
|
} catch (NoCodeCoverageDriverAvailableException|Xdebug2NotEnabledException|Xdebug3NotEnabledException|XdebugNotEnabledException|XdebugNotAvailableException $e) { |
217
|
36 |
|
$output->writeln("<comment>No code coverage driver is available. {$e->getMessage()}</comment>"); |
218
|
36 |
|
$canCollectCodeCoverage = false; |
219
|
|
|
} |
220
|
|
|
|
221
|
144 |
|
if (!$canCollectCodeCoverage || $input->hasParameterOption('--no-coverage')) { |
222
|
72 |
|
$container->getDefinition(EventSubscriber::class)->setArgument('$coverage', null); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
108 |
|
public function initCodeCoverage(Filter $filter, array $filterConfig, ?bool $branchPathConfig, string $cacheDir, OutputInterface $output): CodeCoverage |
227
|
|
|
{ |
228
|
|
|
// set up filter |
229
|
108 |
|
$files = []; |
230
|
|
|
|
231
|
108 |
|
foreach ($filterConfig['include']['directories'] as $directoryToInclude => $details) { |
232
|
108 |
|
foreach ((new FileIteratorFacade())->getFilesAsArray($directoryToInclude, $details['suffix'], $details['prefix']) as $fileToInclude) { |
233
|
|
|
$files[$fileToInclude] = $fileToInclude; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
108 |
|
foreach ($filterConfig['include']['files'] as $fileToInclude) { |
238
|
108 |
|
$files[$fileToInclude] = $fileToInclude; |
239
|
|
|
} |
240
|
|
|
|
241
|
108 |
|
foreach ($filterConfig['exclude']['directories'] as $directoryToExclude => $details) { |
242
|
108 |
|
foreach ((new FileIteratorFacade())->getFilesAsArray($directoryToExclude, $details['suffix'], $details['prefix']) as $fileToExclude) { |
243
|
|
|
unset($files[$fileToExclude]); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
108 |
|
foreach ($filterConfig['exclude']['files'] as $fileToExclude) { |
248
|
108 |
|
unset($files[$fileToExclude]); |
249
|
|
|
} |
250
|
|
|
|
251
|
108 |
|
foreach ($files as $file) { |
252
|
|
|
$filter->includeFile($file); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
// see if we can get a driver |
256
|
108 |
|
$selector = new Selector(); |
257
|
108 |
|
$driver = $selector->forLineCoverage($filter); |
258
|
108 |
|
if ($branchPathConfig !== false) { |
259
|
|
|
try { |
260
|
108 |
|
$driver = $selector->forLineAndPathCoverage($filter); |
261
|
54 |
|
} catch (NoCodeCoverageDriverWithPathCoverageSupportAvailableException|XdebugNotAvailableException|XdebugNotEnabledException $e) { |
262
|
|
|
// fallback driver is already set |
263
|
54 |
|
if ($branchPathConfig === true) { // only warn if explicitly enabled |
|
|
|
|
264
|
18 |
|
$output->writeln(sprintf('<info>%s does not support collecting branch and path data</info>', $driver->nameAndVersion())); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
// and init coverage |
270
|
108 |
|
$codeCoverage = new CodeCoverage($driver, $filter); |
271
|
108 |
|
$codeCoverage->cacheStaticAnalysis($cacheDir); |
272
|
|
|
|
273
|
108 |
|
if ($filterConfig['includeUncoveredFiles']) { |
274
|
36 |
|
$codeCoverage->includeUncoveredFiles(); |
275
|
|
|
} else { |
276
|
72 |
|
$codeCoverage->excludeUncoveredFiles(); |
277
|
|
|
} |
278
|
|
|
|
279
|
108 |
|
if (InstalledVersions::satisfies(new VersionParser(), 'phpunit/php-code-coverage', '^9.0')) { |
280
|
54 |
|
if ($filterConfig['processUncoveredFiles']) { |
281
|
18 |
|
$codeCoverage->processUncoveredFiles(); |
|
|
|
|
282
|
|
|
} else { |
283
|
36 |
|
$codeCoverage->doNotProcessUncoveredFiles(); |
|
|
|
|
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
108 |
|
return $codeCoverage; |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths