diff --git a/system/CLI/Commands.php b/system/CLI/Commands.php index 30bd2c2652cd..2115714106bc 100644 --- a/system/CLI/Commands.php +++ b/system/CLI/Commands.php @@ -125,7 +125,7 @@ public function discoverCommands() /** @var BaseCommand $class */ $class = new $className($this->logger, $this); - if (isset($class->group)) { + if (isset($class->group) && ! isset($this->commands[$class->name])) { $this->commands[$class->name] = [ 'class' => $className, 'file' => $file, diff --git a/tests/_support/_command/ListCommands.php b/tests/_support/_command/ListCommands.php new file mode 100644 index 000000000000..fe1c9611a222 --- /dev/null +++ b/tests/_support/_command/ListCommands.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace App\Commands; + +use CodeIgniter\CLI\CLI; +use CodeIgniter\Commands\ListCommands as BaseListCommands; + +class ListCommands extends BaseListCommands +{ + /** + * 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'; + + /** + * Displays the help for the spark cli script itself. + */ + public function run(array $params) + { + CLI::write('This is ' . self::class); + + return EXIT_SUCCESS; + } +} diff --git a/tests/system/Commands/CommandOverrideTest.php b/tests/system/Commands/CommandOverrideTest.php new file mode 100644 index 000000000000..b21b7e0c6faa --- /dev/null +++ b/tests/system/Commands/CommandOverrideTest.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Commands; + +use CodeIgniter\Test\CIUnitTestCase; +use CodeIgniter\Test\StreamFilterTrait; +use PHPUnit\Framework\Attributes\Group; + +/** + * @internal + */ +#[Group('Others')] +final class CommandOverrideTest extends CIUnitTestCase +{ + use StreamFilterTrait; + + protected function setUp(): void + { + $this->resetServices(); + + parent::setUp(); + } + + protected function getBuffer(): string + { + return $this->getStreamFilterBuffer(); + } + + public function testOverrideListCommands(): void + { + $this->copyListCommands(); + + command('list'); + + $this->assertStringContainsString('This is App\Commands\ListCommands', $this->getBuffer()); + $this->assertStringNotContainsString('Displays basic usage information.', $this->getBuffer()); + + $this->deleteListCommands(); + } + + private function copyListCommands(): void + { + if (! is_dir(APPPATH . 'Commands')) { + mkdir(APPPATH . 'Commands'); + } + copy(SUPPORTPATH . '_command/ListCommands.php', APPPATH . 'Commands/ListCommands.php'); + } + + private function deleteListCommands(): void + { + unlink(APPPATH . 'Commands/ListCommands.php'); + } +}