Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test for an exact Exception message, rather than a substring, with PHPUnit?

According to the PHPUnit Documentation on @expectedExceptionMessage, the string must only be a substring of the actual Exception thrown.

In one of my validation methods, an array item is pushed for each error that occurs, and the final Exception message is displayed by imploding the array of errors.

class MyClass
{
    public function validate($a, $b, $c, $d)
    {
        if($a < $b) $errors[] = "a < b.";
        if($b < $c) $errors[] = "b < c.";
        if($c < $d) $errors[] = "c < d.";

        if(count($errors) > 0) throw new \Exception(trim(implode(" ", $errors)));
    }
}

The problem I have here is that in the PHPUnit test method I check for different combinations. This causes tests to pass that I intend to fail.

/**
 * @expectedException \Exception
 * @expectedExceptionMessage a < b.
 */
public function testValues_ALessBOnly()
{
    $myClass = new MyClass()
    $myClass->validate(1, 2, 4, 3);
}

The string of the Exception message is actually "a < b. b < c." but this test still passes. I intend for this test to fail because the message is not exactly what I expect.

Is there a way with PHPUnit to expect an exact string, rather than a substring? I hope to avoid the following:

public function testValues_ALessBOnly()
{
    $myClass = new MyClass()
    $fail = FALSE;

    try
    {
        $myClass->validate(1, 2, 4, 3);
    }
    catch(\Exception $e)
    {
        $fail = TRUE;
        $this->assertEquals($e->getMessage(), "a < b.";
    }

    if(!$fail) $this->fail("No Exceptions were thrown.");
}
like image 624
Bryson Avatar asked Sep 06 '13 00:09

Bryson


People also ask

What is assertion in PHPUnit?

The assertEquals() function is a builtin function in PHPUnit and is used to assert whether the actual obtained value is equals to expected value or not. This assertion will return true in the case if the expected value is the same as the actual value else returns false.

What is PHPUnit testing?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit design for unit testing systems that began with SUnit and became popular with JUnit. Even a small software development project usually takes hours of hard work.

Why do we use PHPUnit?

With unit testing we test each component of the code individually. All components can be tested at least once. A major advantage of this approach is that it becomes easier to detect bugs early on. The small scope means it is easier to track down the cause of bugs when they occur.


2 Answers

You can use $this->expectExceptionMessage() to handle this.

Example:

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Some error message');
like image 170
Volod Avatar answered Oct 22 '22 01:10

Volod


When this question was posted, PHPUnit v3.7 didn't have a solution to this problem. Newer versions have a new @expectedExceptionMessageRegExp option that you can use to add a regular expression to match the exception message against.

Your case, using ^ and $ to force the string to be exactly what is expected, could look like this:

/**
 * @expectedException \Exception
 * @expectedExceptionMessageRegExp /^a < b\.$/
 */
public function testValues_ALessBOnly()
{
    $myClass = new MyClass()
    $myClass->validate(1, 2, 4, 3);
}
like image 20
cambraca Avatar answered Oct 21 '22 23:10

cambraca