bbrothers / muzzle
对 Guzzle 请求和响应的断言
0.3.3
2019-02-14 06:46 UTC
Requires
- php: ~7.1
- ext-dom: *
- ext-json: *
- guzzlehttp/guzzle: ^6.3
- hamcrest/hamcrest-php: ^2.0
- illuminate/support: ^5.5
- myclabs/php-enum: ^1.5
- phpunit/phpunit: ^7.1
- symfony/var-dumper: ^4.0
Requires (Dev)
- php-vfs/php-vfs: ^1.4
- squizlabs/php_codesniffer: ^2.3
This package is auto-updated.
Last update: 2024-09-16 07:35:41 UTC
README
这是一个关于在 Guzzle 请求和响应中使用断言有用性的实验。此代码主要来自黑客马拉松,未经全面测试,不适合生产使用。
安装
通过 Composer
$ composer require bbrothers/muzzle
使用方法
使用流畅的构建器定义一组预期请求和模拟响应
$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');
如果没有指定,则默认响应为空 200
。
可以使用 expect
方法传递预构建的 Exception
实例
$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();
可以通过使用 append
方法直接将期望添加到 Muzzle
实例
$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);
默认情况下,Muzzle
会期望进行了请求,并返回一个空的 200
响应。
构建器或 Expectation
类上有几个预定义的期望可用
method
:接受一个可变数量的 HTTP 方法列表,并断言实际请求方法在提供的列表中。uri
:接受一个 URI、路径或正则表达式模式,以匹配实际请求。headers
:接受一个头信息数组。它们可以是头信息名称或头信息名称/期望值的键/值对,并将断言所有头信息与提供的值匹配。query
:接受一个数组,表示期望包含在请求中的查询参数。参数应作为[$name => $value]
的关联数组传递,其中值可选为正则表达式模式。queryShouldEqual
:类似于query
,此方法接受一个关联数组参数,但是这些参数必须与实际请求完全匹配(除了顺序)。body
:接受一个字符串、数组、StreamInterface
实例或正则表达式模式。如果提供了一个数组,并且实际请求不是 JSON,它将使用json_encode
将数组编码,并查找完全匹配。如果实际请求是 JSON,它将解码它,并使用与query
方法相同的匹配策略,允许使用正则表达式作为值。当提供一个 JSON 字符串时,它将被解码,并像数组一样处理。json
:接受一个数组,并将其委托给body
方法。bodyShouldEqual
:接受一个字符串或可转换为字符串的对象,例如StreamInterface
实例,并断言它与实际请求体完全匹配。should
:接受一个callable
,并在调用callable
时提供实际请求作为AssertableRequest
实例和Muzzle
实例作为参数。预期的callable
返回类型为void
,因此任何返回值都将被忽略。有关详细信息,请参阅以下内容。
可以通过使用实现 Assertion
接口的 callable
调用 should
方法将自定义断言规则添加到 Expectation
。当运行 Assertion
时,记录的请求将作为 AssertableRequest
实例传递给 __invkoke
方法。也将传递 Muzzle
实例作为可选的第二个参数。
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']));
或者只是一个回调
$expected = ['name' => 'Jane Doe']; (new Expectation)->should(function (AssertableRequest $actual) use ($expected) : void { $actual->assertJson($expected); });
还可以在 Muzzle
的任何响应或事务历史中的请求/响应上运行额外的断言
$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');
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG。
测试
$ composer test
贡献
请参阅CONTRIBUTING和CODE_OF_CONDUCT获取详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件brad@bradbrothers.ca联系,而不是使用问题跟踪器。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。