diff --git a/packages/framework/src/Console/Commands/MakePublicationCommand.php b/packages/framework/src/Console/Commands/MakePublicationCommand.php index 414c93add60..70e8a931eff 100644 --- a/packages/framework/src/Console/Commands/MakePublicationCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationCommand.php @@ -4,6 +4,7 @@ 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; @@ -11,6 +12,7 @@ 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; @@ -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 diff --git a/packages/framework/tests/Feature/Commands/MakePublicationCommandTest.php b/packages/framework/tests/Feature/Commands/MakePublicationCommandTest.php index d373b092090..bc650dae56a 100644 --- a/packages/framework/tests/Feature/Commands/MakePublicationCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePublicationCommandTest.php @@ -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; @@ -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)); @@ -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?') @@ -123,6 +128,7 @@ 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') @@ -130,11 +136,53 @@ public function test_command_with_invalid_publication_type_passed_as_argument() ->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', @@ -145,7 +193,7 @@ protected function makeSchemaFile(): void 'sortField' => '__createdAt', 'sortAscending' => true, ], - 'fields' => [ + 'fields' => [ [ 'name' => 'title', 'min' => '0', @@ -153,7 +201,7 @@ protected function makeSchemaFile(): void 'type' => 'string', ], ], - ]) + ], $merge)) ); }