128 lines
5.2 KiB
PHP
128 lines
5.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Domain\Models\Entities;
|
|
|
|
use Carbon\CarbonImmutable;
|
|
use Domain\Enums\BarcodeFormat;
|
|
use Domain\Models\Entities\RequestedCoupon;
|
|
use Domain\Models\ValueObjects\Coupon\CouponValidity;
|
|
use Domain\Models\ValueObjects\CouponName;
|
|
use Domain\Models\ValueObjects\CouponPicture;
|
|
use Domain\Models\ValueObjects\CouponProductName;
|
|
use Domain\Models\ValueObjects\GS1;
|
|
use Domain\Models\ValueObjects\MoneyAmount;
|
|
use Domain\Models\ValueObjects\Period;
|
|
use Domain\Models\ValueObjects\PicturePath;
|
|
use Domain\Models\ValueObjects\ProcessingCode;
|
|
use Domain\Models\ValueObjects\RequestedCoupon\RequestedCouponId;
|
|
use Domain\Models\ValueObjects\RequestedCoupon\RequestedCouponQuantity;
|
|
use Domain\Models\ValueObjects\WarningThresholdQuantity;
|
|
use Ramsey\Uuid\Uuid;
|
|
use Shared\Enums\Currency;
|
|
use Shared\ValueObjects\DateTime;
|
|
use Shared\ValueObjects\Money;
|
|
|
|
covers(RequestedCoupon::class);
|
|
|
|
describe('requested coupon', function (): void {
|
|
afterEach(function (): void {
|
|
CarbonImmutable::setTestNow();
|
|
});
|
|
|
|
it('hydrates a requested coupon with all its fields', function (): void {
|
|
// GIVEN
|
|
$id = RequestedCouponId::generate();
|
|
$name = new CouponName('Coupon A');
|
|
$productName = new CouponProductName('Product A');
|
|
$quantity = new RequestedCouponQuantity(100);
|
|
$warningThresholdQuantity = new WarningThresholdQuantity(10);
|
|
$period = new Period(new DateTime('2026-01-01'), new DateTime('2026-02-01'));
|
|
$moneyAmount = new MoneyAmount(new Money(Currency::EUR, 300));
|
|
$processingCode = new ProcessingCode('PC1');
|
|
$couponPicture = new CouponPicture(new PicturePath('coupons/product.jpg'), new PicturePath('coupons/thumbnail.jpg'));
|
|
$gs1 = new GS1('01234567890128', BarcodeFormat::EAN13);
|
|
$couponValidity = new CouponValidity(30, DateTime::now());
|
|
|
|
// WHEN
|
|
$coupon = RequestedCoupon::hydrate(
|
|
$id,
|
|
$name,
|
|
$productName,
|
|
$quantity,
|
|
$warningThresholdQuantity,
|
|
$period,
|
|
$moneyAmount,
|
|
$processingCode,
|
|
$couponPicture,
|
|
$gs1,
|
|
$couponValidity,
|
|
);
|
|
|
|
// THEN
|
|
expect($coupon->id())->toBe($id)
|
|
->and($coupon->name)->toBe($name)
|
|
->and($coupon->productName)->toBe($productName)
|
|
->and($coupon->quantity)->toBe($quantity)
|
|
->and($coupon->warningThresholdQuantity)->toBe($warningThresholdQuantity)
|
|
->and($coupon->period)->toBe($period)
|
|
->and($coupon->moneyAmount)->toBe($moneyAmount)
|
|
->and($coupon->processingCode)->toBe($processingCode)
|
|
->and($coupon->couponPicture)->toBe($couponPicture)
|
|
->and($coupon->gs1)->toBe($gs1)
|
|
->and($coupon->couponValidity)->toBe($couponValidity)
|
|
->and($coupon->updatedAt)->toBeNull()
|
|
->and($coupon->updatedAt())->toBeNull();
|
|
});
|
|
|
|
it('hydrates a requested coupon with an explicit updatedAt date', function (): void {
|
|
// GIVEN
|
|
$updatedAt = new \DateTimeImmutable('2026-06-15 12:00:00', new \DateTimeZone('UTC'));
|
|
|
|
// WHEN
|
|
$coupon = RequestedCoupon::hydrate(
|
|
RequestedCouponId::generate(),
|
|
new CouponName('Coupon A'),
|
|
new CouponProductName('Product A'),
|
|
new RequestedCouponQuantity(100),
|
|
new WarningThresholdQuantity(10),
|
|
new Period(new DateTime('2026-01-01'), new DateTime('2026-02-01')),
|
|
new MoneyAmount(new Money(Currency::EUR, 300)),
|
|
new ProcessingCode('PC1'),
|
|
new CouponPicture(new PicturePath('coupons/product.jpg'), new PicturePath('coupons/thumbnail.jpg')),
|
|
new GS1('01234567890128', BarcodeFormat::EAN13),
|
|
new CouponValidity(30, DateTime::now()),
|
|
$updatedAt,
|
|
);
|
|
|
|
// THEN
|
|
expect($coupon->updatedAt)->toBe($updatedAt)
|
|
->and($coupon->updatedAt())->toBe($updatedAt);
|
|
});
|
|
|
|
it('returns the creation date extracted from the id', function (): void {
|
|
// GIVEN
|
|
$expected = new \DateTimeImmutable('2024-06-15 12:34:56.789000', new \DateTimeZone('UTC'));
|
|
$id = new RequestedCouponId(Uuid::uuid7($expected));
|
|
$coupon = RequestedCoupon::hydrate(
|
|
$id,
|
|
new CouponName('Coupon A'),
|
|
new CouponProductName('Product A'),
|
|
new RequestedCouponQuantity(100),
|
|
new WarningThresholdQuantity(10),
|
|
new Period(new DateTime('2026-01-01'), new DateTime('2026-02-01')),
|
|
new MoneyAmount(new Money(Currency::EUR, 300)),
|
|
new ProcessingCode('PC1'),
|
|
new CouponPicture(new PicturePath('coupons/product.jpg'), new PicturePath('coupons/thumbnail.jpg')),
|
|
new GS1('01234567890128', BarcodeFormat::EAN13),
|
|
new CouponValidity(30, DateTime::now()),
|
|
);
|
|
|
|
// WHEN
|
|
$result = $coupon->createdAt();
|
|
|
|
// THEN
|
|
expect($result->format('Y-m-d H:i:s.v'))->toBe($expected->format('Y-m-d H:i:s.v'));
|
|
});
|
|
});
|