1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the doyo/behat-coverage-extension project. |
5
|
|
|
* |
6
|
|
|
* (c) Anthonius Munthi <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Doyo\Behat\Coverage\Bridge\CodeCoverage; |
15
|
|
|
|
16
|
|
|
use Doyo\Behat\Coverage\Bridge\Exception\ProcessorException; |
17
|
|
|
use SebastianBergmann\CodeCoverage\CodeCoverage; |
18
|
|
|
use SebastianBergmann\CodeCoverage\Driver\Driver; |
19
|
|
|
use SebastianBergmann\CodeCoverage\Filter; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Provide bridge to PHP Code Coverage. |
23
|
|
|
* |
24
|
|
|
* @method append(array $data, $id = null, $append = true, $linesToBeCovered = [], array $linesToBeUsed = [], $ignoreForceCoversAnnotation = false) |
25
|
|
|
* @method setAddUncoveredFilesFromWhitelist(bool $flag) |
26
|
|
|
* @method clear() |
27
|
|
|
* @method array getTests() |
28
|
|
|
* @method Driver getDriver() |
29
|
|
|
* @method Filter filter() |
30
|
|
|
* @method array stop(bool $append = true, $linesToBeCovered = [], array $linesToBeUsed = [], bool $ignoreForceCoversAnnotation = false) |
31
|
|
|
* @method void setData(array $data) |
32
|
|
|
* @method array getData(bool $raw = false) |
33
|
|
|
*/ |
34
|
|
|
class Processor |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var CodeCoverage |
38
|
|
|
*/ |
39
|
|
|
private $codeCoverage; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var TestCase[] |
43
|
|
|
*/ |
44
|
|
|
private $testCases = []; |
45
|
|
|
|
46
|
|
|
private $completed = false; |
47
|
|
|
|
48
|
26 |
|
public function __construct($driver = null, $filter = null) |
49
|
|
|
{ |
50
|
26 |
|
$codeCoverage = new CodeCoverage($driver, $filter); |
51
|
26 |
|
$this->codeCoverage = $codeCoverage; |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
public function setCodeCoverage(CodeCoverage $codeCoverage) |
55
|
|
|
{ |
56
|
1 |
|
$this->codeCoverage = $codeCoverage; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return CodeCoverage |
61
|
|
|
*/ |
62
|
7 |
|
public function getCodeCoverage() |
63
|
|
|
{ |
64
|
7 |
|
return $this->codeCoverage; |
65
|
|
|
} |
66
|
|
|
|
67
|
6 |
|
public function addTestCase(TestCase $testCase) |
68
|
|
|
{ |
69
|
6 |
|
$this->testCases[$testCase->getName()] = $testCase; |
70
|
|
|
} |
71
|
|
|
|
72
|
6 |
|
public function complete() |
73
|
|
|
{ |
74
|
6 |
|
$coverage = $this->codeCoverage; |
75
|
6 |
|
$testCases = $this->testCases; |
76
|
6 |
|
$tests = $coverage->getTests(); |
77
|
|
|
|
78
|
6 |
|
foreach ($testCases as $testCase) { |
79
|
6 |
|
$name = $testCase->getName(); |
80
|
6 |
|
$tests[$name]['status'] = $testCase->getResult(); |
81
|
|
|
} |
82
|
|
|
|
83
|
6 |
|
$coverage->setTests($tests); |
84
|
6 |
|
$this->completed = true; |
85
|
|
|
} |
86
|
|
|
|
87
|
7 |
|
public function start(TestCase $testCase, $clear = false) |
88
|
|
|
{ |
89
|
7 |
|
$this->codeCoverage->start($testCase->getName(), $clear); |
90
|
|
|
} |
91
|
|
|
|
92
|
5 |
|
public function updateCoverage($coverage) |
93
|
|
|
{ |
94
|
5 |
|
$aggregate = $this->getData(); |
95
|
|
|
|
96
|
5 |
|
foreach ($coverage as $class => $counts) { |
97
|
4 |
|
if (!isset($this->coverage[$class])) { |
|
|
|
|
98
|
4 |
|
$aggregate[$class] = $counts; |
99
|
4 |
|
continue; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
foreach ($counts as $line => $status) { |
103
|
|
|
$status = !$status ? -1 : ($status > 1 ? 1 : $status); |
104
|
|
|
$aggregate[$class][$line] = $status; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
5 |
|
$this->setData($aggregate); |
109
|
|
|
} |
110
|
|
|
|
111
|
9 |
|
public function __call($name, $arguments) |
112
|
|
|
{ |
113
|
9 |
|
$codeCoverage = $this->codeCoverage; |
114
|
9 |
|
if (method_exists($codeCoverage, $name)) { |
115
|
8 |
|
return \call_user_func_array([$codeCoverage, $name], $arguments); |
116
|
|
|
} |
117
|
1 |
|
throw new ProcessorException('Method name: '.$name.' not supported.'); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|