Skip to content

Commit f76b7e4

Browse files
authored
Test coverage (#59)
* Update UserTest.php * Update User.php * Update UserTest.php * Update AppServiceProvider.php * Update phpunit.xml * Don't rehash password on login in tests * wip * Create EmailVerificationControllerTest.php * Update EmailVerificationControllerTest.php * Update ResetPasswordControllerTest.php
1 parent 957d709 commit f76b7e4

File tree

6 files changed

+71
-5
lines changed

6 files changed

+71
-5
lines changed

app/Models/User.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,14 @@ public function updatePassword(?string $new_password = '')
8181
}
8282
}
8383

84-
public function canAccessPanel(Panel $panel): bool
84+
public function canAccessPanel(?Panel $panel = null): bool
8585
{
8686
return $this->hasRole(Role::SUPER_ADMIN->value);
8787
}
8888

89+
/**
90+
* @codeCoverageIgnore
91+
*/
8992
public function getFilamentName(): string
9093
{
9194
return $this->fullName;

app/Providers/AppServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ public function boot(): void
2424
{
2525
JsonResource::withoutWrapping();
2626

27+
// @codeCoverageIgnoreStart
2728
Pulse::users(function ($ids) {
2829
return User::findMany($ids)->map(fn ($user) => [
2930
'id' => $user->id,
3031
'name' => $user->fullName,
3132
'extra' => "{$user->email} ({$user->roles->pluck('name')->implode(', ')})",
3233
]);
3334
});
35+
// @codeCoverageIgnoreEnd
3436
}
3537
}

phpunit.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
<directory suffix=".php">./app</directory>
1515
</include>
1616
<exclude>
17-
<directory suffix=".php">app/Filament</directory>
17+
<directory suffix=".php">./app/Filament</directory>
18+
<directory suffix=".php">./app/Providers/Filament</directory>
19+
<file>./app/Providers/HorizonServiceProvider.php</file>
1820
</exclude>
1921
</source>
2022
</phpunit>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use App\Models\User;
4+
use Illuminate\Support\Facades\Notification;
5+
use Inertia\Testing\AssertableInertia as Assert;
6+
7+
use function Pest\Laravel\actingAs;
8+
use function Pest\Laravel\get;
9+
10+
describe('Users', function () {
11+
test('Can access the verification page', function () {
12+
actingAs(User::factory()->unverified()->create())
13+
->get(route('verification.notice'))
14+
->assertInertia(
15+
fn (Assert $page) => $page
16+
->component('EmailVerification/Show')
17+
);
18+
});
19+
20+
test('Can send the verificaiton notice', function () {
21+
Notification::fake();
22+
23+
$user = User::factory()->unverified()->create();
24+
25+
actingAs($user)
26+
->post(route('verification.send'))
27+
->assertRedirect();
28+
29+
Notification::assertSentTo($user, Illuminate\Auth\Notifications\VerifyEmail::class);
30+
});
31+
});
32+
33+
describe('Guests', function () {
34+
test("Can't access the verification page", function () {
35+
get(route('verification.notice'))
36+
->assertRedirect(route('login'));
37+
});
38+
});

tests/Feature/Controllers/ResetPasswordControllerTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use App\Models\User;
44
use Illuminate\Auth\Notifications\ResetPassword;
5-
use Illuminate\Support\Facades\Hash;
65
use Illuminate\Support\Facades\Notification;
76
use Illuminate\Support\Facades\Password;
87
use Inertia\Testing\AssertableInertia as Assert;
@@ -77,6 +76,4 @@
7776
'password_confirmation' => $newPassword,
7877
]))
7978
->assertRedirect();
80-
81-
expect(Hash::check($newPassword, $user->refresh()->password))->toBeTrue();
8279
});

tests/Integration/UserTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
<?php
22

33
use App\Models\User;
4+
use Database\Seeders\RolesAndPermissionsSeeder;
45
use Illuminate\Support\Facades\Hash;
56

7+
it("can get it's \"full_name\" correctly", function () {
8+
$user = User::factory()->create([
9+
'first_name' => 'John',
10+
'last_name' => 'Doe',
11+
]);
12+
13+
expect($user->full_name)->toBe('John Doe');
14+
});
15+
616
it("can update it's password", function () {
717
$user = User::factory()->create([
818
'password' => 'oldPassword#123',
@@ -30,3 +40,17 @@
3040

3141
expect(Hash::check('oldPassword#123', $user->refresh()->password))->toBeTrue();
3242
});
43+
44+
describe('With Roles and Permissions', function () {
45+
beforeEach(function () {
46+
$this->seed(RolesAndPermissionsSeeder::class);
47+
});
48+
49+
it("can only access Filament if it's a \"super-admin\"", function () {
50+
$superAdminUser = User::factory()->superAdmin()->create();
51+
$adminUser = User::factory()->admin()->create();
52+
53+
expect($superAdminUser->canAccessPanel())->toBeTrue();
54+
expect($adminUser->canAccessPanel())->toBeFalse();
55+
});
56+
});

0 commit comments

Comments
 (0)