Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: precedence of command classes with the same $name #8898

Merged
merged 2 commits into from
Jun 24, 2024
Merged
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
2 changes: 1 addition & 1 deletion system/CLI/Commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
59 changes: 59 additions & 0 deletions tests/_support/_command/ListCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

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

namespace App\Commands;
kenjis marked this conversation as resolved.
Show resolved Hide resolved

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;
}
}
64 changes: 64 additions & 0 deletions tests/system/Commands/CommandOverrideTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* 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');
}
}
Loading