Add tests for API fixture cleanup and optimize customer trace removal logic

This commit is contained in:
Jeppe Bundgaard
2026-04-15 09:17:46 +02:00
parent 0b672e13a6
commit 40ded9ed95
5 changed files with 979 additions and 5 deletions
@@ -54,6 +54,7 @@ if (!class_exists('TestableEconomicV2DistributionService')) {
public array $stubVersionRows = [];
public array $subscriptionPrices = [];
public array $versionTableState = [];
public ?array $includedCustomers = null;
public function __construct(?economic_v2_versioning_service $versioning = null)
{
@@ -89,6 +90,16 @@ if (!class_exists('TestableEconomicV2DistributionService')) {
return (float)($this->subscriptionPrices[$vehicle_type] ?? 0.0);
}
protected function getProductDepartmentPrice(int $product_id, int $department_id): float
{
return 100.0;
}
protected function resolveDiscountForProduct(int $customer_number, int $product_id, string $timestamp): ?array
{
return null;
}
protected function buildCustomerEnvelope(int $customer_number, array $transaction_map): array
{
return [
@@ -118,6 +129,15 @@ if (!class_exists('TestableEconomicV2DistributionService')) {
return $department_id > 0 && $department_id !== 10;
}
protected function shouldIncludeCustomerNumber(int $customer_number): bool
{
if ($this->includedCustomers === null) {
return true;
}
return in_array($customer_number, $this->includedCustomers, true);
}
protected function parseDepartmentMap(array $department_map): array
{
$parsed = [];
@@ -261,3 +281,116 @@ it('falls back to system orders for wash subscriptions when regular orders yield
expect($result['collective_results']['total_subscription_price'])->toBe(299.0);
expect($result['warnings'])->toContain('System order fallback used for wash subscriptions (department 10).');
});
it('excludes orphaned customer traces from fixed pricing and customer price distributions', function (): void {
$versioning = new FakeEconomicV2DistributionVersioningService();
$versioning->fixedVersion = [
'id' => 91,
'price' => 499.95,
'description' => 'Fixed pricing agreement',
'source' => 'test.fixed',
'confidence' => 1.0,
'inferred' => false,
'effective_from' => '2026-01-01 00:00:00',
'effective_to' => null,
];
$service = new TestableEconomicV2DistributionService($versioning);
$service->includedCustomers = [12345];
$service->stubOrders = [
[
'id' => 501,
'customer_id' => 80000038,
'department_id' => 1,
'created_at' => '2026-01-15 10:00:00',
'include_in_invoice' => 1,
'reg_1' => '',
'reference' => '',
],
[
'id' => 502,
'customer_id' => 12345,
'department_id' => 1,
'created_at' => '2026-01-16 10:00:00',
'include_in_invoice' => 1,
'reg_1' => '',
'reference' => '',
],
];
$service->stubOrderItems = [
501 => [[
'product_id' => 61,
'price' => 250.0,
'quantity' => 1,
'reference' => '',
]],
502 => [[
'product_id' => 61,
'price' => 300.0,
'quantity' => 1,
'reference' => '',
]],
];
$fixedPricing = $service->getFixedPricingDistribution('2026-01-01', '2026-01-31');
$customerPrices = $service->getCustomerPricesDistribution('2026-01-01', '2026-01-31');
expect(array_column($fixedPricing['customers'], 'customer_number'))->toBe([12345]);
expect($fixedPricing['collective_results']['total_fixed_price'])->toBe(499.95);
expect($fixedPricing['collective_results']['total_original_price'])->toBe(300.0);
expect(array_column($customerPrices['customers'], 'customer_number'))->toBe([12345]);
expect($customerPrices['collective_results']['total_discount_amount'])->toBe(0.0);
});
it('excludes orphaned customers from subscription fallback versions', function (): void {
$versioning = new FakeEconomicV2DistributionVersioningService();
$service = new TestableEconomicV2DistributionService($versioning);
$service->includedCustomers = [12345];
$service->stubOrders = [[
'id' => 701,
'customer_id' => 12345,
'department_id' => 4,
'created_at' => '2026-01-15 10:00:00',
'include_in_invoice' => 1,
'reg_1' => 'AB12345',
'reference' => '',
]];
$service->subscriptionPrices = [
77 => 299.0,
];
$service->stubVersionRows = [
[
'id' => 41,
'customer_number' => 80000038,
'reg' => 'ZZ99999',
'vehicle_type' => 77,
'wash_subscription' => 1,
'source' => 'test.version',
'confidence' => 1.0,
'inferred' => false,
'effective_from' => '2026-01-01 00:00:00',
'effective_to' => null,
],
[
'id' => 42,
'customer_number' => 12345,
'reg' => 'AB12345',
'vehicle_type' => 77,
'wash_subscription' => 1,
'source' => 'test.version',
'confidence' => 1.0,
'inferred' => false,
'effective_from' => '2026-01-01 00:00:00',
'effective_to' => null,
],
];
$result = $service->getWashSubscriptionsDistribution('2026-01-01', '2026-01-31');
expect(array_column($result['customers'], 'customer_number'))->toBe([12345]);
expect($result['warnings'])->toBe([
'Fallback allocation used for subscription AB12345 customer 12345 in 2026-01',
]);
});