Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the validating command class to reduce repeated code #888

Merged
merged 7 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/publications/src/Commands/MakePublicationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function safeHandle(): int
}
$creator->create();

$this->infoComment('All done! Created file', $creator->getOutputPath());
$this->infoComment("All done! Created file [{$creator->getOutputPath()}]");

return Command::SUCCESS;
}
Expand Down Expand Up @@ -128,21 +128,21 @@ protected function captureFieldInput(PublicationFieldDefinition $field): ?Public

protected function captureTextFieldInput(PublicationFieldDefinition $field): PublicationFieldValue
{
$this->infoComment('Enter lines for field', $field->name, '</>(end with an empty line)');
$this->infoComment("Enter lines for field [$field->name]");

return new PublicationFieldValue(PublicationFieldTypes::Text, implode("\n", InputStreamHandler::call()));
}

protected function captureArrayFieldInput(PublicationFieldDefinition $field): PublicationFieldValue
{
$this->infoComment('Enter values for field', $field->name, '</>(end with an empty line)');
$this->infoComment("Enter values for field [$field->name]");

return new PublicationFieldValue(PublicationFieldTypes::Array, InputStreamHandler::call());
}

protected function captureMediaFieldInput(PublicationFieldDefinition $field): ?PublicationFieldValue
{
$this->infoComment('Select file for image field', $field->name);
$this->infoComment("Select file for image field [$field->name]");

$mediaFiles = PublicationService::getMediaForPubType($this->publicationType);
if ($mediaFiles->isEmpty()) {
Expand All @@ -155,7 +155,7 @@ protected function captureMediaFieldInput(PublicationFieldDefinition $field): ?P
protected function captureTagFieldInput(PublicationFieldDefinition $field): ?PublicationFieldValue
{
$tagGroup = $field->tagGroup ?? throw new InvalidArgumentException("Tag field '$field->name' is missing the 'tagGroup' property");
$this->infoComment('Select a tag for field', $field->name, "from the $tagGroup group");
$this->infoComment(/** @lang Text */ "Select a tag for field [$field->name] from the $tagGroup group");

$options = PublicationService::getValuesForTagName($tagGroup);
if ($options->isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function getTagName(): void
protected function getTagNameFromArgument(?string $value): ?string
{
if ($value) {
$this->infoComment('Using tag name', $value, 'from command line argument');
$this->infoComment("Using tag name [$value] from command line argument");
$this->newLine();

return $value;
Expand Down Expand Up @@ -87,9 +87,9 @@ protected function printSelectionInformation(): void

protected function saveTagsToDisk(): void
{
$this->infoComment('Saving tag data to',
$this->infoComment(sprintf('Saving tag data to [%s]',
\Hyde\Console\Concerns\Command::createClickableFilepath('tags.yml')
);
));

(new PublicationTags)->addTagGroups($this->tags)->save();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected function validateSchemaFiles(): void
protected function displayResults(): void
{
foreach ($this->results as $name => $errors) {
$this->infoComment('Validating schema file for', $name);
$this->infoComment("Validating schema file for [$name]");

$schemaErrors = $errors['schema'];
if (empty($schemaErrors)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ protected function validatePublicationType(PublicationType $publicationType): vo
protected function displayResults(): void
{
foreach ($this->results as $publicationTypeName => $publications) {
$this->infoComment('Validating publication type', $publicationTypeName);
$this->infoComment("Validating publication type [$publicationTypeName]");
foreach ($publications ?? [] as $publicationName => $errors) {
$this->displayPublicationResults($publicationName, $errors);
}
Expand Down
10 changes: 1 addition & 9 deletions packages/publications/src/Commands/ValidatingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use function __;
use function array_merge;
use Exception;
use Hyde\Console\Concerns\Command;
use Illuminate\Support\Facades\Validator;
use function in_array;
use LaravelZero\Framework\Commands\Command;
use RuntimeException;
use function str_ends_with;
use function ucfirst;
Expand Down Expand Up @@ -119,14 +119,6 @@ public function handleException(Exception $exception, ?string $file = null, ?int
return Command::FAILURE;
}

/**
* Write a nicely formatted and consistent message to the console. Using InfoComment for a lack of a better term.
*/
public function infoComment(string $info, string $comment, ?string $moreInfo = null): void
{
$this->line("<info>$info</info> [<comment>$comment</comment>]".($moreInfo ? " <info>$moreInfo</info>" : ''));
}

protected function translate(string $name, string $error): string
{
return __($error, [
Expand Down
32 changes: 0 additions & 32 deletions packages/publications/tests/Feature/ValidatingCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,38 +243,6 @@ public function testCanEnableThrowOnException()
$this->assertSame(1, $code);
}

public function testInfoComment()
{
$command = new DynamicValidatingTestCommand();
$command->closure = function (ValidatingCommand $command) {
$command->infoComment('foo', 'bar');
};
$output = Mockery::mock(OutputStyle::class);

$output->shouldReceive('writeln')->once()->withArgs(function (string $message) {
return $message === '<info>foo</info> [<comment>bar</comment>]';
});

$command->setOutput($output);
$command->handle();
}

public function testInfoCommentWithExtraInfo()
{
$command = new DynamicValidatingTestCommand();
$command->closure = function (ValidatingCommand $command) {
$command->infoComment('foo', 'bar', 'baz');
};
$output = Mockery::mock(OutputStyle::class);

$output->shouldReceive('writeln')->once()->withArgs(function (string $message) {
return $message === '<info>foo</info> [<comment>bar</comment>] <info>baz</info>';
});

$command->setOutput($output);
$command->handle();
}

protected function assertEqualsAsBoolean($expected, $question): bool
{
try {
Expand Down