dvlpm/codeception-wiremock-extension

Codeception的WireMock扩展

0.1.0 2023-03-16 07:08 UTC

This package is auto-updated.

Last update: 2024-09-16 10:08:37 UTC


README

此Codeception扩展允许开发人员和测试人员在运行Codeception测试时使用WireMock来模拟外部服务。

codeception-wiremock-extension可以连接到已运行的WireMock实例,或者也可以自动运行一个本地的独立实例。它还可以下载您偏好的WireMock版本并运行它。测试完成后,它将关闭连接并关闭WireMock服务(如果它启动了)。

另请参阅

注意

如果您需要一个具有与WireMock相似功能且100%使用PHP的应用程序,请尝试Phiremock:[Phiremock](https://github.com/mcustiel/phiremock),它还有一个不错的[Codeception扩展](https://github.com/mcustiel/phiremock-codeception-extension)。

安装

Composer

该项目已发布在Packagist上,因此您只需将其添加到您的composer.json中的依赖项即可。

$ composer require lamoda/codeception-wiremock-extension

配置示例

模块

该模块允许您连接到WireMock实例,它可以是扩展运行的实例或已运行的实例。

# acceptance.suite.yml
modules:
    enabled:
        - WireMock:
            host: my.wiremock.host # defaults to 127.0.0.1
            port: 80 # defaults to 8080

扩展

默认配置

此配置将下载WireMock版本1.57,并在端口8080上运行,将日志写入Codeception测试_output目录。

# codeception.yml
extensions:
    enabled:
        - Codeception\Extension\WireMock    

连接到正在运行的WireMock实例

# codeception.yml
extensions:
    enabled:
        - Codeception\Extension\WireMock
    config:     
        Codeception\Extension\WireMock:
            host: my.wiremock.server
            port: 8080

启动本地WireMock实例,并使用给定的命令行参数运行它

# codeception.yml
extensions:
    enabled:
        - Codeception\Extension\WireMock
    config:     
        Codeception\Extension\WireMock:
            jar-path: /opt/wiremock/bin/wiremock-standalone.jar
            port: 18080
            https-port: 18443
            verbose: true
            root-dir: /opt/wiremock/root
            

下载WireMock实例,并使用给定的命令行参数运行它

# codeception.yml
extensions:
    enabled:
        - Codeception\Extension\WireMock
    config:     
        Codeception\Extension\WireMock:
            download-version: 1.57
            port: 18080
            https-port: 18443
            verbose: true
            root-dir: /opt/wiremock/root
            logs-path: /var/log/wiremock 

如何使用

准备您的应用程序

首先,配置您的应用程序,以便在测试时用WireMock替换其外部服务。例如,如果您向位于http://your.rest.interface的REST服务发送请求,请在配置中将该URL替换为WireMock运行的URL,例如:[https://:8080/rest_interface](https://:8080/rest_interface)。

编写您的测试

// YourCest.php
class YourCest extends \Codeception\TestCase\Test
{
    public function _after(\AcceptanceTester $I)
    {
        $I->cleanAllPreviousRequestsToWireMock();
    }

    // tests
    public function tryToTest(\AcceptanceTester $I)
    {
        $I->expectRequestToWireMock(
            WireMock::get(WireMock::urlEqualTo('/some/url'))
                ->willReturn(WireMock::aResponse()
                ->withHeader('Content-Type', 'text/plain')
                ->withBody('Hello world!'))
        );
        // Here you should execute your application in a way it requests wiremock. I do this directly to show it. 
        $response = file_get_contents('https://:18080/some/url');
        
        $I->assertEquals('Hello world!', $response);
        $I->receivedRequestInWireMock(
            WireMock::getRequestedFor(WireMock::urlEqualTo('/some/url'))
        );
    }
    
    // Also, you can access wiremock-php library directly
    public function moreComplexTest()
    {
        $wiremockPhp = Codeception\Extension\WiremockConnection::get();
        // Now you can use wiremock-php library
    }
}