From 31439490811dc7e48147762913098f71d891faf3 Mon Sep 17 00:00:00 2001 From: David Young Date: Sun, 20 Aug 2023 15:46:19 -0500 Subject: [PATCH] Fixed bug that caused console input to not be trimmed (#292) * Fixed bug that caused console input to not be trimmed * Fixed Psalm error --- src/Output/StreamOutput.php | 2 +- tests/Output/StreamOutputTest.php | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Output/StreamOutput.php b/src/Output/StreamOutput.php index d1b6de5..2f16f91 100644 --- a/src/Output/StreamOutput.php +++ b/src/Output/StreamOutput.php @@ -65,7 +65,7 @@ public function readLine(): string throw new RuntimeException('Failed to read line'); } - return $input; + return \trim($input); } /** diff --git a/tests/Output/StreamOutputTest.php b/tests/Output/StreamOutputTest.php index 85a9202..60cc1ca 100644 --- a/tests/Output/StreamOutputTest.php +++ b/tests/Output/StreamOutputTest.php @@ -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; @@ -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 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(); @@ -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']);