2026-06-14 21:32:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Tests\Unit\Domain\Models\ValueObjects;
|
|
|
|
|
|
|
|
|
|
use Domain\Models\ValueObjects\ProductWeight;
|
|
|
|
|
|
|
|
|
|
describe('ProductWeight', function () {
|
|
|
|
|
|
|
|
|
|
it('can instantiate with grams', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$value = 1600;
|
|
|
|
|
$isVariable = false;
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$vo = new ProductWeight($value, $isVariable);
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:43:10 +01:00
|
|
|
expect($vo->value)->toBe(1600)
|
2026-06-14 21:32:07 +02:00
|
|
|
->and($vo->toGrams())->toBe(1600);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('throws exception if weight is less than 1', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
// WHEN
|
|
|
|
|
$action = fn () => new ProductWeight(0, false);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($action)->toThrow(\DomainException::class);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('formats weight in grams when under 1000', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$vo = new ProductWeight(250, false);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$result = $vo->forDisplay();
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($result)->toBe('250 g');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('formats weight in kg when 1000 or more', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$vo = new ProductWeight(1600, false);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$result = $vo->forDisplay();
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($result)->toBe('1,6 kg');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('formats weight as integer kg when no decimal part', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$vo = new ProductWeight(12000, false);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$result = $vo->forDisplay();
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($result)->toBe('12 kg');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('formats weight correctly for exactly 1 kg', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$vo = new ProductWeight(1000, false);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$result = $vo->forDisplay();
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($result)->toBe('1 kg');
|
|
|
|
|
});
|
|
|
|
|
});
|