UserUpdateRequestTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace Tests\Feature\Http\Requests;
  3. use App\Http\Requests\UserUpdateRequest;
  4. use App\Models\User;
  5. use Illuminate\Foundation\Testing\WithoutMiddleware;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Facades\Validator;
  8. use Mockery;
  9. use PHPUnit\Framework\Attributes\CoversClass;
  10. use PHPUnit\Framework\Attributes\DataProvider;
  11. use Tests\FeatureTestCase;
  12. /**
  13. * UserUpdateRequestTest test class
  14. */
  15. #[CoversClass(UserUpdateRequest::class)]
  16. class UserUpdateRequestTest extends FeatureTestCase
  17. {
  18. use WithoutMiddleware;
  19. /**
  20. * @test
  21. */
  22. public function test_user_is_authorized()
  23. {
  24. Auth::shouldReceive('check')
  25. ->once()
  26. ->andReturn(true);
  27. $request = new UserUpdateRequest();
  28. $this->assertTrue($request->authorize());
  29. }
  30. /**
  31. * @test
  32. */
  33. #[DataProvider('provideValidData')]
  34. public function test_valid_data(array $data) : void
  35. {
  36. /**
  37. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  38. */
  39. $user = User::factory()->create([
  40. 'name' => 'Jane',
  41. 'email' => 'jane@example.com',
  42. ]);
  43. $request = Mockery::mock(UserUpdateRequest::class)->makePartial();
  44. $request->shouldReceive('user')
  45. ->andReturn($user);
  46. $validator = Validator::make($data, $request->rules());
  47. $this->assertFalse($validator->fails());
  48. }
  49. /**
  50. * Provide Valid data for validation test
  51. */
  52. public static function provideValidData() : array
  53. {
  54. return [
  55. [[
  56. 'name' => 'John',
  57. 'email' => 'john@example.com',
  58. 'password' => 'MyPassword',
  59. ]],
  60. [[
  61. 'name' => 'John',
  62. 'email' => 'jane@example.com',
  63. 'password' => 'MyPassword',
  64. ]],
  65. [[
  66. 'name' => 'Jane',
  67. 'email' => 'john@example.com',
  68. 'password' => 'MyPassword',
  69. ]],
  70. ];
  71. }
  72. /**
  73. * @test
  74. */
  75. #[DataProvider('provideInvalidData')]
  76. public function test_invalid_data(array $data) : void
  77. {
  78. /**
  79. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  80. */
  81. $user = User::factory()->create([
  82. 'name' => 'Jane',
  83. 'email' => 'jane@example.com',
  84. ]);
  85. User::factory()->create([
  86. 'name' => 'Bob',
  87. 'email' => 'bob@example.com',
  88. ]);
  89. $request = Mockery::mock(UserUpdateRequest::class)->makePartial();
  90. $request->shouldReceive('user')
  91. ->andReturn($user);
  92. $validator = Validator::make($data, $request->rules());
  93. $this->assertTrue($validator->fails());
  94. }
  95. /**
  96. * Provide invalid data for validation test
  97. */
  98. public static function provideInvalidData() : array
  99. {
  100. return [
  101. [[
  102. 'name' => 'Jane',
  103. 'email' => 'bob@example.com', // unique
  104. 'password' => 'MyPassword',
  105. ]],
  106. [[
  107. 'name' => 'Bob', // unique
  108. 'email' => 'jane@example.com',
  109. 'password' => 'MyPassword',
  110. ]],
  111. [[
  112. 'name' => '', // required
  113. 'email' => 'john@example.com',
  114. 'password' => 'MyPassword',
  115. ]],
  116. [[
  117. 'name' => 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz', // max:255
  118. 'email' => 'john@example.com',
  119. 'password' => 'MyPassword',
  120. ]],
  121. [[
  122. 'name' => true, // string
  123. 'email' => 'john@example.com',
  124. 'password' => 'MyPassword',
  125. ]],
  126. [[
  127. 'name' => 'John',
  128. 'email' => '', // required
  129. 'password' => 'MyPassword',
  130. ]],
  131. [[
  132. 'name' => 'John',
  133. 'email' => 0, // string
  134. 'password' => 'MyPassword',
  135. ]],
  136. [[
  137. 'name' => 'John',
  138. 'email' => 'johnexample.com', // email
  139. 'password' => 'MyPassword',
  140. ]],
  141. [[
  142. 'name' => 'John',
  143. 'email' => 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz@example.com', // max:255
  144. 'password' => 'MyPassword',
  145. ]],
  146. [[
  147. 'name' => 'John',
  148. 'email' => 'john@example.com',
  149. 'password' => '', // required
  150. ]],
  151. ];
  152. }
  153. }