UnitSettingsTest.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Http\Requests\UnitRequest;
  4. use App\Http\Resources\UnitResource;
  5. use App\Models\Unit;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Validator;
  8. use Tests\TestCase;
  9. class UnitSettingsTest extends TestCase
  10. {
  11. public function test_unit_request_validates_active_students_ranges(): void
  12. {
  13. $rules = (new UnitRequest)->rules();
  14. $validValidator = Validator::make([
  15. 'active_students_min' => 10,
  16. 'active_students_max' => 90,
  17. ], $rules);
  18. $this->assertFalse($validValidator->errors()->has('active_students_min'));
  19. $this->assertFalse($validValidator->errors()->has('active_students_max'));
  20. $invalidValidator = Validator::make([
  21. 'active_students_min' => -5, // Below 0
  22. 'active_students_max' => 'string', // Not an integer
  23. ], $rules);
  24. $this->assertTrue($invalidValidator->errors()->has('active_students_min'));
  25. $this->assertTrue($invalidValidator->errors()->has('active_students_max'));
  26. }
  27. public function test_unit_resource_includes_active_students_ranges(): void
  28. {
  29. $unit = current(array_filter([new class extends Unit {
  30. public $active_students_min = 25;
  31. public $active_students_max = 85;
  32. public $name = 'Teste';
  33. public $social_reason = 'Teste';
  34. }]));
  35. $resource = new UnitResource($unit);
  36. $array = $resource->toArray(Request::create('/'));
  37. $this->assertArrayHasKey('active_students_min', $array);
  38. $this->assertArrayHasKey('active_students_max', $array);
  39. $this->assertSame(25, $array['active_students_min']);
  40. $this->assertSame(85, $array['active_students_max']);
  41. }
  42. }