How can I get PHPUnit MockObjects to return different values based on a parameter?

前端 未结 11 1622
忘掉有多难
忘掉有多难 2020-12-04 05:43

I\'ve got a PHPUnit mock object that returns \'return value\' no matter what its arguments:

// From inside a test...
$mock = $this->getMock(\         


        
11条回答
  •  执念已碎
    2020-12-04 06:23

    From the latest phpUnit docs: "Sometimes a stubbed method should return different values depending on a predefined list of arguments. You can use returnValueMap() to create a map that associates arguments with corresponding return values."

    $mock->expects($this->any())
        ->method('getConfigValue')
        ->will(
            $this->returnValueMap(
                array(
                    array('firstparam', 'secondparam', 'retval'),
                    array('modes', 'foo', array('Array', 'of', 'modes'))
                )
            )
        );
    

提交回复
热议问题