Skip to content

Commit 402f0e7

Browse files
authored
fix(console): render nullable enum arguments (#1711)
1 parent 82318a6 commit 402f0e7

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed

packages/console/src/Actions/RenderConsoleCommand.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,7 @@ private function renderArgument(ConsoleArgumentDefinition $argument): string
6363
return $formattedArgumentName->wrap('<style="fg-gray dim"><</style>', '<style="fg-gray dim">></style>')->toString();
6464
}
6565

66-
$defaultValue = match (true) {
67-
$argument->default === true => 'true',
68-
$argument->default === false => 'false',
69-
is_null($argument->default) => 'null',
70-
is_array($argument->default) => 'array',
71-
default => "{$argument->default}",
72-
};
66+
$defaultValue = $this->getArgumentDefaultValue($argument);
7367

7468
return str()
7569
->append(str('[')->wrap('<style="fg-gray dim">', '</style>'))
@@ -89,11 +83,24 @@ private function renderEnumArgument(ConsoleArgumentDefinition $argument): string
8983

9084
$partsAsString = ' {<style="fg-blue">' . implode('|', $parts) . '</style>}';
9185
$line = "<style=\"fg-blue\">{$argument->name}</style>";
86+
$defaultValue = $this->getArgumentDefaultValue($argument);
9287

9388
if ($argument->hasDefault) {
94-
return "[{$line}={$argument->default->value}{$partsAsString}]";
89+
return "[{$line}={$defaultValue}{$partsAsString}]";
9590
}
9691

9792
return "<{$line}{$partsAsString}>";
9893
}
94+
95+
private function getArgumentDefaultValue(ConsoleArgumentDefinition $argument): string
96+
{
97+
return match (true) {
98+
$argument->default === true => 'true',
99+
$argument->default === false => 'false',
100+
is_null($argument->default) => 'null',
101+
is_array($argument->default) => 'array',
102+
$argument->default instanceof BackedEnum => $argument->default->value,
103+
default => "{$argument->default}",
104+
};
105+
}
99106
}

tests/Integration/Console/Fixtures/MyConsole.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ public function handle(
1313
string $path,
1414
TestStringEnum $type,
1515
TestStringEnum $fallback = TestStringEnum::A,
16+
?TestStringEnum $nullableEnum = null,
1617
int $times = 1,
1718
bool $force = false,
19+
?string $optional = null,
1820
): void {
1921
}
2022
}

tests/Integration/Console/RenderConsoleCommandTest.php

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,61 @@
2323
*/
2424
final class RenderConsoleCommandTest extends FrameworkIntegrationTestCase
2525
{
26-
public function test_render(): void
27-
{
28-
$handler = new MethodReflector(new ReflectionMethod(new MyConsole(), 'handle'));
26+
private GenericConsole $testConsole;
2927

30-
$consoleCommand = $handler->getAttributes(ConsoleCommand::class)[0];
28+
private MemoryOutputBuffer $consoleOutput;
3129

32-
$consoleCommand->setHandler($handler);
30+
protected function setUp(): void
31+
{
32+
parent::setUp();
3333

34-
$output = new MemoryOutputBuffer();
34+
$this->consoleOutput = new MemoryOutputBuffer();
3535

3636
$highlighter = new Highlighter(new TextTerminalTheme());
3737

38-
$console = new GenericConsole(
39-
output: $output,
38+
$this->testConsole = new GenericConsole(
39+
output: $this->consoleOutput,
4040
input: new UnsupportedInputBuffer(),
4141
highlighter: $highlighter,
4242
executeConsoleCommand: $this->container->get(ExecuteConsoleCommand::class),
4343
argumentBag: $this->container->get(ConsoleArgumentBag::class),
4444
);
45+
}
46+
47+
public function test_render(): void
48+
{
49+
$handler = new MethodReflector(new ReflectionMethod(new MyConsole(), 'handle'));
50+
51+
$consoleCommand = $handler->getAttributes(ConsoleCommand::class)[0];
52+
53+
$consoleCommand->setHandler($handler);
4554

46-
(new RenderConsoleCommand($console))($consoleCommand);
55+
(new RenderConsoleCommand($this->testConsole))($consoleCommand);
4756

4857
$this->assertSame(
4958
'test description',
50-
trim($output->getBufferWithoutFormatting()[0]),
59+
trim($this->consoleOutput->getBufferWithoutFormatting()[0]),
60+
);
61+
}
62+
63+
public function test_render_arguments(): void
64+
{
65+
$handler = new MethodReflector(new ReflectionMethod(new MyConsole(), 'handle'));
66+
67+
$consoleCommand = $handler->getAttributes(ConsoleCommand::class)[0];
68+
69+
$consoleCommand->setHandler($handler);
70+
71+
$renderConsoleCommand = new RenderConsoleCommand(
72+
console: $this->testConsole,
73+
renderArguments: true,
74+
);
75+
76+
$renderConsoleCommand($consoleCommand);
77+
78+
$this->assertSame(
79+
'test <path> <type {a|b|c}> [fallback=a {a|b|c}] [nullable-enum=null {a|b|c}] [times=1] [--force=false] [optional=null] description',
80+
trim($this->consoleOutput->getBufferWithoutFormatting()[0]),
5181
);
5282
}
5383
}

0 commit comments

Comments
 (0)