diff --git a/src/Symfony/Foundation/Debug/ErrorHandler.php b/src/Symfony/Foundation/Debug/ErrorHandler.php index 5da7104ed7ca..c514d37dfa73 100644 --- a/src/Symfony/Foundation/Debug/ErrorHandler.php +++ b/src/Symfony/Foundation/Debug/ErrorHandler.php @@ -42,9 +42,11 @@ public function __construct($level = null) $this->level = null === $level ? error_reporting() : $level; } - public function register() + public function register($enable=true) { - set_error_handler(array($this, 'handle')); + if($enable) { + set_error_handler(array($this, 'handle')); + } } /** diff --git a/src/Symfony/Foundation/Resources/config/services.xml b/src/Symfony/Foundation/Resources/config/services.xml index 97dadfbef7d0..68c954d50cf0 100644 --- a/src/Symfony/Foundation/Resources/config/services.xml +++ b/src/Symfony/Foundation/Resources/config/services.xml @@ -11,6 +11,7 @@ Symfony\Components\HttpKernel\Response Symfony\Foundation\Debug\ErrorHandler null + true true @@ -21,7 +22,9 @@ %error_handler.level% - + + %error_handler.enable% + diff --git a/src/Symfony/Foundation/Resources/config/test.xml b/src/Symfony/Foundation/Resources/config/test.xml index a9f89b556289..94c484dc820a 100644 --- a/src/Symfony/Foundation/Resources/config/test.xml +++ b/src/Symfony/Foundation/Resources/config/test.xml @@ -9,6 +9,7 @@ Symfony\Components\BrowserKit\History Symfony\Components\BrowserKit\CookieJar + false diff --git a/src/Symfony/Foundation/bootstrap.php b/src/Symfony/Foundation/bootstrap.php index dc222294baf7..9e88374967fb 100644 --- a/src/Symfony/Foundation/bootstrap.php +++ b/src/Symfony/Foundation/bootstrap.php @@ -287,9 +287,11 @@ public function __construct($level = null) $this->level = null === $level ? error_reporting() : $level; } - public function register() + public function register($enable=true) { - set_error_handler(array($this, 'handle')); + if($enable) { + set_error_handler(array($this, 'handle')); + } } diff --git a/tests/Symfony/Tests/Foundation/Debug/ErrorHandlerTest.php b/tests/Symfony/Tests/Foundation/Debug/ErrorHandlerTest.php new file mode 100644 index 000000000000..5837092a923f --- /dev/null +++ b/tests/Symfony/Tests/Foundation/Debug/ErrorHandlerTest.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Foundation\Debug; + +use Symfony\Foundation\Debug\ErrorHandler; + +class ErrorHandlerTest extends \PHPUnit_Framework_TestCase +{ + /** + * @covers Symfony\Foundation\Debug\ErrorHandler::register + */ + public function testRegister() + { + $fakeHandler = function($errno, $errstr) + { + die('Fake error handler triggered'); + }; + + $oldHandler = set_error_handler($fakeHandler); + restore_error_handler(); + + $handler = new ErrorHandler(); + $handler->register(false); + $newHandler = set_error_handler($fakeHandler); + restore_error_handler(); + $this->assertEquals($oldHandler, $newHandler); + + $handler = new ErrorHandler(); + $handler->register(true); + $newHandler = set_error_handler($fakeHandler); + restore_error_handler(); + $this->assertNotEquals($oldHandler, $newHandler); + restore_error_handler(); + } +}