-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathCodeCoverageExtension.php
123 lines (105 loc) · 4.41 KB
/
CodeCoverageExtension.php
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
<?php
namespace LeanPHP\PhpSpec\CodeCoverage;
use LeanPHP\PhpSpec\CodeCoverage\Listener\CodeCoverageListener;
use PhpSpec\ServiceContainer;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Filter;
use SebastianBergmann\CodeCoverage\Report;
use SebastianBergmann\CodeCoverage\Version;
use Symfony\Component\Console\Input\InputOption;
/**
* Injects a Event Subscriber into the EventDispatcher.
* The Subscriber will add Code Coverage information before each example
*
* @author Henrik Bjornskov
*/
class CodeCoverageExtension implements \PhpSpec\Extension
{
/**
* {@inheritDoc}
*/
public function load(ServiceContainer $container, array $params = [])
{
foreach ($container->getByTag('console.commands') as $command) {
$command->addOption('no-coverage', null, InputOption::VALUE_NONE, 'Skip code coverage generation');
}
$container->define('code_coverage.filter', function () {
return new Filter();
});
$container->define('code_coverage', function ($container) {
return new CodeCoverage(null, $container->get('code_coverage.filter'));
});
$container->define('code_coverage.options', function ($container) use ($params) {
$options = !empty($params) ? $params : $container->getParam('code_coverage');
if (!isset($options['format'])) {
$options['format'] = array('html');
} elseif (!is_array($options['format'])) {
$options['format'] = (array) $options['format'];
}
if (isset($options['output'])) {
if (!is_array($options['output']) && count($options['format']) == 1) {
$format = $options['format'][0];
$options['output'] = array($format => $options['output']);
}
}
if (!isset($options['show_uncovered_files'])) {
$options['show_uncovered_files'] = true;
}
if (!isset($options['lower_upper_bound'])) {
$options['lower_upper_bound'] = 35;
}
if (!isset($options['high_lower_bound'])) {
$options['high_lower_bound'] = 70;
}
return $options;
});
$container->define('code_coverage.reports', function ($container) {
$options = $container->get('code_coverage.options');
$reports = array();
foreach ($options['format'] as $format) {
switch ($format) {
case 'clover':
$reports['clover'] = new Report\Clover();
break;
case 'php':
$reports['php'] = new Report\PHP();
break;
case 'text':
$reports['text'] = new Report\Text(
$options['lower_upper_bound'],
$options['high_lower_bound'],
$options['show_uncovered_files'],
/* $showOnlySummary */ false
);
break;
case 'xml':
$reports['xml'] = new Report\Xml\Facade(Version::id());
break;
case 'crap4j':
$reports['crap4j'] = new Report\Crap4j();
break;
case 'html':
$reports['html'] = new Report\Html\Facade();
break;
}
}
$container->setParam('code_coverage', $options);
return $reports;
});
$container->define('event_dispatcher.listeners.code_coverage', function ($container) {
$skipCoverage = false;
$input = $container->get('console.input');
if ($input->hasOption('no-coverage') && $input->getOption('no-coverage')) {
$skipCoverage = true;
}
$listener = new CodeCoverageListener(
$container->get('console.io'),
$container->get('code_coverage'),
$container->get('code_coverage.reports'),
$skipCoverage
);
$listener->setOptions($container->getParam('code_coverage', array()));
return $listener;
}, ['event_dispatcher.listeners']);
}
}