Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 550
Steve E. Avatar asked Nov 10 '16 23:11

Steve E.


People also ask

What is the purpose of PHPUnit?

PHPUnit is a framework independent library for unit testing PHP. Unit testing is a method by which small units of code are tested against expected results. Traditional testing tests an app as a whole meaning that individual components rarely get tested alone.

What is assert in PHPUnit?

PHPUnit assertSame() Function The assertSame() function is a builtin function in PHPUnit and is used to assert whether the actually obtained value is the same as the 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.

What are PHPUnit fixtures?

One of the most time-consuming parts of writing tests is writing the code to set the world up in a known state and then return it to its original state when the test is complete. This known state is called the fixture of the test.


1 Answers

Probably you can implement 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

like image 157
Matteo Avatar answered Oct 14 '22 08:10

Matteo