测试第三方 API 时,以简单且声明性的方式模拟它们通常具有挑战性。该包旨在通过为 guzzle 提供具有类似路由器行为的自定义处理程序来帮助简化此过程,而不是依赖于以任何特定顺序从堆栈中弹出的响应。
您可以通过 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 调用是按正确的顺序进行的很有用。例如,也许您必须在获取/users
之前调用/login
。这是通过为您的响应命名,然后在拨打电话后断言命令来实现的。
$ 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)。请参阅许可证文件以获取更多信息。
该包是使用 PHP 包样板生成的。