neur0toxine/pock 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

neur0toxine/pock

Composer 安装命令:

composer require neur0toxine/pock

包简介

PSR-18 compatible HTTP mock library

README 文档

README

Build Status Coverage Latest stable PHP from Packagist License

pock

Easy to use HTTP mocking solution, compatible with PSR-18 and HTTPlug.

Project is still in its early development stage. API can change over time, but I'll try to not introduce breaking changes. You can find autogenerated documentation here or look at the examples. API for the mock building can be found here and API for the response building (returned from PockBuilder::reply call) can be found here.

Examples

Mock JSON API route with Basic authorization, reply with JSON.

use Pock\Enum\RequestMethod;
use Pock\Enum\RequestScheme;
use Pock\PockBuilder;

$builder = new PockBuilder();
$builder->matchMethod(RequestMethod::GET)
    ->matchScheme(RequestScheme::HTTPS)
    ->matchHost('example.com')
    ->matchPath('/api/v1/users')
    ->matchHeaders([
        'Content-Type' => 'application/json',
        'Authorization' => 'Basic YWxhZGRpbjpvcGVuc2VzYW1l'
    ])
    ->reply(200)
    ->withHeader('Content-Type', 'application/json')
    ->withJson([
        [
            'name' => 'John Doe',
            'username' => 'john',
            'email' => 'john@example.com'
        ],
        [
            'name' => 'Jane Doe',
            'username' => 'jane',
            'email' => 'jane@example.com'
        ],
    ]);

// Pass PSR-18 compatible client to the API client.
$client = new MysteriousApiClient($builder->getClient());
$client->setCredentials('username', 'password');

// Receive mock response.
$response = $client->getUsers();

Same mock, but with models! Also, the code itself is slightly shorter.

use Pock\Enum\RequestMethod;
use Pock\PockBuilder;

$builder = new PockBuilder();
$builder->matchMethod(RequestMethod::GET)
    ->matchUri('https://example.com/api/v1/users')
    ->matchHeaders([
        'Content-Type' => 'application/json',
        'Authorization' => 'Basic YWxhZGRpbjpvcGVuc2VzYW1l'
    ])
    ->reply(200)
    ->withHeader('Content-Type', 'application/json')
    ->withJson([
        // We're assuming here that MysteriousUser's constructor can receive an initial values.
        new MysteriousUser('John Doe', 'john', 'john@example.com'),
        new MysteriousUser('Jane Doe', 'jane', 'jane@example.com'),
    ]);

// Pass PSR-18 compatible client to the API client.
$client = new MysteriousApiClient($builder->getClient());
$client->setCredentials('username', 'password');

// Receive mock response.
$response = $client->getUsers();

It is possible to mock a response using DTO's because pock can use third-party serializers under the hood.

Serializer support

pock supports JMS serializer and Symfony serializer out of the box. Available serializer will be instantiated automatically. It will be used to serialize requests and responses in mocks which means you actually can pass an entire DTO into the corresponding methods (for example, matchJsonBody as an assertion or withJsonBody to generate a response body).

By default, JMS serializer has more priority than the Symfony serializer. You can use methods below before running tests (bootstrap.php) if you want to override default behavior.

use Pock\Factory\JsonSerializerFactory;
use Pock\Factory\XmlSerializerFactory;
use Pock\Serializer\SymfonySerializerAdapter;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

$encoders = [new XmlEncoder(), new JsonEncoder()];
$normalizers = [new ObjectNormalizer()];
$serializer = new SymfonySerializerAdapter(new Serializer($normalizers, $encoders));

JsonSerializerFactory::setSerializer($serializer);
XmlSerializerFactory::setSerializer($serializer);

In order to use unsupported serializer you should create an adapter which implements Pock\Serializer\SerializerInterface.

Roadmap to stable

  • at(N) - execute mock only at Nth call.
  • always() - always execute this mock (removes mock expiration).
  • Separate UniversalMockException into several exceptions (PockClientException, PockNetworkException, etc).
  • Add methods for easier throwing of exceptions listed in previous entry.
  • replyWithCallback - reply using specified callback.
  • replyWithFactory - reply using specified response factory (provide corresponding interface).
  • Compare XML bodies using DOMDocument, fallback to text comparison in case of problems.
  • Regexp matchers for body, query, URI and path.
  • Form Data body matcher (partial & exact)
  • Multipart form body matcher (just like callback matcher but parses the body as a multipart form data)
  • BREAKING CHANGE: Rename serializer decorators to serializer adapters.
  • Real network response for mocked requests.
  • symfony/http-client support.
  • Document everything (with examples if it’s feasible).

neur0toxine/pock 适用场景与选型建议

neur0toxine/pock 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 46.07k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2021 年 05 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「http」 「mock」 「psr-7」 「php-http」 「psr-18」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 neur0toxine/pock 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 neur0toxine/pock 我们能提供哪些服务?
定制开发 / 二次开发

基于 neur0toxine/pock 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 46.07k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 0
  • 依赖项目数: 7
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-05-15