From d40c190742621abcfc96d1f132fa026c0cc00856 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Fri, 21 Nov 2025 21:03:11 +0000 Subject: [PATCH] Fix unit tests Signed-off-by: Kamil Tekiela --- src/Plugins/Export/ExportCsv.php | 10 ++-- tests/unit/Plugins/Export/ExportCsvTest.php | 61 +++++++-------------- tests/unit/Stubs/DbiDummy.php | 9 +++ 3 files changed, 35 insertions(+), 45 deletions(-) diff --git a/src/Plugins/Export/ExportCsv.php b/src/Plugins/Export/ExportCsv.php index 978345e897..9e23a996c1 100644 --- a/src/Plugins/Export/ExportCsv.php +++ b/src/Plugins/Export/ExportCsv.php @@ -297,27 +297,27 @@ class ExportCsv extends ExportPlugin ); $this->terminated = $this->setStringValue( $request->getParsedBodyParam('csv_terminated'), - $exportConfig['csv_terminated'] ?? null, + $exportConfig['csv_terminated'] ?? $this->terminated, ); $this->separator = $this->setStringValue( $request->getParsedBodyParam('csv_separator'), - $exportConfig['csv_separator'] ?? null, + $exportConfig['csv_separator'] ?? $this->separator, ); $this->columns = (bool) ($request->getParsedBodyParam('csv_columns') ?? $exportConfig['csv_columns'] ?? false); $this->enclosed = $this->setStringValue( $request->getParsedBodyParam('csv_enclosed'), - $exportConfig['csv_enclosed'] ?? null, + $exportConfig['csv_enclosed'] ?? $this->enclosed, ); $this->escaped = $this->setStringValue( $request->getParsedBodyParam('csv_escaped'), - $exportConfig['csv_escaped'] ?? null, + $exportConfig['csv_escaped'] ?? $this->escaped, ); $this->removeCrLf = (bool) ($request->getParsedBodyParam('csv_removeCRLF') ?? $exportConfig['csv_removeCRLF'] ?? false); $this->null = $this->setStringValue( $request->getParsedBodyParam('csv_null'), - $exportConfig['csv_null'] ?? null, + $exportConfig['csv_null'] ?? $this->null, ); } diff --git a/tests/unit/Plugins/Export/ExportCsvTest.php b/tests/unit/Plugins/Export/ExportCsvTest.php index ced2fae7e3..9d99d4e079 100644 --- a/tests/unit/Plugins/Export/ExportCsvTest.php +++ b/tests/unit/Plugins/Export/ExportCsvTest.php @@ -281,17 +281,19 @@ class ExportCsvTest extends AbstractTestCase ->withParsedBody(['csv_terminated' => ';', 'csv_columns' => 'On']); $this->object->setExportOptions($request, []); + $this->object->exportHeader(); ob_start(); self::assertTrue($this->object->exportData( 'test_db', 'test_table', - 'SELECT * FROM `test_db`.`test_table`;', + 'SELECT * FROM `test_db`.`test_table_csv_export`;', )); $result = ob_get_clean(); self::assertSame( - 'idnamedatetimefield;1abcd2011-01-20 02:00:02;2foo2010-01-20 02:00:02;3Abcd2012-01-20 02:00:02;', + 'id,name,datetimefield;1,"ab""cd",2011-01-20 02:00:02;2,"foo;",2010-01-20 02:00:02;3,' + . "\n" . 'Abcd,2012-01-20 02:00:02;', $result, ); @@ -300,63 +302,42 @@ class ExportCsvTest extends AbstractTestCase ->withParsedBody(['csv_enclosed' => '"', 'csv_terminated' => ';', 'csv_columns' => 'On']); $this->object->setExportOptions($request, []); + $this->object->exportHeader(); ob_start(); self::assertTrue($this->object->exportData( 'test_db', - 'test_table', - 'SELECT * FROM `test_db`.`test_table`;', + 'test_table_csv_export', + 'SELECT * FROM `test_db`.`test_table_csv_export`;', )); $result = ob_get_clean(); self::assertSame( - '"id""name""datetimefield";"1""abcd""2011-01-20 02:00:02";' - . '"2""foo""2010-01-20 02:00:02";"3""Abcd""2012-01-20 02:00:02";', + 'id,name,datetimefield;1,"ab""cd",2011-01-20 02:00:02;2,"foo;",2010-01-20 02:00:02;3,' + . "\n" . 'Abcd,2012-01-20 02:00:02;', $result, ); // case 4 + $request = ServerRequestFactory::create()->createServerRequest('POST', 'https://example.com/') + ->withParsedBody(['csv_enclosed' => '|', 'csv_terminated' => "\n", 'csv_escaped' => '\\', 'csv_columns' => 'On']); + + $this->object->setExportOptions($request, []); + $this->object->exportHeader(); + ob_start(); self::assertTrue($this->object->exportData( 'test_db', - 'test_table', - 'SELECT * FROM `test_db`.`test_table`;', + 'test_table_csv_export', + 'SELECT * FROM `test_db`.`test_table_csv_export`;', )); $result = ob_get_clean(); self::assertSame( - '"id""name""datetimefield";"1""abcd""2011-01-20 02:00:02";' - . '"2""foo""2010-01-20 02:00:02";"3""Abcd""2012-01-20 02:00:02";', - $result, - ); - - // case 5 - ob_start(); - self::assertTrue($this->object->exportData( - 'test_db', - 'test_table', - 'SELECT * FROM `test_db`.`test_table`;', - )); - $result = ob_get_clean(); - - self::assertSame( - '"id""name""datetimefield";"1""abcd""2011-01-20 02:00:02";' - . '"2""foo""2010-01-20 02:00:02";"3""Abcd""2012-01-20 02:00:02";', - $result, - ); - - // case 6 - ob_start(); - self::assertTrue($this->object->exportData( - 'test_db', - 'test_table', - 'SELECT * FROM `test_db`.`test_table`;', - )); - $result = ob_get_clean(); - - self::assertSame( - '"id""name""datetimefield";"1""abcd""2011-01-20 02:00:02";' - . '"2""foo""2010-01-20 02:00:02";"3""Abcd""2012-01-20 02:00:02";', + 'id,name,datetimefield' . "\n" + . '1,ab"cd,2011-01-20 02:00:02' . "\n" + . '2,foo;,2010-01-20 02:00:02' . "\n" + . '3,|' . "\n" . 'Abcd|,2012-01-20 02:00:02' . "\n", $result, ); } diff --git a/tests/unit/Stubs/DbiDummy.php b/tests/unit/Stubs/DbiDummy.php index 8a17dbc4d2..0bda773051 100644 --- a/tests/unit/Stubs/DbiDummy.php +++ b/tests/unit/Stubs/DbiDummy.php @@ -1739,6 +1739,15 @@ class DbiDummy implements DbiExtension ['3', 'Abcd', '2012-01-20 02:00:02'], ], ], + [ + 'query' => 'SELECT * FROM `test_db`.`test_table_csv_export`;', + 'columns' => ['id', 'name', 'datetimefield'], + 'result' => [ + ['1', 'ab"cd', '2011-01-20 02:00:02'], + ['2', 'foo;', '2010-01-20 02:00:02'], + ['3', "\nAbcd", '2012-01-20 02:00:02'], + ], + ], [ 'query' => 'SELECT * FROM `test_db`.`test_table_complex`;', 'columns' => ['f1', 'f2', 'f3', 'f4'],