2

我有以下抽象类:

abstract class Voter
{
    public function vote()
    {
        $this->something();
        $this->something();

        $this->voteFoo();
        $this->voteBar();
    }

    public function __call($method, $args)
    {
        //...
    }

    abstract public function something();
}

voteFoo并由voteBar处理__call

我想断言同时vote()调用voteFoo()and voteBar(),我该怎么做?

我尝试使用$this->at(..),但它给了我一个错误:

$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.

编辑

如果我将值更改$this->at()23,则测试通过,这意味着由于某种原因$this->something()也会触发该__call方法。

这个Voter类不是我真正的Voter类,它是问题的简单版本。__call在我真正的课堂上,我不知道会被调用多少次..

4

2 回答 2

0

docs中并不完全清楚,但是您应该可以使用returnValueMap它:

$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));
于 2012-11-29T20:15:05.433 回答
0

当计算所有函数调用时,这是 PHPunit 错误。3.7版本修复了Bug

于 2013-10-22T10:57:11.283 回答