ClassNotFoundFatalErrorHandlerTest.php 7.3 KB

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