GroupStoreRequestTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace Tests\Api\v1\Requests;
  3. use App\Api\v1\Requests\GroupStoreRequest;
  4. use App\Models\Group;
  5. use App\Models\User;
  6. use Illuminate\Foundation\Testing\WithoutMiddleware;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\Validator;
  9. use Mockery;
  10. use PHPUnit\Framework\Attributes\CoversClass;
  11. use PHPUnit\Framework\Attributes\DataProvider;
  12. use PHPUnit\Framework\Attributes\Test;
  13. use Tests\FeatureTestCase;
  14. /**
  15. * GroupStoreRequestTest test class
  16. */
  17. #[CoversClass(GroupStoreRequest::class)]
  18. class GroupStoreRequestTest extends FeatureTestCase
  19. {
  20. use WithoutMiddleware;
  21. /**
  22. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  23. */
  24. protected $user;
  25. const UNIQUE_GROUP_NAME = 'MyGroup';
  26. protected function setUp() : void
  27. {
  28. parent::setUp();
  29. $this->user = User::factory()->create();
  30. }
  31. #[Test]
  32. public function test_user_is_authorized()
  33. {
  34. Auth::shouldReceive('check')
  35. ->once()
  36. ->andReturn(true);
  37. $request = new GroupStoreRequest;
  38. $this->assertTrue($request->authorize());
  39. }
  40. #[Test]
  41. #[DataProvider('provideValidData')]
  42. public function test_valid_data(array $data) : void
  43. {
  44. $request = Mockery::mock(GroupStoreRequest::class)->makePartial();
  45. $request->shouldReceive('user')
  46. ->andReturn($this->user);
  47. $validator = Validator::make($data, $request->rules());
  48. $this->assertFalse($validator->fails());
  49. }
  50. /**
  51. * Provide Valid data for validation test
  52. */
  53. public static function provideValidData() : array
  54. {
  55. return [
  56. [[
  57. 'name' => 'validWord',
  58. ]],
  59. ];
  60. }
  61. #[Test]
  62. #[DataProvider('provideInvalidData')]
  63. public function test_invalid_data(array $data) : void
  64. {
  65. $group = Group::factory()->for($this->user)->create([
  66. 'name' => self::UNIQUE_GROUP_NAME,
  67. ]);
  68. $request = Mockery::mock(GroupStoreRequest::class)->makePartial();
  69. $request->shouldReceive('user')
  70. ->andReturn($this->user);
  71. $validator = Validator::make($data, $request->rules());
  72. $this->assertTrue($validator->fails());
  73. }
  74. /**
  75. * Provide invalid data for validation test
  76. */
  77. public static function provideInvalidData() : array
  78. {
  79. return [
  80. [[
  81. 'name' => '', // required
  82. ]],
  83. [[
  84. 'name' => true, // string
  85. ]],
  86. [[
  87. 'name' => 'mmmmmmoooooorrrrrreeeeeeettttttthhhhhhaaaaaaannnnnn32cccccchhhhhaaaaaarrrrrrsssssss', // max:32
  88. ]],
  89. [[
  90. 'name' => self::UNIQUE_GROUP_NAME, // unique
  91. ]],
  92. ];
  93. }
  94. }