packico/hoverphp

Hoverfly 的 PHP 库

0.0.3 2021-05-10 14:31 UTC

This package is auto-updated.

Last update: 2024-09-10 22:06:22 UTC


README

Author Source Code Software License ci php min/max+

一个小型 PHP 库,用于与 Hoverfly 交互。

注意:这个库处于非常早期的阶段,其 API 可能会发生变化。

动机

Docker 提供了一种轻松创建集成测试的方法。然而,它并不总是能解决问题。假设你想对 AWS SDK 进行集成测试,或者对已经包含大量数据点的 Prometheus 实例进行测试,或者对你的应用程序进行测试,该应用程序可以轻松地用于容器中。在这些场景中,Docker 镜像并不能解决问题,这就是 Hoverfly 发挥作用的地方,它允许你轻松地捕获和模拟 HTTP 响应,同时充当代理和 Web 服务器。

尽管 hoverctl 命令行应用程序易于使用,但它并没有集成到 PHP 生态系统,因此迫使你进行一些繁琐的编排才能将测试与模拟定义放在一起。

幸运的是,它有一个出色的 REST API。HoverPHP 是一个用于在测试用例中管理该 REST API 的微型 SDK。

为了方便,它能够通过实现 PSR-7: HTTP 消息接口 的类来定义模拟。

目前,它只处理与设置模拟相关的功能,这是创建这个库的主要动机。但是,如果需要,它可以很容易地进行扩展。请随意为此提交一个问题。

安装

通过 Composer

composer require pachico/hoverphp

用法

参考 examples/folder

简单用例

你的集成测试想要确保你的应用程序 SuperApp 成功地与服务 SuperService 进行通信。

这是一个测试用例可能的样子

class MyAwesomeIntegrationTest extends TestCase
{
    /**
     * This test will make sure HTTP Repository "SuperHTTPRepo"
     * Communicates with SuperService without mocking the HTTP client
     * but by using a simulation in
     */
    public function testSuperAppDoesMagicWithSuperService()
    {
        //++ Arrange

        $hClient = new Client('http://myhoverflyhost:8888');

        // I am instantiating this Guzzle Client by changing its base_uri pointing
        // to Hoverfly's hostname and its webserver port.
        // Alternatively, you could use Hoverfly proxy to serve simulations
        $httpClient = new GuzzleHttpClient(['base_uri' => 'http://myhoverflyhost:8888']);

        // I pass this HTTP client to my HTTP Repo
        $superRepo = new SuperHTTPRepo($httpClient);

        // Make sure Hoverfly is set in simulation mode
        $hClient->setMode(Client::MODE_SIMULATION);

        // And set simulation in Hoverfly from within the test
        $hClient->setSimulation(Simulation::new()->withPair(
            Request::fromPSR7(
                new Psr7Request('GET', '/superapi/foo', ['Content-Type' => 'application/json'])
            ),
            Response::fromPSR7(
                new Psr7Response(200, ['Content-Type' => 'application/json'], '{"bar": "true"')
            )
        ));

        //++ Act

        // Now your repo can do its work by triggering a real HTTP request to simulated service
        $returnedValue = $superRepo->doMyAwesomeWork();

        //++ Assert

        // Finally, you can do your assertions
        $this->assertTrue($returnedValue);

        //++ Clean UP
        // Either here or in TearDown(), you might want to clear simulations with
        $hClient->deleteSimulation();
    }
}

导出模拟

这是一个展示如何导出模拟的例子

// This is a working example. Start hoverfly with docker-compose by typing `make up` in root folder

// Let's create our client and point it to Hoverfly's location
$hClient = Client::new('http://localhost:8888');

// Enable capture mode
$hClient->setMode(Client::MODE_CAPTURE);

// Set the simulation in hoverfly
$hClient->setSimulation(
    H\Simulation::new()->withPair(
        H\Request::new()->withDestinationMatcher(Matcher::GLOB, '*'),
        H\Response::new(200, 'My awesome result')
    )
);

// Export it to JSON string
$jsonSimulation = $hClient->exportSimulation();

贡献

请参阅 CONTRIBUTINGCODE_OF_CONDUCT 以获取详细信息。

安全

如果你发现任何与安全相关的问题,请通过电子邮件 pachicodev@gmail.com 而不是使用问题跟踪器。

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件