|
1
|
|
|
<?php |
|
2
|
|
|
namespace Hal\Metric\System\UnitTesting; |
|
3
|
|
|
|
|
4
|
|
|
use Hal\Application\Config\Config; |
|
5
|
|
|
use Hal\Application\Config\ConfigException; |
|
6
|
|
|
use Hal\Metric\Class_\ClassEnumVisitor; |
|
7
|
|
|
use Hal\Metric\Class_\Coupling\ExternalsVisitor; |
|
8
|
|
|
use Hal\Metric\ClassMetric; |
|
9
|
|
|
use Hal\Metric\Metrics; |
|
10
|
|
|
use Hal\Metric\ProjectMetric; |
|
11
|
|
|
use PhpParser\ParserFactory; |
|
12
|
|
|
|
|
13
|
|
|
class UnitTesting |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
private $files = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Config |
|
23
|
|
|
*/ |
|
24
|
|
|
private $config; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* GitChanges constructor. |
|
28
|
|
|
* @param array $files |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(Config $config, array $files) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->files = $files; |
|
33
|
|
|
$this->config = $config; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param Metrics $metrics |
|
38
|
|
|
* @throws ConfigException |
|
39
|
|
|
*/ |
|
40
|
|
|
public function calculate(Metrics $metrics) |
|
41
|
|
|
{ |
|
42
|
|
|
|
|
43
|
|
|
if (!$this->config->has('junit')) { |
|
44
|
|
|
return; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// parse junit file |
|
48
|
|
|
$filename = $this->config->get('junit'); |
|
|
|
|
|
|
49
|
|
|
if (!file_exists($filename) || !is_readable($filename)) { |
|
50
|
|
|
throw new ConfigException('JUnit report cannot be read'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$unitsTests = []; |
|
54
|
|
|
$infoAboutTests = []; |
|
55
|
|
|
$projectMetric = new ProjectMetric('unitTesting'); |
|
56
|
|
|
|
|
57
|
|
|
// injects default value for each metric |
|
58
|
|
|
foreach ($metrics->all() as $metric) { |
|
59
|
|
|
$metric->set('numberOfUnitTests', 0); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// parsing of XML file without any dependency to DomDocument or simpleXML |
|
63
|
|
|
// we want to be compatible with every platforms |
|
64
|
|
|
$xml = file_get_contents($filename); |
|
65
|
|
|
if (preg_match_all('!<testsuite name="(.*?)" file="(.*?)"!i', $xml, $matches, PREG_SET_ORDER)) { |
|
66
|
|
|
foreach ($matches as $m) { |
|
67
|
|
|
list(, $classname, $fileOfUnitTest) = $m; |
|
68
|
|
|
$unitsTests[$fileOfUnitTest] = $classname; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// analyze each unit test |
|
73
|
|
|
// This code is slow and can be optimized |
|
74
|
|
|
foreach ($unitsTests as $filename => $classname) { |
|
75
|
|
|
$metricsOfUnitTest = new Metrics(); |
|
76
|
|
|
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7); |
|
77
|
|
|
$traverser = new \PhpParser\NodeTraverser(); |
|
78
|
|
|
$traverser->addVisitor(new \PhpParser\NodeVisitor\NameResolver()); |
|
79
|
|
|
$traverser->addVisitor(new ClassEnumVisitor($metricsOfUnitTest)); |
|
80
|
|
|
$traverser->addVisitor(new ExternalsVisitor($metricsOfUnitTest)); |
|
81
|
|
|
|
|
82
|
|
|
$code = file_get_contents($filename); |
|
83
|
|
|
$stmts = $parser->parse($code); |
|
84
|
|
|
$traverser->traverse($stmts); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
if (!$metricsOfUnitTest->has($classname)) { |
|
87
|
|
|
continue; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// list of externals sources of unit test |
|
91
|
|
|
$metric = $metricsOfUnitTest->get($classname); |
|
|
|
|
|
|
92
|
|
|
$externals = (array)$metric->get('externals'); |
|
93
|
|
|
|
|
94
|
|
|
// global stats for each test |
|
95
|
|
|
$infoAboutTests[$classname] = (object)[ |
|
96
|
|
|
'nbExternals' => sizeof(array_unique($externals)), |
|
97
|
|
|
'externals' => array_unique($externals), |
|
98
|
|
|
'filename' => $fileOfUnitTest, |
|
|
|
|
|
|
99
|
|
|
'classname' => $classname |
|
100
|
|
|
]; |
|
101
|
|
|
|
|
102
|
|
|
foreach ($externals as $external) { |
|
103
|
|
|
|
|
104
|
|
|
// search for this external in metrics |
|
105
|
|
|
if (!$metrics->has($external)) { |
|
106
|
|
|
continue; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// SUT (tested class) has unit test |
|
110
|
|
|
$numberOfUnitTest = $metrics->get($external)->get('numberOfUnitTests'); |
|
|
|
|
|
|
111
|
|
|
$numberOfUnitTest++; |
|
112
|
|
|
$metrics->get($external)->set('numberOfUnitTests', $numberOfUnitTest); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// statistics |
|
117
|
|
|
$sum = 0; |
|
118
|
|
|
$nb = 0; |
|
119
|
|
|
foreach ($metrics->all() as $metric) { |
|
120
|
|
|
if (!$metric instanceof ClassMetric) { |
|
121
|
|
|
continue; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$sum++; |
|
125
|
|
|
if ($metric->get('numberOfUnitTests') > 0) { |
|
126
|
|
|
$nb++; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$projectMetric->set('tests', $infoAboutTests); |
|
131
|
|
|
$projectMetric->set('nbTests', sizeof($unitsTests)); |
|
132
|
|
|
$projectMetric->set('nbCoveredClasses', $nb); |
|
133
|
|
|
$projectMetric->set('percentCoveredClasses', round($nb / $sum * 100, 2)); |
|
134
|
|
|
$projectMetric->set('nbUncoveredClasses', $sum - $nb); |
|
135
|
|
|
$projectMetric->set('percentUncoveredClasses', round(($sum - $nb) / $sum * 100, 2)); |
|
136
|
|
|
|
|
137
|
|
|
$metrics->attach($projectMetric); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.