Steve E. Steve E. - 24 days ago 6
PHP Question

PHPUnit Assert One of two possible outcomes

I am writing unit tests to validate the functionality of two libraries is the same. Essentially, testing an interface.

However, in some tests related to error handling. The error level is different and it has to be different because for example, one generates E_WARNING and the other can only generate E_USER_WARNING.

So the question is. Is there an assert in PHP unit which can say, the error must be one of two possible results? Something like:

assertIsIn(array(E_WARNING, E_USER_WARNING), $generatedError);


I know I could probably work around it by swapping the expected and actual answers over in assertContains() or possibly some pre-assert manipulation of results. However is there a cleaner approach?

Answer

Probably you can implements with the assertContains method (Asserts that a haystack contains a needle). As Example:

public function testAssertIsIn()
{
    $errorLevel = array(E_WARNING, E_USER_WARNING);

    $generatedError = E_WARNING;
    $this->assertContains($generatedError, $errorLevel);

    $generatedError = E_USER_WARNING;
    $this->assertContains($generatedError, $errorLevel);
}

Hope this help