PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Front_End/vendor/symfony/debug/Tests/DebugClassLoaderTest.php

https://gitlab.com/Sigpot/AirSpot
PHP | 315 lines | 236 code | 46 blank | 33 comment | 10 complexity | 8b9098d019c6389d8a4ae5971a6e6ca8 MD5 | raw file
  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. }