定制 webwhales/guzzle-conditional-mock-handler 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-03-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固