Skip to content

Commit

Permalink
Added ability to disable Symfony's error handler.
Browse files Browse the repository at this point in the history
PHPUnit has built in support for testing if PHP errors are thrown by
looking for special exception classes:
  PHPUnit_Framework_Error
  PHPUnit_Framework_Warning
  PHPUnit_Framework_Notice

This support is only enabled if no other error handlers are registered.
The Symfony kernel registers an error handler by default, thus disabling
PHPUnit's special PHP error exceptions if the kernel has been booted.

This commit adds support for a new configuration parameter,
error_handler.enable, which can prevent the Symfony error handler from
registering if set to false.

After this commit, by default the error handler will be disabled in the
test environment.  To enable it, add the following to your
config_test.yml:

parameters:
  error_handler.enable:  true
  • Loading branch information
Brandon Turner committed Jul 7, 2010
1 parent b6799d0 commit 4b24544
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/Symfony/Foundation/Debug/ErrorHandler.php
Expand Up @@ -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'));
}
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Foundation/Resources/config/services.xml
Expand Up @@ -11,6 +11,7 @@
<parameter key="response.class">Symfony\Components\HttpKernel\Response</parameter>
<parameter key="error_handler.class">Symfony\Foundation\Debug\ErrorHandler</parameter>
<parameter key="error_handler.level">null</parameter>
<parameter key="error_handler.enable">true</parameter>
<parameter key="kernel.include_core_classes">true</parameter>
</parameters>

Expand All @@ -21,7 +22,9 @@

<service id="error_handler" class="%error_handler.class%">
<argument>%error_handler.level%</argument>
<call method="register" />
<call method="register">
<argument>%error_handler.enable%</argument>
</call>
</service>

<service id="http_kernel" class="%http_kernel.class%">
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Foundation/Resources/config/test.xml
Expand Up @@ -9,6 +9,7 @@
<parameter key="test.client.parameters" type="collection"></parameter>
<parameter key="test.client.history.class">Symfony\Components\BrowserKit\History</parameter>
<parameter key="test.client.cookiejar.class">Symfony\Components\BrowserKit\CookieJar</parameter>
<parameter key="error_handler.enable">false</parameter>
</parameters>

<services>
Expand Down
6 changes: 4 additions & 2 deletions src/Symfony/Foundation/bootstrap.php
Expand Up @@ -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'));
}
}


Expand Down
44 changes: 44 additions & 0 deletions tests/Symfony/Tests/Foundation/Debug/ErrorHandlerTest.php
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* 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();
}
}

0 comments on commit 4b24544

Please sign in to comment.