UserUpdateRequestTest.php 3.5 KB

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