ClassNotFoundFatalErrorHandlerTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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->assertSame($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. $debugClassLoader = new DebugClassLoader([$autoloader, 'loadClass']);
  63. return [
  64. [
  65. [
  66. 'type' => 1,
  67. 'line' => 12,
  68. 'file' => 'foo.php',
  69. 'message' => 'Class \'WhizBangFactory\' not found',
  70. ],
  71. "Attempted to load class \"WhizBangFactory\" from the global namespace.\nDid you forget a \"use\" statement?",
  72. ],
  73. [
  74. [
  75. 'type' => 1,
  76. 'line' => 12,
  77. 'file' => 'foo.php',
  78. 'message' => 'Class \'Foo\\Bar\\WhizBangFactory\' not found',
  79. ],
  80. "Attempted to load class \"WhizBangFactory\" from namespace \"Foo\\Bar\".\nDid you forget a \"use\" statement for another namespace?",
  81. ],
  82. [
  83. [
  84. 'type' => 1,
  85. 'line' => 12,
  86. 'file' => 'foo.php',
  87. 'message' => 'Class \'UndefinedFunctionException\' not found',
  88. ],
  89. "Attempted to load class \"UndefinedFunctionException\" from the global namespace.\nDid you forget a \"use\" statement for \"Symfony\Component\Debug\Exception\UndefinedFunctionException\"?",
  90. ],
  91. [
  92. [
  93. 'type' => 1,
  94. 'line' => 12,
  95. 'file' => 'foo.php',
  96. 'message' => 'Class \'PEARClass\' not found',
  97. ],
  98. "Attempted to load class \"PEARClass\" from the global namespace.\nDid you forget a \"use\" statement for \"Symfony_Component_Debug_Tests_Fixtures_PEARClass\"?",
  99. ],
  100. [
  101. [
  102. 'type' => 1,
  103. 'line' => 12,
  104. 'file' => 'foo.php',
  105. 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
  106. ],
  107. "Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\Bar\".\nDid you forget a \"use\" statement for \"Symfony\Component\Debug\Exception\UndefinedFunctionException\"?",
  108. ],
  109. [
  110. [
  111. 'type' => 1,
  112. 'line' => 12,
  113. 'file' => 'foo.php',
  114. 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
  115. ],
  116. "Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\Bar\".\nDid you forget a \"use\" statement for \"Symfony\Component\Debug\Exception\UndefinedFunctionException\"?",
  117. [$autoloader, 'loadClass'],
  118. ],
  119. [
  120. [
  121. 'type' => 1,
  122. 'line' => 12,
  123. 'file' => 'foo.php',
  124. 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
  125. ],
  126. "Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\Bar\".\nDid you forget a \"use\" statement for \"Symfony\Component\Debug\Exception\UndefinedFunctionException\"?",
  127. [$debugClassLoader, 'loadClass'],
  128. ],
  129. [
  130. [
  131. 'type' => 1,
  132. 'line' => 12,
  133. 'file' => 'foo.php',
  134. 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
  135. ],
  136. "Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\\Bar\".\nDid you forget a \"use\" statement for another namespace?",
  137. function ($className) { /* do nothing here */ },
  138. ],
  139. ];
  140. }
  141. public function testCannotRedeclareClass()
  142. {
  143. if (!file_exists(__DIR__.'/../FIXTURES2/REQUIREDTWICE.PHP')) {
  144. $this->markTestSkipped('Can only be run on case insensitive filesystems');
  145. }
  146. require_once __DIR__.'/../FIXTURES2/REQUIREDTWICE.PHP';
  147. $error = [
  148. 'type' => 1,
  149. 'line' => 12,
  150. 'file' => 'foo.php',
  151. 'message' => 'Class \'Foo\\Bar\\RequiredTwice\' not found',
  152. ];
  153. $handler = new ClassNotFoundFatalErrorHandler();
  154. $exception = $handler->handleError($error, new FatalErrorException('', 0, $error['type'], $error['file'], $error['line']));
  155. $this->assertInstanceOf('Symfony\Component\Debug\Exception\ClassNotFoundException', $exception);
  156. }
  157. }