tomb1n0 / guzzle-mock-handler
1.6
2024-04-04 12:21 UTC
Requires
- php: ^7.1|^8.0
- guzzlehttp/guzzle: ^6.2|^7.0
Requires (Dev)
- phpunit/phpunit: ^7.0|^8.0|^9.0
README
在测试第三方API时,常常需要以简单和声明性的方式对其进行模拟。本包旨在通过提供一个具有路由器行为的自定义处理器来帮助简化此过程,而不是依赖于按特定顺序弹出堆栈中的响应。
安装
您可以通过composer安装此包
composer require tomb1n0/guzzle-mock-handler
用法
基本示例
use GuzzleHttp\Client; use Tomb1n0\GuzzleMockHandler\GuzzleMockHandler; // Create a new instance of the mock handler $handler = new GuzzleMockHandler; // Create a new mock response for '/login', returning ['key' => 'value'] in the body. // By default responses expect a GET verb, and return a 200 response. $loginResponse = (new GuzzleMockResponse('/login'))->withBody([ 'key' => 'value' ]); // Tell the handler that we're expecting this response $handler->expect($loginResponse); // Create a new Guzzle Handlerstack, passing in our custom handler $stack = HandlerStack::create($handler); // Finally, create the guzzle client, passing our stack in $guzzle = new Client(['handler' => $stack]); $response = $guzzle->get('/login'); // A normal guzzle response object $response->getStatusCode(); // == 200 json_decode((string) $response->getBody()); // == ['key' => 'value']
请求断言
有时对返回的响应执行断言很有用。也许您有一个登录第三方API的类,并且您想断言用户名和密码是否正确发送。
$handler = new GuzzleMockHandler; $loginResponse = (new GuzzleMockResponse('/login')) ->withMethod('post') ->assertRequestJson([ 'username' => 'tomb1n0', 'password' => 'correct-horse-battery-staple' ]); // NOTE: If you only care about the username in this case, you can pass in a key as the second parameter to assertRequestJson like so: /** * ->assertRequestJson('tomb1n0, 'username'); **/ $handler->expect($loginResponse); $stack = HandlerStack::create($handler); $guzzle = new Client(['handler' => $stack]); // Just before the response is actually sent back to guzzle, our handler will assert the request JSON is corect. $response = $guzzle->post('/login', [ 'json' => [ 'username' => 'tomb1n0', 'password' => 'correct-horse-battery-staple' ] ]);
注意:您也可以使用->assertRequestHeaders()
执行完全相同的断言,这将允许您确保API请求包含X-API-KEY
头或类似内容。
自定义断言
断言正文或头可能不够,因此我们允许您调用->withAssertion()
,将请求和响应对象传递给您,以便您执行自己的断言
$handler = new GuzzleMockHandler; $loginResponse = (new GuzzleMockResponse('/login')) ->withMethod('post') // if you want to perform multiple assertions, you can call ->withAssertion multiple times. ->withAssertion(function(RequestInterface $request, ResponseInterface $response) { $this->assertEquals('super-secure-key', $request->getHeader('X-API-KEY')); }); $handler->expect($loginResponse); $stack = HandlerStack::create($handler); $guzzle = new Client(['handler' => $stack]); $guzzle->post('/login');
断言顺序
有时断言API调用按正确顺序执行很有用。例如,您可能必须先调用/login
,然后再获取/users
。这是通过给您的响应命名,然后在调用之后断言顺序来实现的。
$handler = new GuzzleMockHandler; $loginResponse = (new GuzzleMockResponse('/login'))->withMethod('post'); $usersResponse = new GuzzleMockResponse('/users'); $handler->expect($loginResponse, 'login-response'); $handler->expect($usersResponse, 'users-response'); $stack = HandlerStack::create($handler); $guzzle = new Client(['handler' => $stack]); $guzzle->post('/login'); $guzzle->get('/users'); // Performs a assertsEquals behind the scenes, as the handler keeps track of the order calls were made in. $handler->assertCalledOrder([ 'login-response', 'users-response' ]);
只允许响应被调用一次
有时您可能只想在测试中允许端点被调用一次 - 这可以通过在响应对象上调用->once()
来实现。
$handler = new GuzzleMockHandler; $loginResponse = (new GuzzleMockResponse('/login')) ->withMethod('post') ->once(); $handler->expect($loginResponse); $stack = HandlerStack::create($handler); $guzzle = new Client(['handler' => $stack]); $response = $guzzle->post('/login'); // successfull $response = $guzzle->post('/login'); // ResponseNotFound exception is thrown, "No response set for post => /login"
测试
composer test
致谢
许可
MIT许可(MIT)。有关更多信息,请参阅许可文件。
PHP包模板
此包使用PHP包模板生成。