-
Notifications
You must be signed in to change notification settings - Fork 1
/
EntityPreloadBlogManyHasOneDeepTest.php
138 lines (109 loc) · 5.44 KB
/
EntityPreloadBlogManyHasOneDeepTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php declare(strict_types = 1);
namespace ShipMonkTests\DoctrineEntityPreloader;
use Doctrine\ORM\Mapping\ClassMetadata;
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\Article;
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\Category;
use ShipMonkTests\DoctrineEntityPreloader\Lib\TestCase;
use function array_filter;
use function array_map;
use function array_unique;
use function array_values;
use function count;
class EntityPreloadBlogManyHasOneDeepTest extends TestCase
{
public function testManyHasOneDeepUnoptimized(): void
{
$this->createDummyBlogData(categoryCount: 5, categoryParentsCount: 5, articleInEachCategoryCount: 5);
$articles = $this->getEntityManager()->getRepository(Article::class)->findAll();
$this->readArticleCategoryParentNames($articles);
self::assertAggregatedQueries([
['count' => 1, 'query' => 'SELECT * FROM article t0'],
['count' => 5 + 5, 'query' => 'SELECT * FROM category t0 WHERE t0.id = ?'],
]);
}
public function testManyHasOneDeepWithManualPreload(): void
{
$this->createDummyBlogData(categoryCount: 5, categoryParentsCount: 5, articleInEachCategoryCount: 5);
$articles = $this->getEntityManager()->getRepository(Article::class)->findAll();
$categoryIds = array_map(static fn (Article $article) => $article->getCategory()?->getId(), $articles);
$categoryIds = array_filter($categoryIds, static fn (?int $id) => $id !== null);
if (count($categoryIds) > 0) {
$categories = $this->getEntityManager()->createQueryBuilder()
->select('category')
->from(Category::class, 'category')
->where('category.id IN (:ids)')
->setParameter('ids', array_values(array_unique($categoryIds)))
->getQuery()
->getResult();
$parentCategoryIds = array_map(static fn (Category $category) => $category->getParent()?->getId(), $categories);
$parentCategoryIds = array_filter($parentCategoryIds, static fn (?int $id) => $id !== null);
if (count($parentCategoryIds) > 0) {
$this->getEntityManager()->createQueryBuilder()
->select('category')
->from(Category::class, 'category')
->where('category.id IN (:ids)')
->setParameter('ids', array_values(array_unique($parentCategoryIds)))
->getQuery()
->getResult();
}
}
$this->readArticleCategoryParentNames($articles);
self::assertAggregatedQueries([
['count' => 1, 'query' => 'SELECT * FROM article t0'],
['count' => 2, 'query' => 'SELECT * FROM category c0_ WHERE c0_.id IN (?, ?, ?, ?, ?)'],
]);
}
public function testManyHasOneDeepWithFetchJoin(): void
{
$this->createDummyBlogData(categoryCount: 5, categoryParentsCount: 5, articleInEachCategoryCount: 5);
$articles = $this->getEntityManager()->createQueryBuilder()
->select('article', 'category', 'parentCategory')
->from(Article::class, 'article')
->leftJoin('article.category', 'category')
->leftJoin('category.parent', 'parentCategory')
->getQuery()
->getResult();
$this->readArticleCategoryParentNames($articles);
self::assertAggregatedQueries([
['count' => 1, 'query' => 'SELECT * FROM article a0_ LEFT JOIN category c1_ ON a0_.category_id = c1_.id LEFT JOIN category c2_ ON c1_.parent_id = c2_.id'],
]);
}
public function testManyHasOneDeepWithEagerFetchMode(): void
{
$this->createDummyBlogData(categoryCount: 5, categoryParentsCount: 5, articleInEachCategoryCount: 5);
$articles = $this->getEntityManager()->createQueryBuilder()
->select('article')
->from(Article::class, 'article')
->getQuery()
->setFetchMode(Article::class, 'category', ClassMetadata::FETCH_EAGER)
->setFetchMode(Category::class, 'parent', ClassMetadata::FETCH_EAGER) // this does not work
->getResult();
$this->readArticleCategoryParentNames($articles);
self::assertAggregatedQueries([
['count' => 1, 'query' => 'SELECT * FROM article a0_'],
['count' => 1, 'query' => 'SELECT * FROM category t0 WHERE t0.id IN (?, ?, ?, ?, ?)'],
['count' => 5, 'query' => 'SELECT * FROM category t0 WHERE t0.id = ?'],
]);
}
public function testManyHasOneDeepWithPreload(): void
{
$this->createDummyBlogData(categoryCount: 5, categoryParentsCount: 5, articleInEachCategoryCount: 5);
$articles = $this->getEntityManager()->getRepository(Article::class)->findAll();
$categories = $this->getEntityPreloader()->preload($articles, 'category');
$this->getEntityPreloader()->preload($categories, 'parent');
$this->readArticleCategoryParentNames($articles);
self::assertAggregatedQueries([
['count' => 1, 'query' => 'SELECT * FROM article t0'],
['count' => 2, 'query' => 'SELECT * FROM category c0_ WHERE c0_.id IN (?, ?, ?, ?, ?)'],
]);
}
/**
* @param array<Article> $articles
*/
private function readArticleCategoryParentNames(array $articles): void
{
foreach ($articles as $article) {
$article->getCategory()?->getParent()?->getName();
}
}
}