From 5c65d8afa5d82b6c4efae316bebae73eb462ee9d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 29 Oct 2023 14:42:23 +0100 Subject: [PATCH] Extract a shared Command method for asking for a string --- .../src/Console/Commands/MakePageCommand.php | 7 +--- .../src/Console/Commands/MakePostCommand.php | 5 --- .../src/Console/Concerns/Command.php | 9 ++++ .../framework/tests/Feature/CommandTest.php | 41 +++++++++++++++++++ 4 files changed, 51 insertions(+), 11 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePageCommand.php b/packages/framework/src/Console/Commands/MakePageCommand.php index d5edac94a07..e3f47510838 100644 --- a/packages/framework/src/Console/Commands/MakePageCommand.php +++ b/packages/framework/src/Console/Commands/MakePageCommand.php @@ -9,7 +9,7 @@ use Hyde\Pages\BladePage; use Hyde\Pages\DocumentationPage; use Hyde\Pages\MarkdownPage; -use LaravelZero\Framework\Commands\Command; +use Hyde\Console\Concerns\Command; use function strtolower; use function ucfirst; @@ -116,9 +116,4 @@ protected function getTypeOption(): ?string return null; } - - protected function askForString(string $question, ?string $default = null): ?string - { - return $this->ask($question, $default); - } } diff --git a/packages/framework/src/Console/Commands/MakePostCommand.php b/packages/framework/src/Console/Commands/MakePostCommand.php index 7346949c415..70137bbeba0 100644 --- a/packages/framework/src/Console/Commands/MakePostCommand.php +++ b/packages/framework/src/Console/Commands/MakePostCommand.php @@ -99,9 +99,4 @@ protected function createPostFile(CreatesNewMarkdownPostFile $creator): int return (int) $exception->getCode(); } } - - protected function askForString(string $question, ?string $default = null): ?string - { - return is_string($answer = $this->output->ask($question, $default)) ? $answer : null; - } } diff --git a/packages/framework/src/Console/Concerns/Command.php b/packages/framework/src/Console/Concerns/Command.php index 0eff7580dad..00274510c1b 100644 --- a/packages/framework/src/Console/Concerns/Command.php +++ b/packages/framework/src/Console/Concerns/Command.php @@ -9,6 +9,7 @@ use Hyde\Facades\Config; use LaravelZero\Framework\Commands\Command as BaseCommand; +use function is_string; use function array_keys; use function array_values; use function realpath; @@ -128,4 +129,12 @@ public function indentedLine(int $spaces, string $string): void { $this->line(str_repeat(' ', $spaces).$string); } + + public function askForString(string $question, ?string $default = null): ?string + { + /** @var string|null $answer */ + $answer = $this->output->ask($question, $default); + + return is_string($answer) ? $answer : $default; + } } diff --git a/packages/framework/tests/Feature/CommandTest.php b/packages/framework/tests/Feature/CommandTest.php index cab20e33601..1d0739765de 100644 --- a/packages/framework/tests/Feature/CommandTest.php +++ b/packages/framework/tests/Feature/CommandTest.php @@ -215,6 +215,39 @@ public function testCanEnableThrowOnException() $this->assertSame(1, $code); } + public function testAskForString() + { + $this->testOutput(function ($command) { + $this->assertSame('foo', $command->askForString('foo')); + }, function ($output) { + $output->shouldReceive('ask')->once()->withArgs(function (string $question, ?string $default): bool { + return $this->assertIsSame('foo', $question) && $this->assertIsNull($default); + })->andReturn('foo'); + }); + } + + public function testAskForStringWithDefaultValue() + { + $this->testOutput(function ($command) { + $this->assertSame('foo', $command->askForString('foo', 'bar')); + }, function ($output) { + $output->shouldReceive('ask')->once()->withArgs(function (string $question, ?string $default): bool { + return $this->assertIsSame('foo', $question) && $this->assertIsSame('bar', $default); + })->andReturn('foo'); + }); + } + + public function testAskForStringWithDefaultValueSupplyingNull() + { + $this->testOutput(function ($command) { + $this->assertSame('bar', $command->askForString('foo', 'bar')); + }, function ($output) { + $output->shouldReceive('ask')->once()->withArgs(function (string $question, ?string $default): bool { + return $this->assertIsSame('foo', $question) && $this->assertIsSame('bar', $default); + })->andReturn(null); + }); + } + protected function assertIsSame(string $expected, string $actual): bool { $this->assertSame($expected, $actual); @@ -222,6 +255,14 @@ protected function assertIsSame(string $expected, string $actual): bool return $actual === $expected; } + protected function assertIsNull(mixed $expected): bool + { + $this->assertNull($expected); + + return $expected === null; + } + + protected function testOutput(Closure $closure, Closure $expectations = null): void { $command = new MockableTestCommand();