UserUpdateRequestTest.php 5.1 KB

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