1
|
|
|
<?php |
2
|
|
|
namespace exussum12\CoverageChecker; |
3
|
|
|
|
4
|
|
|
use Exception; |
5
|
|
|
use PHPUnit_Framework_AssertionFailedError as AssertionFailedError; |
6
|
|
|
use PHPUnit_Framework_Test as Test; |
7
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
8
|
|
|
use PHPUnit_Framework_TestListener as TestListener; |
9
|
|
|
use PHPUnit_Framework_TestSuite as TestSuite; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @codeCoverageIgnore |
13
|
|
|
*/ |
14
|
|
|
class DiffFilter implements TestListener |
15
|
|
|
{ |
16
|
|
|
protected $modifiedTests = null; |
17
|
|
|
protected $modifiedSuites = null; |
18
|
|
|
public function __construct($old, $diff) |
19
|
|
|
{ |
20
|
|
|
try { |
21
|
|
|
$diff = new DiffFileLoader($diff); |
22
|
|
|
$matcher = new FileMatchers\EndsWith(); |
23
|
|
|
$coverage = new PhpunitFilter($diff, $matcher, $old); |
24
|
|
|
$this->modifiedTests = $coverage->getTestsForRunning(); |
25
|
|
|
$this->modifiedSuites = array_keys($this->modifiedTests); |
26
|
|
|
unset($coverage); |
27
|
|
|
} catch (Exception $exception) { |
28
|
|
|
//Something has gone wrong, Don't filter |
29
|
|
|
echo "Missing required diff / php coverage, Running all tests\n"; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function addError(Test $test, Exception $exception, $time) |
34
|
|
|
{ |
35
|
|
|
} |
36
|
|
|
public function addFailure(Test $test, AssertionFailedError $exception, $time) |
37
|
|
|
{ |
38
|
|
|
} |
39
|
|
|
public function addRiskyTest(Test $test, Exception $exception, $time) |
40
|
|
|
{ |
41
|
|
|
} |
42
|
|
|
public function startTestSuite(TestSuite $suite) |
43
|
|
|
{ |
44
|
|
|
if (!is_array($this->modifiedTests)) { |
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$suiteName = $suite->getName(); |
49
|
|
|
$runTests = []; |
50
|
|
|
if (empty($suiteName)) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$tests = $suite->tests(); |
55
|
|
|
|
56
|
|
|
foreach ($tests as $test) { |
57
|
|
|
$skipTest = |
58
|
|
|
$test instanceof TestCase && |
59
|
|
|
!$this->hasTestChanged( |
60
|
|
|
$test |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
if ($skipTest) { |
64
|
|
|
continue; |
65
|
|
|
} |
66
|
|
|
$runTests[]= $test; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$suite->setTests($runTests); |
70
|
|
|
} |
71
|
|
|
public function startTest(Test $test) |
72
|
|
|
{ |
73
|
|
|
} |
74
|
|
|
public function endTest(Test $test, $time) |
75
|
|
|
{ |
76
|
|
|
} |
77
|
|
|
public function addIncompleteTest(Test $test, Exception $e, $time) |
78
|
|
|
{ |
79
|
|
|
} |
80
|
|
|
public function addSkippedTest(Test $test, Exception $e, $time) |
81
|
|
|
{ |
82
|
|
|
} |
83
|
|
|
public function endTestSuite(TestSuite $suite) |
84
|
|
|
{ |
85
|
|
|
} |
86
|
|
|
public function onFatalError() |
87
|
|
|
{ |
88
|
|
|
} |
89
|
|
|
public function onCancel() |
90
|
|
|
{ |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function startsWith($haystack, $needle) |
94
|
|
|
{ |
95
|
|
|
$length = strlen($needle); |
96
|
|
|
return (substr($haystack, 0, $length) === $needle); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function shouldRunTest($modifiedTest, $currentTest, $class) |
100
|
|
|
{ |
101
|
|
|
foreach ($modifiedTest as $test) { |
102
|
|
|
$testName = $currentTest->getName(); |
103
|
|
|
$testMatches = |
104
|
|
|
strpos($class, get_class($currentTest)) !== false && |
105
|
|
|
( |
106
|
|
|
empty($test) || |
107
|
|
|
strpos($test, $testName) !== false |
108
|
|
|
) |
109
|
|
|
; |
110
|
|
|
if ($testMatches) { |
111
|
|
|
return true; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function hasTestChanged(TestCase $test) |
118
|
|
|
{ |
119
|
|
|
foreach ($this->modifiedTests as $class => $modifiedTest) { |
120
|
|
|
if ($this->shouldRunTest($modifiedTest, $test, $class)) { |
121
|
|
|
return true; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|