summaryrefslogtreecommitdiff
path: root/tests/phpunit/structure/StructureTest.php
blob: 14461be691c6d3cd9a54fbc3f60a6f068a222b2c (plain)
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
<?php
/**
 * The tests here verify the structure of the code.  This is for outright bugs,
 * not just style issues.
 */

class StructureTest extends MediaWikiTestCase {
	/**
	 * Verify all files that appear to be tests have file names ending in
	 * Test.  If the file names do not end in Test, they will not be run.
	 * @group medium
	 */
	public function testUnitTestFileNamesEndWithTest() {
		if ( wfIsWindows() ) {
			$this->markTestSkipped( 'This test does not work on Windows' );
		}
		$rootPath = escapeshellarg( __DIR__ . '/..' );
		$testClassRegex = implode( '|', array(
			'ApiFormatTestBase',
			'ApiTestCase',
			'ApiQueryTestBase',
			'ApiQueryContinueTestBase',
			'MediaWikiLangTestCase',
			'MediaWikiMediaTestCase',
			'MediaWikiTestCase',
			'ResourceLoaderTestCase',
			'PHPUnit_Framework_TestCase',
			'DumpTestCase',
		) );
		$testClassRegex = "^class .* extends ($testClassRegex)";
		$finder = "find $rootPath -name '*.php' '!' -name '*Test.php'" .
			" | xargs grep -El '$testClassRegex|function suite\('";

		$results = null;
		$exitCode = null;
		exec( $finder, $results, $exitCode );

		$this->assertEquals(
			0,
			$exitCode,
			'Verify find/grep command succeeds.'
		);

		$results = array_filter(
			$results,
			array( $this, 'filterSuites' )
		);
		$strip = strlen( $rootPath ) - 1;
		foreach ( $results as $k => $v ) {
			$results[$k] = substr( $v, $strip );
		}
		$this->assertEquals(
			array(),
			$results,
			"Unit test file in $rootPath must end with Test."
		);
	}

	/**
	 * Filter to remove testUnitTestFileNamesEndWithTest false positives.
	 * @param string $filename
	 * @return bool
	 */
	public function filterSuites( $filename ) {
		return strpos( $filename, __DIR__ . '/../suites/' ) !== 0;
	}
}