| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types = 1 ); |
| 4 |
|
| 5 |
namespace MediaWiki\Composer\PhpUnitSplitter; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class PhpUnitXmlManager { |
| 11 |
|
| 12 |
private string $rootDir; |
| 13 |
|
| 14 |
private string $testsListFile; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
private const EXPECTED_MISSING_CLASSES = [ |
| 26 |
"PHPUnit\\Framework\\SkippedTestCase", |
| 27 |
"MediaWiki\\Extension\\Scribunto\\Tests\\Engines\\LuaCommon\\LuaEngineTestSkip", |
| 28 |
"\\ParserIntegrationTest", |
| 29 |
]; |
| 30 |
|
| 31 |
public function __construct( string $rootDir, string $testsListFile ) { |
| 32 |
$this->rootDir = $rootDir; |
| 33 |
$this->testsListFile = $testsListFile; |
| 34 |
} |
| 35 |
|
| 36 |
private function getPhpUnitXmlTarget(): string { |
| 37 |
return $this->rootDir . DIRECTORY_SEPARATOR . PhpUnitXml::PHP_UNIT_XML_FILE; |
| 38 |
} |
| 39 |
|
| 40 |
private function getPhpUnitXmlDist(): string { |
| 41 |
return $this->rootDir . DIRECTORY_SEPARATOR . "phpunit.xml.dist"; |
| 42 |
} |
| 43 |
|
| 44 |
private function getTestsList(): string { |
| 45 |
return $this->rootDir . DIRECTORY_SEPARATOR . $this->testsListFile; |
| 46 |
} |
| 47 |
|
| 48 |
private function loadPhpUnitXmlDist(): PhpUnitXml { |
| 49 |
return $this->loadPhpUnitXml( $this->getPhpUnitXmlDist() ); |
| 50 |
} |
| 51 |
|
| 52 |
private function loadPhpUnitXml( string $targetFile ): PhpUnitXml { |
| 53 |
return new PhpUnitXml( $targetFile ); |
| 54 |
} |
| 55 |
|
| 56 |
private function loadTestClasses(): array { |
| 57 |
if ( !file_exists( $this->getTestsList() ) ) { |
| 58 |
throw new TestListMissingException( $this->getTestsList() ); |
| 59 |
} |
| 60 |
return ( new PhpUnitTestListProcessor( $this->getTestsList() ) )->getTestClasses(); |
| 61 |
} |
| 62 |
|
| 63 |
private function scanForTestFiles(): array { |
| 64 |
return ( new PhpUnitTestFileScanner( $this->rootDir ) )->scanForFiles(); |
| 65 |
} |
| 66 |
|
| 67 |
private static function extractNamespaceFromFile( $filename ): array { |
| 68 |
$contents = file_get_contents( $filename ); |
| 69 |
$matches = []; |
| 70 |
if ( preg_match( '/^namespace\s+([^\s;]+)/m', $contents, $matches ) ) { |
| 71 |
return explode( '\\', $matches[1] ); |
| 72 |
} |
| 73 |
return []; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
private function resolveFileForTest( TestDescriptor $testDescriptor, array $phpFiles ): ?string { |
| 84 |
$filename = $testDescriptor->getClassName() . ".php"; |
| 85 |
if ( !array_key_exists( $filename, $phpFiles ) ) { |
| 86 |
if ( !in_array( $testDescriptor->getFullClassname(), self::EXPECTED_MISSING_CLASSES ) ) { |
| 87 |
throw new UnlocatedTestException( $testDescriptor ); |
| 88 |
} else { |
| 89 |
return null; |
| 90 |
} |
| 91 |
} |
| 92 |
if ( count( $phpFiles[$filename] ) === 1 ) { |
| 93 |
return $phpFiles[$filename][0]; |
| 94 |
} |
| 95 |
$possibleNamespaces = []; |
| 96 |
foreach ( $phpFiles[$filename] as $file ) { |
| 97 |
$namespace = self::extractNamespaceFromFile( $file ); |
| 98 |
if ( $namespace === $testDescriptor->getNamespace() ) { |
| 99 |
return $file; |
| 100 |
} |
| 101 |
$possibleNamespaces[] = $namespace; |
| 102 |
} |
| 103 |
throw new MissingNamespaceMatchForTestException( $testDescriptor ); |
| 104 |
} |
| 105 |
|
| 106 |
private function buildSuites( array $testClasses, int $groups ): array { |
| 107 |
return ( new TestSuiteBuilder() )->buildSuites( $testClasses, $groups ); |
| 108 |
} |
| 109 |
|
| 110 |
public function isPhpUnitXmlPrepared(): bool { |
| 111 |
return PhpUnitXml::isPhpUnitXmlPrepared( $this->rootDir . DIRECTORY_SEPARATOR . "phpunit.xml" ); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public function createPhpUnitXml( int $groups ) { |
| 122 |
$unitFile = $this->loadPhpUnitXmlDist(); |
| 123 |
$testFiles = $this->scanForTestFiles(); |
| 124 |
$testClasses = $this->loadTestClasses(); |
| 125 |
$seenFiles = []; |
| 126 |
foreach ( $testClasses as $testDescriptor ) { |
| 127 |
$file = $this->resolveFileForTest( $testDescriptor, $testFiles ); |
| 128 |
if ( is_string( $file ) && !array_key_exists( $file, $seenFiles ) ) { |
| 129 |
$testDescriptor->setFilename( $file ); |
| 130 |
$seenFiles[$file] = 1; |
| 131 |
} |
| 132 |
} |
| 133 |
$suites = $this->buildSuites( $testClasses, $groups - 1 ); |
| 134 |
$unitFile->addSplitGroups( $suites ); |
| 135 |
$unitFile->addSpecialCaseTests( $groups ); |
| 136 |
$unitFile->saveToDisk( $this->getPhpUnitXmlTarget() ); |
| 137 |
} |
| 138 |
|
| 139 |
public static function listTestsNotice() { |
| 140 |
print( PHP_EOL ); |
| 141 |
print( 'Running `phpunit --list-tests-xml` to get a list of expected tests ... ' . PHP_EOL ); |
| 142 |
print( PHP_EOL ); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public static function splitTestsList( string $testListFile ) { |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
( new PhpUnitXmlManager( getcwd(), $testListFile ) )->createPhpUnitXml( 8 ); |
| 158 |
print( PHP_EOL . 'Created modified `phpunit.xml` with test suite groups' . PHP_EOL ); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public static function splitTestsListExtensions() { |
| 168 |
self::splitTestsList( 'tests-list-extensions.xml' ); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public static function splitTestsListDefault() { |
| 178 |
self::splitTestsList( 'tests-list-default.xml' ); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public static function splitTestsCustom() { |
| 188 |
if ( $_SERVER["argc"] < 3 ) { |
| 189 |
print( 'Specify a filename to split' . PHP_EOL ); |
| 190 |
exit( 1 ); |
| 191 |
} |
| 192 |
$filename = $_SERVER["argv"][2]; |
| 193 |
self::splitTestsList( $filename ); |
| 194 |
} |
| 195 |
} |