Handle errors using a custom error handler

Without the custom handler, assertions after the first failure wouldn't be executed. This catches E_USER_DEPRECATED errors and passes everything else on to the phpunit error handler.
This commit is contained in:
Colin Frei 2012-12-03 21:43:03 +01:00
parent f49704b14f
commit b35de9e15b
2 changed files with 54 additions and 27 deletions

View File

@ -17,6 +17,16 @@ use Symfony\Component\HttpFoundation\Request;
class RequestTest extends \PHPUnit_Framework_TestCase
{
public function deprecationErrorHandler($errorNumber, $message, $file, $line, $context)
{
if ($errorNumber & E_USER_DEPRECATED) {
return true;
}
\PHPUnit_Util_ErrorHandler::handleError($errorNumber, $message, $file, $line);
return false;
}
/**
* @covers Symfony\Component\HttpFoundation\Request::__construct
*/
@ -706,9 +716,9 @@ class RequestTest extends \PHPUnit_Framework_TestCase
$request->initialize(array(), array(), array(), array(), array(), $server);
if ($proxy) {
$this->setExpectedException('PHPUnit_Framework_Error_Deprecated');
set_error_handler(array($this, "deprecationErrorHandler"));
$this->startTrustingProxyData();
restore_error_handler();
}
$this->assertEquals($expected, $request->getClientIp($proxy));
@ -823,11 +833,10 @@ class RequestTest extends \PHPUnit_Framework_TestCase
$this->disableHttpMethodParameterOverride();
}
/**
* @expectedException PHPUnit_Framework_Error_Deprecated
*/
public function testOverrideGlobals()
{
set_error_handler(array($this, "deprecationErrorHandler"));
$request = new Request();
$request->initialize(array('foo' => 'bar'));
@ -858,6 +867,8 @@ class RequestTest extends \PHPUnit_Framework_TestCase
// restore initial $_SERVER array
$_SERVER = $server;
restore_error_handler();
}
public function testGetScriptName()
@ -1100,13 +1111,14 @@ class RequestTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider splitHttpAcceptHeaderData
* @expectedException PHPUnit_Framework_Error_Deprecated
*/
public function testSplitHttpAcceptHeader($acceptHeader, $expected)
{
$request = new Request();
set_error_handler(array($this, "deprecationErrorHandler"));
$this->assertEquals($expected, $request->splitHttpAcceptHeader($acceptHeader));
restore_error_handler();
}
public function splitHttpAcceptHeaderData()
@ -1254,9 +1266,6 @@ class RequestTest extends \PHPUnit_Framework_TestCase
$property->setValue(false);
}
/**
* @expectedException PHPUnit_Framework_Error_Deprecated
*/
public function testTrustedProxies()
{
$request = Request::create('http://example.com/');
@ -1277,11 +1286,13 @@ class RequestTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($request->isSecure());
// trusted proxy via deprecated trustProxyData()
set_error_handler(array($this, "deprecationErrorHandler"));
Request::trustProxyData();
$this->assertEquals('2.2.2.2', $request->getClientIp());
$this->assertEquals('real.example.com', $request->getHost());
$this->assertEquals(443, $request->getPort());
$this->assertTrue($request->isSecure());
restore_error_handler();
// disabling proxy trusting
Request::setTrustedProxies(array());

View File

@ -47,6 +47,16 @@ class SessionTest extends \PHPUnit_Framework_TestCase
$this->session = null;
}
public function deprecationErrorHandler($errorNumber, $message, $file, $line, $context)
{
if ($errorNumber & E_USER_DEPRECATED) {
return true;
}
\PHPUnit_Util_ErrorHandler::handleError($errorNumber, $message, $file, $line);
return false;
}
public function testStart()
{
$this->assertEquals('', $this->session->getId());
@ -191,11 +201,10 @@ class SessionTest extends \PHPUnit_Framework_TestCase
// deprecated since 2.1, will be removed from 2.3
/**
* @expectedException PHPUnit_Framework_Error_Deprecated
*/
public function testGetSetFlashes()
{
set_error_handler(array($this, "deprecationErrorHandler"));
$array = array('notice' => 'hello', 'error' => 'none');
$this->assertEquals(array(), $this->session->getFlashes());
$this->session->setFlashes($array);
@ -206,13 +215,14 @@ class SessionTest extends \PHPUnit_Framework_TestCase
// test that BC works by only retrieving the first added.
$this->session->getFlashBag()->add('notice', 'foo2');
$this->assertEquals(array('notice' => 'foo'), $this->session->getFlashes());
restore_error_handler();
}
/**
* @expectedException PHPUnit_Framework_Error_Deprecated
*/
public function testGetFlashesWithArray()
{
set_error_handler(array($this, "deprecationErrorHandler"));
$array = array('notice' => 'hello', 'error' => 'none');
$this->assertEquals(array(), $this->session->getFlashes());
$this->session->setFlash('foo', $array);
@ -224,13 +234,14 @@ class SessionTest extends \PHPUnit_Framework_TestCase
$this->session->setFlash('foo', $array);
$this->assertEquals(array('foo' => 'hello'), $this->session->getFlashes());
$this->assertEquals(array(), $this->session->getFlashes());
restore_error_handler();
}
/**
* @expectedException PHPUnit_Framework_Error_Deprecated
*/
public function testGetSetFlash()
{
set_error_handler(array($this, "deprecationErrorHandler"));
$this->assertNull($this->session->getFlash('notice'));
$this->assertEquals('default', $this->session->getFlash('notice', 'default'));
$this->session->getFlashBag()->add('notice', 'foo');
@ -239,36 +250,39 @@ class SessionTest extends \PHPUnit_Framework_TestCase
// test that BC works by only retrieving the first added.
$this->assertEquals('foo', $this->session->getFlash('notice'));
$this->assertNull($this->session->getFlash('notice'));
restore_error_handler();
}
/**
* @expectedException PHPUnit_Framework_Error_Deprecated
*/
public function testHasFlash()
{
set_error_handler(array($this, "deprecationErrorHandler"));
$this->assertFalse($this->session->hasFlash('notice'));
$this->session->setFlash('notice', 'foo');
$this->assertTrue($this->session->hasFlash('notice'));
restore_error_handler();
}
/**
* @expectedException PHPUnit_Framework_Error_Deprecated
*/
public function testRemoveFlash()
{
set_error_handler(array($this, "deprecationErrorHandler"));
$this->session->setFlash('notice', 'foo');
$this->session->setFlash('error', 'bar');
$this->assertTrue($this->session->hasFlash('notice'));
$this->session->removeFlash('error');
$this->assertTrue($this->session->hasFlash('notice'));
$this->assertFalse($this->session->hasFlash('error'));
restore_error_handler();
}
/**
* @expectedException PHPUnit_Framework_Error_Deprecated
*/
public function testClearFlashes()
{
set_error_handler(array($this, "deprecationErrorHandler"));
$this->assertFalse($this->session->hasFlash('notice'));
$this->assertFalse($this->session->hasFlash('error'));
$this->session->setFlash('notice', 'foo');
@ -278,6 +292,8 @@ class SessionTest extends \PHPUnit_Framework_TestCase
$this->session->clearFlashes();
$this->assertFalse($this->session->hasFlash('notice'));
$this->assertFalse($this->session->hasFlash('error'));
restore_error_handler();
}
/**