ErrorHandlerTest.php 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

12
namespace Symfony\Component\Debug\Tests;
13

14
use Symfony\Component\Debug\ErrorHandler;
15 16 17 18 19 20 21 22

/**
 * ErrorHandlerTest
 *
 * @author Robert Schönthal <seroscho@googlemail.com>
 */
class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
{
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    public function testCompileTimeError()
    {
        // the ContextErrorException must not be loaded for this test to work
        if (class_exists('Symfony\Component\Debug\Exception\ContextErrorException', false)) {
            $this->markTestSkipped('The ContextErrorException class is already loaded.');
        }
    
        $handler = ErrorHandler::register(E_ALL | E_STRICT);
        $displayErrors = ini_get('display_errors');
        ini_set('display_errors', '1');

        try {
            // trigger compile time error
            eval(<<<'PHP'
class _BaseCompileTimeError { function foo() {} }
class _CompileTimeError extends _BaseCompileTimeError { function foo($invalid) {} }
PHP
            );
        } catch(\Exception $e) {
            // if an exception is thrown, the test passed
        }

        ini_set('display_errors', $displayErrors);
        restore_error_handler();
    }
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

    public function testConstruct()
    {
        $handler = ErrorHandler::register(3);

        $level = new \ReflectionProperty($handler, 'level');
        $level->setAccessible(true);

        $this->assertEquals(3, $level->getValue($handler));

        restore_error_handler();
    }

    public function testHandle()
    {
        $handler = ErrorHandler::register(0);
        $this->assertFalse($handler->handle(0, 'foo', 'foo.php', 12, 'foo'));

        restore_error_handler();

        $handler = ErrorHandler::register(3);
        $this->assertFalse($handler->handle(4, 'foo', 'foo.php', 12, 'foo'));

        restore_error_handler();

        $handler = ErrorHandler::register(3);
        try {
75
            $handler->handle(111, 'foo', 'foo.php', 12, 'foo');
76
        } catch (\ErrorException $e) {
77 78
            $this->assertSame('111: foo in foo.php line 12', $e->getMessage());
            $this->assertSame(111, $e->getSeverity());
79 80
            $this->assertSame('foo.php', $e->getFile());
            $this->assertSame(12, $e->getLine());
81 82 83
        }

        restore_error_handler();
84 85 86 87 88 89 90 91 92 93 94

        $handler = ErrorHandler::register(E_USER_DEPRECATED);
        $this->assertTrue($handler->handle(E_USER_DEPRECATED, 'foo', 'foo.php', 12, 'foo'));

        restore_error_handler();

        $handler = ErrorHandler::register(E_DEPRECATED);
        $this->assertTrue($handler->handle(E_DEPRECATED, 'foo', 'foo.php', 12, 'foo'));

        restore_error_handler();

95
        $logger = $this->getMock('Psr\Log\LoggerInterface');
96 97 98 99 100 101 102 103 104 105 106 107

        $that = $this;
        $warnArgCheck = function($message, $context) use ($that) {
            $that->assertEquals('foo', $message);
            $that->assertArrayHasKey('type', $context);
            $that->assertEquals($context['type'], ErrorHandler::TYPE_DEPRECATION);
            $that->assertArrayHasKey('stack', $context);
            $that->assertInternalType('array', $context['stack']);
        };

        $logger
            ->expects($this->once())
108
            ->method('warning')
109 110
            ->will($this->returnCallback($warnArgCheck))
        ;
111 112 113 114 115 116

        $handler = ErrorHandler::register(E_USER_DEPRECATED);
        $handler->setLogger($logger);
        $handler->handle(E_USER_DEPRECATED, 'foo', 'foo.php', 12, 'foo');

        restore_error_handler();
117 118
    }
}