DebugClassLoaderTest.php 11 KB

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