Skip to content

Commit cd8f5dc

Browse files
committed
test(database): fix state leaks in MySQL random-order execution
1 parent ad8526f commit cd8f5dc

7 files changed

Lines changed: 66 additions & 26 deletions

File tree

‎tests/system/Database/Live/ConnectTest.php‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,29 @@ protected function setUp(): void
4646
$this->group2['DBDriver'] = 'Postgre';
4747
}
4848

49+
protected function tearDown(): void
50+
{
51+
parent::tearDown();
52+
$this->setPrivateProperty(Database::class, 'instances', []);
53+
}
54+
55+
4956
public function testConnectWithMultipleCustomGroups(): void
5057
{
58+
$this->group1['DBPrefix'] = uniqid('g1_', true);
59+
$this->group2['DBPrefix'] = uniqid('g2_', true);
60+
5161
// We should have our test database connection already.
5262
$instances = $this->getPrivateProperty(Database::class, 'instances');
53-
$this->assertCount(1, $instances);
63+
$initialCount = count($instances);
5464

5565
$db1 = Database::connect($this->group1);
5666
$db2 = Database::connect($this->group2);
5767

5868
$this->assertNotSame($db1, $db2);
5969

6070
$instances = $this->getPrivateProperty(Database::class, 'instances');
61-
$this->assertCount(3, $instances);
71+
$this->assertCount($initialCount + 2, $instances);
6272
}
6373

6474
public function testConnectReturnsProvidedConnection(): void

‎tests/system/Database/Live/ForgeTest.php‎

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,52 @@ final class ForgeTest extends CIUnitTestCase
3636
protected $seed = CITestSeeder::class;
3737
private Forge $forge;
3838

39+
private function dropAllMockTables(): void
40+
{
41+
$tablesToDrop = [
42+
'forge_test_invoices',
43+
'forge_test_inv',
44+
'forge_test_users',
45+
'actions',
46+
'forge_test_table',
47+
'test_exists',
48+
'forge_test_attributes',
49+
'forge_array_constraint',
50+
'forge_nullable_table',
51+
'forge_test_1',
52+
'forge_test_two',
53+
'forge_test_three',
54+
'forge_test_four',
55+
'forge_test_modify',
56+
'droptest',
57+
'key_test_users',
58+
'test_stores',
59+
'user2',
60+
'forge_test_table_dummy',
61+
];
62+
foreach ($tablesToDrop as $table) {
63+
$this->forge->dropTable($table, true);
64+
}
65+
}
66+
3967
protected function setUp(): void
4068
{
4169
$this->forge = Database::forge($this->DBGroup);
4270

43-
// when running locally if one of these tables isn't dropped it may cause error
44-
$this->forge->dropTable('forge_test_invoices', true);
45-
$this->forge->dropTable('forge_test_inv', true);
46-
$this->forge->dropTable('forge_test_users', true);
47-
$this->forge->dropTable('actions', true);
71+
$this->dropAllMockTables();
72+
73+
db_connect($this->DBGroup)->resetDataCache();
4874

4975
parent::setUp();
5076
}
5177

78+
protected function tearDown(): void
79+
{
80+
parent::tearDown();
81+
$this->dropAllMockTables();
82+
self::$doneMigration = false;
83+
}
84+
5285
public function testCreateDatabase(): void
5386
{
5487
if ($this->db->DBDriver === 'OCI8') {
@@ -75,7 +108,7 @@ public function testCreateDatabaseWithDots(): void
75108
// Checks if tableExists() works.
76109
$config = config(Database::class)->{$this->DBGroup};
77110
$config['database'] = $dbName;
78-
$db = db_connect($config);
111+
$db = db_connect($config, false);
79112
$result = $db->tableExists('not_exist');
80113

81114
$this->assertFalse($result);

‎tests/system/Database/Live/GetVersionTest.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public function testGetVersion(): void
3636
$this->db->connID = false;
3737

3838
$version = $this->db->getVersion();
39-
40-
$this->assertMatchesRegularExpression('/\A\d+(\.\d+)*\z/', $version);
39+
$this->assertMatchesRegularExpression('/\A\d+(\.\d+)*/', $version);
4140
}
4241
}

