1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\PHPQA\PHPUnit; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\PHPQA\Helper; |
6
|
|
|
|
7
|
|
|
class RerunCommandGenerator |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* no failed tests so just include all |
11
|
|
|
* however, this can confuse bash - try using `set -f` to disable globbing |
12
|
|
|
*/ |
13
|
|
|
public const NO_FILTER = '/.*/'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $logPath; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \SimpleXMLElement |
22
|
|
|
*/ |
23
|
|
|
private $simpleXml; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $toRerun = []; |
29
|
|
|
|
30
|
5 |
|
public function main(string $junitLogPath = null): string |
31
|
|
|
{ |
32
|
5 |
|
$this->toRerun = []; |
33
|
5 |
|
$this->logPath = $junitLogPath ?? $this->getDefaultFilePath(); |
34
|
5 |
|
if (!file_exists($this->logPath)) { |
35
|
1 |
|
return self::NO_FILTER; |
36
|
|
|
} |
37
|
4 |
|
$contents = (string)file_get_contents($this->logPath); |
38
|
4 |
|
if ('' === $contents) { |
39
|
1 |
|
return self::NO_FILTER; |
40
|
|
|
} |
41
|
3 |
|
$failureNodes = $this->getFailureNodes($contents); |
42
|
2 |
|
foreach ($failureNodes as $testCaseNode) { |
43
|
1 |
|
$attributes = $testCaseNode->attributes(); |
44
|
1 |
|
if ($attributes instanceof \SimpleXMLElement) { |
45
|
1 |
|
list($class, $name) = $this->getClassAndName($attributes); |
46
|
1 |
|
$this->toRerun[$class][] = $name; |
47
|
|
|
} |
48
|
|
|
} |
49
|
2 |
|
if ($this->toRerun === []) { |
50
|
1 |
|
return self::NO_FILTER; |
51
|
|
|
} |
52
|
1 |
|
$command = '/('; |
53
|
1 |
|
foreach ($this->toRerun as $class => $testNames) { |
54
|
1 |
|
foreach ($testNames as $testName) { |
55
|
1 |
|
$command .= "$class::$testName|"; |
56
|
|
|
} |
57
|
|
|
} |
58
|
1 |
|
$command = rtrim($command, '|'); |
59
|
1 |
|
$command .= ')/'; |
60
|
|
|
|
61
|
1 |
|
return $command; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
private function getClassAndName(\SimpleXMLElement $attributes): array |
65
|
|
|
{ |
66
|
1 |
|
$class = $name = null; |
67
|
1 |
|
foreach ($attributes as $attribute) { |
68
|
1 |
|
if ('class' === $attribute->getName()) { |
69
|
1 |
|
$class = str_replace('\\', '\\\\', $attribute->__toString()); |
70
|
1 |
|
continue; |
71
|
|
|
} |
72
|
1 |
|
if ('name' === $attribute->getName()) { |
73
|
1 |
|
$name = $attribute->__toString(); |
74
|
1 |
|
continue; |
75
|
|
|
} |
76
|
|
|
} |
77
|
1 |
|
if (null === $class || null === $name) { |
78
|
|
|
throw new \RuntimeException( |
79
|
|
|
'Failed finding the class and/or name in the attributes:'.$attributes->__toString() |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
return [$class, $name]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $contents |
88
|
|
|
* |
89
|
|
|
* @return array|\SimpleXMLElement[] |
90
|
|
|
*/ |
91
|
3 |
|
private function getFailureNodes(string $contents): array |
92
|
|
|
{ |
93
|
3 |
|
$this->load($contents); |
94
|
|
|
|
95
|
2 |
|
$nodes = $this->simpleXml->xpath( |
96
|
|
|
'//testsuite/testcase[error] | //testsuite/testcase[failure] ' |
97
|
2 |
|
.'| //testsuite/testcase[skipped] | //testsuite/testcase[incomplete]' |
98
|
|
|
); |
99
|
2 |
|
if (false === $nodes) { |
100
|
|
|
return []; |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
return $nodes; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
3 |
|
private function load(string $contents): void |
108
|
|
|
{ |
109
|
|
|
|
110
|
3 |
|
\libxml_use_internal_errors(true); |
111
|
3 |
|
$sXml = \simplexml_load_string($contents); |
112
|
3 |
|
if (false === $sXml) { |
113
|
1 |
|
$message = "Failed loading XML\n"; |
114
|
1 |
|
foreach (libxml_get_errors() as $error) { |
115
|
1 |
|
$message .= "\n\t".$error->message; |
116
|
|
|
} |
117
|
1 |
|
throw new \RuntimeException($message); |
118
|
|
|
} |
119
|
2 |
|
$this->simpleXml = $sXml; |
120
|
2 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return string |
124
|
|
|
* @throws \Exception |
125
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
126
|
|
|
*/ |
127
|
|
|
private function getDefaultFilePath(): string |
128
|
|
|
{ |
129
|
|
|
return Helper::getProjectRootDirectory().'/var/qa/phpunit.junit.log.xml'; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|