PHPUnit - creating Mock objects to act as stubs for properties

前端 未结 2 1480
谎友^
谎友^ 2020-12-31 03:30

I\'m trying to configure a Mock object in PHPunit to return values for different properties (that are accessed using the __get function)

Example:

cla         


        
2条回答
  •  隐瞒了意图╮
    2020-12-31 04:23

    I haven't tried mocking __get yet, but maybe this will work:

    // getMock() is deprecated
    // $mockObject = $this->getMock("OrigionalObject");
    $mockObject = $this->createMock("OrigionalObject");
    
    $mockObject->expects($this->at(0))
        ->method('__get')
        ->with($this->equalTo('ParameterA'))
        ->will($this->returnValue('ValueA'));
    
    $mockObject->expects($this->at(1))
        ->method('__get')
        ->with($this->equalTo('ParameterB'))
        ->will($this->returnValue('ValueB'));
    

    I've already used $this->at() in a test and it works (but isn't an optimal solution). I got it from this tread:

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

提交回复
热议问题