From e869d4975e11efcc7c46ba43bb09695ba0ac18da Mon Sep 17 00:00:00 2001 From: Malte Wunsch Date: Fri, 5 Sep 2025 16:02:05 +0200 Subject: [PATCH 1/3] Test CsvOutputFormatDriver->dumpTableRow() --- .gitignore | 1 + .../Database/CsvOutputFormatDriverTest.php | 71 +++++++++++++++++++ tmp/.gitkeep | 0 3 files changed, 72 insertions(+) create mode 100644 test/Webfactory/Slimdump/Database/CsvOutputFormatDriverTest.php create mode 100644 tmp/.gitkeep diff --git a/.gitignore b/.gitignore index 386df3d..e20d214 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ vendor/ +tmp/ tools/ slimdump.phar .phpunit.cache diff --git a/test/Webfactory/Slimdump/Database/CsvOutputFormatDriverTest.php b/test/Webfactory/Slimdump/Database/CsvOutputFormatDriverTest.php new file mode 100644 index 0000000..6455e59 --- /dev/null +++ b/test/Webfactory/Slimdump/Database/CsvOutputFormatDriverTest.php @@ -0,0 +1,71 @@ +driver = new CsvOutputFormatDriver(self::OUTPUT_DIRECTORY, $this->createMock(Connection::class)); + } + + /** + * @param array $tableRow + */ + #[Test] + #[DataProvider('provideDataFor_dumpTableRow')] + public function dumpTableRow(string $tableConfigAsXml, array $tableRow, string $expectedValue): void + { + $csvData = $this->dumpTableAsCsv($tableConfigAsXml, $tableRow); + + $this->assertSame($expectedValue, $csvData[1][0]); + } + + public static function provideDataFor_dumpTableRow(): \Generator + { + yield 'can dump row as is' => [ + '', + ['my-column' => 'my-original-value'], + 'my-original-value', + ]; + + yield 'can dump with configured replacement' => [ + '
', + ['my-column' => 'my-original-value'], + 'my-replacing-value', + ]; + } + + /** + * @param array $tableRow + * @return array> the dumped CSV data as array of rows, each row being an array of columns + */ + private function dumpTableAsCsv(string $tableConfigAsXml, array $tableRow): array + { + $tableSchema = new \Doctrine\DBAL\Schema\Table('my-table'); + $tableConfig = new Table( + new \SimpleXMLElement($tableConfigAsXml) + ); + + $this->driver->beginTableDataDump($tableSchema, $tableConfig); + $this->driver->dumpTableRow($tableRow, $tableSchema, $tableConfig); + $this->driver->endTableDataDump($tableSchema, $tableConfig); + + $outputFile = self::OUTPUT_DIRECTORY . '/my-table.csv'; + $this->assertFileExists($outputFile); + + return array_map('str_getcsv', file($outputFile)); + } +} \ No newline at end of file diff --git a/tmp/.gitkeep b/tmp/.gitkeep new file mode 100644 index 0000000..e69de29 From 494b8b3c7069d0e7f027030f27802457d9eae892 Mon Sep 17 00:00:00 2001 From: Malte Wunsch Date: Fri, 5 Sep 2025 16:08:44 +0200 Subject: [PATCH 2/3] Use configured replacements when dumping table rows in CSV --- src/Webfactory/Slimdump/Database/CsvOutputFormatDriver.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Webfactory/Slimdump/Database/CsvOutputFormatDriver.php b/src/Webfactory/Slimdump/Database/CsvOutputFormatDriver.php index 94a4f72..620273b 100644 --- a/src/Webfactory/Slimdump/Database/CsvOutputFormatDriver.php +++ b/src/Webfactory/Slimdump/Database/CsvOutputFormatDriver.php @@ -73,6 +73,12 @@ public function dumpTableRow(array $row, Schema\Table $asset, Table $config): vo $this->firstLine = false; } + foreach ($row as $columnName => $value) { + if ($column = $config->findColumn($columnName)) { + $row[$columnName] = $column->processRowValue($value); + } + } + fputcsv($this->outputFile, $row); } From 9e79734c8fd3ba16f2e929439af4b27f637ffed5 Mon Sep 17 00:00:00 2001 From: MalteWunsch <2504942+MalteWunsch@users.noreply.github.com> Date: Fri, 5 Sep 2025 14:11:24 +0000 Subject: [PATCH 3/3] Fix CS with PHP-CS-Fixer --- .../Slimdump/Database/CsvOutputFormatDriverTest.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/test/Webfactory/Slimdump/Database/CsvOutputFormatDriverTest.php b/test/Webfactory/Slimdump/Database/CsvOutputFormatDriverTest.php index 6455e59..32f0f87 100644 --- a/test/Webfactory/Slimdump/Database/CsvOutputFormatDriverTest.php +++ b/test/Webfactory/Slimdump/Database/CsvOutputFormatDriverTest.php @@ -3,14 +3,16 @@ namespace Webfactory\Slimdump\Database; use Doctrine\DBAL\Connection; +use Generator; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; +use SimpleXMLElement; use Webfactory\Slimdump\Config\Table; final class CsvOutputFormatDriverTest extends TestCase { - private const OUTPUT_DIRECTORY = __DIR__ . '/../../../../tmp'; + private const OUTPUT_DIRECTORY = __DIR__.'/../../../../tmp'; private CsvOutputFormatDriver $driver; @@ -33,7 +35,7 @@ public function dumpTableRow(string $tableConfigAsXml, array $tableRow, string $ $this->assertSame($expectedValue, $csvData[1][0]); } - public static function provideDataFor_dumpTableRow(): \Generator + public static function provideDataFor_dumpTableRow(): Generator { yield 'can dump row as is' => [ '', @@ -50,22 +52,23 @@ public static function provideDataFor_dumpTableRow(): \Generator /** * @param array $tableRow + * * @return array> the dumped CSV data as array of rows, each row being an array of columns */ private function dumpTableAsCsv(string $tableConfigAsXml, array $tableRow): array { $tableSchema = new \Doctrine\DBAL\Schema\Table('my-table'); $tableConfig = new Table( - new \SimpleXMLElement($tableConfigAsXml) + new SimpleXMLElement($tableConfigAsXml) ); $this->driver->beginTableDataDump($tableSchema, $tableConfig); $this->driver->dumpTableRow($tableRow, $tableSchema, $tableConfig); $this->driver->endTableDataDump($tableSchema, $tableConfig); - $outputFile = self::OUTPUT_DIRECTORY . '/my-table.csv'; + $outputFile = self::OUTPUT_DIRECTORY.'/my-table.csv'; $this->assertFileExists($outputFile); return array_map('str_getcsv', file($outputFile)); } -} \ No newline at end of file +}