| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types = 1 ); |
| 4 |
|
| 5 |
namespace MediaWiki\Composer\PhpUnitSplitter; |
| 6 |
|
| 7 |
use DOMDocument; |
| 8 |
use SimpleXMLElement; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class PhpUnitXml { |
| 14 |
|
| 15 |
public const PHP_UNIT_XML_FILE = "phpunit.xml"; |
| 16 |
|
| 17 |
private SimpleXMLElement $xml; |
| 18 |
|
| 19 |
public function __construct( string $phpUnitXmlFile ) { |
| 20 |
$this->xml = new SimpleXMLElement( file_get_contents( $phpUnitXmlFile ) ); |
| 21 |
} |
| 22 |
|
| 23 |
public static function isPhpUnitXmlPrepared( string $targetFile ): bool { |
| 24 |
if ( !file_exists( $targetFile ) ) { |
| 25 |
return false; |
| 26 |
} |
| 27 |
$unitFile = new PhpUnitXml( $targetFile ); |
| 28 |
return $unitFile->containsSplitGroups(); |
| 29 |
} |
| 30 |
|
| 31 |
public function containsSplitGroups(): bool { |
| 32 |
if ( !property_exists( $this->xml, "testsuites" ) || |
| 33 |
!property_exists( $this->xml->testsuites, "testsuite" ) ) { |
| 34 |
return false; |
| 35 |
} |
| 36 |
foreach ( $this->xml->testsuites->testsuite as $child ) { |
| 37 |
if ( isset( $child->attributes()["name"] ) && |
| 38 |
strpos( (string)$child->attributes()["name"], "split_group_" ) === 0 ) { |
| 39 |
return true; |
| 40 |
} |
| 41 |
} |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
public function addSplitGroups( array $splitGroups ) { |
| 46 |
$groups = count( $splitGroups ); |
| 47 |
for ( $i = 0; $i < $groups; $i++ ) { |
| 48 |
$suite = $this->xml->testsuites->addChild( "testsuite" ); |
| 49 |
$suite->addAttribute( "name", "split_group_" . $i ); |
| 50 |
$group = $splitGroups[$i]; |
| 51 |
if ( !empty( $group["list"] ) ) { |
| 52 |
foreach ( $group["list"] as $file ) { |
| 53 |
$suite->addChild( "file", $file ); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
private function getSplitGroupSuite( int $groupId ): SimpleXMLElement { |
| 63 |
foreach ( $this->xml->testsuites->testsuite as $child ) { |
| 64 |
if ( isset( $child->attributes()["name"] ) && |
| 65 |
(string)$child->attributes()["name"] === "split_group_" . $groupId ) { |
| 66 |
return $child; |
| 67 |
} |
| 68 |
} |
| 69 |
throw new SuiteGenerationException( $groupId ); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public function addSpecialCaseTests( int $groupCount ) { |
| 80 |
$suite = $this->xml->testsuites->addChild( "testsuite" ); |
| 81 |
$suite->addAttribute( "name", "split_group_" . ( $groupCount - 1 ) ); |
| 82 |
$suite->addChild( "file", "tests/phpunit/suites/ExtensionsParserTestSuite.php" ); |
| 83 |
} |
| 84 |
|
| 85 |
public function saveToDisk( string $targetXml ) { |
| 86 |
$dom = new DOMDocument( '1.0' ); |
| 87 |
$dom->preserveWhiteSpace = false; |
| 88 |
$dom->formatOutput = true; |
| 89 |
$dom->loadXML( $this->xml->asXML() ); |
| 90 |
file_put_contents( $targetXml, $dom->saveXML() ); |
| 91 |
} |
| 92 |
|
| 93 |
} |