Skip to content

Commit 148c649

Browse files
authored
Merge pull request #54 from dcblogdev/feature/allow-multiple-admin-registrations
refactor role creation in RegisteredUserController
2 parents c162ca7 + 8d13f95 commit 148c649

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

app/Http/Controllers/Auth/RegisteredUserController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ public function store(RegisterRequest $request): RedirectResponse
4040
$user->image = $this->generateImage($user);
4141
$user->save();
4242

43-
Role::create([
43+
Role::firstOrCreate([
4444
'name' => 'admin',
4545
'label' => 'Admin',
4646
]);
4747

48-
Role::create([
48+
Role::firstOrCreate([
4949
'name' => 'user',
5050
'label' => 'User',
5151
]);

tests/Feature/App/Http/Controllers/Auth/RegisteredUserControllerTest.php

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

33
use App\Models\User;
44

5+
use Illuminate\Auth\Notifications\VerifyEmail;
6+
use Illuminate\Support\Facades\Notification;
57
use function Pest\Laravel\assertGuest;
68
use function Pest\Laravel\get;
79
use function Pest\Laravel\post;
@@ -66,8 +68,30 @@
6668
expect($user->hasRole('admin'))->toBeTrue();
6769
});
6870

71+
test('users can register multiple times', function () {
72+
//first user
73+
post(route('register'), [
74+
'name' => fake()->name(),
75+
'email' => fake()->email(),
76+
'password' => 'ght73A3!$^DS',
77+
'confirmPassword' => 'ght73A3!$^DS',
78+
]);
79+
80+
//second user
81+
post(route('register'), [
82+
'name' => fake()->name(),
83+
'email' => fake()->email(),
84+
'password' => 'ght73A3!$^DS',
85+
'confirmPassword' => 'ght73A3!$^DS',
86+
])
87+
->assertValid()
88+
->assertRedirect();
89+
90+
expect(User::count())->toBe(2);
91+
});
92+
6993
test('users can register and receive only one email verification notification', function () {
70-
\Illuminate\Support\Facades\Notification::fake();
94+
Notification::fake();
7195

7296
$password = 'ght73A3!$^DS';
7397
$email = fake()->email();
@@ -82,9 +106,9 @@
82106

83107
$user = User::where('email', $email)->first();
84108

85-
\Illuminate\Support\Facades\Notification::assertSentTo(
109+
Notification::assertSentTo(
86110
[$user],
87-
\Illuminate\Auth\Notifications\VerifyEmail::class,
111+
VerifyEmail::class,
88112
1
89113
);
90114

0 commit comments

Comments
 (0)