AutoLoaderTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. class AutoLoaderTest extends MediaWikiTestCase {
  3. protected function setUp() {
  4. global $wgAutoloadLocalClasses, $wgAutoloadClasses;
  5. parent::setUp();
  6. // Fancy dance to trigger a rebuild of AutoLoader::$autoloadLocalClassesLower
  7. $this->testLocalClasses = array(
  8. 'TestAutoloadedLocalClass' => __DIR__ . '/../data/autoloader/TestAutoloadedLocalClass.php',
  9. 'TestAutoloadedCamlClass' => __DIR__ . '/../data/autoloader/TestAutoloadedCamlClass.php',
  10. 'TestAutoloadedSerializedClass' => __DIR__ . '/../data/autoloader/TestAutoloadedSerializedClass.php',
  11. );
  12. $this->setMwGlobals( 'wgAutoloadLocalClasses', $this->testLocalClasses + $wgAutoloadLocalClasses );
  13. AutoLoader::resetAutoloadLocalClassesLower();
  14. $this->testExtensionClasses = array(
  15. 'TestAutoloadedClass' => __DIR__ . '/../data/autoloader/TestAutoloadedClass.php',
  16. );
  17. $this->setMwGlobals( 'wgAutoloadClasses', $this->testExtensionClasses + $wgAutoloadClasses );
  18. }
  19. /**
  20. * Assert that there were no classes loaded that are not registered with the AutoLoader.
  21. *
  22. * For example foo.php having class Foo and class Bar but only registering Foo.
  23. * This is important because we should not be relying on Foo being used before Bar.
  24. */
  25. public function testAutoLoadConfig() {
  26. $results = self::checkAutoLoadConf();
  27. $this->assertEquals(
  28. $results['expected'],
  29. $results['actual']
  30. );
  31. }
  32. protected static function checkAutoLoadConf() {
  33. global $wgAutoloadLocalClasses, $wgAutoloadClasses, $IP;
  34. // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
  35. $expected = $wgAutoloadLocalClasses + $wgAutoloadClasses;
  36. $actual = array();
  37. $files = array_unique( $expected );
  38. foreach ( $files as $file ) {
  39. // Only prefix $IP if it doesn't have it already.
  40. // Generally local classes don't have it, and those from extensions and test suites do.
  41. if ( substr( $file, 0, 1 ) != '/' && substr( $file, 1, 1 ) != ':' ) {
  42. $filePath = "$IP/$file";
  43. } else {
  44. $filePath = $file;
  45. }
  46. $contents = file_get_contents( $filePath );
  47. // We could use token_get_all() here, but this is faster
  48. $matches = array();
  49. preg_match_all( '/
  50. ^ [\t ]* (?:
  51. (?:final\s+)? (?:abstract\s+)? (?:class|interface) \s+
  52. (?P<class> [a-zA-Z0-9_]+)
  53. |
  54. class_alias \s* \( \s*
  55. ([\'"]) (?P<original> [^\'"]+) \g{-2} \s* , \s*
  56. ([\'"]) (?P<alias> [^\'"]+ ) \g{-2} \s*
  57. \) \s* ;
  58. )
  59. /imx', $contents, $matches, PREG_SET_ORDER );
  60. $namespaceMatch = array();
  61. preg_match( '/
  62. ^ [\t ]*
  63. namespace \s+
  64. ([a-zA-Z0-9_]+(\\\\[a-zA-Z0-9_]+)*)
  65. \s* ;
  66. /imx', $contents, $namespaceMatch );
  67. $fileNamespace = $namespaceMatch ? $namespaceMatch[1] . '\\' : '';
  68. $classesInFile = array();
  69. $aliasesInFile = array();
  70. foreach ( $matches as $match ) {
  71. if ( !empty( $match['class'] ) ) {
  72. $class = $fileNamespace . $match['class'];
  73. $actual[$class] = $file;
  74. $classesInFile[$class] = true;
  75. } else {
  76. $aliasesInFile[$match['alias']] = $match['original'];
  77. }
  78. }
  79. // Only accept aliases for classes in the same file, because for correct
  80. // behavior, all aliases for a class must be set up when the class is loaded
  81. // (see <https://bugs.php.net/bug.php?id=61422>).
  82. foreach ( $aliasesInFile as $alias => $class ) {
  83. if ( isset( $classesInFile[$class] ) ) {
  84. $actual[$alias] = $file;
  85. } else {
  86. $actual[$alias] = "[original class not in $file]";
  87. }
  88. }
  89. }
  90. return array(
  91. 'expected' => $expected,
  92. 'actual' => $actual,
  93. );
  94. }
  95. function testCoreClass() {
  96. $this->assertTrue( class_exists( 'TestAutoloadedLocalClass' ) );
  97. }
  98. function testExtensionClass() {
  99. $this->assertTrue( class_exists( 'TestAutoloadedClass' ) );
  100. }
  101. function testWrongCaseClass() {
  102. $this->assertTrue( class_exists( 'testautoLoadedcamlCLASS' ) );
  103. }
  104. function testWrongCaseSerializedClass() {
  105. $dummyCereal = 'O:29:"testautoloadedserializedclass":0:{}';
  106. $uncerealized = unserialize( $dummyCereal );
  107. $this->assertFalse( $uncerealized instanceof __PHP_Incomplete_Class,
  108. "unserialize() can load classes case-insensitively." );
  109. }
  110. }