ClassNotFoundFatalErrorHandlerTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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\FatalErrorHandler;
  11. use Composer\Autoload\ClassLoader as ComposerClassLoader;
  12. use PHPUnit\Framework\TestCase;
  13. use Symfony\Component\Debug\DebugClassLoader;
  14. use Symfony\Component\Debug\Exception\FatalErrorException;
  15. use Symfony\Component\Debug\FatalErrorHandler\ClassNotFoundFatalErrorHandler;
  16. class ClassNotFoundFatalErrorHandlerTest extends TestCase
  17. {
  18. public static function setUpBeforeClass()
  19. {
  20. foreach (spl_autoload_functions() as $function) {
  21. if (!\is_array($function)) {
  22. continue;
  23. }
  24. // get class loaders wrapped by DebugClassLoader
  25. if ($function[0] instanceof DebugClassLoader) {
  26. $function = $function[0]->getClassLoader();
  27. }
  28. if ($function[0] instanceof ComposerClassLoader) {
  29. $function[0]->add('Symfony_Component_Debug_Tests_Fixtures', \dirname(\dirname(\dirname(\dirname(\dirname(__DIR__))))));
  30. break;
  31. }
  32. }
  33. }
  34. /**
  35. * @dataProvider provideClassNotFoundData
  36. */
  37. public function testHandleClassNotFound($error, $translatedMessage, $autoloader = null)
  38. {
  39. if ($autoloader) {
  40. // Unregister all autoloaders to ensure the custom provided
  41. // autoloader is the only one to be used during the test run.
  42. $autoloaders = spl_autoload_functions();
  43. array_map('spl_autoload_unregister', $autoloaders);
  44. spl_autoload_register($autoloader);
  45. }
  46. $handler = new ClassNotFoundFatalErrorHandler();
  47. $exception = $handler->handleError($error, new FatalErrorException('', 0, $error['type'], $error['file'], $error['line']));
  48. if ($autoloader) {
  49. spl_autoload_unregister($autoloader);
  50. array_map('spl_autoload_register', $autoloaders);
  51. }
  52. $this->assertInstanceOf('Symfony\Component\Debug\Exception\ClassNotFoundException', $exception);
  53. $this->assertRegExp($translatedMessage, $exception->getMessage());
  54. $this->assertSame($error['type'], $exception->getSeverity());
  55. $this->assertSame($error['file'], $exception->getFile());
  56. $this->assertSame($error['line'], $exception->getLine());
  57. }
  58. public function provideClassNotFoundData()
  59. {
  60. $autoloader = new ComposerClassLoader();
  61. $autoloader->add('Symfony\Component\Debug\Exception\\', realpath(__DIR__.'/../../Exception'));
  62. $autoloader->add('Symfony_Component_Debug_Tests_Fixtures', realpath(__DIR__.'/../../Tests/Fixtures'));
  63. $debugClassLoader = new DebugClassLoader([$autoloader, 'loadClass']);
  64. return [
  65. [
  66. [
  67. 'type' => 1,
  68. 'line' => 12,
  69. 'file' => 'foo.php',
  70. 'message' => 'Class \'WhizBangFactory\' not found',
  71. ],
  72. "/^Attempted to load class \"WhizBangFactory\" from the global namespace.\nDid you forget a \"use\" statement\?$/",
  73. ],
  74. [
  75. [
  76. 'type' => 1,
  77. 'line' => 12,
  78. 'file' => 'foo.php',
  79. 'message' => 'Class \'Foo\\Bar\\WhizBangFactory\' not found',
  80. ],
  81. "/^Attempted to load class \"WhizBangFactory\" from namespace \"Foo\\\\Bar\".\nDid you forget a \"use\" statement for another namespace\?$/",
  82. ],
  83. [
  84. [
  85. 'type' => 1,
  86. 'line' => 12,
  87. 'file' => 'foo.php',
  88. 'message' => 'Class \'UndefinedFunctionException\' not found',
  89. ],
  90. "/^Attempted to load class \"UndefinedFunctionException\" from the global namespace.\nDid you forget a \"use\" statement for .*\"Symfony\\\\Component\\\\Debug\\\\Exception\\\\UndefinedFunctionException\"\?$/",
  91. [$debugClassLoader, 'loadClass'],
  92. ],
  93. [
  94. [
  95. 'type' => 1,
  96. 'line' => 12,
  97. 'file' => 'foo.php',
  98. 'message' => 'Class \'PEARClass\' not found',
  99. ],
  100. "/^Attempted to load class \"PEARClass\" from the global namespace.\nDid you forget a \"use\" statement for \"Symfony_Component_Debug_Tests_Fixtures_PEARClass\"\?$/",
  101. [$debugClassLoader, 'loadClass'],
  102. ],
  103. [
  104. [
  105. 'type' => 1,
  106. 'line' => 12,
  107. 'file' => 'foo.php',
  108. 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
  109. ],
  110. "/^Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\\\\Bar\".\nDid you forget a \"use\" statement for .*\"Symfony\\\\Component\\\\Debug\\\\Exception\\\\UndefinedFunctionException\"\?$/",
  111. [$debugClassLoader, 'loadClass'],
  112. ],
  113. [
  114. [
  115. 'type' => 1,
  116. 'line' => 12,
  117. 'file' => 'foo.php',
  118. 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
  119. ],
  120. "/^Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\\\\Bar\".\nDid you forget a \"use\" statement for \"Symfony\\\\Component\\\\Debug\\\\Exception\\\\UndefinedFunctionException\"\?$/",
  121. [$autoloader, 'loadClass'],
  122. ],
  123. [
  124. [
  125. 'type' => 1,
  126. 'line' => 12,
  127. 'file' => 'foo.php',
  128. 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
  129. ],
  130. "/^Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\\\\Bar\".\nDid you forget a \"use\" statement for \"Symfony\\\\Component\\\\Debug\\\\Exception\\\\UndefinedFunctionException\"\?/",
  131. [$debugClassLoader, 'loadClass'],
  132. ],
  133. [
  134. [
  135. 'type' => 1,
  136. 'line' => 12,
  137. 'file' => 'foo.php',
  138. 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
  139. ],
  140. "/^Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\\\\Bar\".\nDid you forget a \"use\" statement for another namespace\?$/",
  141. function ($className) { /* do nothing here */ },
  142. ],
  143. ];
  144. }
  145. public function testCannotRedeclareClass()
  146. {
  147. if (!file_exists(__DIR__.'/../FIXTURES2/REQUIREDTWICE.PHP')) {
  148. $this->markTestSkipped('Can only be run on case insensitive filesystems');
  149. }
  150. require_once __DIR__.'/../FIXTURES2/REQUIREDTWICE.PHP';
  151. $error = [
  152. 'type' => 1,
  153. 'line' => 12,
  154. 'file' => 'foo.php',
  155. 'message' => 'Class \'Foo\\Bar\\RequiredTwice\' not found',
  156. ];
  157. $handler = new ClassNotFoundFatalErrorHandler();
  158. $exception = $handler->handleError($error, new FatalErrorException('', 0, $error['type'], $error['file'], $error['line']));
  159. $this->assertInstanceOf('Symfony\Component\Debug\Exception\ClassNotFoundException', $exception);
  160. }
  161. }