aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/structure/PHPUnitConfigTest.php
blob: 7fe133769fdc6bf75db4407fe80ce932df752f72 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php

/**
 * The tests here verify that phpunit/suite.xml covers all of the tests under /tests/phpunit
 * @group medium
 * @coversNothing
 */
class PHPUnitConfigTest extends PHPUnit\Framework\TestCase {

	/**
	 * @dataProvider provideConfigFiles
	 */
	public function testConfigDirectories( string $configPath ) {
		// realpath() also normalizes directory separator on windows for prefix compares
		$testRootDir = realpath( __DIR__ . '/..' );

		$dom = new DOMDocument();
		$dom->load( $configPath );
		/** @var DOMElement $suites */
		$suites = $dom->documentElement->getElementsByTagName( 'testsuites' )[0];

		$suiteInfos = [];
		/** @var DOMElement $suite */
		foreach ( $suites->getElementsByTagName( 'testsuite' ) as $suite ) {
			$generalDirs = [];
			foreach ( $suite->getElementsByTagName( 'directory' ) as $dirNode ) {
				$generalDirs[] = str_replace( 'tests/phpunit/', '', $dirNode->textContent );
			}
			$excludedDirs = [];
			foreach ( $suite->getElementsByTagName( 'exclude' ) as $dirNode ) {
				$excludedDirs[] = str_replace( 'tests/phpunit/', '', $dirNode->textContent );
			}
			$suiteInfos[$suite->getAttribute( 'name' )] = [ $generalDirs, $excludedDirs ];
		}

		$directoriesFound = scandir( $testRootDir, SCANDIR_SORT_ASCENDING );
		if ( !$directoriesFound ) {
			$this->fail( "Could not scan '$testRootDir' directory" );
		}

		$directoriesNeeded = array_values( array_diff(
			array_filter(
				$directoriesFound,
				static function ( $name ) use ( $testRootDir ) {
					return (
						$name !== '.' &&
						$name !== '..' &&
						is_dir( "$testRootDir/$name" )
					);
				}
			),
			[
				'data',
				'docs',
				'documentation',
				'mocks',
				'suites' // custom suite entry points load their own files
			]
		) );

		$directoriesIncluded = [];
		foreach ( $directoriesNeeded as $directory ) {
			if ( $this->isDirectoryIncluded( $directory, $suiteInfos ) ) {
				$directoriesIncluded[] = $directory;
			}
		}

		$this->assertSame(
			$directoriesNeeded,
			$directoriesIncluded,
			"All suites included"
		);
	}

	public static function provideConfigFiles(): array {
		return [
			'suite.xml' => [ __DIR__ . '/../suite.xml' ],
			'phpunit.xml.dist' => [ __DIR__ . '/../../../phpunit.xml.dist' ],
		];
	}

	private function isDirectoryIncluded( $dir, array $suiteInfos ) {
		foreach ( $suiteInfos as [ $generalDirs, $excludedDirs ] ) {
			$found = false;
			foreach ( $generalDirs as $generalDir ) {
				if ( $this->isSameOrChildOfDirectory( $dir, $generalDir ) ) {
					$found = true;
					break;
				}
			}
			foreach ( $excludedDirs as $excludedDir ) {
				if ( $this->isSameOrChildOfDirectory( $dir, $excludedDir ) ) {
					$found = false;
					break;
				}
			}
			if ( $found ) {
				return true;
			}
		}

		return false;
	}

	private function isSameOrChildOfDirectory( $dirA, $dirB ) {
		if ( $dirA === $dirB ) {
			return true;
		}

		if ( strpos( "$dirB/", $dirA ) === 0 ) {
			return true;
		}

		return false;
	}
}