1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
/** |
5
|
|
|
* Code Coverage Extension for Behat. |
6
|
|
|
* |
7
|
|
|
* @copyright 2013 Anthon Pang |
8
|
|
|
* |
9
|
|
|
* @license BSD-2-Clause |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace DVDoug\Behat\CodeCoverage; |
13
|
|
|
|
14
|
|
|
use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface; |
15
|
|
|
use Behat\Testwork\ServiceContainer\ExtensionManager; |
16
|
|
|
use DVDoug\Behat\CodeCoverage\Subscriber\EventSubscriber; |
17
|
|
|
use SebastianBergmann\CodeCoverage\CodeCoverage; |
18
|
|
|
use SebastianBergmann\CodeCoverage\Driver\Driver; |
19
|
|
|
use SebastianBergmann\CodeCoverage\Filter; |
20
|
|
|
use SebastianBergmann\CodeCoverage\NoCodeCoverageDriverAvailableException; |
21
|
|
|
use SebastianBergmann\CodeCoverage\NoCodeCoverageDriverWithPathCoverageSupportAvailableException; |
22
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
23
|
|
|
use Symfony\Component\Config\FileLocator; |
24
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
25
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
26
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
27
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
28
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Code coverage extension. |
32
|
|
|
* |
33
|
|
|
* @author Anthon Pang <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class Extension implements ExtensionInterface |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function initialize(ExtensionManager $extensionManager): void |
41
|
|
|
{ |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function load(ContainerBuilder $container, array $config): void |
48
|
|
|
{ |
49
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/Resources/config')); |
50
|
|
|
|
51
|
|
|
$servicesFile = 'services.xml'; |
52
|
|
|
$loader->load($servicesFile); |
53
|
|
|
|
54
|
|
|
$container->setParameter('behat.code_coverage.config.filter', $config['filter']); |
55
|
|
|
$container->setParameter('behat.code_coverage.config.branchAndPathCoverage', $config['branchAndPathCoverage']); |
56
|
|
|
$container->setParameter('behat.code_coverage.config.reports', $config['reports'] ?? []); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function configure(ArrayNodeDefinition $builder): void |
63
|
|
|
{ |
64
|
|
|
$builder |
65
|
|
|
->children() |
66
|
|
|
->booleanNode('branchAndPathCoverage') |
67
|
|
|
->defaultNull() // use null to mean auto |
68
|
|
|
->end() |
69
|
|
|
->arrayNode('filter') |
|
|
|
|
70
|
|
|
->addDefaultsIfNotSet() |
71
|
|
|
->children() |
72
|
|
|
->scalarNode('includeUncoveredFiles') |
73
|
|
|
->defaultTrue() |
74
|
|
|
->end() |
75
|
|
|
->scalarNode('processUncoveredFiles') |
76
|
|
|
->defaultFalse() |
77
|
|
|
->end() |
78
|
|
|
->arrayNode('include') |
79
|
|
|
->addDefaultsIfNotSet() |
80
|
|
|
->children() |
81
|
|
|
->arrayNode('directories') |
82
|
|
|
->useAttributeAsKey('name') |
83
|
|
|
->normalizeKeys(false) |
84
|
|
|
->prototype('array') |
85
|
|
|
->children() |
86
|
|
|
->scalarNode('prefix')->defaultValue('')->end() |
87
|
|
|
->scalarNode('suffix')->defaultValue('.php')->end() |
88
|
|
|
->end() |
89
|
|
|
->end() |
90
|
|
|
->end() |
91
|
|
|
->arrayNode('files') |
92
|
|
|
->prototype('scalar')->end() |
93
|
|
|
->end() |
94
|
|
|
->end() |
95
|
|
|
->end() |
96
|
|
|
->arrayNode('exclude') |
97
|
|
|
->addDefaultsIfNotSet() |
98
|
|
|
->children() |
99
|
|
|
->arrayNode('directories') |
100
|
|
|
->useAttributeAsKey('name') |
101
|
|
|
->normalizeKeys(false) |
102
|
|
|
->prototype('array') |
103
|
|
|
->children() |
104
|
|
|
->scalarNode('prefix')->defaultValue('')->end() |
105
|
|
|
->scalarNode('suffix')->defaultValue('.php')->end() |
106
|
|
|
->end() |
107
|
|
|
->end() |
108
|
|
|
->end() |
109
|
|
|
->arrayNode('files') |
110
|
|
|
->prototype('scalar')->end() |
111
|
|
|
->end() |
112
|
|
|
->end() |
113
|
|
|
->end() |
114
|
|
|
->end() |
115
|
|
|
->end() |
116
|
|
|
->arrayNode('reports') |
117
|
|
|
->children() |
118
|
|
|
->arrayNode('clover') |
119
|
|
|
->children() |
120
|
|
|
->scalarNode('name')->defaultNull()->end() |
121
|
|
|
->scalarNode('target')->isRequired()->cannotBeEmpty()->end() |
122
|
|
|
->end() |
123
|
|
|
->end() |
124
|
|
|
->arrayNode('crap4j') |
125
|
|
|
->children() |
126
|
|
|
->scalarNode('name')->defaultNull()->end() |
127
|
|
|
->scalarNode('target')->isRequired()->cannotBeEmpty()->end() |
128
|
|
|
->end() |
129
|
|
|
->end() |
130
|
|
|
->arrayNode('html') |
131
|
|
|
->children() |
132
|
|
|
->scalarNode('target')->isRequired()->cannotBeEmpty()->end() |
133
|
|
|
->scalarNode('lowUpperBound')->defaultValue(50)->end() |
134
|
|
|
->scalarNode('highLowerBound')->defaultValue(90)->end() |
135
|
|
|
->end() |
136
|
|
|
->end() |
137
|
|
|
->arrayNode('php') |
138
|
|
|
->children() |
139
|
|
|
->scalarNode('target')->isRequired()->cannotBeEmpty()->end() |
140
|
|
|
->end() |
141
|
|
|
->end() |
142
|
|
|
->arrayNode('text') |
143
|
|
|
->children() |
144
|
|
|
->booleanNode('showColors')->defaultValue(false)->end() |
145
|
|
|
->scalarNode('lowUpperBound')->defaultValue(50)->end() |
146
|
|
|
->scalarNode('highLowerBound')->defaultValue(90)->end() |
147
|
|
|
->booleanNode('showOnlySummary')->defaultValue(false)->end() |
148
|
|
|
->booleanNode('showUncoveredFiles')->defaultValue(false)->end() |
149
|
|
|
->end() |
150
|
|
|
->end() |
151
|
|
|
->arrayNode('xml') |
152
|
|
|
->children() |
153
|
|
|
->scalarNode('target')->isRequired()->cannotBeEmpty()->end() |
154
|
|
|
->end() |
155
|
|
|
->end() |
156
|
|
|
->end() |
157
|
|
|
->end() |
158
|
|
|
->end() |
159
|
|
|
->end(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
|
|
public function getConfigKey() |
166
|
|
|
{ |
167
|
|
|
return 'code_coverage'; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
|
|
public function process(ContainerBuilder $container): void |
174
|
|
|
{ |
175
|
|
|
/** @var InputInterface $input */ |
176
|
|
|
$input = $container->get('cli.input'); |
177
|
|
|
|
178
|
|
|
/** @var OutputInterface $output */ |
179
|
|
|
$output = $container->get('cli.output'); |
180
|
|
|
|
181
|
|
|
$filterConfig = $container->getParameter('behat.code_coverage.config.filter'); |
182
|
|
|
$branchPathConfig = $container->getParameter('behat.code_coverage.config.branchAndPathCoverage'); |
183
|
|
|
|
184
|
|
|
$canCollectCodeCoverage = true; |
185
|
|
|
try { |
186
|
|
|
$this->initCodeCoverage(new Filter(), $filterConfig, null, $output); |
187
|
|
|
|
188
|
|
|
$codeCoverageDefinition = $container->getDefinition(CodeCoverage::class); |
189
|
|
|
$filterDefinition = $container->getDefinition(Filter::class); |
190
|
|
|
$codeCoverageDefinition->setFactory([new Reference(self::class), 'initCodeCoverage']); |
191
|
|
|
$codeCoverageDefinition->setArguments([$filterDefinition, $filterConfig, $branchPathConfig, $output]); |
192
|
|
|
} catch (NoCodeCoverageDriverAvailableException $e) { |
193
|
|
|
$output->writeln('<comment>No code coverage driver is available</comment>'); |
194
|
|
|
$canCollectCodeCoverage = false; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
if (!$canCollectCodeCoverage || $input->hasParameterOption('--no-coverage')) { |
198
|
|
|
$container->getDefinition(EventSubscriber::class)->setArgument('$coverage', null); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function initCodeCoverage(Filter $filter, array $filterConfig, ?bool $branchPathConfig, OutputInterface $output): CodeCoverage |
203
|
|
|
{ |
204
|
|
|
// set up filter |
205
|
|
|
array_walk($filterConfig['include']['directories'], static function (array $dir, string $path, Filter $filter): void { |
206
|
|
|
$filter->includeDirectory($path, $dir['suffix'], $dir['prefix']); |
207
|
|
|
}, $filter); |
208
|
|
|
|
209
|
|
|
array_walk($filterConfig['include']['files'], static function (string $file, string $key, Filter $filter): void { |
210
|
|
|
$filter->includeFile($file); |
211
|
|
|
}, $filter); |
212
|
|
|
|
213
|
|
|
array_walk($filterConfig['exclude']['directories'], static function (array $dir, string $path, Filter $filter): void { |
214
|
|
|
$filter->excludeDirectory($path, $dir['suffix'], $dir['prefix']); |
215
|
|
|
}, $filter); |
216
|
|
|
|
217
|
|
|
array_walk($filterConfig['exclude']['files'], static function (string $file, string $key, Filter $filter): void { |
218
|
|
|
$filter->excludeFile($file); |
219
|
|
|
}, $filter); |
220
|
|
|
|
221
|
|
|
// see if we can get a driver |
222
|
|
|
$driver = Driver::forLineCoverage($filter); |
223
|
|
|
if ($branchPathConfig !== false) { |
224
|
|
|
try { |
225
|
|
|
$driver = Driver::forLineAndPathCoverage($filter); |
226
|
|
|
} catch (NoCodeCoverageDriverWithPathCoverageSupportAvailableException $e) { |
227
|
|
|
// fallback driver is already set |
228
|
|
|
if ($branchPathConfig === true) { //only warn if explicitly enabled |
|
|
|
|
229
|
|
|
$output->writeln(sprintf('<info>%s does not support collecting branch and path data</info>', $driver->nameAndVersion())); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
// and init coverage |
235
|
|
|
$codeCoverage = new CodeCoverage($driver, $filter); |
236
|
|
|
|
237
|
|
|
if ($filterConfig['includeUncoveredFiles']) { |
238
|
|
|
$codeCoverage->includeUncoveredFiles(); |
239
|
|
|
} else { |
240
|
|
|
$codeCoverage->excludeUncoveredFiles(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
if ($filterConfig['processUncoveredFiles']) { |
244
|
|
|
$codeCoverage->processUncoveredFiles(); |
245
|
|
|
} else { |
246
|
|
|
$codeCoverage->doNotProcessUncoveredFiles(); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return $codeCoverage; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|