dshafik/guzzlehttp-vcr 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dshafik/guzzlehttp-vcr

Composer 安装命令:

composer require dshafik/guzzlehttp-vcr

包简介

README 文档

README

LicenseTravis CI Status Scrutinizer Code Quality

Guzzle VCR

Based on the idea of PHP•VCR, this Guzzle Middleware will record responses and replay them in response to subsequent requests.

This middleware is simplistic in that it will simply replay the responses in order in response to any requests. This is handy for testing clients that have time-based authentication and need to generate dynamic requests but still want predictable responses to test response handling.

Installation

To add to your project, use composer:

$ composer require dshafik/guzzlehttp-vcr

Usage

It's use is similar to Guzzles \GuzzleHttp\Handler\MockHandler, and in fact uses the MockHandler to replay the recorded requests. Calling the Dshafik\GuzzleHttp\VcrHandler::turnOn() method will return either an instance of the standard GuzzleHttp\HandlerStack with either the VcrHandler or MockHandler (with the requests loaded) added as middleware.

You then pass the handler in as the GuzzleHttp\Client handler option, either in the constructor, or with the individual request.

The recording is halted on script termination, or the next time VcrHandler::turnOn() is called for that recording.

<?php
class ApiClientTest {
    public function testSomething() {
        $vcr = \Dshafik\GuzzleHttp\VcrHandler::turnOn(__DIR__ . '/fixtures/somethingtest.json');
        $client = new \GuzzleHttp\Client(['handler' => $vcr]);

        $client->get('/test');
    }
}
?>

In this example, if the fixture exists, it will be used — using MockHandler — in response to any requests made until it runs out of possible responses. Once it runs out of responses it will throw an \OutOfBoundsException exception on the next request.

To update the fixture, just delete the file and re-run the test.

Fixtures

Fixtures are simple JSON files that you can edit or create by hand:

[
    {
        "body": "Hello World",
        "headers": {
            "Connection": [
                "keep-alive"
            ],
            "Date": [
                "Fri, 21 Aug 2015 01:10:34 GMT"
            ],
            "Transfer-Encoding": [
                "chunked"
            ],
            "X-VCR-Recording": [
                "1440119434"
            ]
        },
        "reason": "OK",
        "status": 200,
        "version": "1.1"
    }
]

The only difference between the recording and the original response is the addition of an X-VCR-Recording header that contains the UNIX timestamp of the time it was recorded.

Running the Tests

The unit tests for this library use Guzzles built-in Node.js server, this means that you must install with the --prefer-source flag, otherwise test sources are not included.

To run the unit tests simply run phpunit in the root of the repository:

 $ phpunit
 PHPUnit 4.8.5 by Sebastian Bergmann and contributors.
 
 Runtime:	PHP 5.6.10 with Xdebug 2.3.3
 Configuration:	/Users/dshafik/src/guzzlehttp-vcr/phpunit.xml.dist
 
 ....
 
 Time: 2.94 seconds, Memory: 9.00Mb
 
 OK (5 tests, 62 assertions)

统计信息

  • 总下载量: 49k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 63
  • 点击次数: 1
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 62
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-08-21