Undefined method on mock object implementing a given interface in PHPUnit?

前端 未结 3 2103
夕颜
夕颜 2021-02-07 04:34

I\'m new to unit testing and PHPUnit.

I need a mock, on which I have a full control, implementing ConfigurationInterface interface. Test subject is Re

3条回答
  •  北海茫月
    2021-02-07 05:14

    It's because there is no declaration of "getClass" method in ConfigurationInterface. The only declaration in this interface is method "getAliasName".

    All you need is to tell the mock what methods you will be stubing:

    $cls = 'Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface';
    $mock = $this->getMock($cls, array('getClass', 'getAliasName'));
    

    Notice that there is no "getClass" declaration but you can stub/mock non existing method as well. Therefor you can mock it:

    $mock->expects($this->any())
        ->method('getClass')
        ->will($this->returnValue('Some\Other\Class'));
    

    But in addtion you need to mock "getAliasName" method as well as long as it's interface's method or abstract one and it has to be "implemented". Eg.:

    $mock->expects($this->any())
       ->method('getAliasName')
       ->will($this->returnValue('SomeValue'));
    

提交回复
热议问题