StudentTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. public function test_it_rejects_deletion_when_the_student_has_an_active_contract(): void
  41. {
  42. app()->setLocale('pt');
  43. $student = (new TestableStudent)->withActiveContract();
  44. $this->expectException(ValidationException::class);
  45. $this->expectExceptionMessage('Não é possível excluir um estudante que possui contratos ativos.');
  46. $student->fireDeletingEvent();
  47. }
  48. public function test_it_allows_deletion_when_the_student_has_no_active_contract(): void
  49. {
  50. $student = new TestableStudent;
  51. $this->assertNull($student->fireDeletingEvent());
  52. }
  53. }
  54. class TestableStudent extends Student
  55. {
  56. private bool $hasActiveContract = false;
  57. public function fireCreatingEvent(): mixed
  58. {
  59. return $this->fireModelEvent('creating');
  60. }
  61. public function fireDeletingEvent(): mixed
  62. {
  63. return $this->fireModelEvent('deleting');
  64. }
  65. public function withActiveContract(): self
  66. {
  67. $this->hasActiveContract = true;
  68. return $this;
  69. }
  70. public function hasActiveContract(): bool
  71. {
  72. return $this->hasActiveContract;
  73. }
  74. }