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;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Debug\DebugClassLoader;
16 use Symfony\Component\Debug\ErrorHandler;
18 class DebugClassLoaderTest extends TestCase
21 * @var int Error reporting level before running tests
23 private $errorReporting;
27 protected function setUp()
29 $this->errorReporting = error_reporting(E_ALL);
30 $this->loader = new ClassLoader();
31 spl_autoload_register(array($this->loader, 'loadClass'), true, true);
32 DebugClassLoader::enable();
35 protected function tearDown()
37 DebugClassLoader::disable();
38 spl_autoload_unregister(array($this->loader, 'loadClass'));
39 error_reporting($this->errorReporting);
42 public function testIdempotence()
44 DebugClassLoader::enable();
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);
53 $this->assertNotInstanceOf('Symfony\Component\Debug\DebugClassLoader', $reflProp->getValue($function[0]));
59 $this->fail('DebugClassLoader did not register');
63 * @expectedException \Exception
64 * @expectedExceptionMessage boo
66 public function testThrowingClass()
69 class_exists(__NAMESPACE__.'\Fixtures\Throwing');
70 $this->fail('Exception expected');
71 } catch (\Exception $e) {
72 $this->assertSame('boo', $e->getMessage());
75 // the second call also should throw
76 class_exists(__NAMESPACE__.'\Fixtures\Throwing');
79 public function testUnsilencing()
81 if (\PHP_VERSION_ID >= 70000) {
82 $this->markTestSkipped('PHP7 throws exceptions, unsilencing is not required anymore.');
84 if (defined('HHVM_VERSION')) {
85 $this->markTestSkipped('HHVM is not handled in this test case.');
90 $this->iniSet('log_errors', 0);
91 $this->iniSet('display_errors', 1);
93 // See below: this will fail with parse error
94 // but this should not be @-silenced.
95 @class_exists(__NAMESPACE__.'\TestingUnsilencing', true);
97 $output = ob_get_clean();
99 $this->assertStringMatchesFormat('%aParse error%a', $output);
102 public function testStacking()
104 // the ContextErrorException must not be loaded to test the workaround
105 // for https://bugs.php.net/65322.
106 if (class_exists('Symfony\Component\Debug\Exception\ContextErrorException', false)) {
107 $this->markTestSkipped('The ContextErrorException class is already loaded.');
109 if (defined('HHVM_VERSION')) {
110 $this->markTestSkipped('HHVM is not handled in this test case.');
113 ErrorHandler::register();
116 // Trigger autoloading + E_STRICT at compile time
117 // which in turn triggers $errorHandler->handle()
118 // that again triggers autoloading for ContextErrorException.
119 // Error stacking works around the bug above and everything is fine.
122 namespace '.__NAMESPACE__.';
123 class ChildTestingStacking extends TestingStacking { function foo($bar) {} }
125 $this->fail('ContextErrorException expected');
126 } catch (\ErrorException $exception) {
127 // if an exception is thrown, the test passed
128 $this->assertStringStartsWith(__FILE__, $exception->getFile());
129 if (\PHP_VERSION_ID < 70000) {
130 $this->assertRegExp('/^Runtime Notice: Declaration/', $exception->getMessage());
131 $this->assertEquals(E_STRICT, $exception->getSeverity());
133 $this->assertRegExp('/^Warning: Declaration/', $exception->getMessage());
134 $this->assertEquals(E_WARNING, $exception->getSeverity());
137 restore_error_handler();
138 restore_exception_handler();
143 * @expectedException \RuntimeException
144 * @expectedExceptionMessage Case mismatch between loaded and declared class names
146 public function testNameCaseMismatch()
148 class_exists(__NAMESPACE__.'\TestingCaseMismatch', true);
152 * @expectedException \RuntimeException
153 * @expectedExceptionMessage Case mismatch between class and real file names
155 public function testFileCaseMismatch()
157 if (!file_exists(__DIR__.'/Fixtures/CaseMismatch.php')) {
158 $this->markTestSkipped('Can only be run on case insensitive filesystems');
161 class_exists(__NAMESPACE__.'\Fixtures\CaseMismatch', true);
165 * @expectedException \RuntimeException
166 * @expectedExceptionMessage Case mismatch between loaded and declared class names
168 public function testPsr4CaseMismatch()
170 class_exists(__NAMESPACE__.'\Fixtures\Psr4CaseMismatch', true);
173 public function testNotPsr0()
175 $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\NotPSR0', true));
178 public function testNotPsr0Bis()
180 $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\NotPSR0bis', true));
183 public function testClassAlias()
185 $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\ClassAlias', true));
189 * @dataProvider provideDeprecatedSuper
191 public function testDeprecatedSuper($class, $super, $type)
193 set_error_handler(function () { return false; });
194 $e = error_reporting(0);
195 trigger_error('', E_USER_DEPRECATED);
197 class_exists('Test\\'.__NAMESPACE__.'\\'.$class, true);
200 restore_error_handler();
202 $lastError = error_get_last();
203 unset($lastError['file'], $lastError['line']);
206 'type' => E_USER_DEPRECATED,
207 '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.',
210 $this->assertSame($xError, $lastError);
213 public function provideDeprecatedSuper()
216 array('DeprecatedInterfaceClass', 'DeprecatedInterface', 'implements'),
217 array('DeprecatedParentClass', 'DeprecatedClass', 'extends'),
221 public function testInterfaceExtendsDeprecatedInterface()
223 set_error_handler(function () { return false; });
224 $e = error_reporting(0);
225 trigger_error('', E_USER_NOTICE);
227 class_exists('Test\\'.__NAMESPACE__.'\\NonDeprecatedInterfaceClass', true);
230 restore_error_handler();
232 $lastError = error_get_last();
233 unset($lastError['file'], $lastError['line']);
236 'type' => E_USER_NOTICE,
240 $this->assertSame($xError, $lastError);
243 public function testDeprecatedSuperInSameNamespace()
245 set_error_handler(function () { return false; });
246 $e = error_reporting(0);
247 trigger_error('', E_USER_NOTICE);
249 class_exists('Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent', true);
252 restore_error_handler();
254 $lastError = error_get_last();
255 unset($lastError['file'], $lastError['line']);
258 'type' => E_USER_NOTICE,
262 $this->assertSame($xError, $lastError);
265 public function testReservedForPhp7()
267 if (\PHP_VERSION_ID >= 70000) {
268 $this->markTestSkipped('PHP7 already prevents using reserved names.');
271 set_error_handler(function () { return false; });
272 $e = error_reporting(0);
273 trigger_error('', E_USER_NOTICE);
275 class_exists('Test\\'.__NAMESPACE__.'\\Float', true);
278 restore_error_handler();
280 $lastError = error_get_last();
281 unset($lastError['file'], $lastError['line']);
284 'type' => E_USER_DEPRECATED,
285 'message' => 'The "Test\Symfony\Component\Debug\Tests\Float" class uses the reserved name "Float", it will break on PHP 7 and higher',
288 $this->assertSame($xError, $lastError);
291 public function testExtendedFinalClass()
293 set_error_handler(function () { return false; });
294 $e = error_reporting(0);
295 trigger_error('', E_USER_NOTICE);
297 class_exists('Test\\'.__NAMESPACE__.'\\ExtendsFinalClass', true);
300 restore_error_handler();
302 $lastError = error_get_last();
303 unset($lastError['file'], $lastError['line']);
306 'type' => E_USER_DEPRECATED,
307 'message' => 'The "Symfony\Component\Debug\Tests\Fixtures\FinalClass" 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\ExtendsFinalClass".',
310 $this->assertSame($xError, $lastError);
313 public function testExtendedFinalMethod()
315 $deprecations = array();
316 set_error_handler(function ($type, $msg) use (&$deprecations) { $deprecations[] = $msg; });
317 $e = error_reporting(E_USER_DEPRECATED);
319 class_exists(__NAMESPACE__.'\\Fixtures\\ExtendedFinalMethod', true);
322 restore_error_handler();
325 '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".',
326 '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 $this->assertSame($xError, $deprecations);
332 public function testExtendedDeprecatedMethodDoesntTriggerAnyNotice()
334 set_error_handler(function () { return false; });
335 $e = error_reporting(0);
336 trigger_error('', E_USER_NOTICE);
338 class_exists('Test\\'.__NAMESPACE__.'\\ExtendsAnnotatedClass', true);
341 restore_error_handler();
343 $lastError = error_get_last();
344 unset($lastError['file'], $lastError['line']);
346 $this->assertSame(array('type' => E_USER_NOTICE, 'message' => ''), $lastError);
349 public function testInternalsUse()
351 $deprecations = array();
352 set_error_handler(function ($type, $msg) use (&$deprecations) { $deprecations[] = $msg; });
353 $e = error_reporting(E_USER_DEPRECATED);
355 class_exists('Test\\'.__NAMESPACE__.'\\ExtendsInternals', true);
358 restore_error_handler();
360 $this->assertSame($deprecations, array(
361 '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".',
362 '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".',
363 '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".',
364 '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".',
368 public function testUseTraitWithInternalMethod()
370 $deprecations = array();
371 set_error_handler(function ($type, $msg) use (&$deprecations) { $deprecations[] = $msg; });
372 $e = error_reporting(E_USER_DEPRECATED);
374 class_exists('Test\\'.__NAMESPACE__.'\\UseTraitWithInternalMethod', true);
377 restore_error_handler();
379 $this->assertSame(array(), $deprecations);
385 public function loadClass($class)
389 public function getClassMap()
391 return array(__NAMESPACE__.'\Fixtures\NotPSR0bis' => __DIR__.'/Fixtures/notPsr0Bis.php');
394 public function findFile($class)
396 $fixtureDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR;
398 if (__NAMESPACE__.'\TestingUnsilencing' === $class) {
399 eval('-- parse error --');
400 } elseif (__NAMESPACE__.'\TestingStacking' === $class) {
401 eval('namespace '.__NAMESPACE__.'; class TestingStacking { function foo() {} }');
402 } elseif (__NAMESPACE__.'\TestingCaseMismatch' === $class) {
403 eval('namespace '.__NAMESPACE__.'; class TestingCaseMisMatch {}');
404 } elseif (__NAMESPACE__.'\Fixtures\Psr4CaseMismatch' === $class) {
405 return $fixtureDir.'psr4'.DIRECTORY_SEPARATOR.'Psr4CaseMismatch.php';
406 } elseif (__NAMESPACE__.'\Fixtures\NotPSR0' === $class) {
407 return $fixtureDir.'reallyNotPsr0.php';
408 } elseif (__NAMESPACE__.'\Fixtures\NotPSR0bis' === $class) {
409 return $fixtureDir.'notPsr0Bis.php';
410 } elseif ('Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent' === $class) {
411 eval('namespace Symfony\Bridge\Debug\Tests\Fixtures; class ExtendsDeprecatedParent extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
412 } elseif ('Test\\'.__NAMESPACE__.'\DeprecatedParentClass' === $class) {
413 eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedParentClass extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
414 } elseif ('Test\\'.__NAMESPACE__.'\DeprecatedInterfaceClass' === $class) {
415 eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\DeprecatedInterface {}');
416 } elseif ('Test\\'.__NAMESPACE__.'\NonDeprecatedInterfaceClass' === $class) {
417 eval('namespace Test\\'.__NAMESPACE__.'; class NonDeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\NonDeprecatedInterface {}');
418 } elseif ('Test\\'.__NAMESPACE__.'\Float' === $class) {
419 eval('namespace Test\\'.__NAMESPACE__.'; class Float {}');
420 } elseif ('Test\\'.__NAMESPACE__.'\ExtendsFinalClass' === $class) {
421 eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsFinalClass extends \\'.__NAMESPACE__.'\Fixtures\FinalClass {}');
422 } elseif ('Test\\'.__NAMESPACE__.'\ExtendsAnnotatedClass' === $class) {
423 eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsAnnotatedClass extends \\'.__NAMESPACE__.'\Fixtures\AnnotatedClass {
424 public function deprecatedMethod() { }
426 } elseif ('Test\\'.__NAMESPACE__.'\ExtendsInternals' === $class) {
427 eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsInternals extends ExtendsInternalsParent {
428 use \\'.__NAMESPACE__.'\Fixtures\InternalTrait;
430 public function internalMethod() { }
432 } elseif ('Test\\'.__NAMESPACE__.'\ExtendsInternalsParent' === $class) {
433 eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsInternalsParent extends \\'.__NAMESPACE__.'\Fixtures\InternalClass implements \\'.__NAMESPACE__.'\Fixtures\InternalInterface { }');
434 } elseif ('Test\\'.__NAMESPACE__.'\UseTraitWithInternalMethod' === $class) {
435 eval('namespace Test\\'.__NAMESPACE__.'; class UseTraitWithInternalMethod { use \\'.__NAMESPACE__.'\Fixtures\TraitWithInternalMethod; }');