Debian Codegraph @

tests/phpunit/structure/PHPUnitConfigTest.php

raw symbols SCIP
1 <?php
2
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 {
9
10 /**
11 * @dataProvider provideConfigFiles
12 */
13 public function testConfigDirectories( string $configPath ) {
14 // realpath() also normalizes directory separator on windows for prefix compares
15 $testRootDir = realpath( __DIR__ . '/..' );
16
17 $dom = new DOMDocument();
18 $dom->load( $configPath );
19 /** @var DOMElement $suites */
20 $suites = $dom->documentElement->getElementsByTagName( 'testsuites' )[0];
21
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 );
28 }
29 $excludedDirs = [];
30 foreach ( $suite->getElementsByTagName( 'exclude' ) as $dirNode ) {
31 $excludedDirs[] = str_replace( 'tests/phpunit/', '', $dirNode->textContent );
32 }
33 $suiteInfos[$suite->getAttribute( 'name' )] = [ $generalDirs, $excludedDirs ];
34 }
35
36 $directoriesFound = scandir( $testRootDir, SCANDIR_SORT_ASCENDING );
37 if ( !$directoriesFound ) {
38 $this->fail( "Could not scan '$testRootDir' directory" );
39 }
40
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" )
49 );
50 }
51 ),
52 [
53 'data',
54 'docs',
55 'documentation',
56 'mocks',
57 'suites' // custom suite entry points load their own files
58 ]
59 ) );
60
61 $directoriesIncluded = [];
62 foreach ( $directoriesNeeded as $directory ) {
63 if ( $this->isDirectoryIncluded( $directory, $suiteInfos ) ) {
64 $directoriesIncluded[] = $directory;
65 }
66 }
67
68 $this->assertSame(
69 $directoriesNeeded,
70 $directoriesIncluded,
71 "All suites included"
72 );
73 }
74
75 public static function provideConfigFiles(): array {
76 return [
77 'suite.xml' => [ __DIR__ . '/../suite.xml' ],
78 'phpunit.xml.dist' => [ __DIR__ . '/../../../phpunit.xml.dist' ],
79 ];
80 }
81
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;
89 }
90 }
91 foreach ( $excludedDirs as $excludedDir ) {
92 if ( $this->isSameOrChildOfDirectory( $dir, $excludedDir ) ) {
93 $found = false;
94 break;
95 }
96 }
97 if ( $found ) {
98 return true;
99 }
100 }
101
102 return false;
103 }
104
105 private function isSameOrChildOfDirectory( $dirA, $dirB ) {
106 if ( $dirA === $dirB ) {
107 return true;
108 }
109
110 if ( str_starts_with( "$dirB/", $dirA ) ) {
111 return true;
112 }
113
114 return false;
115 }
116 }