PHPUnit assert mock method with multiple stringContains for one parameter

php, phpunit, unit-testing

Solution

I know this answer is quite late, but I had the same problem and found a solution that seems to be a little bit smarter... :)

Try this: (untested for your case)

$this->getMock()
     ->expects($this->any())
     ->method('setString')
     ->with($this->logicalAnd(
          $this->stringContains('word1'),
          $this->stringContains('word3')
      ))
     ->will($this->returnSelf());

Problem

Is there a way to assert mocked class method string parameter for multiple matches? ``` $this->getMock() ->expects($this->any()) ->method('setString') ->with($this->stringContains('word3')) ->will($this->returnSelf()); ``` This example will pass for ->setString('word1 word2 word3 word4') What I need to do is match if setString() is called with parameter containing 'word1' AND 'word3' But ``` $this->getMock() ->expects($this->any()) ->method('setString') ->with( $this->stringContains('word1'), $this->stringContains('word3') ) ->will($this->returnSelf()); ``` this implementation is checking for 2 parameters for setString() and this is not what I have intended to check. Ideas? using $this->callback() ? Is there some better suited PHPUnit assertions for this?

Original source