GroupStoreRequestTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Tests\Api\v1\Requests;
  3. use App\Group;
  4. use App\Api\v1\Requests\GroupStoreRequest;
  5. use Illuminate\Foundation\Testing\WithoutMiddleware;
  6. use Illuminate\Support\Facades\Validator;
  7. use Illuminate\Support\Facades\Auth;
  8. use Tests\FeatureTestCase;
  9. class GroupStoreRequestTest extends FeatureTestCase
  10. {
  11. use WithoutMiddleware;
  12. /**
  13. *
  14. */
  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. }