Skip to content

Commit 744e717

Browse files
authored
test: keep CommandsTest out of app/Commands so parallel runs do not race (#10564)
1 parent 605624d commit 744e717

1 file changed

Lines changed: 15 additions & 20 deletions

File tree

‎tests/system/CLI/CommandsTest.php‎

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
namespace CodeIgniter\CLI;
1515

16+
use App\Commands\ListCommands as AppListCommands;
1617
use CodeIgniter\Autoloader\FileLocatorInterface;
18+
use CodeIgniter\Commands\ListCommands;
1719
use CodeIgniter\Test\CIUnitTestCase;
1820
use CodeIgniter\Test\StreamFilterTrait;
1921
use Config\Services;
@@ -42,22 +44,6 @@ protected function resetAll(): void
4244
CLI::reset();
4345
}
4446

45-
private function copyAppListCommands(): void
46-
{
47-
if (! is_dir(APPPATH . 'Commands')) {
48-
mkdir(APPPATH . 'Commands');
49-
}
50-
51-
copy(SUPPORTPATH . '_command/ListCommands.php', APPPATH . 'Commands/ListCommands.php');
52-
}
53-
54-
private function deleteAppListCommands(): void
55-
{
56-
if (is_file(APPPATH . 'Commands/ListCommands.php')) {
57-
unlink(APPPATH . 'Commands/ListCommands.php');
58-
}
59-
}
60-
6147
public function testRunOnUnknownCommand(): void
6248
{
6349
$commands = new Commands();
@@ -166,13 +152,22 @@ public function testDiscoverCommandsWithNoFiles(): void
166152

167153
public function testDiscoveredCommandsCanBeOverridden(): void
168154
{
169-
$this->copyAppListCommands();
155+
// The fixture sits outside any PSR-4 root, so the autoloader cannot load it.
156+
require_once SUPPORTPATH . '_command/ListCommands.php';
157+
158+
$files = [
159+
SUPPORTPATH . '_command/ListCommands.php' => AppListCommands::class,
160+
SYSTEMPATH . 'Commands/ListCommands.php' => ListCommands::class,
161+
];
162+
163+
$locator = $this->createMock(FileLocatorInterface::class);
164+
$locator->method('listFiles')->with('Commands/')->willReturn(array_keys($files));
165+
$locator->method('findQualifiedNameFromPath')->willReturnCallback(static fn (string $file): string => $files[$file]);
166+
Services::injectMock('locator', $locator);
170167

171-
command('list');
168+
(new Commands())->run('list', []);
172169

173170
$this->assertStringContainsString('This is App\Commands\ListCommands', $this->getStreamFilterBuffer());
174171
$this->assertStringNotContainsString('Displays basic usage information.', $this->getStreamFilterBuffer());
175-
176-
$this->deleteAppListCommands();
177172
}
178173
}

0 commit comments

Comments
 (0)