1

So the question is the same as in subject: How to simulate and properly write test with error_get_last() function in phpunit

Sample code to write test to:

    $lasterror = error_get_last();
    if ($lasterror !== null) {
        //do something
    }
Marek G.
  • 99
  • 9
  • Are you interested in creating an error so that `error_get_last()` returns it? Perhaps then [`trigger_error()`](https://php.net/trigger_error). but it's not entirely clear to me about which part you're concerned. Maybe adding the test code that shows what you would like to do - then this maybe would be more clear to me. - Probably also this? https://stackoverflow.com/q/1225776/367456 or this https://stackoverflow.com/q/8412746/367456 – hakre Jun 09 '21 at 21:27
  • @hakre The first sentence od your comment is exactly what I want to achieve. – Marek G. Jun 10 '21 at 14:59
  • The I guess trigger_error() is the answer, does it work for you? I can leave it as an answer then ... – hakre Jun 10 '21 at 15:30
  • Not exactly. While using `trigger_error('Oh no')` a get: `Time: 00:00.013, Memory: 6.00 MB There was 1 error: 1) XxxTest::testCriticalErrors ErrorException: Oh no ` But solution is probably i code of test in https://github.com/symfony/symfony/blob/5.4/src/Symfony/Component/ErrorHandler/Tests/ErrorHandlerTest.php#L228 of class https://github.com/symfony/symfony/blob/5.4/src/Symfony/Component/ErrorHandler/ErrorHandler.php#L122 I think when I study this parts of code I will find the solution. – Marek G. Jun 10 '21 at 19:07
  • yes in phpunit by (some) default, errors make the test fail, right. try with `@trigger_error()`, the error suppression in front. normally phpunit let these pass so you can do the assertions. sorry, it's so common for me nowadays I forgot to mention. – hakre Jun 11 '21 at 01:38
  • I will try, but when I did `@$undefVar;` produced error and result was as in above. – Marek G. Jun 11 '21 at 12:41
  • Hmm, that is somehow surprising, maybe there is some specific configuration that turns any of your errors even suppressed ones into ErrorExceptions? [A quick search through phpunit](https://github.com/sebastianbergmann/phpunit/search?q=ErrorException) suggests its not by it. – hakre Jun 11 '21 at 13:15

2 Answers2

1

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

hakre
  • 193,403
  • 52
  • 435
  • 836
1

If you want get know something talk with the another human.

Thank you a lot @hakre!

And compressed above answer:

protected function generateSafelySimulatedError(\Closure $errorGeneratingFn, string $generatedErrorMsg = null): void
{
    try {
        set_error_handler(null);
        set_exception_handler(null);
        $errorGeneratingFn();
        restore_error_handler();
        restore_exception_handler();
        try {
            $errorGeneratingFn();
        } catch (\ErrorException $good) {
            if ($generatedErrorMsg !== null) {
                $this->assertTrue($generatedErrorMsg === $good->getMessage(), get_class($good));
            }
        } finally {
            $this->assertTrue(isset($good), 'An expected exception was thrown.');
        }
    } catch (\ErrorException $fail) {
        $this->assertTrue(false, sprintf('The "%s" exception must not be thrown.', get_class($fail)));
    } finally {
        $this->assertTrue(!isset($fail), 'There must not have been any exception.');
    }
}

And sample usage:

public function testCriticalErrors(): void
{
    $testObject = $this->getObjectUnderTest();//Here is creation of object and also here is set_error_handler() and other similar.

    $this->generateSafelySimulatedError(function (): void {
        @trigger_error('Oh no');
    }, 'Oh no');

    $testObject->criticalErrors();//tested method like:
    /*
    $lasterror = error_get_last();
    if ($lasterror !== null) {
        //do something
    }
    */
    //Below other assertions if needed.
    $this->expectOutputString('<html class="error"><body><h1>500</h1>Error 500</body></html>');
}
Marek G.
  • 99
  • 9
  • That looks like a good fit for Phpunit. A way to extend on it in Phpunit is to make it a specific TestCase (abstract class extending from PhpUnitTestCase) and work with setUp / and tearDown. I didn't want to put this in my answer first as it might have been too verbose (and as written and usual there are multiple ways). --- For the sample usage, I would put the output expectation to the very beginning of the test method so that it is visible upfront what the expectations are. Just FYI. – hakre Jun 13 '21 at 07:50
  • Just a remark (also applies to my answer) while stumbling over in a another question regarding phpunit and error handling: the [error suppression operator `@`](https://www.php.net/manual/en/language.operators.errorcontrol.php) has less side-effects since PHP 8 (which is good), but might not be "fully compatible" with phpunit's error handler. As we remove the error handler, the answers should (continue to) work with PHP 8, but depending on test scenario, there maybe subtle differences. [ref](https://stackoverflow.com/q/68226825/367456) – hakre Jul 03 '21 at 08:29