DebugClassLoaderTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. use Symfony\Component\Debug\ErrorHandler;
  14. class DebugClassLoaderTest extends TestCase
  15. {
  16. /**
  17. * @var int Error reporting level before running tests
  18. */
  19. private $errorReporting;
  20. private $loader;
  21. protected function setUp()
  22. {
  23. $this->errorReporting = error_reporting(E_ALL);
  24. $this->loader = new ClassLoader();
  25. spl_autoload_register(array($this->loader, 'loadClass'), true, true);
  26. DebugClassLoader::enable();
  27. }
  28. protected function tearDown()
  29. {
  30. DebugClassLoader::disable();
  31. spl_autoload_unregister(array($this->loader, 'loadClass'));
  32. error_reporting($this->errorReporting);
  33. }
  34. public function testIdempotence()
  35. {
  36. DebugClassLoader::enable();
  37. $functions = spl_autoload_functions();
  38. foreach ($functions as $function) {
  39. if (is_array($function) && $function[0] instanceof DebugClassLoader) {
  40. $reflClass = new \ReflectionClass($function[0]);
  41. $reflProp = $reflClass->getProperty('classLoader');
  42. $reflProp->setAccessible(true);
  43. $this->assertNotInstanceOf('Symfony\Component\Debug\DebugClassLoader', $reflProp->getValue($function[0]));
  44. return;
  45. }
  46. }
  47. $this->fail('DebugClassLoader did not register');
  48. }
  49. public function testUnsilencing()
  50. {
  51. if (\PHP_VERSION_ID >= 70000) {
  52. $this->markTestSkipped('PHP7 throws exceptions, unsilencing is not required anymore.');
  53. }
  54. if (defined('HHVM_VERSION')) {
  55. $this->markTestSkipped('HHVM is not handled in this test case.');
  56. }
  57. ob_start();
  58. $this->iniSet('log_errors', 0);
  59. $this->iniSet('display_errors', 1);
  60. // See below: this will fail with parse error
  61. // but this should not be @-silenced.
  62. @class_exists(__NAMESPACE__.'\TestingUnsilencing', true);
  63. $output = ob_get_clean();
  64. $this->assertStringMatchesFormat('%aParse error%a', $output);
  65. }
  66. public function testStacking()
  67. {
  68. // the ContextErrorException must not be loaded to test the workaround
  69. // for https://bugs.php.net/65322.
  70. if (class_exists('Symfony\Component\Debug\Exception\ContextErrorException', false)) {
  71. $this->markTestSkipped('The ContextErrorException class is already loaded.');
  72. }
  73. if (defined('HHVM_VERSION')) {
  74. $this->markTestSkipped('HHVM is not handled in this test case.');
  75. }
  76. ErrorHandler::register();
  77. try {
  78. // Trigger autoloading + E_STRICT at compile time
  79. // which in turn triggers $errorHandler->handle()
  80. // that again triggers autoloading for ContextErrorException.
  81. // Error stacking works around the bug above and everything is fine.
  82. eval('
  83. namespace '.__NAMESPACE__.';
  84. class ChildTestingStacking extends TestingStacking { function foo($bar) {} }
  85. ');
  86. $this->fail('ContextErrorException expected');
  87. } catch (\ErrorException $exception) {
  88. // if an exception is thrown, the test passed
  89. $this->assertStringStartsWith(__FILE__, $exception->getFile());
  90. if (\PHP_VERSION_ID < 70000) {
  91. $this->assertRegExp('/^Runtime Notice: Declaration/', $exception->getMessage());
  92. $this->assertEquals(E_STRICT, $exception->getSeverity());
  93. } else {
  94. $this->assertRegExp('/^Warning: Declaration/', $exception->getMessage());
  95. $this->assertEquals(E_WARNING, $exception->getSeverity());
  96. }
  97. } finally {
  98. restore_error_handler();
  99. restore_exception_handler();
  100. }
  101. }
  102. /**
  103. * @expectedException \RuntimeException
  104. */
  105. public function testNameCaseMismatch()
  106. {
  107. class_exists(__NAMESPACE__.'\TestingCaseMismatch', true);
  108. }
  109. /**
  110. * @expectedException \RuntimeException
  111. * @expectedExceptionMessage Case mismatch between class and real file names
  112. */
  113. public function testFileCaseMismatch()
  114. {
  115. if (!file_exists(__DIR__.'/Fixtures/CaseMismatch.php')) {
  116. $this->markTestSkipped('Can only be run on case insensitive filesystems');
  117. }
  118. class_exists(__NAMESPACE__.'\Fixtures\CaseMismatch', true);
  119. }
  120. /**
  121. * @expectedException \RuntimeException
  122. */
  123. public function testPsr4CaseMismatch()
  124. {
  125. class_exists(__NAMESPACE__.'\Fixtures\Psr4CaseMismatch', true);
  126. }
  127. public function testNotPsr0()
  128. {
  129. $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\NotPSR0', true));
  130. }
  131. public function testNotPsr0Bis()
  132. {
  133. $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\NotPSR0bis', true));
  134. }
  135. public function testClassAlias()
  136. {
  137. $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\ClassAlias', true));
  138. }
  139. /**
  140. * @dataProvider provideDeprecatedSuper
  141. */
  142. public function testDeprecatedSuper($class, $super, $type)
  143. {
  144. set_error_handler(function () { return false; });
  145. $e = error_reporting(0);
  146. trigger_error('', E_USER_DEPRECATED);
  147. class_exists('Test\\'.__NAMESPACE__.'\\'.$class, 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_DEPRECATED,
  154. '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.',
  155. );
  156. $this->assertSame($xError, $lastError);
  157. }
  158. public function provideDeprecatedSuper()
  159. {
  160. return array(
  161. array('DeprecatedInterfaceClass', 'DeprecatedInterface', 'implements'),
  162. array('DeprecatedParentClass', 'DeprecatedClass', 'extends'),
  163. );
  164. }
  165. public function testInterfaceExtendsDeprecatedInterface()
  166. {
  167. set_error_handler(function () { return false; });
  168. $e = error_reporting(0);
  169. trigger_error('', E_USER_NOTICE);
  170. class_exists('Test\\'.__NAMESPACE__.'\\NonDeprecatedInterfaceClass', true);
  171. error_reporting($e);
  172. restore_error_handler();
  173. $lastError = error_get_last();
  174. unset($lastError['file'], $lastError['line']);
  175. $xError = array(
  176. 'type' => E_USER_NOTICE,
  177. 'message' => '',
  178. );
  179. $this->assertSame($xError, $lastError);
  180. }
  181. public function testDeprecatedSuperInSameNamespace()
  182. {
  183. set_error_handler(function () { return false; });
  184. $e = error_reporting(0);
  185. trigger_error('', E_USER_NOTICE);
  186. class_exists('Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent', true);
  187. error_reporting($e);
  188. restore_error_handler();
  189. $lastError = error_get_last();
  190. unset($lastError['file'], $lastError['line']);
  191. $xError = array(
  192. 'type' => E_USER_NOTICE,
  193. 'message' => '',
  194. );
  195. $this->assertSame($xError, $lastError);
  196. }
  197. public function testReservedForPhp7()
  198. {
  199. if (\PHP_VERSION_ID >= 70000) {
  200. $this->markTestSkipped('PHP7 already prevents using reserved names.');
  201. }
  202. set_error_handler(function () { return false; });
  203. $e = error_reporting(0);
  204. trigger_error('', E_USER_NOTICE);
  205. class_exists('Test\\'.__NAMESPACE__.'\\Float', true);
  206. error_reporting($e);
  207. restore_error_handler();
  208. $lastError = error_get_last();
  209. unset($lastError['file'], $lastError['line']);
  210. $xError = array(
  211. 'type' => E_USER_DEPRECATED,
  212. 'message' => 'The "Test\Symfony\Component\Debug\Tests\Float" class uses the reserved name "Float", it will break on PHP 7 and higher',
  213. );
  214. $this->assertSame($xError, $lastError);
  215. }
  216. public function testExtendedFinalClass()
  217. {
  218. set_error_handler(function () { return false; });
  219. $e = error_reporting(0);
  220. trigger_error('', E_USER_NOTICE);
  221. class_exists('Test\\'.__NAMESPACE__.'\\ExtendsFinalClass', true);
  222. error_reporting($e);
  223. restore_error_handler();
  224. $lastError = error_get_last();
  225. unset($lastError['file'], $lastError['line']);
  226. $xError = array(
  227. 'type' => E_USER_DEPRECATED,
  228. 'message' => 'The "Symfony\Component\Debug\Tests\Fixtures\FinalClass" 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\ExtendsFinalClass".',
  229. );
  230. $this->assertSame($xError, $lastError);
  231. }
  232. public function testExtendedFinalMethod()
  233. {
  234. set_error_handler(function () { return false; });
  235. $e = error_reporting(0);
  236. trigger_error('', E_USER_NOTICE);
  237. class_exists(__NAMESPACE__.'\\Fixtures\\ExtendedFinalMethod', true);
  238. error_reporting($e);
  239. restore_error_handler();
  240. $lastError = error_get_last();
  241. unset($lastError['file'], $lastError['line']);
  242. $xError = array(
  243. 'type' => E_USER_DEPRECATED,
  244. 'message' => 'The "Symfony\Component\Debug\Tests\Fixtures\FinalMethod::finalMethod()" method 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 "Symfony\Component\Debug\Tests\Fixtures\ExtendedFinalMethod".',
  245. );
  246. $this->assertSame($xError, $lastError);
  247. }
  248. }
  249. class ClassLoader
  250. {
  251. public function loadClass($class)
  252. {
  253. }
  254. public function getClassMap()
  255. {
  256. return array(__NAMESPACE__.'\Fixtures\NotPSR0bis' => __DIR__.'/Fixtures/notPsr0Bis.php');
  257. }
  258. public function findFile($class)
  259. {
  260. $fixtureDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR;
  261. if (__NAMESPACE__.'\TestingUnsilencing' === $class) {
  262. eval('-- parse error --');
  263. } elseif (__NAMESPACE__.'\TestingStacking' === $class) {
  264. eval('namespace '.__NAMESPACE__.'; class TestingStacking { function foo() {} }');
  265. } elseif (__NAMESPACE__.'\TestingCaseMismatch' === $class) {
  266. eval('namespace '.__NAMESPACE__.'; class TestingCaseMisMatch {}');
  267. } elseif (__NAMESPACE__.'\Fixtures\CaseMismatch' === $class) {
  268. return $fixtureDir.'CaseMismatch.php';
  269. } elseif (__NAMESPACE__.'\Fixtures\Psr4CaseMismatch' === $class) {
  270. return $fixtureDir.'psr4'.DIRECTORY_SEPARATOR.'Psr4CaseMismatch.php';
  271. } elseif (__NAMESPACE__.'\Fixtures\NotPSR0' === $class) {
  272. return $fixtureDir.'reallyNotPsr0.php';
  273. } elseif (__NAMESPACE__.'\Fixtures\NotPSR0bis' === $class) {
  274. return $fixtureDir.'notPsr0Bis.php';
  275. } elseif (__NAMESPACE__.'\Fixtures\DeprecatedInterface' === $class) {
  276. return $fixtureDir.'DeprecatedInterface.php';
  277. } elseif (__NAMESPACE__.'\Fixtures\FinalClass' === $class) {
  278. return $fixtureDir.'FinalClass.php';
  279. } elseif (__NAMESPACE__.'\Fixtures\FinalMethod' === $class) {
  280. return $fixtureDir.'FinalMethod.php';
  281. } elseif (__NAMESPACE__.'\Fixtures\ExtendedFinalMethod' === $class) {
  282. return $fixtureDir.'ExtendedFinalMethod.php';
  283. } elseif ('Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent' === $class) {
  284. eval('namespace Symfony\Bridge\Debug\Tests\Fixtures; class ExtendsDeprecatedParent extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
  285. } elseif ('Test\\'.__NAMESPACE__.'\DeprecatedParentClass' === $class) {
  286. eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedParentClass extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
  287. } elseif ('Test\\'.__NAMESPACE__.'\DeprecatedInterfaceClass' === $class) {
  288. eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\DeprecatedInterface {}');
  289. } elseif ('Test\\'.__NAMESPACE__.'\NonDeprecatedInterfaceClass' === $class) {
  290. eval('namespace Test\\'.__NAMESPACE__.'; class NonDeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\NonDeprecatedInterface {}');
  291. } elseif ('Test\\'.__NAMESPACE__.'\Float' === $class) {
  292. eval('namespace Test\\'.__NAMESPACE__.'; class Float {}');
  293. } elseif ('Test\\'.__NAMESPACE__.'\ExtendsFinalClass' === $class) {
  294. eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsFinalClass extends \\'.__NAMESPACE__.'\Fixtures\FinalClass {}');
  295. }
  296. }
  297. }