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.");
}