Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / structure / PHPUnitConfigTest.php
blobc10b8cfc8207ee58072517a40ac8895f6e8eb3ba
1 <?php
3 /**
4 * The tests here verify that phpunit/suite.xml covers all of the tests under /tests/phpunit
5 * @group medium
6 * @coversNothing
7 */
8 class PHPUnitConfigTest extends PHPUnit\Framework\TestCase {
10 /**
11 * @dataProvider provideConfigFiles
13 public function testConfigDirectories( string $configPath ) {
14 // realpath() also normalizes directory separator on windows for prefix compares
15 $testRootDir = realpath( __DIR__ . '/..' );
17 $dom = new DOMDocument();
18 $dom->load( $configPath );
19 /** @var DOMElement $suites */
20 $suites = $dom->documentElement->getElementsByTagName( 'testsuites' )[0];
22 $suiteInfos = [];
23 /** @var DOMElement $suite */
24 foreach ( $suites->getElementsByTagName( 'testsuite' ) as $suite ) {
25 $generalDirs = [];
26 foreach ( $suite->getElementsByTagName( 'directory' ) as $dirNode ) {
27 $generalDirs[] = str_replace( 'tests/phpunit/', '', $dirNode->textContent );
29 $excludedDirs = [];
30 foreach ( $suite->getElementsByTagName( 'exclude' ) as $dirNode ) {
31 $excludedDirs[] = str_replace( 'tests/phpunit/', '', $dirNode->textContent );
33 $suiteInfos[$suite->getAttribute( 'name' )] = [ $generalDirs, $excludedDirs ];
36 $directoriesFound = scandir( $testRootDir, SCANDIR_SORT_ASCENDING );
37 if ( !$directoriesFound ) {
38 $this->fail( "Could not scan '$testRootDir' directory" );
41 $directoriesNeeded = array_values( array_diff(
42 array_filter(
43 $directoriesFound,
44 static function ( $name ) use ( $testRootDir ) {
45 return (
46 $name !== '.' &&
47 $name !== '..' &&
48 is_dir( "$testRootDir/$name" )
53 'data',
54 'docs',
55 'documentation',
56 'mocks',
57 'suites' // custom suite entry points load their own files
59 ) );
61 $directoriesIncluded = [];
62 foreach ( $directoriesNeeded as $directory ) {
63 if ( $this->isDirectoryIncluded( $directory, $suiteInfos ) ) {
64 $directoriesIncluded[] = $directory;
68 $this->assertSame(
69 $directoriesNeeded,
70 $directoriesIncluded,
71 "All suites included"
75 public static function provideConfigFiles(): array {
76 return [
77 'suite.xml' => [ __DIR__ . '/../suite.xml' ],
78 'phpunit.xml.dist' => [ __DIR__ . '/../../../phpunit.xml.dist' ],
82 private function isDirectoryIncluded( $dir, array $suiteInfos ) {
83 foreach ( $suiteInfos as [ $generalDirs, $excludedDirs ] ) {
84 $found = false;
85 foreach ( $generalDirs as $generalDir ) {
86 if ( $this->isSameOrChildOfDirectory( $dir, $generalDir ) ) {
87 $found = true;
88 break;
91 foreach ( $excludedDirs as $excludedDir ) {
92 if ( $this->isSameOrChildOfDirectory( $dir, $excludedDir ) ) {
93 $found = false;
94 break;
97 if ( $found ) {
98 return true;
102 return false;
105 private function isSameOrChildOfDirectory( $dirA, $dirB ) {
106 if ( $dirA === $dirB ) {
107 return true;
110 if ( str_starts_with( "$dirB/", $dirA ) ) {
111 return true;
114 return false;