Skip to content

Commit

Permalink
Merge branch 'refactor-the-MakePublicationCommand-class' into support…
Browse files Browse the repository at this point in the history
…-meta-fields-as-canonical-publication-fields
  • Loading branch information
caendesilva committed Dec 18, 2022
2 parents d0ecf73 + 0ee0819 commit bfbff57
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 25 deletions.
27 changes: 6 additions & 21 deletions packages/framework/src/Console/Commands/MakePublicationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

namespace Hyde\Console\Commands;

use Hyde\Console\Commands\Helpers\InputStreamHandler;
use Hyde\Console\Concerns\ValidatingCommand;
use Hyde\Framework\Actions\CreatesNewPublicationPage;
use Hyde\Framework\Features\Publications\Concerns\PublicationFieldTypes;
use Hyde\Framework\Features\Publications\Models\PublicationFieldType;
use Hyde\Framework\Features\Publications\Models\PublicationType;
use Hyde\Framework\Features\Publications\PublicationService;
use Illuminate\Support\Str;
use function implode;
use InvalidArgumentException;
use LaravelZero\Framework\Commands\Command;
use Rgasch\Collection\Collection;
Expand Down Expand Up @@ -134,34 +136,17 @@ protected function hasForceOption(): bool
return (bool) $this->option('force');
}

protected function captureTextFieldInput(PublicationFieldType $field): array
protected function captureTextFieldInput(PublicationFieldType $field): string
{
$lines = [];
$this->output->writeln($field->name." (end with a line containing only '<<<')");
do {
$line = Str::replace("\n", '', fgets(STDIN));
if ($line === '<<<') {
break;
}
$lines[] = $line;
} while (true);
$this->output->writeln($field->name.' (end with an empty line)');

return $lines;
return implode("\n", InputStreamHandler::call());
}

protected function captureArrayFieldInput(PublicationFieldType $field): array
{
$lines = [];
$this->output->writeln($field->name.' (end with an empty line)');
do {
$line = Str::replace("\n", '', fgets(STDIN));
if ($line === '') {
break;
}
$lines[] = trim($line);
} while (true);

return $lines;
return InputStreamHandler::call();
}

protected function captureImageFieldInput(PublicationFieldType $field, PublicationType $pubType): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace Hyde\Framework\Testing\Feature\Commands;

use function array_merge;
use function config;
use function file_get_contents;
use Hyde\Console\Commands\Helpers\InputStreamHandler;
use Hyde\Facades\Filesystem;
use Hyde\Hyde;
use Hyde\Testing\TestCase;
Expand All @@ -20,6 +23,7 @@ class MakePublicationCommandTest extends TestCase
protected function setUp(): void
{
parent::setUp();
config(['app.throw_on_console_exception' => true]);

Filesystem::makeDirectory('test-publication');
Carbon::setTestNow(Carbon::create(2022));
Expand Down Expand Up @@ -50,6 +54,7 @@ public function test_command_creates_publication()

public function test_command_with_no_publication_types()
{
config(['app.throw_on_console_exception' => false]);
$this->artisan('make:publication')
->expectsOutputToContain('Creating a new Publication!')
->expectsOutput('Error: Unable to locate any publication types. Did you create any?')
Expand Down Expand Up @@ -123,18 +128,61 @@ public function test_command_with_publication_type_passed_as_argument()

public function test_command_with_invalid_publication_type_passed_as_argument()
{
config(['app.throw_on_console_exception' => false]);
$this->makeSchemaFile();

$this->artisan('make:publication foo')
->expectsOutput('Error: Unable to locate publication type [foo]')
->assertExitCode(1);
}

protected function makeSchemaFile(): void
// text
public function test_command_with_text_input()
{
InputStreamHandler::mockInput("Hello\nWorld");
$this->makeSchemaFile([
'canonicalField' => 'description',
'fields' => [[
'type' => 'text',
'name' => 'description',
'min' => '0',
'max' => '0',
],
],
]);
$this->artisan('make:publication test-publication')
->assertExitCode(0);

$this->assertTrue(File::exists(Hyde::path('test-publication/hello-world.md')));
$this->assertStringContainsString("Hello\nWorld", file_get_contents(Hyde::path('test-publication/hello-world.md')));
}

// array
public function test_command_with_array_input()
{
InputStreamHandler::mockInput("Foo\nBar");
$this->makeSchemaFile([
'fields' => [[
'type' => 'array',
'name' => 'tags',
'min' => '0',
'max' => '0',
],
],
]);

$this->artisan('make:publication test-publication')
->assertExitCode(0);

$this->assertTrue(File::exists(Hyde::path('test-publication/hello-world.md')));
$this->assertStringContainsString("Foo\nBar", file_get_contents(Hyde::path('test-publication/hello-world.md')));
}

protected function makeSchemaFile(array $merge = []): void
{
file_put_contents(
Hyde::path('test-publication/schema.json'),
json_encode([
json_encode(array_merge([
'name' => 'Test Publication',
'canonicalField' => 'title',
'detailTemplate' => 'test-publication_detail',
Expand All @@ -145,15 +193,15 @@ protected function makeSchemaFile(): void
'sortField' => '__createdAt',
'sortAscending' => true,
],
'fields' => [
'fields' => [
[
'name' => 'title',
'min' => '0',
'max' => '0',
'type' => 'string',
],
],
])
], $merge))
);
}

Expand Down

0 comments on commit bfbff57

Please sign in to comment.