Skip to content

Commit 1a2f0a7

Browse files
author
olaru
committed
Add tests
1 parent fa5ec74 commit 1a2f0a7

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,85 @@ public function testHandleFallsBackToHtmlWhenNoMarkdownFileExistsForTheRequested
116116
$this->assertStringContainsString('text/html', $response->getHeaderLine('Content-Type'));
117117
}
118118

119+
/**
120+
* @throws Exception
121+
*/
122+
public function testHandleRendersTheTwigTemplateWhenTheArticleIsFlaggedAsTwig(): void
123+
{
124+
$article = $this->createStub(Post::class);
125+
$article->method('getStatus')->willReturn(PostStatusEnum::Published);
126+
$article->method('isTwig')->willReturn(true);
127+
128+
$postRepository = $this->createStub(PostRepository::class);
129+
$postRepository->method('getArticleResource')->willReturn($article);
130+
$postRepository->method('getAdjacentPosts')->willReturn(['previous' => null, 'next' => null]);
131+
132+
$categoryRepository = $this->createStub(CategoryRepository::class);
133+
$categoryRepository->method('getCategories')->willReturn([$this->createStub(Category::class)]);
134+
135+
$template = $this->createMock(TemplateRendererInterface::class);
136+
$template->expects($this->once())
137+
->method('render')
138+
->with('page::blog-resource/a-category/a-slug', $this->anything())
139+
->willReturn('<html lang="en">twig</html>');
140+
141+
$handler = new GetPostResourceHandler(
142+
$template,
143+
$postRepository,
144+
$categoryRepository,
145+
$this->createStub(BlogServiceInterface::class),
146+
);
147+
148+
$request = (new ServerRequest())->withAttribute('slug', 'a-slug')->withAttribute('categorySlug', 'a-category');
149+
$response = $handler->handle($request);
150+
151+
$this->assertSame(200, $response->getStatusCode());
152+
}
153+
154+
/**
155+
* @throws Exception
156+
*/
157+
public function testHandleRendersTheMarkdownArticleForAPublishedPost(): void
158+
{
159+
$markdownFile = sys_get_temp_dir() . '/' . uniqid('dk-article-', true) . '.md';
160+
file_put_contents($markdownFile, "---\ntitle: \"A Title\"\n---\n\n# A Title\n\nSome body content.");
161+
162+
$article = $this->createStub(Post::class);
163+
$article->method('getStatus')->willReturn(PostStatusEnum::Published);
164+
$article->method('isTwig')->willReturn(false);
165+
166+
$postRepository = $this->createStub(PostRepository::class);
167+
$postRepository->method('getArticleResource')->willReturn($article);
168+
$postRepository->method('getAdjacentPosts')->willReturn(['previous' => null, 'next' => null]);
169+
170+
$categoryRepository = $this->createStub(CategoryRepository::class);
171+
$categoryRepository->method('getCategories')->willReturn([$this->createStub(Category::class)]);
172+
173+
$template = $this->createMock(TemplateRendererInterface::class);
174+
$template->expects($this->once())
175+
->method('render')
176+
->with(
177+
'page::markdown-article',
178+
$this->callback(
179+
static fn (array $params): bool => $params['content'] === 'Some body content.'
180+
&& $params['faq'] === []
181+
)
182+
)
183+
->willReturn('<html lang="en">md</html>');
184+
185+
$blogService = $this->createStub(BlogServiceInterface::class);
186+
$blogService->method('resolveMarkdownFilePath')->willReturn($markdownFile);
187+
188+
$handler = new GetPostResourceHandler($template, $postRepository, $categoryRepository, $blogService);
189+
190+
$request = (new ServerRequest())->withAttribute('slug', 'a-slug')->withAttribute('categorySlug', 'a-category');
191+
$response = $handler->handle($request);
192+
193+
$this->assertSame(200, $response->getStatusCode());
194+
195+
unlink($markdownFile);
196+
}
197+
119198
/**
120199
* @throws Exception
121200
*/

0 commit comments

Comments
 (0)