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