Skip to content

Commit

Permalink
ユニットテスト BlogCommentsService::add (#3085)
Browse files Browse the repository at this point in the history
Co-authored-by: thangnn <[email protected]>
  • Loading branch information
thangnnmd and thangnn authored Feb 26, 2024
1 parent 6004600 commit 5da3f64
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
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
43 changes: 43 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,46 @@ 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 = [
'name' => 'baserCMS',
];
$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']);

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

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

}

0 comments on commit 5da3f64

Please sign in to comment.