ldc-cocotte/tests/Unit/Shared/ValueObjects/CrmHashcodeTest.php

48 lines
1.5 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace Tests\Unit\Shared\ValueObjects;
use Shared\Exceptions\InvalidUuidException;
use Shared\Helpers\UuidHelper;
use Shared\ValueObjects\CrmHashcode;
covers(CrmHashcode::class);
describe('CrmHashcode', function (): void {
it('instantiates from Uuid v4 and fromString with matching value and string cast', function (): void {
// GIVEN
$uuid = UuidHelper::v4();
$v4String = $uuid->toString();
// WHEN
$fromUuid = new CrmHashcode($uuid);
$fromString = CrmHashcode::fromString($v4String);
// THEN
expect($fromUuid->value)->toBe($v4String)
->and((string) $fromUuid)->toBe($v4String)
->and($fromString->value)->toBe($v4String)
->and((string) $fromString)->toBe($v4String);
});
it('rejects non-v4 uuids with DomainException', function (): void {
// GIVEN
$v7 = UuidHelper::v7();
$v1 = UuidHelper::fromString('00000000-0000-1000-8000-000000000000');
// WHEN & THEN
expect(static fn (): CrmHashcode => new CrmHashcode($v7))
->toThrow(\DomainException::class)
->and(static fn (): CrmHashcode => new CrmHashcode($v1))
->toThrow(\DomainException::class);
});
it('throws InvalidUuidException when fromString receives invalid uuid', function (): void {
// GIVEN / WHEN / THEN
expect(static fn (): CrmHashcode => CrmHashcode::fromString('not-a-uuid'))
->toThrow(InvalidUuidException::class);
});
});