Skip to content

Commit 6e5824d

Browse files
committed
Add UUID support
1 parent 06cd557 commit 6e5824d

12 files changed

+49
-36
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"php": "^8.1",
1616
"ext-pdo": "*",
1717
"psr/simple-cache": "1 - 3",
18-
"compositephp/entity": "^0.1.8",
18+
"compositephp/entity": "^0.1.9",
1919
"doctrine/dbal": "^3.5"
2020
},
2121
"require-dev": {

src/AbstractCachedTable.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Composite\DB\Exceptions\DbException;
66
use Composite\Entity\AbstractEntity;
77
use Psr\SimpleCache\CacheInterface;
8+
use Ramsey\Uuid\UuidInterface;
89

910
abstract class AbstractCachedTable extends AbstractTable
1011
{
@@ -196,9 +197,8 @@ protected function findMultiCachedInternal(array $ids, null|int|\DateInterval $t
196197

197198
/**
198199
* @param string|int|array<string, mixed>|AbstractEntity $keyOrEntity
199-
* @throws \Composite\Entity\Exceptions\EntityException
200200
*/
201-
protected function getOneCacheKey(string|int|array|AbstractEntity $keyOrEntity): string
201+
protected function getOneCacheKey(string|int|array|AbstractEntity|UuidInterface $keyOrEntity): string
202202
{
203203
if (!is_array($keyOrEntity)) {
204204
$condition = $this->getPkCondition($keyOrEntity);

src/AbstractTable.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Doctrine\DBAL\Connection;
1212
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
1313
use Doctrine\DBAL\Query\QueryBuilder;
14+
use Ramsey\Uuid\UuidInterface;
1415

1516
abstract class AbstractTable
1617
{
@@ -330,7 +331,7 @@ final protected function createEntities(mixed $data, ?string $keyColumnName = nu
330331
* @return array<string, mixed>
331332
* @throws EntityException
332333
*/
333-
protected function getPkCondition(int|string|array|AbstractEntity $data): array
334+
protected function getPkCondition(int|string|array|AbstractEntity|UuidInterface $data): array
334335
{
335336
$condition = [];
336337
if ($data instanceof AbstractEntity) {

tests/Table/AbstractCachedTableTest.php

+15-13
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
use Composite\DB\Tests\TestStand\Tables;
1010
use Composite\Entity\AbstractEntity;
1111
use Composite\DB\Tests\Helpers;
12+
use Ramsey\Uuid\Uuid;
1213

1314
final class AbstractCachedTableTest extends \PHPUnit\Framework\TestCase
1415
{
1516
public static function getOneCacheKey_dataProvider(): array
1617
{
1718
$cache = Helpers\CacheHelper::getCache();
19+
$uuid = Uuid::uuid4();
20+
$uuidCacheKey = str_replace('-', '_', (string)$uuid);
1821
return [
1922
[
2023
new Tables\TestAutoincrementCachedTable($cache),
@@ -28,24 +31,21 @@ public static function getOneCacheKey_dataProvider(): array
2831
],
2932
[
3033
new Tables\TestUniqueCachedTable($cache),
31-
new Entities\TestUniqueEntity(id: '123abc', name: 'John'),
32-
'sqlite.TestUnique.v1.o.id_123abc',
34+
new Entities\TestUniqueEntity(id: $uuid, name: 'John'),
35+
'sqlite.TestUnique.v1.o.id_' . $uuidCacheKey,
3336
],
3437
[
35-
new Tables\TestUniqueCachedTable($cache),
36-
new Entities\TestUniqueEntity(
37-
id: implode('', array_fill(0, 100, 'a')),
38-
name: 'John',
39-
),
40-
'ed66f06444d851a981a9ddcecbbf4d5860cd3131',
38+
new Tables\TestCompositeCachedTable($cache),
39+
new Entities\TestCompositeEntity(user_id: PHP_INT_MAX, post_id: PHP_INT_MAX, message: 'Text'),
40+
'69b5bbf599d78f0274feb5cb0e6424f35cca0b57',
4141
],
4242
];
4343
}
4444

4545
/**
4646
* @dataProvider getOneCacheKey_dataProvider
4747
*/
48-
public function test_getOneCacheKey(AbstractTable $table, AbstractEntity $object, string $expected): void
48+
public function test_getOneCacheKey(AbstractCachedTable $table, AbstractEntity $object, string $expected): void
4949
{
5050
$reflectionMethod = new \ReflectionMethod($table, 'getOneCacheKey');
5151
$actual = $reflectionMethod->invoke($table, $object);
@@ -194,6 +194,8 @@ public function test_getCustomCacheKey(array $parts, string $expected): void
194194

195195
public static function collectCacheKeysByEntity_dataProvider(): array
196196
{
197+
$uuid = Uuid::uuid4();
198+
$uuidCacheKey = str_replace('-', '_', (string)$uuid);
197199
return [
198200
[
199201
new Entities\TestAutoincrementEntity(name: 'foo'),
@@ -215,21 +217,21 @@ public static function collectCacheKeysByEntity_dataProvider(): array
215217
],
216218
],
217219
[
218-
new Entities\TestUniqueEntity(id: '123abc', name: 'foo'),
220+
new Entities\TestUniqueEntity(id: $uuid, name: 'foo'),
219221
new Tables\TestUniqueCachedTable(Helpers\CacheHelper::getCache()),
220222
[
221223
'sqlite.TestUnique.v1.l.name_eq_foo',
222224
'sqlite.TestUnique.v1.c.name_eq_foo',
223-
'sqlite.TestUnique.v1.o.id_123abc',
225+
'sqlite.TestUnique.v1.o.id_' . $uuidCacheKey,
224226
],
225227
],
226228
[
227-
Entities\TestUniqueEntity::fromArray(['id' => '456def', 'name' => 'bar']),
229+
Entities\TestUniqueEntity::fromArray(['id' => $uuid, 'name' => 'bar']),
228230
new Tables\TestUniqueCachedTable(Helpers\CacheHelper::getCache()),
229231
[
230232
'sqlite.TestUnique.v1.l.name_eq_bar',
231233
'sqlite.TestUnique.v1.c.name_eq_bar',
232-
'sqlite.TestUnique.v1.o.id_456def',
234+
'sqlite.TestUnique.v1.o.id_' . $uuidCacheKey,
233235
],
234236
],
235237
];

tests/Table/AbstractTableTest.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
use Composite\DB\Tests\TestStand\Tables;
88
use Composite\Entity\AbstractEntity;
99
use Composite\Entity\Exceptions\EntityException;
10+
use Ramsey\Uuid\Uuid;
11+
use Ramsey\Uuid\UuidInterface;
1012

1113
final class AbstractTableTest extends \PHPUnit\Framework\TestCase
1214
{
1315
public static function getPkCondition_dataProvider(): array
1416
{
17+
$uuid = Uuid::uuid4();
1518
return [
1619
[
1720
new Tables\TestAutoincrementTable(),
@@ -35,13 +38,13 @@ public static function getPkCondition_dataProvider(): array
3538
],
3639
[
3740
new Tables\TestUniqueTable(),
38-
new Entities\TestUniqueEntity(id: '123abc', name: 'John'),
39-
['id' => '123abc'],
41+
new Entities\TestUniqueEntity(id: $uuid, name: 'John'),
42+
['id' => $uuid->toString()],
4043
],
4144
[
4245
new Tables\TestUniqueTable(),
43-
'123abc',
44-
['id' => '123abc'],
46+
$uuid,
47+
['id' => $uuid->toString()],
4548
],
4649
[
4750
new Tables\TestAutoincrementSdTable(),
@@ -55,16 +58,16 @@ public static function getPkCondition_dataProvider(): array
5558
],
5659
[
5760
new Tables\TestUniqueSdTable(),
58-
new Entities\TestUniqueSdEntity(id: '123abc', name: 'John'),
59-
['id' => '123abc'],
61+
new Entities\TestUniqueSdEntity(id: $uuid, name: 'John'),
62+
['id' => $uuid->toString()],
6063
],
6164
];
6265
}
6366

6467
/**
6568
* @dataProvider getPkCondition_dataProvider
6669
*/
67-
public function test_getPkCondition(AbstractTable $table, int|string|array|AbstractEntity $object, array $expected): void
70+
public function test_getPkCondition(AbstractTable $table, int|string|array|AbstractEntity|UuidInterface $object, array $expected): void
6871
{
6972
$reflectionMethod = new \ReflectionMethod($table, 'getPkCondition');
7073
$actual = $reflectionMethod->invoke($table, $object);

tests/Table/UniqueTableTest.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Composite\DB\Tests\TestStand\Entities;
99
use Composite\DB\Tests\TestStand\Tables;
1010
use Composite\DB\Tests\TestStand\Interfaces\IUniqueTable;
11+
use Ramsey\Uuid\Uuid;
1112

1213
final class UniqueTableTest extends \PHPUnit\Framework\TestCase
1314
{
@@ -43,7 +44,7 @@ public function test_crud(AbstractTable&IUniqueTable $table, string $class): voi
4344
$tableConfig = TableConfig::fromEntitySchema($class::schema());
4445

4546
$entity = new $class(
46-
id: uniqid(),
47+
id: Uuid::uuid4(),
4748
name: Helpers\StringHelper::getUniqueName(),
4849
);
4950
$this->assertEntityNotExists($table, $entity);
@@ -68,19 +69,19 @@ public function test_crud(AbstractTable&IUniqueTable $table, string $class): voi
6869
public function test_multiSave(): void
6970
{
7071
$e1 = new Entities\TestUniqueEntity(
71-
id: uniqid(),
72+
id: Uuid::uuid4(),
7273
name: Helpers\StringHelper::getUniqueName(),
7374
);
7475
$e2 = new Entities\TestUniqueEntity(
75-
id: uniqid(),
76+
id: Uuid::uuid4(),
7677
name: Helpers\StringHelper::getUniqueName(),
7778
);
7879
$e3 = new Entities\TestUniqueEntity(
79-
id: uniqid(),
80+
id: Uuid::uuid4(),
8081
name: Helpers\StringHelper::getUniqueName(),
8182
);
8283
$e4 = new Entities\TestUniqueEntity(
83-
id: uniqid(),
84+
id: Uuid::uuid4(),
8485
name: Helpers\StringHelper::getUniqueName(),
8586
);
8687
$table = new Tables\TestUniqueTable();

tests/TestStand/Entities/TestUniqueEntity.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
use Composite\DB\Attributes\{PrimaryKey};
66
use Composite\DB\Attributes\Table;
77
use Composite\Entity\AbstractEntity;
8+
use Ramsey\Uuid\UuidInterface;
89

910
#[Table(connection: 'sqlite', name: 'TestUnique')]
1011
class TestUniqueEntity extends AbstractEntity
1112
{
1213
public function __construct(
1314
#[PrimaryKey]
14-
public readonly string $id,
15+
public readonly UuidInterface $id,
1516
public string $name,
1617
public readonly \DateTimeImmutable $created_at = new \DateTimeImmutable(),
1718
) {}

tests/TestStand/Interfaces/IUniqueTable.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
use Composite\DB\Tests\TestStand\Entities\TestCompositeEntity;
66
use Composite\DB\Tests\TestStand\Entities\TestUniqueEntity;
7+
use Ramsey\Uuid\UuidInterface;
78

89
interface IUniqueTable
910
{
10-
public function findByPk(string $id): ?TestUniqueEntity;
11+
public function findByPk(UuidInterface $id): ?TestUniqueEntity;
1112
/**
1213
* @return TestCompositeEntity[]
1314
*/

tests/TestStand/Tables/TestUniqueCachedTable.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Composite\DB\Tests\TestStand\Entities\TestUniqueEntity;
88
use Composite\DB\Tests\TestStand\Interfaces\IUniqueTable;
99
use Composite\Entity\AbstractEntity;
10+
use Ramsey\Uuid\UuidInterface;
1011

1112
class TestUniqueCachedTable extends AbstractCachedTable implements IUniqueTable
1213
{
@@ -29,7 +30,7 @@ protected function getFlushCacheKeys(TestUniqueEntity|AbstractEntity $entity): a
2930
];
3031
}
3132

32-
public function findByPk(string $id): ?TestUniqueEntity
33+
public function findByPk(UuidInterface $id): ?TestUniqueEntity
3334
{
3435
return $this->createEntity($this->findByPkInternal($id));
3536
}

tests/TestStand/Tables/TestUniqueSdCachedTable.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Composite\DB\Tests\TestStand\Entities\TestUniqueSdEntity;
88
use Composite\DB\Tests\TestStand\Interfaces\IUniqueTable;
99
use Composite\Entity\AbstractEntity;
10+
use Ramsey\Uuid\UuidInterface;
1011

1112
class TestUniqueSdCachedTable extends AbstractCachedTable implements IUniqueTable
1213
{
@@ -29,7 +30,7 @@ protected function getFlushCacheKeys(TestUniqueSdEntity|AbstractEntity $entity):
2930
];
3031
}
3132

32-
public function findByPk(string $id): ?TestUniqueSdEntity
33+
public function findByPk(UuidInterface $id): ?TestUniqueSdEntity
3334
{
3435
return $this->createEntity($this->findByPkInternal($id));
3536
}

tests/TestStand/Tables/TestUniqueSdTable.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Composite\DB\TableConfig;
66
use Composite\DB\Tests\TestStand\Entities\TestUniqueSdEntity;
7+
use Ramsey\Uuid\UuidInterface;
78

89
class TestUniqueSdTable extends TestUniqueTable
910
{
@@ -18,7 +19,7 @@ protected function getConfig(): TableConfig
1819
return TableConfig::fromEntitySchema(TestUniqueSdEntity::schema());
1920
}
2021

21-
public function findByPk(string $id): ?TestUniqueSdEntity
22+
public function findByPk(UuidInterface $id): ?TestUniqueSdEntity
2223
{
2324
return $this->createEntity($this->findByPkInternal($id));
2425
}
@@ -40,7 +41,7 @@ public function init(): bool
4041
"
4142
CREATE TABLE IF NOT EXISTS {$this->getTableName()}
4243
(
43-
`id` VARCHAR(255) NOT NULL,
44+
`id` VARCHAR(32) NOT NULL,
4445
`name` VARCHAR(255) NOT NULL,
4546
`created_at` TIMESTAMP NOT NULL,
4647
`deleted_at` TIMESTAMP NULL DEFAULT NULL,

tests/TestStand/Tables/TestUniqueTable.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Composite\DB\Tests\TestStand\Entities\TestUniqueEntity;
88
use Composite\DB\Tests\TestStand\Interfaces\IUniqueTable;
99
use Composite\Entity\AbstractEntity;
10+
use Ramsey\Uuid\UuidInterface;
1011

1112
class TestUniqueTable extends AbstractTable implements IUniqueTable
1213
{
@@ -29,7 +30,7 @@ protected function getConfig(): TableConfig
2930
return TableConfig::fromEntitySchema(TestUniqueEntity::schema());
3031
}
3132

32-
public function findByPk(string $id): ?TestUniqueEntity
33+
public function findByPk(UuidInterface $id): ?TestUniqueEntity
3334
{
3435
return $this->createEntity($this->findByPkInternal($id));
3536
}

0 commit comments

Comments
 (0)