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 set_error_handler(function () { return false; });
316 $e = error_reporting(0);
317 trigger_error('', E_USER_NOTICE);
319 class_exists(__NAMESPACE__.'\\Fixtures\\ExtendedFinalMethod', true);
322 restore_error_handler();
324 $lastError = error_get_last();
325 unset($lastError['file'], $lastError['line']);
328 'type' => E_USER_DEPRECATED,
329 'message' => '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".',
332 $this->assertSame($xError, $lastError);
335 public function testExtendedDeprecatedMethodDoesntTriggerAnyNotice()
337 set_error_handler(function () { return false; });
338 $e = error_reporting(0);
339 trigger_error('', E_USER_NOTICE);
341 class_exists('Test\\'.__NAMESPACE__.'\\ExtendsAnnotatedClass', true);
344 restore_error_handler();
346 $lastError = error_get_last();
347 unset($lastError['file'], $lastError['line']);
349 $this->assertSame(array('type' => E_USER_NOTICE, 'message' => ''), $lastError);
352 public function testInternalsUse()
354 $deprecations = array();
355 set_error_handler(function ($type, $msg) use (&$deprecations) { $deprecations[] = $msg; });
356 $e = error_reporting(E_USER_DEPRECATED);
358 class_exists('Test\\'.__NAMESPACE__.'\\ExtendsInternals', true);
361 restore_error_handler();
363 $this->assertSame($deprecations, array(
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\InternalInterface" interface is considered internal. It may change without further notice. You should not use it from "Test\Symfony\Component\Debug\Tests\ExtendsInternalsParent".',
366 '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".',
367 'The "Symfony\Component\Debug\Tests\Fixtures\InternalTrait2::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".',
374 public function loadClass($class)
378 public function getClassMap()
380 return array(__NAMESPACE__.'\Fixtures\NotPSR0bis' => __DIR__.'/Fixtures/notPsr0Bis.php');
383 public function findFile($class)
385 $fixtureDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR;
387 if (__NAMESPACE__.'\TestingUnsilencing' === $class) {
388 eval('-- parse error --');
389 } elseif (__NAMESPACE__.'\TestingStacking' === $class) {
390 eval('namespace '.__NAMESPACE__.'; class TestingStacking { function foo() {} }');
391 } elseif (__NAMESPACE__.'\TestingCaseMismatch' === $class) {
392 eval('namespace '.__NAMESPACE__.'; class TestingCaseMisMatch {}');
393 } elseif (__NAMESPACE__.'\Fixtures\Psr4CaseMismatch' === $class) {
394 return $fixtureDir.'psr4'.DIRECTORY_SEPARATOR.'Psr4CaseMismatch.php';
395 } elseif (__NAMESPACE__.'\Fixtures\NotPSR0' === $class) {
396 return $fixtureDir.'reallyNotPsr0.php';
397 } elseif (__NAMESPACE__.'\Fixtures\NotPSR0bis' === $class) {
398 return $fixtureDir.'notPsr0Bis.php';
399 } elseif ('Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent' === $class) {
400 eval('namespace Symfony\Bridge\Debug\Tests\Fixtures; class ExtendsDeprecatedParent extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
401 } elseif ('Test\\'.__NAMESPACE__.'\DeprecatedParentClass' === $class) {
402 eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedParentClass extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
403 } elseif ('Test\\'.__NAMESPACE__.'\DeprecatedInterfaceClass' === $class) {
404 eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\DeprecatedInterface {}');
405 } elseif ('Test\\'.__NAMESPACE__.'\NonDeprecatedInterfaceClass' === $class) {
406 eval('namespace Test\\'.__NAMESPACE__.'; class NonDeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\NonDeprecatedInterface {}');
407 } elseif ('Test\\'.__NAMESPACE__.'\Float' === $class) {
408 eval('namespace Test\\'.__NAMESPACE__.'; class Float {}');
409 } elseif ('Test\\'.__NAMESPACE__.'\ExtendsFinalClass' === $class) {
410 eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsFinalClass extends \\'.__NAMESPACE__.'\Fixtures\FinalClass {}');
411 } elseif ('Test\\'.__NAMESPACE__.'\ExtendsAnnotatedClass' === $class) {
412 eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsAnnotatedClass extends \\'.__NAMESPACE__.'\Fixtures\AnnotatedClass {
413 public function deprecatedMethod() { }
415 } elseif ('Test\\'.__NAMESPACE__.'\ExtendsInternals' === $class) {
416 eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsInternals extends ExtendsInternalsParent {
417 use \\'.__NAMESPACE__.'\Fixtures\InternalTrait;
419 public function internalMethod() { }
421 } elseif ('Test\\'.__NAMESPACE__.'\ExtendsInternalsParent' === $class) {
422 eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsInternalsParent extends \\'.__NAMESPACE__.'\Fixtures\InternalClass implements \\'.__NAMESPACE__.'\Fixtures\InternalInterface { }');