Skip to content

Commit 1991103

Browse files
Added tests for combined identifiers
1 parent 03c58ac commit 1991103

3 files changed

Lines changed: 270 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\ORM\Entity\Behavior\Identifier\Tests\Fixtures\Combined;
6+
7+
use Cycle\Annotated\Annotation\Column;
8+
use Cycle\Annotated\Annotation\Entity;
9+
use Cycle\ORM\Entity\Behavior\Identifier\Ulid;
10+
use Cycle\ORM\Entity\Behavior\Identifier\Uuid4;
11+
use Ramsey\Identifier\Ulid as UlidInterface;
12+
use Ramsey\Identifier\Uuid;
13+
14+
/**
15+
* @Entity
16+
* @Uuid4
17+
* @Uuid4(field="uuidNullable", column="uuid_nullable", nullable=true)
18+
* @Ulid(field="ulid")
19+
* @Ulid(field="ulidNullable", column="ulid_nullable", nullable=true)
20+
*/
21+
#[Entity]
22+
#[Uuid4]
23+
#[Uuid4(field: 'uuidNullable', column: 'uuid_nullable', nullable: true)]
24+
#[Ulid(field: 'ulid')]
25+
#[Uuid4(field: 'ulidNullable', column: 'ulid_nullable', nullable: true)]
26+
class MultipleIdentifiers
27+
{
28+
/**
29+
* @Column(type="uuid", primary=true)
30+
*/
31+
#[Column(type: 'uuid', primary: true)]
32+
public Uuid $uuid;
33+
34+
/**
35+
* @Column(type="uuid", nullable=true)
36+
*/
37+
#[Column(type: 'uuid')]
38+
public ?Uuid $uuidNullable = null;
39+
40+
/**
41+
* @Column(type="ulid")
42+
*/
43+
#[Column(type: 'ulid')]
44+
public UlidInterface $ulid;
45+
46+
/**
47+
* @Column(type="ulid", nullable=true)
48+
*/
49+
#[Column(type: 'ulid')]
50+
public ?UlidInterface $ulidNullable = null;
51+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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\Annotated\Locator\TokenizerEntityLocator;
8+
use Cycle\ORM\Entity\Behavior\Identifier\Tests\Fixtures\Combined\MultipleIdentifiers;
9+
use Cycle\ORM\Entity\Behavior\Identifier\Tests\Functional\Driver\Common\BaseTest;
10+
use Cycle\ORM\Entity\Behavior\Identifier\Ulid;
11+
use Cycle\ORM\Entity\Behavior\Identifier\Uuid;
12+
use Cycle\ORM\Schema\GeneratedField;
13+
use Cycle\Schema\Registry;
14+
use Spiral\Attributes\AttributeReader;
15+
use Spiral\Attributes\ReaderInterface;
16+
use Spiral\Tokenizer\ClassLocator;
17+
use Symfony\Component\Finder\Finder;
18+
19+
abstract class CombinedTest extends BaseTest
20+
{
21+
protected Registry $registry;
22+
protected TokenizerEntityLocator $tokenizer;
23+
24+
/**
25+
* @dataProvider readersDataProvider
26+
*/
27+
public function testColumnsExist(ReaderInterface $reader): void
28+
{
29+
$this->compileWithTokenizer($this->tokenizer, $reader);
30+
31+
$fields = $this->registry->getEntity(MultipleIdentifiers::class)->getFields();
32+
33+
$this->assertSame(4, $fields->count());
34+
35+
$this->assertTrue($fields->has('uuid'));
36+
$this->assertTrue($fields->hasColumn('uuid'));
37+
$this->assertSame('uuid', $fields->get('uuid')->getType());
38+
$this->assertSame([Uuid::class, 'fromString'], $fields->get('uuid')->getTypecast());
39+
$this->assertSame(GeneratedField::BEFORE_INSERT, $fields->get('uuid')->getGenerated());
40+
41+
$this->assertTrue($fields->has('uuidNullable'));
42+
$this->assertTrue($fields->hasColumn('uuid_nullable'));
43+
$this->assertSame('uuid', $fields->get('uuidNullable')->getType());
44+
$this->assertSame([Uuid::class, 'fromString'], $fields->get('uuidNullable')->getTypecast());
45+
$this->assertSame(GeneratedField::BEFORE_INSERT, $fields->get('uuidNullable')->getGenerated());
46+
47+
$this->assertTrue($fields->has('ulid'));
48+
$this->assertTrue($fields->hasColumn('ulid'));
49+
$this->assertSame('ulid', $fields->get('ulid')->getType());
50+
$this->assertSame([Ulid::class, 'fromString'], $fields->get('ulid')->getTypecast());
51+
$this->assertSame(GeneratedField::BEFORE_INSERT, $fields->get('ulid')->getGenerated());
52+
53+
$this->assertTrue($fields->has('ulidNullable'));
54+
$this->assertTrue($fields->hasColumn('ulid_nullable'));
55+
$this->assertSame('ulid', $fields->get('ulidNullable')->getType());
56+
$this->assertSame([Ulid::class, 'fromString'], $fields->get('ulidNullable')->getTypecast());
57+
$this->assertSame(GeneratedField::BEFORE_INSERT, $fields->get('ulidNullable')->getGenerated());
58+
}
59+
60+
public function setUp(): void
61+
{
62+
parent::setUp();
63+
64+
$locator = new ClassLocator((new Finder())->files()->in([\dirname(__DIR__, 4) . '/Fixtures/Combined']));
65+
$reader = new AttributeReader();
66+
$this->tokenizer = new TokenizerEntityLocator($locator, $reader);
67+
}
68+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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

Comments
 (0)