Skip to content

Commit d468cfe

Browse files
author
Robin de Graaf
committed
Fix bug in getCommands()
1 parent 58b0652 commit d468cfe

4 files changed

Lines changed: 30 additions & 4 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
.DS_Store
12
.idea
23
composer.lock
34
vendor
4-
coverage
5+
coverage

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Parable Console
22

3+
## 0.6.1
4+
5+
_Changes_
6+
- Small bugfix that crept up after splitting named commands and instantiated ones.
7+
38
## 0.6.0
49

510
_Changes_

src/Application.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ public function getCommand(string $commandName): ?Command
102102
/** @var Command $command */
103103
$command = $this->container->get($this->commandNames[$commandName]);
104104
$this->addCommand($command);
105+
106+
// Since we've instantiated now, we can lose the reference to the name alone
107+
unset($this->commandNames[$commandName]);
105108
}
106109

107110
return $this->commands[$commandName];
@@ -114,17 +117,20 @@ public function getCommands(): array
114117
{
115118
$commands = [];
116119

117-
foreach ($this->commands as $commandName => $command) {
120+
foreach ($this->commandNames as $commandName => $className) {
118121
$commands[$commandName] = $this->getCommand($commandName);
119122
}
120123

121-
return array_filter($commands);
124+
return array_filter(array_merge($this->commands, $commands));
122125
}
123126

124127
public function removeCommandByName(string $commandName): void
125128
{
126129
if ($this->hasCommand($commandName)) {
127-
unset($this->commands[$commandName]);
130+
unset(
131+
$this->commands[$commandName],
132+
$this->commandNames[$commandName]
133+
);
128134
}
129135
}
130136

tests/ApplicationTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ public function testGetCommand(): void
9898
self::assertSame('OK2', ValueClass::get());
9999
}
100100

101+
public function testGetCommandsGetsBothInstantiatedAndCommandNames(): void
102+
{
103+
$application = $this->container->buildAll(Application::class);
104+
105+
$application->addCommand($this->command1);
106+
$application->addCommandByNameAndClass('test-command', TestCommand::class);
107+
108+
self::assertCount(2, $application->getCommands());
109+
110+
foreach ($application->getCommands() as $commandName => $command) {
111+
self::assertInstanceOf(Command::class, $command);
112+
}
113+
}
114+
101115
public function testAddCommand(): void
102116
{
103117
self::assertFalse($this->container->has(TestCommand::class));

0 commit comments

Comments
 (0)