65c80fc1cf34a7833bc9f5f72b74b0f2692ec571
[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\Debug\Exception\FatalErrorException;
16 use Symfony\Component\Debug\FatalErrorHandler\ClassNotFoundFatalErrorHandler;
17 use Symfony\Component\Debug\DebugClassLoader;
18 use Composer\Autoload\ClassLoader as ComposerClassLoader;
19
20 class ClassNotFoundFatalErrorHandlerTest extends TestCase
21 {
22     public static function setUpBeforeClass()
23     {
24         foreach (spl_autoload_functions() as $function) {
25             if (!is_array($function)) {
26                 continue;
27             }
28
29             // get class loaders wrapped by DebugClassLoader
30             if ($function[0] instanceof DebugClassLoader) {
31                 $function = $function[0]->getClassLoader();
32             }
33
34             if ($function[0] instanceof ComposerClassLoader) {
35                 $function[0]->add('Symfony_Component_Debug_Tests_Fixtures', dirname(dirname(dirname(dirname(dirname(__DIR__))))));
36                 break;
37             }
38         }
39     }
40
41     /**
42      * @dataProvider provideClassNotFoundData
43      */
44     public function testHandleClassNotFound($error, $translatedMessage, $autoloader = null)
45     {
46         if ($autoloader) {
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);
52         }
53
54         $handler = new ClassNotFoundFatalErrorHandler();
55
56         $exception = $handler->handleError($error, new FatalErrorException('', 0, $error['type'], $error['file'], $error['line']));
57
58         if ($autoloader) {
59             spl_autoload_unregister($autoloader);
60             array_map('spl_autoload_register', $autoloaders);
61         }
62
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());
68     }
69
70     public function provideClassNotFoundData()
71     {
72         $autoloader = new ComposerClassLoader();
73         $autoloader->add('Symfony\Component\Debug\Exception\\', realpath(__DIR__.'/../../Exception'));
74
75         $debugClassLoader = new DebugClassLoader(array($autoloader, 'loadClass'));
76
77         return array(
78             array(
79                 array(
80                     'type' => 1,
81                     'line' => 12,
82                     'file' => 'foo.php',
83                     'message' => 'Class \'WhizBangFactory\' not found',
84                 ),
85                 "Attempted to load class \"WhizBangFactory\" from the global namespace.\nDid you forget a \"use\" statement?",
86             ),
87             array(
88                 array(
89                     'type' => 1,
90                     'line' => 12,
91                     'file' => 'foo.php',
92                     'message' => 'Class \'Foo\\Bar\\WhizBangFactory\' not found',
93                 ),
94                 "Attempted to load class \"WhizBangFactory\" from namespace \"Foo\\Bar\".\nDid you forget a \"use\" statement for another namespace?",
95             ),
96             array(
97                 array(
98                     'type' => 1,
99                     'line' => 12,
100                     'file' => 'foo.php',
101                     'message' => 'Class \'UndefinedFunctionException\' not found',
102                 ),
103                 "Attempted to load class \"UndefinedFunctionException\" from the global namespace.\nDid you forget a \"use\" statement for \"Symfony\Component\Debug\Exception\UndefinedFunctionException\"?",
104             ),
105             array(
106                 array(
107                     'type' => 1,
108                     'line' => 12,
109                     'file' => 'foo.php',
110                     'message' => 'Class \'PEARClass\' not found',
111                 ),
112                 "Attempted to load class \"PEARClass\" from the global namespace.\nDid you forget a \"use\" statement for \"Symfony_Component_Debug_Tests_Fixtures_PEARClass\"?",
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\".\nDid you forget a \"use\" statement for \"Symfony\Component\Debug\Exception\UndefinedFunctionException\"?",
122             ),
123             array(
124                 array(
125                     'type' => 1,
126                     'line' => 12,
127                     'file' => 'foo.php',
128                     'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
129                 ),
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'),
132             ),
133             array(
134                 array(
135                     'type' => 1,
136                     'line' => 12,
137                     'file' => 'foo.php',
138                     'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
139                 ),
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'),
142             ),
143             array(
144                 array(
145                     'type' => 1,
146                     'line' => 12,
147                     'file' => 'foo.php',
148                     'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
149                 ),
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 */ },
152             ),
153         );
154     }
155
156     public function testCannotRedeclareClass()
157     {
158         if (!file_exists(__DIR__.'/../FIXTURES2/REQUIREDTWICE.PHP')) {
159             $this->markTestSkipped('Can only be run on case insensitive filesystems');
160         }
161
162         require_once __DIR__.'/../FIXTURES2/REQUIREDTWICE.PHP';
163
164         $error = array(
165             'type' => 1,
166             'line' => 12,
167             'file' => 'foo.php',
168             'message' => 'Class \'Foo\\Bar\\RequiredTwice\' not found',
169         );
170
171         $handler = new ClassNotFoundFatalErrorHandler();
172         $exception = $handler->handleError($error, new FatalErrorException('', 0, $error['type'], $error['file'], $error['line']));
173
174         $this->assertInstanceOf('Symfony\Component\Debug\Exception\ClassNotFoundException', $exception);
175     }
176 }