4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Debug\Tests\FatalErrorHandler;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Debug\Exception\FatalErrorException;
16 use Symfony\Component\Debug\FatalErrorHandler\ClassNotFoundFatalErrorHandler;
17 use Symfony\Component\Debug\DebugClassLoader;
18 use Composer\Autoload\ClassLoader as ComposerClassLoader;
20 class ClassNotFoundFatalErrorHandlerTest extends TestCase
22 public static function setUpBeforeClass()
24 foreach (spl_autoload_functions() as $function) {
25 if (!is_array($function)) {
29 // get class loaders wrapped by DebugClassLoader
30 if ($function[0] instanceof DebugClassLoader) {
31 $function = $function[0]->getClassLoader();
34 if ($function[0] instanceof ComposerClassLoader) {
35 $function[0]->add('Symfony_Component_Debug_Tests_Fixtures', dirname(dirname(dirname(dirname(dirname(__DIR__))))));
42 * @dataProvider provideClassNotFoundData
44 public function testHandleClassNotFound($error, $translatedMessage, $autoloader = null)
47 // Unregister all autoloaders to ensure the custom provided
48 // autoloader is the only one to be used during the test run.
49 $autoloaders = spl_autoload_functions();
50 array_map('spl_autoload_unregister', $autoloaders);
51 spl_autoload_register($autoloader);
54 $handler = new ClassNotFoundFatalErrorHandler();
56 $exception = $handler->handleError($error, new FatalErrorException('', 0, $error['type'], $error['file'], $error['line']));
59 spl_autoload_unregister($autoloader);
60 array_map('spl_autoload_register', $autoloaders);
63 $this->assertInstanceOf('Symfony\Component\Debug\Exception\ClassNotFoundException', $exception);
64 $this->assertSame($translatedMessage, $exception->getMessage());
65 $this->assertSame($error['type'], $exception->getSeverity());
66 $this->assertSame($error['file'], $exception->getFile());
67 $this->assertSame($error['line'], $exception->getLine());
70 public function provideClassNotFoundData()
72 $autoloader = new ComposerClassLoader();
73 $autoloader->add('Symfony\Component\Debug\Exception\\', realpath(__DIR__.'/../../Exception'));
75 $debugClassLoader = new DebugClassLoader(array($autoloader, 'loadClass'));
83 'message' => 'Class \'WhizBangFactory\' not found',
85 "Attempted to load class \"WhizBangFactory\" from the global namespace.\nDid you forget a \"use\" statement?",
92 'message' => 'Class \'Foo\\Bar\\WhizBangFactory\' not found',
94 "Attempted to load class \"WhizBangFactory\" from namespace \"Foo\\Bar\".\nDid you forget a \"use\" statement for another namespace?",
101 'message' => 'Class \'UndefinedFunctionException\' not found',
103 "Attempted to load class \"UndefinedFunctionException\" from the global namespace.\nDid you forget a \"use\" statement for \"Symfony\Component\Debug\Exception\UndefinedFunctionException\"?",
110 'message' => 'Class \'PEARClass\' not found',
112 "Attempted to load class \"PEARClass\" from the global namespace.\nDid you forget a \"use\" statement for \"Symfony_Component_Debug_Tests_Fixtures_PEARClass\"?",
119 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
121 "Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\Bar\".\nDid you forget a \"use\" statement for \"Symfony\Component\Debug\Exception\UndefinedFunctionException\"?",
128 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
130 "Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\Bar\".\nDid you forget a \"use\" statement for \"Symfony\Component\Debug\Exception\UndefinedFunctionException\"?",
131 array($autoloader, 'loadClass'),
138 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
140 "Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\Bar\".\nDid you forget a \"use\" statement for \"Symfony\Component\Debug\Exception\UndefinedFunctionException\"?",
141 array($debugClassLoader, 'loadClass'),
148 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
150 "Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\\Bar\".\nDid you forget a \"use\" statement for another namespace?",
151 function ($className) { /* do nothing here */ },
156 public function testCannotRedeclareClass()
158 if (!file_exists(__DIR__.'/../FIXTURES2/REQUIREDTWICE.PHP')) {
159 $this->markTestSkipped('Can only be run on case insensitive filesystems');
162 require_once __DIR__.'/../FIXTURES2/REQUIREDTWICE.PHP';
168 'message' => 'Class \'Foo\\Bar\\RequiredTwice\' not found',
171 $handler = new ClassNotFoundFatalErrorHandler();
172 $exception = $handler->handleError($error, new FatalErrorException('', 0, $error['type'], $error['file'], $error['line']));
174 $this->assertInstanceOf('Symfony\Component\Debug\Exception\ClassNotFoundException', $exception);