forked from alefesouza/laravel-vue-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphpunit-log-junit-fix.php
49 lines (37 loc) · 1.24 KB
/
phpunit-log-junit-fix.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/**
* @author Alefe Souza <[email protected]>
*
* Remove nested <testsuite> tags from PHPUnit --log-junit generated file.
* I've created it after I wanted to run tests on Visual Studio Team Services CI,
* but an error occurred because of these nested tags.
*
* Use it:
*
* php phpunit-junit-log-fix.php <phpunit junit log>.xml <new file>.xml
*/
$argvCount = count($argv);
if ($argvCount === 1) {
exit('Please type the name of the generated PHPUnit --log-junit file.');
}
$importFile = $argv[1];
$exportFile = isset($argv[2]) ? $argv[2] : $importFile;
try {
if(!file_exists($importFile)) {
exit('File not found.');
}
$content = file_get_contents($importFile);
$xml = new SimpleXMLElement($content, LIBXML_NOERROR);
if($xml->getName() !== 'testsuites') {
exit('It does not look to be a PHPUnit --log-junit file.');
}
} catch(Exception $e) {
exit('It does not look to be a valid XML file.');
}
$xpath = $xml->xpath('//testcase/parent::testsuite');
$newValue = '';
foreach($xpath as $node){
$newValue .= $node->asXML();
}
$newXML = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><testsuites>'.$newValue.'</testsuites>');
file_put_contents($exportFile, $newXML->asXML());