devizzent/codeception-mockserver-helper

Codeception 辅助工具,用于管理和验证 MockServer 期望

1.1.1 2023-06-29 20:24 UTC

README

这个库是 Codeception 的一个模块,允许您的测试以简单直观的方式与 mock-server(v5) 交互。

MockServer 允许您在测试/本地环境中模拟 http 服务,此助手允许您检查发送到 mock 服务器的所有请求并管理 mock 服务器的期望。

如何使用

//You can create expectations before call your application in a test
$I->createMockRequest('{"id": "elastic-get-entity-1", "httpRequest": {...}, "httpResponse": {...}}')
$I->createMockRequest('{"id": "elastic-get-entity-2", "httpRequest": {...}, "httpResponse": {...}}')
$I->createMockRequestFromJsonFile('/root/path/elastic-get-entity-3.json')
$I->sendGet('/applition/endpoint/1');
//After execute our application we can check our mocked HTTP communication
$I->seeMockRequestWasCalled('elastic-get-entity-1');
$I->seeMockRequestWasNotCalled('elastic-get-entity-2');
$I->seeAllRequestWereMatched();
$I->removeMockRequest("elastic-get-entity-2");
$I->removeAllMockRequest();

安装

使用以下命令安装最新版本

$ composer require devizzent/codeception-mockserver-helper --dev

Codeception 配置

此助手不是 Codeception 模块,不能在全局 codeception.yml 中配置,只能在 *.suite.yml 配置中配置。

转到您的 codeception 套件 配置并添加此模块

modules:
  enabled:
    - DEVizzent\CodeceptionMockServerHelper\MockServerHelper:
        ##Mandatory field, mock server url
        url: 'http://mockserver:1080' 
        ## Optional field, [test, suite, never] allowed. Default: test
        cleanupBefore: 'test'
        ## Optional field, [enabled, disabled] allowed. Default: enabled
        notMatchedRequest: 'enabled' 
        ## Optional field, path of the expectations folder or file to load before test
        expectationsPath: '/absolute/expectations/path' 

cleanupBefore

此变量设置我们想要清理 mockserver 日志的时间,以不影响其他测试或套件。我们建议将此值设置为默认值。

  • 'test' 将在开始任何测试之前删除所有日志,以确保您不受前一个测试执行中请求的影响。
  • 'suite' 将在开始测试套件之前删除所有日志。
  • 'never' 不会删除您的 mock-server 日志。它可能会在测试中引起混淆和失败,因为如果检查某物被调用 X 次,则每次运行测试时次数都会增加。如果您只检查一个请求是否被调用,则它可能在之前的执行中被调用,但错误地不在当前执行中。

notMatchedRequest

当此选项启用时,它会创建一个最低优先级的期望,以匹配所有未匹配我们期望的请求,返回一个带有消息 请求未由 MockServer 匹配 的 500 错误。

它允许我们验证我们的应用程序所做的所有请求都是预期的,并且我们没有更改与外部服务的通信。

expectationsPath

获取路径中的文件或文件,并将内容发送到 mockserver 创建期望。

关于

要求

Codecetion-MockServer Helper 需要 php 7.4 或更高版本以及 codeception 4 或更高版本。

如何贡献

创建一个描述错误或新功能的 issue,创建此项目的 fork 并发送您的 PR。

为了使用开发环境,您只需要 Docker,Docker-compose 和 Makefile。

Usage:
  make <target>

Targets:
  help                       Display this help
  install                    Install required software and initialize your local configuration
  up                         Start application containers and required services
  debug                      Start application containers and required services in debug mode
  down                       Stop application containers and required services
  test                       Execute all phpunit test
  composer-update            Run composer update
  composer-install           Run composer update

作者

Vicent Valls - vizzent@gmail.com - https://twitter.com/ViMalaBarrakahttps://www.youtube.com/@DEVizzent
请参阅参与此项目的贡献者名单。

许可

Codecetion-MockServer Helper 在 MIT 许可证下授权 - 有关详细信息,请参阅 LICENSE 文件