-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #686 from hydephp/extract-service-helper-to-base-c…
…ommand-class Extract service helper to base command class
- Loading branch information
Showing
5 changed files
with
195 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
packages/framework/src/Console/Concerns/ValidatingCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Console\Concerns; | ||
|
||
use Illuminate\Contracts\Support\Arrayable; | ||
use Illuminate\Support\Facades\Validator; | ||
use LaravelZero\Framework\Commands\Command; | ||
use RuntimeException; | ||
use function ucfirst; | ||
|
||
/** | ||
* An extended Command class that provides validation methods. | ||
* | ||
* @see \Hyde\Framework\Testing\Feature\ValidatingCommandTest | ||
*/ | ||
class ValidatingCommand extends Command | ||
{ | ||
/** @var int How many times can the validation loop run? Guards against infinite loops. */ | ||
protected final const MAX_RETRIES = 30; | ||
|
||
/** | ||
* Ask for a CLI input value until we pass validation rules. | ||
* | ||
* @param string $name | ||
* @param string $question | ||
* @param \Illuminate\Contracts\Support\Arrayable|array $rules | ||
* @param mixed|null $default | ||
* @param int $retryCount How many times has the question been asked? | ||
* @return mixed | ||
* | ||
* @throws RuntimeException | ||
*/ | ||
public function askWithValidation( | ||
string $name, | ||
string $question, | ||
Arrayable|array $rules = [], | ||
mixed $default = null, | ||
int $retryCount = 0 | ||
): mixed { | ||
if ($rules instanceof Arrayable) { | ||
$rules = $rules->toArray(); | ||
} | ||
|
||
$answer = $this->ask(ucfirst($question), $default); | ||
$validator = Validator::make([$name => $answer], [$name => $rules]); | ||
|
||
if ($validator->passes()) { | ||
return $answer; | ||
} | ||
|
||
foreach ($validator->errors()->all() as $error) { | ||
$this->error($error); | ||
} | ||
|
||
$retryCount++; | ||
|
||
if ($retryCount >= self::MAX_RETRIES) { | ||
// Prevent infinite loops that may happen, for example when testing. The retry count is high enough to not affect normal usage. | ||
throw new RuntimeException(sprintf("Too many validation errors trying to validate '$name' with rules: [%s]", implode(', ', $rules))); | ||
} | ||
|
||
return $this->askWithValidation($name, $question, $rules, null, $retryCount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
packages/framework/tests/Feature/ValidatingCommandTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Testing\Feature; | ||
|
||
use Hyde\Console\Concerns\ValidatingCommand; | ||
use Hyde\Testing\TestCase; | ||
use Illuminate\Console\OutputStyle; | ||
use Illuminate\Support\Facades\Validator; | ||
use Mockery; | ||
use RuntimeException; | ||
use function str_starts_with; | ||
|
||
/** | ||
* @covers \Hyde\Console\Concerns\ValidatingCommand | ||
*/ | ||
class ValidatingCommandTest extends TestCase | ||
{ | ||
public function testAskWithValidationCapturesInput() | ||
{ | ||
$command = new ValidationTestCommand(); | ||
|
||
$output = Mockery::mock(OutputStyle::class); | ||
|
||
$output->shouldReceive('ask')->once()->withArgs(function (string $question) { | ||
return $question === 'What is your name?'; | ||
})->andReturn('Jane Doe'); | ||
|
||
$output->shouldReceive('writeln')->once()->withArgs(function (string $message) { | ||
return $message === 'Hello Jane Doe!'; | ||
}); | ||
|
||
$command->setOutput($output); | ||
$command->handle(); | ||
} | ||
|
||
public function testAskWithValidationRetries() | ||
{ | ||
$command = new ValidationTestCommand(); | ||
$output = Mockery::mock(OutputStyle::class); | ||
|
||
$output->shouldReceive('ask')->times(2)->withArgs(function (string $question) { | ||
return $question === 'What is your name?'; | ||
})->andReturn('', 'Jane Doe'); | ||
|
||
$output->shouldReceive('writeln')->times(1)->withArgs(function (string $message) { | ||
return $message === '<error>validation.required</error>'; | ||
}); | ||
|
||
$output->shouldReceive('writeln')->once()->withArgs(function (string $message) { | ||
return $message === 'Hello Jane Doe!'; | ||
}); | ||
|
||
$command->setOutput($output); | ||
$command->handle(); | ||
} | ||
|
||
public function testAskWithValidationRetriesTooManyTimes() | ||
{ | ||
$command = new ValidationTestCommand(); | ||
$output = Mockery::mock(OutputStyle::class); | ||
|
||
$output->shouldReceive('ask')->times(30)->withArgs(function (string $question) { | ||
return $question === 'What is your name?'; | ||
})->andReturn(''); | ||
|
||
$output->shouldReceive('writeln')->times(30)->withArgs(function (string $message) { | ||
return $message === '<error>validation.required</error>'; | ||
}); | ||
|
||
$this->expectException(RuntimeException::class); | ||
$this->expectExceptionMessage("Too many validation errors trying to validate 'name' with rules: [required]"); | ||
|
||
$command->setOutput($output); | ||
$command->handle(); | ||
|
||
$output->shouldNotReceive('writeln')->once()->withArgs(function (string $message) { | ||
return str_starts_with($message, 'Hello'); | ||
}); | ||
} | ||
|
||
public function testValidationIsCalled() | ||
{ | ||
$command = new ValidationTestCommand(); | ||
$output = Mockery::mock(OutputStyle::class); | ||
|
||
$output->shouldReceive('ask')->once()->andReturn('Jane Doe'); | ||
$output->shouldReceive('writeln')->once(); | ||
|
||
$validator = Validator::spy(); | ||
$validator->shouldReceive('make')->once()->withArgs(function (array $data, array $rules) { | ||
return $data === ['name' => 'Jane Doe'] | ||
&& $rules === ['name' => ['required']]; | ||
})->andReturn($validator); | ||
$validator->shouldReceive('passes')->once()->andReturn(true); | ||
|
||
$command->setOutput($output); | ||
$command->handle(); | ||
} | ||
} | ||
|
||
class ValidationTestCommand extends ValidatingCommand | ||
{ | ||
public function handle() | ||
{ | ||
$name = $this->askWithValidation('name', 'What is your name?', ['required'], 'John Doe'); | ||
$this->output->writeln("Hello $name!"); | ||
} | ||
} |