GroupStoreRequestTest.php 2.2 KB

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