ikwattro/guzzle-stereo 问题修复 & 功能扩展

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

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

ikwattro/guzzle-stereo

Composer 安装命令:

composer require ikwattro/guzzle-stereo

包简介

Guzzle Recorder for recording Request/Responses and replay them back in a Mock

README 文档

README

Record and Replay HTTP Responses easily.

Build Status

Requirements

  • PHP 5.6+
  • Guzzle 6

Installation

Require the composer package :

composer require ikwattro/guzzle-stereo

NB: If you're using the Symfony Framework, you can take a look at the GuzzleStereoBundle from @estahn.

Basics

Recorder

A recorder is the main object that knows everything about tapes and how to store them afterwards.

Tapes

A tape will record Response objects. It has a name and can have filters. A filter can tell for example that only Responses with a 200 status code will be recorded in that tape.

Defining tapes and filters is done through a yaml file definition :

tapes:
	my_tape:
		filters:
			status_code: 200

This tape will record only success responses.

Filters

A filter is a rule telling if the Response object should be included or not. There is a set of built-in filters available with the library or you can create your own and register them.

NB: If you feel that your filter can be generic enough, do not hesitate to open a PullRequest

Store directory

In order to be replayed later (see the Replay section below), all tapes will then be dumped in json files to disk. You need to provide a writable directory.

Player

A player is able to replay dumped json files as Response objects. A nice use case is to replay them with a Mock Handler in your test suites.

Usage

Recording

Instantiate the recorder by providing a writable store directory and the location of your tapes definitions file :

require_once(__DIR__.'/vendor/autoload.php');

use Ikwattro\GuzzleStereo\Recorder;

$recorder = new Recorder(__DIR__.'/var/records', __DIR__.'/stereo.yml');

Next, when creating your Guzzle client, you need to make him aware of the recorder. An easy way to do this is by using a Middleware available with the library :

$stack = \GuzzleHttp\HandlerStack::create();
$stack->push(\Ikwattro\GuzzleStereo\RecorderMiddleware::record($recorder));

$client = new \GuzzleHttp\Client(['handler' => $stack]);

You can now make http requests with the Client as you would usually do, for e.g. here we'll call the Github events API 10 times :

for ($i = 0; $i < 10; $i++) {
	try {
		$client->get('https://api.github.com/events');
	} catch (RequestException $e) {
		// Do what you want
	}
}

Finally, you'll need to tell the recorder to dump the tapes to the disk :

$recorder->dump();

A file named record_ + {tape_name} will be created in the provided store directory containing the responses passing the filters :

Replaying

In order to replay the recorded tapes, you can use the Player. The player is in fact creating a Mock Handler and will return you a GuzzleHttp\Client instance created with the MockHandler containing the responses from the tape file.

use Ikwattro\GuzzleStereo\Player;

$player = Player::replayFromTape('/path/to/tape.json');

$player->get('/');
// will return you the first response record present in the tape

Filters reference

StatusCode

Include the Response only if it has the corresponding status code.

tapes:
	my_tape:
		filters:
			status_code: 200

Non Empty Body

Include the Response only if the body is not empty.

tapes:
	my_tape:
		filters:
			non_empty_body: ~

Has Header

Include the Response only if she contains a header with the specified key.

tapes:
	my_tape:
		filters:
			has_header: "Content-Type"

Creating your own filters

Creating a filter is really easy. You need to create a filter class implementing Ikwattro\GuzzleStereo\Filter\FilterInterface and declaring the two methods :

  • public static function getName()
  • public function isIncluded(ResponseInterface $response)

The getName static function is responsible for defining the name of the filters in your tapes.

The isIncluded function will contain the logic determining if the received Response should be included or not in the Tape.

The following example filter will receive a response from the Github events api, and will record it only if the body contains an event done by the Github users you will pass as arguments when associating your filter to a tape.

<?php
namespace Acme\MyApp\Filter;

use Ikwattro\GuzzleStereo\Filter\FilterInterface;
use Psr\Http\Message\ResponseInterface;

class ActorFilter implements FilterInterface
{
	const FILTER_NAME = "actor_filter";
	
	protected $users;
	
	public function__construct(array $users = array())
	{
		$this->users = $users;
	}
	
	public static function getName()
	{
		return self::FILTER_NAME;
	}
	
	public function isIncluded(ResponseInterface $response)
	{
		$body = json_decode((string) $response->getBody());
		foreach ($body as $event) {
			$actor = $event['actor']['login'];
			if (in_array($actor, $this->users)) {
				return true;
			}
		}
		
		return false;
	}
}

You can now add your custom filter in your configuration and use it in your tapes :

custom_filters:
	- "Acme\MyApp\Filter\ActorFilter"
	- 
tapes:
	my_tape:
		filters:
			actor_filter: ["jexp","ikwattro","luanne"]

Your tape will now contain only Responses that included in their actors one of the provided actors.

Extra configuration setting :

Your Responses objects can contain a specific header as a marker by setting the following configuration flag :

marker_header: true

which will add a X-Guzzle-Stereo header with a true value.

TODO

  • more filters
  • blacklists
  • null tape
  • better configuration management (maybe Symfony config component)
  • combined requestAndResponses
  • filter what should be returned from the player
  • your ideas ?

Tests

phpspec and phpunit are used for the tests:

./vendor/bin/phpspec run -f pretty
./vendor/bin/phpunit

License

The library is issued under the MIT license, please refer to the LICENSE file provided with the package.

Contribute

Feel free to contribute or report issues on Github.

Author

Christophe Willemsen

Twitter: @ikwattro

Github: @ikwattro

ikwattro/guzzle-stereo 适用场景与选型建议

ikwattro/guzzle-stereo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.59k 次下载、GitHub Stars 达 78, 最近一次更新时间为 2015 年 07 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「http」 「mock」 「Guzzle」 「response」 「record」 「replay」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ikwattro/guzzle-stereo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ikwattro/guzzle-stereo 我们能提供哪些服务?
定制开发 / 二次开发

基于 ikwattro/guzzle-stereo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 78
  • Watchers: 6
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-07-26