測試第三方 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 包樣板生成的。