I have the following PHP / PHPUnit code:
<?php
namespace Experiments\Tests;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\RequestOptions;
use PHPUnit\Framework\TestCase;
final class ApiGatewayTest extends TestCase
{
public function testExecute()
{
$req = json_decode('{ "root": { "param1": [ "value1" ] } }', true);
$resp = '{ "echo": { "root": { "param1": [ "value1" ] } } }';
$options = [
RequestOptions::HEADERS => [
'Content-Type' => 'application/json',
],
RequestOptions::JSON => $req,
RequestOptions::TIMEOUT => 10,
];
$handlerStack = HandlerStack::create(
new MockHandler([new Response(200, ['Content-Type' => 'application/json'], $resp)])
);
$client = new HttpClient(['handler' => $handlerStack]);
$response = $client->request(
'POST',
'whatever-just-mocking',
$options
);
$responseBodyJson = (string) $response->getBody();
$responseBody = json_decode($responseBodyJson, true);
$this->assertSame('value1', $responseBody['echo']['root']['param1'][0]);
}
}
The unit test above works just fine. You can test it with:
$ ./vendor/bin/phpunit --filter 'testExecute\b'
What I need: Delay the mock response for 5 seconds, so I can implement and test a timeout handling in the same class above.
Any idea on how to do that?
Thanks!