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 Symfony\Component\Debug\Exception\FatalErrorException;
  12. use Symfony\Component\ClassLoader\ClassLoader as SymfonyClassLoader;
  13. use Symfony\Component\Debug\FatalErrorHandler\ClassNotFoundFatalErrorHandler;
  14. use Symfony\Component\Debug\DebugClassLoader;
  15. use Composer\Autoload\ClassLoader as ComposerClassLoader;
  16. class ClassNotFoundFatalErrorHandlerTest extends \PHPUnit_Framework_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. $prefixes = array('Symfony\Component\Debug\Exception\\' => realpath(__DIR__.'/../../Exception'));
  61. $symfonyAutoloader = new SymfonyClassLoader();
  62. $symfonyAutoloader->addPrefixes($prefixes);
  63. $debugClassLoader = new DebugClassLoader(array($symfonyAutoloader, 'loadClass'));
  64. return array(
  65. array(
  66. array(
  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. array(
  75. array(
  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. array(
  84. array(
  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. ),
  92. array(
  93. array(
  94. 'type' => 1,
  95. 'line' => 12,
  96. 'file' => 'foo.php',
  97. 'message' => 'Class \'PEARClass\' not found',
  98. ),
  99. "Attempted to load class \"PEARClass\" from the global namespace.\nDid you forget a \"use\" statement for \"Symfony_Component_Debug_Tests_Fixtures_PEARClass\"?",
  100. ),
  101. array(
  102. array(
  103. 'type' => 1,
  104. 'line' => 12,
  105. 'file' => 'foo.php',
  106. 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
  107. ),
  108. "Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\Bar\".\nDid you forget a \"use\" statement for \"Symfony\Component\Debug\Exception\UndefinedFunctionException\"?",
  109. ),
  110. array(
  111. array(
  112. 'type' => 1,
  113. 'line' => 12,
  114. 'file' => 'foo.php',
  115. 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
  116. ),
  117. "Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\Bar\".\nDid you forget a \"use\" statement for \"Symfony\Component\Debug\Exception\UndefinedFunctionException\"?",
  118. array($symfonyAutoloader, 'loadClass'),
  119. ),
  120. array(
  121. array(
  122. 'type' => 1,
  123. 'line' => 12,
  124. 'file' => 'foo.php',
  125. 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
  126. ),
  127. "Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\Bar\".\nDid you forget a \"use\" statement for \"Symfony\Component\Debug\Exception\UndefinedFunctionException\"?",
  128. array($debugClassLoader, 'loadClass'),
  129. ),
  130. array(
  131. array(
  132. 'type' => 1,
  133. 'line' => 12,
  134. 'file' => 'foo.php',
  135. 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
  136. ),
  137. "Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\\Bar\".\nDid you forget a \"use\" statement for another namespace?",
  138. function ($className) { /* do nothing here */ },
  139. ),
  140. );
  141. }
  142. public function testCannotRedeclareClass()
  143. {
  144. if (!file_exists(__DIR__.'/../FIXTURES2/REQUIREDTWICE.PHP')) {
  145. $this->markTestSkipped('Can only be run on case insensitive filesystems');
  146. }
  147. require_once __DIR__.'/../FIXTURES2/REQUIREDTWICE.PHP';
  148. $error = array(
  149. 'type' => 1,
  150. 'line' => 12,
  151. 'file' => 'foo.php',
  152. 'message' => 'Class \'Foo\\Bar\\RequiredTwice\' not found',
  153. );
  154. $handler = new ClassNotFoundFatalErrorHandler();
  155. $exception = $handler->handleError($error, new FatalErrorException('', 0, $error['type'], $error['file'], $error['line']));
  156. $this->assertInstanceOf('Symfony\Component\Debug\Exception\ClassNotFoundException', $exception);
  157. }
  158. }