SettingStoreRequestTest.php 2.6 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. class SettingStoreRequestTest extends FeatureTestCase
  9. {
  10. use WithoutMiddleware;
  11. /**
  12. *
  13. */
  14. protected String $uniqueKey = 'UniqueKey';
  15. /**
  16. * @test
  17. */
  18. public function test_user_is_authorized()
  19. {
  20. Auth::shouldReceive('check')
  21. ->once()
  22. ->andReturn(true);
  23. $request = new SettingStoreRequest();
  24. $this->assertTrue($request->authorize());
  25. }
  26. /**
  27. * @dataProvider provideValidData
  28. */
  29. public function test_valid_data(array $data) : void
  30. {
  31. $request = new SettingStoreRequest();
  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. 'key' => 'MyKey',
  43. 'value' => true
  44. ]],
  45. [[
  46. 'key' => 'MyKey',
  47. 'value' => 'MyValue'
  48. ]],
  49. [[
  50. 'key' => 'MyKey',
  51. 'value' => 10
  52. ]],
  53. ];
  54. }
  55. /**
  56. * @dataProvider provideInvalidData
  57. */
  58. public function test_invalid_data(array $data) : void
  59. {
  60. $settingService = resolve('App\Services\SettingService');
  61. $settingService->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. }