StudentTest.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Tests\Unit\Models;
  3. use App\Models\Student;
  4. use Illuminate\Validation\ValidationException;
  5. use Tests\TestCase;
  6. class StudentTest extends TestCase
  7. {
  8. public function test_it_rejects_a_minor_without_a_responsible_when_creating(): void
  9. {
  10. app()->setLocale('pt');
  11. $student = new TestableStudent;
  12. $student->birth_date = now()->subYears(10)->toDateString();
  13. $this->expectException(ValidationException::class);
  14. $this->expectExceptionMessage('É obrigatório informar um responsável para estudantes menores de idade.');
  15. $student->fireCreatingEvent();
  16. }
  17. public function test_it_allows_a_minor_with_a_responsible_when_creating(): void
  18. {
  19. $student = (new TestableStudent)
  20. ->withResponsibleForCreation(['name' => 'Responsável']);
  21. $student->birth_date = now()->subYears(10)->toDateString();
  22. $student->fireCreatingEvent();
  23. $this->assertTrue($student->isMinor());
  24. }
  25. public function test_it_allows_an_adult_without_a_responsible_when_creating(): void
  26. {
  27. $student = new TestableStudent;
  28. $student->birth_date = now()->subYears(18)->toDateString();
  29. $student->fireCreatingEvent();
  30. $this->assertFalse($student->isMinor());
  31. }
  32. }
  33. class TestableStudent extends Student
  34. {
  35. public function fireCreatingEvent(): mixed
  36. {
  37. return $this->fireModelEvent('creating');
  38. }
  39. }