-
Notifications
You must be signed in to change notification settings - Fork 1
/
EntityPreloadBlogManyHasManyTest.php
117 lines (91 loc) · 4.56 KB
/
EntityPreloadBlogManyHasManyTest.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
<?php declare(strict_types = 1);
namespace ShipMonkTests\DoctrineEntityPreloader;
use Doctrine\ORM\Mapping\ClassMetadata;
use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\Article;
use ShipMonkTests\DoctrineEntityPreloader\Lib\TestCase;
class EntityPreloadBlogManyHasManyTest extends TestCase
{
public function testManyHasManyUnoptimized(): void
{
$this->createDummyBlogData(articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
$articles = $this->getEntityManager()->getRepository(Article::class)->findAll();
$this->readTagLabels($articles);
self::assertAggregatedQueries([
['count' => 1, 'query' => 'SELECT * FROM article t0'],
['count' => 5, 'query' => 'SELECT * FROM tag t0 INNER JOIN article_tag ON t0.id = article_tag.tag_id WHERE article_tag.article_id = ?'],
]);
}
public function testOneHasManyWithWithManualPreloadUsingPartial(): void
{
$this->skipIfPartialEntitiesAreNotSupported();
$this->createDummyBlogData(articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
$articles = $this->getEntityManager()->getRepository(Article::class)->findAll();
$this->getEntityManager()->createQueryBuilder()
->select('PARTIAL article.{id}', 'tag')
->from(Article::class, 'article')
->leftJoin('article.tags', 'tag')
->where('article IN (:articles)')
->setParameter('articles', $articles)
->getQuery()
->getResult();
$this->readTagLabels($articles);
self::assertAggregatedQueries([
['count' => 1, 'query' => 'SELECT * FROM article t0'],
['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 (?, ?, ?, ?, ?)'],
]);
}
public function testManyHasManyWithFetchJoin(): void
{
$this->createDummyBlogData(articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
$articles = $this->getEntityManager()->createQueryBuilder()
->select('article', 'tag')
->from(Article::class, 'article')
->leftJoin('article.tags', 'tag')
->getQuery()
->getResult();
$this->readTagLabels($articles);
self::assertAggregatedQueries([
['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'],
]);
}
public function testManyHasManyWithEagerFetchMode(): void
{
$this->createDummyBlogData(articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
// for eagerly loaded Many-To-Many associations one query has to be made for each collection
// https://www.doctrine-project.org/projects/doctrine-orm/en/3.2/reference/working-with-objects.html#by-eager-loading
$articles = $this->getEntityManager()->createQueryBuilder()
->select('article')
->from(Article::class, 'article')
->getQuery()
->setFetchMode(Article::class, 'tags', ClassMetadata::FETCH_EAGER)
->getResult();
$this->readTagLabels($articles);
self::assertAggregatedQueries([
['count' => 1, 'query' => 'SELECT * FROM article a0_'],
['count' => 5, 'query' => 'SELECT * FROM tag t0 INNER JOIN article_tag ON t0.id = article_tag.tag_id WHERE article_tag.article_id = ?'],
]);
}
public function testManyHasManyWithPreload(): void
{
$this->createDummyBlogData(articleInEachCategoryCount: 5, tagForEachArticleCount: 5);
$articles = $this->getEntityManager()->getRepository(Article::class)->findAll();
$this->getEntityPreloader()->preload($articles, 'tags');
$this->readTagLabels($articles);
self::assertAggregatedQueries([
['count' => 1, 'query' => 'SELECT * FROM article t0'],
['count' => 1, 'query' => 'SELECT * FROM article a0_ INNER JOIN article_tag a2_ ON a0_.id = a2_.article_id INNER JOIN tag t1_ ON t1_.id = a2_.tag_id WHERE a0_.id IN (?, ?, ?, ?, ?)'],
['count' => 1, 'query' => 'SELECT * FROM tag t0_ WHERE t0_.id IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'],
]);
}
/**
* @param array<Article> $articles
*/
private function readTagLabels(array $articles): void
{
foreach ($articles as $article) {
foreach ($article->getTags() as $tag) {
$tag->getLabel();
}
}
}
}