jeudi 30 mars 2017

is laravel cloning my mock object?

Im testing a Soap web service with getMockFromWsdl from phpunit, for unit testing within laravel works fine, but when I try to replace the SoapClient in a feature test, it always fails, like the web service never called, but actually the mock is called.

I suspect that laravel is cloning somewhere my $this->soapClient because if I debug the code, it calls the soap mock and gets what is faked in the mock but always receive the error:

Expectation failed for method name is equal to <string:GetToken> when invoked at least once.
Expected invocation at least once but it never occurred.

My code is like:

public function test_soap_call()
{
    $this->soapClient = $this->getMockFromWsdl(dirname(__FILE__).'/../Mocks/service.wsdl');

    $this->soapClient->expects($this->atLeastOnce())
        ->method('GetToken')
        ->with(['Code' => '03600', 'User' => 'username'])
        ->willReturn(unserialize('O:8:"stdClass":1:{s:26:"GetTokenResult";s:36:"5aae60ec-2bcd-459d-a135-a20eb7c10007";}'));

    $this->app->instance('MySoapClient', $this->soapClient);

    $this->postJson('/api/order', $this->getValidRequest());

}

and in my controller (/api/order) I have

$soap = $this->app->make('MySoapClient');
$soap->GetToken(['Code' => '03600', 'User' => 'username']);

Am I using correctly the Laravel Service Container?

PD: Something similar happened to me, when doing a Spy and using $app->instance, where I was trying to get what was passed to an object, but always got null. I solved it declaring the field of the spy static.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire