@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
use Tempo\Finance\Budget\BudgetControl;
|
||||
use Tempo\Finance\Budget\BudgetDistributionPlan;
|
||||
use Tempo\Finance\Budget\BudgetFailure;
|
||||
|
||||
// Approved common-source extension, exercised without modifying installed artifacts or a database.
|
||||
if (! class_exists(BudgetControl::class)) {
|
||||
require_once (getenv('COMMON_BUDGET_PATH') ?: __DIR__.'/../../packages/budget-control').'/bootstrap.php';
|
||||
}
|
||||
require_once 'C:/laragon/www/sgf-tempo/packages/budget-control/src/BudgetDistributionPlan.php';
|
||||
|
||||
function distributionPlanFixture(): array
|
||||
{
|
||||
return [
|
||||
[12 => ['subtotal_minor' => 10000, 'total_minor' => 11600, 'required_kind' => 'operating'],
|
||||
13 => ['subtotal_minor' => 2000, 'total_minor' => 2000, 'required_kind' => 'operating']],
|
||||
[
|
||||
['allocation_key' => 'line-12-centre-1', 'line_id' => 12, 'centre_id' => 1, 'subtotal_minor' => 3333, 'total_minor' => 3866],
|
||||
['allocation_key' => 'line-12-centre-2', 'line_id' => 12, 'centre_id' => 2, 'subtotal_minor' => 6667, 'total_minor' => 7734],
|
||||
['allocation_key' => 'line-13-centre-1', 'line_id' => 13, 'centre_id' => 1, 'subtotal_minor' => 2000, 'total_minor' => 2000],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
test('explicit allocations reconcile every native line without invented rounding or percentages', function () {
|
||||
[$lines, $input] = distributionPlanFixture();
|
||||
$plan = BudgetDistributionPlan::normalise($input, $lines);
|
||||
expect(array_sum(array_column($plan, 'subtotal_minor')))->toBe(12000)
|
||||
->and(array_sum(array_column($plan, 'total_minor')))->toBe(13600)
|
||||
->and($plan[0]['subtotal_minor'])->toBe(3333)->and($plan[0]['total_minor'])->toBe(3866)
|
||||
->and($plan[1]['required_kind'])->toBe('operating');
|
||||
$hash = BudgetDistributionPlan::fingerprint($plan, str_repeat('a', 64));
|
||||
expect(BudgetDistributionPlan::fingerprint(BudgetDistributionPlan::normalise(array_reverse($input), $lines), str_repeat('a', 64)))->toBe($hash)
|
||||
->and(BudgetDistributionPlan::fingerprint($plan, str_repeat('b', 64)))->not->toBe($hash);
|
||||
});
|
||||
|
||||
test('missing excessive repeated foreign and ambiguous allocations are rejected before any financial effect', function () {
|
||||
[$lines, $input] = distributionPlanFixture();
|
||||
$cases = [[], [$input[0], $input[1]], [...$input, $input[0]]];
|
||||
foreach ([['subtotal_minor' => -1], ['total_minor' => 3867], ['line_id' => 99], ['centre_id' => 0], ['allocation_key' => '../other'],
|
||||
['subtotal_minor' => '3333'], ['subtotal_minor' => 3333.0], ['total_minor' => 1], ['override' => true]] as $change) {
|
||||
$cases[] = [[...$input[0], ...$change], ...array_slice($input, 1)];
|
||||
}
|
||||
foreach ($cases as $case) {
|
||||
expect(fn () => BudgetDistributionPlan::normalise($case, $lines))->toThrow(BudgetFailure::class);
|
||||
}
|
||||
expect(fn () => BudgetDistributionPlan::normalise($input, []))->toThrow(BudgetFailure::class)
|
||||
->and(fn () => BudgetDistributionPlan::fingerprint(BudgetDistributionPlan::normalise($input, $lines), 'unknown'))->toThrow(BudgetFailure::class);
|
||||
});
|
||||
Reference in New Issue
Block a user