Skip to content

Commit

Permalink
ユニットテストを調整
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuring committed Nov 27, 2024
1 parent afa69c2 commit a2468f6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public function tearDown(): void

/**
* Test create
*
* プレフィックスを変更するテストを行う場合、
* 他のテストで、トランザクション処理を行う際にデッドロックが発生してしまう様子。
* 原因が不明のため、一旦、プレフィックスの変更テストは行わない
*/
public function testTable()
{
Expand All @@ -79,19 +83,8 @@ public function testTable()
$adapter = $factory->getAdapter('mysql', $options);
$this->BcMigration->setAdapter($adapter);

// prefixをセットアップ
$config = ConnectionManager::getConfig('test');
ConnectionManager::drop('test');
$config['prefix'] = 'my_prefix_';
ConnectionManager::setConfig('test', $config);

// 実行
$rs = $this->BcMigration->table('test');
$this->assertEquals('my_prefix_test', $rs->getName());

// 後処理
$config['prefix'] = '';
ConnectionManager::drop('test');
ConnectionManager::setConfig('test', $config);
$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: 7 additions & 3 deletions plugins/bc-blog/tests/TestCase/Service/BlogPostsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -917,9 +917,13 @@ public function testBatch()
// 戻り値を確認
$this->assertTrue($result);

// 例外が発生
// $this->expectExceptionMessage('Call to undefined method BcBlog\Service\BlogPostsService::test()');
// $this->BlogPostsService->batch('update', [1, 2, 3]);
// 存在しない処理を指定した場合は false を返すこと
$this->assertFalse($this->BlogPostsService->batch('test', [1, 2, 3]));

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

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

// 例外が発生
// $this->expectExceptionMessage('Call to undefined method BcBlog\Service\BlogPostsService::test()');
// $this->BlogTagsService->batch('test', [1, 2, 3]);
// 存在メソッドの場合は 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 a2468f6

Please sign in to comment.