DebugClassLoaderTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 Symfony\Component\Debug\DebugClassLoader;
  12. use Symfony\Component\Debug\ErrorHandler;
  13. class DebugClassLoaderTest extends \PHPUnit_Framework_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. public function testUnsilencing()
  49. {
  50. if (PHP_VERSION_ID >= 70000) {
  51. $this->markTestSkipped('PHP7 throws exceptions, unsilencing is not required anymore.');
  52. }
  53. if (defined('HHVM_VERSION')) {
  54. $this->markTestSkipped('HHVM is not handled in this test case.');
  55. }
  56. ob_start();
  57. $this->iniSet('log_errors', 0);
  58. $this->iniSet('display_errors', 1);
  59. // See below: this will fail with parse error
  60. // but this should not be @-silenced.
  61. @class_exists(__NAMESPACE__.'\TestingUnsilencing', true);
  62. $output = ob_get_clean();
  63. $this->assertStringMatchesFormat('%aParse error%a', $output);
  64. }
  65. public function testStacking()
  66. {
  67. // the ContextErrorException must not be loaded to test the workaround
  68. // for https://bugs.php.net/65322.
  69. if (class_exists('Symfony\Component\Debug\Exception\ContextErrorException', false)) {
  70. $this->markTestSkipped('The ContextErrorException class is already loaded.');
  71. }
  72. if (defined('HHVM_VERSION')) {
  73. $this->markTestSkipped('HHVM is not handled in this test case.');
  74. }
  75. ErrorHandler::register();
  76. try {
  77. // Trigger autoloading + E_STRICT at compile time
  78. // which in turn triggers $errorHandler->handle()
  79. // that again triggers autoloading for ContextErrorException.
  80. // Error stacking works around the bug above and everything is fine.
  81. eval('
  82. namespace '.__NAMESPACE__.';
  83. class ChildTestingStacking extends TestingStacking { function foo($bar) {} }
  84. ');
  85. $this->fail('ContextErrorException expected');
  86. } catch (\ErrorException $exception) {
  87. // if an exception is thrown, the test passed
  88. $this->assertStringStartsWith(__FILE__, $exception->getFile());
  89. if (PHP_VERSION_ID < 70000) {
  90. $this->assertRegExp('/^Runtime Notice: Declaration/', $exception->getMessage());
  91. $this->assertEquals(E_STRICT, $exception->getSeverity());
  92. } else {
  93. $this->assertRegExp('/^Warning: Declaration/', $exception->getMessage());
  94. $this->assertEquals(E_WARNING, $exception->getSeverity());
  95. }
  96. } finally {
  97. restore_error_handler();
  98. restore_exception_handler();
  99. }
  100. }
  101. /**
  102. * @expectedException \RuntimeException
  103. */
  104. public function testNameCaseMismatch()
  105. {
  106. class_exists(__NAMESPACE__.'\TestingCaseMismatch', true);
  107. }
  108. /**
  109. * @expectedException \RuntimeException
  110. * @expectedExceptionMessage Case mismatch between class and real file names
  111. */
  112. public function testFileCaseMismatch()
  113. {
  114. if (!file_exists(__DIR__.'/Fixtures/CaseMismatch.php')) {
  115. $this->markTestSkipped('Can only be run on case insensitive filesystems');
  116. }
  117. class_exists(__NAMESPACE__.'\Fixtures\CaseMismatch', true);
  118. }
  119. /**
  120. * @expectedException \RuntimeException
  121. */
  122. public function testPsr4CaseMismatch()
  123. {
  124. class_exists(__NAMESPACE__.'\Fixtures\Psr4CaseMismatch', true);
  125. }
  126. public function testNotPsr0()
  127. {
  128. $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\NotPSR0', true));
  129. }
  130. public function testNotPsr0Bis()
  131. {
  132. $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\NotPSR0bis', true));
  133. }
  134. public function testClassAlias()
  135. {
  136. $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\ClassAlias', true));
  137. }
  138. /**
  139. * @dataProvider provideDeprecatedSuper
  140. */
  141. public function testDeprecatedSuper($class, $super, $type)
  142. {
  143. set_error_handler(function () { return false; });
  144. $e = error_reporting(0);
  145. trigger_error('', E_USER_DEPRECATED);
  146. class_exists('Test\\'.__NAMESPACE__.'\\'.$class, true);
  147. error_reporting($e);
  148. restore_error_handler();
  149. $lastError = error_get_last();
  150. unset($lastError['file'], $lastError['line']);
  151. $xError = array(
  152. 'type' => E_USER_DEPRECATED,
  153. '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.',
  154. );
  155. $this->assertSame($xError, $lastError);
  156. }
  157. public function provideDeprecatedSuper()
  158. {
  159. return array(
  160. array('DeprecatedInterfaceClass', 'DeprecatedInterface', 'implements'),
  161. array('DeprecatedParentClass', 'DeprecatedClass', 'extends'),
  162. );
  163. }
  164. public function testInterfaceExtendsDeprecatedInterface()
  165. {
  166. set_error_handler(function () { return false; });
  167. $e = error_reporting(0);
  168. trigger_error('', E_USER_NOTICE);
  169. class_exists('Test\\'.__NAMESPACE__.'\\NonDeprecatedInterfaceClass', true);
  170. error_reporting($e);
  171. restore_error_handler();
  172. $lastError = error_get_last();
  173. unset($lastError['file'], $lastError['line']);
  174. $xError = array(
  175. 'type' => E_USER_NOTICE,
  176. 'message' => '',
  177. );
  178. $this->assertSame($xError, $lastError);
  179. }
  180. public function testDeprecatedSuperInSameNamespace()
  181. {
  182. set_error_handler(function () { return false; });
  183. $e = error_reporting(0);
  184. trigger_error('', E_USER_NOTICE);
  185. class_exists('Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent', true);
  186. error_reporting($e);
  187. restore_error_handler();
  188. $lastError = error_get_last();
  189. unset($lastError['file'], $lastError['line']);
  190. $xError = array(
  191. 'type' => E_USER_NOTICE,
  192. 'message' => '',
  193. );
  194. $this->assertSame($xError, $lastError);
  195. }
  196. public function testReservedForPhp7()
  197. {
  198. if (PHP_VERSION_ID >= 70000) {
  199. $this->markTestSkipped('PHP7 already prevents using reserved names.');
  200. }
  201. set_error_handler(function () { return false; });
  202. $e = error_reporting(0);
  203. trigger_error('', E_USER_NOTICE);
  204. class_exists('Test\\'.__NAMESPACE__.'\\Float', true);
  205. error_reporting($e);
  206. restore_error_handler();
  207. $lastError = error_get_last();
  208. unset($lastError['file'], $lastError['line']);
  209. $xError = array(
  210. 'type' => E_USER_DEPRECATED,
  211. 'message' => 'Test\Symfony\Component\Debug\Tests\Float uses a reserved class name (Float) that will break on PHP 7 and higher',
  212. );
  213. $this->assertSame($xError, $lastError);
  214. }
  215. }
  216. class ClassLoader
  217. {
  218. public function loadClass($class)
  219. {
  220. }
  221. public function getClassMap()
  222. {
  223. return array(__NAMESPACE__.'\Fixtures\NotPSR0bis' => __DIR__.'/Fixtures/notPsr0Bis.php');
  224. }
  225. public function findFile($class)
  226. {
  227. $fixtureDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR;
  228. if (__NAMESPACE__.'\TestingUnsilencing' === $class) {
  229. eval('-- parse error --');
  230. } elseif (__NAMESPACE__.'\TestingStacking' === $class) {
  231. eval('namespace '.__NAMESPACE__.'; class TestingStacking { function foo() {} }');
  232. } elseif (__NAMESPACE__.'\TestingCaseMismatch' === $class) {
  233. eval('namespace '.__NAMESPACE__.'; class TestingCaseMisMatch {}');
  234. } elseif (__NAMESPACE__.'\Fixtures\CaseMismatch' === $class) {
  235. return $fixtureDir.'CaseMismatch.php';
  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 (__NAMESPACE__.'\Fixtures\DeprecatedInterface' === $class) {
  243. return $fixtureDir.'DeprecatedInterface.php';
  244. } elseif ('Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent' === $class) {
  245. eval('namespace Symfony\Bridge\Debug\Tests\Fixtures; class ExtendsDeprecatedParent extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
  246. } elseif ('Test\\'.__NAMESPACE__.'\DeprecatedParentClass' === $class) {
  247. eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedParentClass extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
  248. } elseif ('Test\\'.__NAMESPACE__.'\DeprecatedInterfaceClass' === $class) {
  249. eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\DeprecatedInterface {}');
  250. } elseif ('Test\\'.__NAMESPACE__.'\NonDeprecatedInterfaceClass' === $class) {
  251. eval('namespace Test\\'.__NAMESPACE__.'; class NonDeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\NonDeprecatedInterface {}');
  252. } elseif ('Test\\'.__NAMESPACE__.'\Float' === $class) {
  253. eval('namespace Test\\'.__NAMESPACE__.'; class Float {}');
  254. }
  255. }
  256. }