Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ユニットテスト BlogCommentsService::add #3085

Merged
merged 6 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugins/bc-blog/src/Service/BlogCommentsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public function getNew()
* @param array $postData
* @return EntityInterface
* @checked
* @unitTest
*/
public function add(int $blogContentId, int $blogPostId, array $postData)
{
Expand Down
49 changes: 49 additions & 0 deletions plugins/bc-blog/tests/TestCase/Service/BlogCommentsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use BaserCore\TestSuite\BcTestCase;
use BcBlog\Service\BlogCommentsService;
use BcBlog\Test\Factory\BlogCommentFactory;
use BcBlog\Test\Factory\BlogPostFactory;
use BcBlog\Test\Scenario\BlogCommentsScenario;
use BcBlog\Test\Scenario\BlogCommentsServiceScenario;
Expand Down Expand Up @@ -256,4 +257,52 @@ public function testGetNew()
$result = $this->BlogCommentsService->getNew();
$this->assertEquals('NO NAME', $result['name']);
}
/**
* add
* @return void
*/
public function testAdd()
{
//data test
$this->loadFixtureScenario(
BlogContentScenario::class,
1, // id
1, // siteId
null, // parentId
'news1', // name
'/news/' // url
);
BlogPostFactory::make(['id' => 1, 'blog_content_id' => 1])->persist();
$data = [
'id' => 1,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BlogCommentは、id、no、status 自動生成ですので、post しても意味がありませんので、削除お願いします。
@HungDV2022 @thangnnmd

'no' => 1,
'status' => 1,
'name' => 'baserCMS',
];
HungDV2022 marked this conversation as resolved.
Show resolved Hide resolved
$result = $this->BlogCommentsService->add(1, 1, $data);

//check result return
$this->assertEquals('baserCMS', $result['name']);

//confirm result add
$comment = BlogCommentFactory::get(1);
$this->assertEquals($data['name'], $comment['name']);
$this->assertEquals(1, $comment['blog_content_id']);
HungDV2022 marked this conversation as resolved.
Show resolved Hide resolved

// null name
$data = [
'id' => 1,
'no' => 1,
'status' => 1,
'name' => null,
];
$this->expectExceptionMessage("お名前を入力してください。");
$this->BlogCommentsService->add(1, 1, $data);

//Exception
$this->expectException("Cake\ORM\Exception\PersistenceFailedException");
$this->expectExceptionMessage("関連するコンテンツがありません");
$this->BlogCommentsService->add(1, 1, []);
}

}
Loading