UserControllerTest.php 1.6 KB

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