Skip to content

Commit 27fec60

Browse files
authored
Merge pull request #1308 from EC-CUBE/fix/unit-test-transaction-autocommit
Fix: ランダムに失敗するユニットテストを修正(autocommit対策)
2 parents 6ecc3de + a796496 commit 27fec60

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

tests/class/SC_CartSession/SC_CartSessionTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,34 @@ class SC_CartSessionTest extends Common_TestCase
77
*/
88
protected $objCartSession;
99

10+
/**
11+
* @var array テスト前のデータバックアップ
12+
*/
13+
protected $backupData = [];
14+
1015
protected function setUp(): void
1116
{
1217
parent::setUp();
1318
$this->objCartSession = new SC_CartSession_Ex();
19+
20+
// テストで変更される可能性のあるテーブルをバックアップ
21+
$this->backupData['products'] = $this->objQuery->getAll('SELECT * FROM dtb_products');
22+
$this->backupData['products_class'] = $this->objQuery->getAll('SELECT * FROM dtb_products_class');
23+
$this->backupData['deliv'] = $this->objQuery->getAll('SELECT * FROM dtb_deliv');
24+
$this->backupData['baseinfo'] = $this->objQuery->getAll('SELECT * FROM dtb_baseinfo');
1425
}
1526

1627
protected function tearDown(): void
1728
{
29+
// バックアップからデータを復元
30+
foreach (['products', 'products_class', 'deliv', 'baseinfo'] as $table) {
31+
$tableName = 'dtb_'.$table;
32+
$this->objQuery->delete($tableName);
33+
foreach ($this->backupData[$table] as $row) {
34+
$this->objQuery->insert($tableName, $row);
35+
}
36+
}
37+
1838
parent::tearDown();
1939
$objDb = new SC_Helper_DB_Ex();
2040
$objDb->sfGetBasisData(true);

tests/class/helper/SC_Helper_TaxRule/SC_Helper_TaxRule_TestBase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,25 @@ abstract class SC_Helper_TaxRule_TestBase extends Common_TestCase
3737
*/
3838
protected $objTaxRule;
3939

40+
/**
41+
* @var array バックアップ用の元の税ルールデータ
42+
*/
43+
protected $originalTaxRules = [];
44+
4045
protected function setUp(): void
4146
{
4247
parent::setUp();
48+
// 現在の税ルールをバックアップ
49+
$this->originalTaxRules = $this->objQuery->getAll('SELECT * FROM dtb_tax_rule');
4350
}
4451

4552
protected function tearDown(): void
4653
{
54+
// 税ルールを元に戻す
55+
$this->objQuery->delete('dtb_tax_rule');
56+
foreach ($this->originalTaxRules as $rule) {
57+
$this->objQuery->insert('dtb_tax_rule', $rule);
58+
}
4759
parent::tearDown();
4860
}
4961

0 commit comments

Comments
 (0)