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