send($request, 1); $this->assertFalse($result['success']); $this->assertSame(422, $result['status']); } public function test_send_sends_email_and_logs_history(): void { $shared = Mockery::mock(AdministratorSharedService::class); $shared->shouldReceive('getSchoolYear')->andReturn('2025-2026'); $shared->shouldReceive('getSemester')->andReturn('Fall'); $support = new TeacherSubmissionSupportService(); $request = new Request([ 'notify' => [ 1 => [ 5 => 1, ], ], 'missing_items' => [ 1 => [ 5 => base64_encode(json_encode(['midterm scores', 'attendance'])), ], ], ]); $section = new ClassSection(); $section->class_section_id = 1; $section->class_section_name = 'Grade 5A'; $teacher = new User(); $teacher->id = 5; $teacher->firstname = 'John'; $teacher->lastname = 'Teacher'; $teacher->email = 'teacher@test.com'; $admin = new User(); $admin->id = 1; $admin->firstname = 'Admin'; $admin->lastname = 'User'; ClassSection::shouldReceive('query->select->whereIn->get->keyBy') ->once() ->andReturn(collect([1 => $section])); User::shouldReceive('query->select->whereIn->get->keyBy') ->once() ->andReturn(collect([5 => $teacher])); User::shouldReceive('find') ->once() ->with(1) ->andReturn($admin); Mail::fake(); TeacherSubmissionNotificationHistory::shouldReceive('create') ->once() ->with(Mockery::on(function ($data) { return $data['teacher_id'] === 5 && $data['class_section_id'] === 1 && $data['admin_id'] === 1 && $data['notification_category'] === 'teacher_submissions' && $data['status'] === 'sent'; })) ->andReturnTrue(); $service = new TeacherSubmissionNotificationService($shared, $support); $result = $service->send($request, 1); $this->assertTrue($result['success']); $this->assertSame(200, $result['status']); $this->assertSame(1, $result['sent']); $this->assertSame(0, $result['failed']); Mail::assertSentCount(1); } public function test_send_marks_failed_when_teacher_email_invalid(): void { $shared = Mockery::mock(AdministratorSharedService::class); $shared->shouldReceive('getSchoolYear')->andReturn('2025-2026'); $shared->shouldReceive('getSemester')->andReturn('Fall'); $support = new TeacherSubmissionSupportService(); $request = new Request([ 'notify' => [ 1 => [ 5 => 1, ], ], ]); $section = new ClassSection(); $section->class_section_id = 1; $section->class_section_name = 'Grade 5A'; $teacher = new User(); $teacher->id = 5; $teacher->firstname = 'John'; $teacher->lastname = 'Teacher'; $teacher->email = 'invalid-email'; $admin = new User(); $admin->id = 1; $admin->firstname = 'Admin'; $admin->lastname = 'User'; ClassSection::shouldReceive('query->select->whereIn->get->keyBy') ->once() ->andReturn(collect([1 => $section])); User::shouldReceive('query->select->whereIn->get->keyBy') ->once() ->andReturn(collect([5 => $teacher])); User::shouldReceive('find') ->once() ->with(1) ->andReturn($admin); TeacherSubmissionNotificationHistory::shouldReceive('create') ->once() ->with(Mockery::on(function ($data) { return $data['status'] === 'failed'; })) ->andReturnTrue(); $service = new TeacherSubmissionNotificationService($shared, $support); $result = $service->send($request, 1); $this->assertFalse($result['success']); $this->assertSame(207, $result['status']); $this->assertSame(0, $result['sent']); $this->assertSame(1, $result['failed']); } }