Skip to content

Instantly share code, notes, and snippets.

@mkasberg

mkasberg/CustomLogger.php

Last active Apr 1, 2018
Embed
What would you like to do?
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