36 lines
2.0 KiB
PHP
36 lines
2.0 KiB
PHP
<?php
|
|
|
|
use Tempo\Inventory\QuantityConversion;
|
|
|
|
require_once __DIR__.'/../../packages/inventory-write/development/src/QuantityConversion.php';
|
|
|
|
test('explicit box conversion and partial quantities remain exact and reversible', function () {
|
|
expect(QuantityConversion::toBase('2000', '12', '1'))->toBe('24000')
|
|
->and(QuantityConversion::toBase('1500', '12', '1'))->toBe('18000')
|
|
->and(QuantityConversion::toBase('500', '12', '1'))->toBe('6000')
|
|
->and(QuantityConversion::fromBase('24000', '12', '1'))->toBe('2000')
|
|
->and(QuantityConversion::toBase('2000', '3', '2'))->toBe('3000');
|
|
});
|
|
|
|
test('a known zero quantity remains zero while the factor must still be explicit and positive', function () {
|
|
expect(QuantityConversion::toBase('0', '12', '1'))->toBe('0');
|
|
expect(fn () => QuantityConversion::toBase('0', '', '1'))->toThrow(InvalidArgumentException::class);
|
|
});
|
|
|
|
test('conversion rejects nonrepresentable amounts instead of losing physical stock through rounding', function () {
|
|
expect(fn () => QuantityConversion::toBase('1', '1', '2'))->toThrow(InvalidArgumentException::class)
|
|
->and(fn () => QuantityConversion::fromBase('1', '12', '1'))->toThrow(InvalidArgumentException::class);
|
|
});
|
|
|
|
test('conversion rejects absent zero negative and decimal assumptions', function (string $quantity, string $top, string $bottom) {
|
|
expect(fn () => QuantityConversion::toBase($quantity, $top, $bottom))->toThrow(InvalidArgumentException::class);
|
|
})->with([
|
|
['1', '0', '1'], ['1', '1', '0'], ['1', '-1', '1'], ['-1', '1', '1'], ['1', '1.5', '1'], ['', '1', '1'],
|
|
]);
|
|
|
|
test('conversion checks integer capacity before any host could cast or write the quantity', function () {
|
|
expect(QuantityConversion::toBase((string) PHP_INT_MAX, '1', '1'))->toBe((string) PHP_INT_MAX);
|
|
expect(fn () => QuantityConversion::toBase((string) PHP_INT_MAX, '2', '1'))->toThrow(InvalidArgumentException::class);
|
|
expect(fn () => QuantityConversion::toBase('9999999999999999999', '1', '1'))->toThrow(InvalidArgumentException::class);
|
|
});
|