Skip to content
Open
Show file tree
Hide file tree
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
25 changes: 9 additions & 16 deletions system/CLI/Commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ public function run(string $command, array $params)
return EXIT_ERROR;
}

// The file would have already been loaded during the
// createCommandList function...
$className = $this->commands[$command]['class'];
$class = new $className($this->logger, $this);

Expand Down Expand Up @@ -104,14 +102,10 @@ public function discoverCommands()
$locator = service('locator');
$files = $locator->listFiles('Commands/');

// If no matching command files were found, bail
// This should never happen in unit testing.
if ($files === []) {
return; // @codeCoverageIgnore
return;
}

// Loop over each file checking to see if a command with that
// alias exists in the class.
foreach ($files as $file) {
/** @var class-string<BaseCommand>|false */
$className = $locator->findQualifiedNameFromPath($file);
Expand Down Expand Up @@ -159,21 +153,20 @@ public function verifyCommand(string $command, array $commands): bool
return true;
}

$message = lang('CLI.commandNotFound', [$command]);
$message = lang('CLI.commandNotFound', [$command]);

$alternatives = $this->getCommandAlternatives($command, $commands);

if ($alternatives !== []) {
if (count($alternatives) === 1) {
$message .= "\n\n" . lang('CLI.altCommandSingular') . "\n ";
} else {
$message .= "\n\n" . lang('CLI.altCommandPlural') . "\n ";
}

$message .= implode("\n ", $alternatives);
$message = sprintf(
"%s\n\n%s\n %s",
$message,
count($alternatives) === 1 ? lang('CLI.altCommandSingular') : lang('CLI.altCommandPlural'),
implode("\n ", $alternatives),
);
}

CLI::error($message);
CLI::newLine();

return false;
}
Expand Down
25 changes: 18 additions & 7 deletions tests/_support/Commands/AppInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,40 @@
use CodeIgniter\CodeIgniter;
use CodeIgniter\Exceptions\RuntimeException;

class AppInfo extends BaseCommand
/**
* @internal
*/
final class AppInfo extends BaseCommand
{
protected $group = 'demo';
protected $name = 'app:info';
protected $arguments = ['draft' => 'unused'];
protected $description = 'Displays basic application information.';

public function run(array $params): void
public function run(array $params): int
{
CLI::write('CI Version: ' . CLI::color(CodeIgniter::CI_VERSION, 'red'));
CLI::write(sprintf('CodeIgniter Version: %s', CodeIgniter::CI_VERSION));

return 0;
}

public function bomb(): void
public function bomb(): int
{
try {
CLI::color('test', 'white', 'Background');
} catch (RuntimeException $oops) {
$this->showError($oops);
} catch (RuntimeException $e) {
$this->showError($e);

return 1;
}

return 0;
}

public function helpme(): void
public function helpMe(): int
{
$this->call('help');

return 0;
}
}
30 changes: 0 additions & 30 deletions tests/_support/Commands/ParamsReveal.php

This file was deleted.

16 changes: 5 additions & 11 deletions tests/_support/_command/ListCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,30 @@

namespace App\Commands;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\Commands\ListCommands as BaseListCommands;

class ListCommands extends BaseListCommands
/**
* @internal
*/
final class ListCommands extends BaseCommand
{
/**
* The group the command is lumped under
* when listing commands.
*
* @var string
*/
protected $group = 'App';

/**
* The Command's name
*
* @var string
*/
protected $name = 'list';

/**
* the Command's short description
*
* @var string
*/
protected $description = 'This is testing to override `list` command.';

/**
* the Command's usage
*
* @var string
*/
protected $usage = 'list';
Expand Down
104 changes: 104 additions & 0 deletions tests/system/CLI/BaseCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\CLI;

use CodeIgniter\CLI\Exceptions\CLIException;
use CodeIgniter\CodeIgniter;
use CodeIgniter\Log\Logger;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\StreamFilterTrait;
use PHPUnit\Framework\Attributes\After;
use PHPUnit\Framework\Attributes\Before;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use Tests\Support\Commands\AppInfo;

/**
* @internal
*/
#[CoversClass(BaseCommand::class)]
#[CoversClass(CLIException::class)]
#[Group('Others')]
final class BaseCommandTest extends CIUnitTestCase
{
use StreamFilterTrait;

#[After]
#[Before]
protected function resetCli(): void
{
CLI::reset();
}

public function testRunCommand(): void
{
$command = new AppInfo(single_service('logger'), single_service('commands'));

$this->assertSame(0, $command->run([]));
$this->assertSame(
sprintf("\nCodeIgniter Version: %s\n", CodeIgniter::CI_VERSION),
preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()),
);
}

public function testCallingOtherCommands(): void
{
$command = new AppInfo(single_service('logger'), single_service('commands'));

$this->assertSame(0, $command->helpMe());
$this->assertStringContainsString('Displays basic usage information.', $this->getStreamFilterBuffer());
}

public function testShowError(): void
{
$command = new AppInfo(single_service('logger'), single_service('commands'));

$this->assertSame(1, $command->bomb());
$this->assertStringContainsString('[CodeIgniter\CLI\Exceptions\CLIException]', $this->getStreamFilterBuffer());
$this->assertStringContainsString('Invalid "background" color: "Background".', $this->getStreamFilterBuffer());
}

public function testShowHelp(): void
{
$command = new AppInfo(single_service('logger'), single_service('commands'));
$command->showHelp();

$this->assertSame(
<<<'EOT'

Usage:
app:info [arguments]

Description:
Displays basic application information.

Arguments:
draft unused

EOT,
preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()),
);
}

public function testMagicGetAndIsset(): void
{
$command = new AppInfo(single_service('logger'), single_service('commands'));

$this->assertInstanceOf(Logger::class, $command->logger);
$this->assertInstanceOf(Commands::class, $command->commands);
$this->assertSame('demo', $command->group);
$this->assertSame('app:info', $command->name);
$this->assertNull($command->foo); // @phpstan-ignore property.notFound
}
}
Loading
Loading