mercredi 17 mai 2017

PHPunit mockobject with interface

I have a model called Service that creates an object from a configInterface in the constructor.

public $config;

public function __construct(   \Vendor\Module\Api\Config $config){
    $this->config = $config;
}

And use the following method

public function Foo(){
    $bar = $this->config->Bar();   
    return 'Config bar = '.$bar;
}

In my Unittest setUp() I create a mockobject like this for my service

 $this->serviceMock = $this->getMock(
        'Vendor\Module\Model\Service',
        ['create'],
        [''],
        '',
        true
    );

This will give me the message that __construct() must be an instance of Vendor\Module\Api\Config

Any idea how I can add the config interface in my service mockobject? I tried to create a new object mockobject for my config interface and pass this in my service object but this will return $bar = $this->config->Bar(); null with the my testcase passing.

Aucun commentaire:

Enregistrer un commentaire