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