Files
onboard/tests/Unit/Reports/ReportPatientsReadServiceTest.php

149 lines
5.0 KiB
PHP

<?php
use App\Infrastructure\Reports\Services\ReportPatientsReadService;
use App\Infrastructure\Reports\Services\ReportReadContextResolver;
use App\Models\Department;
use App\Models\User;
use App\Services\DateRange;
use App\Services\SnapshotService;
use App\Services\UnifiedPatientService;
use Carbon\Carbon;
use Illuminate\Support\Collection;
it('reads one-day plan patients from snapshots when submitted report exists', function () {
$department = (new Department())->forceFill([
'department_id' => 100,
'rf_mis_department_id' => 200,
]);
$user = \Mockery::mock(User::class);
$dateRange = new DateRange(
Carbon::parse('2026-04-08 06:00:00'),
Carbon::parse('2026-04-09 06:00:00'),
'2026-04-08 06:00:00',
'2026-04-09 06:00:00',
true,
);
$snapshotPatients = collect([
(object) ['id' => 'mis:10', 'sourceType' => 'mis'],
(object) ['id' => 'manual:501', 'sourceType' => 'manual'],
]);
$unifiedPatientService = \Mockery::mock(UnifiedPatientService::class);
$snapshotService = \Mockery::mock(SnapshotService::class);
$contextResolver = \Mockery::mock(ReportReadContextResolver::class);
$contextResolver->shouldReceive('resolveBranchId')
->once()
->with($department)
->andReturn(10);
$contextResolver->shouldReceive('shouldUseReplicaForLiveStatus')
->once()
->with($user, 'plan', $dateRange)
->andReturn(false);
$contextResolver->shouldReceive('shouldUseSnapshots')
->once()
->with($department, $dateRange, false)
->andReturn(true);
$contextResolver->shouldReceive('getReportsForDateRange')
->once()
->with(100, $dateRange)
->andReturn(collect([(object) ['report_id' => 91]]));
$contextResolver->shouldReceive('getRecipientReportIds')
->once()
->with([91])
->andReturn([91]);
$snapshotService->shouldReceive('getPatientsFromOneDayCurrentSnapshots')
->once()
->with('plan', [91], false, [91])
->andReturn($snapshotPatients);
$service = new ReportPatientsReadService(
unifiedPatientService: $unifiedPatientService,
snapshotService: $snapshotService,
contextResolver: $contextResolver,
);
$patientIds = $service->getPatientsByStatus($department, $user, 'mis-plan', $dateRange, true);
expect($patientIds)->toBeInstanceOf(Collection::class)
->and($patientIds->all())->toBe(['mis:10']);
});
it('always reads reanimation patients from replica sources', function () {
$department = (new Department())->forceFill([
'department_id' => 100,
'rf_mis_department_id' => 200,
]);
$user = \Mockery::mock(User::class);
$dateRange = new DateRange(
Carbon::parse('2026-04-08 06:00:00'),
Carbon::parse('2026-04-09 06:00:00'),
'2026-04-08 06:00:00',
'2026-04-09 06:00:00',
true,
);
$expected = collect([(object) ['id' => 'mis:55']]);
$unifiedPatientService = \Mockery::mock(UnifiedPatientService::class);
$snapshotService = \Mockery::mock(SnapshotService::class);
$contextResolver = \Mockery::mock(ReportReadContextResolver::class);
$contextResolver->shouldReceive('resolveBranchId')
->once()
->with($department)
->andReturn(10);
$unifiedPatientService->shouldReceive('getLivePatientsByStatus')
->once()
->with($department, $user, 'reanimation', $dateRange, 10, false, true)
->andReturn($expected);
$service = new ReportPatientsReadService(
unifiedPatientService: $unifiedPatientService,
snapshotService: $snapshotService,
contextResolver: $contextResolver,
);
expect($service->getPatientsByStatus($department, $user, 'reanimation', $dateRange))->toBe($expected);
});
it('counts scoped replica patients through unified patient service', function () {
$department = (new Department())->forceFill([
'department_id' => 100,
'rf_mis_department_id' => 200,
]);
$user = \Mockery::mock(User::class);
$dateRange = new DateRange(
Carbon::parse('2026-04-08 06:00:00'),
Carbon::parse('2026-04-09 06:00:00'),
'2026-04-08 06:00:00',
'2026-04-09 06:00:00',
true,
);
$unifiedPatientService = \Mockery::mock(UnifiedPatientService::class);
$snapshotService = \Mockery::mock(SnapshotService::class);
$contextResolver = \Mockery::mock(ReportReadContextResolver::class);
$contextResolver->shouldReceive('resolveBranchId')
->once()
->with($department)
->andReturn(10);
$unifiedPatientService->shouldReceive('getLivePatientCountByStatus')
->once()
->with($department, $user, 'special-plan', $dateRange, 10, true)
->andReturn(3);
$service = new ReportPatientsReadService(
unifiedPatientService: $unifiedPatientService,
snapshotService: $snapshotService,
contextResolver: $contextResolver,
);
expect($service->getPatientsCountByStatus($department, $user, 'special-plan', $dateRange))->toBe(3);
});