|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Unit\Lendable\Aggregate; |
| 6 | + |
| 7 | +use Lendable\Aggregate\AggregateIdFactory; |
| 8 | +use Lendable\Aggregate\AggregateType; |
| 9 | +use Lendable\Aggregate\UuidV4AggregateId; |
| 10 | +use Lendable\Aggregate\UuidV7AggregateId; |
| 11 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 12 | +use PHPUnit\Framework\Attributes\Test; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | +use Ramsey\Uuid\Uuid; |
| 15 | + |
| 16 | +#[CoversClass(AggregateIdFactory::class)] |
| 17 | +final class AggregateIdFactoryTest extends TestCase |
| 18 | +{ |
| 19 | + #[Test] |
| 20 | + public function creates_from_binary(): void |
| 21 | + { |
| 22 | + $factory = new AggregateIdFactory(['foo' => UuidV4AggregateId::class, 'bar' => UuidV7AggregateId::class]); |
| 23 | + |
| 24 | + $fooUuid = Uuid::uuid4(); |
| 25 | + $fooId = $factory->fromBinary( |
| 26 | + AggregateType::fromString('foo'), |
| 27 | + $fooUuid->getBytes() |
| 28 | + ); |
| 29 | + |
| 30 | + $this->assertInstanceOf(UuidV4AggregateId::class, $fooId); |
| 31 | + $this->assertSame($fooUuid->toString(), $fooId->toString()); |
| 32 | + |
| 33 | + $barUuid = Uuid::uuid7(); |
| 34 | + $barId = $factory->fromBinary( |
| 35 | + AggregateType::fromString('bar'), |
| 36 | + $barUuid->getBytes() |
| 37 | + ); |
| 38 | + |
| 39 | + $this->assertInstanceOf(UuidV7AggregateId::class, $barId); |
| 40 | + $this->assertSame($barUuid->toString(), $barId->toString()); |
| 41 | + } |
| 42 | + |
| 43 | + #[Test] |
| 44 | + public function creates_from_string(): void |
| 45 | + { |
| 46 | + $factory = new AggregateIdFactory(['foo' => UuidV4AggregateId::class, 'bar' => UuidV7AggregateId::class]); |
| 47 | + |
| 48 | + $fooUuid = Uuid::uuid4(); |
| 49 | + $fooId = $factory->fromString( |
| 50 | + AggregateType::fromString('foo'), |
| 51 | + $fooUuid->toString() |
| 52 | + ); |
| 53 | + |
| 54 | + $this->assertInstanceOf(UuidV4AggregateId::class, $fooId); |
| 55 | + $this->assertSame($fooUuid->toString(), $fooId->toString()); |
| 56 | + |
| 57 | + $barUuid = Uuid::uuid7(); |
| 58 | + $barId = $factory->fromString( |
| 59 | + AggregateType::fromString('bar'), |
| 60 | + $barUuid->toString() |
| 61 | + ); |
| 62 | + |
| 63 | + $this->assertInstanceOf(UuidV7AggregateId::class, $barId); |
| 64 | + $this->assertSame($barUuid->toString(), $barId->toString()); |
| 65 | + } |
| 66 | + |
| 67 | + #[Test] |
| 68 | + public function creates_from_uuid(): void |
| 69 | + { |
| 70 | + $factory = new AggregateIdFactory(['foo' => UuidV4AggregateId::class, 'bar' => UuidV7AggregateId::class]); |
| 71 | + |
| 72 | + $fooUuid = Uuid::uuid4(); |
| 73 | + $fooId = $factory->fromUuid( |
| 74 | + AggregateType::fromString('foo'), |
| 75 | + $fooUuid |
| 76 | + ); |
| 77 | + |
| 78 | + $this->assertSame($fooUuid->toString(), $fooId->toString()); |
| 79 | + |
| 80 | + $barUuid = Uuid::uuid7(); |
| 81 | + $barId = $factory->fromUuid( |
| 82 | + AggregateType::fromString('bar'), |
| 83 | + $barUuid |
| 84 | + ); |
| 85 | + |
| 86 | + $this->assertSame($barUuid->toString(), $barId->toString()); |
| 87 | + } |
| 88 | + |
| 89 | + #[Test] |
| 90 | + public function throws_when_creating_from_binary_if_not_mapped(): void |
| 91 | + { |
| 92 | + $factory = new AggregateIdFactory(['foo' => UuidV4AggregateId::class]); |
| 93 | + |
| 94 | + $this->expectExceptionObject(new \InvalidArgumentException('Aggregate id implementation is not known for aggregate type "bar".')); |
| 95 | + |
| 96 | + $factory->fromBinary(AggregateType::fromString('bar'), Uuid::uuid4()->getBytes()); |
| 97 | + } |
| 98 | + |
| 99 | + #[Test] |
| 100 | + public function throws_when_creating_from_string_if_not_mapped(): void |
| 101 | + { |
| 102 | + $factory = new AggregateIdFactory(['foo' => UuidV4AggregateId::class]); |
| 103 | + |
| 104 | + $this->expectExceptionObject(new \InvalidArgumentException('Aggregate id implementation is not known for aggregate type "bar".')); |
| 105 | + |
| 106 | + $factory->fromString(AggregateType::fromString('bar'), Uuid::uuid4()->toString()); |
| 107 | + } |
| 108 | + |
| 109 | + #[Test] |
| 110 | + public function throws_when_creating_from_uuid_if_not_mapped(): void |
| 111 | + { |
| 112 | + $factory = new AggregateIdFactory(['foo' => UuidV4AggregateId::class]); |
| 113 | + |
| 114 | + $this->expectExceptionObject(new \InvalidArgumentException('Aggregate id implementation is not known for aggregate type "bar".')); |
| 115 | + |
| 116 | + $factory->fromUuid(AggregateType::fromString('bar'), Uuid::uuid4()); |
| 117 | + } |
| 118 | + |
| 119 | + #[Test] |
| 120 | + public function throws_if_constructed_with_empty_map(): void |
| 121 | + { |
| 122 | + $this->expectExceptionObject(new \InvalidArgumentException('Map cannot be empty.')); |
| 123 | + |
| 124 | + new AggregateIdFactory([]); // @phpstan-ignore-line |
| 125 | + } |
| 126 | +} |
0 commit comments