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/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); } diff --git a/test/Webfactory/Slimdump/Database/CsvOutputFormatDriverTest.php b/test/Webfactory/Slimdump/Database/CsvOutputFormatDriverTest.php new file mode 100644 index 0000000..32f0f87 --- /dev/null +++ b/test/Webfactory/Slimdump/Database/CsvOutputFormatDriverTest.php @@ -0,0 +1,74 @@ +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)); + } +} diff --git a/tmp/.gitkeep b/tmp/.gitkeep new file mode 100644 index 0000000..e69de29