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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
<?php
declare( strict_types = 1 );
namespace MediaWiki\Composer\PhpUnitSplitter;
use DOMDocument;
use SimpleXMLElement;
/**
* @license GPL-2.0-or-later
*/
class PhpUnitXml {
public const PHP_UNIT_XML_FILE = "phpunit.xml";
private SimpleXMLElement $xml;
public function __construct( string $phpUnitXmlFile ) {
$this->xml = new SimpleXMLElement( file_get_contents( $phpUnitXmlFile ) );
}
public static function isPhpUnitXmlPrepared( string $targetFile ): bool {
if ( !file_exists( $targetFile ) ) {
return false;
}
$unitFile = new PhpUnitXml( $targetFile );
return $unitFile->containsSplitGroups();
}
public function containsSplitGroups(): bool {
if ( !property_exists( $this->xml, "testsuites" ) ||
!property_exists( $this->xml->testsuites, "testsuite" ) ) {
return false;
}
foreach ( $this->xml->testsuites->testsuite as $child ) {
if ( isset( $child->attributes()["name"] ) &&
strpos( (string)$child->attributes()["name"], "split_group_" ) === 0 ) {
return true;
}
}
return false;
}
public function addSplitGroups( array $splitGroups ) {
$groups = count( $splitGroups );
for ( $i = 0; $i < $groups; $i++ ) {
$suite = $this->xml->testsuites->addChild( "testsuite" );
$suite->addAttribute( "name", "split_group_" . $i );
$group = $splitGroups[$i];
if ( !empty( $group["list"] ) ) {
foreach ( $group["list"] as $file ) {
$suite->addChild( "file", $file );
}
}
}
}
/**
* @throws SuiteGenerationException
*/
private function getSplitGroupSuite( int $groupId ): SimpleXMLElement {
foreach ( $this->xml->testsuites->testsuite as $child ) {
if ( isset( $child->attributes()["name"] ) &&
(string)$child->attributes()["name"] === "split_group_" . $groupId ) {
return $child;
}
}
throw new SuiteGenerationException( $groupId );
}
/**
* There are some tests suites / classes where the test listing does not work because test
* cases are generated dynamically. For this special cases, we need to add the classes
* manually back into the suites list to ensure that they get included in a test run.
* @see T345481
* @see T358394
* @throws SuiteGenerationException
*/
public function addSpecialCaseTests( int $groupCount ) {
$suite = $this->xml->testsuites->addChild( "testsuite" );
$suite->addAttribute( "name", "split_group_" . ( $groupCount - 1 ) );
$suite->addChild( "file", "tests/phpunit/suites/ExtensionsParserTestSuite.php" );
$sandboxTest = "extensions/Scribunto/tests/phpunit/Engines/LuaSandbox/SandboxTest.php";
if ( file_exists( $sandboxTest ) ) {
$suite = $this->getSplitGroupSuite( 0 );
$suite->addChild( "file", $sandboxTest );
}
}
public function saveToDisk( string $targetXml ) {
$dom = new DOMDocument( '1.0' );
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML( $this->xml->asXML() );
file_put_contents( $targetXml, $dom->saveXML() );
}
}
|