Skip to content

Instantly share code, notes, and snippets.

@alisalehi1380
Last active June 15, 2025 21:48
Show Gist options
  • Save alisalehi1380/a787d2af7ee606a0467614ece0ef5d32 to your computer and use it in GitHub Desktop.
Save alisalehi1380/a787d2af7ee606a0467614ece0ef5d32 to your computer and use it in GitHub Desktop.

Authentication

it('redirects guest to login for PostResource index', function () {
    $this->get(PostResource::getUrl('index'))
        ->assertRedirect(route('filament.admin.auth.login'));
});

it('shows post list page for authenticated user', function () {
    $user = User::factory()->create();

    $this->actingAs($user)
        ->get(PostResource::getUrl('index'))
        ->assertSuccessful(); // or ->assertForbidden();
    
});

Model

it('has correct city relation', function () {
    $city = Post::factory()->for(Province::factory())->create();
    $user = User::factory()->for($city)->create();

    expect($user->city())->toBeInstanceOf(BelongsTo::class)
        ->and($user->city)->toBeInstanceOf(Post::class)
        ->and($user->city->getKey())->toBe($city->getKey());
});

Relation

it('consultant has sessions relation', function () {
    $consultant = Consultant::factory()
        ->has(ConsultantSession::factory()->count(3), Consultant::SESSIONS_RELATION)
        ->create();

    expect($consultant->sessions())->toBeInstanceOf(HasMany::class)
        ->and($consultant->sessions)->toBeInstanceOf(Collection::class)
        ->and($consultant->sessions)->toHaveCount(3);
});

Resource

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

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

it('can render user edit page', function () {
    $user = User::factory()->create();

    $this->get(UserResource::getUrl('edit', [
        'record' => $user,
    ]))->assertSuccessful();
});

Get

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

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

Create

it('can create a post', function () {
    $user = User::factory()->create();
    $data = [
        'title' => 'new title',
    ];

    livewire(CreatePost::class)
        ->fillForm($data)
        ->call('create')
        ->assertHasNoFormErrors();

    $this->assertDatabaseHas(Post::class, [
        'title' => $data['title'],
    ]);
});

Edit

it('can update post data via edit page', function () {
    $post = Post::factory()->create();
    $newData = [
        'title' => 'new title',
    ];

    livewire(EditPost::class, [
        'record' => $post->getKey(),
    ])
        ->fillForm($newData)
        ->call('save')
        ->assertHasNoFormErrors();

    expect($post->refresh())
        ->title->toBe($newData['title']);
    });

Deleting

it('can delete a single post from table action', function () {
    $post = Post::factory()->create();

    livewire(ListPosts::class)
        ->callTableAction(DeleteAction::class, $post);

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


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

    livewire(PostResource\Pages\ListPosts::class)
        ->callTableBulkAction(DeleteBulkAction::class, $posts);

    foreach ($posts as $post) {
        $this->assertModelMissing($post);
    }
});

Validation

it('shows error if title is required', function () {
    $data = [
        'title' => '',
    ];

    livewire(CreatePost::class)
        ->fillForm($data)
        ->call('create')
        ->assertHasFormErrors(['title' => 'required']);
});

Table

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

    $title = $posts->first()->title;

    livewire(PostResource\Pages\ListPosts::class)
        ->searchTable($title)
        ->assertCanSeeTableRecords($posts->where('title', $title))
        ->assertCanNotSeeTableRecords($posts->where('title', '!=', $title));
});

Access to see records

it('admin can see all posts', function () {
    $adminRole = Role::findOrCreate('admin');
    $admin = User::factory()->create();
    $admin->assignRole($adminRole);

    $ownPosts = Post::factory()->count(2)->create(['user_id' => $admin->getKey()]);
    $otherPosts = Post::factory()->count(3)->create();

    $this->actingAs($admin);

    livewire(ListPosts::class)
        ->assertCanSeeTableRecords([...$ownPosts, ...$otherPosts])
        ->assertCountTableRecords(5);
});

it('user can see only his own posts', function () {
    $userRole = Role::findOrCreate('user');
    $user = User::factory()->create();
    $user->assignRole($userRole);

    $ownPosts = Post::factory()->count(3)->create(['user_id' => $user->getKey()]);
    $otherPosts = Post::factory()->count(4)->create();

    $this->actingAs($user);

    livewire(ListPosts::class)
        ->assertCanSeeTableRecords($ownPosts)
        ->assertCanNotSeeTableRecords($otherPosts)
        ->assertCountTableRecords(3);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment