Skip to content

Commit ac543cf

Browse files
henderkeschalasr
authored andcommitted
[Console] don't discard existing aliases when constructing Command
1 parent 0bc0f45 commit ac543cf

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Command/Command.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ public function __construct(?string $name = null, ?callable $code = null)
120120
$name = array_shift($aliases);
121121
}
122122

123+
// we must not overwrite existing aliases, combine new ones with existing ones
124+
$aliases = array_unique([
125+
...$this->aliases,
126+
...$aliases,
127+
]);
128+
123129
$this->setAliases($aliases);
124130
}
125131

Tests/Command/CommandTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,25 @@ public function testGetProcessedHelp()
199199
public function testGetSetAliases()
200200
{
201201
$command = new \TestCommand();
202-
$this->assertEquals(['name'], $command->getAliases(), '->getAliases() returns the aliases');
203202
$ret = $command->setAliases(['name1']);
204203
$this->assertEquals($command, $ret, '->setAliases() implements a fluent interface');
205204
$this->assertEquals(['name1'], $command->getAliases(), '->setAliases() sets the aliases');
206205
}
207206

207+
public function testAliasesSetBeforeParentConstructorArePreserved()
208+
{
209+
$command = new class extends Command {
210+
public function __construct()
211+
{
212+
// set aliases before calling parent constructor
213+
$this->setAliases(['existingalias']);
214+
parent::__construct('foo|newalias');
215+
}
216+
};
217+
218+
$this->assertSame(['existingalias', 'newalias'], $command->getAliases(), 'Aliases set before parent::__construct() must be preserved.');
219+
}
220+
208221
#[TestWith(['name|alias1|alias2', 'name', ['alias1', 'alias2'], false])]
209222
#[TestWith(['|alias1|alias2', 'alias1', ['alias2'], true])]
210223
public function testSetAliasesAndHiddenViaName(string $name, string $expectedName, array $expectedAliases, bool $expectedHidden)

0 commit comments

Comments
 (0)