refactor: ♻️ DateTime VO and use native DateTime class
This commit is contained in:
parent
c773e9de4e
commit
e15a372e59
15 changed files with 21 additions and 159 deletions
|
|
@ -6,7 +6,7 @@ namespace Domain\Models\ValueObjects\Coupon;
|
|||
|
||||
use Domain\Models\ValueObjects\Period;
|
||||
use Shared\DDD\Models\Equatable;
|
||||
use Shared\ValueObjects\DateTime;
|
||||
use DateTime;
|
||||
|
||||
final readonly class CouponAvailabilityCalendar implements Equatable
|
||||
{
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ declare(strict_types=1);
|
|||
namespace Domain\Models\ValueObjects\Coupon;
|
||||
|
||||
use Shared\DDD\Models\Equatable;
|
||||
use Shared\ValueObjects\DateTime;
|
||||
use DateTime;
|
||||
|
||||
final readonly class CouponValidity implements Equatable
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Domain\Models\ValueObjects;
|
||||
|
||||
use Shared\ValueObjects\DateTime;
|
||||
use DateTime;
|
||||
|
||||
final readonly class Period
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,51 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shared\ValueObjects;
|
||||
|
||||
use Shared\DDD\Models\Equatable;
|
||||
use Shared\Helpers\DatesHelper;
|
||||
|
||||
/**
|
||||
* Represents a valid point in time, always normalized to UTC.
|
||||
*/
|
||||
final readonly class DateTime implements \Stringable, \JsonSerializable, Equatable
|
||||
{
|
||||
public \DateTimeImmutable $value;
|
||||
|
||||
/**
|
||||
* @param \DateTimeInterface|string $value A DateTimeInterface instance or a parsable date string.
|
||||
* @throws \DomainException
|
||||
*/
|
||||
public function __construct(
|
||||
\DateTimeInterface | string $value,
|
||||
) {
|
||||
$dateTime = is_string($value) ? DatesHelper::parseDateTime($value) : $value;
|
||||
|
||||
$this->value = \DateTimeImmutable::createFromInterface($dateTime)
|
||||
->setTimezone(new \DateTimeZone('UTC'));
|
||||
}
|
||||
|
||||
public static function now(): self
|
||||
{
|
||||
return new self(DatesHelper::now());
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function equals(mixed $other): bool
|
||||
{
|
||||
return $other instanceof self
|
||||
&& $this->value->format(\DateTimeInterface::RFC3339_EXTENDED) === $other->value->format(\DateTimeInterface::RFC3339_EXTENDED);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return DatesHelper::toRfc3339($this->value);
|
||||
}
|
||||
|
||||
public function jsonSerialize(): string
|
||||
{
|
||||
return (string) $this;
|
||||
}
|
||||
}
|
||||
|
|
@ -41,7 +41,7 @@ use Domain\Ports\Repositories\BrandRepository;
|
|||
use Domain\Ports\Repositories\CampaignRequestRepository;
|
||||
use Shared\DDD\UseCases\Input as BaseInput;
|
||||
use Shared\Enums\Currency;
|
||||
use Shared\ValueObjects\DateTime;
|
||||
use DateTime;
|
||||
use Shared\ValueObjects\Email;
|
||||
use Shared\ValueObjects\Money;
|
||||
use Shared\ValueObjects\Rcs;
|
||||
|
|
@ -61,7 +61,7 @@ describe('find campaign request use case', function (): void {
|
|||
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()),
|
||||
new CouponValidity(30, new DateTime()),
|
||||
);
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ use Domain\Models\ValueObjects\WarningThresholdQuantity;
|
|||
use Domain\Ports\Repositories\CampaignRequestRepository;
|
||||
use Shared\DDD\UseCases\Input as BaseInput;
|
||||
use Shared\Enums\Currency;
|
||||
use Shared\ValueObjects\DateTime;
|
||||
use DateTime;
|
||||
use Shared\ValueObjects\Email;
|
||||
use Shared\ValueObjects\Money;
|
||||
use Shared\ValueObjects\Pagination\Paginated;
|
||||
|
|
@ -55,7 +55,7 @@ describe('list campaigns request use case', function (): void {
|
|||
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()),
|
||||
new CouponValidity(30, new DateTime()),
|
||||
);
|
||||
|
||||
$this->couponB = RequestedCoupon::hydrate(
|
||||
|
|
@ -69,7 +69,7 @@ describe('list campaigns request use case', function (): void {
|
|||
new ProcessingCode('PC2'),
|
||||
new CouponPicture(new PicturePath('coupons/product.jpg'), new PicturePath('coupons/thumbnail.jpg')),
|
||||
new GS1('01234567890128', BarcodeFormat::EAN13),
|
||||
new CouponValidity(30, DateTime::now()),
|
||||
new CouponValidity(30, new DateTime()),
|
||||
);
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ use Domain\Models\ValueObjects\WarningThresholdQuantity;
|
|||
use Domain\Ports\Repositories\CampaignRequestRepository;
|
||||
use Shared\DDD\UseCases\Input as BaseInput;
|
||||
use Shared\Enums\Currency;
|
||||
use Shared\ValueObjects\DateTime;
|
||||
use DateTime;
|
||||
use Shared\ValueObjects\Email;
|
||||
use Shared\ValueObjects\Money;
|
||||
|
||||
|
|
@ -60,7 +60,7 @@ describe('reject campaign request use case', function (): void {
|
|||
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()),
|
||||
new CouponValidity(30, new DateTime()),
|
||||
);
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ use Domain\Models\ValueObjects\ProcessingCode;
|
|||
use Domain\Models\ValueObjects\RequestedCoupon\RequestedCouponId;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Shared\Enums\Currency;
|
||||
use Shared\ValueObjects\DateTime;
|
||||
use DateTime;
|
||||
use Shared\ValueObjects\Email;
|
||||
use Shared\ValueObjects\Money;
|
||||
use Carbon\CarbonImmutable;
|
||||
|
|
@ -54,7 +54,7 @@ describe('campaign request', function (): void {
|
|||
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()),
|
||||
new CouponValidity(30, new DateTime()),
|
||||
);
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ use Domain\Models\ValueObjects\PicturePath;
|
|||
use Domain\Models\ValueObjects\ProcessingCode;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Shared\Enums\Currency;
|
||||
use Shared\ValueObjects\DateTime;
|
||||
use DateTime;
|
||||
use Shared\ValueObjects\Money;
|
||||
|
||||
covers(Coupon::class);
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ use Domain\Models\ValueObjects\RequestedCoupon\RequestedCouponQuantity;
|
|||
use Domain\Models\ValueObjects\WarningThresholdQuantity;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Shared\Enums\Currency;
|
||||
use Shared\ValueObjects\DateTime;
|
||||
use DateTime;
|
||||
use Shared\ValueObjects\Money;
|
||||
|
||||
covers(RequestedCoupon::class);
|
||||
|
|
@ -43,7 +43,7 @@ describe('requested coupon', function (): void {
|
|||
$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());
|
||||
$couponValidity = new CouponValidity(30, new DateTime());
|
||||
|
||||
// WHEN
|
||||
$coupon = RequestedCoupon::hydrate(
|
||||
|
|
@ -92,7 +92,7 @@ describe('requested coupon', function (): void {
|
|||
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()),
|
||||
new CouponValidity(30, new DateTime()),
|
||||
$updatedAt,
|
||||
);
|
||||
|
||||
|
|
@ -116,7 +116,7 @@ describe('requested coupon', function (): void {
|
|||
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()),
|
||||
new CouponValidity(30, new DateTime()),
|
||||
);
|
||||
|
||||
// WHEN
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ namespace Tests\Unit\Domain\Models\ValueObjects\Coupon;
|
|||
|
||||
use Domain\Models\ValueObjects\Coupon\CouponAvailabilityCalendar;
|
||||
use Domain\Models\ValueObjects\Period;
|
||||
use Shared\ValueObjects\DateTime;
|
||||
use DateTime;
|
||||
|
||||
covers(CouponAvailabilityCalendar::class);
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ namespace Tests\Unit\Domain\Models\ValueObjects\Coupon;
|
|||
|
||||
use Domain\Models\ValueObjects\Coupon\CouponDiffusion;
|
||||
use Domain\Models\ValueObjects\Period;
|
||||
use Shared\ValueObjects\DateTime;
|
||||
use DateTime;
|
||||
|
||||
covers(CouponDiffusion::class);
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ declare(strict_types=1);
|
|||
namespace Tests\Unit\Domain\Models\ValueObjects;
|
||||
|
||||
use Domain\Models\ValueObjects\Coupon\CouponValidity;
|
||||
use Shared\ValueObjects\DateTime;
|
||||
use DateTime;
|
||||
|
||||
covers(CouponValidity::class);
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ declare(strict_types=1);
|
|||
namespace Tests\Unit\Domain\Models\ValueObjects\RequestedCoupon;
|
||||
|
||||
use Domain\Models\ValueObjects\Period;
|
||||
use Shared\ValueObjects\DateTime;
|
||||
use DateTime;
|
||||
|
||||
covers(Period::class);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,87 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Shared\ValueObjects;
|
||||
|
||||
use Shared\Helpers\DatesHelper;
|
||||
use Shared\Helpers\Exceptions\InvalidDateFormatException;
|
||||
use Shared\ValueObjects\DateTime;
|
||||
use Carbon\CarbonImmutable;
|
||||
|
||||
covers(DateTime::class);
|
||||
|
||||
describe('date time', function (): void {
|
||||
it('accepts a DateTimeInterface instance and normalizes it to UTC', function (): void {
|
||||
// GIVEN
|
||||
$paris = new \DateTimeImmutable('2026-06-15 14:00:00', new \DateTimeZone('Europe/Paris'));
|
||||
|
||||
// WHEN
|
||||
$dateTime = new DateTime($paris);
|
||||
|
||||
// THEN
|
||||
expect($dateTime->value)->toBeInstanceOf(\DateTimeImmutable::class)
|
||||
->and($dateTime->value->getTimezone()->getName())->toBe('UTC')
|
||||
->and($dateTime->value->format('H:i:s'))->toBe('12:00:00');
|
||||
});
|
||||
|
||||
it('parses a valid date string', function (): void {
|
||||
// WHEN
|
||||
$dateTime = new DateTime('2026-01-15');
|
||||
|
||||
// THEN
|
||||
expect($dateTime->value->format('Y-m-d'))->toBe('2026-01-15');
|
||||
});
|
||||
|
||||
it('rejects an invalid date string', function (): void {
|
||||
// WHEN
|
||||
$action = fn (): DateTime => new DateTime('not-a-date');
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(InvalidDateFormatException::class);
|
||||
});
|
||||
|
||||
it('creates the current instant via now', function (): void {
|
||||
// GIVEN
|
||||
CarbonImmutable::setTestNow('2026-06-15T12:00:00Z');
|
||||
|
||||
// WHEN
|
||||
$dateTime = DateTime::now();
|
||||
|
||||
// THEN
|
||||
expect($dateTime)->toBeInstanceOf(DateTime::class)
|
||||
->and($dateTime->value->getTimezone()->getName())->toBe('UTC')
|
||||
->and($dateTime->value->format(\DateTimeInterface::ATOM))->toBe('2026-06-15T12:00:00+00:00');
|
||||
|
||||
// TEARDOWN
|
||||
CarbonImmutable::setTestNow();
|
||||
});
|
||||
|
||||
it('implements equals correctly', function (): void {
|
||||
// GIVEN
|
||||
$a = new DateTime('2026-01-15T10:00:00Z');
|
||||
$b = new DateTime('2026-01-15T10:00:00Z');
|
||||
$c = new DateTime('2026-01-16T10:00:00Z');
|
||||
|
||||
// THEN
|
||||
expect($a->equals($b))->toBeTrue()
|
||||
->and($a->equals($c))->toBeFalse()
|
||||
->and($a->equals(null))->toBeFalse();
|
||||
});
|
||||
|
||||
it('converts to a RFC3339 string', function (): void {
|
||||
// GIVEN
|
||||
$dateTime = new DateTime('2026-01-15T10:00:00Z');
|
||||
|
||||
// THEN
|
||||
expect((string) $dateTime)->toBe(DatesHelper::toRfc3339($dateTime->value));
|
||||
});
|
||||
|
||||
it('serializes to json as a RFC3339 string', function (): void {
|
||||
// GIVEN
|
||||
$dateTime = new DateTime('2026-01-15T10:00:00Z');
|
||||
|
||||
// THEN
|
||||
expect($dateTime->jsonSerialize())->toBe((string) $dateTime);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Reference in a new issue