lequipefr/mockserver-behat-context
MockServer 的 Behat 上下文。
Requires
- php: >=8.0.2
- behat/behat: ^3.0
- behat/gherkin: ^4.0
- guzzlehttp/psr7: ^2.4
- php-http/discovery: ^1.14
- psr/http-client: ^1.0
- psr/http-message: ^1.0|^2.0
Requires (Dev)
- php-http/guzzle7-adapter: ^1.0
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.5
This package is not auto-updated.
Last update: 2024-09-18 19:32:35 UTC
README
为MockServer提供 PHP 客户端和 Behat 上下文。
安装
composer require --dev lequipefr/mockserver-behat-context
然后在您的 behat.yml
文件中添加一个上下文,并指定您本地 MockServer 实例的 URL。
default: suites: default: contexts: # Add this: - Lequipe\MockServer\Behat\MockServerContext: mockServer: 'http://127.0.0.1:1080'
现在,在您的 Behat 测试中,您应该可以使用以下方式模拟项目所依赖的 web 服务。
Given the request "GET" "/users/1" will return the json:
"""
{
"id": 1,
"name": "Zidane"
}
"""
用法
Behat 上下文
查看您可以使用的所有 Behat 表达式(请点击此处)。
PHP 客户端
您还可以将此库用作简单的 PHP 客户端,并以原始数组的形式发送您的预期,如mockserver Swagger API中定义的那样。
<?php use Lequipe\MockServer\Client\MockServerClient; $client = new MockServerClient('http://127.0.0.1:1080'); $client->expectation([ 'httpRequest' => [ 'method' => 'GET', 'path' => '/users/1', ], 'httpResponse' => [ 'body' => [ [ 'id' => 1, 'name' => 'Zidane', ], ], ], ]);
构建器
您可以使用预期构建器而不是原始数组,并让您的 IDE 自动完成。
<?php use Lequipe\MockServer\MockServerClient; use Lequipe\MockServer\Builder\Expectation; $client = new MockServerClient('http://127.0.0.1:1080'); $expectation = new Expectation(); $expectation->httpRequest() ->method('GET') ->path('/users/1') ; $expectation->httpResponse() ->bodyJson([ [ 'id' => 1, 'name' => 'Zidane', ], ]) ; $client->expectation($expectation);
配置示例
您可能想要配置项目,以便在测试环境中,外部 API 被替换为 MockServer。
例如,您可能拥有以下类型的配置:
.env
: 指向真实 API
USERS_API=https://users-api.local
.env.test
: 指向 MockServer 实例
USERS_API=http://127.0.0.1:1080
多个服务
如果您有多个 API 或微服务,并确保将请求发送到预期的服务,您可以使用以下方法之一:
自定义路径前缀
:.env
USERS_API=https://users-api.local
OTHER_API=https://other-api.com
:.env.test
USERS_API=http://127.0.0.1:1080/users-api/
OTHER_API=http://127.0.0.1:1080/other-api/
这样,在运行 Behat 测试时,您的应用程序将始终命中 mockserver,但使用不同的路径前缀。
然后,您的测试将如下所示:
Given the request "GET" "/users-api/users/1" will return the json: ... Given the request "GET" "/other-api/v1/something" will return the json: ...
自定义域名
上述解决方案的一个缺点是,如果您没有从一开始就处理此情况,并且始终假设您在根目录中调用 API,那么将基本主机与相对路径连接起来可能会很困难。
因此,在这种情况下,您可以使用不同的主机名。
:.env
USERS_API=https://users-api.local
OTHER_API=https://other-api.com
:.env.test
USERS_API=https://users-api.mockserver:1080
OTHER_API=https://other-api.mockserver:1080
如果您在主机名解析中做了必要的操作,使任何 *.mockserver
请求都命中您的 localhost,则您的 mockserver 实例将接收所有带有不同主机名的请求,您可以在预期中使用 httpRequest.headers[name = 'Host']
参数进行匹配。
常见问题解答
当我的应用程序尝试查询模拟的外部服务时,我仍然收到 404 错误。
对于您在应用程序和 mockserver 之间遇到的所有问题,您都应该使用 MockServer UI。
它已经安装并可在以下位置访问: http://127.0.0.1:1080/mockserver/dashboard。
如果不这样做,请参阅MockServer UI 文档。
UI 显示有用的信息
- 活动预期,收到的模拟
- 收到的请求,您的测试应用程序已发送到 mockserver 而不是真实的外部服务
- 日志消息,所有模拟、收到的请求,是否已发送模拟,如果没有,原因...
如果您收到 404 错误,您可能需要检查以下所有点:
- mockserver 是否已收到模拟?
在“活动预期”中,您必须看到在您的 Behat 测试中定义的模拟。如果不是这样,请检查 behat.yml
中的 mockserver URL 是否有效且正在使用。
- Lequipe\MockServer\Behat\MockServerContext: mockServer: 'http://127.0.0.1:1080'
- 应用程序是否调用 mockserver?
在“已接收请求”中,您必须看到您测试的应用程序发送的请求。如果没有,请检查请求是发送到哪里的。您应该配置应用程序使用mockserver作为基本主机而不是外部服务,类似于以下内容:
# in .env file, real service:
USERS_API=https://users-api.local
# .env.test file, mockserver:
USERS_API=https://127.0.0.1:1080
- 请求与mock匹配吗?
如果“活动期望”和“已接收请求”包含您的mock和接收到的请求,请求是否与mock匹配?
Mockserver可以包含多个mock,并且只有当路径、头信息等匹配时才会响应。
要检查这一点,在“日志消息”部分,您应该看到MATCHED_EXPECTATION
。如果没有,请检查接收到的请求的日志行,它会告诉您请求为什么没有匹配mock。
有时请求没有匹配只是因为路径中的斜杠(/)使用不当,例如是api/...
而不是/api/...
。
贡献
请参阅CONTRIBUTING.md。
许可
此库受MIT许可保护。