PluginProbe
UpStream: a Project Management Plugin for WordPress / trunk
UpStream: a Project Management Plugin for WordPress vtrunk
trunk 1.39.0 1.39.1 1.39.2 1.39.3 2.0.7 2.1.0
upstream / vendor / symfony / debug / Tests / DebugClassLoaderTest.php

DebugClassLoaderTest.php in UpStream: a Project Management Plugin for WordPress trunk, at vendor/symfony/debug/Tests/DebugClassLoaderTest.php

449 lines 18.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Debug\DebugClassLoader;
16 use Symfony\Component\Debug\ErrorHandler;
17
18 class DebugClassLoaderTest extends TestCase
19 {
20 /**
21 * @var int Error reporting level before running tests
22 */
23 private $errorReporting;
24
25 private $loader;
26
27 protected function setUp()
28 {
29 $this->errorReporting = error_reporting(E_ALL);
30 $this->loader = new ClassLoader();
31 spl_autoload_register([$this->loader, 'loadClass'], true, true);
32 DebugClassLoader::enable();
33 }
34
35 protected function tearDown()
36 {
37 DebugClassLoader::disable();
38 spl_autoload_unregister([$this->loader, 'loadClass']);
39 error_reporting($this->errorReporting);
40 }
41
42 public function testIdempotence()
43 {
44 DebugClassLoader::enable();
45
46 $functions = spl_autoload_functions();
47 foreach ($functions as $function) {
48 if (\is_array($function) && $function[0] instanceof DebugClassLoader) {
49 $reflClass = new \ReflectionClass($function[0]);
50 $reflProp = $reflClass->getProperty('classLoader');
51 $reflProp->setAccessible(true);
52
53 $this->assertNotInstanceOf('Symfony\Component\Debug\DebugClassLoader', $reflProp->getValue($function[0]));
54
55 return;
56 }
57 }
58
59 $this->fail('DebugClassLoader did not register');
60 }
61
62 public function testThrowingClass()
63 {
64 $this->expectException('Exception');
65 $this->expectExceptionMessage('boo');
66 try {
67 class_exists(Fixtures\Throwing::class);
68 $this->fail('Exception expected');
69 } catch (\Exception $e) {
70 $this->assertSame('boo', $e->getMessage());
71 }
72
73 // the second call also should throw
74 class_exists(Fixtures\Throwing::class);
75 }
76
77 public function testUnsilencing()
78 {
79 if (\PHP_VERSION_ID >= 70000) {
80 $this->markTestSkipped('PHP7 throws exceptions, unsilencing is not required anymore.');
81 }
82 if (\defined('HHVM_VERSION')) {
83 $this->markTestSkipped('HHVM is not handled in this test case.');
84 }
85
86 ob_start();
87
88 $this->iniSet('log_errors', 0);
89 $this->iniSet('display_errors', 1);
90
91 // See below: this will fail with parse error
92 // but this should not be @-silenced.
93 @class_exists(TestingUnsilencing::class, true);
94
95 $output = ob_get_clean();
96
97 $this->assertStringMatchesFormat('%aParse error%a', $output);
98 }
99
100 /**
101 * @requires PHP < 8.0
102 */
103 public function testStacking()
104 {
105 // the ContextErrorException must not be loaded to test the workaround
106 // for https://bugs.php.net/65322.
107 if (class_exists('Symfony\Component\Debug\Exception\ContextErrorException', false)) {
108 $this->markTestSkipped('The ContextErrorException class is already loaded.');
109 }
110 if (\defined('HHVM_VERSION')) {
111 $this->markTestSkipped('HHVM is not handled in this test case.');
112 }
113
114 ErrorHandler::register();
115
116 try {
117 // Trigger autoloading + E_STRICT at compile time
118 // which in turn triggers $errorHandler->handle()
119 // that again triggers autoloading for ContextErrorException.
120 // Error stacking works around the bug above and everything is fine.
121
122 eval('
123 namespace '.__NAMESPACE__.';
124 class ChildTestingStacking extends TestingStacking { function foo($bar) {} }
125 ');
126 $this->fail('ContextErrorException expected');
127 } catch (\ErrorException $exception) {
128 // if an exception is thrown, the test passed
129 $this->assertStringStartsWith(__FILE__, $exception->getFile());
130 if (\PHP_VERSION_ID < 70000) {
131 $this->assertMatchesRegularExpression('/^Runtime Notice: Declaration/', $exception->getMessage());
132 $this->assertEquals(E_STRICT, $exception->getSeverity());
133 } else {
134 $this->assertMatchesRegularExpression('/^Warning: Declaration/', $exception->getMessage());
135 $this->assertEquals(E_WARNING, $exception->getSeverity());
136 }
137 } finally {
138 restore_error_handler();
139 restore_exception_handler();
140 }
141 }
142
143 public function testNameCaseMismatch()
144 {
145 $this->expectException('RuntimeException');
146 $this->expectExceptionMessage('Case mismatch between loaded and declared class names');
147 class_exists(TestingCaseMismatch::class, true);
148 }
149
150 public function testFileCaseMismatch()
151 {
152 $this->expectException('RuntimeException');
153 $this->expectExceptionMessage('Case mismatch between class and real file names');
154 if (!file_exists(__DIR__.'/Fixtures/CaseMismatch.php')) {
155 $this->markTestSkipped('Can only be run on case insensitive filesystems');
156 }
157
158 class_exists(Fixtures\CaseMismatch::class, true);
159 }
160
161 public function testPsr4CaseMismatch()
162 {
163 $this->expectException('RuntimeException');
164 $this->expectExceptionMessage('Case mismatch between loaded and declared class names');
165 class_exists(__NAMESPACE__.'\Fixtures\Psr4CaseMismatch', true);
166 }
167
168 public function testNotPsr0()
169 {
170 $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\NotPSR0', true));
171 }
172
173 public function testNotPsr0Bis()
174 {
175 $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\NotPSR0bis', true));
176 }
177
178 public function testClassAlias()
179 {
180 $this->assertTrue(class_exists(Fixtures\ClassAlias::class, true));
181 }
182
183 /**
184 * @dataProvider provideDeprecatedSuper
185 */
186 public function testDeprecatedSuper($class, $super, $type)
187 {
188 set_error_handler(function () { return false; });
189 $e = error_reporting(0);
190 @trigger_error('', E_USER_DEPRECATED);
191
192 class_exists('Test\\'.__NAMESPACE__.'\\'.$class, true);
193
194 error_reporting($e);
195 restore_error_handler();
196
197 $lastError = error_get_last();
198 unset($lastError['file'], $lastError['line']);
199
200 $xError = [
201 'type' => E_USER_DEPRECATED,
202 'message' => 'The "Test\Symfony\Component\Debug\Tests\\'.$class.'" class '.$type.' "Symfony\Component\Debug\Tests\Fixtures\\'.$super.'" that is deprecated but this is a test deprecation notice.',
203 ];
204
205 $this->assertSame($xError, $lastError);
206 }
207
208 public function provideDeprecatedSuper()
209 {
210 return [
211 ['DeprecatedInterfaceClass', 'DeprecatedInterface', 'implements'],
212 ['DeprecatedParentClass', 'DeprecatedClass', 'extends'],
213 ];
214 }
215
216 public function testInterfaceExtendsDeprecatedInterface()
217 {
218 set_error_handler(function () { return false; });
219 $e = error_reporting(0);
220 trigger_error('', E_USER_NOTICE);
221
222 class_exists('Test\\'.NonDeprecatedInterfaceClass::class, true);
223
224 error_reporting($e);
225 restore_error_handler();
226
227 $lastError = error_get_last();
228 unset($lastError['file'], $lastError['line']);
229
230 $xError = [
231 'type' => E_USER_NOTICE,
232 'message' => '',
233 ];
234
235 $this->assertSame($xError, $lastError);
236 }
237
238 public function testDeprecatedSuperInSameNamespace()
239 {
240 set_error_handler(function () { return false; });
241 $e = error_reporting(0);
242 trigger_error('', E_USER_NOTICE);
243
244 class_exists('Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent', true);
245
246 error_reporting($e);
247 restore_error_handler();
248
249 $lastError = error_get_last();
250 unset($lastError['file'], $lastError['line']);
251
252 $xError = [
253 'type' => E_USER_NOTICE,
254 'message' => '',
255 ];
256
257 $this->assertSame($xError, $lastError);
258 }
259
260 public function testReservedForPhp7()
261 {
262 if (\PHP_VERSION_ID >= 70000) {
263 $this->markTestSkipped('PHP7 already prevents using reserved names.');
264 }
265
266 set_error_handler(function () { return false; });
267 $e = error_reporting(0);
268 trigger_error('', E_USER_NOTICE);
269
270 class_exists('Test\\'.Float::class, true);
271
272 error_reporting($e);
273 restore_error_handler();
274
275 $lastError = error_get_last();
276 unset($lastError['file'], $lastError['line']);
277
278 $xError = [
279 'type' => E_USER_DEPRECATED,
280 'message' => 'The "Test\Symfony\Component\Debug\Tests\Float" class uses the reserved name "Float", it will break on PHP 7 and higher',
281 ];
282
283 $this->assertSame($xError, $lastError);
284 }
285
286 public function testExtendedFinalClass()
287 {
288 $deprecations = [];
289 set_error_handler(function ($type, $msg) use (&$deprecations) { $deprecations[] = $msg; });
290 $e = error_reporting(E_USER_DEPRECATED);
291
292 require __DIR__.'/Fixtures/FinalClasses.php';
293
294 $i = 1;
295 while (class_exists($finalClass = Fixtures\FinalClass::class.$i++, false)) {
296 spl_autoload_call($finalClass);
297 class_exists('Test\\'.__NAMESPACE__.'\\Extends'.substr($finalClass, strrpos($finalClass, '\\') + 1), true);
298 }
299
300 error_reporting($e);
301 restore_error_handler();
302
303 $this->assertSame([
304 'The "Symfony\Component\Debug\Tests\Fixtures\FinalClass1" class is considered final since version 3.3. It may change without further notice as of its next major version. You should not extend it from "Test\Symfony\Component\Debug\Tests\ExtendsFinalClass1".',
305 'The "Symfony\Component\Debug\Tests\Fixtures\FinalClass2" class is considered final. It may change without further notice as of its next major version. You should not extend it from "Test\Symfony\Component\Debug\Tests\ExtendsFinalClass2".',
306 'The "Symfony\Component\Debug\Tests\Fixtures\FinalClass3" class is considered final comment with @@@ and ***. It may change without further notice as of its next major version. You should not extend it from "Test\Symfony\Component\Debug\Tests\ExtendsFinalClass3".',
307 'The "Symfony\Component\Debug\Tests\Fixtures\FinalClass4" class is considered final. It may change without further notice as of its next major version. You should not extend it from "Test\Symfony\Component\Debug\Tests\ExtendsFinalClass4".',
308 'The "Symfony\Component\Debug\Tests\Fixtures\FinalClass5" class is considered final multiline comment. It may change without further notice as of its next major version. You should not extend it from "Test\Symfony\Component\Debug\Tests\ExtendsFinalClass5".',
309 'The "Symfony\Component\Debug\Tests\Fixtures\FinalClass6" class is considered final. It may change without further notice as of its next major version. You should not extend it from "Test\Symfony\Component\Debug\Tests\ExtendsFinalClass6".',
310 'The "Symfony\Component\Debug\Tests\Fixtures\FinalClass7" class is considered final another multiline comment... It may change without further notice as of its next major version. You should not extend it from "Test\Symfony\Component\Debug\Tests\ExtendsFinalClass7".',
311 'The "Symfony\Component\Debug\Tests\Fixtures\FinalClass8" class is considered final. It may change without further notice as of its next major version. You should not extend it from "Test\Symfony\Component\Debug\Tests\ExtendsFinalClass8".',
312 ], $deprecations);
313 }
314
315 public function testExtendedFinalMethod()
316 {
317 $deprecations = [];
318 set_error_handler(function ($type, $msg) use (&$deprecations) { $deprecations[] = $msg; });
319 $e = error_reporting(E_USER_DEPRECATED);
320
321 class_exists(Fixtures\ExtendedFinalMethod::class, true);
322
323 error_reporting($e);
324 restore_error_handler();
325
326 $xError = [
327 'The "Symfony\Component\Debug\Tests\Fixtures\FinalMethod::finalMethod()" method is considered final since version 3.3. It may change without further notice as of its next major version. You should not extend it from "Symfony\Component\Debug\Tests\Fixtures\ExtendedFinalMethod".',
328 'The "Symfony\Component\Debug\Tests\Fixtures\FinalMethod::finalMethod2()" method is considered final. It may change without further notice as of its next major version. You should not extend it from "Symfony\Component\Debug\Tests\Fixtures\ExtendedFinalMethod".',
329 ];
330
331 $this->assertSame($xError, $deprecations);
332 }
333
334 public function testExtendedDeprecatedMethodDoesntTriggerAnyNotice()
335 {
336 set_error_handler(function () { return false; });
337 $e = error_reporting(0);
338 trigger_error('', E_USER_NOTICE);
339
340 class_exists('Test\\'.ExtendsAnnotatedClass::class, true);
341
342 error_reporting($e);
343 restore_error_handler();
344
345 $lastError = error_get_last();
346 unset($lastError['file'], $lastError['line']);
347
348 $this->assertSame(['type' => E_USER_NOTICE, 'message' => ''], $lastError);
349 }
350
351 public function testInternalsUse()
352 {
353 $deprecations = [];
354 set_error_handler(function ($type, $msg) use (&$deprecations) { $deprecations[] = $msg; });
355 $e = error_reporting(E_USER_DEPRECATED);
356
357 class_exists('Test\\'.ExtendsInternals::class, true);
358
359 error_reporting($e);
360 restore_error_handler();
361
362 $this->assertSame($deprecations, [
363 'The "Symfony\Component\Debug\Tests\Fixtures\InternalInterface" interface is considered internal. It may change without further notice. You should not use it from "Test\Symfony\Component\Debug\Tests\ExtendsInternalsParent".',
364 'The "Symfony\Component\Debug\Tests\Fixtures\InternalClass" class is considered internal since version 3.4. It may change without further notice. You should not use it from "Test\Symfony\Component\Debug\Tests\ExtendsInternalsParent".',
365 'The "Symfony\Component\Debug\Tests\Fixtures\InternalTrait" trait is considered internal. It may change without further notice. You should not use it from "Test\Symfony\Component\Debug\Tests\ExtendsInternals".',
366 'The "Symfony\Component\Debug\Tests\Fixtures\InternalClass::internalMethod()" method is considered internal since version 3.4. It may change without further notice. You should not extend it from "Test\Symfony\Component\Debug\Tests\ExtendsInternals".',
367 ]);
368 }
369
370 public function testUseTraitWithInternalMethod()
371 {
372 $deprecations = [];
373 set_error_handler(function ($type, $msg) use (&$deprecations) { $deprecations[] = $msg; });
374 $e = error_reporting(E_USER_DEPRECATED);
375
376 class_exists('Test\\'.UseTraitWithInternalMethod::class, true);
377
378 error_reporting($e);
379 restore_error_handler();
380
381 $this->assertSame([], $deprecations);
382 }
383
384 public function testEvaluatedCode()
385 {
386 $this->assertTrue(class_exists(Fixtures\DefinitionInEvaluatedCode::class, true));
387 }
388 }
389
390 class ClassLoader
391 {
392 public function loadClass($class)
393 {
394 }
395
396 public function getClassMap()
397 {
398 return [__NAMESPACE__.'\Fixtures\NotPSR0bis' => __DIR__.'/Fixtures/notPsr0Bis.php'];
399 }
400
401 public function findFile($class)
402 {
403 $fixtureDir = __DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR;
404
405 if (TestingUnsilencing::class === $class) {
406 eval('-- parse error --');
407 } elseif (TestingStacking::class === $class) {
408 eval('namespace '.__NAMESPACE__.'; class TestingStacking { function foo() {} }');
409 } elseif (TestingCaseMismatch::class === $class) {
410 eval('namespace '.__NAMESPACE__.'; class TestingCaseMisMatch {}');
411 } elseif (__NAMESPACE__.'\Fixtures\Psr4CaseMismatch' === $class) {
412 return $fixtureDir.'psr4'.\DIRECTORY_SEPARATOR.'Psr4CaseMismatch.php';
413 } elseif (__NAMESPACE__.'\Fixtures\NotPSR0' === $class) {
414 return $fixtureDir.'reallyNotPsr0.php';
415 } elseif (__NAMESPACE__.'\Fixtures\NotPSR0bis' === $class) {
416 return $fixtureDir.'notPsr0Bis.php';
417 } elseif ('Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent' === $class) {
418 eval('namespace Symfony\Bridge\Debug\Tests\Fixtures; class ExtendsDeprecatedParent extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
419 } elseif ('Test\\'.DeprecatedParentClass::class === $class) {
420 eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedParentClass extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
421 } elseif ('Test\\'.DeprecatedInterfaceClass::class === $class) {
422 eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\DeprecatedInterface {}');
423 } elseif ('Test\\'.NonDeprecatedInterfaceClass::class === $class) {
424 eval('namespace Test\\'.__NAMESPACE__.'; class NonDeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\NonDeprecatedInterface {}');
425 } elseif ('Test\\'.Float::class === $class) {
426 eval('namespace Test\\'.__NAMESPACE__.'; class Float {}');
427 } elseif (0 === strpos($class, 'Test\\'.ExtendsFinalClass::class)) {
428 $classShortName = substr($class, strrpos($class, '\\') + 1);
429 eval('namespace Test\\'.__NAMESPACE__.'; class '.$classShortName.' extends \\'.__NAMESPACE__.'\Fixtures\\'.substr($classShortName, 7).' {}');
430 } elseif ('Test\\'.ExtendsAnnotatedClass::class === $class) {
431 eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsAnnotatedClass extends \\'.__NAMESPACE__.'\Fixtures\AnnotatedClass {
432 public function deprecatedMethod() { }
433 }');
434 } elseif ('Test\\'.ExtendsInternals::class === $class) {
435 eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsInternals extends ExtendsInternalsParent {
436 use \\'.__NAMESPACE__.'\Fixtures\InternalTrait;
437
438 public function internalMethod() { }
439 }');
440 } elseif ('Test\\'.ExtendsInternalsParent::class === $class) {
441 eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsInternalsParent extends \\'.__NAMESPACE__.'\Fixtures\InternalClass implements \\'.__NAMESPACE__.'\Fixtures\InternalInterface { }');
442 } elseif ('Test\\'.UseTraitWithInternalMethod::class === $class) {
443 eval('namespace Test\\'.__NAMESPACE__.'; class UseTraitWithInternalMethod { use \\'.__NAMESPACE__.'\Fixtures\TraitWithInternalMethod; }');
444 }
445
446 return null;
447 }
448 }
449