What is the best way to test for multiple array keys in a PHPUnit mock with() clause?
For example, to test whether a method calls 2nd argument is an array containing a 'foo' key:
$this->stubDispatcher->expects($this->once())
->method('send')
->with('className', $this->arrayHasKey('foo'));
What I’d like to do is something like $this->arrayHasKey('foo', 'bar') while not actually matching the exact contents of the array.
You can pass assertions directly to
->with()but they are named differently.For the list take a look at the source:
https://github.com/sebastianbergmann/phpunit/blob/3.5/PHPUnit/Framework/Assert.php#L2097and following. Everything that doesn’t start with “assert” and returns anew PHPUnit_Framework_*.While i assume @David’s answer works I think this approach, using
logicalAnd()is a little cleaner / easier to read.Working example
Output