|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace LightTest\Unit\Blog\Repository; |
| 6 | + |
| 7 | +use Doctrine\Common\Collections\ArrayCollection; |
| 8 | +use Doctrine\ORM\EntityManagerInterface; |
| 9 | +use Doctrine\ORM\Mapping\ClassMetadata; |
| 10 | +use Doctrine\ORM\Query; |
| 11 | +use Doctrine\ORM\Query\Expr; |
| 12 | +use Doctrine\ORM\Query\Parameter; |
| 13 | +use Doctrine\ORM\QueryBuilder; |
| 14 | +use Light\Blog\Entity\Category; |
| 15 | +use Light\Blog\Enum\PostStatusEnum; |
| 16 | +use Light\Blog\Repository\CategoryRepository; |
| 17 | +use LightTest\Unit\UnitTest; |
| 18 | +use PHPUnit\Framework\MockObject\Exception; |
| 19 | +use PHPUnit\Framework\MockObject\MockObject; |
| 20 | + |
| 21 | +class CategoryRepositoryTest extends UnitTest |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @throws Exception |
| 25 | + */ |
| 26 | + public function testGetCategoriesWithPublishedPostsDoesNotFilterOnVisibility(): void |
| 27 | + { |
| 28 | + $categories = [new Category()]; |
| 29 | + $query = $this->createChainableQuery(); |
| 30 | + $query->expects($this->once())->method('getResult')->willReturn($categories); |
| 31 | + |
| 32 | + $dql = null; |
| 33 | + $entityManager = $this->createEntityManager(function (string $queryDql) use (&$dql, $query): Query { |
| 34 | + $dql = $queryDql; |
| 35 | + |
| 36 | + return $query; |
| 37 | + }); |
| 38 | + |
| 39 | + $result = $this->createRepository($entityManager)->getCategoriesWithPublishedPosts(); |
| 40 | + |
| 41 | + $this->assertSame($categories, $result); |
| 42 | + $this->assertIsString($dql); |
| 43 | + $this->assertStringNotContainsString('isVisible', $dql); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Only published posts count: a category with just drafts, private or archived posts is not listed. |
| 48 | + * |
| 49 | + * @throws Exception |
| 50 | + */ |
| 51 | + public function testGetCategoriesWithPublishedPostsOnlyConsidersPublishedPosts(): void |
| 52 | + { |
| 53 | + $query = $this->createChainableQuery(); |
| 54 | + $query->expects($this->once())->method('getResult')->willReturn([]); |
| 55 | + |
| 56 | + $dql = null; |
| 57 | + $entityManager = $this->createEntityManager(function (string $queryDql) use (&$dql, $query): Query { |
| 58 | + $dql = $queryDql; |
| 59 | + |
| 60 | + return $query; |
| 61 | + }); |
| 62 | + |
| 63 | + $this->createRepository($entityManager)->getCategoriesWithPublishedPosts(); |
| 64 | + |
| 65 | + $this->assertIsString($dql); |
| 66 | + $this->assertStringContainsString('post.status = :published', $dql); |
| 67 | + $this->assertStringContainsString('categories.id IN', $dql); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @throws Exception |
| 72 | + */ |
| 73 | + public function testGetCategoriesWithPublishedPostsBindsThePublishedStatus(): void |
| 74 | + { |
| 75 | + $query = $this->createChainableQuery(); |
| 76 | + $query->method('getResult')->willReturn([]); |
| 77 | + $query->expects($this->once())->method('setParameters')->with($this->callback( |
| 78 | + function (ArrayCollection $parameters): bool { |
| 79 | + $parameter = $parameters->first(); |
| 80 | + |
| 81 | + return $parameters->count() === 1 |
| 82 | + && $parameter instanceof Parameter |
| 83 | + && $parameter->getName() === 'published' |
| 84 | + && $parameter->getValue() === PostStatusEnum::Published; |
| 85 | + } |
| 86 | + ))->willReturnSelf(); |
| 87 | + |
| 88 | + $entityManager = $this->createEntityManager(static fn (): Query => $query); |
| 89 | + |
| 90 | + $this->createRepository($entityManager)->getCategoriesWithPublishedPosts(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @throws Exception |
| 95 | + */ |
| 96 | + public function testGetCategoriesStillFiltersOnVisibility(): void |
| 97 | + { |
| 98 | + $query = $this->createChainableQuery(); |
| 99 | + $query->expects($this->once())->method('getResult')->willReturn([]); |
| 100 | + |
| 101 | + $dql = null; |
| 102 | + $entityManager = $this->createEntityManager(function (string $queryDql) use (&$dql, $query): Query { |
| 103 | + $dql = $queryDql; |
| 104 | + |
| 105 | + return $query; |
| 106 | + }); |
| 107 | + |
| 108 | + $this->createRepository($entityManager)->getCategories(); |
| 109 | + |
| 110 | + $this->assertIsString($dql); |
| 111 | + $this->assertStringContainsString('categories.isVisible = :visible', $dql); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Entity manager whose query builders are real (so DQL is built for real) and whose |
| 116 | + * createQuery() hands the generated DQL to $onCreateQuery. |
| 117 | + * |
| 118 | + * @param callable(string): Query $onCreateQuery |
| 119 | + * @throws Exception |
| 120 | + */ |
| 121 | + private function createEntityManager(callable $onCreateQuery): EntityManagerInterface |
| 122 | + { |
| 123 | + $entityManager = $this->createStub(EntityManagerInterface::class); |
| 124 | + $entityManager->method('createQueryBuilder') |
| 125 | + ->willReturnCallback(static fn (): QueryBuilder => new QueryBuilder($entityManager)); |
| 126 | + $entityManager->method('getExpressionBuilder')->willReturn(new Expr()); |
| 127 | + $entityManager->method('createQuery') |
| 128 | + ->willReturnCallback(static fn (string $dql = ''): Query => $onCreateQuery($dql)); |
| 129 | + |
| 130 | + return $entityManager; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @throws Exception |
| 135 | + */ |
| 136 | + private function createChainableQuery(): Query&MockObject |
| 137 | + { |
| 138 | + $query = $this->createMock(Query::class); |
| 139 | + $query->method('setParameters')->willReturnSelf(); |
| 140 | + $query->method('setParameter')->willReturnSelf(); |
| 141 | + $query->method('setFirstResult')->willReturnSelf(); |
| 142 | + $query->method('setMaxResults')->willReturnSelf(); |
| 143 | + |
| 144 | + return $query; |
| 145 | + } |
| 146 | + |
| 147 | + private function createRepository(EntityManagerInterface $entityManager): CategoryRepository |
| 148 | + { |
| 149 | + return new CategoryRepository($entityManager, new ClassMetadata(Category::class)); |
| 150 | + } |
| 151 | +} |
0 commit comments