是laravel克隆我的模拟对象?

时间:2017-03-30 14:46:14

标签: laravel unit-testing testing laravel-5 phpunit

我正在使用来自phpunit的getMockFromWsdl测试Soap Web服务,以便在laravel中进行单元测试工作正常,但是当我尝试在功能测试中替换SoapClient时,它总是会失败,就像Web服务从未调用过一样,但实际上模拟被称为。

我怀疑laravel正在我的$this->soapClient克隆,因为如果我调试代码,它会调用soap mock并获取模拟中的伪造但总是收到错误:

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

我的代码如下:

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());

}

在我的控制器(/api/order)中

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

我是否正确使用了Laravel服务容器?

PD:在做间谍和使用$app->instance时,我发生了类似的事情,我试图获取传递给对象的内容,但总是得到null。我解决了它宣告间谍静态的领域。

1 个答案:

答案 0 :(得分:3)

从php.net检查: http://php.net/manual/en/language.oop5.references.php

...&#34;对象默认通过引用传递&#34;。这不完全正确。 ...

相关问题