菜谱项目

DebugClassLoaderTest.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. /**
  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. $this->assertStringStartsWith(__FILE__, $exception->getFile());
  105. if (\PHP_VERSION_ID < 70000) {
  106. $this->assertRegExp('/^Runtime Notice: Declaration/', $exception->getMessage());
  107. $this->assertEquals(E_STRICT, $exception->getSeverity());
  108. } else {
  109. $this->assertRegExp('/^Warning: Declaration/', $exception->getMessage());
  110. $this->assertEquals(E_WARNING, $exception->getSeverity());
  111. }
  112. } finally {
  113. restore_error_handler();
  114. restore_exception_handler();
  115. }
  116. }
  117. /**
  118. * @expectedException \RuntimeException
  119. * @expectedExceptionMessage Case mismatch between loaded and declared class names
  120. */
  121. public function testNameCaseMismatch()
  122. {
  123. class_exists(__NAMESPACE__.'\TestingCaseMismatch', true);
  124. }
  125. /**
  126. * @expectedException \RuntimeException
  127. * @expectedExceptionMessage Case mismatch between class and real file names
  128. */
  129. public function testFileCaseMismatch()
  130. {
  131. if (!file_exists(__DIR__.'/Fixtures/CaseMismatch.php')) {
  132. $this->markTestSkipped('Can only be run on case insensitive filesystems');
  133. }
  134. class_exists(__NAMESPACE__.'\Fixtures\CaseMismatch', true);
  135. }
  136. /**
  137. * @expectedException \RuntimeException
  138. * @expectedExceptionMessage Case mismatch between loaded and declared class names
  139. */
  140. public function testPsr4CaseMismatch()
  141. {
  142. class_exists(__NAMESPACE__.'\Fixtures\Psr4CaseMismatch', true);
  143. }
  144. public function testNotPsr0()
  145. {
  146. $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\NotPSR0', true));
  147. }
  148. public function testNotPsr0Bis()
  149. {
  150. $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\NotPSR0bis', true));
  151. }
  152. public function testClassAlias()
  153. {
  154. $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\ClassAlias', true));
  155. }
  156. /**
  157. * @dataProvider provideDeprecatedSuper
  158. */
  159. public function testDeprecatedSuper($class, $super, $type)
  160. {
  161. set_error_handler(function () { return false; });
  162. $e = error_reporting(0);
  163. trigger_error('', E_USER_DEPRECATED);
  164. class_exists('Test\\'.__NAMESPACE__.'\\'.$class, true);
  165. error_reporting($e);
  166. restore_error_handler();
  167. $lastError = error_get_last();
  168. unset($lastError['file'], $lastError['line']);
  169. $xError = array(
  170. 'type' => E_USER_DEPRECATED,
  171. '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',
  172. );
  173. $this->assertSame($xError, $lastError);
  174. }
  175. public function provideDeprecatedSuper()
  176. {
  177. return array(
  178. array('DeprecatedInterfaceClass', 'DeprecatedInterface', 'implements'),
  179. array('DeprecatedParentClass', 'DeprecatedClass', 'extends'),
  180. );
  181. }
  182. public function testInterfaceExtendsDeprecatedInterface()
  183. {
  184. set_error_handler(function () { return false; });
  185. $e = error_reporting(0);
  186. trigger_error('', E_USER_NOTICE);
  187. class_exists('Test\\'.__NAMESPACE__.'\\NonDeprecatedInterfaceClass', true);
  188. error_reporting($e);
  189. restore_error_handler();
  190. $lastError = error_get_last();
  191. unset($lastError['file'], $lastError['line']);
  192. $xError = array(
  193. 'type' => E_USER_NOTICE,
  194. 'message' => '',
  195. );
  196. $this->assertSame($xError, $lastError);
  197. }
  198. public function testDeprecatedSuperInSameNamespace()
  199. {
  200. set_error_handler(function () { return false; });
  201. $e = error_reporting(0);
  202. trigger_error('', E_USER_NOTICE);
  203. class_exists('Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent', true);
  204. error_reporting($e);
  205. restore_error_handler();
  206. $lastError = error_get_last();
  207. unset($lastError['file'], $lastError['line']);
  208. $xError = array(
  209. 'type' => E_USER_NOTICE,
  210. 'message' => '',
  211. );
  212. $this->assertSame($xError, $lastError);
  213. }
  214. public function testReservedForPhp7()
  215. {
  216. if (\PHP_VERSION_ID >= 70000) {
  217. $this->markTestSkipped('PHP7 already prevents using reserved names.');
  218. }
  219. set_error_handler(function () { return false; });
  220. $e = error_reporting(0);
  221. trigger_error('', E_USER_NOTICE);
  222. class_exists('Test\\'.__NAMESPACE__.'\\Float', true);
  223. error_reporting($e);
  224. restore_error_handler();
  225. $lastError = error_get_last();
  226. unset($lastError['file'], $lastError['line']);
  227. $xError = array(
  228. 'type' => E_USER_DEPRECATED,
  229. 'message' => 'The "Test\Symfony\Component\Debug\Tests\Float" class uses the reserved name "Float", it will break on PHP 7 and higher',
  230. );
  231. $this->assertSame($xError, $lastError);
  232. }
  233. public function testExtendedFinalClass()
  234. {
  235. set_error_handler(function () { return false; });
  236. $e = error_reporting(0);
  237. trigger_error('', E_USER_NOTICE);
  238. class_exists('Test\\'.__NAMESPACE__.'\\ExtendsFinalClass', true);
  239. error_reporting($e);
  240. restore_error_handler();
  241. $lastError = error_get_last();
  242. unset($lastError['file'], $lastError['line']);
  243. $xError = array(
  244. 'type' => E_USER_DEPRECATED,
  245. '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".',
  246. );
  247. $this->assertSame($xError, $lastError);
  248. }
  249. public function testExtendedFinalMethod()
  250. {
  251. set_error_handler(function () { return false; });
  252. $e = error_reporting(0);
  253. trigger_error('', E_USER_NOTICE);
  254. class_exists(__NAMESPACE__.'\\Fixtures\\ExtendedFinalMethod', true);
  255. error_reporting($e);
  256. restore_error_handler();
  257. $lastError = error_get_last();
  258. unset($lastError['file'], $lastError['line']);
  259. $xError = array(
  260. 'type' => E_USER_DEPRECATED,
  261. '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".',
  262. );
  263. $this->assertSame($xError, $lastError);
  264. }
  265. }
  266. class ClassLoader
  267. {
  268. public function loadClass($class)
  269. {
  270. }
  271. public function getClassMap()
  272. {
  273. return array(__NAMESPACE__.'\Fixtures\NotPSR0bis' => __DIR__.'/Fixtures/notPsr0Bis.php');
  274. }
  275. public function findFile($class)
  276. {
  277. $fixtureDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR;
  278. if (__NAMESPACE__.'\TestingUnsilencing' === $class) {
  279. eval('-- parse error --');
  280. } elseif (__NAMESPACE__.'\TestingStacking' === $class) {
  281. eval('namespace '.__NAMESPACE__.'; class TestingStacking { function foo() {} }');
  282. } elseif (__NAMESPACE__.'\TestingCaseMismatch' === $class) {
  283. eval('namespace '.__NAMESPACE__.'; class TestingCaseMisMatch {}');
  284. } elseif (__NAMESPACE__.'\Fixtures\CaseMismatch' === $class) {
  285. return $fixtureDir.'CaseMismatch.php';
  286. } elseif (__NAMESPACE__.'\Fixtures\Psr4CaseMismatch' === $class) {
  287. return $fixtureDir.'psr4'.DIRECTORY_SEPARATOR.'Psr4CaseMismatch.php';
  288. } elseif (__NAMESPACE__.'\Fixtures\NotPSR0' === $class) {
  289. return $fixtureDir.'reallyNotPsr0.php';
  290. } elseif (__NAMESPACE__.'\Fixtures\NotPSR0bis' === $class) {
  291. return $fixtureDir.'notPsr0Bis.php';
  292. } elseif (__NAMESPACE__.'\Fixtures\DeprecatedInterface' === $class) {
  293. return $fixtureDir.'DeprecatedInterface.php';
  294. } elseif (__NAMESPACE__.'\Fixtures\FinalClass' === $class) {
  295. return $fixtureDir.'FinalClass.php';
  296. } elseif (__NAMESPACE__.'\Fixtures\FinalMethod' === $class) {
  297. return $fixtureDir.'FinalMethod.php';
  298. } elseif (__NAMESPACE__.'\Fixtures\ExtendedFinalMethod' === $class) {
  299. return $fixtureDir.'ExtendedFinalMethod.php';
  300. } elseif ('Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent' === $class) {
  301. eval('namespace Symfony\Bridge\Debug\Tests\Fixtures; class ExtendsDeprecatedParent extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
  302. } elseif ('Test\\'.__NAMESPACE__.'\DeprecatedParentClass' === $class) {
  303. eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedParentClass extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
  304. } elseif ('Test\\'.__NAMESPACE__.'\DeprecatedInterfaceClass' === $class) {
  305. eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\DeprecatedInterface {}');
  306. } elseif ('Test\\'.__NAMESPACE__.'\NonDeprecatedInterfaceClass' === $class) {
  307. eval('namespace Test\\'.__NAMESPACE__.'; class NonDeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\NonDeprecatedInterface {}');
  308. } elseif ('Test\\'.__NAMESPACE__.'\Float' === $class) {
  309. eval('namespace Test\\'.__NAMESPACE__.'; class Float {}');
  310. } elseif ('Test\\'.__NAMESPACE__.'\ExtendsFinalClass' === $class) {
  311. eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsFinalClass extends \\'.__NAMESPACE__.'\Fixtures\FinalClass {}');
  312. }
  313. }
  314. }