UnitSettingsTest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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_medium' => 40,
  17. 'active_students_max' => 90,
  18. ], $rules);
  19. $this->assertFalse($validValidator->errors()->has('active_students_min'));
  20. $this->assertFalse($validValidator->errors()->has('active_students_medium'));
  21. $this->assertFalse($validValidator->errors()->has('active_students_max'));
  22. $invalidValidator = Validator::make([
  23. 'active_students_min' => -5, // Below 0
  24. 'active_students_medium' => 'string', // Not an integer
  25. ], $rules);
  26. $this->assertTrue($invalidValidator->errors()->has('active_students_min'));
  27. $this->assertTrue($invalidValidator->errors()->has('active_students_medium'));
  28. }
  29. public function test_unit_resource_includes_active_students_ranges(): void
  30. {
  31. $unit = current(array_filter([new class extends Unit {
  32. public $active_students_min = 25;
  33. public $active_students_medium = 55;
  34. public $active_students_max = 85;
  35. public $name = 'Teste';
  36. public $social_reason = 'Teste';
  37. }]));
  38. $resource = new UnitResource($unit);
  39. $array = $resource->toArray(Request::create('/'));
  40. $this->assertArrayHasKey('active_students_min', $array);
  41. $this->assertArrayHasKey('active_students_medium', $array);
  42. $this->assertArrayHasKey('active_students_max', $array);
  43. $this->assertSame(25, $array['active_students_min']);
  44. $this->assertSame(55, $array['active_students_medium']);
  45. $this->assertSame(85, $array['active_students_max']);
  46. }
  47. }