SettingStoreRequestTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Tests\Api\v1\Requests;
  3. use App\Api\v1\Requests\SettingStoreRequest;
  4. use Illuminate\Foundation\Testing\WithoutMiddleware;
  5. use Illuminate\Support\Facades\Validator;
  6. use Illuminate\Support\Facades\Auth;
  7. use Tests\FeatureTestCase;
  8. use App\Facades\Settings;
  9. class SettingStoreRequestTest extends FeatureTestCase
  10. {
  11. use WithoutMiddleware;
  12. /**
  13. *
  14. */
  15. protected String $uniqueKey = 'UniqueKey';
  16. /**
  17. * @test
  18. */
  19. public function test_user_is_authorized()
  20. {
  21. Auth::shouldReceive('check')
  22. ->once()
  23. ->andReturn(true);
  24. $request = new SettingStoreRequest();
  25. $this->assertTrue($request->authorize());
  26. }
  27. /**
  28. * @dataProvider provideValidData
  29. */
  30. public function test_valid_data(array $data) : void
  31. {
  32. $request = new SettingStoreRequest();
  33. $validator = Validator::make($data, $request->rules());
  34. $this->assertFalse($validator->fails());
  35. }
  36. /**
  37. * Provide Valid data for validation test
  38. */
  39. public function provideValidData() : array
  40. {
  41. return [
  42. [[
  43. 'key' => 'MyKey',
  44. 'value' => true
  45. ]],
  46. [[
  47. 'key' => 'MyKey',
  48. 'value' => 'MyValue'
  49. ]],
  50. [[
  51. 'key' => 'MyKey',
  52. 'value' => 10
  53. ]],
  54. ];
  55. }
  56. /**
  57. * @dataProvider provideInvalidData
  58. */
  59. public function test_invalid_data(array $data) : void
  60. {
  61. Settings::set($this->uniqueKey, 'uniqueValue');
  62. $request = new SettingStoreRequest();
  63. $validator = Validator::make($data, $request->rules());
  64. $this->assertTrue($validator->fails());
  65. }
  66. /**
  67. * Provide invalid data for validation test
  68. */
  69. public function provideInvalidData() : array
  70. {
  71. return [
  72. [[
  73. 'key' => null, // required
  74. 'value' => ''
  75. ]],
  76. [[
  77. 'key' => 'my-key', // alpha
  78. 'value' => 'MyValue'
  79. ]],
  80. [[
  81. 'key' => 10, // alpha
  82. 'value' => 'MyValue'
  83. ]],
  84. [[
  85. 'key' => 'mmmmmmoooooorrrrrreeeeeeettttttthhhhhhaaaaaaannnnnn128cccccchhhhhaaaaaarrrrrraaaaaaaccccccttttttttteeeeeeeeerrrrrrrrsssssss', // max:128
  86. 'value' => 'MyValue'
  87. ]],
  88. [[
  89. 'key' => $this->uniqueKey, // unique
  90. 'value' => 'MyValue'
  91. ]],
  92. ];
  93. }
  94. }