Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
vendor/
tmp/
tools/
slimdump.phar
.phpunit.cache
Expand Down
6 changes: 6 additions & 0 deletions src/Webfactory/Slimdump/Database/CsvOutputFormatDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
74 changes: 74 additions & 0 deletions test/Webfactory/Slimdump/Database/CsvOutputFormatDriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

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 CsvOutputFormatDriver $driver;

protected function setUp(): void
{
parent::setUp();

$this->driver = new CsvOutputFormatDriver(self::OUTPUT_DIRECTORY, $this->createMock(Connection::class));
}

/**
* @param array<string, string> $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' => [
'<table dump="full" />',
['my-column' => 'my-original-value'],
'my-original-value',
];

yield 'can dump with configured replacement' => [
'<table dump="full"><column name="my-column" dump="replace" replacement="my-replacing-value"/></table>',
['my-column' => 'my-original-value'],
'my-replacing-value',
];
}

/**
* @param array<string, string> $tableRow
*
* @return array<int, array<string>> 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));
}
}
Empty file added tmp/.gitkeep
Empty file.