Skip to content

Commit

Permalink
Fixed bug that caused console input to not be trimmed (#292)
Browse files Browse the repository at this point in the history
* Fixed bug that caused console input to not be trimmed

* Fixed Psalm error
  • Loading branch information
davidbyoung committed Aug 20, 2023
1 parent 660d8fe commit 3143949
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Output/StreamOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function readLine(): string
throw new RuntimeException('Failed to read line');
}

return $input;
return \trim($input);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/Output/StreamOutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Aphiria\Console\Output\Compilers\OutputCompiler;
use Aphiria\Console\Output\StreamOutput;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use RuntimeException;

Expand All @@ -32,6 +33,16 @@ protected function setUp(): void
$this->output = new StreamOutput(\fopen('php://memory', 'wb'), $this->inputStream, $this->compiler);
}

/**
* Provides input for readln tests
*
* @return list<array{0: string, 1: string}> The list of untrimmed input and expected trimmed input
*/
public static function provideReadlnInput(): array
{
return [['foo' . PHP_EOL, 'foo'], [' foo ', 'foo']];
}

public function testClearDoesNothing(): void
{
$this->output->clear();
Expand Down Expand Up @@ -66,6 +77,14 @@ public function testReadingLineThatFailsThrowsException(): void
$this->output->readLine();
}

#[DataProvider('provideReadlnInput')]
public function testReadingLineTrimsInput(string $rawInput, string $cleanedInput): void
{
\fwrite($this->inputStream, $rawInput);
\rewind($this->inputStream);
$this->assertSame($cleanedInput, $this->output->readLine());
}

public function testWritelnOnArray(): void
{
$this->output->writeln(['foo', 'bar']);
Expand Down

0 comments on commit 3143949

Please sign in to comment.