vendredi 3 juillet 2015

Mock class in phpunit

I'm finally getting into PHP unit testing (yeah better late than never!)

I have a Utility class that does a few things including authenticating a user's access against a webservice. The Utility::authenticate method takes a Settings object, which contains the username etc. that are checked by authenticate.

So in my test, I mock a Settings object like this:

$settings = $this->getMock('Settings', array('getSettings'));

$settings->method('getSettings')
    ->willReturn([
        'USERNAME' => 'testuser',
        'SERVER' => 'testserver'
    ]);

$mock = $settings->getSettings();

So far so good, but when I try to pass that mocked object to authenticate it throws an error:

$this->assertEquals($testvalue, Utilities::authenticate($settings));
Argument 1 passed to Utilities::authenticate() must be an instance of Settings, instance of Mock_Settings_d0361624 given

How can I mock the Settings object so it appears to be a "real" object to the Utilities class?

Aucun commentaire:

Enregistrer un commentaire