upstream
/
vendor
/
symfony
/
debug
/
Tests
/
FatalErrorHandler
/
ClassNotFoundFatalErrorHandlerTest.php
ClassNotFoundFatalErrorHandlerTest.php in UpStream: a Project Management Plugin for WordPress trunk, at 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 Composer\Autoload\ClassLoader as ComposerClassLoader; |
| 15 | use PHPUnit\Framework\TestCase; |
| 16 | use Symfony\Component\Debug\DebugClassLoader; |
| 17 | use Symfony\Component\Debug\Exception\FatalErrorException; |
| 18 | use Symfony\Component\Debug\FatalErrorHandler\ClassNotFoundFatalErrorHandler; |
| 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 | if (!\is_array($function)) { |
| 34 | continue; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | if ($function[0] instanceof ComposerClassLoader) { |
| 39 | $function[0]->add('Symfony_Component_Debug_Tests_Fixtures', \dirname(\dirname(\dirname(\dirname(\dirname(__DIR__)))))); |
| 40 | break; |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @dataProvider provideClassNotFoundData |
| 47 | */ |
| 48 | public function testHandleClassNotFound($error, $translatedMessage, $autoloader = null) |
| 49 | { |
| 50 | if ($autoloader) { |
| 51 | // Unregister all autoloaders to ensure the custom provided |
| 52 | // autoloader is the only one to be used during the test run. |
| 53 | $autoloaders = spl_autoload_functions(); |
| 54 | array_map('spl_autoload_unregister', $autoloaders); |
| 55 | spl_autoload_register($autoloader); |
| 56 | } |
| 57 | |
| 58 | $handler = new ClassNotFoundFatalErrorHandler(); |
| 59 | |
| 60 | $exception = $handler->handleError($error, new FatalErrorException('', 0, $error['type'], $error['file'], $error['line'])); |
| 61 | |
| 62 | if ($autoloader) { |
| 63 | spl_autoload_unregister($autoloader); |
| 64 | array_map('spl_autoload_register', $autoloaders); |
| 65 | } |
| 66 | |
| 67 | $this->assertInstanceOf('Symfony\Component\Debug\Exception\ClassNotFoundException', $exception); |
| 68 | $this->assertMatchesRegularExpression($translatedMessage, $exception->getMessage()); |
| 69 | $this->assertSame($error['type'], $exception->getSeverity()); |
| 70 | $this->assertSame($error['file'], $exception->getFile()); |
| 71 | $this->assertSame($error['line'], $exception->getLine()); |
| 72 | } |
| 73 | |
| 74 | public function provideClassNotFoundData() |
| 75 | { |
| 76 | $autoloader = new ComposerClassLoader(); |
| 77 | $autoloader->add('Symfony\Component\Debug\Exception\\', realpath(__DIR__.'/../../Exception')); |
| 78 | $autoloader->add('Symfony_Component_Debug_Tests_Fixtures', realpath(__DIR__.'/../../Tests/Fixtures')); |
| 79 | |
| 80 | $debugClassLoader = new DebugClassLoader([$autoloader, 'loadClass']); |
| 81 | |
| 82 | return [ |
| 83 | [ |
| 84 | [ |
| 85 | 'type' => 1, |
| 86 | 'line' => 12, |
| 87 | 'file' => 'foo.php', |
| 88 | 'message' => 'Class "WhizBangFactory" not found', |
| 89 | ], |
| 90 | "/^Attempted to load class \"WhizBangFactory\" from the global namespace.\nDid you forget a \"use\" statement\?$/", |
| 91 | ], |
| 92 | [ |
| 93 | [ |
| 94 | 'type' => 1, |
| 95 | 'line' => 12, |
| 96 | 'file' => 'foo.php', |
| 97 | 'message' => 'Class \'WhizBangFactory\' not found', |
| 98 | ], |
| 99 | "/^Attempted to load class \"WhizBangFactory\" from the global namespace.\nDid you forget a \"use\" statement\?$/", |
| 100 | ], |
| 101 | [ |
| 102 | [ |
| 103 | 'type' => 1, |
| 104 | 'line' => 12, |
| 105 | 'file' => 'foo.php', |
| 106 | 'message' => 'Class \'Foo\\Bar\\WhizBangFactory\' not found', |
| 107 | ], |
| 108 | "/^Attempted to load class \"WhizBangFactory\" from namespace \"Foo\\\\Bar\".\nDid you forget a \"use\" statement for another namespace\?$/", |
| 109 | ], |
| 110 | [ |
| 111 | [ |
| 112 | 'type' => 1, |
| 113 | 'line' => 12, |
| 114 | 'file' => 'foo.php', |
| 115 | 'message' => 'Class "Foo\\Bar\\WhizBangFactory" not found', |
| 116 | ], |
| 117 | "/^Attempted to load class \"WhizBangFactory\" from namespace \"Foo\\\\Bar\".\nDid you forget a \"use\" statement for another namespace\?$/", |
| 118 | ], |
| 119 | [ |
| 120 | [ |
| 121 | 'type' => 1, |
| 122 | 'line' => 12, |
| 123 | 'file' => 'foo.php', |
| 124 | 'message' => 'Interface "Foo\\Bar\\WhizBangInterface" not found', |
| 125 | ], |
| 126 | "/^Attempted to load interface \"WhizBangInterface\" from namespace \"Foo\\\\Bar\".\nDid you forget a \"use\" statement for another namespace\?$/", |
| 127 | ], |
| 128 | [ |
| 129 | [ |
| 130 | 'type' => 1, |
| 131 | 'line' => 12, |
| 132 | 'file' => 'foo.php', |
| 133 | 'message' => 'Trait "Foo\\Bar\\WhizBangTrait" not found', |
| 134 | ], |
| 135 | "/^Attempted to load trait \"WhizBangTrait\" from namespace \"Foo\\\\Bar\".\nDid you forget a \"use\" statement for another namespace\?$/", |
| 136 | ], |
| 137 | [ |
| 138 | [ |
| 139 | 'type' => 1, |
| 140 | 'line' => 12, |
| 141 | 'file' => 'foo.php', |
| 142 | 'message' => 'Class \'UndefinedFunctionException\' not found', |
| 143 | ], |
| 144 | "/^Attempted to load class \"UndefinedFunctionException\" from the global namespace.\nDid you forget a \"use\" statement for .*\"Symfony\\\\Component\\\\Debug\\\\Exception\\\\UndefinedFunctionException\"\?$/", |
| 145 | [$debugClassLoader, 'loadClass'], |
| 146 | ], |
| 147 | [ |
| 148 | [ |
| 149 | 'type' => 1, |
| 150 | 'line' => 12, |
| 151 | 'file' => 'foo.php', |
| 152 | 'message' => 'Class \'PEARClass\' not found', |
| 153 | ], |
| 154 | "/^Attempted to load class \"PEARClass\" from the global namespace.\nDid you forget a \"use\" statement for \"Symfony_Component_Debug_Tests_Fixtures_PEARClass\"\?$/", |
| 155 | [$debugClassLoader, 'loadClass'], |
| 156 | ], |
| 157 | [ |
| 158 | [ |
| 159 | 'type' => 1, |
| 160 | 'line' => 12, |
| 161 | 'file' => 'foo.php', |
| 162 | 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found', |
| 163 | ], |
| 164 | "/^Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\\\\Bar\".\nDid you forget a \"use\" statement for .*\"Symfony\\\\Component\\\\Debug\\\\Exception\\\\UndefinedFunctionException\"\?$/", |
| 165 | [$debugClassLoader, 'loadClass'], |
| 166 | ], |
| 167 | [ |
| 168 | [ |
| 169 | 'type' => 1, |
| 170 | 'line' => 12, |
| 171 | 'file' => 'foo.php', |
| 172 | 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found', |
| 173 | ], |
| 174 | "/^Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\\\\Bar\".\nDid you forget a \"use\" statement for \"Symfony\\\\Component\\\\Debug\\\\Exception\\\\UndefinedFunctionException\"\?$/", |
| 175 | [$autoloader, 'loadClass'], |
| 176 | ], |
| 177 | [ |
| 178 | [ |
| 179 | 'type' => 1, |
| 180 | 'line' => 12, |
| 181 | 'file' => 'foo.php', |
| 182 | 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found', |
| 183 | ], |
| 184 | "/^Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\\\\Bar\".\nDid you forget a \"use\" statement for \"Symfony\\\\Component\\\\Debug\\\\Exception\\\\UndefinedFunctionException\"\?/", |
| 185 | [$debugClassLoader, 'loadClass'], |
| 186 | ], |
| 187 | [ |
| 188 | [ |
| 189 | 'type' => 1, |
| 190 | 'line' => 12, |
| 191 | 'file' => 'foo.php', |
| 192 | 'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found', |
| 193 | ], |
| 194 | "/^Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\\\\Bar\".\nDid you forget a \"use\" statement for another namespace\?$/", |
| 195 | function ($className) { /* do nothing here */ }, |
| 196 | ], |
| 197 | ]; |
| 198 | } |
| 199 | |
| 200 | public function testCannotRedeclareClass() |
| 201 | { |
| 202 | if (!file_exists(__DIR__.'/../FIXTURES2/REQUIREDTWICE.PHP')) { |
| 203 | $this->markTestSkipped('Can only be run on case insensitive filesystems'); |
| 204 | } |
| 205 | |
| 206 | require_once __DIR__.'/../FIXTURES2/REQUIREDTWICE.PHP'; |
| 207 | |
| 208 | $error = [ |
| 209 | 'type' => 1, |
| 210 | 'line' => 12, |
| 211 | 'file' => 'foo.php', |
| 212 | 'message' => 'Class \'Foo\\Bar\\RequiredTwice\' not found', |
| 213 | ]; |
| 214 | |
| 215 | $handler = new ClassNotFoundFatalErrorHandler(); |
| 216 | $exception = $handler->handleError($error, new FatalErrorException('', 0, $error['type'], $error['file'], $error['line'])); |
| 217 | |
| 218 | $this->assertInstanceOf('Symfony\Component\Debug\Exception\ClassNotFoundException', $exception); |
| 219 | } |
| 220 | } |
| 221 |