173 lines
5.3 KiB
PHP
173 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Services\Administrator;
|
|
|
|
use App\Models\ClassSection;
|
|
use App\Models\TeacherSubmissionNotificationHistory;
|
|
use App\Models\User;
|
|
use App\Services\Administrator\AdministratorSharedService;
|
|
use App\Services\Administrator\TeacherSubmissionNotificationService;
|
|
use App\Services\Administrator\TeacherSubmissionSupportService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Mockery;
|
|
use Tests\TestCase;
|
|
|
|
class TeacherSubmissionNotificationServiceTest extends TestCase
|
|
{
|
|
protected function tearDown(): void
|
|
{
|
|
Mockery::close();
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function test_send_returns_validation_style_error_when_notify_missing(): void
|
|
{
|
|
$shared = Mockery::mock(AdministratorSharedService::class);
|
|
$support = new TeacherSubmissionSupportService();
|
|
|
|
$service = new TeacherSubmissionNotificationService($shared, $support);
|
|
|
|
$request = new Request([]);
|
|
|
|
$result = $service->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']);
|
|
}
|
|
} |