Skip to content

Commit d75312a

Browse files
Merge pull request #184 from laravel/use-named-routes-for-tests
Use Named Routes for Tests
2 parents 47b4cf7 + dfa9da7 commit d75312a

11 files changed

+51
-48
lines changed

routes/auth.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
Route::get('register', [RegisteredUserController::class, 'create'])
1515
->name('register');
1616

17-
Route::post('register', [RegisteredUserController::class, 'store']);
17+
Route::post('register', [RegisteredUserController::class, 'store'])
18+
->name('register.store');
1819

1920
Route::get('login', [AuthenticatedSessionController::class, 'create'])
2021
->name('login');
2122

22-
Route::post('login', [AuthenticatedSessionController::class, 'store']);
23+
Route::post('login', [AuthenticatedSessionController::class, 'store'])
24+
->name('login.store');
2325

2426
Route::get('forgot-password', [PasswordResetLinkController::class, 'create'])
2527
->name('password.request');
@@ -50,7 +52,8 @@
5052
->name('password.confirm');
5153

5254
Route::post('confirm-password', [ConfirmablePasswordController::class, 'store'])
53-
->middleware('throttle:6,1');
55+
->middleware('throttle:6,1')
56+
->name('password.confirm.store');
5457

5558
Route::post('logout', [AuthenticatedSessionController::class, 'destroy'])
5659
->name('logout');

tests/Feature/Auth/AuthenticationTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class AuthenticationTest extends TestCase
1212

