PHPUnit's returnValueMap not yielding expected results

后端 未结 3 525
既然无缘
既然无缘 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:37

    The Problem seems to lie either somewhere else in the code or might be an issue with an old PHPUnit version.

    For me it works using this code:

    cat EnterpriseTest.php

    getMock('Enterprise', array('field'));
            $enterprise->expects($this->any())
                ->method('field')
                ->will($this->returnValueMap(array(
                    array('subscription_id', null),
                    array('name', 'Monday Farms')
                )))
            ;
            $enterprise->subscribe('basic');        
        }
    
    }
    
    class Enterprise {
    
        public function field($name) {
        }
    
        public function subscribe() {
            echo 'Subscription ID: ';
            var_dump($this->field('subscription_id'));
            echo 'Name: ';
            var_dump($this->field('name'));
        }
    
    }
    

    Output:

    phpunit-dev EnterpriseTest.php 
    PHPUnit 3.7.6 by Sebastian Bergmann.
    
    .Subscription ID: NULL
    Name: string(12) "Monday Farms"
    
    
    Time: 0 seconds, Memory: 6.75Mb
    
    OK (1 test, 1 assertion)
    

    So while I can't tell why it doesn't work I can at least tell you that you are doing it right and that you've understood returnValueMap correctly :)

提交回复
热议问题