jonpurvis/lawman

一个用于帮助进行架构测试 SaloonPHP 集成的 PestPHP 插件

v3.1.0 2024-09-16 02:13 UTC

This package is auto-updated.

Last update: 2024-09-16 02:13:36 UTC


README

Lawman - SaloonPHP 的架构执行者

SaloonPHP 的 PestPHP 插件,帮助您通过 API 集成强制执行架构规则。

Static Analysis Tests GitHub last commit Packagist PHP Version GitHub issues GitHub Packagist Downloads

简介

Lawman 是一个 SaloonPHP 的 PestPHP 插件,允许您轻松编写与 API 集成的架构测试,重点是编写和阅读的简便性。毕竟,如果 SaloonPHP 让我们的 API 集成变得美观,那么这些集成对应的测试也应该是美观的,对吧?

Lawman 使得从架构的角度测试 SaloonPHP API 集成变得容易得多。让您可以一目了然地看到测试实际上在做什么。Lawman 是一个 PestPHP 插件,这意味着您可以使用 Lawman 预期和 PestPHP 预期一起使用,只需为您的测试链出所需的一切即可!

示例

让我们看看 Lawman 如何帮助简化测试编写。

假设我们有一个想要测试的 Connector 类,使用 PestPHP,我们可以这样做

test('connector')
    ->expect('App\Http\Integrations\Integration\Connector')
    ->toExtend('Saloon\Http\Connector')
    ->toUse('Saloon\Traits\Plugins\AcceptsJson')
    ->toUse('Saloon\Traits\Plugins\AlwaysThrowOnErrors');

因此,这个测试确保我们的类扩展了基础 Connector 类,并使用了 AcceptJsonAlwaysThrowOnErrors 特性。虽然这个测试是有效的,但我们可能让它更快地编写和更容易阅读,所以使用 Lawman,您可以这样做

test('connector')
    ->expect('App\Http\Integrations\Integration\Connector')
    ->toBeSaloonConnector()
    ->toUseAcceptsJsonTrait()
    ->toUseAlwaysThrowOnErrorsTrait();

接下来,让我们看看我们已有的一个 Request 测试

test('request')
    ->expect('App\Http\Integrations\Integration\Requests\Request')
    ->toExtend('\Saloon\Http\Request')
    ->toImplement('Saloon\Contracts\Body\HasBody')
    ->toUse('Saloon\Traits\Body\HasFormBody')
    ->toUse('Saloon\Traits\Plugins\AcceptsJson');

Lawman 使这个测试更容易阅读

test('request')
    ->expect('App\Http\Integrations\Integration\Requests\Request')
    ->toBeSaloonRequest()
    ->toSendPostRequest()
    ->toHaveFormBody()
    ->toUseAcceptsJsonTrait();

如果我们想测试我们的 Connector 有一个认证方法呢?Lawman 使这变得简单,它甚至支持多认证

test('connector')
    ->expect('App\Http\Integrations\Integration\Connector')
    ->toBeSaloonConnector()
    ->toUseCertificateAuthentication()
    ->toUseTokenAuthentication();

Lawman 还为 Pagination、Cache 和 Rate Limit 插件提供了预期

test('request')
    ->expect('App\Http\Integrations\Integration\Requests\Request')
    ->toBeSaloonRequest()
    ->toSendPostRequest()
    ->toUsePagedPagination()
    ->toHaveCaching()
    ->toHaveRateLimits()

也许我们的 Connector 有一些我们想要测试的重试指令。再次,使用 Lawman,它就像这样简单

test('connector')
    ->expect('App\Http\Integrations\Integration\Connector')
    ->toBeSaloonConnector()
    ->toBeTriedAgainOnFailure()
    ->toHaveRetryInterval()
    ->toUseExponentialBackoff()

贡献

非常欢迎对包的贡献,如果您想到了一个希望看到的预期,请随时提交一个 Pull Request 或打开一个 Issue。如果您提交了一个 Pull Request,请确保您为您的预期添加了一个新的固定装置并对其进行了测试。

有用链接