I want to test a custom error handling class I put together but this poses a few issues in itself.
First off, since setting a custom error_handler in a test overrides PHPUnits error_handling , one has to call restore_error_handler() every time it is changed to custom implementation and tested, for any later tests to have phpunits error_handling. Though I admit not having tried maybe PHPUnit does reset itself after every test I kind of doubt it.
I have various configurations to test, in different conditions ie E_ERROR , E_WARNING etc ..
I then had the idea of mocking the error_handler, but am still quite green on mocking aspects of testing. A quick google search made me find a similar approach, here is the idea modified and implemented in a way that may suit my needs ...
//Create mock with error_handler method
$errorH = $this->getMock ('ErrorHandler', array ('error_handler'));
// error handler should be called at least once
$errorH->expects ($this->atLeastOnce())->method ('error_handler');
// Mock will need to be configured (here?) and tested
// against specific error_handling settings
// not implemented yet nor needed for this post.
//set mock as error handler for duration of this test
set_error_handler (array($errorH, 'error_handler'));
//test current mock error handler configuration
// for different conditions-> NOTICE / WARNING / ERROR
// Give phpunit's toy back!
restore_error_handler ();
Is this a valid approach?