bbrothers/muzzle
Composer 安装命令:
composer require bbrothers/muzzle
包简介
Guzzle Assertions on Requests and Responses
README 文档
README
An experiment into the usefulness of assertions on Guzzle Requests and Responses. This code is mostly pulled from hack sessions and is not fully tested or ready for production use.
Install
Via Composer
$ composer require bbrothers/muzzle
Usage
Use the fluent builder to define a set of expected requests and mock responses:
$client = Muzzle::builder() ->post('https://example.com/contact') ->json(['name' => 'Jane Doe']) ->replyWith(new Response(HttpStatus::CREATED)) ->get('https://example.com/contact') ->query(['name' => 'Jane Doe']) ->build(); $this->assertInstanceOf(Muzzle::class, $client); $client->post('https://example.com'); $client->get('https://example.com');
If not specified responses will default to an empty 200.
The expect method can be used to pass pre-built Exception instances:
$createUser = (new Expectation) ->post('users') ->json(['name' => 'Jane', 'email' => 'j.doe@example.com']) ->replyWith((new ResponseBuilder)->setJson(User::make([ 'name' => 'Jane', 'email' => 'j.doe@example.com' ])->toArray()); $client = Muzzle::builder()->expect($createUser)->build();
Expectations can also be added directly to the Muzzle instance by using the append method:
$client = new Muzzle; $expectations = []; for ($i = 0; $i < 10; $i++) { $expectations[] = (new Expectation) ->get("users/{$i}") ->replyWith((new ResponseBuilder)->setJson(['number' => $i])); } $client->append(...$expectations);
By default Muzzle will expect that a request was made and return an empty 200 response.
There are several pre-defined expectations available on the builder or Expectation class directly:
method: accepts a variadic list of HTTP methods and asserts the actual request method is in the provided list.uri: accepts a URI, path or regex pattern to match the actual request against.headers: accepts an array of headers. They can be either the header name or a key/value pair of header name/expected value and will assert that all headers match the provided values.query: accepts an array of query parameters expected to be contained in the request. Parameters should be passed as an associative array of[$name => $value], with the value optionally being a regex pattern.queryShouldEqual: likequerythis method accepts an associative array of parameters, however these must match exactly (with the exception of the order) with the actual request.body: accepts a string, array,StreamInterfaceinstance or a regex pattern. If an array is given and the actual request is not json, it willjson_encodethe array and look for an exact match. If the actual request is JSON, it will decode it and use the same matching strategy as thequerymethod, allowing for regex patterns as values. When a JSON string is provided, it will be decoded and treated the same as an array.json: accepts an array and delegates to thebodymethod.bodyShouldEqual: accepts a string or string castable object, such as aStreamInterfaceinstance, and asserts that it is an exact match to the actual request body.should: accepts acallableand provides the actual request as anAssertableRequestinstance and theMuzzleinstance as parameters when invoking thecallable. Thecallableis expected be avoidreturn type, so any return value will be ignored. See below for details.
Custom assertion rules can be added to an Expectation by calling the should method with a callable that implements the Assertion interface. When the Assertion is run, the recorded request will be passed to the __invkoke method as an AssertableRequest instance. The Muzzle instance is also passed as an optional second parameter.
class ContainJson implements Assertion { public function __consturct(array $content) { $this->expected = $expected; } public function __invoke(AssertableRequest $actual) : void { $actual->assertJson($this->expected); } } // then (new Expectation)->should(new ContainJson(['name' => 'Jane Doe']));
Or as just a callback:
$expected = ['name' => 'Jane Doe']; (new Expectation)->should(function (AssertableRequest $actual) use ($expected) : void { $actual->assertJson($expected); });
Additional assertions can also be run on any responses from Muzzle or on requests/responses from the transaction history:
$client = Muzzle::builder() ->post('https://example.com/contact') ->json(['name' => 'Jane Doe']) ->replyWith(new Response(HttpStatus::CREATED)) ->get('http://example.com/contact') ->query(['name' => 'Jane Doe']) ->replyWith(new Response(HttpStatus::MOVED_PERMANENTLY)) ->build(); $this->assertInstanceOf(Muzzle::class, $client); $client->post('https://example.com/contact')->assertSuccessful(); $client->get('http://example.com/contact')->assertRedirect('https://example.com/contact'); $client->lastRequest()->assertUriQueryNotHasKey('age');
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email brad@bradbrothers.ca instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
bbrothers/muzzle 适用场景与选型建议
bbrothers/muzzle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.81k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2018 年 06 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Guzzle」 「assertions」 「bbrothers」 「muzzle」 「http testing」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bbrothers/muzzle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bbrothers/muzzle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bbrothers/muzzle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Additional assertions for PHPUnit
Custom annotations and expectations for PHPUnit.
A collection of helpers for PHPUnit to ease testing Tarantool libraries.
Laravel-specific Testing Helpers and Assertions.
Assertion library for PHP 8.4+
Cloudflare Middleware For Guzzle
统计信息
- 总下载量: 4.81k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-06-08