0

A bit unclear on the title, but here we go.

I am running a unit test on a method that essentially runs a strpos(). Like this:

return strpos($this->getHeaderLine($headerName), $value) !== false;

Now I also want to see what happens if I supply it an empty value. When provide an empty string, I get the following message:

strpos(): Empty needle

This message in itself is clear enough, I don't want to throw an exception here. I do want to know if it returned this message though. How would I go about doing this or do I need to rewrite my method?

1
  • technically you could use set_error_handler and restore_error_handler. but thinking of design it's not a good choice.
    – xmike
    Commented Dec 14, 2016 at 9:28

1 Answer 1

1

Try the following:

public function testWarning()
{
    $this->expectException(PHPUnit_Framework_Error_Warning::class);
    $this->expectExceptionMessage("Empty needle");
    strpos('meh', '');
}

This test passes, but if you comment out the expectations, it fails. There are more related classes in PHPUnit framework, like PHPUnit_Framework_Error_Deprecated, PHPUnit_Framework_Error_Notice and so on.

Not the answer you're looking for? Browse other questions tagged or ask your own question.