MediaWiki master
PhpUnitXml.php
Go to the documentation of this file.
1<?php
2
3declare( strict_types = 1 );
4
6
7use DOMDocument;
8use SimpleXMLElement;
9
14
15 private SimpleXMLElement $xml;
16
17 public function __construct( string $phpUnitXmlFile ) {
18 $this->xml = new SimpleXMLElement( file_get_contents( $phpUnitXmlFile ) );
19 }
20
21 public static function isPhpUnitXmlPrepared( string $targetFile ): bool {
22 if ( !file_exists( $targetFile ) ) {
23 return false;
24 }
25 $unitFile = new PhpUnitXml( $targetFile );
26 return $unitFile->containsSplitGroups();
27 }
28
29 public function containsSplitGroups(): bool {
30 if ( !property_exists( $this->xml, "testsuites" ) ||
31 !property_exists( $this->xml->testsuites, "testsuite" ) ) {
32 return false;
33 }
34 foreach ( $this->xml->testsuites->testsuite as $child ) {
35 if ( isset( $child->attributes()["name"] ) &&
36 strpos( (string)$child->attributes()["name"], "split_group_" ) === 0 ) {
37 return true;
38 }
39 }
40 return false;
41 }
42
43 public function addSplitGroups( array $splitGroups ) {
44 $groups = count( $splitGroups );
45 for ( $i = 0; $i < $groups; $i++ ) {
46 $suite = $this->xml->testsuites->addChild( "testsuite" );
47 $suite->addAttribute( "name", "split_group_" . $i );
48 $group = $splitGroups[$i];
49 if ( !empty( $group["list"] ) ) {
50 foreach ( $group["list"] as $file ) {
51 $suite->addChild( "file", $file );
52 }
53 }
54 }
55 }
56
60 private function getSplitGroupSuite( int $groupId ): SimpleXMLElement {
61 foreach ( $this->xml->testsuites->testsuite as $child ) {
62 if ( isset( $child->attributes()["name"] ) &&
63 (string)$child->attributes()["name"] === "split_group_" . $groupId ) {
64 return $child;
65 }
66 }
67 throw new SuiteGenerationException( $groupId );
68 }
69
78 public function addSpecialCaseTests( int $groupCount ) {
79 $suite = $this->xml->testsuites->addChild( "testsuite" );
80 $suite->addAttribute( "name", "split_group_" . ( $groupCount - 1 ) );
81 $suite->addChild( "file", "tests/phpunit/suites/ExtensionsParserTestSuite.php" );
82
83 $sandboxTest = "extensions/Scribunto/tests/phpunit/Engines/LuaSandbox/SandboxTest.php";
84 if ( file_exists( $sandboxTest ) ) {
85 $suite = $this->getSplitGroupSuite( 0 );
86 $suite->addChild( "file", $sandboxTest );
87 }
88 }
89
90 public function saveToDisk( string $targetXml ) {
91 $dom = new DOMDocument( '1.0' );
92 $dom->preserveWhiteSpace = false;
93 $dom->formatOutput = true;
94 $dom->loadXML( $this->xml->asXML() );
95 file_put_contents( $targetXml, $dom->saveXML() );
96 }
97
98}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:82
addSpecialCaseTests(int $groupCount)
There are some tests suites / classes where the test listing does not work because test cases are gen...
static isPhpUnitXmlPrepared(string $targetFile)