Skip to content

Commit 21c8e42

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

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

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/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)) {

tests/system/Commands/MigrationIntegrationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ final class MigrationIntegrationTest extends CIUnitTestCase
2929
use DatabaseTestTrait;
3030
use StreamFilterTrait;
3131

32+
protected $migrateOnce = false;
3233
private string $migrationFileFrom = SUPPORTPATH . 'Database/Migrations/20160428212500_Create_test_tables.php';
3334
private string $migrationFileTo = APPPATH . 'Database/Migrations/20160428212500_Create_test_tables.php';
3435

0 commit comments

Comments
 (0)