Skip to content

Commit

Permalink
fix #1611 BlogPostsService::get のユニットテスト実装 (#1747)
Browse files Browse the repository at this point in the history
  • Loading branch information
HungDV2022 committed Jan 31, 2023
1 parent ab0f207 commit 2679d4c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions plugins/bc-blog/src/Service/BlogPostsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function setupUpload(int $blogContentId): void
* @return EntityInterface
* @checked
* @noTodo
* @unitTest
*/
public function get(int $id, array $options = [])
{
Expand Down
43 changes: 39 additions & 4 deletions plugins/bc-blog/tests/TestCase/Service/BlogPostsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

namespace BcBlog\Test\TestCase\Service;

use BaserCore\Test\Factory\UserFactory;
use BaserCore\Test\Factory\ContentFactory;
use BaserCore\Test\Factory\SiteFactory;
use BaserCore\Test\Factory\UserFactory;
use BaserCore\TestSuite\BcTestCase;
use BaserCore\Utility\BcContainerTrait;
use BcBlog\Model\Table\BlogPostsTable;
Expand Down Expand Up @@ -51,9 +52,9 @@ class BlogPostsServiceTest extends BcTestCase
'plugin.BaserCore.Factory/UsersUserGroups',
'plugin.BaserCore.Factory/UserGroups',
'plugin.BcBlog.Factory/BlogPosts',
'plugin.BcBlog.Factory/BlogContents',
'plugin.BaserCore.Factory/Contents',
'plugin.BcBlog.Factory/BlogTags',
'plugin.BaserCore.Factory/Contents',
'plugin.BcBlog.Factory/BlogContents',
'plugin.BcBlog.Factory/BlogCategories',
'plugin.BcBlog.Factory/BlogPostsBlogTags',
];
Expand Down Expand Up @@ -101,7 +102,41 @@ public function testSetupUpload()
*/
public function testGet()
{
$this->markTestIncomplete('このテストは、まだ実装されていません。');
// データを生成
BlogPostFactory::make(['id' => 1, 'blog_content_id' => 1, 'blog_category_id' => 1, 'status' => true])->persist();

ContentFactory::make(['id' => 1, 'site_id' => 1, 'type' => 'BlogContent', 'entity_id' => 1, 'title' => 'content title'])->persist();
SiteFactory::make(['id' => 1, 'name' => 'site name'])->persist();
BlogContentFactory::make(['id' => 1, 'description' => 'baser blog description', 'tag_use' => true])->persist();
BlogCategoryFactory::make(['id' => 1, 'blog_content_id' => 1, 'name' => 'category name'])->persist();

BlogTagFactory::make(['id' => 1])->persist();
BlogTagFactory::make(['id' => 2])->persist();
BlogPostBlogTagFactory::make(['blog_post_id' => 1, 'blog_tag_id' => 1])->persist();
BlogPostBlogTagFactory::make(['blog_post_id' => 1, 'blog_tag_id' => 2])->persist();

// サービスメソッドを呼ぶ
// option status publish 公開状態にある
$result = $this->BlogPostsService->get(1, ['status' => 'publish']);
// 戻り値を確認
$this->assertTrue($result->status);
//BlogCategoryを取得できるか確認
$this->assertEquals('category name', $result->blog_category->name);
//blogTagsを取得できるか確認
$this->assertCount(2, $result->blog_tags);
//BlogContentを取得できるか確認
$this->assertEquals('baser blog description', $result->blog_content->description);
//BlogContentのコンテンツを取得できるか確認
$this->assertEquals('content title', $result->blog_content->content->title);
//BlogPostのサイトを取得できるか確認
$this->assertEquals('site name', $result->blog_content->content->site->name);

//BlogPostが非公開にする
$this->BlogPostsService->unpublish(1);
// 例外を表示
$this->expectException('Cake\Datasource\Exception\RecordNotFoundException');
// $options の status を publish として、レコードの status が true でないデータを指定す
$this->BlogPostsService->get(1, ['status' => 'publish']);
}

/**
Expand Down

0 comments on commit 2679d4c

Please sign in to comment.