1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPRealCoverage; |
4
|
|
|
|
5
|
|
|
use PHPRealCoverage\Mutator\MutationGenerator; |
6
|
|
|
use PHPRealCoverage\Mutator\MutationTester; |
7
|
|
|
use PHPRealCoverage\Mutator\Mutator; |
8
|
|
|
use PHPRealCoverage\Proxy\ClassMetadata; |
9
|
|
|
use PHPRealCoverage\Proxy\ProxyAutoloader; |
10
|
|
|
use PHPRealCoverage\Proxy\ProxyFactory; |
11
|
|
|
use PHPRealCoverage\TestRunner\MultirunTestCommand; |
12
|
|
|
use PHPRealCoverage\TestRunner\PHPUnitRunner; |
13
|
|
|
|
14
|
|
|
class RealCoverageRun |
15
|
|
|
{ |
16
|
|
|
public function run($input, $output = 'real-coverage-html') |
17
|
|
|
{ |
18
|
|
|
$report = unserialize(file_get_contents($input)); |
19
|
|
|
$reader = new ParsingCoverageReader(); |
20
|
|
|
$classes = $reader->parseReport($report); |
21
|
|
|
$mutator = new Mutator(); |
22
|
|
|
$testRunner = new PHPUnitRunner(new MultirunTestCommand(), array('tests', 'tests')); |
23
|
|
|
$writer = new RealCoverageModifier($report); |
24
|
|
|
|
25
|
|
|
$proxyFactory = new ProxyFactory($classes); |
26
|
|
|
$autoloader = new ProxyAutoloader($proxyFactory); |
27
|
|
|
$autoloader->register(); |
28
|
|
|
|
29
|
|
|
$classCounter = 0; |
30
|
|
|
/** @var ClassMetadata $class */ |
31
|
|
|
foreach ($classes as $class) { |
32
|
|
|
if (!$class->isCovered()) { |
33
|
|
|
continue; |
34
|
|
|
} |
35
|
|
|
echo "\n" . (int)(++$classCounter * 100 / count($classes)) . "%: Processing " . $class->getName() . "\n"; |
36
|
|
|
|
37
|
|
|
$this->calculateRealCoverage( |
38
|
|
|
$class, |
39
|
|
|
$proxyFactory, |
40
|
|
|
$testRunner, |
41
|
|
|
$mutator |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$writer->write($class); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$autoloader->unregister(); |
48
|
|
|
|
49
|
|
|
echo "\n\nWriting coverage report to " . $output . "\n"; |
50
|
|
|
$htmlWriter = new \PHP_CodeCoverage_Report_HTML(); |
51
|
|
|
$htmlWriter->process($report, $output); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param $class |
56
|
|
|
* @param $proxyFactory |
57
|
|
|
* @param $testRunner |
58
|
|
|
* @param $mutator |
59
|
|
|
* @throws \Exception |
60
|
|
|
*/ |
61
|
|
|
private function calculateRealCoverage( |
62
|
|
|
$class, |
63
|
|
|
ProxyFactory $proxyFactory, |
64
|
|
|
PHPUnitRunner $testRunner, |
65
|
|
|
Mutator $mutator |
66
|
|
|
) { |
67
|
|
|
$proxy = $proxyFactory->getProxy($class); |
68
|
|
|
$tester = new ProxiedMutationTester($proxy, $class, $testRunner); |
69
|
|
|
$this->testPrecondition($tester); |
70
|
|
|
|
71
|
|
|
$mutator->testMutations($tester, new MutationGenerator($class)); |
72
|
|
|
$proxy->loadClass($class); |
73
|
|
|
|
74
|
|
|
$this->testPostcondition($tester); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param $tester |
79
|
|
|
* @throws \Exception |
80
|
|
|
*/ |
81
|
|
|
private function testPrecondition(MutationTester $tester) |
82
|
|
|
{ |
83
|
|
|
if (!$tester->isValid()) { |
84
|
|
|
throw new \Exception("Tester did not reach a valid state before mutation"); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $tester |
90
|
|
|
* @throws \Exception |
91
|
|
|
*/ |
92
|
|
|
private function testPostcondition(MutationTester $tester) |
93
|
|
|
{ |
94
|
|
|
if (!$tester->isValid()) { |
95
|
|
|
throw new \Exception("Tester did not reach a valid state after mutation"); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|