Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions tests/system/CLI/CommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

namespace CodeIgniter\CLI;

use App\Commands\ListCommands as AppListCommands;
use CodeIgniter\Autoloader\FileLocatorInterface;
use CodeIgniter\Commands\ListCommands;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\StreamFilterTrait;
use Config\Services;
Expand Down Expand Up @@ -42,22 +44,6 @@ protected function resetAll(): void
CLI::reset();
}

private function copyAppListCommands(): void
{
if (! is_dir(APPPATH . 'Commands')) {
mkdir(APPPATH . 'Commands');
}

copy(SUPPORTPATH . '_command/ListCommands.php', APPPATH . 'Commands/ListCommands.php');
}

private function deleteAppListCommands(): void
{
if (is_file(APPPATH . 'Commands/ListCommands.php')) {
unlink(APPPATH . 'Commands/ListCommands.php');
}
}

public function testRunOnUnknownCommand(): void
{
$commands = new Commands();
Expand Down Expand Up @@ -166,13 +152,22 @@ public function testDiscoverCommandsWithNoFiles(): void

public function testDiscoveredCommandsCanBeOverridden(): void
{
$this->copyAppListCommands();
// The fixture sits outside any PSR-4 root, so the autoloader cannot load it.
require_once SUPPORTPATH . '_command/ListCommands.php';

$files = [
SUPPORTPATH . '_command/ListCommands.php' => AppListCommands::class,
SYSTEMPATH . 'Commands/ListCommands.php' => ListCommands::class,
];

$locator = $this->createMock(FileLocatorInterface::class);
$locator->method('listFiles')->with('Commands/')->willReturn(array_keys($files));
$locator->method('findQualifiedNameFromPath')->willReturnCallback(static fn (string $file): string => $files[$file]);
Services::injectMock('locator', $locator);

command('list');
(new Commands())->run('list', []);

$this->assertStringContainsString('This is App\Commands\ListCommands', $this->getStreamFilterBuffer());
$this->assertStringNotContainsString('Displays basic usage information.', $this->getStreamFilterBuffer());

$this->deleteAppListCommands();
}
}
Loading