SettingStoreRequestTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. $validator = Validator::make($data, $request->rules());
  35. $this->assertFalse($validator->fails());
  36. }
  37. /**
  38. * Provide Valid data for validation test
  39. */
  40. public static function provideValidData() : array
  41. {
  42. return [
  43. [[
  44. 'key' => 'MyKey',
  45. 'value' => true,
  46. ]],
  47. [[
  48. 'key' => 'MyKey',
  49. 'value' => 'MyValue',
  50. ]],
  51. [[
  52. 'key' => 'MyKey',
  53. 'value' => 10,
  54. ]],
  55. ];
  56. }
  57. #[Test]
  58. #[DataProvider('provideInvalidData')]
  59. public function test_invalid_data(array $data) : void
  60. {
  61. Settings::set(self::UNIQUE_KEY, '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 static 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' => self::UNIQUE_KEY, // unique
  90. 'value' => 'MyValue',
  91. ]],
  92. ];
  93. }
  94. }