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; |
16
|
|
|
|
17
|
|
|
use FriendsOfPhpSpec\PhpSpec\CodeCoverage\Exception\NoCoverageDriverAvailableException; |
18
|
|
|
use FriendsOfPhpSpec\PhpSpec\CodeCoverage\Listener\CodeCoverageListener; |
19
|
|
|
use PhpSpec\Extension; |
20
|
|
|
use PhpSpec\ServiceContainer; |
21
|
|
|
use RuntimeException; |
22
|
|
|
use SebastianBergmann\CodeCoverage\CodeCoverage; |
23
|
|
|
use SebastianBergmann\CodeCoverage\Filter; |
24
|
|
|
use SebastianBergmann\CodeCoverage\Report; |
25
|
|
|
use SebastianBergmann\CodeCoverage\Version; |
26
|
|
|
use Symfony\Component\Console\Input\InputOption; |
27
|
|
|
|
28
|
|
|
use function count; |
29
|
|
|
use function is_array; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Injects Code Coverage Event Subscriber into the EventDispatcher. |
33
|
|
|
* The Subscriber will add Code Coverage information before each example. |
34
|
|
|
* |
35
|
|
|
* @author Henrik Bjornskov |
36
|
|
|
*/ |
37
|
|
|
class CodeCoverageExtension implements Extension |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function load(ServiceContainer $container, array $params = []): void |
43
|
|
|
{ |
44
|
|
|
foreach ($container->getByTag('console.commands') as $command) { |
45
|
|
|
$command->addOption('no-coverage', null, InputOption::VALUE_NONE, 'Skip code coverage generation'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$container->define('code_coverage.filter', static function () { |
49
|
|
|
return new Filter(); |
50
|
|
|
}); |
51
|
|
|
|
52
|
|
|
$container->define('code_coverage', static function ($container) { |
53
|
|
|
try { |
54
|
|
|
$coverage = new CodeCoverage(null, $container->get('code_coverage.filter')); |
55
|
|
|
} catch (RuntimeException $error) { |
56
|
|
|
throw new NoCoverageDriverAvailableException( |
57
|
|
|
'There is no available coverage driver to be used.', |
58
|
|
|
0, |
59
|
|
|
$error |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $coverage; |
64
|
|
|
}); |
65
|
|
|
|
66
|
|
|
$container->define('code_coverage.options', static function ($container) use ($params) { |
67
|
|
|
$options = !empty($params) ? $params : $container->getParam('code_coverage'); |
68
|
|
|
|
69
|
|
|
if (!isset($options['format'])) { |
70
|
|
|
$options['format'] = ['html']; |
71
|
|
|
} elseif (!is_array($options['format'])) { |
72
|
|
|
$options['format'] = (array) $options['format']; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (isset($options['output'])) { |
76
|
|
|
if (!is_array($options['output']) && 1 === count($options['format'])) { |
77
|
|
|
$format = $options['format'][0]; |
78
|
|
|
$options['output'] = [$format => $options['output']]; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (!isset($options['show_uncovered_files'])) { |
83
|
|
|
$options['show_uncovered_files'] = true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (!isset($options['lower_upper_bound'])) { |
87
|
|
|
$options['lower_upper_bound'] = 35; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (!isset($options['high_lower_bound'])) { |
91
|
|
|
$options['high_lower_bound'] = 70; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $options; |
95
|
|
|
}); |
96
|
|
|
|
97
|
|
|
$container->define('code_coverage.reports', static function ($container) { |
98
|
|
|
$options = $container->get('code_coverage.options'); |
99
|
|
|
|
100
|
|
|
$reports = []; |
101
|
|
|
|
102
|
|
|
foreach ($options['format'] as $format) { |
103
|
|
|
switch ($format) { |
104
|
|
|
case 'clover': |
105
|
|
|
$reports['clover'] = new Report\Clover(); |
106
|
|
|
|
107
|
|
|
break; |
108
|
|
|
case 'php': |
109
|
|
|
$reports['php'] = new Report\PHP(); |
110
|
|
|
|
111
|
|
|
break; |
112
|
|
|
case 'text': |
113
|
|
|
$reports['text'] = new Report\Text( |
114
|
|
|
$options['lower_upper_bound'], |
115
|
|
|
$options['high_lower_bound'], |
116
|
|
|
$options['show_uncovered_files'], |
117
|
|
|
/* $showOnlySummary */ |
118
|
|
|
false |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
break; |
122
|
|
|
case 'xml': |
123
|
|
|
$reports['xml'] = new Report\Xml\Facade(Version::id()); |
124
|
|
|
|
125
|
|
|
break; |
126
|
|
|
case 'crap4j': |
127
|
|
|
$reports['crap4j'] = new Report\Crap4j(); |
128
|
|
|
|
129
|
|
|
break; |
130
|
|
|
case 'html': |
131
|
|
|
$reports['html'] = new Report\Html\Facade(); |
132
|
|
|
|
133
|
|
|
break; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$container->setParam('code_coverage', $options); |
138
|
|
|
|
139
|
|
|
return $reports; |
140
|
|
|
}); |
141
|
|
|
|
142
|
|
|
$container->define('event_dispatcher.listeners.code_coverage', static function ($container) { |
143
|
|
|
$skipCoverage = false; |
144
|
|
|
$input = $container->get('console.input'); |
145
|
|
|
|
146
|
|
|
$coverage = $container->get('code_coverage'); |
147
|
|
|
|
148
|
|
|
if ($input->hasOption('no-coverage') && $input->getOption('no-coverage')) { |
149
|
|
|
$skipCoverage = true; |
150
|
|
|
} else { |
151
|
|
|
$coverage->start(null); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$listener = new CodeCoverageListener( |
155
|
|
|
$container->get('console.io'), |
156
|
|
|
$coverage, |
157
|
|
|
$container->get('code_coverage.reports'), |
158
|
|
|
$skipCoverage |
159
|
|
|
); |
160
|
|
|
$listener->setOptions($container->getParam('code_coverage', [])); |
161
|
|
|
|
162
|
|
|
return $listener; |
163
|
|
|
}, ['event_dispatcher.listeners']); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|