Skip to content

Commit

Permalink
dataProvider内で、find() を実行していたため、他のテストに影響が出ていたので調整
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuring committed Nov 6, 2023
1 parent badbec2 commit 53df311
Showing 1 changed file with 134 additions and 135 deletions.
269 changes: 134 additions & 135 deletions plugins/bc-blog/tests/TestCase/Service/BlogPostsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,141 +429,140 @@ public function testCreateOrder()
* ページ一覧用の検索条件を生成する
* @dataProvider createIndexConditionsDataProvider
*/
public function testCreateIndexConditions($isLoadScenario, $query, $options, $expected)
{
if ($isLoadScenario) {
$this->loadFixtureScenario(MultiSiteBlogScenario::class);
BlogPostFactory::make([])->publish(1, 1)->persist();
BlogTagFactory::make(['id' => 1, 'name' => 'tag1'])->persist();
BlogPostBlogTagFactory::make(['blog_post_id' => 1, 'blog_tag_id' => 1])->persist();
UserFactory::make(['id' => 1, 'name' => 'test_author'])->persist();
}

$result = $this->execPrivateMethod($this->BlogPostsService, "createIndexConditions", [$query, $options]);
$this->assertEquals($expected, $result);
}

/**
* createIndexConditionsテストのデータプロバイダ
* @return array
*/
public function createIndexConditionsDataProvider(): array
{
$blogPost = new BlogPostsTable();
return [
// 空配列の結果テスト
[
false,
$blogPost->find('all'),
[],
$blogPost->find('all')
],
//$params ID
[
false,
$blogPost->find('all'),
['id' => 1],
$blogPost->find('all')->where(['BlogPosts.id' => 1])
],
//$params タイトル
[
false,
$blogPost->find('all'),
['title' => 'test title'],
$blogPost->find('all')->where(['BlogPosts.title LIKE' => '%test title%'])
],
//$params ユーザーID
[
false,
$blogPost->find('all'),
['user_id' => 1], $blogPost->find('all')->where(['BlogPosts.user_id' => 1])
],
//$params ブログコンテンツID
[
false,
$blogPost->find('all'),
['blog_content_id' => 1],
$blogPost->find('all')->where(['BlogPosts.blog_content_id' => 1])
],
//$params サイトID
[
false,
$blogPost->find('all'),
['site_id' => 1],
$blogPost->find('all')->where(['Contents.site_id' => 1])
],
//$params URL
[
false,
$blogPost->find('all'),
['contentUrl' => '/test'],
$blogPost->find('all')->contain(['BlogContents' => ['Contents']])->where(['Contents.url' => '/test'])
],
//$params カテゴリID
[
true,
$blogPost->find('all'),
['blog_category_id' => 1],
$blogPost->find('all')->where(['BlogPosts.blog_category_id IN' => [1, 2]])
],
//$params カテゴリ名
[
true,
$blogPost->find('all'),
['category' => 'release', 'force' => true],
$blogPost->find('all')->where(['BlogPosts.blog_category_id IN' => [1, 2]])
],
//$params タグ名
[
true,
$blogPost->find('all'),
['tag' => 'tag1'],
$blogPost->find('all')->where(['BlogPosts.id IN' => 1])
],
//$params 年月日
[
false,
$blogPost->find('all'),
['year' => 1994, 'month' => 8, 'day' => 21],
$blogPost->find('all')->where([
'YEAR(BlogPosts.posted)' => 1994,
'MONTH(BlogPosts.posted)' => 8,
'DAY(BlogPosts.posted)' => 21
])
],
//$params No
[
false,
$blogPost->find('all'),
['no' => 1, 'force' => true],
$blogPost->find('all')->where(['BlogPosts.no' => 1])
],
// $params キーワード
[
false,
$blogPost->find('all'),
['keyword' => 'test'],
$blogPost->find('all')->where([
'and' => [
0 => [
'or' => [
['BlogPosts.title LIKE' => '%test%'],
['BlogPosts.content LIKE' => '%test%'],
['BlogPosts.detail LIKE' => '%test%']
]
]
]
])],
//$params 作成者
[
true,
$blogPost->find('all'),
['author' => 'test_author'],
$blogPost->find('all')->where(['BlogPosts.user_id' => 1])
],
];
}

