Thanks for asking this question, it was not clear to me at the very beginning where the problem resulted from writing a test for it. That is due to the nature of error and exception handling, especially in testing these things can be considered a bit harder and there are normally no ready-made "easy" solutions because at the end of the day you need to write the test yourself.
I hope this could be clarified in comments and the following can show how to approach them in a way.
NOTE: As we're writing test-code, I use assert()
statements in the examples, and this is by intention as assert
is a more pure language feature. It makes the examples more portable, too.
To make use of it, execute php having assertions in development mode and throwning (zend.assertions=1
and assert.exception=1
) and run the example as it evolves and have later phpunit run with these settings as well.
You can certainly port this into a concrete test-case to implement it Phpunit specific, for the examples the concrete assertion library should not be of interest, but the assertions. They are expected in the examples to bring PHP to halt if they aren't fullfilled and this is how I wrote the examples (some people say those are expectations).
In general testing code that tests for the side-effect of there being an error:
$lasterror = error_get_last();
if ($lasterror !== null) {
//do something
}
and then in your test-suite you get not errors but exceptions:
@trigger_error('Oh no'); // ErrorException: Oh no
which will prevent further code to run (the code you want to put under test).
What to do now? Well, the naked error-trigger would make phpunit highlight you've got an ErrorException, what you can do is to assert the fixture-setup is correct (fixture is the "Oh no" error) and just declare it so.
What? Yes, we turn the obstacle in front of us into the good condition just by saying so:
try {
@trigger_error('Oh no');
} catch (ErrorException $good) {
assert('Oh no' === $good->getMessage(), get_class($good));
} finally {
assert(isset($good), 'an expected exception was thrown');
}
This example helps to better describe what is going on and what we know to deal with, however the original problem still remains: Due to some error handler turning errors into exceptions, error_get_last()
returns NULL
as there was no error, but an exception.
Execute this code. It will show it passes.
So what is to assert is the opposite. Let's do this, turn the good condition into this bad, bad failure by negating it:
try {
@trigger_error('Oh no');
} catch (ErrorException $fail) {
assert(false, sprintf('the "%s" exception must not be thrown', get_class($fail)));
} finally {
assert(!isset($fail), 'there must not have been any exception');
}
I hope you can follow so far. First flip.
Now the fixture will only be asserted properly set-up when the expected state is given, here asserting that no ErrorException is being thrown, as we learned that would be "bad" (ruin any test of code with the error_get_last()
call).
This is normally what one would expect to be the first version. We actually have already done two test runs now. One green, one red.
As long as this code does not pass, we know the fixture was not set-up and hence there is no need to even run any further tests.
As you can see testing is as easy as programming. You write the test code as-if everything is already in order, pure magic. Even when it makes no sense at all (there is no test yet, right?!) and still this is what you want to achieve.
Why is something that simple so hard? One part might be that writing the code before the test can be irritating then when writing the test. The mind is filled with expectations about the code while it should be only concentrated on the test(-code).
Don't sweat, just don't get irritated that testing then does not work first of all, that would be the same otherwise, too. A good test starts with failing very soon at the beginning. And this is exactly where we are and what we did is just to turn condition red into green. Second flip.
In testing this is also called the Happy Path, that is where it goes happily ever after and the test just tests for that what when things are unspecifically just working and unspecifically just testing for it.
But this only to side-track you a bit.
So now let's make the fixture pass this happy-path (remove the existing error handler, set the error, restore the old handler, I know, you have read this up from the manual already, right? So I can spare the links, sorry for being lazy):
try {
set_error_handler(null); // <--- this
@trigger_error('Oh no');
restore_error_handler(); // <--- restore original runtime configuration
} catch (ErrorException $fail) {
assert(false, sprintf('the "%s" exception must not be thrown', get_class($fail)));
} finally {
assert(!isset($fail), 'there must not have been any exception');
}
Et voilá, the happy path is green!
But this is only the happy path. Good tests test for others paths, too. And rigorously.
Didn't we have one? A right, yes, the one with the first assertion that ErrorException were triggered, we have that test already. What once was good despite a failure is now good again (but despite the name, on the unhappy path). Third flip.
try {
set_error_handler(null);
@trigger_error('Oh no');
restore_error_handler();
try {
@trigger_error('Oh no');
} catch (ErrorException $good) {
assert('Oh no' === $good->getMessage(), get_class($good));
} finally {
assert(isset($good), 'an expected exception was thrown');
}
} catch (ErrorException $fail) {
assert(false, sprintf('the "%s" exception must not be thrown', get_class($fail)));
} finally {
assert(!isset($fail), 'there must not have been any exception');
}
Wow, now this certainly escalated as the story evolved. And it's not even complete yet. You've spotted the one or other problem with it, right?
The initial expectation that there is the ErrorException throwing error-handler on the happy path is missing. Let's put it on top as an early check (and unset the check variable afterwards as there is the re-use for the post-assertion).
And even more importantly, ensure error_get_last()
returns NULL as otherwise the test could be easily tainted for the result:
try {
@trigger_error('Oh no');
} catch (ErrorException $good) {
assert('Oh no' === $good->getMessage(), get_class($good));
} finally {
assert(isset($good), 'an expected exception was thrown');
}
unset($good);
assert(NULL === error_get_last()); // <-- this easy to miss
try {
set_error_handler(null);
@trigger_error('Oh no');
restore_error_handler();
try {
@trigger_error('Oh no');
} catch (ErrorException $good) {
assert('Oh no' === $good->getMessage(), get_class($good));
} finally {
assert(isset($good), 'an expected exception was thrown');
}
} catch (ErrorException $fail) {
assert(false, sprintf('the "%s" exception must not be thrown', get_class($fail)));
} finally {
assert(!isset($fail), 'there must not have been any exception');
}
// continue here...
It is verbose but shows all the things that are done:
- awareness that triggering an error alone does not work and why
- the precondition that error_get_last(); returns NULL before setting an error
- that there is an ErrorException error-handler that needs to get out of the way to trigger a ture PHP error
- that this error handler is re-instated as likely the system under test needs it (or should be tested with it)
Thanks to assert()
statements it documents its own expectations.
Wrap it in a function and give it a useful name and a short description for what it is good for. You can - but must not - safe-guard the assertions. I normally do that inside the Phpunit test-suite so that the expected runtime-configuration is not getting lost.
Here I come to an end which hopefully is another beginning for you.
This is only one way how you could approach such hard problems, and these problems are really hard to test because it's full of side-effects here. Not only would error_get_last()
pose a problem for a straight forward handling.
Yes, error handling is hard, often forgotten or attacked with a "burn-it-with-fire" attiture. At the end of the day this is not helpful when you actually need to deal with it.
And by the definition of it, writing a test for that, is.
The full example incl. simulation of an error exception throwing error handler and some of the earlier flips on 3v4l.org: https://3v4l.org/8fopq
It should go without saying that turning all errors into exceptions is some of the stupidest idea some developers have fallen for. It is just another example that error handling is hard.
Errors and exceptions are a major source of complexity and bugs