DebugClassLoaderTest.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Debug\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Debug\DebugClassLoader;
  13. class DebugClassLoaderTest extends TestCase
  14. {
  15. /**
  16. * @var int Error reporting level before running tests
  17. */
  18. private $errorReporting;
  19. private $loader;
  20. protected function setUp()
  21. {
  22. $this->errorReporting = error_reporting(E_ALL);
  23. $this->loader = new ClassLoader();
  24. spl_autoload_register(array($this->loader, 'loadClass'), true, true);
  25. DebugClassLoader::enable();
  26. }
  27. protected function tearDown()
  28. {
  29. DebugClassLoader::disable();
  30. spl_autoload_unregister(array($this->loader, 'loadClass'));
  31. error_reporting($this->errorReporting);
  32. }
  33. public function testIdempotence()
  34. {
  35. DebugClassLoader::enable();
  36. $functions = spl_autoload_functions();
  37. foreach ($functions as $function) {
  38. if (is_array($function) && $function[0] instanceof DebugClassLoader) {
  39. $reflClass = new \ReflectionClass($function[0]);
  40. $reflProp = $reflClass->getProperty('classLoader');
  41. $reflProp->setAccessible(true);
  42. $this->assertNotInstanceOf('Symfony\Component\Debug\DebugClassLoader', $reflProp->getValue($function[0]));
  43. return;
  44. }
  45. }
  46. $this->fail('DebugClassLoader did not register');
  47. }
  48. /**
  49. * @expectedException \Exception
  50. * @expectedExceptionMessage boo
  51. */
  52. public function testThrowingClass()
  53. {
  54. try {
  55. class_exists(__NAMESPACE__.'\Fixtures\Throwing');
  56. $this->fail('Exception expected');
  57. } catch (\Exception $e) {
  58. $this->assertSame('boo', $e->getMessage());
  59. }
  60. // the second call also should throw
  61. class_exists(__NAMESPACE__.'\Fixtures\Throwing');
  62. }
  63. /**
  64. * @expectedException \RuntimeException
  65. */
  66. public function testNameCaseMismatch()
  67. {
  68. class_exists(__NAMESPACE__.'\TestingCaseMismatch', true);
  69. }
  70. /**
  71. * @expectedException \RuntimeException
  72. * @expectedExceptionMessage Case mismatch between class and real file names
  73. */
  74. public function testFileCaseMismatch()
  75. {
  76. if (!file_exists(__DIR__.'/Fixtures/CaseMismatch.php')) {
  77. $this->markTestSkipped('Can only be run on case insensitive filesystems');
  78. }
  79. class_exists(__NAMESPACE__.'\Fixtures\CaseMismatch', true);
  80. }
  81. /**
  82. * @expectedException \RuntimeException
  83. */
  84. public function testPsr4CaseMismatch()
  85. {
  86. class_exists(__NAMESPACE__.'\Fixtures\Psr4CaseMismatch', true);
  87. }
  88. public function testNotPsr0()
  89. {
  90. $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\NotPSR0', true));
  91. }
  92. public function testNotPsr0Bis()
  93. {
  94. $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\NotPSR0bis', true));
  95. }
  96. public function testClassAlias()
  97. {
  98. $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\ClassAlias', true));
  99. }
  100. /**
  101. * @dataProvider provideDeprecatedSuper
  102. */
  103. public function testDeprecatedSuper($class, $super, $type)
  104. {
  105. set_error_handler(function () { return false; });
  106. $e = error_reporting(0);
  107. trigger_error('', E_USER_DEPRECATED);
  108. class_exists('Test\\'.__NAMESPACE__.'\\'.$class, true);
  109. error_reporting($e);
  110. restore_error_handler();
  111. $lastError = error_get_last();
  112. unset($lastError['file'], $lastError['line']);
  113. $xError = array(
  114. 'type' => E_USER_DEPRECATED,
  115. 'message' => 'The "Test\Symfony\Component\Debug\Tests\\'.$class.'" class '.$type.' "Symfony\Component\Debug\Tests\Fixtures\\'.$super.'" that is deprecated but this is a test deprecation notice.',
  116. );
  117. $this->assertSame($xError, $lastError);
  118. }
  119. public function provideDeprecatedSuper()
  120. {
  121. return array(
  122. array('DeprecatedInterfaceClass', 'DeprecatedInterface', 'implements'),
  123. array('DeprecatedParentClass', 'DeprecatedClass', 'extends'),
  124. );
  125. }
  126. public function testInterfaceExtendsDeprecatedInterface()
  127. {
  128. set_error_handler(function () { return false; });
  129. $e = error_reporting(0);
  130. trigger_error('', E_USER_NOTICE);
  131. class_exists('Test\\'.__NAMESPACE__.'\\NonDeprecatedInterfaceClass', true);
  132. error_reporting($e);
  133. restore_error_handler();
  134. $lastError = error_get_last();
  135. unset($lastError['file'], $lastError['line']);
  136. $xError = array(
  137. 'type' => E_USER_NOTICE,
  138. 'message' => '',
  139. );
  140. $this->assertSame($xError, $lastError);
  141. }
  142. public function testDeprecatedSuperInSameNamespace()
  143. {
  144. set_error_handler(function () { return false; });
  145. $e = error_reporting(0);
  146. trigger_error('', E_USER_NOTICE);
  147. class_exists('Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent', true);
  148. error_reporting($e);
  149. restore_error_handler();
  150. $lastError = error_get_last();
  151. unset($lastError['file'], $lastError['line']);
  152. $xError = array(
  153. 'type' => E_USER_NOTICE,
  154. 'message' => '',
  155. );
  156. $this->assertSame($xError, $lastError);
  157. }
  158. public function testExtendedFinalClass()
  159. {
  160. set_error_handler(function () { return false; });
  161. $e = error_reporting(0);
  162. trigger_error('', E_USER_NOTICE);
  163. class_exists('Test\\'.__NAMESPACE__.'\\ExtendsFinalClass', true);
  164. error_reporting($e);
  165. restore_error_handler();
  166. $lastError = error_get_last();
  167. unset($lastError['file'], $lastError['line']);
  168. $xError = array(
  169. 'type' => E_USER_DEPRECATED,
  170. 'message' => 'The "Symfony\Component\Debug\Tests\Fixtures\FinalClass" class is considered final. It may change without further notice as of its next major version. You should not extend it from "Test\Symfony\Component\Debug\Tests\ExtendsFinalClass".',
  171. );
  172. $this->assertSame($xError, $lastError);
  173. }
  174. public function testExtendedFinalMethod()
  175. {
  176. set_error_handler(function () { return false; });
  177. $e = error_reporting(0);
  178. trigger_error('', E_USER_NOTICE);
  179. class_exists(__NAMESPACE__.'\\Fixtures\\ExtendedFinalMethod', true);
  180. error_reporting($e);
  181. restore_error_handler();
  182. $lastError = error_get_last();
  183. unset($lastError['file'], $lastError['line']);
  184. $xError = array(
  185. 'type' => E_USER_DEPRECATED,
  186. 'message' => 'The "Symfony\Component\Debug\Tests\Fixtures\FinalMethod::finalMethod()" method is considered final. It may change without further notice as of its next major version. You should not extend it from "Symfony\Component\Debug\Tests\Fixtures\ExtendedFinalMethod".',
  187. );
  188. $this->assertSame($xError, $lastError);
  189. }
  190. public function testExtendedDeprecatedMethodDoesntTriggerAnyNotice()
  191. {
  192. set_error_handler(function () { return false; });
  193. $e = error_reporting(0);
  194. trigger_error('', E_USER_NOTICE);
  195. class_exists('Test\\'.__NAMESPACE__.'\\ExtendsAnnotatedClass', true);
  196. error_reporting($e);
  197. restore_error_handler();
  198. $lastError = error_get_last();
  199. unset($lastError['file'], $lastError['line']);
  200. $this->assertSame(array('type' => E_USER_NOTICE, 'message' => ''), $lastError);
  201. }
  202. public function testInternalsUse()
  203. {
  204. $deprecations = array();
  205. set_error_handler(function ($type, $msg) use (&$deprecations) { $deprecations[] = $msg; });
  206. $e = error_reporting(E_USER_DEPRECATED);
  207. class_exists('Test\\'.__NAMESPACE__.'\\ExtendsInternals', true);
  208. error_reporting($e);
  209. restore_error_handler();
  210. $this->assertSame($deprecations, array(
  211. 'The "Symfony\Component\Debug\Tests\Fixtures\InternalClass" class is considered internal. It may change without further notice. You should not use it from "Test\Symfony\Component\Debug\Tests\ExtendsInternalsParent".',
  212. 'The "Symfony\Component\Debug\Tests\Fixtures\InternalInterface" interface is considered internal. It may change without further notice. You should not use it from "Test\Symfony\Component\Debug\Tests\ExtendsInternalsParent".',
  213. 'The "Symfony\Component\Debug\Tests\Fixtures\InternalTrait" trait is considered internal. It may change without further notice. You should not use it from "Test\Symfony\Component\Debug\Tests\ExtendsInternals".',
  214. 'The "Symfony\Component\Debug\Tests\Fixtures\InternalTrait2::internalMethod()" method is considered internal. It may change without further notice. You should not extend it from "Test\Symfony\Component\Debug\Tests\ExtendsInternals".',
  215. ));
  216. }
  217. }
  218. class ClassLoader
  219. {
  220. public function loadClass($class)
  221. {
  222. }
  223. public function getClassMap()
  224. {
  225. return array(__NAMESPACE__.'\Fixtures\NotPSR0bis' => __DIR__.'/Fixtures/notPsr0Bis.php');
  226. }
  227. public function findFile($class)
  228. {
  229. $fixtureDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR;
  230. if (__NAMESPACE__.'\TestingUnsilencing' === $class) {
  231. eval('-- parse error --');
  232. } elseif (__NAMESPACE__.'\TestingStacking' === $class) {
  233. eval('namespace '.__NAMESPACE__.'; class TestingStacking { function foo() {} }');
  234. } elseif (__NAMESPACE__.'\TestingCaseMismatch' === $class) {
  235. eval('namespace '.__NAMESPACE__.'; class TestingCaseMisMatch {}');
  236. } elseif (__NAMESPACE__.'\Fixtures\Psr4CaseMismatch' === $class) {
  237. return $fixtureDir.'psr4'.DIRECTORY_SEPARATOR.'Psr4CaseMismatch.php';
  238. } elseif (__NAMESPACE__.'\Fixtures\NotPSR0' === $class) {
  239. return $fixtureDir.'reallyNotPsr0.php';
  240. } elseif (__NAMESPACE__.'\Fixtures\NotPSR0bis' === $class) {
  241. return $fixtureDir.'notPsr0Bis.php';
  242. } elseif ('Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent' === $class) {
  243. eval('namespace Symfony\Bridge\Debug\Tests\Fixtures; class ExtendsDeprecatedParent extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
  244. } elseif ('Test\\'.__NAMESPACE__.'\DeprecatedParentClass' === $class) {
  245. eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedParentClass extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
  246. } elseif ('Test\\'.__NAMESPACE__.'\DeprecatedInterfaceClass' === $class) {
  247. eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\DeprecatedInterface {}');
  248. } elseif ('Test\\'.__NAMESPACE__.'\NonDeprecatedInterfaceClass' === $class) {
  249. eval('namespace Test\\'.__NAMESPACE__.'; class NonDeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\NonDeprecatedInterface {}');
  250. } elseif ('Test\\'.__NAMESPACE__.'\Float' === $class) {
  251. eval('namespace Test\\'.__NAMESPACE__.'; class Float {}');
  252. } elseif ('Test\\'.__NAMESPACE__.'\ExtendsFinalClass' === $class) {
  253. eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsFinalClass extends \\'.__NAMESPACE__.'\Fixtures\FinalClass {}');
  254. } elseif ('Test\\'.__NAMESPACE__.'\ExtendsAnnotatedClass' === $class) {
  255. eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsAnnotatedClass extends \\'.__NAMESPACE__.'\Fixtures\AnnotatedClass {
  256. public function deprecatedMethod() { }
  257. }');
  258. } elseif ('Test\\'.__NAMESPACE__.'\ExtendsInternals' === $class) {
  259. eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsInternals extends ExtendsInternalsParent {
  260. use \\'.__NAMESPACE__.'\Fixtures\InternalTrait;
  261. public function internalMethod() { }
  262. }');
  263. } elseif ('Test\\'.__NAMESPACE__.'\ExtendsInternalsParent' === $class) {
  264. eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsInternalsParent extends \\'.__NAMESPACE__.'\Fixtures\InternalClass implements \\'.__NAMESPACE__.'\Fixtures\InternalInterface { }');
  265. }
  266. }
  267. }