Skip to content

Commit 7f60241

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

11 files changed

Lines changed: 111 additions & 44 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/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();

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/Generators/ScaffoldGeneratorTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,33 @@ protected function setUp(): void
3535
parent::setUp();
3636
}
3737

38+
protected function tearDown(): void
39+
{
40+
parent::tearDown();
41+
42+
$this->removeGeneratedFiles();
43+
}
44+
45+
private function removeGeneratedFiles(): void
46+
{
47+
preg_match_all('/File (?:created|overwritten): (APPPATH[^\r\n]+)/', $this->getStreamFilterBuffer(), $matches);
48+
49+
foreach ($matches[1] as $file) {
50+
$path = str_replace('APPPATH' . DIRECTORY_SEPARATOR, APPPATH, $file);
51+
52+
if (is_file($path)) {
53+
@unlink($path);
54+
}
55+
56+
$dir = dirname($path);
57+
$dirFiles = is_dir($dir) ? scandir($dir) : false;
58+
59+
if (str_starts_with($dir, APPPATH) && $dirFiles !== false && count($dirFiles) === 2) {
60+
@rmdir($dir);
61+
}
62+
}
63+
}
64+
3865
protected function getFileContents(string $filepath): string
3966
{
4067
if (! is_file($filepath)) {

0 commit comments

Comments
 (0)