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
<?php
namespace Codeception\Coverage;
use Codeception\Configuration;
use Codeception\Coverage\Subscriber\Printer;
use Codeception\Lib\Interfaces\Remote;
use Codeception\Stub;
use Codeception\Subscriber\Shared\StaticEvents;
use PHPUnit\Framework\CodeCoverageException;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
abstract class SuiteSubscriber implements EventSubscriberInterface
{
use StaticEvents;
protected $defaultSettings = [
'enabled' => false,
'remote' => false,
'local' => false,
'xdebug_session' => 'codeception',
'remote_config' => null,
'show_uncovered' => false,
'c3_url' => null
];
protected $settings = [];
protected $filters = [];
protected $modules = [];
protected $coverage;
protected $logDir;
protected $options;
public static $events = [];
abstract protected function isEnabled();
public function __construct($options = [])
{
$this->options = $options;
$this->logDir = Configuration::outputDir();
}
protected function applySettings($settings)
{
try {
$this->coverage = new \SebastianBergmann\CodeCoverage\CodeCoverage();
} catch (CodeCoverageException $e) {
throw new \Exception(
'XDebug is required to collect CodeCoverage. Please install xdebug extension and enable it in php.ini'
);
}
$this->filters = $settings;
$this->settings = $this->defaultSettings;
$keys = array_keys($this->defaultSettings);
foreach ($keys as $key) {
if (isset($settings['coverage'][$key])) {
$this->settings[$key] = $settings['coverage'][$key];
}
}
$this->coverage->setProcessUncoveredFilesFromWhitelist($this->settings['show_uncovered']);
}
/**
* @param array $modules
* @return \Codeception\Lib\Interfaces\Remote|null
*/
protected function getServerConnectionModule(array $modules)
{
foreach ($modules as $module) {
if ($module instanceof Remote) {
return $module;
}
}
return null;
}
public function applyFilter(\PHPUnit\Framework\TestResult $result)
{
$driver = Stub::makeEmpty('SebastianBergmann\CodeCoverage\Driver\Driver');
$result->setCodeCoverage(new CodeCoverage($driver));
Filter::setup($this->coverage)
->whiteList($this->filters)
->blackList($this->filters);
$result->setCodeCoverage($this->coverage);
}
protected function mergeToPrint($coverage)
{
Printer::$coverage->merge($coverage);
}
}