1313
public function test_login_screen_can_be_rendered()
1414
{
15-
$response = $this->get('/login');
15+
$response = $this->get(route('login'));
1616

1717
$response->assertStatus(200);
1818
}
@@ -21,7 +21,7 @@ public function test_users_can_authenticate_using_the_login_screen()
2121
{
2222
$user = User::factory()->create();
2323

24-
$response = $this->post('/login', [
24+
$response = $this->post(route('login.store'), [
2525
'email' => $user->email,
2626
'password' => 'password',
2727
]);
@@ -34,7 +34,7 @@ public function test_users_can_not_authenticate_with_invalid_password()
3434
{
3535
$user = User::factory()->create();
3636

37-
$this->post('/login', [
37+
$this->post(route('login.store'), [
3838
'email' => $user->email,
3939
'password' => 'wrong-password',
4040
]);
@@ -46,26 +46,26 @@ public function test_users_can_logout()
4646
{
4747
$user = User::factory()->create();
4848

49-
$response = $this->actingAs($user)->post('/logout');
49+
$response = $this->actingAs($user)->post(route('logout'));
5050

5151
$this->assertGuest();
52-
$response->assertRedirect('/');
52+
$response->assertRedirect(route('home'));
5353
}
5454

5555
public function test_users_are_rate_limited()
5656
{
5757
$user = User::factory()->create();
5858

5959
for ($i = 0; $i < 5; $i++) {
60-
$this->post('/login', [
60+
$this->post(route('login.store'), [
6161
'email' => $user->email,
6262
'password' => 'wrong-password',
6363
])->assertStatus(302)->assertSessionHasErrors([
6464
'email' => 'These credentials do not match our records.',
6565
]);
6666
}
6767

68-
$response = $this->post('/login', [
68+
$response = $this->post(route('login.store'), [
6969
'email' => $user->email,
7070
'password' => 'wrong-password',
7171
]);

tests/Feature/Auth/EmailVerificationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function test_email_verification_screen_can_be_rendered()
1717
{
1818
$user = User::factory()->unverified()->create();
1919

20-
$response = $this->actingAs($user)->get('/verify-email');
20+
$response = $this->actingAs($user)->get(route('verification.notice'));
2121

2222
$response->assertStatus(200);
2323
}
@@ -79,7 +79,7 @@ public function test_verified_user_is_redirected_to_dashboard_from_verification_
7979
'email_verified_at' => now(),
8080
]);
8181

82-
$response = $this->actingAs($user)->get('/verify-email');
82+
$response = $this->actingAs($user)->get(route('verification.notice'));
8383

8484
$response->assertRedirect(route('dashboard', absolute: false));
8585
}

tests/Feature/Auth/PasswordConfirmationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function test_confirm_password_screen_can_be_rendered()
1414
{
1515
$user = User::factory()->create();
1616

17-
$response = $this->actingAs($user)->get('/confirm-password');
17+
$response = $this->actingAs($user)->get(route('password.confirm'));
1818

1919
$response->assertStatus(200);
2020
}
@@ -23,7 +23,7 @@ public function test_password_can_be_confirmed()
2323
{
2424
$user = User::factory()->create();
2525

26-
$response = $this->actingAs($user)->post('/confirm-password', [
26+
$response = $this->actingAs($user)->post(route('password.confirm.store'), [
2727
'password' => 'password',
2828
]);
2929

@@ -35,7 +35,7 @@ public function test_password_is_not_confirmed_with_invalid_password()
3535
{
3636
$user = User::factory()->create();
3737

38-
$response = $this->actingAs($user)->post('/confirm-password', [
38+
$response = $this->actingAs($user)->post(route('password.confirm.store'), [
3939
'password' => 'wrong-password',
4040
]);
4141

tests/Feature/Auth/PasswordResetTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class PasswordResetTest extends TestCase
1414

1515
public function test_reset_password_link_screen_can_be_rendered()
1616
{
17-
$response = $this->get('/forgot-password');
17+
$response = $this->get(route('password.request'));
1818

1919
$response->assertStatus(200);
2020
}
@@ -25,7 +25,7 @@ public function test_reset_password_link_can_be_requested()
2525

2626
$user = User::factory()->create();
2727

28-
$this->post('/forgot-password', ['email' => $user->email]);
28+
$this->post(route('password.email'), ['email' => $user->email]);
2929

3030
Notification::assertSentTo($user, ResetPassword::class);
3131
}
@@ -36,10 +36,10 @@ public function test_reset_password_screen_can_be_rendered()
3636

3737
$user = User::factory()->create();
3838

39-
$this->post('/forgot-password', ['email' => $user->email]);
39+
$this->post(route('password.email'), ['email' => $user->email]);
4040

4141
Notification::assertSentTo($user, ResetPassword::class, function ($notification) {
42-
$response = $this->get('/reset-password/'.$notification->token);
42+
$response = $this->get(route('password.request', $notification->token));
4343

4444
$response->assertStatus(200);
4545

@@ -53,10 +53,10 @@ public function test_password_can_be_reset_with_valid_token()
5353

5454
$user = User::factory()->create();
5555

56-
$this->post('/forgot-password', ['email' => $user->email]);
56+
$this->post(route('password.email'), ['email' => $user->email]);
5757

5858
Notification::assertSentTo($user, ResetPassword::class, function ($notification) use ($user) {
59-
$response = $this->post('/reset-password', [
59+
$response = $this->post(route('password.store'), [
6060
'token' => $notification->token,
6161
'email' => $user->email,
6262
'password' => 'password',
@@ -75,7 +75,7 @@ public function test_password_cannot_be_reset_with_invalid_token(): void
7575
{
7676
$user = User::factory()->create();
7777

78-
$response = $this->post('/reset-password', [
78+
$response = $this->post(route('password.store'), [
7979
'token' => 'invalid-token',
8080
'email' => $user->email,
8181
'password' => 'newpassword123',

tests/Feature/Auth/RegistrationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ class RegistrationTest extends TestCase
1111

1212
public function test_registration_screen_can_be_rendered()
1313
{
14-
$response = $this->get('/register');
14+
$response = $this->get(route('register'));
1515

1616
$response->assertStatus(200);
1717
}
1818

1919
public function test_new_users_can_register()
2020
{
21-
$response = $this->post('/register', [
21+
$response = $this->post(route('register.store'), [
2222
'name' => 'Test User',
2323
'email' => 'test@example.com',
2424
'password' => 'password',

tests/Feature/Auth/VerificationNotificationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public function test_sends_verification_notification(): void
2121
]);
2222

2323
$this->actingAs($user)
24-
->post('email/verification-notification')
25-
->assertRedirect('/');
24+
->post(route('verification.send'))
25+
->assertRedirect(route('home'));
2626

2727
Notification::assertSentTo($user, VerifyEmail::class);
2828
}
@@ -36,7 +36,7 @@ public function test_does_not_send_verification_notification_if_email_is_verifie
3636
]);
3737

3838
$this->actingAs($user)
39-
->post('email/verification-notification')
39+
->post(route('verification.send'))
4040
->assertRedirect(route('dashboard', absolute: false));
4141

4242
Notification::assertNothingSent();

tests/Feature/DashboardTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ class DashboardTest extends TestCase
1212

1313
public function test_guests_are_redirected_to_the_login_page()
1414
{
15-
$response = $this->get('/dashboard');
16-
$response->assertRedirect('/login');
15+
$response = $this->get(route('dashboard'));
16+
$response->assertRedirect(route('login'));
1717
}
1818

1919
public function test_authenticated_users_can_visit_the_dashboard()
2020
{
2121
$user = User::factory()->create();
2222
$this->actingAs($user);
2323

24-
$response = $this->get('/dashboard');
24+
$response = $this->get(route('dashboard'));
2525
$response->assertStatus(200);
2626
}
2727
}

tests/Feature/ExampleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ExampleTest extends TestCase
1111

1212
public function test_returns_a_successful_response()
1313
{
14-
$response = $this->get('/');
14+
$response = $this->get(route('home'));
1515

1616
$response->assertStatus(200);
1717
}

tests/Feature/Settings/PasswordUpdateTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function test_password_update_page_is_displayed()
1717

1818
$response = $this
1919
->actingAs($user)
20-
->get('/settings/password');
20+
->get(route('password.edit'));
2121

2222
$response->assertStatus(200);
2323
}
@@ -28,16 +28,16 @@ public function test_password_can_be_updated()
2828

2929
$response = $this
3030
->actingAs($user)
31-
->from('/settings/password')
32-
->put('/settings/password', [
31+
->from(route('password.edit'))
32+
->put(route('password.update'), [
3333
'current_password' => 'password',
3434
'password' => 'new-password',
3535
'password_confirmation' => 'new-password',
3636
]);
3737

3838
$response
3939
->assertSessionHasNoErrors()
40-
->assertRedirect('/settings/password');
40+
->assertRedirect(route('password.edit'));
4141

4242
$this->assertTrue(Hash::check('new-password', $user->refresh()->password));
4343
}
@@ -48,15 +48,15 @@ public function test_correct_password_must_be_provided_to_update_password()
4848

4949
$response = $this
5050
->actingAs($user)
51-
->from('/settings/password')
52-
->put('/settings/password', [
51+
->from(route('password.edit'))
52+
->put(route('password.update'), [
5353
'current_password' => 'wrong-password',
5454
'password' => 'new-password',
5555
'password_confirmation' => 'new-password',
5656
]);
5757

5858
$response
5959
->assertSessionHasErrors('current_password')
60-
->assertRedirect('/settings/password');
60+
->assertRedirect(route('password.edit'));
6161
}
6262
}

0 commit comments

Comments
 (0)