lastdragon-ru /lara-asp-testing
Laravel 的强大测试辅助包集 - 测试助手。
Requires
- php: ^8.1|^8.2|^8.3
- ext-dom: *
- ext-json: *
- ext-libxml: *
- ext-mbstring: *
- ext-xmlreader: *
- composer/semver: ^3.2
- doctrine/sql-formatter: ^1.1
- http-interop/http-factory-guzzle: ^1.0.0
- illuminate/collections: ^10.34.0|^11.0.0
- illuminate/console: ^10.34.0|^11.0.0
- illuminate/container: ^10.34.0|^11.0.0
- illuminate/contracts: ^10.34.0|^11.0.0
- illuminate/database: ^10.34.0|^11.0.0
- illuminate/testing: ^10.34.0|^11.0.0
- illuminate/translation: ^10.34.0|^11.0.0
- mockery/mockery: ^1.6.2
- opis/json-schema: ^2.3.0
- phpunit/phpunit: ^10.1.0|^11.0.0
- psr/http-message: ^1.0.0|^2.0.0
- sebastian/comparator: ^5.0|^6.0.0
- sebastian/exporter: ^5.0|^6.0.0
- symfony/deprecation-contracts: ^3.0.0
- symfony/filesystem: ^6.3.0|^7.0.0
- symfony/http-foundation: ^6.3.0|^7.0.0
- symfony/mime: ^6.3.0|^7.0.0
- symfony/polyfill-php83: ^1.28
- symfony/psr-http-message-bridge: ^2.0.0|^6.4.0|^7.0.0
Requires (Dev)
- fakerphp/faker: ^1.21.0
- guzzlehttp/psr7: ^1.9.1|^2.4.5
- illuminate/http: ^10.34.0|^11.0.0
- laravel/scout: ^9.8.0|^10.0.0
- orchestra/testbench: ^8.0.0|^9.0.0
- symfony/console: ^6.3.0|^7.0.0
- symfony/http-kernel: ^6.3.0|^7.0.0
- dev-main
- 6.x-dev
- 6.4.2
- 6.4.1
- 6.4.0
- 6.3.0
- 6.2.0
- 6.1.0
- 6.0.0
- 5.x-dev
- 5.6.0
- 5.5.0
- 5.4.0
- 5.3.1
- 5.3.0
- 5.2.0
- 5.1.0
- 5.0.0
- 5.0.0-beta.1
- 5.0.0-beta.0
- 4.x-dev
- 4.6.0
- 4.5.2
- 4.5.1
- 4.5.0
- 4.4.0
- 4.3.0
- 4.2.1
- 4.2.0
- 4.1.0
- 4.0.0
- 3.0.0
- 2.x-dev
- 2.1.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.x-dev
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.15.0
- 0.14.1
- 0.14.0
- 0.13.0
- 0.12.0
- 0.11.0
- 0.10.0
- 0.9.0
- 0.8.1
- 0.8.0
- 0.7.0
- 0.6.1
- 0.6.0
- 0.5.0
- 0.4.0
- 0.3.0
- 0.2.0
- 0.1.0
- dev-gitattributes
This package is auto-updated.
Last update: 2024-09-26 07:31:08 UTC
README
此包为 PHPUnit 提供各种有用的断言,并提供了更好的 HTTP 测试解决方案 - 测试 HTTP 响应从未如此简单!这不仅关于 TestResponse
,还包括任何 PSR 响应 😎
需求
安装
注意
该包旨在在开发中使用。
composer require --dev lastdragon-ru/lara-asp-testing
使用方法
重要
默认情况下,包覆盖了标量比较器以使其严格!因此 assertEquals(true, 1)
将返回 false
。
一般情况下,您只需更新 tests/TestCase.php
以包括最重要的内容,但您也可以仅包含所需的功能,请参阅以下相关的特性和扩展。
<?php declare(strict_types = 1); namespace Tests; use Illuminate\Contracts\Foundation\Application; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; use LastDragon_ru\LaraASP\Testing\Assertions\Assertions; use LastDragon_ru\LaraASP\Testing\Concerns\Concerns; use Override; abstract class TestCase extends BaseTestCase { use Assertions; // Added use Concerns; // Added use CreatesApplication; #[Override] protected function app(): Application { return $this->app; } }
比较器
提示
应在测试之前注册,请检查/使用 内置特性。
DatabaseQueryComparator
比较两个 Query
。
在比较之前,我们执行以下归一化操作以更精确
- 重新编号
laravel_reserved_*
(它将始终从0
开始且不包含间隔) - 使用
doctrine/sql-formatter
包格式化查询
EloquentModelComparator
比较两个 Eloquent 模型。
问题在于从工厂创建和从数据库中选择后的模型可能具有相同属性的不同的类型。例如,factory()->create()
将 key
设置为 int
,但 select
将它设置为 string
,并且(严格的)比较将失败。此比较器在比较之前将属性类型进行归一化。
ScalarStrictComparator
使标量的比较严格。
扩展
PHPUnit TestCase
RefreshDatabaseIfEmpty
💀
该特性与标准的 \Illuminate\Foundation\Testing\RefreshDatabase
非常相似,但有一个区别:它仅当数据库为空时才刷新数据库。这对于本地测试非常有用,并可以显着减少引导时间。
<?php declare(strict_types = 1); namespace Tests; use LastDragon_ru\LaraASP\Testing\Database\RefreshDatabaseIfEmpty; use LastDragon_ru\LaraASP\Testing\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { use CreatesApplication; use RefreshDatabaseIfEmpty; protected function shouldSeed() { return true; } }
WithTempDirectory
允许创建临时目录。脚本关闭后将自动删除该目录。
WithTempFile
允许创建临时文件。脚本关闭后将自动删除该文件。
WithTestData
允许获取 TestData
(加载与测试相关数据的辅助工具)的实例
Laravel TestCase
WithTranslations
允许替换 Laravel 的翻译字符串。
Override
类似于 \Illuminate\Foundation\Testing\Concerns\InteractsWithContainer
,但如果测试期间未使用覆盖,则将测试标记为失败(这有助于查找未使用的代码)。
Eloquent Model Factory
FixRecentlyCreated
创建模型后,将具有 wasRecentlyCreated = true
,在大多数情况下这不是期望的行为,此特性修复了它。
WithoutModelEvents
在创建/制作期间禁用模型事件。
混入
\Illuminate\Testing\TestResponse
断言
assertDatabaseQueryEquals
断言 SQL 查询等于 SQL 查询。
阅读更多.
assertJsonMatchesSchema
断言JSON符合模式。基于Opis JSON Schema包的验证。
阅读更多.
assertPsrResponse
断言PSR响应满足给定的约束(我们有很多内置的约束和响应,但当然,您可以创建自定义的)。
阅读更多.
assertQueryLogEquals
断言QueryLog
等于QueryLog
。
阅读更多.
assertScheduled
断言计划包含任务。
阅读更多.
assertScoutQueryEquals
断言Scout查询等于Scout查询。
阅读更多.
assertThatResponse
💀
断言PSR响应满足给定的约束(我们有很多内置的约束和响应,但当然,您可以创建自定义的)。
阅读更多.
assertXmlMatchesSchema
断言XML符合模式XSD或Relax NG。基于DOMDocument
类的标准方法进行验证。
阅读更多.
Laravel响应测试
Laravel方法有什么问题?嗯,有两个大问题。
错误在哪里?
您永远不知道测试失败的原因,需要调试以找到原因。现实生活中的例子
<?php declare(strict_types = 1); namespace App\Http\Controllers; use PHPUnit\Framework\Attributes\CoversClass; use Tests\TestCase; /** * @internal */ #[CoversClass(IndexController::class)] class IndexControllerTest extends TestCase { public function testIndex() { $this->get('/') ->assertOk() ->assertHeader('Content-Type', 'application/json'); } }
assertOk()失败
Testing started at 15:46 ...
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
Random Seed: 1610451974
Expected status code 200 but received 500.
Failed asserting that 200 is identical to 500.
vendor/laravel/framework/src/Illuminate/Testing/TestResponse.php:186
app/Http/Controllers/IndexControllerTest.php:16
Time: 00:01.373, Memory: 26.00 MB
assertHeader()失败
Testing started at 17:57 ...
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
Random Seed: 1610459878
Header [Content-Type] was found, but value [text/html; charset=UTF-8] does not match [application/json].
Failed asserting that two values are equal.
Expected :'application/json'
Actual :'text/html; charset=UTF-8'
<Click to see difference>
vendor/laravel/framework/src/Illuminate/Testing/TestResponse.php:229
app/Http/Controllers/IndexControllerTest.php:18
Time: 00:01.082, Memory: 24.00 MB
FAILURES!
Tests: 1, Assertions: 3, Failures: 1.
Process finished with exit code 1
期望状态码200但收到500。
嗯,500,这可能是PHP错误?为什么?在哪里? 😰
与
<?php declare(strict_types = 1); namespace App\Http\Controllers; use LastDragon_ru\LaraASP\Testing\Constraints\Response\ContentTypes\JsonContentType; use LastDragon_ru\LaraASP\Testing\Constraints\Response\Response; use LastDragon_ru\LaraASP\Testing\Constraints\Response\StatusCodes\Ok; use PHPUnit\Framework\Attributes\CoversClass; use Tests\TestCase; /** * @internal */ #[CoversClass(IndexController::class)] class IndexControllerTest extends TestCase { public function testIndex() { $this->get('/')->assertThat(new Response( new Ok(), new JsonContentType() )); } }
assertThat()失败
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
Random Seed: 1610461475
Failed asserting that GuzzleHttp\Psr7\Response Object &000000001ef973410000000013328b0b (
'reasonPhrase' => 'Internal Server Error'
'statusCode' => 500
'headers' => Array &0 (
'cache-control' => Array &1 (
0 => 'no-cache, private'
)
'date' => Array &2 (
0 => 'Tue, 12 Jan 2021 14:24:36 GMT'
)
'content-type' => Array &3 (
0 => 'text/html; charset=UTF-8'
)
)
'headerNames' => Array &5 (
'cache-control' => 'cache-control'
'date' => 'date'
'content-type' => 'content-type'
'set-cookie' => 'Set-Cookie'
)
'protocol' => '1.1'
'stream' => GuzzleHttp\Psr7\Stream Object &000000001ef972d20000000013328b0b (
'stream' => resource(846) of type (stream)
'size' => null
'seekable' => true
'readable' => true
'writable' => true
'uri' => 'php://temp'
'customMetadata' => Array &6 ()
)
) has Status Code is equal to 200.
<!doctype html>
<html class="theme-light">
<!--
Error: Call to undefined function App\Http\Controllers\dview() in file app/Http/Controllers/IndexController.php on line 7
#0 vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): App\Http\Controllers\IndexController->index()
#1 vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\Routing\Controller->callAction()
#2 vendor/laravel/framework/src/Illuminate/Routing/Route.php(254): Illuminate\Routing\ControllerDispatcher->dispatch()
#3 vendor/laravel/framework/src/Illuminate/Routing/Route.php(197): Illuminate\Routing\Route->runController()
#4 vendor/laravel/framework/src/Illuminate/Routing/Router.php(692): Illuminate\Routing\Route->run()
#5 vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Routing\Router->Illuminate\Routing\{closure}()
#6 vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#7 vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Routing\Middleware\SubstituteBindings->handle()
#8 vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(78): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
...
Time: 00:01.356, Memory: 28.00 MB
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
Process finished with exit code 1
重用测试代码有问题
在大多数实际应用中,您有多种角色(例如guest
、user
、admin
)、守卫和政策。很难测试所有这些,并且通常需要创建许多带有大量样板代码的testRouteIsNotAvailableForGuest()
、testRouteIsAvailableForAdminOnly()
等测试。此外,通常无法重用这些(样板)代码,必须一次又一次地编写它们。这真的很烦人。
解决此问题非常简单。首先,我们需要为所需的响应创建类(实际上包已经提供了一些最常用的响应 🙄)。让我们从一个简单的JSON响应开始
<?php declare(strict_types = 1); namespace Tests\Responses; use LastDragon_ru\LaraASP\Testing\Constraints\Response\ContentTypes\JsonContentType; use LastDragon_ru\LaraASP\Testing\Constraints\Response\Response; use LastDragon_ru\LaraASP\Testing\Constraints\Response\StatusCodes\Ok; class JsonResponse extends Response { public function __construct() { parent::__construct( new Ok(), new JsonContentType(), ); } }
接下来,让我们添加JSON验证错误
<?php declare(strict_types = 1); namespace Tests\Responses; use LastDragon_ru\LaraASP\Testing\Constraints\Json\JsonMatchesSchema; use LastDragon_ru\LaraASP\Testing\Constraints\Json\JsonSchema; use LastDragon_ru\LaraASP\Testing\Constraints\Response\Body; use LastDragon_ru\LaraASP\Testing\Constraints\Response\ContentTypes\JsonContentType; use LastDragon_ru\LaraASP\Testing\Constraints\Response\Response; use LastDragon_ru\LaraASP\Testing\Constraints\Response\StatusCodes\UnprocessableEntity; use LastDragon_ru\LaraASP\Testing\Utils\WithTestData; class ValidationErrorResponse extends Response { use WithTestData; public function __construct() { parent::__construct( new UnprocessableEntity(), new JsonContentType(), new Body([ new JsonMatchesSchema(new JsonSchema(self::getTestData(self::class)->file('.json'))), ]), ); } }
最后,进行测试
<?php declare(strict_types = 1); namespace App\Http\Controllers; use PHPUnit\Framework\Attributes\CoversClass; use Tests\Responses; use Tests\TestCase; /** * @internal */ #[CoversClass(IndexController::class)] class IndexControllerTest extends TestCase { public function testIndex() { $this->getJson('/')->assertThat(new ValidationErrorResponse()); } public function testTest() { $this->getJson('/test')->assertThat(new ValidationErrorResponse()); } }
具有默认断言的相同测试可能看起来像这样
<?php declare(strict_types = 1); namespace App\Http\Controllers; use PHPUnit\Framework\Attributes\CoversClass; use Tests\TestCase; /** * @internal */ #[CoversClass(IndexController::class)] class IndexControllerTest extends TestCase { public function testIndex() { $this->getJson('/') ->assertStatus(422) ->assertHeader('Content-Type', 'application/json') ->assertJsonStructure([ 'message', 'errors', ]); } public function testTest() { $this->getJson('/test') ->assertStatus(422) ->assertHeader('Content-Type', 'application/json') ->assertJsonStructure([ 'message', 'errors', ]);; } }
感受一下差异 😉
PSR响应测试
内部包使用PSR-7
,因此您可以测试任何Psr\Http\Message\ResponseInterface
🤩
<?php declare(strict_types = 1); use LastDragon_ru\LaraASP\Testing\Assertions\ResponseAssertions; use LastDragon_ru\LaraASP\Testing\Constraints\Response\ContentTypes\JsonContentType; use LastDragon_ru\LaraASP\Testing\Constraints\Response\Response; use LastDragon_ru\LaraASP\Testing\Constraints\Response\StatusCodes\Ok; use PHPUnit\Framework\TestCase; class ResponseInterfaceTest extends TestCase { use ResponseAssertions; public function testResponse() { /** @var \Psr\Http\Message\ResponseInterface $response */ $response = null; self::assertThatResponse($response, new Response( new Ok(), new JsonContentType(), )); } }
数据提供者的强化版
还有另一个酷炫的功能,允许我们在不重复代码的情况下测试大量用例——这是CompositeDataProvider
。它以以下方式合并多个提供者
Providers:
[
['expected a', 'value a'],
['expected final', 'value final'],
]
[
['expected b', 'value b'],
['expected c', 'value c'],
]
[
['expected d', 'value d'],
['expected e', 'value e'],
]
Merged:
[
'0 / 0 / 0' => ['expected d', 'value a', 'value b', 'value d'],
'0 / 0 / 1' => ['expected e', 'value a', 'value b', 'value e'],
'0 / 1 / 0' => ['expected d', 'value a', 'value c', 'value d'],
'0 / 1 / 1' => ['expected e', 'value a', 'value c', 'value e'],
'1' => ['expected final', 'value final'],
]
因此,我们可以这样组织我们的测试
<?php declare(strict_types = 1); namespace Tests\Feature; use App\Models\User; use Closure; use Illuminate\Http\Request; use Illuminate\Routing\Middleware\SubstituteBindings; use Illuminate\Support\Facades\Route; use LastDragon_ru\LaraASP\Testing\Constraints\Response\Response; use LastDragon_ru\LaraASP\Testing\Constraints\Response\StatusCodes\NotFound; use LastDragon_ru\LaraASP\Testing\Constraints\Response\StatusCodes\Ok; use LastDragon_ru\LaraASP\Testing\Constraints\Response\StatusCodes\Unauthorized; use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\CompositeDataProvider; use LastDragon_ru\LaraASP\Testing\Providers\DataProvider as DataProviderContract; use LastDragon_ru\LaraASP\Testing\Providers\ExpectedFinal; use LastDragon_ru\LaraASP\Testing\Responses\Laravel\Json\ValidationErrorResponse; use PHPUnit\Framework\Attributes\DataProvider;use Tests\TestCase; class ExampleTest extends TestCase { // <editor-fold desc="Prepare"> // ========================================================================= public function setUp(): void { parent::setUp(); Route::get('/users/{user}', function (User $user) { return $user->email; })->middleware(['auth', SubstituteBindings::class]); Route::post('/users/{user}', function (Request $request, User $user) { $user->email = $request->validate([ 'email' => 'required|email', ]); return $user->email; })->middleware(['auth', SubstituteBindings::class]); } // </editor-fold> // <editor-fold desc="Tests"> // ========================================================================= #[DataProvider('dataProviderGet')] public function testGet(Response $expected, Closure $actingAs = null, Closure $user = null): void { $user = $user ? $user()->getKey() : 0; if ($actingAs) { $this->actingAs($actingAs()); } $this->getJson("/users/{$user}")->assertThat($expected); } #[DataProvider('dataProviderUpdate')] public function testUpdate(Response $expected, Closure $actingAs = null, Closure $user = null, array $data = []) { $user = $user ? $user()->getKey() : 0; if ($actingAs) { $this->actingAs($actingAs()); } $this->postJson("/users/{$user}", $data)->assertThat($expected); } // </editor-fold> // <editor-fold desc="DataProvider"> // ========================================================================= public static function dataProviderGet(): array { return (new CompositeDataProvider( self::getUserDataProvider(), self::getModelDataProvider(), ))->getData(); } public static function dataProviderUpdate(): array { return (new CompositeDataProvider( self::getUserDataProvider(), self::getModelDataProvider(), new ArrayDataProvider([ 'no email' => [ new ValidationErrorResponse(['email' => null]), [], ], 'invalid email' => [ new ValidationErrorResponse([ 'email' => 'The email must be a valid email address.', ]), [ 'email' => '123', ], ], 'valid email' => [ new Ok(), [ 'email' => 'test@example.com', ], ], ]) ))->getData(); } // </editor-fold> // <editor-fold desc="Shared"> // ========================================================================= protected static function getUserDataProvider(): DataProviderContract { return new ArrayDataProvider([ 'guest' => [ new ExpectedFinal(new Unauthorized()), null, ], 'authenticated' => [ new Ok(), function () { return User::factory()->create(); }, ], ]); } protected static function getModelDataProvider(): DataProviderContract { return new ArrayDataProvider([ 'user not exists' => [ new ExpectedFinal(new NotFound()), null, ], 'user exists' => [ new Ok(), function () { return User::factory()->create(); }, ], ]); } // </editor-fold> }
享受 😸
模拟属性(Mockery)🧪
重要
关于如何模拟受保护的属性?(#1142)的工作原型。请注意,实现依赖于反射和内部Mockery方法和属性。
限制/注意事项
- 只读属性应未初始化。
- 不支持私有属性。
- 属性值必须是一个对象。
- 属性必须在测试中使用。
- 属性只能模拟一次。
- 没有方法的对象将被标记为未使用。
<?php declare(strict_types = 1); // phpcs:disable PSR1.Files.SideEffects // phpcs:disable PSR1.Classes.ClassDeclaration namespace LastDragon_ru\LaraASP\Testing\Docs\Examples\MockProperties; use LastDragon_ru\LaraASP\Testing\Mockery\MockProperties; use Mockery; class A { public function __construct( protected readonly B $b, ) { // empty } public function a(): void { $this->b->b(); } } class B { public function b(): void { echo 1; } } $mock = Mockery::mock(A::class, MockProperties::class); $mock ->shouldUseProperty('b') ->value( Mockery::mock(B::class), // or just `new B()`. ); $mock->a();
自定义测试需求
不幸的是,PHPUnit不允许添加/扩展现有需求,可能也不会。
我认为不应该添加测试需求的附加属性。毕竟,现有的属性只是方便的语法糖。只需在测试之前的方法中检查您的自定义需求,并在它们不满足时调用
markTestSkipped()
即可。© @sebastianbergmann
该插件监听多个事件,并检查实现了Requirement
接口的测试类/方法的所有属性。如果要求不满足,测试将被标记为跳过。请注意,无论如何至少会执行一个“before”钩子(PHPUnit在钩子执行后发出事件)。
首先需要注册插件。
<extensions> <bootstrap class="LastDragon_ru\LaraASP\Testing\Requirements\PhpUnit\Extension"/> </extensions>
然后
<?php declare(strict_types = 1); use LastDragon_ru\LaraASP\Testing\Requirements\Requirements\RequiresComposerPackage; use PHPUnit\Framework\TestCase; class SomePackageTest extends TestCase { #[RequiresComposerPackage('some/package')] public function testSomePackage(): void { // ..... } }
升级
请遵循升级指南。
贡献
此包是Laravel的Awesome Set of Packages的一部分。请使用主仓库来报告问题、发送pull请求或提问。