‎tests/system/Database/Live/MySQLi/FoundRowsTest.php‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function testEnableFoundRows(): void
5454
{
5555
$this->tests['foundRows'] = true;
5656

57-
$db1 = Database::connect($this->tests);
57+
$db1 = Database::connect($this->tests, false);
5858

5959
$this->assertTrue($db1->foundRows);
6060
}
@@ -63,7 +63,7 @@ public function testDisableFoundRows(): void
6363
{
6464
$this->tests['foundRows'] = false;
6565

66-
$db1 = Database::connect($this->tests);
66+
$db1 = Database::connect($this->tests, false);
6767

6868
$this->assertFalse($db1->foundRows);
6969
}
@@ -72,7 +72,7 @@ public function testAffectedRowsAfterEnableFoundRowsWithNoChange(): void
7272
{
7373
$this->tests['foundRows'] = true;
7474

75-
$db1 = Database::connect($this->tests);
75+
$db1 = Database::connect($this->tests, false);
7676

7777
$db1->table('db_user')
7878
->set('country', 'US')
@@ -88,7 +88,7 @@ public function testAffectedRowsAfterDisableFoundRowsWithNoChange(): void
8888
{
8989
$this->tests['foundRows'] = false;
9090

91-
$db1 = Database::connect($this->tests);
91+
$db1 = Database::connect($this->tests, false);
9292

9393
$db1->table('db_user')
9494
->set('country', 'US')
@@ -104,7 +104,7 @@ public function testAffectedRowsAfterEnableFoundRowsWithChange(): void
104104
{
105105
$this->tests['foundRows'] = true;
106106

107-
$db1 = Database::connect($this->tests);
107+
$db1 = Database::connect($this->tests, false);
108108

109109
$db1->table('db_user')
110110
->set('country', 'NZ')
@@ -120,7 +120,7 @@ public function testAffectedRowsAfterDisableFoundRowsWithChange(): void
120120
{
121121
$this->tests['foundRows'] = false;
122122

123-
$db1 = Database::connect($this->tests);
123+
$db1 = Database::connect($this->tests, false);
124124

125125
$db1->table('db_user')
126126
->set('country', 'NZ')
@@ -136,7 +136,7 @@ public function testAffectedRowsAfterEnableFoundRowsWithPartialChange(): void
136136
{
137137
$this->tests['foundRows'] = true;
138138

139-
$db1 = Database::connect($this->tests);
139+
$db1 = Database::connect($this->tests, false);
140140

141141
$db1->table('db_user')
142142
->set('name', 'Derek Jones')
@@ -152,7 +152,7 @@ public function testAffectedRowsAfterDisableFoundRowsWithPartialChange(): void
152152
{
153153
$this->tests['foundRows'] = false;
154154

155-
$db1 = Database::connect($this->tests);
155+
$db1 = Database::connect($this->tests, false);
156156

157157
$db1->table('db_user')
158158
->set('name', 'Derek Jones')

‎tests/system/Database/Live/MySQLi/NumberNativeTest.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function testEnableNumberNative(): void
4444
{
4545
$this->tests['numberNative'] = true;
4646

47-
$db1 = Database::connect($this->tests);
47+
$db1 = Database::connect($this->tests, false);
4848

4949
if ($db1->DBDriver !== 'MySQLi') {
5050
$this->markTestSkipped('Only MySQLi can complete this test.');
@@ -57,7 +57,7 @@ public function testDisableNumberNative(): void
5757
{
5858
$this->tests['numberNative'] = false;
5959

60-
$db1 = Database::connect($this->tests);
60+
$db1 = Database::connect($this->tests, false);
6161

6262
if ($db1->DBDriver !== 'MySQLi') {
6363
$this->markTestSkipped('Only MySQLi can complete this test.');
@@ -70,7 +70,7 @@ public function testQueryDataAfterEnableNumberNative(): void
7070
{
7171
$this->tests['numberNative'] = true;
7272

73-
$db1 = Database::connect($this->tests);
73+
$db1 = Database::connect($this->tests, false);
7474

7575
if ($db1->DBDriver !== 'MySQLi') {
7676
$this->markTestSkipped('Only MySQLi can complete this test.');
@@ -88,7 +88,7 @@ public function testQueryDataAfterDisableNumberNative(): void
8888
{
8989
$this->tests['numberNative'] = false;
9090

91-
$db1 = Database::connect($this->tests);
91+
$db1 = Database::connect($this->tests, false);
9292

9393
if ($db1->DBDriver !== 'MySQLi') {
9494
$this->markTestSkipped('Only MySQLi can complete this test.');

‎tests/system/Database/Live/WorkerModeTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ final class WorkerModeTest extends CIUnitTestCase
2727
{
2828
use DatabaseTestTrait;
2929

30+
3031
protected function tearDown(): void
3132
{
3233
parent::tearDown();
33-
3434
$this->setPrivateProperty(Config::class, 'instances', []);
3535
}
3636

‎tests/system/Database/Migrations/MigrationRunnerTest.php‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ protected function tearDown(): void
6969
{
7070
parent::tearDown();
7171

72-
// To delete data with `$this->regressDatabase()`, set it true.
73-
$this->migrate = true;
74-
$this->regressDatabase();
72+
$this->resetTables();
7573
}
7674

7775
public function testLoadsDefaultDatabaseWhenNoneSpecified(): void

0 commit comments

Comments
 (0)