Перевод на доменную архитектуру
This commit is contained in:
38
tests/Unit/Reports/BedDaysCalculatorTest.php
Normal file
38
tests/Unit/Reports/BedDaysCalculatorTest.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
use App\Domain\Reports\Calculators\BedDaysCalculator;
|
||||
use App\Domain\Reports\Models\StayInterval;
|
||||
|
||||
it('calculates total and average bed days', function () {
|
||||
$calculator = new BedDaysCalculator;
|
||||
|
||||
$result = $calculator->calculate([
|
||||
new StayInterval(
|
||||
startAt: new DateTimeImmutable('2026-04-01 10:00:00'),
|
||||
endAt: new DateTimeImmutable('2026-04-04 09:00:00'),
|
||||
),
|
||||
new StayInterval(
|
||||
startAt: new DateTimeImmutable('2026-04-05 10:00:00'),
|
||||
endAt: new DateTimeImmutable('2026-04-07 09:00:00'),
|
||||
),
|
||||
]);
|
||||
|
||||
expect($result->total)->toBe(5)
|
||||
->and($result->count)->toBe(2)
|
||||
->and($result->average)->toBe(2.5);
|
||||
});
|
||||
|
||||
it('ignores invalid bed day intervals', function () {
|
||||
$calculator = new BedDaysCalculator;
|
||||
|
||||
$result = $calculator->calculate([
|
||||
new StayInterval(
|
||||
startAt: new DateTimeImmutable('2026-04-04 10:00:00'),
|
||||
endAt: new DateTimeImmutable('2026-04-01 09:00:00'),
|
||||
),
|
||||
]);
|
||||
|
||||
expect($result->total)->toBe(0)
|
||||
->and($result->count)->toBe(0)
|
||||
->and($result->average)->toBe(0.0);
|
||||
});
|
||||
47
tests/Unit/Reports/CompareLegacyAndNewReportUseCaseTest.php
Normal file
47
tests/Unit/Reports/CompareLegacyAndNewReportUseCaseTest.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
use App\Application\Reports\CompareLegacyAndNewReportUseCase;
|
||||
use App\Application\Reports\DTO\GenerateReportInput;
|
||||
use App\Domain\Reports\Contracts\ReportRepository;
|
||||
use App\Domain\Reports\Models\ReportSnapshot;
|
||||
use App\Infrastructure\Reports\Adapters\LegacyReportServiceAdapter;
|
||||
|
||||
it('returns matched when saved snapshot equals expected snapshot', function () {
|
||||
$input = new GenerateReportInput(
|
||||
departmentId: 10,
|
||||
userId: 5015,
|
||||
actorUserId: 15,
|
||||
periodStart: new DateTimeImmutable('2026-04-08 06:00:00'),
|
||||
periodEnd: new DateTimeImmutable('2026-04-09 06:00:00'),
|
||||
reportType: 'daily',
|
||||
rawPayload: [
|
||||
'departmentId' => 10,
|
||||
'userId' => 5015,
|
||||
'dates' => [1744063200, 1744149600],
|
||||
'metrics' => ['metrika_item_4' => 11],
|
||||
'observationPatients' => [],
|
||||
'unwantedEvents' => [],
|
||||
],
|
||||
persistedReportId: 55,
|
||||
);
|
||||
|
||||
$snapshot = new ReportSnapshot(
|
||||
departmentId: 10,
|
||||
userId: 5015,
|
||||
actorUserId: 15,
|
||||
periodStart: new DateTimeImmutable('2026-04-08 06:00:00'),
|
||||
periodEnd: new DateTimeImmutable('2026-04-09 06:00:00'),
|
||||
metrics: [4 => 11],
|
||||
);
|
||||
|
||||
$repository = \Mockery::mock(ReportRepository::class);
|
||||
$repository->shouldReceive('findSnapshot')->once()->with(55)->andReturn($snapshot);
|
||||
|
||||
$adapter = \Mockery::mock(LegacyReportServiceAdapter::class);
|
||||
$adapter->shouldReceive('buildSnapshotFromInput')->once()->with($input)->andReturn($snapshot);
|
||||
|
||||
$result = (new CompareLegacyAndNewReportUseCase($repository, $adapter))->handle($input);
|
||||
|
||||
expect($result->status)->toBe('matched')
|
||||
->and($result->diff)->toBe([]);
|
||||
});
|
||||
15
tests/Unit/Reports/DepartmentLoadCalculatorTest.php
Normal file
15
tests/Unit/Reports/DepartmentLoadCalculatorTest.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
use App\Domain\Reports\Calculators\DepartmentLoadCalculator;
|
||||
|
||||
it('calculates department load percentage', function () {
|
||||
$calculator = new DepartmentLoadCalculator;
|
||||
|
||||
expect($calculator->calculate(27, 30))->toBe(90);
|
||||
});
|
||||
|
||||
it('returns zero when beds count is zero', function () {
|
||||
$calculator = new DepartmentLoadCalculator;
|
||||
|
||||
expect($calculator->calculate(27, 0))->toBe(0);
|
||||
});
|
||||
92
tests/Unit/Reports/GenerateReportUseCaseTest.php
Normal file
92
tests/Unit/Reports/GenerateReportUseCaseTest.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
use App\Application\Reports\CompareLegacyAndNewReportUseCase;
|
||||
use App\Application\Reports\DTO\GenerateReportInput;
|
||||
use App\Application\Reports\DTO\ReportComparisonResult;
|
||||
use App\Application\Reports\GenerateReportUseCase;
|
||||
use App\Domain\Reports\Contracts\AuditLogger;
|
||||
use App\Domain\Reports\Contracts\PatientSource;
|
||||
use App\Domain\Reports\Contracts\ReportRepository;
|
||||
use App\Domain\Reports\Models\PatientCollection;
|
||||
use App\Domain\Reports\Models\ReportContext;
|
||||
use App\Domain\Reports\Models\ReportSnapshot;
|
||||
use App\Domain\Reports\Models\SavedReportResult;
|
||||
use App\Infrastructure\Reports\Adapters\LegacyReportServiceAdapter;
|
||||
|
||||
it('stores generated report and writes comparison audit', function () {
|
||||
$input = new GenerateReportInput(
|
||||
departmentId: 10,
|
||||
userId: 5015,
|
||||
actorUserId: 15,
|
||||
periodStart: new DateTimeImmutable('2026-04-08 06:00:00'),
|
||||
periodEnd: new DateTimeImmutable('2026-04-09 06:00:00'),
|
||||
reportType: 'daily',
|
||||
autoFill: true,
|
||||
);
|
||||
|
||||
$patientSource = new class implements PatientSource
|
||||
{
|
||||
public function load(ReportContext $context): PatientCollection
|
||||
{
|
||||
return new PatientCollection([], [
|
||||
'payload' => [
|
||||
'departmentId' => $context->departmentId,
|
||||
'userId' => $context->userId,
|
||||
'dates' => [$context->periodStart->getTimestamp(), $context->periodEnd->getTimestamp()],
|
||||
'status' => 'submitted',
|
||||
'metrics' => ['metrika_item_4' => 11],
|
||||
'observationPatients' => [],
|
||||
'unwantedEvents' => [],
|
||||
],
|
||||
]);
|
||||
}
|
||||
};
|
||||
|
||||
$snapshot = new ReportSnapshot(
|
||||
departmentId: 10,
|
||||
userId: 5015,
|
||||
actorUserId: 15,
|
||||
periodStart: new DateTimeImmutable('2026-04-08 06:00:00'),
|
||||
periodEnd: new DateTimeImmutable('2026-04-09 06:00:00'),
|
||||
status: 'submitted',
|
||||
autoFill: true,
|
||||
metrics: [4 => 11],
|
||||
);
|
||||
|
||||
$repository = \Mockery::mock(ReportRepository::class);
|
||||
$repository->shouldReceive('save')
|
||||
->once()
|
||||
->andReturn(new SavedReportResult(88, $snapshot));
|
||||
|
||||
$repository->shouldReceive('findSnapshot')
|
||||
->once()
|
||||
->with(88)
|
||||
->andReturn($snapshot);
|
||||
|
||||
$adapter = \Mockery::mock(LegacyReportServiceAdapter::class);
|
||||
$adapter->shouldReceive('buildSnapshotFromInput')
|
||||
->once()
|
||||
->andReturn($snapshot);
|
||||
|
||||
$comparator = new CompareLegacyAndNewReportUseCase($repository, $adapter);
|
||||
|
||||
$auditLogger = \Mockery::mock(AuditLogger::class);
|
||||
$auditLogger->shouldReceive('logComparison')->once();
|
||||
|
||||
$useCase = new GenerateReportUseCase(
|
||||
reportRepository: $repository,
|
||||
auditLogger: $auditLogger,
|
||||
comparator: $comparator,
|
||||
patientSource: $patientSource,
|
||||
calculators: [],
|
||||
compareBeforeCutover: true,
|
||||
);
|
||||
|
||||
$result = $useCase->handle($input);
|
||||
|
||||
expect($result->reportId)->toBe(88)
|
||||
->and($result->usedNewArchitecture)->toBeTrue()
|
||||
->and($result->comparison?->status)->toBe('matched')
|
||||
->and($result->comparison?->diff)->toBe([])
|
||||
->and($result->comparison?->reportId)->toBe(88);
|
||||
});
|
||||
28
tests/Unit/Reports/MetrikaConfigTest.php
Normal file
28
tests/Unit/Reports/MetrikaConfigTest.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use App\Domain\Reports\ValueObjects\MetrikaConfig;
|
||||
|
||||
it('normalizes metrics from payload keys and numeric ids', function () {
|
||||
$normalized = MetrikaConfig::normalizeMetrics([
|
||||
'metrika_item_12' => 7,
|
||||
4 => 11,
|
||||
'invalid' => 100,
|
||||
'15' => 3,
|
||||
]);
|
||||
|
||||
expect($normalized)->toBe([
|
||||
4 => 11,
|
||||
12 => 7,
|
||||
15 => 3,
|
||||
]);
|
||||
});
|
||||
|
||||
it('converts normalized metrics back to payload keys', function () {
|
||||
expect(MetrikaConfig::toPayloadMetrics([
|
||||
4 => 11,
|
||||
12 => 7,
|
||||
]))->toBe([
|
||||
'metrika_item_4' => 11,
|
||||
'metrika_item_12' => 7,
|
||||
]);
|
||||
});
|
||||
38
tests/Unit/Reports/PreoperativeDaysCalculatorTest.php
Normal file
38
tests/Unit/Reports/PreoperativeDaysCalculatorTest.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
use App\Domain\Reports\Calculators\PreoperativeDaysCalculator;
|
||||
use App\Domain\Reports\Models\OperationInterval;
|
||||
|
||||
it('calculates total and average preoperative days', function () {
|
||||
$calculator = new PreoperativeDaysCalculator;
|
||||
|
||||
$result = $calculator->calculate([
|
||||
new OperationInterval(
|
||||
admittedAt: new DateTimeImmutable('2026-04-01 10:00:00'),
|
||||
operationAt: new DateTimeImmutable('2026-04-03 09:00:00'),
|
||||
),
|
||||
new OperationInterval(
|
||||
admittedAt: new DateTimeImmutable('2026-04-05 10:00:00'),
|
||||
operationAt: new DateTimeImmutable('2026-04-06 09:00:00'),
|
||||
),
|
||||
]);
|
||||
|
||||
expect($result->total)->toBe(3)
|
||||
->and($result->count)->toBe(2)
|
||||
->and($result->average)->toBe(1.5);
|
||||
});
|
||||
|
||||
it('ignores invalid preoperative intervals', function () {
|
||||
$calculator = new PreoperativeDaysCalculator;
|
||||
|
||||
$result = $calculator->calculate([
|
||||
new OperationInterval(
|
||||
admittedAt: new DateTimeImmutable('2026-04-03 10:00:00'),
|
||||
operationAt: new DateTimeImmutable('2026-04-01 09:00:00'),
|
||||
),
|
||||
]);
|
||||
|
||||
expect($result->total)->toBe(0)
|
||||
->and($result->count)->toBe(0)
|
||||
->and($result->average)->toBe(0.0);
|
||||
});
|
||||
58
tests/Unit/Reports/ReportInputFactoryTest.php
Normal file
58
tests/Unit/Reports/ReportInputFactoryTest.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
use App\Application\Reports\ReportInputFactory;
|
||||
use App\Models\Department;
|
||||
use App\Models\User;
|
||||
use App\Services\DateRange;
|
||||
use App\Services\DateRangeService;
|
||||
use Carbon\Carbon;
|
||||
|
||||
it('builds manual generate report input from validated payload', function () {
|
||||
$user = new User;
|
||||
$user->id = 15;
|
||||
|
||||
$factory = new ReportInputFactory(app(DateRangeService::class));
|
||||
|
||||
$input = $factory->forManualSave($user, [
|
||||
'departmentId' => 10,
|
||||
'userId' => 5015,
|
||||
'dates' => [1744063200, 1744149600],
|
||||
'metrics' => ['metrika_item_4' => 11],
|
||||
'observationPatients' => [['id' => 100]],
|
||||
'unwantedEvents' => [['title' => 'A']],
|
||||
'status' => 'draft',
|
||||
'reportId' => 55,
|
||||
]);
|
||||
|
||||
expect($input->departmentId)->toBe(10)
|
||||
->and($input->userId)->toBe(5015)
|
||||
->and($input->actorUserId)->toBe(15)
|
||||
->and($input->reportId)->toBe(55)
|
||||
->and($input->metrics)->toBe(['metrika_item_4' => 11])
|
||||
->and($input->rawPayload['actorUserId'])->toBe(15);
|
||||
});
|
||||
|
||||
it('builds auto fill generate report input from scoped user and date range', function () {
|
||||
$user = new User;
|
||||
$user->id = 15;
|
||||
$user->rf_lpudoctor_id = 5015;
|
||||
|
||||
$department = new Department;
|
||||
$department->department_id = 10;
|
||||
|
||||
$dateRange = new DateRange(
|
||||
startDate: Carbon::parse('2026-04-08 06:00:00', 'Asia/Yakutsk'),
|
||||
endDate: Carbon::parse('2026-04-09 06:00:00', 'Asia/Yakutsk'),
|
||||
startDateRaw: '2026-04-08 06:00:00',
|
||||
endDateRaw: '2026-04-09 06:00:00',
|
||||
isOneDay: true,
|
||||
);
|
||||
|
||||
$factory = new ReportInputFactory(app(DateRangeService::class));
|
||||
$input = $factory->forAutoFill($user, $department, $dateRange);
|
||||
|
||||
expect($input->autoFill)->toBeTrue()
|
||||
->and($input->status)->toBe('submitted')
|
||||
->and($input->departmentId)->toBe(10)
|
||||
->and($input->userId)->toBe(5015);
|
||||
});
|
||||
148
tests/Unit/Reports/ReportPatientsReadServiceTest.php
Normal file
148
tests/Unit/Reports/ReportPatientsReadServiceTest.php
Normal file
@@ -0,0 +1,148 @@
|
||||
<?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);
|
||||
});
|
||||
36
tests/Unit/Reports/ReportSnapshotTest.php
Normal file
36
tests/Unit/Reports/ReportSnapshotTest.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use App\Domain\Reports\Models\ReportSnapshot;
|
||||
|
||||
it('normalizes metrics and exposes comparable payload', function () {
|
||||
$snapshot = new ReportSnapshot(
|
||||
departmentId: 10,
|
||||
userId: 5015,
|
||||
actorUserId: 15,
|
||||
periodStart: new DateTimeImmutable('2026-04-08 06:00:00'),
|
||||
periodEnd: new DateTimeImmutable('2026-04-09 06:00:00'),
|
||||
status: 'submitted',
|
||||
autoFill: true,
|
||||
metrics: [
|
||||
'metrika_item_12' => 7,
|
||||
4 => 11,
|
||||
],
|
||||
observationPatients: [['medical_history_id' => 100]],
|
||||
unwantedEvents: [['title' => 'Event']],
|
||||
);
|
||||
|
||||
expect($snapshot->normalizedMetrics())->toBe([
|
||||
4 => 11,
|
||||
12 => 7,
|
||||
])->and($snapshot->toComparableArray()['auto_fill'])->toBeTrue();
|
||||
});
|
||||
|
||||
it('rejects invalid period ranges', function () {
|
||||
new ReportSnapshot(
|
||||
departmentId: 10,
|
||||
userId: 5015,
|
||||
actorUserId: 15,
|
||||
periodStart: new DateTimeImmutable('2026-04-09 06:00:00'),
|
||||
periodEnd: new DateTimeImmutable('2026-04-08 06:00:00'),
|
||||
);
|
||||
})->throws(InvalidArgumentException::class);
|
||||
Reference in New Issue
Block a user