canaltp/abstract-guzzle

Allows to use multiple versions of Guzzle in a same project.

1.1.2 2016-12-05 09:14 UTC

This package is not auto-updated.

Last update: 2020-11-27 22:18:23 UTC


README

Build Status Scrutinizer Code Quality Latest Stable Version License

Allows to use multiple versions of Guzzle in a same project.

It uses an abstract Guzzle client which handle PSR-7 Request and Response objects, so whatever your Guzzle version, you can send Request and handle Response the same way.

Composer

Install via composer:

{
    "require": {
        "canaltp/abstract-guzzle": "~1.0.0"
    }
}

Usage

use GuzzleHttp\Psr7\Request;
use CanalTP\AbstractGuzzle\GuzzleFactory;

$baseUri = 'http://api.my-app.com/v1/';

// Instanciate an abstract Guzzle client
$guzzle = GuzzleFactory::createGuzzle($baseUri);

// Create a PSR-7 Request
$request = new Request('PATCH', 'users/4', ['Content-Type' => 'application/json'], '{"username":"new_username"}');

// Send your request
$response = $guzzle->send($request);

// Get content of your PSR-7 Response
$result = $response->getBody();

Or use shortcut methods:

use CanalTP\AbstractGuzzle\GuzzleFactory;

$baseUri = 'http://api.my-app.com/v1/';

// Instanciate an abstract Guzzle client
$guzzle = GuzzleFactory::createGuzzle($baseUri);

$response = $guzzle->patch('users/4', ['Content-Type' => 'application/json'], '{"username":"new_username"}');
$result = $response->getBody();

Mocking guzzle client

use CanalTP\AbstractGuzzle\GuzzleFactory;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;

// use factory to easily create client mock and add the response you expect
$clientMock = GuzzleFactory::createClientMock([
    new Response(200, ['content-type' => 'application/json', 'content-length' => 26, 'canaltp' => 42]),
    new Response(200, [], '{"lines":"expected-lines"}'),
    new Response(404, ['Content-Length' => 0])
]);

// here some examples of assert
$firstCall = $clientMock->send(new Request('get', 'github'));
$firstCallHeaders = $firstCall->getHeaders();
$this->assertInstanceOf('GuzzleHttp\\Psr7\\Response', $firstCall);
$this->assertEquals(200, $firstCall->getStatusCode());
$this->assertEquals(42, $firstCallHeaders['canaltp'][0]);
$this->assertEquals('application/json', $firstCallHeaders['content-type'][0]);

Supported Guzzle versions

  • Guzzle 3
  • Guzzle 5
  • Guzzle 6

Testing

Running tests:

vendor\bin\phpunit -c .

Check coding style:

vendor\bin\phpcs --standard=PSR2 src

License

This project is under MIT License.