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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With