I have the following abstract class:
abstract class Voter
{
public function vote()
{
$this->something();
$this->something();
$this->voteFoo();
$this->voteBar();
}
public function __call($method, $args)
{
//...
}
abstract public function something();
}
voteFoo
and voteBar
are handled by __call
.
I want to assert that vote()
calls both voteFoo()
and voteBar()
, how can I do that?
I tried using $this->at(..)
but it gives me an error:
$voter = $this->getMockForAbstractClass('Voter', array(), '', true, true, true, array('__call'));
$voter->expects($this->any())
->method('something')
->will($this->returnValue(true));
$voter->expects($this->at(0))
->method('__call')
->with($this->equalTo('voteFoo'));
$voter->expects($this->at(1))
->method('__call')
->with($this->equalTo('voteBar'));
$voter->vote();
*********** ERROR *************
Expectation failed for method name is equal to <string:__call> when invoked at
sequence index 0.
Mocked method does not exist.
Edit:
If I change the values in $this->at()
to 2 and 3 the test passes, which means that for some reason $this->something()
also triggers the __call
method.
This Voter class is not my real Voter class, it's a simple version for the question. In my real class I can't tell how many times __call
will be called..
It's not entirely clear from the docs , but you should be able to use returnValueMap
for this:
$voter->expects($this->any())
->method('something')
->will($this->returnValue(true));
$argumentMap = array(
array('voteFoo'),
array('voteBar')
);
$voter->expects($this->exactly(2))
->method('__call')
->will($this->returnValueMap($argumentMap));