Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita4ccf3d

Browse files
authored
Fix: Use configured replacements when dumping table rows in CSV (#116)
- Test CsvOutputFormatDriver->dumpTableRow()- Fix: Use configured replacements when dumping table rows in CSV
1 parenta95fa0a commita4ccf3d

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
vendor/
2+
tmp/
23
tools/
34
slimdump.phar
45
.phpunit.cache

‎src/Webfactory/Slimdump/Database/CsvOutputFormatDriver.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ public function dumpTableRow(array $row, Schema\Table $asset, Table $config): vo
7373
$this->firstLine =false;
7474
}
7575

76+
foreach ($rowas$columnName =>$value) {
77+
if ($column =$config->findColumn($columnName)) {
78+
$row[$columnName] =$column->processRowValue($value);
79+
}
80+
}
81+
7682
fputcsv($this->outputFile,$row);
7783
}
7884

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespaceWebfactory\Slimdump\Database;
4+
5+
useDoctrine\DBAL\Connection;
6+
useGenerator;
7+
usePHPUnit\Framework\Attributes\DataProvider;
8+
usePHPUnit\Framework\Attributes\Test;
9+
usePHPUnit\Framework\TestCase;
10+
useSimpleXMLElement;
11+
useWebfactory\Slimdump\Config\Table;
12+
13+
finalclass CsvOutputFormatDriverTestextends TestCase
14+
{
15+
privateconstOUTPUT_DIRECTORY =__DIR__.'/../../../../tmp';
16+
17+
privateCsvOutputFormatDriver$driver;
18+
19+
protectedfunctionsetUp():void
20+
{
21+
parent::setUp();
22+
23+
$this->driver =newCsvOutputFormatDriver(self::OUTPUT_DIRECTORY,$this->createMock(Connection::class));
24+
}
25+
26+
/**
27+
* @param array<string, string> $tableRow
28+
*/
29+
#[Test]
30+
#[DataProvider('provideDataFor_dumpTableRow')]
31+
publicfunctiondumpTableRow(string$tableConfigAsXml,array$tableRow,string$expectedValue):void
32+
{
33+
$csvData =$this->dumpTableAsCsv($tableConfigAsXml,$tableRow);
34+
35+
$this->assertSame($expectedValue,$csvData[1][0]);
36+
}
37+
38+
publicstaticfunctionprovideDataFor_dumpTableRow():Generator
39+
{
40+
yield'can dump row as is' => [
41+
'<table dump="full" />',
42+
['my-column' =>'my-original-value'],
43+
'my-original-value',
44+
];
45+
46+
yield'can dump with configured replacement' => [
47+
'<table dump="full"><column name="my-column" dump="replace" replacement="my-replacing-value"/></table>',
48+
['my-column' =>'my-original-value'],
49+
'my-replacing-value',
50+
];
51+
}
52+
53+
/**
54+
* @param array<string, string> $tableRow
55+
*
56+
* @return array<int, array<string>> the dumped CSV data as array of rows, each row being an array of columns
57+
*/
58+
privatefunctiondumpTableAsCsv(string$tableConfigAsXml,array$tableRow):array
59+
{
60+
$tableSchema =new \Doctrine\DBAL\Schema\Table('my-table');
61+
$tableConfig =newTable(
62+
newSimpleXMLElement($tableConfigAsXml)
63+
);
64+
65+
$this->driver->beginTableDataDump($tableSchema,$tableConfig);
66+
$this->driver->dumpTableRow($tableRow,$tableSchema,$tableConfig);
67+
$this->driver->endTableDataDump($tableSchema,$tableConfig);
68+
69+
$outputFile =self::OUTPUT_DIRECTORY.'/my-table.csv';
70+
$this->assertFileExists($outputFile);
71+
72+
returnarray_map('str_getcsv',file($outputFile));
73+
}
74+
}

‎tmp/.gitkeep‎

Whitespace-only changes.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp