UserPatchPwdRequestTest.php 3.1 KB

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