Skip to content

Commit

Permalink
[HttpKernel] refactored the ErrorHandler class
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed May 5, 2011
1 parent ca3c5e6 commit 3f69333
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
17 changes: 12 additions & 5 deletions src/Symfony/Component/HttpKernel/Debug/ErrorHandler.php
Expand Up @@ -31,18 +31,25 @@ class ErrorHandler
private $level;

/**
* Constructor.
* Register the error handler.
*
* @param integer $level The level at which the conversion to Exception is done (null to use the error_reporting() value and 0 to disable)
*
* @return The registered error handler
*/
public function __construct($level = null)
static public function register($level = null)
{
$this->level = null === $level ? error_reporting() : $level;
$handler = new static();
$handler->setLevel($level);

set_error_handler(array($handler, 'handle'));

return $handler;
}

public function register()
public function setLevel($level)
{
set_error_handler(array($this, 'handle'));
$this->level = null === $level ? error_reporting() : $level;
}

/**
Expand Down
47 changes: 19 additions & 28 deletions tests/Symfony/Tests/Component/HttpKernel/Debug/ErrorHandlerTest.php
Expand Up @@ -24,44 +24,35 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase

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

$this->assertInstanceOf('Symfony\Component\HttpKernel\Debug\ErrorHandler',$e);

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

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

public function testRegister()
{
$e = new ErrorHandler(3);
$e = $this->getMock(get_class($e), array('handle'));
$e->expects($this->once())->method('handle');

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

try{
trigger_error('foo');
}catch(\Exception $e){
}
restore_error_handler();
}

public function testHandle()
{
$e = new ErrorHandler(0);
$this->assertFalse($e->handle(0,'foo','foo.php',12,'foo'));
$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'));

$e = new ErrorHandler(3);
$this->assertFalse($e->handle(4,'foo','foo.php',12,'foo'));
restore_error_handler();

$e = new ErrorHandler(3);
try{
$e->handle(1, 'foo', 'foo.php', 12,'foo');
}catch(\ErrorException $e){
$this->assertSame('1: foo in foo.php line 12',$e->getMessage());
$handler = ErrorHandler::register(3);
try {
$handler->handle(1, 'foo', 'foo.php', 12, 'foo');
} catch (\ErrorException $e) {
$this->assertSame('1: foo in foo.php line 12', $e->getMessage());
}
}

restore_error_handler();
}
}

0 comments on commit 3f69333

Please sign in to comment.