PageRenderTime 25ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/4gdevs/online-class-record-system
PHP | 296 lines | 217 code | 46 blank | 33 comment | 10 complexity | ec946309e8c47e2528243e27e2e98134 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. use Symfony\Component\Debug\Exception\ContextErrorException;
  14. class DebugClassLoaderTest extends \PHPUnit_Framework_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. 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. restore_error_handler();
  90. restore_exception_handler();
  91. $this->assertStringStartsWith(__FILE__, $exception->getFile());
  92. if (PHP_VERSION_ID < 70000) {
  93. $this->assertRegExp('/^Runtime Notice: Declaration/', $exception->getMessage());
  94. $this->assertEquals(E_STRICT, $exception->getSeverity());
  95. } else {
  96. $this->assertRegExp('/^Warning: Declaration/', $exception->getMessage());
  97. $this->assertEquals(E_WARNING, $exception->getSeverity());
  98. }
  99. } catch (\Exception $exception) {
  100. restore_error_handler();
  101. restore_exception_handler();
  102. throw $exception;
  103. }
  104. }
  105. /**
  106. * @expectedException \RuntimeException
  107. */
  108. public function testNameCaseMismatch()
  109. {
  110. class_exists(__NAMESPACE__.'\TestingCaseMismatch', true);
  111. }
  112. /**
  113. * @expectedException \RuntimeException
  114. * @expectedExceptionMessage Case mismatch between class and real file names
  115. */
  116. public function testFileCaseMismatch()
  117. {
  118. if (!file_exists(__DIR__.'/Fixtures/CaseMismatch.php')) {
  119. $this->markTestSkipped('Can only be run on case insensitive filesystems');
  120. }
  121. class_exists(__NAMESPACE__.'\Fixtures\CaseMismatch', true);
  122. }
  123. /**
  124. * @expectedException \RuntimeException
  125. */
  126. public function testPsr4CaseMismatch()
  127. {
  128. class_exists(__NAMESPACE__.'\Fixtures\Psr4CaseMismatch', true);
  129. }
  130. public function testNotPsr0()
  131. {
  132. $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\NotPSR0', true));
  133. }
  134. public function testNotPsr0Bis()
  135. {
  136. $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\NotPSR0bis', true));
  137. }
  138. public function testClassAlias()
  139. {
  140. $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\ClassAlias', true));
  141. }
  142. /**
  143. * @dataProvider provideDeprecatedSuper
  144. */
  145. public function testDeprecatedSuper($class, $super, $type)
  146. {
  147. set_error_handler(function() { return false; });
  148. $e = error_reporting(0);
  149. trigger_error('', E_USER_DEPRECATED);
  150. class_exists('Test\\'.__NAMESPACE__.'\\'.$class, true);
  151. error_reporting($e);
  152. restore_error_handler();
  153. $lastError = error_get_last();
  154. unset($lastError['file'], $lastError['line']);
  155. $xError = array(
  156. 'type' => E_USER_DEPRECATED,
  157. '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.',
  158. );
  159. $this->assertSame($xError, $lastError);
  160. }
  161. public function provideDeprecatedSuper()
  162. {
  163. return array(
  164. array('DeprecatedInterfaceClass', 'DeprecatedInterface', 'implements'),
  165. array('DeprecatedParentClass', 'DeprecatedClass', 'extends'),
  166. );
  167. }
  168. public function testDeprecatedSuperInSameNamespace()
  169. {
  170. set_error_handler(function() { return false; });
  171. $e = error_reporting(0);
  172. trigger_error('', E_USER_NOTICE);
  173. class_exists('Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent', true);
  174. error_reporting($e);
  175. restore_error_handler();
  176. $lastError = error_get_last();
  177. unset($lastError['file'], $lastError['line']);
  178. $xError = array(
  179. 'type' => E_USER_NOTICE,
  180. 'message' => '',
  181. );
  182. $this->assertSame($xError, $lastError);
  183. }
  184. public function testReservedForPhp7()
  185. {
  186. if (PHP_VERSION_ID >= 70000) {
  187. $this->markTestSkipped('PHP7 already prevents using reserved names.');
  188. }
  189. set_error_handler(function() { return false; });
  190. $e = error_reporting(0);
  191. trigger_error('', E_USER_NOTICE);
  192. class_exists('Test\\'.__NAMESPACE__.'\\Float', true);
  193. error_reporting($e);
  194. restore_error_handler();
  195. $lastError = error_get_last();
  196. unset($lastError['file'], $lastError['line']);
  197. $xError = array(
  198. 'type' => E_USER_DEPRECATED,
  199. 'message' => 'Test\Symfony\Component\Debug\Tests\Float uses a reserved class name (Float) that will break on PHP 7 and higher',
  200. );
  201. $this->assertSame($xError, $lastError);
  202. }
  203. }
  204. class ClassLoader
  205. {
  206. public function loadClass($class)
  207. {
  208. }
  209. public function getClassMap()
  210. {
  211. return array(__NAMESPACE__.'\Fixtures\NotPSR0bis' => __DIR__.'/Fixtures/notPsr0Bis.php');
  212. }
  213. public function findFile($class)
  214. {
  215. $fixtureDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR;
  216. if (__NAMESPACE__.'\TestingUnsilencing' === $class) {
  217. eval('-- parse error --');
  218. } elseif (__NAMESPACE__.'\TestingStacking' === $class) {
  219. eval('namespace '.__NAMESPACE__.'; class TestingStacking { function foo() {} }');
  220. } elseif (__NAMESPACE__.'\TestingCaseMismatch' === $class) {
  221. eval('namespace '.__NAMESPACE__.'; class TestingCaseMisMatch {}');
  222. } elseif (__NAMESPACE__.'\Fixtures\CaseMismatch' === $class) {
  223. return $fixtureDir.'CaseMismatch.php';
  224. } elseif (__NAMESPACE__.'\Fixtures\Psr4CaseMismatch' === $class) {
  225. return $fixtureDir.'psr4'.DIRECTORY_SEPARATOR.'Psr4CaseMismatch.php';
  226. } elseif (__NAMESPACE__.'\Fixtures\NotPSR0' === $class) {
  227. return $fixtureDir.'reallyNotPsr0.php';
  228. } elseif (__NAMESPACE__.'\Fixtures\NotPSR0bis' === $class) {
  229. return $fixtureDir.'notPsr0Bis.php';
  230. } elseif (__NAMESPACE__.'\Fixtures\DeprecatedInterface' === $class) {
  231. return $fixtureDir.'DeprecatedInterface.php';
  232. } elseif ('Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent' === $class) {
  233. eval('namespace Symfony\Bridge\Debug\Tests\Fixtures; class ExtendsDeprecatedParent extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
  234. } elseif ('Test\\'.__NAMESPACE__.'\DeprecatedParentClass' === $class) {
  235. eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedParentClass extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
  236. } elseif ('Test\\'.__NAMESPACE__.'\DeprecatedInterfaceClass' === $class) {
  237. eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\DeprecatedInterface {}');
  238. } elseif ('Test\\'.__NAMESPACE__.'\Float' === $class) {
  239. eval('namespace Test\\'.__NAMESPACE__.'; class Float {}');
  240. }
  241. }
  242. }