跳到主要内容
版本:3.0

测试

概述

所有本指南内的示例都会使用 Pest 编写。不过,你可以将其适配到 PHPUnit。

由于应用中的所有页面都是 Livewire 组件,我们始终都在使用 Livewire 测试辅助函数。如果你之前从未测试过 Livewire 组件,请阅读 Livewire 文档下的这一向导

开始

确保你在测试案例(TestCase)中有权限访问应用:

protected function setUp(): void
{
parent::setUp();

$this->actingAs(User::factory()->create());
}

资源

页面

列表

路由 & 渲染

要确保 PostResource 的列表页渲染成功,需生成页面 URL、发送请求到该 URL 并检查是否成功:

it('can render page', function () {
$this->get(PostResource::getUrl('index'))->assertSuccessful();
});
表格

Filament 内含一系列辅助函数用来测试表格。表格测试全指南可查阅表格构造器文档

要使用表格测试辅助函数,在持有该表格的资源列表类中使用断言:

use function Pest\Livewire\livewire;

it('can list posts', function () {
$posts = Post::factory()->count(10)->create();

livewire(PostResource\Pages\ListPosts::class)
->assertCanSeeTableRecords($posts);
});

新建

路由 & 渲染

检查 PostResource 的新建页是否可以成功渲染,生成页面 URL,发起请求到该地址,检测是否成功:

it('can render page', function () {
$this->get(PostResource::getUrl('create'))->assertSuccessful();
});
创建

你可使用表单数据调用 fillForm() 检查存入到数据库的数据时候正确,并断言数据库中是否包含一条匹配的记录:

use function Pest\Livewire\livewire;

it('can create', function () {
$newData = Post::factory()->make();

livewire(PostResource\Pages\CreatePost::class)
->fillForm([
'author_id' => $newData->author->getKey(),
'content' => $newData->content,
'tags' => $newData->tags,
'title' => $newData->title,
])
->call('create')
->assertHasNoFormErrors();

$this->assertDatabaseHas(Post::class, [
'author_id' => $newData->author->getKey(),
'content' => $newData->content,
'tags' => json_encode($newData->tags),
'title' => $newData->title,
]);
});
验证

使用 assertHasFormErrors() 检查表单中的数据验证是否正确:

use function Pest\Livewire\livewire;

it('can validate input', function () {
$newData = Post::factory()->make();

livewire(PostResource\Pages\CreatePost::class)
->fillForm([
'title' => null,
])
->call('create')
->assertHasFormErrors(['title' => 'required']);
});

编辑

路由 & 渲染

检查 PostResource 的编辑页是否可以成功渲染,生成页面 URL,发起请求到该地址,检测是否成功:

it('can render page', function () {
$this->get(PostResource::getUrl('edit', [
'record' => Post::factory()->create(),
]))->assertSuccessful();
});
填充已有数据

检测当前表单是否正确填充了数据库的数据,你可以使用 assertFormSet() 断言表单中的数据是否匹配数据库记录:

use function Pest\Livewire\livewire;

it('can retrieve data', function () {
$post = Post::factory()->create();

livewire(PostResource\Pages\EditPost::class, [
'record' => $post->getRouteKey(),
])
->assertFormSet([
'author_id' => $post->author->getKey(),
'content' => $post->content,
'tags' => $post->tags,
'title' => $post->title,
]);
});
保存

你可以使用表单数据调用 fillForm(),检查数据是否正确存入数据库,并断言数据库中是否包含匹配的记录:

use function Pest\Livewire\livewire;

it('can save', function () {
$post = Post::factory()->create();
$newData = Post::factory()->make();

livewire(PostResource\Pages\EditPost::class, [
'record' => $post->getRouteKey(),
])
->fillForm([
'author_id' => $newData->author->getKey(),
'content' => $newData->content,
'tags' => $newData->tags,
'title' => $newData->title,
])
->call('save')
->assertHasNoFormErrors();

expect($post->refresh())
->author_id->toBe($newData->author->getKey())
->content->toBe($newData->content)
->tags->toBe($newData->tags)
->title->toBe($newData->title);
});
验证

使用 assertHasFormErrors() 检测表单中的数据是否被正确验证:

use function Pest\Livewire\livewire;

it('can validate input', function () {
$post = Post::factory()->create();
$newData = Post::factory()->make();

livewire(PostResource\Pages\EditPost::class, [
'record' => $post->getRouteKey(),
])
->fillForm([
'title' => null,
])
->call('save')
->assertHasFormErrors(['title' => 'required']);
});
删除

你可以使用 callPageAction() 测试 DeleteAction

use Filament\Actions\DeleteAction;
use function Pest\Livewire\livewire;

it('can delete', function () {
$post = Post::factory()->create();

livewire(PostResource\Pages\EditPost::class, [
'record' => $post->getRouteKey(),
])
->callAction(DeleteAction::class);

$this->assertModelMissing($post);
});

你可以使用 assertPageActionHidden() 断言 DeleteAction 对特定用户隐藏:

use Filament\Actions\DeleteAction;
use function Pest\Livewire\livewire;

it('can not delete', function () {
$post = Post::factory()->create();

livewire(PostResource\Pages\EditPost::class, [
'record' => $post->getRouteKey(),
])
->assertActionHidden(DeleteAction::class);
});

查看

路由 & 渲染

检查 PostResource 的查看页是否可以成功渲染,生成页面 URL,发起请求到该地址,检测是否成功:

it('can render page', function () {
$this->get(PostResource::getUrl('view', [
'record' => Post::factory()->create(),
]))->assertSuccessful();
});
填充已有数据

检测当前表单是否正确填充了数据库的数据,你可以使用 assertFormSet() 断言表单中的数据是否匹配数据库记录:

use function Pest\Livewire\livewire;

it('can retrieve data', function () {
$post = Post::factory()->create();

livewire(PostResource\Pages\ViewPost::class, [
'record' => $post->getRouteKey(),
])
->assertFormSet([
'author_id' => $post->author->getKey(),
'content' => $post->content,
'tags' => $post->tags,
'title' => $post->title,
]);
});

关联管理器

渲染

要检测关联管理器是否渲染成功,挂载 Livewire 组件:

use App\Filament\Resources\CategoryResource\Pages\EditCategory;
use function Pest\Livewire\livewire;

it('can render relation manager', function () {
$category = Category::factory()
->has(Post::factory()->count(10))
->create();

livewire(CategoryResource\RelationManagers\PostsRelationManager::class, [
'ownerRecord' => $category,
'pageClass' => EditCategory::class,
])
->assertSuccessful();
});
表格

Filament 内含一系列辅助函数用于测试表格。表格测试全指南可查阅表格构造器文档

要使用表格测试辅助函数,在持有该表格的关联管理器类中使用断言:

use function Pest\Livewire\livewire;

it('can list posts', function () {
$category = Category::factory()
->has(Post::factory()->count(10))
->create();

livewire(CategoryResource\RelationManagers\PostsRelationManager::class, [
'ownerRecord' => $category,
])
->assertCanSeeTableRecords($category->posts);
});