-
Notifications
You must be signed in to change notification settings - Fork 1
/
EntityPreloadBlogOneHasManyDeepTest.php
169 lines (138 loc) · 6.68 KB
/
EntityPreloadBlogOneHasManyDeepTest.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php declare(strict_types = 1);
namespace ShipMonkTests\DoctrineEntityPreloader;
use Doctrine\ORM\Mapping\ClassMetadata;
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\Category;
use ShipMonkTests\DoctrineEntityPreloader\Lib\TestCase;
use function array_map;
use function array_merge;
class EntityPreloadBlogOneHasManyDeepTest extends TestCase
{
public function testOneHasManyDeepUnoptimized(): void
{
$this->createCategoryTree(depth: 5, branchingFactor: 5);
$rootCategories = $this->getEntityManager()->createQueryBuilder()
->select('category')
->from(Category::class, 'category')
->where('category.parent IS NULL')
->getQuery()
->getResult();
$this->readSubSubCategoriesNames($rootCategories);
self::assertAggregatedQueries([
['count' => 1, 'query' => 'SELECT * FROM category c0_ WHERE c0_.parent_id IS NULL'],
['count' => 5 + 25, 'query' => 'SELECT * FROM category t0 WHERE t0.parent_id = ?'],
]);
}
public function testOneHasManyDeepWithWithManualPreloadUsingPartial(): void
{
$this->skipIfPartialEntitiesAreNotSupported();
$this->createCategoryTree(depth: 5, branchingFactor: 5);
$rootCategories = $this->getEntityManager()->createQueryBuilder()
->select('category')
->from(Category::class, 'category')
->where('category.parent IS NULL')
->getQuery()
->getResult();
$this->getEntityManager()->createQueryBuilder()
->select('PARTIAL category.{id}', 'subCategory')
->from(Category::class, 'category')
->leftJoin('category.children', 'subCategory')
->where('category IN (:categories)')
->setParameter('categories', $rootCategories)
->getQuery()
->getResult();
$subCategories = array_merge(...array_map(static fn(Category $category) => $category->getChildren()->toArray(), $rootCategories));
$this->getEntityManager()->createQueryBuilder()
->select('PARTIAL subCategory.{id}', 'subSubCategory')
->from(Category::class, 'subCategory')
->leftJoin('subCategory.children', 'subSubCategory')
->where('subCategory IN (:subCategories)')
->setParameter('subCategories', $subCategories)
->getQuery()
->getResult();
$this->readSubSubCategoriesNames($rootCategories);
self::assertAggregatedQueries([
['count' => 1, 'query' => 'SELECT * FROM category c0_ WHERE c0_.parent_id IS NULL'],
['count' => 1, 'query' => 'SELECT * FROM category c0_ LEFT JOIN category c1_ ON c0_.id = c1_.parent_id WHERE c0_.id IN (?, ?, ?, ?, ?)'],
['count' => 1, 'query' => 'SELECT * FROM category c0_ LEFT JOIN category c1_ ON c0_.id = c1_.parent_id WHERE c0_.id IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'],
]);
}
public function testOneHasManyDeepWithFetchJoin(): void
{
$this->createCategoryTree(depth: 5, branchingFactor: 5);
$rootCategories = $this->getEntityManager()->createQueryBuilder()
->select('category', 'subCategories', 'subSubCategories')
->from(Category::class, 'category')
->leftJoin('category.children', 'subCategories')
->leftJoin('subCategories.children', 'subSubCategories')
->where('category.parent IS NULL')
->getQuery()
->getResult();
$this->readSubSubCategoriesNames($rootCategories);
self::assertAggregatedQueries([
['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'],
]);
}
public function testOneHasManyDeepWithEagerFetchMode(): void
{
$this->createCategoryTree(depth: 5, branchingFactor: 5);
$rootCategories = $this->getEntityManager()->createQueryBuilder()
->select('category')
->from(Category::class, 'category')
->where('category.parent IS NULL')
->getQuery()
->setFetchMode(Category::class, 'children', ClassMetadata::FETCH_EAGER)
->getResult();
$this->readSubSubCategoriesNames($rootCategories);
self::assertAggregatedQueries([
['count' => 1, 'query' => 'SELECT * FROM category c0_ WHERE c0_.parent_id IS NULL'],
['count' => 1, 'query' => 'SELECT * FROM category t0 WHERE t0.parent_id IN (?, ?, ?, ?, ?)'],
['count' => 25, 'query' => 'SELECT * FROM category t0 WHERE t0.parent_id = ?'],
]);
}
public function testOneHasManyDeepWithPreload(): void
{
$this->createCategoryTree(depth: 5, branchingFactor: 5);
$rootCategories = $this->getEntityManager()->createQueryBuilder()
->select('category')
->from(Category::class, 'category')
->where('category.parent IS NULL')
->getQuery()
->getResult();
$subCategories = $this->getEntityPreloader()->preload($rootCategories, 'children');
$this->getEntityPreloader()->preload($subCategories, 'children');
$this->readSubSubCategoriesNames($rootCategories);
self::assertAggregatedQueries([
['count' => 1, 'query' => 'SELECT * FROM category c0_ WHERE c0_.parent_id IS NULL'],
['count' => 1, 'query' => 'SELECT * FROM category c0_ WHERE c0_.parent_id IN (?, ?, ?, ?, ?)'],
['count' => 1, 'query' => 'SELECT * FROM category c0_ WHERE c0_.parent_id IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'],
]);
}
private function createCategoryTree(int $depth, int $branchingFactor, ?Category $parent = null): void
{
for ($i = 0; $i < $branchingFactor; $i++) {
$category = new Category("Category $depth-$i", $parent);
$this->getEntityManager()->persist($category);
if ($depth > 1) {
$this->createCategoryTree($depth - 1, $branchingFactor, $category);
}
}
if ($parent === null) {
$this->getEntityManager()->flush();
$this->getEntityManager()->clear();
$this->getQueryLogger()->clear();
}
}
/**
* @param array<Category> $categories
*/
private function readSubSubCategoriesNames(array $categories): void
{
foreach ($categories as $category) {
foreach ($category->getChildren() as $child) {
foreach ($child->getChildren() as $child2) {
$child2->getName();
}
}
}
}
}