Skip to content

Commit 7bfe6fa

Browse files
committed
test: fix Commands random-order failures
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent 6c4acef commit 7bfe6fa

7 files changed

Lines changed: 38 additions & 9 deletions

File tree

.github/scripts/random-tests-config.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ AutoReview
1313
Autoloader
1414
# Cache
1515
CLI
16-
# Commands
16+
Commands
1717
# Config
1818
Cookie
1919
# DataCaster

system/Commands/Database/ShowTableInfo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ private function makeTbodyForShowAllTables(array $tables): array
243243
{
244244
$this->removeDBPrefix();
245245

246-
foreach ($tables as $id => $tableName) {
246+
foreach (array_values($tables) as $id => $tableName) {
247247
$table = $this->db->protectIdentifiers($tableName);
248248
$db = $this->db->query("SELECT * FROM {$table}");
249249

tests/_support/Commands/Unsuffixable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,14 @@ public function run(array $params): void
7676
$this->setEnabledSuffixing(false);
7777
$this->generateClass($params);
7878
}
79+
80+
protected function prepare(string $class): string
81+
{
82+
return $this->parseTemplate(
83+
$class,
84+
['{group}', '{command}'],
85+
['Generators', 'make:foo'],
86+
['type' => 'basic'],
87+
);
88+
}
7989
}

tests/system/Commands/CreateDatabaseTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final class CreateDatabaseTest extends CIUnitTestCase
3535

3636
protected function setUp(): void
3737
{
38-
$this->connection = Database::connect();
38+
$this->connection = Database::connect(null, false);
3939

4040
parent::setUp();
4141

@@ -54,12 +54,31 @@ private function dropDatabase(): void
5454
if ($this->connection instanceof SQLite3Connection) {
5555
$file = WRITEPATH . 'database.db';
5656

57+
$this->closeDatabaseConnections();
58+
5759
if (is_file($file)) {
5860
unlink($file);
5961
}
60-
} elseif (Database::utils('tests')->databaseExists('database')) {
62+
63+
return;
64+
}
65+
66+
if (Database::utils('tests')->databaseExists('database')) {
6167
Database::forge()->dropDatabase('database');
6268
}
69+
70+
$this->closeDatabaseConnections();
71+
}
72+
73+
private function closeDatabaseConnections(): void
74+
{
75+
$this->connection->close();
76+
77+
foreach (Database::getConnections() as $connection) {
78+
$connection->close();
79+
}
80+
81+
$this->setPrivateProperty(Database::class, 'instances', []);
6382
}
6483

6584
protected function getBuffer(): string

tests/system/Commands/Database/ShowTableInfoTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use CodeIgniter\Test\CIUnitTestCase;
1818
use CodeIgniter\Test\DatabaseTestTrait;
1919
use CodeIgniter\Test\StreamFilterTrait;
20-
use Config\Database;
2120
use PHPUnit\Framework\Attributes\Group;
2221
use Tests\Support\Database\Seeds\CITestSeeder;
2322

@@ -30,7 +29,7 @@ final class ShowTableInfoTest extends CIUnitTestCase
3029
use DatabaseTestTrait;
3130
use StreamFilterTrait;
3231

33-
protected $migrateOnce = true;
32+
protected $seed = CITestSeeder::class;
3433

3534
protected function setUp(): void
3635
{
@@ -121,9 +120,6 @@ public function testDbTableMetadata(): void
121120

122121
public function testDbTableDesc(): void
123122
{
124-
$seeder = Database::seeder();
125-
$seeder->call(CITestSeeder::class);
126-
127123
command('db:table db_user --desc');
128124

129125
$result = $this->getNormalizedResult();

tests/system/Commands/DatabaseCommandsTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace CodeIgniter\Commands;
1515

1616
use CodeIgniter\Test\CIUnitTestCase;
17+
use CodeIgniter\Test\DatabaseTestTrait;
1718
use CodeIgniter\Test\StreamFilterTrait;
1819
use PHPUnit\Framework\Attributes\Group;
1920

@@ -25,6 +26,7 @@
2526
#[Group('DatabaseLive')]
2627
final class DatabaseCommandsTest extends CIUnitTestCase
2728
{
29+
use DatabaseTestTrait;
2830
use StreamFilterTrait;
2931

3032
protected function tearDown(): void

tests/system/Commands/MigrationIntegrationTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace CodeIgniter\Commands;
1515

1616
use CodeIgniter\Test\CIUnitTestCase;
17+
use CodeIgniter\Test\DatabaseTestTrait;
1718
use CodeIgniter\Test\StreamFilterTrait;
1819
use PHPUnit\Framework\Attributes\Group;
1920

@@ -25,6 +26,7 @@
2526
#[Group('DatabaseLive')]
2627
final class MigrationIntegrationTest extends CIUnitTestCase
2728
{
29+
use DatabaseTestTrait;
2830
use StreamFilterTrait;
2931

3032
private string $migrationFileFrom = SUPPORTPATH . 'Database/Migrations/20160428212500_Create_test_tables.php';

0 commit comments

Comments
 (0)