AuthenticationTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace Tests\Feature\Auth;
  3. use App\Models\User;
  4. use Illuminate\Foundation\Testing\RefreshDatabase;
  5. use Livewire\Volt\Volt;
  6. use Tests\TestCase;
  7. class AuthenticationTest extends TestCase
  8. {
  9. use RefreshDatabase;
  10. public function test_login_screen_can_be_rendered(): void
  11. {
  12. $response = $this->get('/login');
  13. $response
  14. ->assertOk()
  15. ->assertSeeVolt('pages.auth.login');
  16. }
  17. public function test_users_can_authenticate_using_the_login_screen(): void
  18. {
  19. $user = User::factory()->create();
  20. $component = Volt::test('pages.auth.login')
  21. ->set('form.email', $user->email)
  22. ->set('form.password', 'password');
  23. $component->call('login');
  24. $component
  25. ->assertHasNoErrors()
  26. ->assertRedirect(route('dashboard', absolute: false));
  27. $this->assertAuthenticated();
  28. }
  29. public function test_users_can_not_authenticate_with_invalid_password(): void
  30. {
  31. $user = User::factory()->create();
  32. $component = Volt::test('pages.auth.login')
  33. ->set('form.email', $user->email)
  34. ->set('form.password', 'wrong-password');
  35. $component->call('login');
  36. $component
  37. ->assertHasErrors()
  38. ->assertNoRedirect();
  39. $this->assertGuest();
  40. }
  41. public function test_navigation_menu_can_be_rendered(): void
  42. {
  43. $user = User::factory()->create();
  44. $this->actingAs($user);
  45. $response = $this->get('/dashboard');
  46. $response
  47. ->assertOk()
  48. ->assertSeeVolt('layout.navigation');
  49. }
  50. public function test_users_can_logout(): void
  51. {
  52. $user = User::factory()->create();
  53. $this->actingAs($user);
  54. $component = Volt::test('layout.navigation');
  55. $component->call('logout');
  56. $component
  57. ->assertHasNoErrors()
  58. ->assertRedirect('/');
  59. $this->assertGuest();
  60. }
  61. }