-
Notifications
You must be signed in to change notification settings - Fork 3
/
GuzzleTestCase.php
49 lines (41 loc) · 1.56 KB
/
GuzzleTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
namespace Twirfony;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Promise\FulfilledPromise;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;
use Symfony\Component\HttpFoundation\HeaderBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
// Allows for easy testing of api clients that use guzzle.
class GuzzleTestCase extends KernelTestCase
{
public static function getClient(string $baseurl): Client
{
$stack = new HandlerStack();
$stack->setHandler(function(RequestInterface $psrRequest, $options) {
$kernel = static::bootKernel();
$symfonyRequest = Request::create(
$psrRequest->getUri(),
$psrRequest->getMethod(),
[], [], [], [],
$psrRequest->getBody()->getContents()
);
$symfonyRequest->headers = new HeaderBag($psrRequest->getHeaders());
$symfonyResponse = $kernel->handle($symfonyRequest, HttpKernelInterface::MASTER_REQUEST, false);
$guzzleResponse = new Response(
$symfonyResponse->getStatusCode(),
$symfonyResponse->headers->all(),
$symfonyResponse->getContent(),
$symfonyResponse->getProtocolVersion()
);
return new FulfilledPromise($guzzleResponse);
});
return new Client([
'handler' => $stack,
'base_uri' => $baseurl,
]);
}
}