StructureTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * The tests here verify the structure of the code. This is for outright bugs,
  4. * not just style issues.
  5. */
  6. class StructureTest extends MediaWikiTestCase {
  7. /**
  8. * Verify all files that appear to be tests have file names ending in
  9. * Test. If the file names do not end in Test, they will not be run.
  10. * @group medium
  11. * @coversNothing
  12. */
  13. public function testUnitTestFileNamesEndWithTest() {
  14. // realpath() also normalizes directory separator on windows for prefix compares
  15. $rootPath = realpath( __DIR__ . '/..' );
  16. $suitesPath = realpath( __DIR__ . '/../suites/' );
  17. $testClassRegex = implode( '|', [
  18. 'ApiFormatTestBase',
  19. 'ApiTestCase',
  20. 'ApiQueryTestBase',
  21. 'ApiQueryContinueTestBase',
  22. 'MediaWikiLangTestCase',
  23. 'MediaWikiMediaTestCase',
  24. 'MediaWikiTestCase',
  25. 'ResourceLoaderTestCase',
  26. 'PHPUnit_Framework_TestCase',
  27. '\\?PHPUnit\\Framework\\TestCase',
  28. 'TestCase', // \PHPUnit\Framework\TestCase with appropriate use statement
  29. 'DumpTestCase',
  30. 'SpecialPageTestBase',
  31. ] );
  32. $testClassRegex = "/^class .* extends ($testClassRegex)/m";
  33. $results = $this->recurseFiles( $rootPath );
  34. $results = array_filter(
  35. $results,
  36. function ( $filename ) use ( $testClassRegex, $suitesPath ) {
  37. // Remove testUnitTestFileNamesEndWithTest false positives
  38. if ( strpos( $filename, $suitesPath ) === 0
  39. || substr( $filename, -8 ) === 'Test.php'
  40. ) {
  41. return false;
  42. }
  43. $contents = file_get_contents( $filename );
  44. return preg_match( $testClassRegex, $contents );
  45. }
  46. );
  47. $strip = strlen( $rootPath ) + 1;
  48. foreach ( $results as $k => $v ) {
  49. $results[$k] = substr( $v, $strip );
  50. }
  51. $this->assertEquals(
  52. [],
  53. $results,
  54. "Unit test file in $rootPath must end with Test."
  55. );
  56. }
  57. private function recurseFiles( $dir ) {
  58. return ( new File_Iterator_Facade() )->getFilesAsArray( $dir, [ '.php' ] );
  59. }
  60. }