webwhales/guzzle-conditional-mock-handler
最新稳定版本:v1.0.5
Composer 安装命令:
composer require webwhales/guzzle-conditional-mock-handler
包简介
Offers a way to load mocked responses conditionally with Guzzle instead of a fixed queue
README 文档
README
This is a package that offers a way to load mock responses conditionally based on the URL, instead of a fixed queue.
Installation
Install this package using composer:
composer require --dev webwhales/guzzle-conditional-mock-handler
Simple Example
To use this Conditional Mock Handler, use the following example:
use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7\Response; use WebWhales\GuzzleConditionalMockHandler\Handler as MockHandler; $mockHandler = new MockHandler(); // Create a handler stack to mock the Guzzle Client with $handler = HandlerStack::create($mockHandler); $client = new Client(['handler' => $handler]); // Add mocked responses $mockHandler->addResponse('https://example.com', new Response(200, [], 'This is a test')); // Make a request to a matching URL $response = $client->request('GET', 'https://example.com'); echo $response->getBody(); // Outputs "This is a test" // Make a request to a non matching URL $response = $client->request('GET', 'https://www.example.com'); echo $response->getBody(); // Outputs the actual content of https://www.example.com
Regex example
This packages also supports regex patterns:
// Add mocked responses $mockHandler->addResponse('^http(s)?://example\.', new Response(200, [], 'This is a test')); // Make a request to a matching URL $response = $client->request('GET', 'https://example.com'); echo $response->getBody(); // Outputs "This is a test" // Make a request to a non matching URL $response = $client->request('GET', 'https://www.example.com'); echo $response->getBody(); // Outputs the actual content of https://www.example.com
Initialization Helper
Using this handler can also be simplified using the the following helper:
$client = null; $mockHandler = MockHandler::initializeWithClient($client);
License
The this package is open source software licensed under the MIT license
统计信息
- 总下载量: 30.92k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 2
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-30