forceFill([ 'department_id' => 100, 'rf_mis_department_id' => 200, ]); $user = \Mockery::mock(User::class); $dateRange = reportStatisticsDateRange(); $contextResolver = \Mockery::mock(ReportReadContextResolver::class); $contextResolver->shouldReceive('resolveBranchId') ->once() ->with($department) ->andReturn(null); $service = new ReportStatisticsReadService( unifiedPatientService: \Mockery::mock(UnifiedPatientService::class), patientService: \Mockery::mock(PatientService::class), snapshotService: \Mockery::mock(SnapshotService::class), contextResolver: $contextResolver, calculatedMetricsSynchronizer: \Mockery::mock(CalculatedMetricsSynchronizer::class), ); expect($service->getReportStatistics($department, $user, $dateRange))->toMatchArray([ 'recipientCount' => 0, 'extractCount' => 0, 'currentCount' => 0, 'deadCount' => 0, 'surgicalCount' => [0, 0], 'recipientIds' => [], 'percentDead' => 0, 'beds' => 0, ]); }); it('reads live report statistics from replica when snapshots are not active', function () { $department = (new Department())->forceFill([ 'department_id' => 100, 'rf_mis_department_id' => 200, ]); $department->setRelation('metrikaDefault', collect([ (object) ['rf_metrika_item_id' => 1, 'value' => 42], ])); $user = \Mockery::mock(User::class); $dateRange = reportStatisticsDateRange(); $contextResolver = \Mockery::mock(ReportReadContextResolver::class); $contextResolver->shouldReceive('resolveBranchId') ->once() ->with($department) ->andReturn(10); $contextResolver->shouldReceive('shouldUseSnapshots') ->once() ->with($department, $dateRange) ->andReturn(false); $unifiedPatientService = \Mockery::mock(UnifiedPatientService::class); $unifiedPatientService->shouldReceive('getLivePatientCountByStatus') ->once() ->with($department, $user, 'plan', $dateRange, 10, true) ->andReturn(7); $unifiedPatientService->shouldReceive('getLivePatientCountByStatus') ->once() ->with($department, $user, 'emergency', $dateRange, 10, true) ->andReturn(5); $unifiedPatientService->shouldReceive('getLivePatientCountByStatus') ->once() ->with($department, $user, 'current', $dateRange, 10) ->andReturn(30); $unifiedPatientService->shouldReceive('getLivePatientCountByStatus') ->once() ->with($department, $user, 'recipient', $dateRange, 10) ->andReturn(4); $unifiedPatientService->shouldReceive('getLivePatientCountByStatus') ->once() ->with($department, $user, 'outcome', $dateRange, 10) ->andReturn(8); $unifiedPatientService->shouldReceive('getLivePatientCountByStatus') ->once() ->with($department, $user, 'outcome-deceased', $dateRange, 10) ->andReturn(2); $unifiedPatientService->shouldReceive('getRecipientIdsForReport') ->once() ->with($department, $user, $dateRange, 10) ->andReturn([11, 12]); $patientService = \Mockery::mock(PatientService::class); $patientService->shouldReceive('getSurgicalPatients') ->once() ->with('emergency', 10, $dateRange, true) ->andReturn(3); $patientService->shouldReceive('getSurgicalPatients') ->once() ->with('plan', 10, $dateRange, true) ->andReturn(6); $calculatedMetricsSynchronizer = \Mockery::mock(CalculatedMetricsSynchronizer::class); $calculatedMetricsSynchronizer->shouldReceive('getManualSurgicalCounts') ->once() ->with($department, $dateRange) ->andReturn([1, 2]); $service = new ReportStatisticsReadService( unifiedPatientService: $unifiedPatientService, patientService: $patientService, snapshotService: \Mockery::mock(SnapshotService::class), contextResolver: $contextResolver, calculatedMetricsSynchronizer: $calculatedMetricsSynchronizer, ); expect($service->getReportStatistics($department, $user, $dateRange))->toMatchArray([ 'recipientCount' => 4, 'extractCount' => 8, 'currentCount' => 30, 'deadCount' => 2, 'surgicalCount' => [4, 8], 'recipientIds' => [11, 12], 'planCount' => 7, 'emergencyCount' => 5, 'percentDead' => 25.0, 'beds' => 42, ]); }); function reportStatisticsDateRange(): DateRange { return 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, ); }