PHPUnit and mock request from Guzzle PHPUnit and mock request from Guzzle laravel laravel

PHPUnit and mock request from Guzzle


I think as suggested is better to use http://docs.guzzlephp.org/en/stable/testing.html#mock-handler

as it looks like the most elegant way to do it properly.

Thank you all


The mocked Response doesn't need to be anything in particular, your code just expects it to be an object with a getBody method. So you can just use a stdClass, with a getBody method which returns some json_encoded object. Something like:

$jsonObject = json_encode(['foo']);$uri = 'path/to/foo/bar/';$mockResponse = $this->getMockBuilder(\stdClass::class)->getMock();$mockResponse->method('getBody')->willReturn($jsonObject);$clientMock = $this->getMockBuilder('GuzzleHttp\Client')->getMock();$clientMock->expects($this->once())    ->method('request')    ->with(        'GET',         $uri,        $this->anything()    )    ->willReturn($mockResponse);$result = $yourClass->get($uri);$expected = json_decode($jsonObject);$this->assertSame($expected, $result);


I prefer this way to mock a Client in PHP. In this example I am using Guzzle Client.

Clone the code or install it via composer

$ composer require doppiogancio/mocked-client

And then...

$builder = new HandlerStackBuilder();// Add a route with a response via callback$builder->addRoute(    'GET', '/country/IT', static function (ServerRequestInterface $request): Response {        return new Response(200, [], '{"id":"+39","code":"IT","name":"Italy"}');    });// Add a route with a response in a text file$builder->addRouteWithFile('GET',  '/country/IT/json', __DIR__ . '/fixtures/country.json');// Add a route with a response in a string$builder->addRouteWithFile('GET',  '{"id":"+39","code":"IT","name":"Italy"}');// Add a route mocking directly the response$builder->addRouteWithResponse('GET', '/admin/dashboard', new Response(401));$client = new Client(['handler' => $builder->build()]);

Once you have mocked the client you can use it like this:

$response = $client->request('GET', '/country/DE/json');$body = (string) $response->getBody();$country = json_decode($body, true);print_r($country);// will returnArray(    [id] => +49    [code] => DE    [name] => Germany)