定制 mcustiel/phiremock-codeception-module 二次开发

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

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

mcustiel/phiremock-codeception-module

Composer 安装命令:

composer create-project mcustiel/phiremock-codeception-module

包简介

Codeception module for PhireMock. Allows to stub remote services for HTTP requests.

README 文档

README

This codeception module allows you to connect to a Phiremock Server and to interact with it in a semantic way through the codeception actor in your tests.

Packagist Version Build Status Scrutinizer Code Quality Packagist Downloads

Installation

Composer:

    "require-dev": {
        "mcustiel/phiremock-codeception-module": "^1.0",
        "guzzlehttp/guzzle": "^6.0"
    }

Configuration

You need to enable Phiremock module in your suite's configuration file:

modules:
    enabled:
        - Phiremock:
            host: phiremock-myhost.dev # Defaults to localhost
            port: 18080 # Defaults to 8086
            reset_before_each_test: false # if set to true, executes `$I->haveACleanSetupInRemoteService` before each test. Defaults to false
            expectations_path: /path/to/my/expectation/json/files # Defaults to codeception_dir/_data/phiremock-expectations
            client_factory: \My\ClientFactory # Defaults to 'default'
            secure: true # Default: false
            extra_connections:  [] # Default: empty array

Options

host

Specifies the host where phiremock-server is listening for requests.

Default: localhost

port

Specifies the port where phiremock-server is listening for requests.

Default: 8086

reset_before_each_test

Determines whether or not phiremock-server must be reset before each test.

Default: false

expectations_path

Specifies the path where expectation json files are located. The files can then be referenced using annotations and will be loaded automatically.

Default: codeception_dir/_data/phiremock-expectations

secure

A boolean specifying if the connection is secure or not. If it's secure, the request is done through https, otherwise it's done through http.

Default: false. The requests to phiremock-client are done through http.

extra_connections

An list of objects specifying the parameters to request other phiremock-servers. This is useful if you want to have 2 phiremock instances, one listening for http connections, and the other listening for https connections.

Default: An empty list, meaning that no other phiremock servers are requested.

Example
modules:
    enabled:
        - Phiremock:
            host: phiremock-myhost.dev
            port: 18080 
            secure: false 
            extra_connections: 
                secure-host:
                    host: phiremock-myhost.dev
                    port: 18081 
                    secure: true

Then in the code you can use each connection by name as follows:

<?php
$I->takeConnection('secure-host')->reset();

The default connection is called 'default' and you can also take it:

$I->takeConnection('default')->reset();

client_factory

Specifies the fully qualified class name of a class which overrides default phiremock-client factory. This is useful if you want to avoid using Guzzle HttpClient v6 (the one supported by default in phiremock-client).

Default: default

Example
"require-dev": {
    "mcustiel/phiremock-codeception-module": "v1.0",
    "guzzlehttp/guzzle": "^7.0"

The you can create a factory as follows and provide the fully qualified class name in the module configuration:

<?php
namespace My\Namespace;

use Mcustiel\Phiremock\Client\Factory;
use GuzzleHttp;
use Psr\Http\Client\ClientInterface;

class FactoryWithGuzzle7 extends Factory
{
    public function createRemoteConnection(): ClientInterface
    {
        return new GuzzleHttp\Client(['allow_redirects' => false]);
    }
}

Any http client implementing psr18 can be provided.

Usage

The module provides the following handy methods to communicate with Phiremock server:

expectARequestToRemoteServiceWithAResponse

Allows you to setup an expectation in Phiremock, specifying the expected request and the response the server should give for it:

    $I->expectARequestToRemoteServiceWithAResponse(
        on(getRequest()->andUrl(isEqualTo('/some/url')))
            ->then(respond(203)->andBody('I am a response'))
    );

haveACleanSetupInRemoteService

Cleans the server of all configured expectations, scenarios and requests history, and reloads expectation files if they are present.

    $I->haveACleanSetupInRemoteService();

dontExpectRequestsInRemoteService

Cleans all previously configured expectations and requests history.

    $I->dontExpectRequestsInRemoteService();

haveCleanScenariosInRemoteService

Cleans the state of all scenarios (sets all of them to inital state).

    $I->haveCleanScenariosInRemoteService();

seeRemoteServiceReceived

Allows you to verify that the server received a request a given amount of times. This request could or not be previously set up as an expectation.

    $I->seeRemoteServiceReceived(1, getRequest()->andUrl(isEqualTo('/some/url')));

didNotReceiveRequestsInRemoteService

Resets the requests counter for the verifier in Phiremock.

    $I->didNotReceiveRequestsInRemoteService();

grabRequestsMadeToRemoteService

Retrieves all the requests received by Phiremock server matching the one specified.

    $I->grabRequestsMadeToRemoteService(getRequest()->andUrl(isEqualTo('/some/url')));

setScenarioState

Forces the state of a scenario.

    $I->setScenarioState('scenarioName', 'newScenarioState');

takeConnection

Allows to use several connections to different phiremock servers.

    $I->takeConnection('connectionName');

@expectation Annotations

Allows you to set up an expectation via a json file

    /**
     * @expectation("get_client_timeout")
     */
    public function test(FunctionalTester $I)
    {
        ...
    }

That will load by default the file at tests/_data/phiremock-expectations/get_client_timeout.json. The path where to place the expectations is configurable.

You may use expectations placed in subdirectories

    /**
     * @expectation("edge_cases/get_client_timeout")
     */
    public function test(FunctionalTester $I)
    {
        ...
    }

Multiple annotation formats are accepted

     * @expectation get_client_timeout
     * @expectation get_client_timeout.json
     * @expectation(get_client_timeout.json)
     * @expectation(get_client_timeout)
     * @expectation("get_client_timeout")

See also:

mcustiel/phiremock-codeception-module 适用场景与选型建议

mcustiel/phiremock-codeception-module 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 562.98k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2019 年 02 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 mcustiel/phiremock-codeception-module 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 562.98k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 22
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2019-02-25