PHPUnit: how do I mock multiple method calls with multiple arguments?

后端 未结 4 2166

I am writing a unit test for a method using PHPUnit. The method I am testing makes a call to the same method on the same object 3 times but with different sets of arguments.

4条回答
  •  执念已碎
    2020-12-12 22:23

    Stubbing a method call to return the value from a map

    $map = array(
        array('arg1_1', 'arg2_1', 'arg3_1', 'return_1'),
        array('arg1_2', 'arg2_2', 'arg3_2', 'return_2'),
        array('arg1_3', 'arg2_3', 'arg3_3', 'return_3'),
    );
    $mock->expects($this->exactly(3))
        ->method('MyMockedMethod')
        ->will($this->returnValueMap($map));
    

    Or you can use

    $mock->expects($this->exactly(3))
        ->method('MyMockedMethod')
        ->will($this->onConsecutiveCalls('return_1', 'return_2', 'return_3'));
    

    if you don't need to specify input arguments

提交回复
热议问题