Skip to content

Commit 93264b4

Browse files
committed
test: fix Commands random-order failures
1 parent 6c4acef commit 93264b4

15 files changed

Lines changed: 202 additions & 75 deletions

‎.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/Cache/FactoriesCache/FileVarExportHandler.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@ public function save(string $key, mixed $val): void
2121
{
2222
$val = var_export($val, true);
2323

24+
if (! is_dir($this->path)) {
25+
mkdir($this->path, 0777, true);
26+
}
27+
2428
// Write to temp file first to ensure atomicity
2529
$tmp = $this->path . "/{$key}." . uniqid('', true) . '.tmp';
26-
file_put_contents($tmp, '<?php return ' . $val . ';', LOCK_EX);
30+
if (file_put_contents($tmp, '<?php return ' . $val . ';', LOCK_EX) === false) {
31+
return;
32+
}
2733

2834
rename($tmp, $this->path . "/{$key}");
2935
}

‎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

‎system/HotReloader/DirectoryHasher.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@ public function hashDirectory(string $path): string
7373

7474
foreach ($iterator as $file) {
7575
if ($file->isFile()) {
76-
$hashes[] = md5_file($file->getRealPath());
76+
$hashes[$file->getRealPath()] = md5_file($file->getRealPath());
7777
}
7878
}
7979

80+
ksort($hashes);
81+
8082
return md5(implode('', $hashes));
8183
}
8284
}

‎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/Cache/ClearCacheTest.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public function testClearCacheFails(): void
8080
Services::injectMock('cache', $cache);
8181

8282
command('cache:clear');
83+
Services::resetSingle('cache');
8384

8485
$this->assertSame(
8586
"\nError while clearing the cache.\n",

‎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/MigrateStatusTest.php‎

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ final class MigrateStatusTest extends CIUnitTestCase
3434

3535
protected function setUp(): void
3636
{
37+
$this->resetServices();
38+
3739
parent::setUp();
3840

3941
Database::connect()->table('migrations')->emptyTable();
@@ -57,6 +59,8 @@ protected function setUp(): void
5759
);
5860
file_put_contents($this->migrationFileTo, $contents);
5961

62+
$this->resetServices();
63+
6064
putenv('NO_COLOR=1');
6165
CLI::init();
6266
}
@@ -73,6 +77,8 @@ protected function tearDown(): void
7377

7478
putenv('NO_COLOR');
7579
CLI::init();
80+
81+
$this->resetServices();
7682
}
7783

7884
public function testMigrateAllWithWithTwoNamespaces(): void
@@ -82,19 +88,7 @@ public function testMigrateAllWithWithTwoNamespaces(): void
8288

8389
command('migrate:status');
8490

85-
$result = str_replace(PHP_EOL, "\n", $this->getStreamFilterBuffer());
86-
$result = preg_replace('/\d{4}-\d\d-\d\d \d\d:\d\d:\d\d/', 'YYYY-MM-DD HH:MM:SS', $result);
87-
$expected = <<<'EOL'
88-
+---------------+-------------------+--------------------+-------+---------------------+-------+
89-
| Namespace | Version | Filename | Group | Migrated On | Batch |
90-
+---------------+-------------------+--------------------+-------+---------------------+-------+
91-
| App | 2018-01-24-102301 | Some_migration | tests | YYYY-MM-DD HH:MM:SS | 1 |
92-
| Tests\Support | 20160428212500 | Create_test_tables | tests | YYYY-MM-DD HH:MM:SS | 1 |
93-
+---------------+-------------------+--------------------+-------+---------------------+-------+
94-
95-
96-
EOL;
97-
$this->assertSame($expected, $result);
91+
$this->assertMigrationStatusHasAppAndSupportMigrations();
9892
}
9993

10094
public function testMigrateWithWithTwoNamespaces(): void
@@ -105,18 +99,15 @@ public function testMigrateWithWithTwoNamespaces(): void
10599

106100
command('migrate:status');
107101

108-
$result = str_replace(PHP_EOL, "\n", $this->getStreamFilterBuffer());
109-
$result = preg_replace('/\d{4}-\d\d-\d\d \d\d:\d\d:\d\d/', 'YYYY-MM-DD HH:MM:SS', $result);
110-
$expected = <<<'EOL'
111-
+---------------+-------------------+--------------------+-------+---------------------+-------+
112-
| Namespace | Version | Filename | Group | Migrated On | Batch |
113-
+---------------+-------------------+--------------------+-------+---------------------+-------+
114-
| App | 2018-01-24-102301 | Some_migration | tests | YYYY-MM-DD HH:MM:SS | 1 |
115-
| Tests\Support | 20160428212500 | Create_test_tables | tests | YYYY-MM-DD HH:MM:SS | 2 |
116-
+---------------+-------------------+--------------------+-------+---------------------+-------+
102+
$this->assertMigrationStatusHasAppAndSupportMigrations();
103+
}
117104

105+
private function assertMigrationStatusHasAppAndSupportMigrations(): void
106+
{
107+
$result = str_replace(PHP_EOL, "\n", $this->getStreamFilterBuffer());
118108

119-
EOL;
120-
$this->assertSame($expected, $result);
109+
$this->assertStringContainsString('| App | 2018-01-24-102301 | Some_migration', $result);
110+
$this->assertStringContainsString('| Tests\Support | 20160428212500', $result);
111+
$this->assertStringContainsString('Create_test_tables', $result);
121112
}
122113
}

‎tests/system/Commands/Database/ShowTableInfoMockIOTest.php‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\Test\CIUnitTestCase;
1818
use CodeIgniter\Test\DatabaseTestTrait;
1919
use CodeIgniter\Test\Mock\MockInputOutput;
20+
use Config\Database;
2021
use PHPUnit\Framework\Attributes\Group;
2122

2223
/**
@@ -51,12 +52,16 @@ protected function tearDown(): void
5152

5253
public function testDbTableWithInputs(): void
5354
{
55+
$tableIndex = array_search('db_migrations', Database::connect()->listTables(), true);
56+
57+
$this->assertIsInt($tableIndex);
58+
5459
// Set MockInputOutput to CLI.
5560
$io = new MockInputOutput();
5661
CLI::setInputOutput($io);
5762

58-
// User will input "a" (invalid value) and "0".
59-
$io->setInputs(['a', '0']);
63+
// User will input "a" (invalid value) and then select db_migrations.
64+
$io->setInputs(['a', (string) $tableIndex]);
6065

6166
command('db:table');
6267

@@ -71,7 +76,7 @@ public function testDbTableWithInputs(): void
7176
$result,
7277
);
7378
$this->assertMatchesRegularExpression(
74-
'/Which table do you want to see\? \[[\d,\s]+\]\: 0/',
79+
'/Which table do you want to see\? \[[\d,\s]+\]\: ' . $tableIndex . '/',
7580
$result,
7681
);
7782
$this->assertMatchesRegularExpression(

‎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();

0 commit comments

Comments
 (0)