Skip to content

Commit 1638ad0

Browse files
authored
Testing refactor (#43)
* Update UserTest.php * User `describe` blocks for tests * Update AccountControllerTest.php * Change `inertia()->render()` to `inertia()` * Create separate `LogoutController` * Update RegisterController.php * Seed an organisation for each admin user * Use enum for roles * Update 2023_11_21_161739_create_permission_tables.php
1 parent 8aa3201 commit 1638ad0

18 files changed

+260
-209
lines changed

app/Http/Controllers/AccountController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class AccountController extends Controller
1010
{
1111
public function edit(Request $request)
1212
{
13-
return \inertia()->render('Account/Edit', [
13+
return \inertia('Account/Edit', [
1414
'user' => UserResource::make($request->user()),
1515
]);
1616
}

app/Http/Controllers/HomeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ class HomeController extends Controller
66
{
77
public function index()
88
{
9-
return \inertia()->render('Home/Index');
9+
return \inertia('Home/Index');
1010
}
1111
}

app/Http/Controllers/LoginController.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function show(Request $request)
1212
{
1313
$isProd = \app()->environment('production');
1414

15-
return \inertia()->render('Login/Show', [
15+
return \inertia('Login/Show', [
1616
'email' => ! $isProd ? \env('SEED_ADMIN_EMAIL') : '',
1717
'password' => ! $isProd ? '12345' : '',
1818
'remember' => ! $isProd ? true : false,
@@ -23,7 +23,7 @@ public function show(Request $request)
2323
public function store(LoginStore $request)
2424
{
2525
\throw_if(
26-
! \auth()->attempt($request->only('email', 'password'), $request->validated('remember')),
26+
! \auth()->attempt($request->only('email', 'password'), $request->only('remember')),
2727
ValidationException::withMessages([
2828
'email' => \__('auth.failed'),
2929
])
@@ -37,14 +37,4 @@ public function store(LoginStore $request)
3737

3838
return \redirect()->route('home');
3939
}
40-
41-
public function destroy(Request $request)
42-
{
43-
\auth()->logout();
44-
45-
$request->session()->invalidate();
46-
$request->session()->regenerateToken();
47-
48-
return \redirect()->route('login');
49-
}
5040
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
7+
class LogoutController extends Controller
8+
{
9+
public function __invoke(Request $request)
10+
{
11+
\auth()->logout();
12+
13+
$request->session()->invalidate();
14+
$request->session()->regenerateToken();
15+
16+
return \redirect()->route('login');
17+
}
18+
}

app/Http/Controllers/RegisterController.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Enums\Role;
56
use App\Http\Requests\Register\RegisterStore;
67
use App\Models\User;
78

89
class RegisterController extends Controller
910
{
1011
public function show()
1112
{
12-
$isProd = \app()->environment('production');
13-
14-
return \inertia()->render('Register/Show', [
15-
'first_name' => ! $isProd ? 'Jim' : '',
16-
'last_name' => ! $isProd ? 'Gordon' : '',
17-
'email' => ! $isProd ? 'test@test.com' : '',
18-
'password' => ! $isProd ? '123456Ab#' : '',
19-
'organisation_name' => ! $isProd ? 'GCPD' : '',
20-
]);
13+
return \inertia('Register/Show', \app()->environment('local') ? [
14+
'first_name' => 'Jim',
15+
'last_name' => 'Gordon',
16+
'email' => 'test@test.com',
17+
'password' => '123456Ab#',
18+
'organisation_name' => 'GCPD',
19+
] : []);
2120
}
2221

2322
public function store(RegisterStore $request)
@@ -32,7 +31,7 @@ public function store(RegisterStore $request)
3231
]);
3332
$user->currentOrganisation()->associate($user->organisations->first())->save();
3433

35-
$user->assignRole('user');
34+
$user->assignRole(Role::USER->value);
3635

3736
\auth()->loginUsingId($user->id);
3837

app/Http/Middleware/HandleInertiaRequests.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class HandleInertiaRequests extends Middleware
1010
{
1111
public function share(Request $request): array
1212
{
13-
return array_merge(parent::share($request), [
13+
return \array_merge(parent::share($request), [
1414
'auth' => [
1515
'user' => $request->user() ? UserResource::make($request->user()) : [],
1616
],

app/Models/User.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
// use Illuminate\Contracts\Auth\MustVerifyEmail;
66

7+
use App\Enums\Role;
78
use Filament\Models\Contracts\FilamentUser;
89
use Filament\Models\Contracts\HasName;
910
use Filament\Panel;
@@ -79,7 +80,7 @@ public function updatePassword(?string $new_password = '')
7980

8081
public function canAccessPanel(Panel $panel): bool
8182
{
82-
return $this->hasRole('admin');
83+
return $this->hasRole(Role::ADMIN->value);
8384
}
8485

8586
public function getFilamentName(): string

database/factories/UserFactory.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Database\Factories;
44

5+
use App\Enums\Role;
56
use App\Models\User;
67
use Illuminate\Database\Eloquent\Factories\Factory;
78
use Illuminate\Support\Str;
@@ -33,7 +34,14 @@ public function unverified()
3334
public function admin()
3435
{
3536
return $this->afterCreating(function (User $user) {
36-
$user->assignRole('admin');
37+
$user->assignRole(Role::ADMIN->value);
38+
});
39+
}
40+
41+
public function user()
42+
{
43+
return $this->afterCreating(function (User $user) {
44+
$user->assignRole(Role::USER->value);
3745
});
3846
}
3947
}

database/migrations/2023_11_21_161739_create_permission_tables.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use App\Enums\Permission;
4+
use App\Enums\Role;
35
use Illuminate\Database\Migrations\Migration;
46
use Illuminate\Database\Schema\Blueprint;
57
use Illuminate\Support\Facades\Schema;
@@ -26,7 +28,7 @@ public function up(): void
2628

2729
Schema::create($tableNames['permissions'], function (Blueprint $table) {
2830
$table->bigIncrements('id'); // permission id
29-
$table->string('name'); // For MySQL 8.0 use string('name', 125);
31+
$table->enum('name', Permission::values());
3032
$table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125);
3133
$table->timestamps();
3234

@@ -39,7 +41,7 @@ public function up(): void
3941
$table->unsignedBigInteger($columnNames['team_foreign_key'])->nullable();
4042
$table->index($columnNames['team_foreign_key'], 'roles_team_foreign_key_index');
4143
}
42-
$table->string('name'); // For MySQL 8.0 use string('name', 125);
44+
$table->enum('name', Role::values());
4345
$table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125);
4446
$table->timestamps();
4547
if ($teams || config('permission.testing')) {

database/seeders/OrganisationsSeeder.php

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

33
namespace Database\Seeders;
44

5+
use App\Enums\Role;
56
use App\Models\Organisation;
67
use App\Models\User;
78
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
@@ -13,17 +14,19 @@ class OrganisationsSeeder extends Seeder
1314

1415
public function run()
1516
{
16-
$adminUser = User::whereEmail(\env('SEED_ADMIN_EMAIL'))->firstOrFail();
17-
18-
$orgs = Organisation::factory(5)
19-
->for($adminUser)
20-
->afterCreating(function (Organisation $org) use ($adminUser) {
21-
$org->users()->attach($adminUser);
22-
})
23-
->create();
24-
25-
$adminUser->currentOrganisation()
26-
->associate($orgs->first())
27-
->save();
17+
User::whereHas('roles', fn ($q) => $q->where('name', Role::ADMIN->value))
18+
->get()
19+
->each(function (User $user) {
20+
$user->currentOrganisation()
21+
->associate(
22+
Organisation::factory()
23+
->for($user)
24+
->afterCreating(function (Organisation $org) use ($user) {
25+
$org->users()->attach($user);
26+
})
27+
->create()
28+
)
29+
->save();
30+
});
2831
}
2932
}

0 commit comments

Comments
 (0)