Skip to content

Commit

Permalink
fix #1656 BlogPostsService::getNextPost のユニットテスト実装 (#1754)
Browse files Browse the repository at this point in the history
Co-authored-by: Đỗ Văn Hùng <HungDV2022>
  • Loading branch information
HungDV2022 committed Jan 16, 2023
1 parent cfce84b commit ed1518d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
3 changes: 3 additions & 0 deletions plugins/bc-blog/src/Service/BlogPostsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,9 @@ public function getPrevPost(BlogPost $post)
*
* @param BlogPost $post ブログ記事
* @return BlogPost|EntityInterface
* @checked
* @noTodo
* @unitTest
*/
public function getNextPost(BlogPost $post)
{
Expand Down
48 changes: 47 additions & 1 deletion plugins/bc-blog/tests/TestCase/Service/BlogPostsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,53 @@ public function testGetPrevPost()
*/
public function testGetNextPost()
{
$this->markTestIncomplete('このテストは、まだ実装されていません。');
//データ生成
BlogPostFactory::make([
'id' => 1,
'blog_content_id' => 3,
'title' => 'blog post 1',
'posted' => '2022-10-02 09:00:00',
'status' => 0,
'publish_begin' => '2021-10-01 09:00:00',
'publish_end' => '9999-11-01 09:00:00'
])->persist();
BlogPostFactory::make([
'id' => 2,
'blog_content_id' => 3,
'title' => 'blog post 2',
'posted' => '2022-10-02 09:00:00',
'status' => 1,
'publish_begin' => '2021-02-01 09:00:00',
'publish_end' => '9999-12-01 09:00:00'
])->persist();
BlogPostFactory::make([
'id' => 3,
'blog_content_id' => 3,
'title' => 'blog post 3',
'posted' => '2022-08-02 09:00:00',
'status' => 1,
'publish_begin' => '2021-05-06 09:00:00',
'publish_end' => '9999-02-01 09:00:00'
])->persist();

//投稿日が年月日時分秒が同一のデータの対応のため、投稿日が同じでIDが小さいデータを検索
$result = $this->BlogPostsService->getNextPost(BlogPostFactory::get(1));
//戻り値を確認
$this->assertEquals(2, $result->id);
$this->assertEquals(3, $result->blog_content_id);
$this->assertEquals("blog post 2", $result->title);

//テスト投稿日が新しいデータを取得
$result = $this->BlogPostsService->getNextPost(BlogPostFactory::get(3));
//戻り値を確認
$this->assertEquals(2, $result->id);
$this->assertEquals(3, $result->blog_content_id);
$this->assertEquals("blog post 2", $result->title);

//テスト status=0, 結果はnullに戻る
$result = $this->BlogPostsService->getNextPost(BlogPostFactory::get(2));
//戻り値を確認
$this->assertNull($result);
}

}

0 comments on commit ed1518d

Please sign in to comment.