@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
use Tempo\Purchasing\Delivery\BootOverlay;
|
||||
|
||||
require_once __DIR__.'/../../integrations/sgf/operational/BootOverlay.php';
|
||||
|
||||
beforeEach(function () {
|
||||
$this->host = __DIR__.'/../../storage/framework/testing/boot-overlay-'.bin2hex(random_bytes(6));
|
||||
mkdir($this->host.'/app/Providers', 0700, true);
|
||||
mkdir($this->host.'/app/Shared/Context', 0700, true);
|
||||
file_put_contents($this->host.'/app/Providers/AppServiceProvider.php', '<?php class Provider { public function register() { $this->app->scoped(CompanyContext::class); }'."\npublic function boot(): void\n {\n }\n}");
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
foreach (['app/Providers/AppServiceProvider.php', 'app/Shared/Context/ModuleNavigation.php'] as $file) {
|
||||
unlink($this->host.'/'.$file);
|
||||
}
|
||||
foreach (['app/Providers', 'app/Shared/Context', 'app/Shared', 'app', ''] as $directory) {
|
||||
rmdir($this->host.($directory === '' ? '' : '/'.$directory));
|
||||
}
|
||||
});
|
||||
|
||||
test('prepares legacy host without changing source files or grants', function () {
|
||||
$file = $this->host.'/app/Shared/Context/ModuleNavigation.php';
|
||||
$before = "<?php class Nav { const RIGHTS=['purchasing'=>['purchasing_budget.', 'supplier_payables.']]; }";
|
||||
file_put_contents($file, $before);
|
||||
$result = BootOverlay::prepare($this->host);
|
||||
expect($result['app/Shared/Context/ModuleNavigation.php']['contents'])->toContain("'purchasing_requisitions.'")
|
||||
->and(file_get_contents($file))->toBe($before);
|
||||
});
|
||||
|
||||
test('preserves expanded host navigation and is repeatable', function () {
|
||||
$before = "<?php class Nav { const RIGHTS=['purchasing' => ['purchasing_budget.', 'purchasing_requisitions.', 'purchasing_orders.', 'purchasing_invoices.', 'supplier_payables.']]; }";
|
||||
file_put_contents($this->host.'/app/Shared/Context/ModuleNavigation.php', $before);
|
||||
$result = BootOverlay::prepare($this->host);
|
||||
expect($result['app/Shared/Context/ModuleNavigation.php']['contents'])->toBe($before);
|
||||
foreach ($result as $file => $entry) {
|
||||
file_put_contents($this->host.'/'.$file, $entry['contents']);
|
||||
}
|
||||
$again = BootOverlay::prepare($this->host);
|
||||
foreach ($result as $file => $entry) {
|
||||
expect($again[$file]['contents'])->toBe($entry['contents']);
|
||||
}
|
||||
});
|
||||
|
||||
test('refuses ambiguous or executable host prefix lists', function (string $navigation) {
|
||||
file_put_contents($this->host.'/app/Shared/Context/ModuleNavigation.php', $navigation);
|
||||
expect(fn () => BootOverlay::prepare($this->host))->toThrow(RuntimeException::class);
|
||||
})->with([
|
||||
'unknown expression' => "<?php class Nav { const RIGHTS=['purchasing'=>['purchasing_budget.', dynamicPrefix(), 'supplier_payables.']]; }",
|
||||
'missing anchor' => "<?php class Nav { const RIGHTS=['purchasing'=>['supplier_payables.']]; }",
|
||||
'duplicate' => "<?php class Nav { const RIGHTS=['purchasing'=>['purchasing_budget.', 'supplier_payables.'], 'purchasing'=>['purchasing_budget.', 'supplier_payables.']]; }",
|
||||
]);
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
use Tempo\Purchasing\Inventory\StockCosting;
|
||||
|
||||
require_once __DIR__.'/../../packages/purchasing/src/StockCosting.php';
|
||||
|
||||
function costingOpening(): array
|
||||
{
|
||||
return ['reference' => 'synthetic-empty-cut', 'quantity_milli' => 0, 'value_minor' => 0, 'layers' => []];
|
||||
}
|
||||
function costingReceipt(int $id, int $quantity, int $value): array
|
||||
{
|
||||
return ['id' => $id, 'reference' => 'synthetic-receipt-'.$id, 'kind' => 'receipt', 'quantity_milli' => $quantity, 'value_minor' => $value];
|
||||
}
|
||||
function costingIssue(int $id, int $quantity): array
|
||||
{
|
||||
return ['id' => $id, 'reference' => 'synthetic-issue-'.$id, 'kind' => 'issue', 'quantity_milli' => $quantity];
|
||||
}
|
||||
|
||||
test('FIFO and weighted average are selected explicitly and give distinct exact costs', function () {
|
||||
$facts = [costingReceipt(1, 1000, 100), costingReceipt(2, 1000, 200), costingIssue(3, 1000)];
|
||||
$fifo = StockCosting::project('fifo', costingOpening(), $facts);
|
||||
$average = StockCosting::project('weighted_average', costingOpening(), $facts);
|
||||
expect($fifo['movements'][2]['value']['amount_minor'])->toBe('100')
|
||||
->and($average['movements'][2]['value']['amount_minor'])->toBe('150')
|
||||
->and($fifo['closing']['value']['amount_minor'])->toBe('200')
|
||||
->and($average['closing']['value']['amount_minor'])->toBe('150');
|
||||
});
|
||||
|
||||
test('partial FIFO layers preserve the full remaining value', function () {
|
||||
$r = StockCosting::project('fifo', costingOpening(), [costingReceipt(1, 3000, 100), costingIssue(2, 1000), costingIssue(3, 2000)]);
|
||||
expect($r['movements'][1]['value'])->toMatchArray(['numerator' => '100', 'denominator' => '3', 'amount_minor' => null, 'rounding_required' => true])
|
||||
->and($r['movements'][2]['value']['numerator'])->toBe('200')
|
||||
->and($r['closing'])->toMatchArray(['quantity_milli' => 0, 'value' => ['numerator' => '0', 'denominator' => '1', 'amount_minor' => '0', 'rounding_required' => false]]);
|
||||
});
|
||||
|
||||
test('unknown receipt costs are not converted to zero', function () {
|
||||
$r = costingReceipt(1, 1000, 100);
|
||||
unset($r['value_minor']);
|
||||
expect(fn () => StockCosting::project('weighted_average', costingOpening(), [$r]))->toThrow(InvalidArgumentException::class);
|
||||
});
|
||||
|
||||
test('FIFO does not invent layers for an opening balance', function () {
|
||||
expect(fn () => StockCosting::project('fifo', ['reference' => 'opening', 'quantity_milli' => 1000, 'value_minor' => 100], []))->toThrow(DomainException::class);
|
||||
});
|
||||
|
||||
test('unknown methods, reordered facts and negative stock are refused', function (string $method, array $facts, string $exception) {
|
||||
expect(fn () => StockCosting::project($method, costingOpening(), $facts))->toThrow($exception);
|
||||
})->with([
|
||||
'unknown method' => ['default', [], InvalidArgumentException::class],
|
||||
'reordered' => ['fifo', [costingReceipt(2, 1000, 100), costingReceipt(1, 1000, 100)], DomainException::class],
|
||||
'shortage' => ['weighted_average', [costingReceipt(1, 1000, 100), costingIssue(2, 1001)], DomainException::class],
|
||||
'override' => ['fifo', [costingReceipt(1, 1000, 100), costingIssue(2, 1000) + ['value_minor' => 1]], InvalidArgumentException::class],
|
||||
]);
|
||||
|
||||
test('explicit zero-value receipt differs from unknown cost', function () {
|
||||
$r = StockCosting::project('fifo', costingOpening(), [costingReceipt(1, 1000, 0), costingIssue(2, 1000)]);
|
||||
expect($r['movements'][1]['value']['amount_minor'])->toBe('0')->and($r['journals_written'])->toBeFalse();
|
||||
});
|
||||
Reference in New Issue
Block a user