UserUpdateRequestTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 Tests\FeatureTestCase;
  9. /**
  10. * @covers \App\Http\Requests\UserUpdateRequest
  11. */
  12. class UserUpdateRequestTest extends FeatureTestCase
  13. {
  14. use WithoutMiddleware;
  15. /**
  16. * @test
  17. */
  18. public function test_user_is_authorized()
  19. {
  20. Auth::shouldReceive('check')
  21. ->once()
  22. ->andReturn(true);
  23. $request = new UserUpdateRequest();
  24. $this->assertTrue($request->authorize());
  25. }
  26. /**
  27. * @dataProvider provideValidData
  28. */
  29. public function test_valid_data(array $data) : void
  30. {
  31. User::factory()->create([
  32. 'name' => 'Jane',
  33. 'email' => 'jane@example.com',
  34. ]);
  35. $request = new UserUpdateRequest();
  36. $validator = Validator::make($data, $request->rules());
  37. $this->assertFalse($validator->fails());
  38. }
  39. /**
  40. * Provide Valid data for validation test
  41. */
  42. public function provideValidData() : array
  43. {
  44. return [
  45. [[
  46. 'name' => 'John',
  47. 'email' => 'john@example.com',
  48. 'password' => 'MyPassword',
  49. ]],
  50. ];
  51. }
  52. /**
  53. * @dataProvider provideInvalidData
  54. */
  55. public function test_invalid_data(array $data) : void
  56. {
  57. User::factory()->create([
  58. 'name' => 'Jane',
  59. 'email' => 'jane@example.com',
  60. ]);
  61. $request = new UserUpdateRequest();
  62. $validator = Validator::make($data, $request->rules());
  63. $this->assertTrue($validator->fails());
  64. }
  65. /**
  66. * Provide invalid data for validation test
  67. */
  68. public function provideInvalidData() : array
  69. {
  70. return [
  71. [[
  72. 'name' => 'John',
  73. 'email' => 'jane@example.com', // unique
  74. 'password' => 'MyPassword',
  75. ]],
  76. [[
  77. 'name' => '', // required
  78. 'email' => 'john@example.com',
  79. 'password' => 'MyPassword',
  80. ]],
  81. [[
  82. 'name' => 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz', // max:255
  83. 'email' => 'john@example.com',
  84. 'password' => 'MyPassword',
  85. ]],
  86. [[
  87. 'name' => true, // string
  88. 'email' => 'john@example.com',
  89. 'password' => 'MyPassword',
  90. ]],
  91. [[
  92. 'name' => 'John',
  93. 'email' => '', // required
  94. 'password' => 'MyPassword',
  95. ]],
  96. [[
  97. 'name' => 'John',
  98. 'email' => 0, // string
  99. 'password' => 'MyPassword',
  100. ]],
  101. [[
  102. 'name' => 'John',
  103. 'email' => 'johnexample.com', // email
  104. 'password' => 'MyPassword',
  105. ]],
  106. [[
  107. 'name' => 'John',
  108. 'email' => 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz@example.com', // max:255
  109. 'password' => 'MyPassword',
  110. ]],
  111. [[
  112. 'name' => 'John',
  113. 'email' => 'john@example.com',
  114. 'password' => '', // required
  115. ]],
  116. ];
  117. }
  118. }