Skip to content

Commit 4d739a4

Browse files
authored
fix tests compatibility with Doctrine 3.3.0 which reenabled partial (#11)
1 parent c9485d5 commit 4d739a4

4 files changed

+90
-18
lines changed

tests/EntityPreloadBlogManyHasManyTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,30 @@ public function testManyHasManyUnoptimized(): void
2323
]);
2424
}
2525

26+
public function testOneHasManyWithWithManualPreloadUsingPartial(): void
27+
{
28+
$this->skipIfPartialEntitiesAreNotSupported();
29+
$this->createDummyBlogData(articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
30+
31+
$articles = $this->getEntityManager()->getRepository(Article::class)->findAll();
32+
33+
$this->getEntityManager()->createQueryBuilder()
34+
->select('PARTIAL article.{id}', 'tag')
35+
->from(Article::class, 'article')
36+
->leftJoin('article.tags', 'tag')
37+
->where('article IN (:articles)')
38+
->setParameter('articles', $articles)
39+
->getQuery()
40+
->getResult();
41+
42+
$this->readTagLabels($articles);
43+
44+
self::assertAggregatedQueries([
45+
['count' => 1, 'query' => 'SELECT * FROM article t0'],
46+
['count' => 1, 'query' => 'SELECT * FROM article a0_ LEFT JOIN article_tag a2_ ON a0_.id = a2_.article_id LEFT JOIN tag t1_ ON t1_.id = a2_.tag_id WHERE a0_.id IN (?, ?, ?, ?, ?)'],
47+
]);
48+
}
49+
2650
public function testManyHasManyWithFetchJoin(): void
2751
{
2852
$this->createDummyBlogData(articleInEachCategoryCount: 5, tagForEachArticleCount: 5);

tests/EntityPreloadBlogOneHasManyDeepTest.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Doctrine\ORM\Mapping\ClassMetadata;
66
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\Category;
77
use ShipMonkTests\DoctrineEntityPreloader\Lib\TestCase;
8+
use function array_map;
9+
use function array_merge;
810

911
class EntityPreloadBlogOneHasManyDeepTest extends TestCase
1012
{
@@ -28,6 +30,46 @@ public function testOneHasManyDeepUnoptimized(): void
2830
]);
2931
}
3032

33+
public function testOneHasManyDeepWithWithManualPreloadUsingPartial(): void
34+
{
35+
$this->skipIfPartialEntitiesAreNotSupported();
36+
$this->createCategoryTree(depth: 5, branchingFactor: 5);
37+
38+
$rootCategories = $this->getEntityManager()->createQueryBuilder()
39+
->select('category')
40+
->from(Category::class, 'category')
41+
->where('category.parent IS NULL')
42+
->getQuery()
43+
->getResult();
44+
45+
$this->getEntityManager()->createQueryBuilder()
46+
->select('PARTIAL category.{id}', 'subCategory')
47+
->from(Category::class, 'category')
48+
->leftJoin('category.children', 'subCategory')
49+
->where('category IN (:categories)')
50+
->setParameter('categories', $rootCategories)
51+
->getQuery()
52+
->getResult();
53+
54+
$subCategories = array_merge(...array_map(static fn(Category $category) => $category->getChildren()->toArray(), $rootCategories));
55+
$this->getEntityManager()->createQueryBuilder()
56+
->select('PARTIAL subCategory.{id}', 'subSubCategory')
57+
->from(Category::class, 'subCategory')
58+
->leftJoin('subCategory.children', 'subSubCategory')
59+
->where('subCategory IN (:subCategories)')
60+
->setParameter('subCategories', $subCategories)
61+
->getQuery()
62+
->getResult();
63+
64+
$this->readSubSubCategoriesNames($rootCategories);
65+
66+
self::assertAggregatedQueries([
67+
['count' => 1, 'query' => 'SELECT * FROM category c0_ WHERE c0_.parent_id IS NULL'],
68+
['count' => 1, 'query' => 'SELECT * FROM category c0_ LEFT JOIN category c1_ ON c0_.id = c1_.parent_id WHERE c0_.id IN (?, ?, ?, ?, ?)'],
69+
['count' => 1, 'query' => 'SELECT * FROM category c0_ LEFT JOIN category c1_ ON c0_.id = c1_.parent_id WHERE c0_.id IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'],
70+
]);
71+
}
72+
3173
public function testOneHasManyDeepWithFetchJoin(): void
3274
{
3375
$this->createCategoryTree(depth: 5, branchingFactor: 5);
@@ -37,13 +79,14 @@ public function testOneHasManyDeepWithFetchJoin(): void
3779
->from(Category::class, 'category')
3880
->leftJoin('category.children', 'subCategories')
3981
->leftJoin('subCategories.children', 'subSubCategories')
82+
->where('category.parent IS NULL')
4083
->getQuery()
4184
->getResult();
4285

4386
$this->readSubSubCategoriesNames($rootCategories);
4487

4588
self::assertAggregatedQueries([
46-
['count' => 1, 'query' => 'SELECT * FROM category c0_ LEFT JOIN category c1_ ON c0_.id = c1_.parent_id LEFT JOIN category c2_ ON c1_.id = c2_.parent_id'],
89+
['count' => 1, 'query' => 'SELECT * FROM category c0_ LEFT JOIN category c1_ ON c0_.id = c1_.parent_id LEFT JOIN category c2_ ON c1_.id = c2_.parent_id WHERE c0_.parent_id IS NULL'],
4790
]);
4891
}
4992