// TODO dataProvider内で、find() を実行しないように変更
// dataProvider内で、find() を実行すると、DBの接続先が、test でなく、default になってしまい、
// 全体実行の際に、他のテストに影響が出てしまうため。
// public function testCreateIndexConditions($isLoadScenario, $query, $options, $expected)
// {
// $users = $this->getTableLocator()->get('BaserCore.Users');
// if ($isLoadScenario) {
// $this->loadFixtureScenario(MultiSiteBlogScenario::class);
// BlogPostFactory::make([])->publish(1, 1)->persist();
// BlogTagFactory::make(['id' => 1, 'name' => 'tag1'])->persist();
// BlogPostBlogTagFactory::make(['blog_post_id' => 1, 'blog_tag_id' => 1])->persist();
// UserFactory::make(['id' => 1, 'name' => 'test_author'])->persist();
// }
//
// $result = $this->execPrivateMethod($this->BlogPostsService, "createIndexConditions", [$query, $options]);
// $this->assertEquals($expected, $result);
// }
//
// public function createIndexConditionsDataProvider(): array
// {
// $blogPost = new BlogPostsTable();
// return [
// // 空配列の結果テスト
// [
// false,
// $blogPost->find('all'),
// [],
// $blogPost->find('all')
// ],
// //$params ID
// [
// false,
// $blogPost->find('all'),
// ['id' => 1],
// $blogPost->find('all')->where(['BlogPosts.id' => 1])
// ],
// //$params タイトル
// [
// false,
// $blogPost->find('all'),
// ['title' => 'test title'],
// $blogPost->find('all')->where(['BlogPosts.title LIKE' => '%test title%'])
// ],
// //$params ユーザーID
// [
// false,
// $blogPost->find('all'),
// ['user_id' => 1], $blogPost->find('all')->where(['BlogPosts.user_id' => 1])
// ],
// //$params ブログコンテンツID
// [
// false,
// $blogPost->find('all'),
// ['blog_content_id' => 1],
// $blogPost->find('all')->where(['BlogPosts.blog_content_id' => 1])
// ],
// //$params サイトID
// [
// false,
// $blogPost->find('all'),
// ['site_id' => 1],
// $blogPost->find('all')->where(['Contents.site_id' => 1])
// ],
// //$params URL
// [
// false,
// $blogPost->find('all'),
// ['contentUrl' => '/test'],
// $blogPost->find('all')->contain(['BlogContents' => ['Contents']])->where(['Contents.url' => '/test'])
// ],
// //$params カテゴリID
// [
// true,
// $blogPost->find('all'),
// ['blog_category_id' => 1],
// $blogPost->find('all')->where(['BlogPosts.blog_category_id IN' => [1, 2]])
// ],
// //$params カテゴリ名
// [
// true,
// $blogPost->find('all'),
// ['category' => 'release', 'force' => true],
// $blogPost->find('all')->where(['BlogPosts.blog_category_id IN' => [1, 2]])
// ],
// //$params タグ名
// [
// true,
// $blogPost->find('all'),
// ['tag' => 'tag1'],
// $blogPost->find('all')->where(['BlogPosts.id IN' => 1])
// ],
// //$params 年月日
// [
// false,
// $blogPost->find('all'),
// ['year' => 1994, 'month' => 8, 'day' => 21],
// $blogPost->find('all')->where([
// 'YEAR(BlogPosts.posted)' => 1994,
// 'MONTH(BlogPosts.posted)' => 8,
// 'DAY(BlogPosts.posted)' => 21
// ])
// ],
// //$params No
// [
// false,
// $blogPost->find('all'),
// ['no' => 1, 'force' => true],
// $blogPost->find('all')->where(['BlogPosts.no' => 1])
// ],
// // $params キーワード
// [
// false,
// $blogPost->find('all'),
// ['keyword' => 'test'],
// $blogPost->find('all')->where([
// 'and' => [
// 0 => [
// 'or' => [
// ['BlogPosts.title LIKE' => '%test%'],
// ['BlogPosts.content LIKE' => '%test%'],
// ['BlogPosts.detail LIKE' => '%test%']
// ]
// ]
// ]
// ])],
// //$params 作成者
// [
// true,
// $blogPost->find('all'),
// ['author' => 'test_author'],
// $blogPost->find('all')->where(['BlogPosts.user_id' => 1])
// ],
// ];
// }

/**
* 同じタグの関連投稿を取得する
Expand Down

0 comments on commit 53df311

Please sign in to comment.