Skip to content

Commit c6decb6

Browse files
author
Malte Riesch
committed
Merge branch 'development'
2 parents 9186aae + cf0807f commit c6decb6

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

lib/TestDbAcle/Psv/PsvParser.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ public function parsePsv($psvTableContent)
105105
$psvHeaderLine = $this->extractHeaders($psvRows);
106106
$headers = $this->splitByPipe($psvHeaderLine);
107107

108+
foreach($headers as $index=>$header){
109+
110+
$headers[$index] = $this->filterHeader($header);
111+
}
112+
108113
$contentTable = array();
109114

110115
foreach ($psvRows as $psvRow) {
@@ -158,6 +163,24 @@ protected function trimArrayElements(array $row)
158163
}
159164
return $row;
160165
}
166+
167+
protected function filterHeader($value)
168+
{
169+
$self = $this;//we need to to do this for PHP 5.3 compatability
170+
$filters = array(//@TODO remove duplication
171+
'stripComments' => function(&$value) use($self){
172+
if(strpos($value, $self::SYMBOL_COMMENT) !== false && strpos($value, $self::SYMBOL_COMMENT)!==0){
173+
list($valuePart,) = preg_split('/(?<!\\\\)'.$self::SYMBOL_COMMENT.'/', $value);
174+
$value = trim($valuePart);
175+
}
176+
},
177+
);
178+
179+
foreach($filters as $filter){
180+
$filter($value);
181+
}
182+
return $value;
183+
}
161184

162185
protected function filterColumnValue($value)
163186
{

lib/TestDbAcle/Psv/Table/Meta.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function isReplaceMode()
4040

4141
public function getIdentifyColumns()
4242
{
43-
return $this->getAttribute('identifiedBy', array());
43+
return (array) $this->getAttribute('identifiedBy', array());
4444
}
4545

4646
public function getTruncateDateColumns()

tests/TestDbAcle/Psv/PsvParserTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,48 @@ function test_parsePsv_WithEscapedCharacters() {
6666
$this->assertSame($expectedArray, $psvParser->parsePsv($psvToParse));
6767
}
6868

69+
function test_parsePsv_WithNull_AndCommentInLastHeader() {
70+
71+
$psvParser = new \TestDbAcle\Psv\PsvParser();
72+
73+
$psvToParse = "
74+
id |first_name #moo |last_name
75+
10 |NULL #nul |miller
76+
20 |stu |Smith
77+
";
78+
79+
$expectedArray = array(
80+
array("id" => "10",
81+
"first_name" => NULL,
82+
"last_name" => "miller"),
83+
array("id" => "20",
84+
"first_name" => "stu",
85+
"last_name" => "Smith"),
86+
);
87+
$this->assertSame($expectedArray, $psvParser->parsePsv($psvToParse));
88+
}
89+
90+
function test_parsePsv_WithNull_AndCommentInHeaders() {
91+
92+
$psvParser = new \TestDbAcle\Psv\PsvParser();
93+
94+
$psvToParse = "
95+
id |first_name |last_name #comment
96+
10 |NULL #nul |miller
97+
20 |stu |Smith
98+
";
99+
100+
$expectedArray = array(
101+
array("id" => "10",
102+
"first_name" => NULL,
103+
"last_name" => "miller"),
104+
array("id" => "20",
105+
"first_name" => "stu",
106+
"last_name" => "Smith"),
107+
);
108+
$this->assertSame($expectedArray, $psvParser->parsePsv($psvToParse));
109+
}
110+
69111
function test_parsePsv_WithNull_AndInlineComment() {
70112

71113
$psvParser = new \TestDbAcle\Psv\PsvParser();

tests/TestDbAcle/Psv/Table/MetaTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ public function test_Replace()
1818
$this->assertEquals(array('first_name','last_name'), $meta->getIdentifyColumns());
1919
}
2020

21+
public function test_Replace_OneIdColumn()
22+
{
23+
$meta = new \TestDbAcle\Psv\Table\Meta(array( 'mode'=> 'replace', 'identifiedBy' => 'id'));
24+
$this->assertTrue($meta->isReplaceMode());
25+
$this->assertEquals(array('id'), $meta->getIdentifyColumns());
26+
}
27+
2128
public function test_TruncateDates_none()
2229
{
2330
$meta = new \TestDbAcle\Psv\Table\Meta(array());

0 commit comments

Comments
 (0)