1
|
|
|
<?php |
2
|
|
|
namespace exussum12\CoverageChecker; |
3
|
|
|
|
4
|
|
|
use Exception; |
5
|
|
|
|
6
|
|
|
class PhpunitFilter |
7
|
|
|
{ |
8
|
|
|
protected $diff; |
9
|
|
|
protected $matcher; |
10
|
|
|
protected $coverage; |
11
|
|
|
public function __construct(DiffFileLoader $diff, FileMatcher $matcher, $coveragePhp) |
12
|
|
|
{ |
13
|
|
|
if (!is_readable(($coveragePhp))) { |
14
|
|
|
throw new Exception("Coverage File not found"); |
15
|
|
|
} |
16
|
|
|
$this->coverage = include($coveragePhp); |
17
|
|
|
$this->diff = $diff; |
18
|
|
|
$this->matcher = $matcher; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function getTestsForRunning($fuzziness = 0) |
22
|
|
|
{ |
23
|
|
|
$changes = $this->diff->getChangedLines(); |
24
|
|
|
$testData = $this->coverage->getData(); |
25
|
|
|
$fileNames = array_keys($testData); |
26
|
|
|
$runTests = []; |
27
|
|
|
foreach ($changes as $file => $lines) { |
28
|
|
|
try { |
29
|
|
|
if ($found = $this->matcher->match($file, $fileNames)) { |
30
|
|
|
foreach ($lines as $line) { |
31
|
|
|
$runTests = $this->matchFuzzyLines($fuzziness, $testData, $found, $line, $runTests); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
} catch (Exception $e) { |
35
|
|
|
if ($this->endsWith($file, ".php")) { |
36
|
|
|
$runTests[] = $this->stripFileExtension($file); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
return $this->groupTestsBySuite($runTests); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function endsWith(string $haystack, string $needle) |
44
|
|
|
{ |
45
|
|
|
$length = strlen($needle); |
46
|
|
|
return (substr($haystack, -$length) === $needle); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function stripFileExtension(string $file) |
50
|
|
|
{ |
51
|
|
|
$ext = ".php"; |
52
|
|
|
return str_replace('/', '\\', substr($file, 0, -strlen($ext))); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function groupTestsBySuite(array $tests) |
56
|
|
|
{ |
57
|
|
|
$groupedTests = []; |
58
|
|
|
foreach ($tests as $test) { |
59
|
|
|
$suite = $test; |
60
|
|
|
$testName = ''; |
61
|
|
|
|
62
|
|
|
if (strpos($test, '::') > 0) { |
63
|
|
|
list ($suite, $testName) = explode('::', $test); |
64
|
|
|
} |
65
|
|
|
$groupedTests[$suite][] = $testName; |
66
|
|
|
} |
67
|
|
|
return $groupedTests; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function matchFuzzyLines( |
71
|
|
|
int $fuzziness, |
72
|
|
|
array $testData, |
73
|
|
|
string $found, |
74
|
|
|
int $line, |
75
|
|
|
array $runTests |
76
|
|
|
) { |
77
|
|
|
$index = -$fuzziness; |
78
|
|
|
do { |
79
|
|
|
if (isset($testData[$found][$line + $index])) { |
80
|
|
|
$runTests = array_unique( |
81
|
|
|
array_merge( |
82
|
|
|
$runTests, |
83
|
|
|
$testData[$found][$line] |
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} while (++$index < $fuzziness); |
88
|
|
|
|
89
|
|
|
return $runTests; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|