Repository navigation
Expand file tree
/
Copy pathEntityPreloadBlogOneHasManyTest.php
More file actions
196 lines (156 loc) · 7.95 KB
/
Copy pathEntityPreloadBlogOneHasManyTest.php
File metadata and controls
196 lines (156 loc) · 7.95 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php declare(strict_types = 1);
namespace ShipMonkTests\DoctrineEntityPreloader;
use Doctrine\DBAL\Types\Type as DbalType;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\PersistentCollection;
use PHPUnit\Framework\Attributes\DataProvider;
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\Article;
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\Category;
use ShipMonkTests\DoctrineEntityPreloader\Lib\TestCase;
use function array_map;
use function array_values;
class EntityPreloadBlogOneHasManyTest extends TestCase
{
#[DataProvider('providePrimaryKeyTypes')]
public function testOneHasManyUnoptimized(DbalType $primaryKey): void
{
$this->createDummyBlogData($primaryKey, categoryCount: 5, articleInEachCategoryCount: 5);
$categories = $this->getEntityManager()->getRepository(Category::class)->findAll();
$this->readArticleTitles($categories);
self::assertAggregatedQueries([
['count' => 1, 'query' => 'SELECT * FROM category t0'],
['count' => 5, 'query' => 'SELECT * FROM article t0 WHERE t0.category_id = ?'],
]);
}
#[DataProvider('providePrimaryKeyTypes')]
public function testOneHasManyWithWithManualPreload(DbalType $primaryKey): void
{
$this->createDummyBlogData($primaryKey, categoryCount: 5, articleInEachCategoryCount: 5);
$categories = $this->getEntityManager()->getRepository(Category::class)->findAll();
$this->getEntityManager()->createQueryBuilder()
->select('article')
->from(Article::class, 'article')
->where('article.category IN (:categories)')
->setParameter('categories', $categories)
->getQuery()
->getResult();
$this->readArticleTitles($categories);
self::assertAggregatedQueries([
['count' => 1, 'query' => 'SELECT * FROM category t0'],
['count' => 1, 'query' => 'SELECT * FROM article a0_ WHERE a0_.category_id IN (?, ?, ?, ?, ?)'],
['count' => 5, 'query' => 'SELECT * FROM article t0 WHERE t0.category_id = ?'],
]);
}
#[DataProvider('providePrimaryKeyTypes')]
public function testOneHasManyWithWithManualPreloadUsingPartial(DbalType $primaryKey): void
{
$this->skipIfPartialEntitiesAreNotSupported();
$this->createDummyBlogData($primaryKey, categoryCount: 5, articleInEachCategoryCount: 5);
$categories = $this->getEntityManager()->getRepository(Category::class)->findAll();
$platform = $this->getEntityManager()->getConnection()->getDatabasePlatform();
$rawCategoryIds = array_map(
static fn (Category $category) => $primaryKey->convertToDatabaseValue($category->getId(), $platform),
$categories,
);
$this->getEntityManager()->createQueryBuilder()
->select('PARTIAL category.{id}', 'article')
->from(Category::class, 'category')
->leftJoin('category.articles', 'article')
->where('category IN (:categories)')
->setParameter('categories', $rawCategoryIds, $this->deduceArrayParameterType($primaryKey))
->getQuery()
->getResult();
$this->readArticleTitles($categories);
self::assertAggregatedQueries([
['count' => 1, 'query' => 'SELECT * FROM category t0'],
['count' => 1, 'query' => 'SELECT * FROM category c0_ LEFT JOIN article a1_ ON c0_.id = a1_.category_id WHERE c0_.id IN (?, ?, ?, ?, ?)'],
]);
}
#[DataProvider('providePrimaryKeyTypes')]
public function testOneHasManyWithFetchJoin(DbalType $primaryKey): void
{
$this->createDummyBlogData($primaryKey, categoryCount: 5, articleInEachCategoryCount: 5);
$categories = $this->getEntityManager()->createQueryBuilder()
->select('category', 'article')
->from(Category::class, 'category')
->leftJoin('category.articles', 'article')
->getQuery()
->getResult();
$this->readArticleTitles($categories);
self::assertAggregatedQueries([
['count' => 1, 'query' => 'SELECT * FROM category c0_ LEFT JOIN article a1_ ON c0_.id = a1_.category_id'],
]);
}
#[DataProvider('providePrimaryKeyTypes')]
public function testOneHasManyWithEagerFetchMode(DbalType $primaryKey): void
{
$this->skipIfDoctrineOrmHasBrokenUnhandledMatchCase();
$this->skipIfDoctrineOrmHasBrokenEagerFetch($primaryKey); // here the test it green, but emits PHP warning
$this->createDummyBlogData($primaryKey, categoryCount: 5, articleInEachCategoryCount: 5);
$categories = $this->getEntityManager()->createQueryBuilder()
->select('category')
->from(Category::class, 'category')
->getQuery()
->setFetchMode(Category::class, 'articles', ClassMetadata::FETCH_EAGER)
->getResult();
$this->readArticleTitles($categories);
self::assertAggregatedQueries([
['count' => 1, 'query' => 'SELECT * FROM category c0_'],
['count' => 1, 'query' => 'SELECT * FROM article t0 WHERE t0.category_id IN (?, ?, ?, ?, ?)'],
]);
}
#[DataProvider('providePrimaryKeyTypes')]
public function testOneHasManyWithPreload(DbalType $primaryKey): void
{
$this->createDummyBlogData($primaryKey, categoryCount: 5, articleInEachCategoryCount: 5);
$categories = $this->getEntityManager()->getRepository(Category::class)->findAll();
$this->getEntityPreloader()->preload($categories, 'articles');
$this->readArticleTitles($categories);
self::assertAggregatedQueries([
['count' => 1, 'query' => 'SELECT * FROM category t0'],
['count' => 1, 'query' => 'SELECT * FROM article a0_ WHERE a0_.category_id IN (?, ?, ?, ?, ?)'],
]);
}
#[DataProvider('providePrimaryKeyTypes')]
public function testOneHasManyWithPreloadOfUninitializedCollectionWithFlushedElement(DbalType $primaryKey): void
{
$this->createDummyBlogData($primaryKey, categoryCount: 2, articleInEachCategoryCount: 2);
$categories = $this->getEntityManager()->getRepository(Category::class)->findAll();
$articles = $categories[0]->getArticles();
self::assertInstanceOf(PersistentCollection::class, $articles);
$newArticle = new Article('NewArticle', 'Content of new article');
$this->getEntityManager()->getClassMetadata(Article::class)->setFieldValue($newArticle, 'category', $categories[0]); // Category::addArticle() would initialize the collection
$this->getEntityManager()->persist($newArticle);
$articles->add($newArticle); // does not initialize the collection
$this->getEntityManager()->flush(); // takes snapshot, so the collection is not dirty, but it is still uninitialized and holds the new article
self::assertFalse($articles->isInitialized());
self::assertFalse($articles->isDirty());
$this->getEntityPreloader()->preload($categories, 'articles');
$this->getQueryLogger()->clear();
self::assertEqualsCanonicalizing(['Article#0', 'Article#1', 'NewArticle'], $this->getArticleTitles($categories[0]));
self::assertEqualsCanonicalizing(['Article#0', 'Article#1'], $this->getArticleTitles($categories[1]));
self::assertAggregatedQueries([]); // preloaded collections are initialized
}
/**
* @return list<string>
*/
private function getArticleTitles(Category $category): array
{
return array_values(array_map(
static fn (Article $article): string => $article->getTitle(),
$category->getArticles()->toArray(),
));
}
/**
* @param array<Category> $categories
*/
private function readArticleTitles(array $categories): void
{
foreach ($categories as $category) {
$category->getName();
foreach ($category->getArticles() as $article) {
$article->getTitle();
}
}
}
}