PHPUnit - creating Mock objects to act as stubs for properties

前端 未结 2 1482
谎友^
谎友^ 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:26

    This should work:

    class Test extends \PHPUnit_Framework_TestCase {
    ...
        function testSomething() {
             $mockObject = $this->getMock("OrigionalObject");
    
             $mockObject
                  ->expects( $this->any() )
                  ->method('__get')
                  ->will( $this->returnCallback('myMockGetter'));
             ...
         }
    ...
    }
    
    function myMockGetter( $classPropertyName ) {
        switch( $classPropertyName ) {
            case 'ParameterA':
                return 'ValueA';
    
            case 'ParameterB':
                return 'ValueB';
        }
    }
    ... ... 
    

提交回复
热议问题