StudentTest.php 1.8 KB

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