darkmatus / phiremock-codeception-module
PhireMock 的 Codeception 扩展模块。允许模拟远程服务的 HTTP 请求。
Requires
- php: >=8.0
- codeception/codeception: 5.0.x-dev
- codeception/lib-asserts: ^2.0.0
- mcustiel/phiremock-client: ^1.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-11 14:53:40 UTC
README
此 codeception 模块允许您连接到 Phiremock 服务器,并通过测试中的 codeception Actor 以语义化方式与之交互。
安装
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
指定存放期望 json 文件的路径。然后可以使用注解引用这些文件,并将它们自动加载。
默认: codeception_dir/_data/phiremock-expectations
secure
一个布尔值,指定连接是否安全。如果是安全的,则通过 https 进行请求,否则通过 http 进行请求。
默认: false. 对 phiremock-client 的请求是通过 http 进行的。
extra_connections
一个对象列表,指定请求其他 phiremock-servers 的参数。如果您想有 2 个 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")
另请参阅
- Phiremock 客户端:https://github.com/mcustiel/phiremock-client
- Phiremock Codeception 扩展:https://github.com/mcustiel/phiremock-codeception-extension
- 测试中的示例:https://github.com/mcustiel/phiremock-codeception-module/tree/master/tests/acceptance