Skip to content

Commit 65c81df

Browse files
authored
Merge pull request #1416 from hydephp/add-base-command-ask-for-string-helper
Extract a shared base Command method for asking for a string
2 parents 9feaf30 + bb4c8bc commit 65c81df

File tree

5 files changed

+51
-12
lines changed

5 files changed

+51
-12
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This serves two purposes:
1313
- Added support for setting custom content when calling source file creator actions directly in https://github.com/hydephp/develop/pull/1393
1414
- Added support for setting a custom post date when calling post file creator action directly in https://github.com/hydephp/develop/pull/1393
1515
- Added optional `FileNotFoundException` constructor parameter to set a custom exception message https://github.com/hydephp/develop/pull/1398
16+
- 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
1617
- 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
1718

1819
### Changed

packages/framework/src/Console/Commands/MakePageCommand.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Hyde\Pages\BladePage;
1010
use Hyde\Pages\DocumentationPage;
1111
use Hyde\Pages\MarkdownPage;
12-
use LaravelZero\Framework\Commands\Command;
12+
use Hyde\Console\Concerns\Command;
1313

1414
use function strtolower;
1515
use function ucfirst;
@@ -116,9 +116,4 @@ protected function getTypeOption(): ?string
116116

117117
return null;
118118
}
119-
120-
protected function askForString(string $question, ?string $default = null): ?string
121-
{
122-
return $this->ask($question, $default);
123-
}
124119
}

packages/framework/src/Console/Commands/MakePostCommand.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Hyde\Console\Concerns\Command;
99
use Hyde\Framework\Actions\CreatesNewMarkdownPostFile;
1010

11-
use function is_string;
1211
use function sprintf;
1312
use function ucwords;
1413

@@ -99,9 +98,4 @@ protected function createPostFile(CreatesNewMarkdownPostFile $creator): int
9998
return (int) $exception->getCode();
10099
}
101100
}
102-
103-
protected function askForString(string $question, ?string $default = null): ?string
104-
{
105-
return is_string($answer = $this->output->ask($question, $default)) ? $answer : null;
106-
}
107101
}

packages/framework/src/Console/Concerns/Command.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Hyde\Facades\Config;
1010
use LaravelZero\Framework\Commands\Command as BaseCommand;
1111

12+
use function is_string;
1213
use function array_keys;
1314
use function array_values;
1415
use function realpath;
@@ -128,4 +129,12 @@ public function indentedLine(int $spaces, string $string): void
128129
{
129130
$this->line(str_repeat(' ', $spaces).$string);
130131
}
132+
133+
public function askForString(string $question, ?string $default = null): ?string
134+
{
135+
/** @var string|null $answer */
136+
$answer = $this->output->ask($question, $default);
137+
138+
return is_string($answer) ? $answer : $default;
139+
}
131140
}

packages/framework/tests/Feature/CommandTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,53 @@ public function testCanEnableThrowOnException()
215215
$this->assertSame(1, $code);
216216
}
217217

218+
public function testAskForString()
219+
{
220+
$this->testOutput(function ($command) {
221+
$this->assertSame('foo', $command->askForString('foo'));
222+
}, function ($output) {
223+
$output->shouldReceive('ask')->once()->withArgs(function (string $question, ?string $default): bool {
224+
return $this->assertIsSame('foo', $question) && $this->assertIsNull($default);
225+
})->andReturn('foo');
226+
});
227+
}
228+
229+
public function testAskForStringWithDefaultValue()
230+
{
231+
$this->testOutput(function ($command) {
232+
$this->assertSame('foo', $command->askForString('foo', 'bar'));
233+
}, function ($output) {
234+
$output->shouldReceive('ask')->once()->withArgs(function (string $question, ?string $default): bool {
235+
return $this->assertIsSame('foo', $question) && $this->assertIsSame('bar', $default);
236+
})->andReturn('foo');
237+
});
238+
}
239+
240+
public function testAskForStringWithDefaultValueSupplyingNull()
241+
{
242+
$this->testOutput(function ($command) {
243+
$this->assertSame('bar', $command->askForString('foo', 'bar'));
244+
}, function ($output) {
245+
$output->shouldReceive('ask')->once()->withArgs(function (string $question, ?string $default): bool {
246+
return $this->assertIsSame('foo', $question) && $this->assertIsSame('bar', $default);
247+
})->andReturn(null);
248+
});
249+
}
250+
218251
protected function assertIsSame(string $expected, string $actual): bool
219252
{
220253
$this->assertSame($expected, $actual);
221254

222255
return $actual === $expected;
223256
}
224257

258+
protected function assertIsNull(mixed $expected): bool
259+
{
260+
$this->assertNull($expected);
261+
262+
return $expected === null;
263+
}
264+
225265
protected function testOutput(Closure $closure, Closure $expectations = null): void
226266
{
227267
$command = new MockableTestCommand();

0 commit comments

Comments
 (0)