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

Change input string handler termination sequence to <<< #936

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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
class InputStreamHandler
{
public const TERMINATION_SEQUENCE = '<<<';

private static ?array $mockedStreamBuffer = null;

public static function call(): array
Expand All @@ -36,7 +38,7 @@ protected function getLinesFromInputStream(): array
$lines = [];
do {
$line = Hyde::stripNewlines($this->readInputStream());
if ($line === '') {
if ($this->shouldTerminate($line)) {
break;
}
$lines[] = trim($line);
Expand All @@ -45,6 +47,11 @@ protected function getLinesFromInputStream(): array
return $lines;
}

protected function shouldTerminate(string $line): bool
{
return $line === self::TERMINATION_SEQUENCE;
}

/** @codeCoverageIgnore Allows for mocking of the standard input stream */
protected function readInputStream(): string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ protected function captureFieldInput(PublicationFieldDefinition $field): ?Public

protected function captureTextFieldInput(PublicationFieldDefinition $field): PublicationFieldValue
{
$this->infoComment("Enter lines for field [$field->name]");
$this->infoComment("Enter lines for field [$field->name] <fg=gray>(terminate with '<<<')</>");

return new PublicationFieldValue(PublicationFieldTypes::Text, implode("\n", InputStreamHandler::call()));
}
Expand Down
26 changes: 20 additions & 6 deletions packages/publications/tests/Feature/InputStreamHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,42 @@ class InputStreamHandlerTest extends TestCase
{
public function testCanCollectInput()
{
InputStreamHandler::mockInput('foo');
InputStreamHandler::mockInput("foo\n<<<");

$this->assertSame(0, $this->makeCommand(['foo'])->handle());
}

public function testCanTerminateWithHereSequence()
{
InputStreamHandler::mockInput("foo\nbar\nbaz\n<<<");

$this->assertSame(0, $this->makeCommand(['foo', 'bar', 'baz'])->handle());
}

public function testCanTerminateWithHereSequenceAfterCarriageReturns()
{
InputStreamHandler::mockInput("foo\r\nbar\r\nbaz\r\n<<<");

$this->assertSame(0, $this->makeCommand(['foo', 'bar', 'baz'])->handle());
}

public function testCanCollectMultipleInputLines()
{
InputStreamHandler::mockInput("foo\nbar\nbaz\n");
InputStreamHandler::mockInput("foo\nbar\nbaz\n<<<");

$this->assertSame(0, $this->makeCommand(['foo', 'bar', 'baz'])->handle());
}

public function testCanTerminateWithCarriageReturns()
public function testCanEnterMultipleCarriageReturns()
{
InputStreamHandler::mockInput("foo\r\nbar\r\nbaz\r\n");
InputStreamHandler::mockInput("foo\r\nbar\r\nbaz\r\n<<<");

$this->assertSame(0, $this->makeCommand(['foo', 'bar', 'baz'])->handle());
}

public function testCanTerminateWithUnixEndings()
public function testCanEnterMultipleUnixEndings()
{
InputStreamHandler::mockInput("foo\nbar\nbaz\n");
InputStreamHandler::mockInput("foo\nbar\nbaz\n<<<");

$this->assertSame(0, $this->makeCommand(['foo', 'bar', 'baz'])->handle());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function test_command_does_not_ask_user_to_fill_in_meta_fields()

public function test_command_with_text_input()
{
InputStreamHandler::mockInput("Hello\nWorld");
InputStreamHandler::mockInput("Hello\nWorld\n<<<");
$this->makeSchemaFile([
'canonicalField' => '__createdAt',
'fields' => [[
Expand Down Expand Up @@ -256,7 +256,7 @@ public function test_command_with_boolean_input()

public function test_command_with_array_input()
{
InputStreamHandler::mockInput("First Tag\nSecond Tag\nThird Tag");
InputStreamHandler::mockInput("First Tag\nSecond Tag\nThird Tag\n<<<");
$this->makeSchemaFile([
'canonicalField' => '__createdAt',
'fields' => [[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function tearDown(): void

public function testCanCreateNewPublicationTag()
{
InputStreamHandler::mockInput("foo\nbar\nbaz\n");
InputStreamHandler::mockInput("foo\nbar\nbaz\n<<<");

$this->artisan('make:publicationTag')
->expectsQuestion('Tag name', 'foo')
Expand All @@ -49,7 +49,7 @@ public function testCanCreateNewPublicationTag()

public function testCanCreateNewPublicationTagWithTagNameArgument()
{
InputStreamHandler::mockInput("foo\nbar\nbaz\n");
InputStreamHandler::mockInput("foo\nbar\nbaz\n<<<");

$this->artisan('make:publicationTag foo')
->expectsOutput('Using tag name [foo] from command line argument')
Expand All @@ -74,12 +74,12 @@ public function testCanCreateNewPublicationTagWithTagNameArgument()

public function testCommandFailsIfTagNameIsAlreadySet()
{
InputStreamHandler::mockInput("foo\nbar\nbaz\n");
InputStreamHandler::mockInput("foo\nbar\nbaz\n<<<");

$this->artisan('make:publicationTag foo')
->assertExitCode(0);

InputStreamHandler::mockInput("foo\nbar\nbaz\n");
InputStreamHandler::mockInput("foo\nbar\nbaz\n<<<");

$this->artisan('make:publicationTag foo')
->expectsOutput('Error: Tag [foo] already exists')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public function testWithTagFieldInputButNoTagsCanPromptToCreateTags()
{
$this->directory('test-publication');
$this->cleanUpWhenDone('tags.yml');
InputStreamHandler::mockInput("foo\nbar\nbaz\n");
InputStreamHandler::mockInput("foo\nbar\nbaz\n<<<");

$this->artisan('make:publicationType "Test Publication"')
->expectsQuestion('Enter name for field #1', 'MyTag')
Expand Down