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 | E_STRICT);
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');
62 public function testUnsilencing()
64 if (\PHP_VERSION_ID >= 70000) {
65 $this->markTestSkipped('PHP7 throws exceptions, unsilencing is not required anymore.');
67 if (defined('HHVM_VERSION')) {
68 $this->markTestSkipped('HHVM is not handled in this test case.');
73 $this->iniSet('log_errors', 0);
74 $this->iniSet('display_errors', 1);
76 // See below: this will fail with parse error
77 // but this should not be @-silenced.
78 @class_exists(__NAMESPACE__.'\TestingUnsilencing', true);
80 $output = ob_get_clean();
82 $this->assertStringMatchesFormat('%aParse error%a', $output);
85 public function testStacking()
87 // the ContextErrorException must not be loaded to test the workaround
88 // for https://bugs.php.net/65322.
89 if (class_exists('Symfony\Component\Debug\Exception\ContextErrorException', false)) {
90 $this->markTestSkipped('The ContextErrorException class is already loaded.');
92 if (defined('HHVM_VERSION')) {
93 $this->markTestSkipped('HHVM is not handled in this test case.');
96 ErrorHandler::register();
99 // Trigger autoloading + E_STRICT at compile time
100 // which in turn triggers $errorHandler->handle()
101 // that again triggers autoloading for ContextErrorException.
102 // Error stacking works around the bug above and everything is fine.
105 namespace '.__NAMESPACE__.';
106 class ChildTestingStacking extends TestingStacking { function foo($bar) {} }
108 $this->fail('ContextErrorException expected');
109 } catch (\ErrorException $exception) {
110 // if an exception is thrown, the test passed
111 restore_error_handler();
112 restore_exception_handler();
113 $this->assertStringStartsWith(__FILE__, $exception->getFile());
114 if (\PHP_VERSION_ID < 70000) {
115 $this->assertRegExp('/^Runtime Notice: Declaration/', $exception->getMessage());
116 $this->assertEquals(E_STRICT, $exception->getSeverity());
118 $this->assertRegExp('/^Warning: Declaration/', $exception->getMessage());
119 $this->assertEquals(E_WARNING, $exception->getSeverity());
121 } catch (\Exception $exception) {
122 restore_error_handler();
123 restore_exception_handler();
130 * @expectedException \RuntimeException
132 public function testNameCaseMismatch()
134 class_exists(__NAMESPACE__.'\TestingCaseMismatch', true);
138 * @expectedException \RuntimeException
139 * @expectedExceptionMessage Case mismatch between class and real file names
141 public function testFileCaseMismatch()
143 if (!file_exists(__DIR__.'/Fixtures/CaseMismatch.php')) {
144 $this->markTestSkipped('Can only be run on case insensitive filesystems');
147 class_exists(__NAMESPACE__.'\Fixtures\CaseMismatch', true);
151 * @expectedException \RuntimeException
153 public function testPsr4CaseMismatch()
155 class_exists(__NAMESPACE__.'\Fixtures\Psr4CaseMismatch', true);
158 public function testNotPsr0()
160 $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\NotPSR0', true));
163 public function testNotPsr0Bis()
165 $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\NotPSR0bis', true));
168 public function testClassAlias()
170 $this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\ClassAlias', true));
174 * @dataProvider provideDeprecatedSuper
176 public function testDeprecatedSuper($class, $super, $type)
178 set_error_handler(function () { return false; });
179 $e = error_reporting(0);
180 trigger_error('', E_USER_DEPRECATED);
182 class_exists('Test\\'.__NAMESPACE__.'\\'.$class, true);
185 restore_error_handler();
187 $lastError = error_get_last();
188 unset($lastError['file'], $lastError['line']);
191 'type' => E_USER_DEPRECATED,
192 '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.',
195 $this->assertSame($xError, $lastError);
198 public function provideDeprecatedSuper()
201 array('DeprecatedInterfaceClass', 'DeprecatedInterface', 'implements'),
202 array('DeprecatedParentClass', 'DeprecatedClass', 'extends'),
206 public function testInterfaceExtendsDeprecatedInterface()
208 set_error_handler(function () { return false; });
209 $e = error_reporting(0);
210 trigger_error('', E_USER_NOTICE);
212 class_exists('Test\\'.__NAMESPACE__.'\\NonDeprecatedInterfaceClass', true);
215 restore_error_handler();
217 $lastError = error_get_last();
218 unset($lastError['file'], $lastError['line']);
221 'type' => E_USER_NOTICE,
225 $this->assertSame($xError, $lastError);
228 public function testDeprecatedSuperInSameNamespace()
230 set_error_handler(function () { return false; });
231 $e = error_reporting(0);
232 trigger_error('', E_USER_NOTICE);
234 class_exists('Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent', true);
237 restore_error_handler();
239 $lastError = error_get_last();
240 unset($lastError['file'], $lastError['line']);
243 'type' => E_USER_NOTICE,
247 $this->assertSame($xError, $lastError);
250 public function testReservedForPhp7()
252 if (\PHP_VERSION_ID >= 70000) {
253 $this->markTestSkipped('PHP7 already prevents using reserved names.');
256 set_error_handler(function () { return false; });
257 $e = error_reporting(0);
258 trigger_error('', E_USER_NOTICE);
260 class_exists('Test\\'.__NAMESPACE__.'\\Float', true);
263 restore_error_handler();
265 $lastError = error_get_last();
266 unset($lastError['file'], $lastError['line']);
269 'type' => E_USER_DEPRECATED,
270 'message' => 'Test\Symfony\Component\Debug\Tests\Float uses a reserved class name (Float) that will break on PHP 7 and higher',
273 $this->assertSame($xError, $lastError);
279 public function loadClass($class)
283 public function getClassMap()
285 return array(__NAMESPACE__.'\Fixtures\NotPSR0bis' => __DIR__.'/Fixtures/notPsr0Bis.php');
288 public function findFile($class)
290 $fixtureDir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR;
292 if (__NAMESPACE__.'\TestingUnsilencing' === $class) {
293 eval('-- parse error --');
294 } elseif (__NAMESPACE__.'\TestingStacking' === $class) {
295 eval('namespace '.__NAMESPACE__.'; class TestingStacking { function foo() {} }');
296 } elseif (__NAMESPACE__.'\TestingCaseMismatch' === $class) {
297 eval('namespace '.__NAMESPACE__.'; class TestingCaseMisMatch {}');
298 } elseif (__NAMESPACE__.'\Fixtures\CaseMismatch' === $class) {
299 return $fixtureDir.'CaseMismatch.php';
300 } elseif (__NAMESPACE__.'\Fixtures\Psr4CaseMismatch' === $class) {
301 return $fixtureDir.'psr4'.DIRECTORY_SEPARATOR.'Psr4CaseMismatch.php';
302 } elseif (__NAMESPACE__.'\Fixtures\NotPSR0' === $class) {
303 return $fixtureDir.'reallyNotPsr0.php';
304 } elseif (__NAMESPACE__.'\Fixtures\NotPSR0bis' === $class) {
305 return $fixtureDir.'notPsr0Bis.php';
306 } elseif (__NAMESPACE__.'\Fixtures\DeprecatedInterface' === $class) {
307 return $fixtureDir.'DeprecatedInterface.php';
308 } elseif ('Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent' === $class) {
309 eval('namespace Symfony\Bridge\Debug\Tests\Fixtures; class ExtendsDeprecatedParent extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
310 } elseif ('Test\\'.__NAMESPACE__.'\DeprecatedParentClass' === $class) {
311 eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedParentClass extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
312 } elseif ('Test\\'.__NAMESPACE__.'\DeprecatedInterfaceClass' === $class) {
313 eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\DeprecatedInterface {}');
314 } elseif ('Test\\'.__NAMESPACE__.'\NonDeprecatedInterfaceClass' === $class) {
315 eval('namespace Test\\'.__NAMESPACE__.'; class NonDeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\NonDeprecatedInterface {}');
316 } elseif ('Test\\'.__NAMESPACE__.'\Float' === $class) {
317 eval('namespace Test\\'.__NAMESPACE__.'; class Float {}');