Skip to content

Commit d51c7f8

Browse files
committed
updated category filters
Signed-off-by: bidi47 <bidi@apidemia.com>
1 parent 8f858e0 commit d51c7f8

7 files changed

Lines changed: 301 additions & 6 deletions

File tree

‎src/Blog/src/Entity/Category.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ public function setSlug(string $slug): void
5858
$this->slug = $slug;
5959
}
6060

61+
/**
62+
* Visibility only controls the category sidebar widget (and the 404/410 pages, post, tag, author
63+
* and page views that render it), via CategoryRepository::getCategories().
64+
*
65+
* It does not hide a category that has published posts: those are still listed on /categories/
66+
* and in the sitemap (CategoryRepository::getCategoriesWithPublishedPosts()), and their
67+
* /category/{slug}/ page is served.
68+
*/
6169
public function isVisible(): bool
6270
{
6371
return $this->isVisible;

‎src/Blog/src/Handler/GetCategoryCollectionHandler.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct(
2121

2222
public function handle(ServerRequestInterface $request): ResponseInterface
2323
{
24-
$categories = $this->categoryRepository->getCategories();
24+
$categories = $this->categoryRepository->getCategoriesWithPublishedPosts();
2525

2626
return new HtmlResponse(
2727
$this->template->render('page::categories', [

‎src/Blog/src/Handler/GetCategoryResourceHandler.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
3131
if ($category === null) {
3232
return $this->blogService->notFound($categories);
3333
}
34-
if (! $category->isVisible()) {
34+
if ($category->getPublishedPostsCount() === 0) {
3535
return $this->blogService->gone($categories);
3636
}
3737
$meta = $category;

‎src/Blog/src/Repository/CategoryRepository.php‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ public function getCategoriesWithPublishedPosts(): array
4242
->from(Category::class, 'categories');
4343

4444
$qb->where($qb->expr()->in('categories.id', $publishedCategoryIds->getDQL()))
45-
->andWhere('categories.isVisible = :visible')
46-
->setParameter('published', PostStatusEnum::Published)
47-
->setParameter('visible', true);
45+
->setParameter('published', PostStatusEnum::Published);
4846

4947
return $qb->getQuery()->getResult();
5048
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LightTest\Unit\Blog\Handler;
6+
7+
use Laminas\Diactoros\ServerRequest;
8+
use Light\Blog\Entity\Category;
9+
use Light\Blog\Handler\GetCategoryCollectionHandler;
10+
use Light\Blog\Repository\CategoryRepository;
11+
use LightTest\Unit\UnitTest;
12+
use Mezzio\Template\TemplateRendererInterface;
13+
use PHPUnit\Framework\MockObject\Exception;
14+
15+
class GetCategoryCollectionHandlerTest extends UnitTest
16+
{
17+
/**
18+
* Visibility must not decide what /categories/ lists: only categories with published posts do.
19+
*
20+
* @throws Exception
21+
*/
22+
public function testHandleListsCategoriesWithPublishedPostsRegardlessOfVisibility(): void
23+
{
24+
$hiddenWithPosts = new Category();
25+
$hiddenWithPosts->setVisibility(false);
26+
$categories = [$hiddenWithPosts];
27+
28+
$categoryRepository = $this->createMock(CategoryRepository::class);
29+
$categoryRepository->expects($this->once())
30+
->method('getCategoriesWithPublishedPosts')
31+
->willReturn($categories);
32+
$categoryRepository->expects($this->never())->method('getCategories');
33+
34+
$template = $this->createMock(TemplateRendererInterface::class);
35+
$template->expects($this->once())
36+
->method('render')
37+
->with('page::categories', ['categories' => $categories])
38+
->willReturn('<html lang="en"></html>');
39+
40+
$response = (new GetCategoryCollectionHandler($template, $categoryRepository))
41+
->handle(new ServerRequest());
42+
43+
$this->assertSame(200, $response->getStatusCode());
44+
}
45+
46+
/**
47+
* @throws Exception
48+
*/
49+
public function testHandleRendersEmptyListWhenNoCategoryHasPublishedPosts(): void
50+
{
51+
$categoryRepository = $this->createStub(CategoryRepository::class);
52+
$categoryRepository->method('getCategoriesWithPublishedPosts')->willReturn([]);
53+
54+
$template = $this->createMock(TemplateRendererInterface::class);
55+
$template->expects($this->once())
56+
->method('render')
57+
->with('page::categories', ['categories' => []])
58+
->willReturn('');
59+
60+
$response = (new GetCategoryCollectionHandler($template, $categoryRepository))
61+
->handle(new ServerRequest());
62+
63+
$this->assertSame(200, $response->getStatusCode());
64+
}
65+
}

‎test/Unit/Blog/Handler/GetCategoryResourceHandlerTest.php‎

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace LightTest\Unit\Blog\Handler;
66

7+
use Doctrine\ORM\Query;
8+
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;
79
use Fig\Http\Message\StatusCodeInterface;
810
use Laminas\Diactoros\Response\HtmlResponse;
911
use Laminas\Diactoros\ServerRequest;
@@ -31,16 +33,87 @@ public function testHandleReturnsNotFoundWhenCategoryDoesNotExist(): void
3133
/**
3234
* @throws Exception
3335
*/
34-
public function testHandleReturnsGoneWhenCategoryIsNotVisible(): void
36+
public function testHandleReturnsGoneWhenCategoryIsNotVisibleAndHasNoPublishedPosts(): void
3537
{
3638
$category = $this->createStub(Category::class);
3739
$category->method('isVisible')->willReturn(false);
40+
$category->method('getPublishedPostsCount')->willReturn(0);
3841

3942
$response = $this->handle($category);
4043

4144
$this->assertSame(410, $response->getStatusCode());
4245
}
4346

47+
/**
48+
* @throws Exception
49+
*/
50+
public function testHandleReturnsGoneWhenVisibleCategoryHasNoPublishedPosts(): void
51+
{
52+
$category = $this->createStub(Category::class);
53+
$category->method('isVisible')->willReturn(true);
54+
$category->method('getPublishedPostsCount')->willReturn(0);
55+
56+
$response = $this->handle($category);
57+
58+
$this->assertSame(410, $response->getStatusCode());
59+
}
60+
61+
/**
62+
* A hidden category is listed on /categories/ when it has published posts, so its page must be served.
63+
*
64+
* @throws Exception
65+
*/
66+
public function testHandleRendersHiddenCategoryThatHasPublishedPosts(): void
67+
{
68+
$category = $this->createStub(Category::class);
69+
$category->method('isVisible')->willReturn(false);
70+
$category->method('getPublishedPostsCount')->willReturn(2);
71+
72+
$this->assertSame(200, $this->handleRendering($category)->getStatusCode());
73+
}
74+
75+
/**
76+
* @throws Exception
77+
*/
78+
public function testHandleRendersVisibleCategoryThatHasPublishedPosts(): void
79+
{
80+
$category = $this->createStub(Category::class);
81+
$category->method('isVisible')->willReturn(true);
82+
$category->method('getPublishedPostsCount')->willReturn(1);
83+
84+
$this->assertSame(200, $this->handleRendering($category)->getStatusCode());
85+
}
86+
87+
/**
88+
* @throws Exception
89+
*/
90+
private function handleRendering(Category $category): ResponseInterface
91+
{
92+
$query = $this->createStub(Query::class);
93+
$query->method('getResult')->willReturn([]);
94+
95+
$paginator = $this->createStub(DoctrinePaginator::class);
96+
$paginator->method('count')->willReturn(0);
97+
$paginator->method('getQuery')->willReturn($query);
98+
99+
$categoryRepository = $this->createStub(CategoryRepository::class);
100+
$categoryRepository->method('getCategories')->willReturn([]);
101+
$categoryRepository->method('getCategoryResource')->willReturn($category);
102+
$categoryRepository->method('getCategoryPost')->willReturn($paginator);
103+
104+
$template = $this->createMock(TemplateRendererInterface::class);
105+
$template->expects($this->once())->method('render')
106+
->with('page::category-resource', $this->anything())
107+
->willReturn('<html lang="en"></html>');
108+
109+
$blogService = $this->createMock(BlogServiceInterface::class);
110+
$blogService->expects($this->never())->method('gone');
111+
$blogService->expects($this->never())->method('notFound');
112+
113+
return (new GetCategoryResourceHandler($template, $categoryRepository, $blogService))
114+
->handle((new ServerRequest())->withAttribute('slug', 'a-category'));
115+
}
116+
44117
/**
45118
* @throws Exception
46119
*/
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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

Comments
 (0)