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 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
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
80 public function addSpecialCaseTests( int $groupCount ) {
81 $suite = $this->xml->testsuites->addChild( "testsuite" );
82 $suite->addAttribute( "name", "split_group_" . ( $groupCount - 1 ) );
83 $suite->addChild( "file", "tests/phpunit/suites/ExtensionsParserTestSuite.php" );
84
85 $sandboxTest = "extensions/Scribunto/tests/phpunit/Engines/LuaSandbox/SandboxTest.php";
86 if ( file_exists( $sandboxTest ) ) {
87 $suite = $this->getSplitGroupSuite( 0 );
88 $suite->addChild( "file", $sandboxTest );
89 }
90 }
91
92 public function saveToDisk( string $targetXml ) {
93 $dom = new DOMDocument( '1.0' );
94 $dom->preserveWhiteSpace = false;
95 $dom->formatOutput = true;
96 $dom->loadXML( $this->xml->asXML() );
97 file_put_contents( $targetXml, $dom->saveXML() );
98 }
99
100}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
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)