UserControllerTest.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Tests\Api\v1\Controllers\Auth;
  3. use App\Models\User;
  4. use Tests\FeatureTestCase;
  5. class UserControllerTest extends FeatureTestCase
  6. {
  7. /**
  8. * @var \App\Models\User
  9. */
  10. protected $user;
  11. /**
  12. * @test
  13. */
  14. public function setUp(): void
  15. {
  16. parent::setUp();
  17. $this->user = User::factory()->create();
  18. }
  19. /**
  20. * @test
  21. */
  22. public function test_show_existing_user_when_authenticated_returns_success()
  23. {
  24. $response = $this->actingAs($this->user, 'api-guard')
  25. ->json('GET', '/api/v1/user')
  26. ->assertOk()
  27. ->assertExactJson([
  28. 'name' => $this->user->name,
  29. 'id' => $this->user->id,
  30. 'email' => $this->user->email,
  31. ]);
  32. }
  33. /**
  34. * @test
  35. */
  36. public function test_show_existing_user_when_anonymous_returns_success()
  37. {
  38. $response = $this->json('GET', '/api/v1/user/name')
  39. ->assertOk()
  40. ->assertExactJson([
  41. 'name' => $this->user->name,
  42. ]);
  43. }
  44. /**
  45. * @test
  46. */
  47. public function test_show_missing_user_returns_success_with_null_name()
  48. {
  49. User::destroy($this->user->id);
  50. $response = $this->actingAs($this->user, 'api-guard')
  51. ->json('GET', '/api/v1/user')
  52. ->assertOk()
  53. ->assertExactJson([
  54. 'name' => $this->user->name,
  55. 'id' => $this->user->id,
  56. 'email' => $this->user->email,
  57. ]);
  58. }
  59. }