aldidigitalservices / php-mock-collector
一个简单的PHP Mock服务器,它使您能够获取发送到Mock服务器的请求(例如,用于测试)
Requires
- php: ~7.4 || ^8.0
- guzzlehttp/guzzle: ^7.0
- guzzlehttp/psr7: ^2.0
- nyholm/psr7: ^1.4
- symfony/http-foundation: ^5.2
- symfony/psr-http-message-bridge: ^2.1
Requires (Dev)
- phpunit/phpunit: ^9
This package is auto-updated.
Last update: 2024-09-23 12:38:10 UTC
README
## 使用方法:使用composer安装phpMockServer
composer require aldidigitalservices/php-mock-collector
创建phpMockServer的新实例并运行run方法
require_once 'vendor/autoload.php'; $ms = new \ALDIDigitalServices\pms\phpMockServer(__DIR__."/mocks"); $ms->run();
之后,您可以在mock文件夹中定义mocks(默认情况下,它位于phpMockServer.php文件的../mocks位置,但可以通过构造函数进行配置)
如果您想为/hello/world创建mock,请创建/hello/world文件夹并在其中放置一个mock.json文件。
在这个json文件中,您可以定义不同的方法(GET、POST、PUT等)并定义静态响应。
如果您需要更灵活,可以定义一个实现customCallbackInterface的自定义callback类,并将其放置在以您的类名命名的mock文件夹中,并在mock.json中添加customCallback设置(参见/hellophp/mock.json)
## 配置Mock您可以通过以下选项来配置mock
body: could be a string or an array. The array would be json encoded
header: is a array of headers that should be send to the client (key => value)
httpcode: The responecode that is returned by the mock. e.g. 404
latency: This latency is added to the execution time. (Value in seconds)
customCallback: the Classname of the customCallback that should be used to create the response. This option is exclusiv so the other options ( exept rules will not apply)
ProxyPath: A URL to proxy the request to
rules: An array of rules that has to apply for this response. See rules
除此之外,您还可以定义通配符路由。为此,您必须将方法配置放置在包含具有正则表达式path元素的object中
{ "path": [{ "route": "{{regex}}", "GET": [ { ... } ] }] }
## 规则您可以通过以下方式配置规则
param: is a list of key values that has to be set. Value can be *.
bodyregex: a list of regex that would be performed on the body content
preselection: you can define a preseletion upfront and check with this rule if the preselection match the value that was set by preselection
您可以通过实现ruleImplementationInterface来实现额外的规则。
## 集成 ## Codeception您可以在codeception测试中使用Mock请求,通过将MockAwaitModule添加到您的测试套件中
actor: ApiTester modules: enabled: - \Helper\Api - REST: url: https://jsonplaceholder.typicode.com/ depends: PhpBrowser - \ALDIDigitalServices\pms\integrations\codeception\MockAwait
在外部测试中包含有效负载验证
对于发送到mock服务器的每个请求,您都可以使用clientDemo.php中的awaitcall函数等待有效负载和附加数据(这必须转换为codeception模块/辅助工具)。您可以定义一个路径、一个方法(GET、POST、PUT等)以及等待响应的超时时间。