Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing classes - is throwing errors out of the question?

I am getting my head around PHPUnit, and trying to build a test case for an existing class.

The class is a static configuration class, getting, setting and listing configuration options that will be available in the application.

The class is very strictly built. If I try to set a configuration setting with an incompatible value, or a configuration setting that does not exist, a E_USER_ERROR is thrown, halting the execution of the script. Even if it's not the fine art of error handling, it works fine for the purposes of this class. An error in that class is always the result of a programming error, and never of bad user input.

This has the great advantage that you don't have to worry about how error messages are handled or logged, which keeps the code slim. Output the message (or not, in production), die(), done.

However, with Unit tests, I don't see how I can continue to work with classic PHP errors. I will have to convert the functions to return success flags, or throw exceptions.

Am I correct?

Or is there a way in PHPUnit to expect errors, as there is to expect Exceptions? I can't see any straight away.

like image 979
Pekka Avatar asked Mar 22 '26 04:03

Pekka


1 Answers

From the PHPUnit manual:

By default, PHPUnit converts PHP errors, warnings, and notices that are triggered during the execution of a test to an exception. Using these exceptions, you can, for instance, expect a test to trigger a PHP error as shown in Example 4.8.

class ExpectedErrorTest extends PHPUnit_Framework_TestCase
{
    /**
     * @expectedException PHPUnit_Framework_Error
     */
    public function testFailingInclude()
    {
        include 'not_existing_file.php';
    }
}

Same works for exceptions of course.

Edit: didn't see this was already mentioned in the comments, but I'll leave it here for reference in case someone looks for the same question and doesn't read the comments

like image 183
Gordon Avatar answered Mar 24 '26 16:03

Gordon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!