tests/EntityPreloadBlogOneHasManyTest.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
namespace ShipMonkTests\DoctrineEntityPreloader;
44

5-
use Composer\InstalledVersions;
65
use Doctrine\ORM\Mapping\ClassMetadata;
7-
use Doctrine\ORM\Query\QueryException;
86
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\Article;
97
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\Category;
108
use ShipMonkTests\DoctrineEntityPreloader\Lib\TestCase;
11-
use function str_starts_with;
129

1310
class EntityPreloadBlogOneHasManyTest extends TestCase
1411
{
@@ -52,31 +49,26 @@ public function testOneHasManyWithWithManualPreload(): void
5249

5350
public function testOneHasManyWithWithManualPreloadUsingPartial(): void
5451
{
52+
$this->skipIfPartialEntitiesAreNotSupported();
5553
$this->createDummyBlogData(categoryCount: 5, articleInEachCategoryCount: 5);
5654

5755
$categories = $this->getEntityManager()->getRepository(Category::class)->findAll();
5856

59-
$query = $this->getEntityManager()->createQueryBuilder()
57+
$this->getEntityManager()->createQueryBuilder()
6058
->select('PARTIAL category.{id}', 'article')
6159
->from(Category::class, 'category')
6260
->leftJoin('category.articles', 'article')
6361
->where('category IN (:categories)')
6462
->setParameter('categories', $categories)
65-
->getQuery();
66-
67-
if (str_starts_with(InstalledVersions::getVersion('doctrine/orm') ?? 'unknown', '3.')) {
68-
self::assertException(QueryException::class, null, static fn() => $query->getResult());
69-
70-
} else {
71-
$query->getResult();
63+
->getQuery()
64+
->getResult();
7265

73-
$this->readArticleTitles($categories);
66+
$this->readArticleTitles($categories);
7467

75-
self::assertAggregatedQueries([
76-
['count' => 1, 'query' => 'SELECT * FROM category t0'],
77-
['count' => 1, 'query' => 'SELECT * FROM category c0_ LEFT JOIN article a1_ ON c0_.id = a1_.category_id WHERE c0_.id IN (?, ?, ?, ?, ?)'],
78-
]);
79-
}
68+
self::assertAggregatedQueries([
69+
['count' => 1, 'query' => 'SELECT * FROM category t0'],
70+
['count' => 1, 'query' => 'SELECT * FROM category c0_ LEFT JOIN article a1_ ON c0_.id = a1_.category_id WHERE c0_.id IN (?, ?, ?, ?, ?)'],
71+
]);
8072
}
8173

8274
public function testOneHasManyWithFetchJoin(): void
@@ -137,6 +129,8 @@ public function testOneHasManyWithPreload(): void
137129
private function readArticleTitles(array $categories): void
138130
{
139131
foreach ($categories as $category) {
132+
$category->getName();
133+
140134
foreach ($category->getArticles() as $article) {
141135
$article->getTitle();
142136
}

tests/Lib/TestCase.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ShipMonkTests\DoctrineEntityPreloader\Lib;
44

5+
use Composer\InstalledVersions;
56
use Doctrine\DBAL\DriverManager;
67
use Doctrine\DBAL\Logging\Middleware;
78
use Doctrine\ORM\EntityManager;
@@ -23,6 +24,7 @@
2324
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\User;
2425
use Throwable;
2526
use function unlink;
27+
use function version_compare;
2628

2729
abstract class TestCase extends PhpUnitTestCase
2830
{
@@ -175,6 +177,15 @@ protected function refreshExistingEntity(object $entity): object
175177
return $freshEntity;
176178
}
177179

180+
protected function skipIfPartialEntitiesAreNotSupported(): void
181+
{
182+
$ormVersion = InstalledVersions::getVersion('doctrine/orm') ?? '0.0.0';
183+
184+
if (version_compare($ormVersion, '3.0.0', '>=') && version_compare($ormVersion, '3.3.0', '<')) {
185+
self::markTestSkipped('Partial entities are not supported in Doctrine ORM versions 3.0 to 3.2');
186+
}
187+
}
188+
178189
protected function getQueryLogger(): QueryLogger
179190
{
180191
return $this->queryLogger ??= $this->createQueryLogger();

0 commit comments

Comments
 (0)