|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace ShipMonkTests\DoctrineEntityPreloader; |
| 4 | + |
| 5 | +use Doctrine\ORM\Mapping\ClassMetadata; |
| 6 | +use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\Article; |
| 7 | +use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\Comment; |
| 8 | +use ShipMonkTests\DoctrineEntityPreloader\Lib\TestCase; |
| 9 | + |
| 10 | +class EntityPreloadBlogOneHasManyAbstractTest extends TestCase |
| 11 | +{ |
| 12 | + |
| 13 | + public function testOneHasManyAbstractUnoptimized(): void |
| 14 | + { |
| 15 | + $this->createDummyBlogData(categoryCount: 1, articleInEachCategoryCount: 5, commentForEachArticleCount: 5); |
| 16 | + |
| 17 | + $articles = $this->getEntityManager()->getRepository(Article::class)->findAll(); |
| 18 | + |
| 19 | + $this->readComments($articles); |
| 20 | + |
| 21 | + self::assertAggregatedQueries([ |
| 22 | + ['count' => 1, 'query' => 'SELECT * FROM article t0'], |
| 23 | + ['count' => 5, 'query' => 'SELECT * FROM comment t0 WHERE t0.article_id = ?'], |
| 24 | + ['count' => 25, 'query' => 'SELECT * FROM contributor t0 WHERE t0.id = ? AND t0.dtype IN (?)'], |
| 25 | + ]); |
| 26 | + } |
| 27 | + |
| 28 | + public function testOneHasManyAbstractWithFetchJoin(): void |
| 29 | + { |
| 30 | + $this->createDummyBlogData(categoryCount: 1, articleInEachCategoryCount: 5, commentForEachArticleCount: 5); |
| 31 | + |
| 32 | + $articles = $this->getEntityManager()->createQueryBuilder() |
| 33 | + ->select('article', 'comment', 'author') |
| 34 | + ->from(Article::class, 'article') |
| 35 | + ->leftJoin('article.comments', 'comment') |
| 36 | + ->leftJoin('comment.author', 'author') |
| 37 | + ->getQuery() |
| 38 | + ->getResult(); |
| 39 | + |
| 40 | + $this->readComments($articles); |
| 41 | + |
| 42 | + self::assertAggregatedQueries([ |
| 43 | + ['count' => 1, 'query' => 'SELECT * FROM article a0_ LEFT JOIN comment c1_ ON a0_.id = c1_.article_id LEFT JOIN contributor c2_ ON c1_.author_id = c2_.id AND c2_.dtype IN (?)'], |
| 44 | + ]); |
| 45 | + } |
| 46 | + |
| 47 | + public function testOneHasManyAbstractWithEagerFetchMode(): void |
| 48 | + { |
| 49 | + $this->createDummyBlogData(categoryCount: 1, articleInEachCategoryCount: 5, commentForEachArticleCount: 5); |
| 50 | + |
| 51 | + $articles = $this->getEntityManager()->createQueryBuilder() |
| 52 | + ->select('article') |
| 53 | + ->from(Article::class, 'article') |
| 54 | + ->getQuery() |
| 55 | + ->setFetchMode(Article::class, 'comments', ClassMetadata::FETCH_EAGER) |
| 56 | + ->setFetchMode(Comment::class, 'author', ClassMetadata::FETCH_EAGER) |
| 57 | + ->getResult(); |
| 58 | + |
| 59 | + $this->readComments($articles); |
| 60 | + |
| 61 | + self::assertAggregatedQueries([ |
| 62 | + ['count' => 1, 'query' => 'SELECT * FROM article a0_'], |
| 63 | + ['count' => 1, 'query' => 'SELECT * FROM comment t0 WHERE t0.article_id IN (?, ?, ?, ?, ?)'], |
| 64 | + ['count' => 25, 'query' => 'SELECT * FROM contributor t0 WHERE t0.id = ? AND t0.dtype IN (?)'], |
| 65 | + ]); |
| 66 | + } |
| 67 | + |
| 68 | + public function testOneHasManyAbstractWithPreload(): void |
| 69 | + { |
| 70 | + $this->createDummyBlogData(categoryCount: 1, articleInEachCategoryCount: 5, commentForEachArticleCount: 5); |
| 71 | + |
| 72 | + $articles = $this->getEntityManager()->getRepository(Article::class)->findAll(); |
| 73 | + $this->getEntityPreloader()->preload($articles, 'comments'); |
| 74 | + |
| 75 | + $this->readComments($articles); |
| 76 | + |
| 77 | + self::assertAggregatedQueries([ |
| 78 | + ['count' => 1, 'query' => 'SELECT * FROM article t0'], |
| 79 | + ['count' => 1, 'query' => 'SELECT * FROM comment c0_ LEFT JOIN contributor c1_ ON c0_.author_id = c1_.id AND c1_.dtype IN (?) WHERE c0_.article_id IN (?, ?, ?, ?, ?)'], |
| 80 | + ]); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @param array<Article> $articles |
| 85 | + */ |
| 86 | + private function readComments(array $articles): void |
| 87 | + { |
| 88 | + foreach ($articles as $article) { |
| 89 | + foreach ($article->getComments() as $comment) { |
| 90 | + $comment->getContent(); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments