PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/debug/Symfony/Component/Debug/Tests/DebugClassLoaderTest.php

https://gitlab.com/pthapa81/MeroSaaman-1.0
PHP | 196 lines | 134 code | 33 blank | 29 comment | 8 complexity | cf9dcf04e4da675cbd9ed6aa00683131 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. ob_start();
  55. $this->iniSet('log_errors', 0);
  56. $this->iniSet('display_errors', 1);
  57. // See below: this will fail with parse error
  58. // but this should not be @-silenced.
  59. @class_exists(__NAMESPACE__.'\TestingUnsilencing', true);
  60. $output = ob_get_clean();
  61. $this->assertStringMatchesFormat('%aParse error%a', $output);
  62. }
  63. public function testStacking()
  64. {
  65. // the ContextErrorException must not be loaded to test the workaround
  66. // for https://bugs.php.net/65322.
  67. if (class_exists('Symfony\Component\Debug\Exception\ContextErrorException', false)) {
  68. $this->markTestSkipped('The ContextErrorException class is already loaded.');
  69. }
  70. ErrorHandler::register();
  71. try {
  72. // Trigger autoloading + E_STRICT at compile time
  73. // which in turn triggers $errorHandler->handle()
  74. // that again triggers autoloading for ContextErrorException.
  75. // Error stacking works around the bug above and everything is fine.
  76. eval('
  77. namespace '.__NAMESPACE__.';
  78. class ChildTestingStacking extends TestingStacking { function foo($bar) {} }
  79. ');
  80. $this->fail('ContextErrorException expected');
  81. } catch (\ErrorException $exception) {
  82. // if an exception is thrown, the test passed
  83. restore_error_handler();
  84. restore_exception_handler();
  85. $this->assertStringStartsWith(__FILE__, $exception->getFile());
  86. if (PHP_VERSION_ID < 70000) {
  87. $this->assertRegexp('/^Runtime Notice: Declaration/', $exception->getMessage());
  88. $this->assertEquals(E_STRICT, $exception->getSeverity());
  89. } else {
  90. $this->assertRegexp('/^Warning: Declaration/', $exception->getMessage());
  91. $this->assertEquals(E_WARNING, $exception->getSeverity());
  92. }
  93. } catch (\Exception $exception) {
  94. restore_error_handler();
  95. restore_exception_handler();
  96. throw $exception;
  97. }
  98. }
  99. /**
  100. * @expectedException \RuntimeException
  101. */
  102. public function testNameCaseMismatch()
  103. {
  104. class_exists(__NAMESPACE__.'\TestingCaseMismatch', true);
  105. }
  106. /**
  107. * @expectedException \RuntimeException
  108. */
  109. public function testFileCaseMismatch()
  110. {
  111. if (!file_exists(__DIR__.'/Fixtures/CaseMismatch.php')) {
  112. $this->markTestSkipped('Can only be run on case insensitive filesystems');
  113. }
  114. class_exists(__NAMESPACE__.'\Fixtures\CaseMismatch', true);
  115. }
  116. /**
  117. * @expectedException \RuntimeException
  118. */
  119. public function testPsr4CaseMismatch()
  120. {
  121. class_exists(__NAMESPACE__.'\Fixtures\Psr4CaseMismatch', true);
  122. }
  123. public function testNotPsr0()
  124. {
  125. $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\NotPSR0', true));
  126. }
  127. public function testNotPsr0Bis()
  128. {
  129. $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\NotPSR0bis', true));
  130. }
  131. public function testClassAlias()
  132. {
  133. $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\ClassAlias', true));
  134. }
  135. }
  136. class ClassLoader
  137. {
  138. public function loadClass($class)
  139. {
  140. }
  141. public function getClassMap()
  142. {
  143. return array(__NAMESPACE__.'\Fixtures\NotPSR0bis' => __DIR__.'/Fixtures/notPsr0Bis.php');
  144. }
  145. public function findFile($class)
  146. {
  147. if (__NAMESPACE__.'\TestingUnsilencing' === $class) {
  148. eval('-- parse error --');
  149. } elseif (__NAMESPACE__.'\TestingStacking' === $class) {
  150. eval('namespace '.__NAMESPACE__.'; class TestingStacking { function foo() {} }');
  151. } elseif (__NAMESPACE__.'\TestingCaseMismatch' === $class) {
  152. eval('namespace '.__NAMESPACE__.'; class TestingCaseMisMatch {}');
  153. } elseif (__NAMESPACE__.'\Fixtures\CaseMismatch' === $class) {
  154. return __DIR__.'/Fixtures/CaseMismatch.php';
  155. } elseif (__NAMESPACE__.'\Fixtures\Psr4CaseMismatch' === $class) {
  156. return __DIR__.'/Fixtures/psr4/Psr4CaseMismatch.php';
  157. } elseif (__NAMESPACE__.'\Fixtures\NotPSR0' === $class) {
  158. return __DIR__.'/Fixtures/reallyNotPsr0.php';
  159. } elseif (__NAMESPACE__.'\Fixtures\NotPSR0bis' === $class) {
  160. return __DIR__.'/Fixtures/notPsr0Bis.php';
  161. }
  162. }
  163. }