面板构建器
测试
概述
所有本指南内的示例都使用 Pest 编写。要使用 Pest 的 Livewire 插件进行测试,请参考 Pest 文档中的插件安装说明:Pest 的 Livewire 插件。不过,你也可以将其适配到 PHPUnit。
由于应用中的所有页面都是 Livewire 组件,我们始终都在使用 Livewire 测试辅助函数。如果你之前从未测试过 Livewire 组件,请阅读 Livewire 文档下的这一向导。
开始
确保你在测试案例(TestCase)中有权限访问应用:
protected function setUp(): void{    parent::setUp();     $this->actingAs(User::factory()->create());}测试多个面板
如果你有多个面板并且想要测试非默认面板,你需要告诉 Filament 你在测试哪个面板。可以在测试用例的 setUp() 方法中完成此操作,或者可以在特殊用例的开头完成。当你通过请求访问面板时,Filament 通常在中间件中实现此操作,因此,如果你在测试中没有像测试 Livewire 组件那样发起请求,则需要手动设置当前面板:
use Filament\Facades\Filament; Filament::setCurrentPanel(    Filament::getPanel('app'), // Where `app` is the ID of the panel you want to test.);资源
页面
列表
路由 & 渲染
要确保 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 () {    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();     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);});Still need help? Join our Discord community or open a GitHub discussion