From aa7beda38c807c5a503b8c186dfe9046fcfcead0 Mon Sep 17 00:00:00 2001 From: Lee Hesselden Date: Tue, 8 Oct 2019 00:31:21 +0100 Subject: [PATCH 1/3] Added a test when using a reading only resource ('r') with the empty escape parser and an empty last line --- tests/Polyfill/EmptyEscapeParserTest.php | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/Polyfill/EmptyEscapeParserTest.php b/tests/Polyfill/EmptyEscapeParserTest.php index feaf6596..3eedc7a0 100644 --- a/tests/Polyfill/EmptyEscapeParserTest.php +++ b/tests/Polyfill/EmptyEscapeParserTest.php @@ -124,6 +124,34 @@ public function testPreserveEmptyLines() } } + /** + * @covers ::parse + * @covers ::extractRecord + * @covers ::extractFieldContent + * @covers ::extractEnclosedFieldContent + */ + public function testReadingOnlyStream() + { + $source = << $record) { + self::assertSame($expected[$offset], $record); + } + + unlink($path); + } + /** * @covers ::parse * @covers ::extractRecord From 749d5caf83584c72b922d2fba88a2a2ee5893a1d Mon Sep 17 00:00:00 2001 From: Lee Hesselden Date: Tue, 8 Oct 2019 01:00:55 +0100 Subject: [PATCH 2/3] Possible fix for the empty escape parser bug --- src/Polyfill/EmptyEscapeParser.php | 3 +++ tests/Polyfill/EmptyEscapeParserTest.php | 1 + 2 files changed, 4 insertions(+) diff --git a/src/Polyfill/EmptyEscapeParser.php b/src/Polyfill/EmptyEscapeParser.php index a4425ea1..2128c1e8 100644 --- a/src/Polyfill/EmptyEscapeParser.php +++ b/src/Polyfill/EmptyEscapeParser.php @@ -151,6 +151,9 @@ private static function extractRecord(): array { $record = []; self::$line = self::$document->fgets(); + if (self::$line === false) { + return [null]; + } do { $method = 'extractFieldContent'; $buffer = ltrim(self::$line, self::$trim_mask); diff --git a/tests/Polyfill/EmptyEscapeParserTest.php b/tests/Polyfill/EmptyEscapeParserTest.php index 3eedc7a0..bbc39f8a 100644 --- a/tests/Polyfill/EmptyEscapeParserTest.php +++ b/tests/Polyfill/EmptyEscapeParserTest.php @@ -139,6 +139,7 @@ public function testReadingOnlyStream() $expected = [ ['1','2'], + [null] ]; $path = sys_get_temp_dir() . '/test.csv'; From daaf2012bc97567e56deb8707a06dc797ad0d72d Mon Sep 17 00:00:00 2001 From: Lee Hesselden Date: Tue, 8 Oct 2019 01:13:50 +0100 Subject: [PATCH 3/3] Now passing checks --- src/Polyfill/EmptyEscapeParser.php | 3 +-- tests/Polyfill/EmptyEscapeParserTest.php | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Polyfill/EmptyEscapeParser.php b/src/Polyfill/EmptyEscapeParser.php index 2128c1e8..8bef3b51 100644 --- a/src/Polyfill/EmptyEscapeParser.php +++ b/src/Polyfill/EmptyEscapeParser.php @@ -150,8 +150,7 @@ private static function filterDocument($document) private static function extractRecord(): array { $record = []; - self::$line = self::$document->fgets(); - if (self::$line === false) { + if (false === (self::$line = self::$document->fgets())) { return [null]; } do { diff --git a/tests/Polyfill/EmptyEscapeParserTest.php b/tests/Polyfill/EmptyEscapeParserTest.php index bbc39f8a..cfbcda0e 100644 --- a/tests/Polyfill/EmptyEscapeParserTest.php +++ b/tests/Polyfill/EmptyEscapeParserTest.php @@ -138,11 +138,11 @@ public function testReadingOnlyStream() EOF; $expected = [ - ['1','2'], - [null] + ['1', '2'], + [null], ]; - $path = sys_get_temp_dir() . '/test.csv'; + $path = sys_get_temp_dir().'/test.csv'; file_put_contents($path, $source); $stream = Stream::createFromPath($path);