1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace PHPChunkit; |
6
|
|
|
|
7
|
|
|
use ReflectionClass; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @testClass PHPChunkit\Test\TestCounterTest |
11
|
|
|
*/ |
12
|
|
|
class TestCounter |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var FileClassesHelper |
16
|
|
|
*/ |
17
|
|
|
private $fileClassesHelper; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $cache = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $cachePath = ''; |
28
|
|
|
|
29
|
7 |
|
public function __construct(FileClassesHelper $fileClassesHelper) |
30
|
|
|
{ |
31
|
7 |
|
$this->fileClassesHelper = $fileClassesHelper; |
32
|
7 |
|
$this->cachePath = sprintf('%s/testcounter.cache', sys_get_temp_dir()); |
33
|
|
|
|
34
|
7 |
|
$this->loadCache(); |
35
|
7 |
|
} |
36
|
|
|
|
37
|
1 |
|
public function __destruct() |
38
|
|
|
{ |
39
|
1 |
|
$this->writeCache(); |
40
|
1 |
|
} |
41
|
|
|
|
42
|
2 |
|
public function countNumTestsInFile(string $file) : int |
43
|
|
|
{ |
44
|
2 |
|
$cacheKey = $file.@filemtime($file); |
45
|
|
|
|
46
|
2 |
|
if (isset($this->cache[$cacheKey])) { |
47
|
1 |
|
return $this->cache[$cacheKey]; |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
$numTestsInFile = 0; |
51
|
|
|
|
52
|
2 |
|
$classes = $this->fileClassesHelper->getFileClasses($file); |
53
|
|
|
|
54
|
2 |
|
if (empty($classes)) { |
55
|
|
|
$this->cache[$cacheKey] = $numTestsInFile; |
56
|
|
|
|
57
|
|
|
return $numTestsInFile; |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
$className = $classes[0]; |
61
|
|
|
|
62
|
2 |
|
require_once $file; |
63
|
|
|
|
64
|
2 |
|
foreach ($classes as $className) { |
65
|
2 |
|
$numTestsInFile += $this->countNumTestsInClass($className); |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
$this->cache[$cacheKey] = $numTestsInFile; |
69
|
|
|
|
70
|
2 |
|
return $numTestsInFile; |
71
|
|
|
} |
72
|
|
|
|
73
|
7 |
|
public function clearCache() |
74
|
|
|
{ |
75
|
7 |
|
if (file_exists($this->cachePath)) { |
76
|
1 |
|
unlink($this->cachePath); |
77
|
|
|
} |
78
|
|
|
|
79
|
7 |
|
$this->cache = []; |
80
|
7 |
|
} |
81
|
|
|
|
82
|
1 |
|
protected function loadCache() |
83
|
|
|
{ |
84
|
1 |
|
if (file_exists($this->cachePath)) { |
85
|
|
|
$this->cache = include($this->cachePath); |
86
|
|
|
} |
87
|
1 |
|
} |
88
|
|
|
|
89
|
1 |
|
protected function writeCache() |
90
|
|
|
{ |
91
|
1 |
|
file_put_contents($this->cachePath, '<?php return '.var_export($this->cache, true).';'); |
92
|
1 |
|
} |
93
|
|
|
|
94
|
2 |
|
private function countNumTestsInClass(string $className) : int |
95
|
|
|
{ |
96
|
2 |
|
$reflectionClass = new ReflectionClass($className); |
97
|
|
|
|
98
|
2 |
|
if ($reflectionClass->isAbstract()) { |
99
|
2 |
|
return 0; |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
$numTests = 0; |
103
|
|
|
|
104
|
2 |
|
$methods = $reflectionClass->getMethods(); |
105
|
|
|
|
106
|
2 |
|
foreach ($methods as $method) { |
107
|
2 |
|
if (strpos($method->name, 'test') === 0) { |
108
|
2 |
|
$docComment = $method->getDocComment(); |
109
|
|
|
|
110
|
2 |
|
if ($docComment) { |
111
|
2 |
|
preg_match_all('/@dataProvider\s([a-zA-Z0-9_]+)/', $docComment, $dataProvider); |
112
|
|
|
|
113
|
2 |
|
if (isset($dataProvider[1][0])) { |
114
|
2 |
|
$providerMethod = $dataProvider[1][0]; |
115
|
|
|
|
116
|
2 |
|
$test = new $className(); |
117
|
|
|
|
118
|
2 |
|
$numTests = $numTests + count($test->$providerMethod()); |
119
|
|
|
|
120
|
2 |
|
continue; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
$numTests++; |
125
|
|
|
} else { |
126
|
2 |
|
$docComment = $method->getDocComment(); |
127
|
|
|
|
128
|
2 |
|
if ($docComment) { |
129
|
2 |
|
preg_match_all('/@test/', $docComment, $tests); |
130
|
|
|
|
131
|
2 |
|
if ($tests[0]) { |
132
|
2 |
|
$numTests = $numTests + count($tests[0]); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
2 |
|
return $numTests; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|