Skip to content

Commit

Permalink
fix #1612 BlogPostsService::getControlSource のユニットテスト実装 (#1748)
Browse files Browse the repository at this point in the history
  • Loading branch information
HungDV2022 committed Jan 16, 2023
1 parent 5d9183e commit 98bd9f4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
1 change: 1 addition & 0 deletions plugins/bc-blog/src/Service/BlogPostsService.php
Expand Up @@ -562,6 +562,7 @@ public function allowPublish(EntityInterface $post)
* @return array|false
* @checked
* @noTodo
* @unitTest
*/
public function getControlSource(string $field, array $options = [])
{
Expand Down
2 changes: 2 additions & 0 deletions plugins/bc-blog/tests/TestCase/Model/BlogTagsTableTest.php
Expand Up @@ -23,6 +23,8 @@ class BlogTagsTableTest extends BcTestCase
{

public $fixtures = [
'plugin.BcBlog.Factory/BlogPosts',
'plugin.BcBlog.Factory/BlogPostsBlogTags',
'plugin.BcBlog.Factory/BlogTags',
];

Expand Down
52 changes: 45 additions & 7 deletions plugins/bc-blog/tests/TestCase/Service/BlogPostsServiceTest.php
Expand Up @@ -20,7 +20,6 @@
use BcBlog\Service\BlogPostsService;
use BcBlog\Service\BlogPostsServiceInterface;
use BcBlog\Test\Factory\BlogCategoryFactory;
use BcBlog\Test\Factory\BlogContentFactory;
use BcBlog\Test\Factory\BlogPostBlogTagFactory;
use BcBlog\Test\Factory\BlogPostFactory;
use BcBlog\Test\Factory\BlogTagFactory;
Expand Down Expand Up @@ -56,6 +55,9 @@ class BlogPostsServiceTest extends BcTestCase
'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 @@ -332,16 +334,16 @@ public function testCreateTagCondition()
$this->assertEquals(1, $result["BlogPosts.id IN"][0]);

//配列:存在しているタグを確認場合、
$result = $this->BlogPostsService->createTagCondition([], ['tag1','tag2']);
$result = $this->BlogPostsService->createTagCondition([], ['tag1', 'tag2']);
$this->assertEquals(1, $result["BlogPosts.id IN"][0]);
$this->assertEquals(2, $result['BlogPosts.id IN'][1]);

//配列:存在しているタグと存在していないタグを確認場合、
$result = $this->BlogPostsService->createTagCondition([], ['tag1111','tag2']);
$result = $this->BlogPostsService->createTagCondition([], ['tag1111', 'tag2']);
$this->assertEquals(2, $result["BlogPosts.id IN"][0]);

//配列:存在していないタグを確認場合、
$result = $this->BlogPostsService->createTagCondition([], ['tag1111','tag22222']);
$result = $this->BlogPostsService->createTagCondition([], ['tag1111', 'tag22222']);
$this->assertNull($result["BlogPosts.id IS"]);
}

Expand Down Expand Up @@ -660,8 +662,8 @@ public function testCreateExceptionPosted()
*/
// public function testCreateExceptionPostMaxSize()
// {
// TODO ローカルでは成功するが、GitHubActions上でうまくいかないためコメントアウト(原因不明)
// データ量を超えていると仮定する
// TODO ローカルでは成功するが、GitHubActions上でうまくいかないためコメントアウト(原因不明)
// データ量を超えていると仮定する
// $postMaxSize = ini_get('post_max_size');
// $_SERVER['REQUEST_METHOD'] = 'POST';
// $_SERVER['CONTENT_LENGTH'] = BcUtil::convertSize($postMaxSize) + 1;
Expand Down Expand Up @@ -729,7 +731,43 @@ public function testAllowPublish()
*/
public function testGetControlSource()
{
$this->markTestIncomplete('このテストは、まだ実装されていません。');
BlogCategoryFactory::make(['id' => 1, 'blog_content_id' => 1, 'title' => 'test title 4'])->persist();
BlogTagFactory::make(['id' => 2, 'name' => 'tag name1'])->persist();
BlogTagFactory::make(['id' => 3, 'name' => 'tag name2'])->persist();
BlogPostBlogTagFactory::make(['blog_tag_id' => 2])->persist();
BlogPostBlogTagFactory::make(['blog_tag_id' => 3])->persist();
UserFactory::make(['id' => '1', 'name' => 'test 1', 'nickname' => 'James'])->persist();
UserFactory::make(['id' => '2', 'nickname' => 'nyc'])->persist();

//$field = blog_category_id かつ blogContentId = 1
$result = $this->BlogPostsService->getControlSource('blog_category_id', ['blogContentId' => 1]);
//戻り値を確認
$this->assertEquals('test title 4', $result[1]);

//$field = blog_category_id かつ 配列 blogContentIdと他の変数 
$result = $this->BlogPostsService->getControlSource('blog_category_id', ['blogContentId' => 1, 'empty' => 'sd']);
//戻り値を確認
$this->assertEquals(['' => 'sd', 1 => 'test title 4'], $result);

//$field = blog_category_id かつ 配列 存在しないblogContentId
$result = $this->BlogPostsService->getControlSource('blog_category_id', ['blogContentId' => '2']);
//戻り値を確認
$this->assertEquals([], $result);

//$field = user_id かつ 空配列
$result = $this->BlogPostsService->getControlSource('user_id');
//戻り値を確認
$this->assertEquals([1 => 'James', 2 => 'nyc'], $result);

//$field = user_id かつ 条件配列
$result = $this->BlogPostsService->getControlSource('user_id', [['name' => 'test 1']]);
//戻り値を確認
$this->assertEquals([1 => 'James'], $result);

//$field = blog_tag_id かつ 空配列
$result = $this->BlogPostsService->getControlSource('blog_tag_id');
//戻り値を確認
$this->assertEquals([2 => 'tag name1', 3 => 'tag name2'], $result);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions plugins/bc-blog/tests/TestCase/View/Helper/BlogHelperTest.php
Expand Up @@ -53,6 +53,8 @@ class BlogHelperTest extends BcTestCase
'plugin.BcBlog.Factory/BlogContents',
'plugin.BcBlog.Factory/BlogCategories',
'plugin.BcBlog.Factory/BlogPosts',
'plugin.BcBlog.Factory/BlogTags',
'plugin.BcBlog.Factory/BlogPostsBlogTags',
];

/**
Expand Down

0 comments on commit 98bd9f4

Please sign in to comment.