Skip to content

Commit 855b62f

Browse files
committed
Test custom PKs stored as binary/string/integer
1 parent a132276 commit 855b62f

21 files changed

+399
-160
lines changed

tests/EntityPreloadBlogManyHasManyInversedTest.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
namespace ShipMonkTests\DoctrineEntityPreloader;
44

5+
use Doctrine\DBAL\Types\Type as DbalType;
56
use Doctrine\ORM\Mapping\ClassMetadata;
7+
use PHPUnit\Framework\Attributes\DataProvider;
68
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\Tag;
79
use ShipMonkTests\DoctrineEntityPreloader\Lib\TestCase;
810

911
class EntityPreloadBlogManyHasManyInversedTest extends TestCase
1012
{
1113

12-
public function testManyHasManyInversedUnoptimized(): void
14+
#[DataProvider('providePrimaryKeyTypes')]
15+
public function testManyHasManyInversedUnoptimized(DbalType $primaryKey): void
1316
{
14-
$this->createDummyBlogData(articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
17+
$this->createDummyBlogData($primaryKey, articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
1518

1619
$tags = $this->getEntityManager()->getRepository(Tag::class)->findAll();
1720

@@ -23,9 +26,10 @@ public function testManyHasManyInversedUnoptimized(): void
2326
]);
2427
}
2528

26-
public function testManyHasManyInversedWithFetchJoin(): void
29+
#[DataProvider('providePrimaryKeyTypes')]
30+
public function testManyHasManyInversedWithFetchJoin(DbalType $primaryKey): void
2731
{
28-
$this->createDummyBlogData(articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
32+
$this->createDummyBlogData($primaryKey, articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
2933

3034
$tags = $this->getEntityManager()->createQueryBuilder()
3135
->select('tag', 'article')
@@ -41,9 +45,10 @@ public function testManyHasManyInversedWithFetchJoin(): void
4145
]);
4246
}
4347

44-
public function testManyHasManyInversedWithEagerFetchMode(): void
48+
#[DataProvider('providePrimaryKeyTypes')]
49+
public function testManyHasManyInversedWithEagerFetchMode(DbalType $primaryKey): void
4550
{
46-
$this->createDummyBlogData(articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
51+
$this->createDummyBlogData($primaryKey, articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
4752

4853
// for eagerly loaded Many-To-Many associations one query has to be made for each collection
4954
// https://www.doctrine-project.org/projects/doctrine-orm/en/3.2/reference/working-with-objects.html#by-eager-loading
@@ -62,9 +67,10 @@ public function testManyHasManyInversedWithEagerFetchMode(): void
6267
]);
6368
}
6469

65-
public function testManyHasManyInversedWithPreload(): void
70+
#[DataProvider('providePrimaryKeyTypes')]
71+
public function testManyHasManyInversedWithPreload(DbalType $primaryKey): void
6672
{
67-
$this->createDummyBlogData(articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
73+
$this->createDummyBlogData($primaryKey, articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
6874

6975
$tags = $this->getEntityManager()->getRepository(Tag::class)->findAll();
7076
$this->getEntityPreloader()->preload($tags, 'articles');

tests/EntityPreloadBlogManyHasManyTest.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22

33
namespace ShipMonkTests\DoctrineEntityPreloader;
44

5-
use Doctrine\DBAL\ArrayParameterType;
5+
use Doctrine\DBAL\Types\Type as DbalType;
66
use Doctrine\ORM\Mapping\ClassMetadata;
7+
use PHPUnit\Framework\Attributes\DataProvider;
78
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\Article;
89
use ShipMonkTests\DoctrineEntityPreloader\Lib\TestCase;
910
use function array_map;
1011

1112
class EntityPreloadBlogManyHasManyTest extends TestCase
1213
{
1314

14-
public function testManyHasManyUnoptimized(): void
15+
#[DataProvider('providePrimaryKeyTypes')]
16+
public function testManyHasManyUnoptimized(DbalType $primaryKey): void
1517
{
16-
$this->createDummyBlogData(articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
18+
$this->createDummyBlogData($primaryKey, articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
1719

1820
$articles = $this->getEntityManager()->getRepository(Article::class)->findAll();
1921

@@ -25,14 +27,16 @@ public function testManyHasManyUnoptimized(): void
2527
]);
2628
}
2729

28-
public function testOneHasManyWithWithManualPreloadUsingPartial(): void
30+
#[DataProvider('providePrimaryKeyTypes')]
31+
public function testOneHasManyWithWithManualPreloadUsingPartial(DbalType $primaryKey): void
2932
{
3033
$this->skipIfPartialEntitiesAreNotSupported();
31-
$this->createDummyBlogData(articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
34+
$this->createDummyBlogData($primaryKey, articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
3235

3336
$articles = $this->getEntityManager()->getRepository(Article::class)->findAll();
37+
$platform = $this->getEntityManager()->getConnection()->getDatabasePlatform();
3438
$rawArticleIds = array_map(
35-
static fn (Article $article): string => $article->getId()->getBytes(),
39+
static fn (Article $article) => $primaryKey->convertToDatabaseValue($article->getId(), $platform),
3640
$articles,
3741
);
3842

@@ -41,7 +45,7 @@ public function testOneHasManyWithWithManualPreloadUsingPartial(): void
4145
->from(Article::class, 'article')
4246
->leftJoin('article.tags', 'tag')
4347
->where('article IN (:articles)')
44-
->setParameter('articles', $rawArticleIds, ArrayParameterType::BINARY)
48+
->setParameter('articles', $rawArticleIds, $this->deduceArrayParameterType($primaryKey))
4549
->getQuery()
4650
->getResult();
4751

@@ -53,9 +57,10 @@ public function testOneHasManyWithWithManualPreloadUsingPartial(): void
5357
]);
5458
}
5559

56-
public function testManyHasManyWithFetchJoin(): void
60+
#[DataProvider('providePrimaryKeyTypes')]
61+
public function testManyHasManyWithFetchJoin(DbalType $primaryKey): void
5762
{
58-
$this->createDummyBlogData(articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
63+
$this->createDummyBlogData($primaryKey, articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
5964

6065
$articles = $this->getEntityManager()->createQueryBuilder()
6166
->select('article', 'tag')
@@ -71,9 +76,10 @@ public function testManyHasManyWithFetchJoin(): void
7176
]);
7277
}
7378

74-
public function testManyHasManyWithEagerFetchMode(): void
79+
#[DataProvider('providePrimaryKeyTypes')]
80+
public function testManyHasManyWithEagerFetchMode(DbalType $primaryKey): void
7581
{
76-
$this->createDummyBlogData(articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
82+
$this->createDummyBlogData($primaryKey, articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
7783

7884
// for eagerly loaded Many-To-Many associations one query has to be made for each collection
7985
// https://www.doctrine-project.org/projects/doctrine-orm/en/3.2/reference/working-with-objects.html#by-eager-loading
@@ -92,9 +98,10 @@ public function testManyHasManyWithEagerFetchMode(): void
9298
]);
9399
}
94100

95-
public function testManyHasManyWithPreload(): void
101+
#[DataProvider('providePrimaryKeyTypes')]
102+
public function testManyHasManyWithPreload(DbalType $primaryKey): void
96103
{
97-
$this->createDummyBlogData(articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
104+
$this->createDummyBlogData($primaryKey, articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
98105

99106
$articles = $this->getEntityManager()->getRepository(Article::class)->findAll();
100107
$this->getEntityPreloader()->preload($articles, 'tags');

tests/EntityPreloadBlogManyHasOneDeepTest.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace ShipMonkTests\DoctrineEntityPreloader;
44

5-
use Doctrine\DBAL\ArrayParameterType;
5+
use Doctrine\DBAL\Types\Type as DbalType;
66
use Doctrine\ORM\Mapping\ClassMetadata;
7+
use PHPUnit\Framework\Attributes\DataProvider;
78
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\Article;
89
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\Category;
910
use ShipMonkTests\DoctrineEntityPreloader\Lib\TestCase;
@@ -16,9 +17,10 @@
1617
class EntityPreloadBlogManyHasOneDeepTest extends TestCase
1718
{
1819

19-
public function testManyHasOneDeepUnoptimized(): void
20+
#[DataProvider('providePrimaryKeyTypes')]
21+
public function testManyHasOneDeepUnoptimized(DbalType $primaryKey): void
2022
{
21-
$this->createDummyBlogData(categoryCount: 5, categoryParentsCount: 5, articleInEachCategoryCount: 5);
23+
$this->createDummyBlogData($primaryKey, categoryCount: 5, categoryParentsCount: 5, articleInEachCategoryCount: 5);
2224

2325
$articles = $this->getEntityManager()->getRepository(Article::class)->findAll();
2426

@@ -30,33 +32,35 @@ public function testManyHasOneDeepUnoptimized(): void
3032
]);
3133
}
3234

33-
public function testManyHasOneDeepWithManualPreload(): void
35+
#[DataProvider('providePrimaryKeyTypes')]
36+
public function testManyHasOneDeepWithManualPreload(DbalType $primaryKey): void
3437
{
35-
$this->createDummyBlogData(categoryCount: 5, categoryParentsCount: 5, articleInEachCategoryCount: 5);
38+
$this->createDummyBlogData($primaryKey, categoryCount: 5, categoryParentsCount: 5, articleInEachCategoryCount: 5);
3639

3740
$articles = $this->getEntityManager()->getRepository(Article::class)->findAll();
41+
$platform = $this->getEntityManager()->getConnection()->getDatabasePlatform();
3842

39-
$categoryIds = array_map(static fn (Article $article) => $article->getCategory()?->getId()->getBytes(), $articles);
43+
$categoryIds = array_map(static fn (Article $article) => $primaryKey->convertToDatabaseValue($article->getCategory()?->getId(), $platform), $articles);
4044
$categoryIds = array_filter($categoryIds, static fn (?string $id) => $id !== null);
4145

4246
if (count($categoryIds) > 0) {
4347
$categories = $this->getEntityManager()->createQueryBuilder()
4448
->select('category')
4549
->from(Category::class, 'category')
4650
->where('category.id IN (:ids)')
47-
->setParameter('ids', array_values(array_unique($categoryIds)), ArrayParameterType::BINARY)
51+
->setParameter('ids', array_values(array_unique($categoryIds)), $this->deduceArrayParameterType($primaryKey))
4852
->getQuery()
4953
->getResult();
5054

51-
$parentCategoryIds = array_map(static fn (Category $category) => $category->getParent()?->getId()->getBytes(), $categories);
55+
$parentCategoryIds = array_map(static fn (Category $category) => $primaryKey->convertToDatabaseValue($category->getParent()?->getId(), $platform), $categories);
5256
$parentCategoryIds = array_filter($parentCategoryIds, static fn (?string $id) => $id !== null);
5357

5458
if (count($parentCategoryIds) > 0) {
5559
$this->getEntityManager()->createQueryBuilder()
5660
->select('category')
5761
->from(Category::class, 'category')
5862
->where('category.id IN (:ids)')
59-
->setParameter('ids', array_values(array_unique($parentCategoryIds)), ArrayParameterType::BINARY)
63+
->setParameter('ids', array_values(array_unique($parentCategoryIds)), $this->deduceArrayParameterType($primaryKey))
6064
->getQuery()
6165
->getResult();
6266
}
@@ -70,9 +74,10 @@ public function testManyHasOneDeepWithManualPreload(): void
7074
]);
7175
}
7276

73-
public function testManyHasOneDeepWithFetchJoin(): void
77+
#[DataProvider('providePrimaryKeyTypes')]
78+
public function testManyHasOneDeepWithFetchJoin(DbalType $primaryKey): void
7479
{
75-
$this->createDummyBlogData(categoryCount: 5, categoryParentsCount: 5, articleInEachCategoryCount: 5);
80+
$this->createDummyBlogData($primaryKey, categoryCount: 5, categoryParentsCount: 5, articleInEachCategoryCount: 5);
7681

7782
$articles = $this->getEntityManager()->createQueryBuilder()
7883
->select('article', 'category', 'parentCategory')
@@ -89,10 +94,11 @@ public function testManyHasOneDeepWithFetchJoin(): void
8994
]);
9095
}
9196

92-
public function testManyHasOneDeepWithEagerFetchMode(): void
97+
#[DataProvider('providePrimaryKeyTypes')]
98+
public function testManyHasOneDeepWithEagerFetchMode(DbalType $primaryKey): void
9399
{
94100
$this->skipIfDoctrineOrmHasBrokenUnhandledMatchCase();
95-
$this->createDummyBlogData(categoryCount: 5, categoryParentsCount: 5, articleInEachCategoryCount: 5);
101+
$this->createDummyBlogData($primaryKey, categoryCount: 5, categoryParentsCount: 5, articleInEachCategoryCount: 5);
96102

97103
$articles = $this->getEntityManager()->createQueryBuilder()
98104
->select('article')
@@ -111,9 +117,10 @@ public function testManyHasOneDeepWithEagerFetchMode(): void
111117
]);
112118
}
113119

114-
public function testManyHasOneDeepWithPreload(): void
120+
#[DataProvider('providePrimaryKeyTypes')]
121+
public function testManyHasOneDeepWithPreload(DbalType $primaryKey): void
115122
{
116-
$this->createDummyBlogData(categoryCount: 5, categoryParentsCount: 5, articleInEachCategoryCount: 5);
123+
$this->createDummyBlogData($primaryKey, categoryCount: 5, categoryParentsCount: 5, articleInEachCategoryCount: 5);
117124

118125
$articles = $this->getEntityManager()->getRepository(Article::class)->findAll();
119126
$categories = $this->getEntityPreloader()->preload($articles, 'category');

tests/EntityPreloadBlogManyHasOneTest.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace ShipMonkTests\DoctrineEntityPreloader;
44

5-
use Doctrine\DBAL\ArrayParameterType;
5+
use Doctrine\DBAL\Types\Type as DbalType;
66
use Doctrine\ORM\Mapping\ClassMetadata;
7+
use PHPUnit\Framework\Attributes\DataProvider;
78
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\Article;
89
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\Category;
910
use ShipMonkTests\DoctrineEntityPreloader\Lib\TestCase;
@@ -16,9 +17,10 @@
1617
class EntityPreloadBlogManyHasOneTest extends TestCase
1718
{
1819

19-
public function testManyHasOneUnoptimized(): void
20+
#[DataProvider('providePrimaryKeyTypes')]
21+
public function testManyHasOneUnoptimized(DbalType $primaryKey): void
2022
{
21-
$this->createDummyBlogData(categoryCount: 5, articleInEachCategoryCount: 5);
23+
$this->createDummyBlogData($primaryKey, categoryCount: 5, articleInEachCategoryCount: 5);
2224

2325
$articles = $this->getEntityManager()->getRepository(Article::class)->findAll();
2426

@@ -30,21 +32,23 @@ public function testManyHasOneUnoptimized(): void
3032
]);
3133
}
3234

33-
public function testManyHasOneWithManualPreload(): void
35+
#[DataProvider('providePrimaryKeyTypes')]
36+
public function testManyHasOneWithManualPreload(DbalType $primaryKey): void
3437
{
35-
$this->createDummyBlogData(categoryCount: 5, articleInEachCategoryCount: 5);
38+
$this->createDummyBlogData($primaryKey, categoryCount: 5, articleInEachCategoryCount: 5);
3639

3740
$articles = $this->getEntityManager()->getRepository(Article::class)->findAll();
41+
$platform = $this->getEntityManager()->getConnection()->getDatabasePlatform();
3842

39-
$categoryIds = array_map(static fn (Article $article): ?string => $article->getCategory()?->getId()->getBytes(), $articles);
43+
$categoryIds = array_map(static fn (Article $article) => $primaryKey->convertToDatabaseValue($article->getCategory()?->getId(), $platform), $articles);
4044
$categoryIds = array_filter($categoryIds, static fn (?string $id) => $id !== null);
4145

4246
if (count($categoryIds) > 0) {
4347
$this->getEntityManager()->createQueryBuilder()
4448
->select('category')
4549
->from(Category::class, 'category')
4650
->where('category.id IN (:ids)')
47-
->setParameter('ids', array_values(array_unique($categoryIds)), ArrayParameterType::BINARY)
51+
->setParameter('ids', array_values(array_unique($categoryIds)), $this->deduceArrayParameterType($primaryKey))
4852
->getQuery()
4953
->getResult();
5054
}
@@ -57,9 +61,10 @@ public function testManyHasOneWithManualPreload(): void
5761
]);
5862
}
5963

