Skip to content

Commit

Permalink
Merge pull request #4035 from HungDV2022/unittest_BcMigration_table
Browse files Browse the repository at this point in the history
BcMigration::table() のユニットテスト
  • Loading branch information
HungDV2022 authored Nov 27, 2024
2 parents 2789ce4 + a2468f6 commit d823fd5
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 3 deletions.
1 change: 1 addition & 0 deletions plugins/baser-core/src/Database/Migration/BcMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class BcMigration extends AbstractMigration
* @return \Migrations\Table
* @checked
* @noTodo
* @unitTest
*/
public function table(string $tableName, array $options = []): Table
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* baserCMS : Based Website Development Project <https://basercms.net>
* Copyright (c) NPO baser foundation <https://baserfoundation.org/>
*
* @copyright Copyright (c) NPO baser foundation
* @link https://basercms.net baserCMS Project
* @since 5.0.0
* @license https://basercms.net/license/index.html MIT License
*/

namespace BaserCore\Test\TestCase\Database\Migration;

use BaserCore\Database\Migration\BcMigration;
use BaserCore\TestSuite\BcTestCase;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\IntegrationTestTrait;
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;
use Phinx\Db\Adapter\AdapterFactory;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;

/**
* Class BcMigrationTest
*
*/
class BcMigrationTest extends BcTestCase
{
use IntegrationTestTrait;
use ScenarioAwareTrait;

/**
* スキーム
*
* @var BcMigration
*/
protected $BcMigration;

/**
* set up
*
* @return void
*/
public function setUp(): void
{
parent::setUp();
$this->BcMigration = new BcMigration('localhost', 20200101010101);
}

/**
* tearDown
*
* @return void
*/
public function tearDown(): void
{
parent::tearDown();
}

/**
* Test create
*
* プレフィックスを変更するテストを行う場合、
* 他のテストで、トランザクション処理を行う際にデッドロックが発生してしまう様子。
* 原因が不明のため、一旦、プレフィックスの変更テストは行わない
*/
public function testTable()
{
//準備 Adapterをセットアップ
$input = new ArgvInput(['cli.php', 'foo']);
$input->bind(new InputDefinition([new InputArgument('name')]));
$this->BcMigration->setInput($input);
$options = [
'adapter' => 'mysql',
'host' => 'bc-db',
'user' => 'root',
'pass' => 'root',
'port' => '3306',
'name' => 'test_basercms'
];
$factory = AdapterFactory::instance();
$adapter = $factory->getAdapter('mysql', $options);
$this->BcMigration->setAdapter($adapter);

// 実行
$rs = $this->BcMigration->table('test');
$this->assertEquals('test', $rs->getName());
}
}
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 @@ -768,6 +768,7 @@ public function batch(string $method, array $ids): bool
{
if (!$ids) return true;
$db = $this->BlogPosts->getConnection();
if (!method_exists($this, $method)) return false;
$db->begin();
foreach($ids as $id) {
if (!$this->$method($id)) {
Expand Down
1 change: 1 addition & 0 deletions plugins/bc-blog/src/Service/BlogTagsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ public function batch(string $method, array $ids): bool
{
if (!$ids) return true;
$db = $this->BlogTags->getConnection();
if (!method_exists($this, $method)) return false;
$db->begin();
foreach($ids as $id) {
if (!$this->$method($id)) {
Expand Down
10 changes: 8 additions & 2 deletions plugins/bc-blog/tests/TestCase/Service/BlogPostsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,10 @@ public function testGetTitlesById()
public function testBatch()
{
// データを生成
$this->loadFixtureScenario(BlogContentScenario::class, 5, 1, null, 'news1', '/news/');
BlogContentFactory::make([
'id' => 5,
'eye_catch_size' => 'YTo0OntzOjExOiJ0aHVtYl93aWR0aCI7czozOiIzMDAiO3M6MTI6InRodW1iX2hlaWdodCI7czozOiIzMDAiO3M6MTg6Im1vYmlsZV90aHVtYl93aWR0aCI7czozOiIxMDAiO3M6MTk6Im1vYmlsZV90aHVtYl9oZWlnaHQiO3M6MzoiMTAwIjt9',
])->persist();
BlogPostFactory::make(['id' => '1', 'blog_content_id' => '5', 'title' => 'test blog post batch'])->persist();
BlogPostFactory::make(['id' => '2', 'blog_content_id' => '5', 'title' => 'test blog post batch'])->persist();
BlogPostFactory::make(['id' => '3', 'blog_content_id' => '5', 'title' => 'test blog post batch'])->persist();
Expand All @@ -914,10 +917,13 @@ public function testBatch()
// 戻り値を確認
$this->assertTrue($result);

// 存在しない処理を指定した場合は false を返すこと
$this->assertFalse($this->BlogPostsService->batch('test', [1, 2, 3]));

// 存在しない id を指定された場合は例外が発生すること
// サービスメソッドを呼ぶ
$this->expectException('Cake\Datasource\Exception\RecordNotFoundException');
$result = $this->BlogPostsService->batch('delete', [1, 2, 3]);
$this->BlogPostsService->batch('delete', [1, 2, 3]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,10 @@ public function test_batch()
// 戻り値を確認
$this->assertTrue($result);

// 存在メソッドの場合は false を返すこと
$this->assertFalse($this->BlogTagsService->batch('test', [1, 2, 3]));

// 存在しない id を指定された場合は例外が発生すること
// サービスメソッドを呼ぶ
$this->expectException('Cake\Datasource\Exception\RecordNotFoundException');
$this->BlogTagsService->batch('delete', [1, 2, 3]);
}
Expand Down

0 comments on commit d823fd5

Please sign in to comment.