SettingStoreRequestTest.php 2.8 KB

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