setLocale('pt'); $student = new TestableStudent; $student->birth_date = now()->subYears(10)->toDateString(); $this->expectException(ValidationException::class); $this->expectExceptionMessage('É obrigatório informar um responsável para estudantes menores de idade.'); $student->fireCreatingEvent(); } public function test_it_allows_a_minor_with_a_responsible_when_creating(): void { $student = (new TestableStudent) ->withResponsibleForCreation(['name' => 'Responsável']); $student->birth_date = now()->subYears(10)->toDateString(); $student->fireCreatingEvent(); $this->assertTrue($student->isMinor()); } public function test_it_allows_an_adult_without_a_responsible_when_creating(): void { $student = new TestableStudent; $student->birth_date = now()->subYears(18)->toDateString(); $student->fireCreatingEvent(); $this->assertFalse($student->isMinor()); } public function test_it_casts_the_student_document_number_to_cpf(): void { $student = new Student; $student->document_number = '529.982.247-25'; $this->assertInstanceOf(Cpf::class, $student->document_number); $this->assertSame('52998224725', $student->document_number->value()); } public function test_it_rejects_deletion_when_the_student_has_an_active_contract(): void { app()->setLocale('pt'); $student = (new TestableStudent)->withActiveContract(); $this->expectException(ValidationException::class); $this->expectExceptionMessage('Não é possível excluir um estudante que possui contratos ativos.'); $student->fireDeletingEvent(); } public function test_it_allows_deletion_when_the_student_has_no_active_contract(): void { $student = new TestableStudent; $this->assertNull($student->fireDeletingEvent()); } } class TestableStudent extends Student { private bool $hasActiveContract = false; public function fireCreatingEvent(): mixed { return $this->fireModelEvent('creating'); } public function fireDeletingEvent(): mixed { return $this->fireModelEvent('deleting'); } public function withActiveContract(): self { $this->hasActiveContract = true; return $this; } public function hasActiveContract(): bool { return $this->hasActiveContract; } }