du2229 2013-09-26 20:26
浏览 15

PHPUnit存根 - 使预期调用独立于调用顺序

I have the following unit test code:

$map = [
   'key1' => 'A',
   'key2' => 'B'
]; 

$stub = $this->getMockBuilder('Phalcon\Session\Bag')
    ->setConstructorArgs([$sessionNamespace])
    ->getMock();

$stub->expects($this->at(0)) // Always called first by: new Phalcon\Session\Bag()
    ->method('setDI')
    ->will($this->returnCallback(function($di) {
        $this->di = $di;
}));

$stub->expects($this->at(1)) // First, we're checking if session key is set
    ->method('__isset')
    ->will($this->returnCallback(function($sessionKey) {
        // Yes, always set
        return true;
    }));

$stub->expects($this->at(2)) // Then, we're fetching it
    ->method('__get')
    ->will($this->returnValueMap($map));

I'm trying to return mapped values whenever __isset($key) or __get($key) are invoked on Phalcon\Session\Bag.

First invocation, everything works as expected:

$bag = new Phalcon\Session\Bag('someNamespace');
$var1 = (isset($bag->key1)) ? $bag->key1 : null; // $val1 is 'A'

However, all subsequent calls return NULL:

$var2 = (isset($bag->key2)) ? $bag->key2 : null; // $val1 is NULL
$var3 = (isset($bag->key1)) ? $bag->key1 : null; // $val1 is NULL

Obviously, the problem is with ->at(0|1), those are the position indices.

I have tried replacing ->at() with ->any(), but without luck - I was getting PHPUnit assertion error "Failed asserting that two strings are equal." - expected "__isset" does not match actual "setDI".

Question:

How can I tell expected methods of a stub to work irrespective of invocation order?

Thanks, Temuri

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)