canaltp/abstract-guzzle

允许在同一个项目中使用多个版本的Guzzle。

该包的官方仓库似乎已消失,因此该包已被冻结。

1.1.2 2016-12-05 09:14 UTC

This package is not auto-updated.

Last update: 2023-05-13 11:50:43 UTC


README

Build Status Scrutinizer Code Quality Latest Stable Version License

允许在同一个项目中使用多个版本的Guzzle

它使用一个抽象的Guzzle客户端来处理PSR-7 请求和响应对象,因此无论你的Guzzle版本是什么,你都可以以相同的方式发送请求和处理响应。

Composer

通过composer安装

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

用法

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();

或者使用快捷方法

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();

模拟Guzzle客户端

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]);

支持的Guzzle版本

  • Guzzle 3
  • Guzzle 5
  • Guzzle 6

测试

运行测试

vendor\bin\phpunit -c .

检查代码风格

vendor\bin\phpcs --standard=PSR2 src

许可证

本项目采用MIT许可证