PHPUnit's returnValueMap not yielding expected results

后端 未结 3 523
既然无缘
既然无缘 2021-01-30 21:05

I\'m trying to use PHPUnit\'s returnValueMap() to stub out the results of a read. It isn\'t yielding the expected results, but an equivalent returnCallback() does. I\'ve made

3条回答
  •  既然无缘
    2021-01-30 21:34

    I had the same problem and eventually found out that returnValueMap() has to map all parameters of your function, including optional ones, then the desired return value.

    Example function from Zend Framework:

    public function getParam($key, $default = null)
    {
        $key = (string) $key;
        if (isset($this->_params[$key])) {
            return $this->_params[$key];
        }
    
        return $default;
    }
    

    Has to mapped like this:

    $request->expects($this->any())
            ->method('getParam')
            ->will($this->returnValueMap(array(array($param, null, $value))));
    

    Without the null in the middle, it won't work.

提交回复
热议问题