Skip to content

Commit

Permalink
Merge pull request #1416 from hydephp/add-base-command-ask-for-string…
Browse files Browse the repository at this point in the history
…-helper

Extract a shared base Command method for asking for a string
  • Loading branch information
caendesilva authored Oct 29, 2023
2 parents 9feaf30 + bb4c8bc commit 65c81df
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This serves two purposes:
- Added support for setting custom content when calling source file creator actions directly in https://github.com/hydephp/develop/pull/1393
- Added support for setting a custom post date when calling post file creator action directly in https://github.com/hydephp/develop/pull/1393
- Added optional `FileNotFoundException` constructor parameter to set a custom exception message https://github.com/hydephp/develop/pull/1398
- Added a new helper method to the base `Command` class to ask for a string input from the user in https://github.com/hydephp/develop/pull/1416
- The realtime compiler dashboard is now interactive, and allows you to make edits to your project right from the browser https://github.com/hydephp/develop/pull/1392

### Changed
Expand Down
7 changes: 1 addition & 6 deletions packages/framework/src/Console/Commands/MakePageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -116,9 +116,4 @@ protected function getTypeOption(): ?string

return null;
}

protected function askForString(string $question, ?string $default = null): ?string
{
return $this->ask($question, $default);
}
}
6 changes: 0 additions & 6 deletions packages/framework/src/Console/Commands/MakePostCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Hyde\Console\Concerns\Command;
use Hyde\Framework\Actions\CreatesNewMarkdownPostFile;

use function is_string;
use function sprintf;
use function ucwords;

Expand Down Expand Up @@ -99,9 +98,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;
}
}
9 changes: 9 additions & 0 deletions packages/framework/src/Console/Concerns/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
40 changes: 40 additions & 0 deletions packages/framework/tests/Feature/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,53 @@ 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);

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();
Expand Down

0 comments on commit 65c81df

Please sign in to comment.