|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\PHPQA; |
|
4
|
|
|
|
|
5
|
|
|
class PHPUnitRerunCommandGenerator |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* no failed tests so just include all |
|
9
|
|
|
* however, this can confuse bash - try using `set -f` to disable globbing |
|
10
|
|
|
*/ |
|
11
|
|
|
public const NO_FILTER = '/.*/'; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
private $logPath; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var \SimpleXMLElement |
|
20
|
|
|
*/ |
|
21
|
|
|
private $simpleXml; |
|
22
|
|
|
|
|
23
|
|
|
private $toRerun = []; |
|
24
|
|
|
|
|
25
|
|
|
public function main(string $junitLogPath = null): string |
|
26
|
|
|
{ |
|
27
|
|
|
$this->toRerun = []; |
|
28
|
|
|
$this->logPath = $junitLogPath ?? $this->getDefaultFilePath(); |
|
29
|
|
|
if (!file_exists($this->logPath)) { |
|
30
|
|
|
return self::NO_FILTER; |
|
31
|
|
|
} |
|
32
|
|
|
$contents = file_get_contents($this->logPath); |
|
33
|
|
|
if ('' === $contents) { |
|
34
|
|
|
return self::NO_FILTER; |
|
35
|
|
|
} |
|
36
|
|
|
$this->load($contents); |
|
37
|
|
|
$failureNodes = $this->simpleXml->xpath( |
|
38
|
|
|
'//testsuite/testcase[error] | //testsuite/testcase[failure] ' |
|
39
|
|
|
.'| //testsuite/testcase[skipped] | //testsuite/testcase[incomplete]' |
|
40
|
|
|
); |
|
41
|
|
|
foreach ($failureNodes as $testCaseNode) { |
|
42
|
|
|
$attributes = $testCaseNode->attributes(); |
|
43
|
|
|
$class = str_replace('\\', '\\\\', (string)$attributes->class); |
|
44
|
|
|
$this->toRerun[$class][] = (string)$attributes->name; |
|
45
|
|
|
} |
|
46
|
|
|
if ($this->toRerun === []) { |
|
47
|
|
|
return self::NO_FILTER; |
|
48
|
|
|
} |
|
49
|
|
|
$command = '/('; |
|
50
|
|
|
foreach ($this->toRerun as $class => $testNames) { |
|
51
|
|
|
foreach ($testNames as $testName) { |
|
52
|
|
|
$command .= "$class::$testName|"; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
$command = rtrim($command, '|'); |
|
56
|
|
|
$command .= ')/'; |
|
57
|
|
|
|
|
58
|
|
|
return $command; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
protected function load(string $contents) |
|
63
|
|
|
{ |
|
64
|
|
|
|
|
65
|
|
|
libxml_use_internal_errors(true); |
|
66
|
|
|
$this->simpleXml = simplexml_load_string($contents); |
|
|
|
|
|
|
67
|
|
|
if (false === $this->simpleXml) { |
|
68
|
|
|
$message = "Failed loading XML\n"; |
|
69
|
|
|
foreach (libxml_get_errors() as $error) { |
|
70
|
|
|
$message .= "\n\t".$error->message; |
|
71
|
|
|
} |
|
72
|
|
|
throw new \RuntimeException($message); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return string |
|
78
|
|
|
* @throws \Exception |
|
79
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function getDefaultFilePath(): string |
|
82
|
|
|
{ |
|
83
|
|
|
return Helper::getProjectRootDirectory().'/var/qa/phpunit.junit.log.xml'; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.