wiki.techinc.nl/tests/phpunit/structure/StructureTest.php
Gergő Tisza 45d4e8d13a Exempt structure tests from @covers checks
@covers does not make any sense for structure tests, which either
do not cover any PHP lines (they test things like configuration or
messages), or cover lines which cannot be determined at the time
of writing the tests (e.g. they cover all classes implementing a
certain interface). Requiring @coversNothing to be manually added
for all of them is a waste of developer time.

tests/phpunit/suite.xml has forceCoversAnnotation=true so removing
the annotations will not change test coverage, these files will
still be skipped.

Change-Id: I27cb58e92341b9b1a76f109701f5bc843adbaa9b
2019-06-11 21:40:14 +00:00

63 lines
1.8 KiB
PHP

<?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() {
// realpath() also normalizes directory separator on windows for prefix compares
$rootPath = realpath( __DIR__ . '/..' );
$suitesPath = realpath( __DIR__ . '/../suites/' );
$testClassRegex = implode( '|', [
'ApiFormatTestBase',
'ApiTestCase',
'ApiQueryTestBase',
'ApiQueryContinueTestBase',
'MediaWikiLangTestCase',
'MediaWikiMediaTestCase',
'MediaWikiTestCase',
'ResourceLoaderTestCase',
'PHPUnit_Framework_TestCase',
'\\?PHPUnit\\Framework\\TestCase',
'TestCase', // \PHPUnit\Framework\TestCase with appropriate use statement
'DumpTestCase',
'SpecialPageTestBase',
] );
$testClassRegex = "/^class .* extends ($testClassRegex)/m";
$results = $this->recurseFiles( $rootPath );
$results = array_filter(
$results,
function ( $filename ) use ( $testClassRegex, $suitesPath ) {
// Remove testUnitTestFileNamesEndWithTest false positives
if ( strpos( $filename, $suitesPath ) === 0
|| substr( $filename, -8 ) === 'Test.php'
) {
return false;
}
$contents = file_get_contents( $filename );
return preg_match( $testClassRegex, $contents );
}
);
$strip = strlen( $rootPath ) + 1;
foreach ( $results as $k => $v ) {
$results[$k] = substr( $v, $strip );
}
$this->assertEquals(
[],
$results,
"Unit test file in $rootPath must end with Test."
);
}
private function recurseFiles( $dir ) {
return ( new File_Iterator_Facade() )->getFilesAsArray( $dir, [ '.php' ] );
}
}