mcustiel/phiremock-codeception-module

PhireMock 的 Codeception 模块。允许模拟远程服务以进行 HTTP 请求。

v2.0.0 2022-07-29 18:20 UTC

This package is auto-updated.

Last update: 2024-08-29 04:48:57 UTC


README

此 Codeception 模块允许您连接到 Phiremock 服务器,并通过您的测试中的 Codeception Actor 以语义化的方式与其交互。

Packagist Version Build Status Scrutinizer Code Quality Packagist Downloads

安装

Composer

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

配置

您需要在套件配置文件中启用 Phiremock 模块

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

选项

host

指定 phiremock-server 监听请求的主机。

默认: localhost

port

指定 phiremock-server 监听请求的端口。

默认值 8086

reset_before_each_test

确定是否在每个测试之前重置 phiremock-server。

默认: false

expectations_path

指定 expectation json 文件所在的路径。然后可以使用注解引用这些文件,并且它们将被自动加载。

默认: codeception_dir/_data/phiremock-expectations

secure

一个布尔值,指定连接是否安全。如果是安全的,请求将通过 https 完成,否则将通过 http 完成。

默认: false。对 phiremock-client 的请求通过 http 完成。

extra_connections

一个对象列表,指定请求其他 phiremock-servers 的参数。如果您想拥有两个 phiremock 实例,一个监听 http 连接,另一个监听 https 连接,这很有用。

默认: 空列表,意味着不请求其他 phiremock 服务器。

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

然后在代码中,您可以按如下方式按名称使用每个连接

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

默认连接名为 'default',您也可以使用它

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

client_factory

指定一个类,该类重写了默认的 phiremock-client 工厂。如果您想避免使用 Guzzle HttpClient v6(默认由 phiremock-client 支持),这很有用。

默认: default

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

然后您可以创建一个工厂如下,并在模块配置中提供完全限定的类名

<?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]);
    }
}

可以提供实现 psr18 的任何 HTTP 客户端。

用法

该模块提供了以下便捷方法来与 Phiremock 服务器通信

expectARequestToRemoteServiceWithAResponse

允许您在 Phiremock 中设置期望,指定预期的请求和服务器应为此请求提供的响应

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

haveACleanSetupInRemoteService

清除服务器上的所有配置期望、场景和请求历史,如果存在,则重新加载期望文件。

    $I->haveACleanSetupInRemoteService();

dontExpectRequestsInRemoteService

清除所有先前配置的期望和请求历史。

    $I->dontExpectRequestsInRemoteService();

haveCleanScenariosInRemoteService

清除所有场景的状态(将它们全部设置为初始状态)。

    $I->haveCleanScenariosInRemoteService();

seeRemoteServiceReceived

允许您验证服务器接收了指定次数的请求。此请求可能或可能未以前设置为期望。

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

didNotReceiveRequestsInRemoteService

重置 Phiremock 中验证器的请求计数器。

    $I->didNotReceiveRequestsInRemoteService();

grabRequestsMadeToRemoteService

检索 Phiremock 服务器接收到的所有与指定请求匹配的请求。

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

setScenarioState

强制设置场景的状态。

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

takeConnection

允许使用多个连接到不同的 phiremock 服务器。

    $I->takeConnection('connectionName');

@expectation 注解

允许您通过 json 文件设置期望

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

默认情况下将加载位于 tests/_data/phiremock-expectations/get_client_timeout.json 的文件。可以配置期望的位置。

您可以使用在子目录中设置期望

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

接受多种注释格式

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

另请参阅