/vendor/symfony/debug/Symfony/Component/Debug/Tests/FatalErrorHandler/ClassNotFoundFatalErrorHandlerTest.php

https://gitlab.com/xolotsoft/pumasruiz · PHP · 159 lines · 129 code · 17 blank · 13 comment · 3 complexity · 36ab3a5c1728650463e59be1be56f09f MD5 · raw file

  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\ClassLoader\ClassLoader as SymfonyClassLoader;
  12. use Symfony\Component\ClassLoader\UniversalClassLoader as SymfonyUniversalClassLoader;
  13. use Symfony\Component\Debug\Exception\FatalErrorException;
  14. use Symfony\Component\Debug\FatalErrorHandler\ClassNotFoundFatalErrorHandler;
  15. class ClassNotFoundFatalErrorHandlerTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @dataProvider provideClassNotFoundData
  19. */
  20. public function testHandleClassNotFound($error, $translatedMessage, $autoloader = null)
  21. {
  22. if ($autoloader) {
  23. // Unregister all autoloaders to ensure the custom provided
  24. // autoloader is the only one to be used during the test run.
  25. $autoloaders = spl_autoload_functions();
  26. array_map('spl_autoload_unregister', $autoloaders);
  27. spl_autoload_register($autoloader);
  28. }
  29. $handler = new ClassNotFoundFatalErrorHandler();
  30. $exception = $handler->handleError($error, new FatalErrorException('', 0, $error['type'], $error['file'], $error['line']));
  31. if ($autoloader) {
  32. spl_autoload_unregister($autoloader);
  33. array_map('spl_autoload_register', $autoloaders);
  34. }
  35. $this->assertInstanceof('Symfony\Component\Debug\Exception\ClassNotFoundException', $exception);
  36. $this->assertSame($translatedMessage, $exception->getMessage());
  37. $this->assertSame($error['type'], $exception->getSeverity());
  38. $this->assertSame($error['file'], $exception->getFile());
  39. $this->assertSame($error['line'], $exception->getLine());
  40. }
  41. public function provideClassNotFoundData()
  42. {
  43. $prefixes = array('Symfony\Component\Debug\Exception\\' => realpath(__DIR__.'/../../Exception'));
  44. $symfonyAutoloader = new SymfonyClassLoader();
  45. $symfonyAutoloader->addPrefixes($prefixes);
  46. $symfonyUniversalClassLoader = new SymfonyUniversalClassLoader();
  47. $symfonyUniversalClassLoader->registerPrefixes($prefixes);
  48. return array(
  49. array(
  50. array(
  51. 'type' => 1,
  52. 'line' => 12,
  53. 'file' => 'foo.php',
  54. 'message' => 'Class \'WhizBangFactory\' not found',
  55. ),
  56. 'Attempted to load class "WhizBangFactory" from the global namespace in foo.php line 12. Did you forget a use statement for this class?',
  57. ),
  58. array(
  59. array(
  60. 'type' => 1,
  61. 'line' => 12,
  62. 'file' => 'foo.php',
  63. 'message' => 'Class \'Foo\\Bar\\WhizBangFactory\' not found',
  64. ),
  65. 'Attempted to load class "WhizBangFactory" from namespace "Foo\\Bar" in foo.php line 12. Do you need to "use" it from another namespace?',
  66. ),
  67. array(
  68. array(
  69. 'type' => 1,
  70. 'line' => 12,
  71. 'file' => 'foo.php',
  72. 'message' => 'Class \'UndefinedFunctionException\' not found',
  73. ),
  74. 'Attempted to load class "UndefinedFunctionException" from the global namespace in foo.php line 12. Did you forget a use statement for this class? Perhaps you need to add a use statement for one of the following: Symfony\Component\Debug\Exception\UndefinedFunctionException.',
  75. ),
  76. array(
  77. array(
  78. 'type' => 1,
  79. 'line' => 12,
  80. 'file' => 'foo.php',
  81. 'message' => 'Class \'PEARClass\' not found',
  82. ),
  83. 'Attempted to load class "PEARClass" from the global namespace in foo.php line 12. Did you forget a use statement for this class? Perhaps you need to add a use statement for one of the following: Symfony_Component_Debug_Tests_Fixtures_PEARClass.',
  84. ),
  85. array(
  86. array(
  87. 'type' => 1,
  88. 'line' => 12,
  89. 'file' => 'foo.php',
  90. 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
  91. ),
  92. 'Attempted to load class "UndefinedFunctionException" from namespace "Foo\Bar" in foo.php line 12. Do you need to "use" it from another namespace? Perhaps you need to add a use statement for one of the following: Symfony\Component\Debug\Exception\UndefinedFunctionException.',
  93. ),
  94. array(
  95. array(
  96. 'type' => 1,
  97. 'line' => 12,
  98. 'file' => 'foo.php',
  99. 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
  100. ),
  101. 'Attempted to load class "UndefinedFunctionException" from namespace "Foo\Bar" in foo.php line 12. Do you need to "use" it from another namespace? Perhaps you need to add a use statement for one of the following: Symfony\Component\Debug\Exception\UndefinedFunctionException.',
  102. array($symfonyAutoloader, 'loadClass'),
  103. ),
  104. array(
  105. array(
  106. 'type' => 1,
  107. 'line' => 12,
  108. 'file' => 'foo.php',
  109. 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
  110. ),
  111. 'Attempted to load class "UndefinedFunctionException" from namespace "Foo\Bar" in foo.php line 12. Do you need to "use" it from another namespace? Perhaps you need to add a use statement for one of the following: Symfony\Component\Debug\Exception\UndefinedFunctionException.',
  112. array($symfonyUniversalClassLoader, 'loadClass'),
  113. ),
  114. array(
  115. array(
  116. 'type' => 1,
  117. 'line' => 12,
  118. 'file' => 'foo.php',
  119. 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
  120. ),
  121. 'Attempted to load class "UndefinedFunctionException" from namespace "Foo\Bar" in foo.php line 12. Do you need to "use" it from another namespace?',
  122. function ($className) { /* do nothing here */ },
  123. ),
  124. );
  125. }
  126. public function testCannotRedeclareClass()
  127. {
  128. if (!file_exists(__DIR__.'/../FIXTURES/REQUIREDTWICE.PHP')) {
  129. $this->markTestSkipped('Can only be run on case insensitive filesystems');
  130. }
  131. require_once __DIR__.'/../FIXTURES/REQUIREDTWICE.PHP';
  132. $error = array(
  133. 'type' => 1,
  134. 'line' => 12,
  135. 'file' => 'foo.php',
  136. 'message' => 'Class \'Foo\\Bar\\RequiredTwice\' not found',
  137. );
  138. $handler = new ClassNotFoundFatalErrorHandler();
  139. $exception = $handler->handleError($error, new FatalErrorException('', 0, $error['type'], $error['file'], $error['line']));
  140. $this->assertInstanceof('Symfony\Component\Debug\Exception\ClassNotFoundException', $exception);
  141. }
  142. }