DebugClassLoaderTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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([$this->loader, 'loadClass'], true, true);
  25. DebugClassLoader::enable();
  26. }
  27. protected function tearDown()
  28. {
  29. DebugClassLoader::disable();
  30. spl_autoload_unregister([$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 = [
  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 [
  122. ['DeprecatedInterfaceClass', 'DeprecatedInterface', 'implements'],
  123. ['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 = [
  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 = [
  153. 'type' => E_USER_NOTICE,
  154. 'message' => '',
  155. ];
  156. $this->assertSame($xError, $lastError);
  157. }
  158. public function testExtendedFinalClass()
  159. {
  160. $deprecations = [];
  161. set_error_handler(function ($type, $msg) use (&$deprecations) { $deprecations[] = $msg; });
  162. $e = error_reporting(E_USER_DEPRECATED);
  163. require __DIR__.'/Fixtures/FinalClasses.php';
  164. $i = 1;
  165. while(class_exists($finalClass = __NAMESPACE__.'\\Fixtures\\FinalClass'.$i++, false)) {
  166. spl_autoload_call($finalClass);
  167. class_exists('Test\\'.__NAMESPACE__.'\\Extends'.substr($finalClass, strrpos($finalClass, '\\') + 1), true);
  168. }
  169. error_reporting($e);
  170. restore_error_handler();
  171. $this->assertSame([
  172. 'The "Symfony\Component\Debug\Tests\Fixtures\FinalClass1" class is considered final since version 3.3. It may change without further notice as of its next major version. You should not extend it from "Test\Symfony\Component\Debug\Tests\ExtendsFinalClass1".',
  173. 'The "Symfony\Component\Debug\Tests\Fixtures\FinalClass2" 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\ExtendsFinalClass2".',
  174. 'The "Symfony\Component\Debug\Tests\Fixtures\FinalClass3" class is considered final comment with @@@ and ***. It may change without further notice as of its next major version. You should not extend it from "Test\Symfony\Component\Debug\Tests\ExtendsFinalClass3".',
  175. 'The "Symfony\Component\Debug\Tests\Fixtures\FinalClass4" 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\ExtendsFinalClass4".',
  176. 'The "Symfony\Component\Debug\Tests\Fixtures\FinalClass5" class is considered final multiline comment. It may change without further notice as of its next major version. You should not extend it from "Test\Symfony\Component\Debug\Tests\ExtendsFinalClass5".',
  177. 'The "Symfony\Component\Debug\Tests\Fixtures\FinalClass6" 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\ExtendsFinalClass6".',
  178. 'The "Symfony\Component\Debug\Tests\Fixtures\FinalClass7" class is considered final another multiline comment... It may change without further notice as of its next major version. You should not extend it from "Test\Symfony\Component\Debug\Tests\ExtendsFinalClass7".',
  179. 'The "Symfony\Component\Debug\Tests\Fixtures\FinalClass8" 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\ExtendsFinalClass8".',
  180. ], $deprecations);
  181. }
  182. public function testExtendedFinalMethod()
  183. {
  184. $deprecations = [];
  185. set_error_handler(function ($type, $msg) use (&$deprecations) { $deprecations[] = $msg; });
  186. $e = error_reporting(E_USER_DEPRECATED);
  187. class_exists(__NAMESPACE__.'\\Fixtures\\ExtendedFinalMethod', true);
  188. error_reporting($e);
  189. restore_error_handler();
  190. $xError = [
  191. '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".',
  192. 'The "Symfony\Component\Debug\Tests\Fixtures\FinalMethod::finalMethod2()" 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".',
  193. ];
  194. $this->assertSame($xError, $deprecations);
  195. }
  196. public function testExtendedDeprecatedMethodDoesntTriggerAnyNotice()
  197. {
  198. set_error_handler(function () { return false; });
  199. $e = error_reporting(0);
  200. trigger_error('', E_USER_NOTICE);
  201. class_exists('Test\\'.__NAMESPACE__.'\\ExtendsAnnotatedClass', true);
  202. error_reporting($e);
  203. restore_error_handler();
  204. $lastError = error_get_last();
  205. unset($lastError['file'], $lastError['line']);
  206. $this->assertSame(['type' => E_USER_NOTICE, 'message' => ''], $lastError);
  207. }
  208. public function testInternalsUse()
  209. {
  210. $deprecations = [];
  211. set_error_handler(function ($type, $msg) use (&$deprecations) { $deprecations[] = $msg; });
  212. $e = error_reporting(E_USER_DEPRECATED);
  213. class_exists('Test\\'.__NAMESPACE__.'\\ExtendsInternals', true);
  214. error_reporting($e);
  215. restore_error_handler();
  216. $this->assertSame($deprecations, [
  217. '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".',
  218. '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".',
  219. '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".',
  220. 'The "Symfony\Component\Debug\Tests\Fixtures\InternalClass::internalMethod()" method is considered internal. It may change without further notice. You should not extend it from "Test\Symfony\Component\Debug\Tests\ExtendsInternals".',
  221. ]);
  222. }
  223. public function testExtendedMethodDefinesNewParameters()
  224. {
  225. $deprecations = [];
  226. set_error_handler(function ($type, $msg) use (&$deprecations) { $deprecations[] = $msg; });
  227. $e = error_reporting(E_USER_DEPRECATED);
  228. class_exists(__NAMESPACE__.'\\Fixtures\SubClassWithAnnotatedParameters', true);
  229. error_reporting($e);
  230. restore_error_handler();
  231. $this->assertSame([
  232. 'The "Symfony\Component\Debug\Tests\Fixtures\SubClassWithAnnotatedParameters::quzMethod()" method will require a new "Quz $quz" argument in the next major version of its parent class "Symfony\Component\Debug\Tests\Fixtures\ClassWithAnnotatedParameters", not defining it is deprecated.',
  233. 'The "Symfony\Component\Debug\Tests\Fixtures\SubClassWithAnnotatedParameters::whereAmI()" method will require a new "bool $matrix" argument in the next major version of its parent class "Symfony\Component\Debug\Tests\Fixtures\InterfaceWithAnnotatedParameters", not defining it is deprecated.',
  234. 'The "Symfony\Component\Debug\Tests\Fixtures\SubClassWithAnnotatedParameters::isSymfony()" method will require a new "true $yes" argument in the next major version of its parent class "Symfony\Component\Debug\Tests\Fixtures\ClassWithAnnotatedParameters", not defining it is deprecated.',
  235. ], $deprecations);
  236. }
  237. public function testUseTraitWithInternalMethod()
  238. {
  239. $deprecations = [];
  240. set_error_handler(function ($type, $msg) use (&$deprecations) { $deprecations[] = $msg; });
  241. $e = error_reporting(E_USER_DEPRECATED);
  242. class_exists('Test\\'.__NAMESPACE__.'\\UseTraitWithInternalMethod', true);
  243. error_reporting($e);
  244. restore_error_handler();
  245. $this->assertSame([], $deprecations);
  246. }
  247. }
  248. class ClassLoader
  249. {
  250. public function loadClass($class)
  251. {
  252. }
  253. public function getClassMap()
  254. {
  255. return [__NAMESPACE__.'\Fixtures\NotPSR0bis' => __DIR__.'/Fixtures/notPsr0Bis.php'];
  256. }
  257. public function findFile($class)
  258. {
  259. $fixtureDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR;
  260. if (__NAMESPACE__.'\TestingUnsilencing' === $class) {
  261. eval('-- parse error --');
  262. } elseif (__NAMESPACE__.'\TestingStacking' === $class) {
  263. eval('namespace '.__NAMESPACE__.'; class TestingStacking { function foo() {} }');
  264. } elseif (__NAMESPACE__.'\TestingCaseMismatch' === $class) {
  265. eval('namespace '.__NAMESPACE__.'; class TestingCaseMisMatch {}');
  266. } elseif (__NAMESPACE__.'\Fixtures\Psr4CaseMismatch' === $class) {
  267. return $fixtureDir.'psr4'.DIRECTORY_SEPARATOR.'Psr4CaseMismatch.php';
  268. } elseif (__NAMESPACE__.'\Fixtures\NotPSR0' === $class) {
  269. return $fixtureDir.'reallyNotPsr0.php';
  270. } elseif (__NAMESPACE__.'\Fixtures\NotPSR0bis' === $class) {
  271. return $fixtureDir.'notPsr0Bis.php';
  272. } elseif ('Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent' === $class) {
  273. eval('namespace Symfony\Bridge\Debug\Tests\Fixtures; class ExtendsDeprecatedParent extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
  274. } elseif ('Test\\'.__NAMESPACE__.'\DeprecatedParentClass' === $class) {
  275. eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedParentClass extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
  276. } elseif ('Test\\'.__NAMESPACE__.'\DeprecatedInterfaceClass' === $class) {
  277. eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\DeprecatedInterface {}');
  278. } elseif ('Test\\'.__NAMESPACE__.'\NonDeprecatedInterfaceClass' === $class) {
  279. eval('namespace Test\\'.__NAMESPACE__.'; class NonDeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\NonDeprecatedInterface {}');
  280. } elseif ('Test\\'.__NAMESPACE__.'\Float' === $class) {
  281. eval('namespace Test\\'.__NAMESPACE__.'; class Float {}');
  282. } elseif (0 === strpos($class, 'Test\\'.__NAMESPACE__.'\ExtendsFinalClass')) {
  283. $classShortName = substr($class, strrpos($class, '\\') + 1);
  284. eval('namespace Test\\'.__NAMESPACE__.'; class '.$classShortName.' extends \\'.__NAMESPACE__.'\Fixtures\\'.substr($classShortName, 7).' {}');
  285. } elseif ('Test\\'.__NAMESPACE__.'\ExtendsAnnotatedClass' === $class) {
  286. eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsAnnotatedClass extends \\'.__NAMESPACE__.'\Fixtures\AnnotatedClass {
  287. public function deprecatedMethod() { }
  288. }');
  289. } elseif ('Test\\'.__NAMESPACE__.'\ExtendsInternals' === $class) {
  290. eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsInternals extends ExtendsInternalsParent {
  291. use \\'.__NAMESPACE__.'\Fixtures\InternalTrait;
  292. public function internalMethod() { }
  293. }');
  294. } elseif ('Test\\'.__NAMESPACE__.'\ExtendsInternalsParent' === $class) {
  295. eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsInternalsParent extends \\'.__NAMESPACE__.'\Fixtures\InternalClass implements \\'.__NAMESPACE__.'\Fixtures\InternalInterface { }');
  296. } elseif ('Test\\'.__NAMESPACE__.'\UseTraitWithInternalMethod' === $class) {
  297. eval('namespace Test\\'.__NAMESPACE__.'; class UseTraitWithInternalMethod { use \\'.__NAMESPACE__.'\Fixtures\TraitWithInternalMethod; }');
  298. }
  299. }
  300. }