GroupStoreRequestTest.php 2.7 KB

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