mcustiel/phiremock-codeception-extension

Phiremock 的 Codeception 扩展。允许对 HTTP 请求进行远程服务的模拟。

v3.1.0 2024-04-22 07:06 UTC

README

Codeception 扩展,用于简化与 Phiremock 服务器 的工作。允许在执行测试套件之前启动 Phiremock 服务器,并在测试套件结束时停止它。

Latest Stable Version Build Status Scrutinizer Code Quality Monthly Downloads

安装

Composer

    "require-dev": {
        "mcustiel/phiremock-codeception-extension": "^3.0"
    }

可选,如果您想在依赖项之间安装 Phiremock 服务器,请安装它。如果不安装,您需要在配置中指定 phiremock 的路径。

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

Phiremock 服务器已成为可选依赖项,以便您可以从 Phar 文件、全局 Composer 依赖项或 Docker 容器中运行它,而不将其作为项目依赖项。

配置

extensions:
    enabled:
        - \Codeception\Extension\Phiremock
    config:
        \Codeception\Extension\Phiremock:
            listen: 127.0.0.1:18080 # defaults to 0.0.0.0:8086 
            bin_path: ../vendor/bin # defaults to codeception_dir/../vendor/bin 
            logs_path: /var/log/my_app/tests/logs # defaults to codeception's tests output dir
            debug: true # defaults to false
            wait_until_ready: true # defaults to false
            wait_until_ready_timeout: 15 # (seconds) defaults to 30
            wait_until_ready_interval: 100 # (microseconds) defaults to 50000
            expectations_path: /my/expectations/path # defaults to tests/_expectations
            server_factory: \My\FactoryClass # defaults to 'default'
            extra_instances: [] # deaults to an empty array
            suites: [] # defaults to an empty array
            certificate: /path/to/cert # defaults to null
            certificate_key: /path/to/cert-key # defaults to null
            cert_passphrase: 'my-pass' # defaults to null

注意:自 Codeception 版本 2.2.7 以来,可以在套件配置文件中直接添加扩展配置。这样可以避免为每个套件启动 Phiremock。

参数

listen

指定 Phiremock 必须监听请求的接口和端口。

默认值 0.0.0.0:8086

bin_path

Phiremock 服务器 "二进制" 文件所在的路径。例如,您可以指向文件系统中 Phar 的位置。

默认值: codeception_dir/../vendor/bin/phiremock

logs_path

输出写入的路径。

默认值: codeception 的测试输出目录

debug

是否将调试数据写入日志文件。

默认值: false

start_delay

Phiremock 服务器启动后等待的时间,然后再运行测试(用于给 Phiremock 服务器启动时间)

默认值 0

wait_until_ready

这是 start_delay 的更健壮的替代方案。它将在运行测试之前检查 Phiremock 服务器是否实际正在运行。注意:它依赖于通过 Composer 安装的 Phiremeock 客户端(它用于检查 Phiremock 服务器状态)。

默认值: false

wait_until_ready_timeout

这将仅在 wait_until_ready 设置为 true 时使用。您可以指定多少秒后停止检查 Phiremock 服务器是否正在运行。

默认值 30

expectations_path

指定一个目录,用于搜索默认要加载的期望定义的 json 文件。

默认值: codecption_dir/_expectations

certificate

证书文件的路径,以允许 phiremock-server 监听安全的 https 连接。

默认值: null。表示 phiremock 只会监听非安全的 http 连接。

certificate-key

证书密钥文件的路径。

默认值: null。

cert-passphrase

用于加密证书的证书密码文件的路径(如果加密则需要)。

默认值: null。表示不会执行基于密码的解密。

suites

指定必须执行 phiremock-server 的套件列表。

默认值: [] 空数组,表示 phiremock 将为每个套件执行。

extra_instances

允许指定更多 phiremock-server 实例以运行。如果您想要,例如,运行一个监听 http 连接和一个监听 https 连接的实例,这很有用。每个实例都有自己的配置,并且可以单独为不同的套件运行。

默认值: [] 空数组,表示没有配置额外的 phiremock-server 实例。

示例

extensions:
    enabled:
        - \Codeception\Extension\Phiremock
    config:
        \Codeception\Extension\Phiremock:
            listen: 127.0.0.1:18080  
            debug: true 
            expectations_path: /my/expectations/path-1 
            suites: 
                - acceptance
            extra_instances: 
                - 
                    listen: 127.0.0.1:18081
                    debug: true
                    start_delay: 1
                    expectations_path: /my/expectations/path-2
                    suites:
                        - acceptance
                        - api
                    certificate: /path/to/cert
                    certificate_key: /path/to/cert-key 
                    cert_passphrase: 'my-pass' 

server_factory

指定一个扩展 \Mcustiel\Phiremock\Server\Factory\Factory 的工厂类。如果您想提供自己的PSR,这将很有用。此功能仅在使用composer文件中指定为本地依赖项安装phiremock时有效。

默认值:default

示例:如果此内容在您的composer.json中

"require-dev": {
    "mcustiel/phiremock-codeception-extension": "v2.0",
    "mcustiel/phiremock-server": "^1.0",
    "guzzlehttp/guzzle": "^7.0"

则可以创建工厂类如下

<?php
namespace My\Namespace;

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

class FactoryWithGuzzle7 extends Factory
{
    public function createHttpClient(): ClientInterface
    {
        return new GuzzleHttp\Client();
    }
}

并在扩展配置中提供该类的完全限定命名空间

 enabled:
   - \Codeception\Extension\Phiremock
 config:
   \Codeception\Extension\Phiremock:
     server_factory: \My\Namespace\FactoryWithGuzzle7

另请参阅