|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Cycle\ORM\Entity\Behavior\Identifier\Tests\Functional\Driver\Common\Combined; |
| 6 | + |
| 7 | +use Cycle\ORM\Entity\Behavior\Identifier\Tests\Fixtures\Combined\MultipleIdentifiers; |
| 8 | +use Cycle\ORM\Entity\Behavior\Identifier\Tests\Functional\Driver\Common\BaseTest; |
| 9 | +use Cycle\ORM\Entity\Behavior\Identifier\Tests\Traits\TableTrait; |
| 10 | +use Cycle\ORM\Entity\Behavior\Identifier\Listener\Ulid as UlidListener; |
| 11 | +use Cycle\ORM\Entity\Behavior\Identifier\Listener\Uuid4 as Uuid4Listener; |
| 12 | +use Cycle\ORM\Entity\Behavior\Identifier\Ulid; |
| 13 | +use Cycle\ORM\Entity\Behavior\Identifier\Uuid; |
| 14 | +use Cycle\ORM\Heap\Heap; |
| 15 | +use Cycle\ORM\Schema; |
| 16 | +use Cycle\ORM\SchemaInterface; |
| 17 | +use Cycle\ORM\Select; |
| 18 | +use Ramsey\Identifier\Ulid as UlidInterface; |
| 19 | +use Ramsey\Identifier\Ulid\UlidFactory; |
| 20 | +use Ramsey\Identifier\Uuid\UntypedUuid; |
| 21 | +use Ramsey\Identifier\Uuid\UuidFactory; |
| 22 | + |
| 23 | +abstract class ListenerTest extends BaseTest |
| 24 | +{ |
| 25 | + use TableTrait; |
| 26 | + |
| 27 | + public function testAssignManually(): void |
| 28 | + { |
| 29 | + $this->withListeners([ |
| 30 | + Uuid4Listener::class, |
| 31 | + UlidListener::class |
| 32 | + ]); |
| 33 | + |
| 34 | + $identifiers = new MultipleIdentifiers(); |
| 35 | + $identifiers->uuid = (new UuidFactory())->v4(); |
| 36 | + $identifiers->ulid = (new UlidFactory())->create(); |
| 37 | + $uuidBytes = $identifiers->uuid->toBytes(); |
| 38 | + $ulidBytes = $identifiers->ulid->toBytes(); |
| 39 | + |
| 40 | + $this->save($identifiers); |
| 41 | + |
| 42 | + $select = new Select($this->orm->with(heap: new Heap()), MultipleIdentifiers::class); |
| 43 | + $data = $select->fetchOne(); |
| 44 | + |
| 45 | + $this->assertSame($uuidBytes, $data->uuid->toBytes()); |
| 46 | + $this->assertSame($ulidBytes, $data->ulid->toBytes()); |
| 47 | + } |
| 48 | + |
| 49 | + public function testWithNullableTrue(): void |
| 50 | + { |
| 51 | + $this->withListeners([ |
| 52 | + Uuid4Listener::class, |
| 53 | + UlidListener::class, |
| 54 | + ]); |
| 55 | + |
| 56 | + $identifiers = new MultipleIdentifiers(); |
| 57 | + $identifiers->uuid = (new UuidFactory())->v4(); |
| 58 | + $identifiers->ulid = (new UlidFactory())->create(); |
| 59 | + |
| 60 | + $this->save($identifiers); |
| 61 | + |
| 62 | + $select = new Select($this->orm->with(heap: new Heap()), User::class); |
| 63 | + $data = $select->fetchData(); |
| 64 | + |
| 65 | + $this->assertNull($data[0]['uuid_nullable']); |
| 66 | + $this->assertNull($data[0]['ulid_nullable']); |
| 67 | + } |
| 68 | + |
| 69 | + public function testCombined(): void |
| 70 | + { |
| 71 | + $this->withListeners([ |
| 72 | + Uuid4Listener::class, |
| 73 | + UlidListener::class, |
| 74 | + ]); |
| 75 | + |
| 76 | + $identifiers = new MultipleIdentifiers(); |
| 77 | + $this->save($identifiers); |
| 78 | + |
| 79 | + $select = new Select($this->orm->with(heap: new Heap()), User::class); |
| 80 | + $data = $select->fetchOne(); |
| 81 | + |
| 82 | + $this->assertInstanceOf(UntypedUuid::class, $data->uuid); |
| 83 | + $this->assertInstanceOf(UlidInterface::class, $data->ulid); |
| 84 | + $this->assertNull($data->uuidNullable); |
| 85 | + $this->assertNull($data->ulidNullable); |
| 86 | + $this->assertIsString($data->uuid->toBytes()); |
| 87 | + $this->assertIsString($data->uuid->toString()); |
| 88 | + $this->assertIsString($data->ulid->toBytes()); |
| 89 | + $this->assertIsString($data->ulid->toString()); |
| 90 | + } |
| 91 | + |
| 92 | + public function testComparison(): void |
| 93 | + { |
| 94 | + $this->withListeners([ |
| 95 | + Uuid4Listener::class, |
| 96 | + UlidListener::class, |
| 97 | + ]); |
| 98 | + |
| 99 | + $expectedDate = '2025-06-17 03:24:36.160 +00:00'; |
| 100 | + |
| 101 | + $identifiers = new MultipleIdentifiers(); |
| 102 | + $identifiers->uuid = (new UuidFactory())->createFromString('01977bea-d1c0-7154-87bb-6550974155c2'); |
| 103 | + $identifiers->ulid = (new UlidFactory())->createFromString('01JXXYNME0E5A8FEV5A2BM2NE2'); |
| 104 | + |
| 105 | + $this->save($identifiers); |
| 106 | + |
| 107 | + $select = new Select($this->orm->with(heap: new Heap()), MultipleIdentifiers::class); |
| 108 | + $data = $select->fetchOne(); |
| 109 | + |
| 110 | + $this->assertSame($expectedDate, $data->uuid->getDateTime()->format('Y-m-d H:i:s.v P')); |
| 111 | + $this->assertSame($expectedDate, $data->ulid->getDateTime()->format('Y-m-d H:i:s.v P')); |
| 112 | + $this->assertTrue($data->uuid->equals($data->ulid)); |
| 113 | + } |
| 114 | + |
| 115 | + public function withListeners(array|string $listeners): void |
| 116 | + { |
| 117 | + $this->withSchema(new Schema([ |
| 118 | + MultipleIdentifiers::class => [ |
| 119 | + SchemaInterface::ROLE => 'multiple_identifier', |
| 120 | + SchemaInterface::DATABASE => 'default', |
| 121 | + SchemaInterface::TABLE => 'multiple_identifiers', |
| 122 | + SchemaInterface::PRIMARY_KEY => 'ulid', |
| 123 | + SchemaInterface::COLUMNS => ['uuid', 'uuid_nullable', 'ulid', 'ulid_nullable'], |
| 124 | + SchemaInterface::LISTENERS => [$listeners], |
| 125 | + SchemaInterface::SCHEMA => [], |
| 126 | + SchemaInterface::RELATIONS => [], |
| 127 | + SchemaInterface::TYPECAST => [ |
| 128 | + 'uuid' => [Uuid::class, 'fromString'], |
| 129 | + 'uuid_nullable' => [Uuid::class, 'fromString'], |
| 130 | + 'ulid' => [Ulid::class, 'fromString'], |
| 131 | + 'ulid_nullable' => [Ulid::class, 'fromString'], |
| 132 | + ], |
| 133 | + ], |
| 134 | + ])); |
| 135 | + } |
| 136 | + |
| 137 | + public function setUp(): void |
| 138 | + { |
| 139 | + parent::setUp(); |
| 140 | + |
| 141 | + $this->makeTable( |
| 142 | + 'multiple_identifiers', |
| 143 | + [ |
| 144 | + 'uuid' => 'string', |
| 145 | + 'uuid_nullable' => 'string,nullable', |
| 146 | + 'ulid' => 'string', |
| 147 | + 'ulid_nullable' => 'string,nullable', |
| 148 | + ], |
| 149 | + ); |
| 150 | + } |
| 151 | +} |
0 commit comments