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()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

これは、すべての関数呼び出しがカウントされる場合の PHPunit のバグです。バージョン 3.7 でバグが修正されました

于 2013-10-22T10:57:11.283 に答える
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 に答える