Skip to content

Instantly share code, notes, and snippets.

@mkasberg
Last active April 1, 2018 12:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkasberg/ce7611c602e5f445b730493577b9c8e5 to your computer and use it in GitHub Desktop.
Save mkasberg/ce7611c602e5f445b730493577b9c8e5 to your computer and use it in GitHub Desktop.
PHPUnit Hides Errors After set_error_handler() Called
<?php
class CustomLogger {
public function __construct() {
// Try commenting out this line:
set_error_handler(array($this, 'handleError'));
}
public function __desctruct() {
restore_error_handler();
}
public function handleError($errno, $errstr, $errfile = null, $errline = null, $errcontext = null) {
// Do stuff.
// Log something.
return true;
}
}
<?php
/**
* CustomLogger, Thing, CustomLoggerTest, ThingTest
*
* Place all 4 files in the same directory. Run `phpunit .`. No errors will be shown.
* Now run `phpunit --process-isolation .`. Errors will be shown.
* Also try `phpunit ThingTest.php`. Errors will be shown.
* Tested with PHPUnit 5.7.15.
*/
require_once "CustomLogger.php";
class CustomLoggerTest extends PHPUnit_Framework_TestCase {
public function testSomething() {
$logger = new CustomLogger();
$this->assertTrue(true);
}
public function testSomethingElse() {
$logger = new CustomLogger();
$this->assertTrue(true);
}
}
<?php
class Thing {
public function doStuff() {
// Do stuff.
}
}
<?php
require_once "Thing.php";
class ThingTest extends PHPUnit_Framework_TestCase {
public function testTheThing() {
array_keys(10); // Throw an error!
$this->assertTrue(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment