ClassNotFoundFatalErrorHandlerTest.php 8.3 KB

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