RegisterTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Tests\Feature\Auth;
  3. use App\User;
  4. use Tests\TestCase;
  5. class RegisterTest extends TestCase
  6. {
  7. /** @var \App\User */
  8. protected $user;
  9. /**
  10. * @test
  11. */
  12. public function setUp(): void
  13. {
  14. parent::setUp();
  15. $this->user = factory(User::class)->create();
  16. }
  17. /**
  18. * test Existing user count via API
  19. *
  20. * @test
  21. */
  22. public function testExistingUserCount()
  23. {
  24. $response = $this->json('POST', '/api/checkuser')
  25. ->assertStatus(200)
  26. ->assertJson([
  27. 'username' => $this->user->name,
  28. ]);
  29. }
  30. /**
  31. * test creation of another user via API
  32. *
  33. * @test
  34. */
  35. public function testUserCreationWithAnExistingUser()
  36. {
  37. $response = $this->json('POST', '/api/register', [
  38. 'name' => 'testCreate',
  39. 'email' => 'testCreate@example.org',
  40. 'password' => 'test',
  41. 'password_confirmation' => 'test',
  42. ]);
  43. $response->assertStatus(422);
  44. }
  45. /**
  46. * test User creation with missing values via API
  47. *
  48. * @test
  49. */
  50. public function testUserCreationWithMissingValues()
  51. {
  52. // we delete the existing user
  53. User::destroy(1);
  54. $response = $this->json('POST', '/api/register', [
  55. 'name' => '',
  56. 'email' => '',
  57. 'password' => '',
  58. 'password_confirmation' => '',
  59. ]);
  60. $response->assertStatus(422);
  61. }
  62. /**
  63. * test User creation with invalid values via API
  64. *
  65. * @test
  66. */
  67. public function testUserCreationWithInvalidData()
  68. {
  69. // we delete the existing user
  70. User::destroy(1);
  71. $response = $this->json('POST', '/api/register', [
  72. 'name' => 'testInvalid',
  73. 'email' => 'email',
  74. 'password' => 'test',
  75. 'password_confirmation' => 'tset',
  76. ]);
  77. $response->assertStatus(422);
  78. }
  79. /**
  80. * test User creation via API
  81. *
  82. * @test
  83. */
  84. public function testUserCreation()
  85. {
  86. // we delete the existing user
  87. User::destroy(1);
  88. $response = $this->json('POST', '/api/register', [
  89. 'name' => 'newUser',
  90. 'email' => 'newUser@example.org',
  91. 'password' => 'password',
  92. 'password_confirmation' => 'password',
  93. ]);
  94. $response->assertStatus(200)
  95. ->assertJsonStructure([
  96. 'message' => ['token', 'name']
  97. ]);
  98. }
  99. }