Php Laravel模拟多个Guzzle端点
我正在使用Laravel6并尝试测试端点。终点 正在向外部API发出2个请求(来自mollie)。目前我 像这样嘲笑它:Php Laravel模拟多个Guzzle端点,php,laravel,testing,guzzle,laravel-testing,Php,Laravel,Testing,Guzzle,Laravel Testing,我正在使用Laravel6并尝试测试端点。终点 正在向外部API发出2个请求(来自mollie)。目前我 像这样嘲笑它: public function test() { $this->mockApiCall( new Response( 200, [], '{ "response": "here is the response", }'
public function test()
{
$this->mockApiCall(
new Response(
200,
[],
'{
"response": "here is the response",
}'
)
);
Mollie::shouldReceive('api')
->once()
->andReturn(new MollieApiWrapper($this->app['config'], $this->apiClient));
dd(Mollie::api()->customers()->get('238u3n'));
}
抽象BaseMollieEndpointTest
<?php
namespace Tests;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Mollie\Api\MollieApiClient;
abstract class BaseMollieEndpointTest extends TestCase
{
/**
* @var Client|\PHPUnit_Framework_MockObject_MockObject
*/
protected $guzzleClient;
/**
* @var MollieApiClient
*/
protected $apiClient;
protected function mockApiCall(Response $response)
{
$this->guzzleClient = $this->createMock(Client::class);
$this->apiClient = new MollieApiClient($this->guzzleClient);
$this->apiClient->setApiKey('test_dHar4XY7LxsDOtmnkVtjNVWXLSlXsM');
$this->guzzleClient
->expects($this->once())
->method('send')
->with($this->isInstanceOf(Request::class))
->willReturnCallback(function (Request $request) use ($response) {
return $response;
});
}
}
这是有效的。但问题是,当我需要在同一个api调用中模拟另一个请求时,我会得到相同的结果
那么,我如何确保可以模拟2个响应(而不是1个)并将其返回给特定的url呢?看看模拟HTTP调用的方法,以及
回答您的特定问题,对于Guzzler,它可以简单到:
$this->guzzler->expects($this->exactly(2))
->endpoint("/send", "POST")
->willRespond($response)
->willRespond(new Response(409));