-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathCoverageCommand.php
More file actions
161 lines (140 loc) · 5.4 KB
/
CoverageCommand.php
File metadata and controls
161 lines (140 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
declare(strict_types=1);
namespace Nimut\PhpunitMerger\Command;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Driver\Selector;
use SebastianBergmann\CodeCoverage\Filter;
use SebastianBergmann\CodeCoverage\Report\Clover;
use SebastianBergmann\CodeCoverage\Report\Html\Facade;
use SebastianBergmann\CodeCoverage\Report\Thresholds;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
class CoverageCommand extends Command
{
protected function configure()
{
$this->setName('coverage')
->setDescription('Merges multiple PHPUnit coverage php files into one')
->addArgument(
'directory',
InputArgument::REQUIRED,
'The directory containing PHPUnit coverage php files'
)
->addArgument(
'file',
InputArgument::OPTIONAL,
'The file where to write the merged result. Default: Standard output'
)
->addOption(
'html',
null,
InputOption::VALUE_REQUIRED,
'The directory where to write the code coverage report in HTML format'
)
->addOption(
'lowUpperBound',
null,
InputOption::VALUE_REQUIRED,
'The lowUpperBound value to be used for HTML format'
)
->addOption(
'highLowerBound',
null,
InputOption::VALUE_REQUIRED,
'The highLowerBound value to be used for HTML format'
)
->addOption(
'projectRoot',
null,
InputOption::VALUE_REQUIRED,
'The path to the project source code'
)
->addOption(
'fixRoot',
null,
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
'The root path that will be replaced with the project root path',
[]
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$finder = new Finder();
$finder->files()
->in(realpath($input->getArgument('directory')));
$codeCoverage = $this->getCodeCoverage();
$projectRoot = $input->getOption('projectRoot');
$fixRoot = $input->getOption('fixRoot');
foreach ($finder as $file) {
$coverage = require $file->getRealPath();
if (!$coverage instanceof CodeCoverage) {
throw new \RuntimeException($file->getRealPath() . ' doesn\'t return a valid ' . CodeCoverage::class . ' object!');
}
$this->normalizeCoverage($coverage);
if (!empty($projectRoot) && !empty($fixRoot)) {
$this->fixPaths($coverage, $projectRoot, $fixRoot);
}
$codeCoverage->merge($coverage);
}
$this->writeCodeCoverage($codeCoverage, $output, $input->getArgument('file'));
$html = $input->getOption('html');
if ($html !== null) {
$lowUpperBound = (int)($input->getOption('lowUpperBound') ?: 50);
$highLowerBound = (int)($input->getOption('highLowerBound') ?: 90);
$this->writeHtmlReport($codeCoverage, $html, $lowUpperBound, $highLowerBound);
}
return 0;
}
private function getCodeCoverage()
{
$filter = new Filter();
return new CodeCoverage((new Selector())->forLineCoverage($filter), $filter);
}
private function normalizeCoverage(CodeCoverage $coverage)
{
$tests = $coverage->getTests();
foreach ($tests as &$test) {
$test['fromTestcase'] = $test['fromTestcase'] ?? false;
}
$coverage->setTests($tests);
}
private function fixPaths(CodeCoverage $coverage, string $projectRoot, array $paths)
{
$data = $coverage->getData();
$lineCoverage = [];
foreach ($data->lineCoverage() as $fileName => $coverageData) {
$newName = $fileName;
foreach ($paths as $path) {
$newName = str_replace($path, $projectRoot, $fileName);
if ($newName != $fileName) {
break;
}
}
$lineCoverage[$newName] = $coverageData;
}
$data->setLineCoverage($lineCoverage);
$coverage->setData($data);
}
private function writeCodeCoverage(CodeCoverage $codeCoverage, OutputInterface $output, $file = null)
{
$writer = new Clover();
$buffer = $writer->process($codeCoverage, $file);
if ($file === null) {
$output->write($buffer);
}
}
private function writeHtmlReport(CodeCoverage $codeCoverage, string $destination, int $lowUpperBound, int $highLowerBound)
{
if (class_exists('SebastianBergmann\\CodeCoverage\\Report\\Thresholds')) {
$writer = new Facade('', null, Thresholds::from($lowUpperBound, $highLowerBound));
} else {
$writer = new Facade($lowUpperBound, $highLowerBound);
}
$writer->process($codeCoverage, $destination);
}
}