60-
public function testManyHasOneWithFetchJoin(): void
64+
#[DataProvider('providePrimaryKeyTypes')]
65+
public function testManyHasOneWithFetchJoin(DbalType $primaryKey): void
6166
{
62-
$this->createDummyBlogData(categoryCount: 5, articleInEachCategoryCount: 5);
67+
$this->createDummyBlogData($primaryKey, categoryCount: 5, articleInEachCategoryCount: 5);
6368

6469
$articles = $this->getEntityManager()->createQueryBuilder()
6570
->select('article', 'category')
@@ -75,10 +80,11 @@ public function testManyHasOneWithFetchJoin(): void
7580
]);
7681
}
7782

78-
public function testManyHasOneWithEagerFetchMode(): void
83+
#[DataProvider('providePrimaryKeyTypes')]
84+
public function testManyHasOneWithEagerFetchMode(DbalType $primaryKey): void
7985
{
8086
$this->skipIfDoctrineOrmHasBrokenUnhandledMatchCase();
81-
$this->createDummyBlogData(categoryCount: 5, articleInEachCategoryCount: 5);
87+
$this->createDummyBlogData($primaryKey, categoryCount: 5, articleInEachCategoryCount: 5);
8288

8389
$articles = $this->getEntityManager()->createQueryBuilder()
8490
->select('article')
@@ -95,9 +101,10 @@ public function testManyHasOneWithEagerFetchMode(): void
95101
]);
96102
}
97103

98-
public function testManyHasOneWithPreload(): void
104+
#[DataProvider('providePrimaryKeyTypes')]
105+
public function testManyHasOneWithPreload(DbalType $primaryKey): void
99106
{
100-
$this->createDummyBlogData(categoryCount: 5, articleInEachCategoryCount: 5);
107+
$this->createDummyBlogData($primaryKey, categoryCount: 5, articleInEachCategoryCount: 5);
101108

102109
$articles = $this->getEntityManager()->getRepository(Article::class)->findAll();
103110
$this->getEntityPreloader()->preload($articles, 'category');

0 commit comments

Comments
 (0)