Is it possible to configure PHPUnit mock in this way?
$context = $this->getMockBuilder(\'Context\')
->getMock();
$context->expects($this->any
Following on from the answer of @edorian and the comments (@MarijnHuizendveld) regarding ensuring that the method is called with both Matcher and Logger, and not simply twice with either Matcher or Logger, here is an example.
$expectedArguments = array('Matcher', 'Logger');
$context->expects($this->exactly(2))
->method('offsetGet')
->with($this->logicalOr(
$this->equalTo('Matcher'),
$this->equalTo('Logger')
))
->will($this->returnCallback(
function($param) use (&$expectedArguments){
if(($key = array_search($param, $expectedArguments)) !== false) {
// remove called argument from list
unset($expectedArguments[$key]);
}
// The first arg will be Matcher or Logger
// so something like "return new $param" should work here
}
));
// perform actions...
// check all arguments removed
$this->assertEquals(array(), $expectedArguments, 'Method offsetGet not called with all required arguments');
This is with PHPUnit 3.7.
If the method you are testing doesn't actually return anything, and you simply need to test that it is called with the correct arguments, the same approach applies. For this scenario, I also attempted doing this using a callback function for $this->callback as the argument to the with, rather than returnCallback in the will. This fails, as internally phpunit calls the callback twice in the process of verifying the argument matcher callback. This means that the approach fails as on the second call that argument has already been removed from the expected arguments array. I don't know why phpunit calls it twice (seems an unnecessary waste), and I guess you could work around that by only removing it on the second call, but I wasn't confident enough that this is intended and consistent phpunit behaviour to rely on that occurring.