Last active
February 23, 2018 17:20
-
-
Save fabiang/93a451c3de3b3240d147b474d32b5bdb to your computer and use it in GitHub Desktop.
Disable trigger_error() for only one test in PHPUnit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyNamespace; | |
use PHPUnit\Framework\TestCase; | |
use PHPUnit_Framework_Error_Warning as WarningError; | |
class BarTest extends TestCase | |
{ | |
private $object; | |
private $errorHandler; | |
private $expectedErrors = 0; | |
protected function setUp() | |
{ | |
$this->object = new Bar; | |
$this->errorHandler = set_error_handler([$this, 'handleErrors']); | |
} | |
protected function tearDown() | |
{ | |
restore_error_handler(); | |
$this->expectedErrors = 0; | |
} | |
// this is the test which expectes a trigger_error(…, E_USER_WARNING) | |
public function testFooTriggerEUserWarning() | |
{ | |
$this->expectException(WarningError::class); | |
$this->object->foo(); | |
} | |
//this is the test which should ignore the trigger_error() and check the return value | |
public function testFooReturnsValue() | |
{ | |
set_error_handler(function () {}); | |
$this->expectedErrors = E_USER_WARNING; | |
// $this->assertSame(…, $this->object->foo()); | |
} | |
public function handleErrors($errno, ...$args) | |
{ | |
// expected error type | |
if ($this->expectedErrors & $errno) { | |
return; | |
} | |
call_user_func($this->errorHandler, $errno, ...$args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment