LoginRequestTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Tests\Feature\Http\Requests;
  3. use App\Models\User;
  4. use App\Http\Requests\LoginRequest;
  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\LoginRequest
  11. */
  12. class LoginRequestTest extends FeatureTestCase
  13. {
  14. use WithoutMiddleware;
  15. /**
  16. * @test
  17. */
  18. public function test_user_is_authorized()
  19. {
  20. $request = new LoginRequest();
  21. $this->assertTrue($request->authorize());
  22. }
  23. /**
  24. * @dataProvider provideValidData
  25. */
  26. public function test_valid_data(array $data) : void
  27. {
  28. User::factory()->create([
  29. 'email' => 'JOHN.DOE@example.com'
  30. ]);
  31. $request = new LoginRequest();
  32. $validator = Validator::make($data, $request->rules());
  33. $this->assertFalse($validator->fails());
  34. }
  35. /**
  36. * Provide Valid data for validation test
  37. */
  38. public function provideValidData() : array
  39. {
  40. return [
  41. [[
  42. 'email' => 'john.doe@example.com',
  43. 'password' => 'MyPassword'
  44. ]],
  45. [[
  46. 'email' => 'JOHN.doe@example.com',
  47. 'password' => 'MyPassword'
  48. ]],
  49. ];
  50. }
  51. /**
  52. * @dataProvider provideInvalidData
  53. */
  54. public function test_invalid_data(array $data) : void
  55. {
  56. User::factory()->create([
  57. 'email' => 'JOHN.DOE@example.com'
  58. ]);
  59. $request = new LoginRequest();
  60. $validator = Validator::make($data, $request->rules());
  61. $this->assertTrue($validator->fails());
  62. }
  63. /**
  64. * Provide invalid data for validation test
  65. */
  66. public function provideInvalidData() : array
  67. {
  68. return [
  69. [[
  70. 'email' => '', // required
  71. 'password' => 'MyPassword',
  72. ]],
  73. [[
  74. 'email' => 'john', // email
  75. 'password' => 'MyPassword',
  76. ]],
  77. [[
  78. 'email' => 'john@example.com', // exists
  79. 'password' => 'MyPassword',
  80. ]],
  81. [[
  82. 'email' => 'john.doe@example.com',
  83. 'password' => '', // required
  84. ]],
  85. [[
  86. 'email' => 'john.doe@example.com',
  87. 'password' => true, // string
  88. ]],
  89. ];
  90. }
  91. }