Merged from branches/2.3

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@71282 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sean Harvey 2009-02-03 04:00:02 +00:00
parent 2f81c822bb
commit a38b3268c6
3 changed files with 21 additions and 10 deletions

View File

@ -5,22 +5,25 @@ class CSVParserTest extends SapphireTest {
/* By default, a CSV file will be interpreted as having headers */ /* By default, a CSV file will be interpreted as having headers */
$csv = new CSVParser('sapphire/tests/dev/CsvBulkLoaderTest_PlayersWithHeader.csv'); $csv = new CSVParser('sapphire/tests/dev/CsvBulkLoaderTest_PlayersWithHeader.csv');
$firstNames = $birthdays = $biographies = array(); $firstNames = $birthdays = $biographies = $registered = array();
foreach($csv as $record) { foreach($csv as $record) {
/* Each row in the CSV file will be keyed with the header row */ /* Each row in the CSV file will be keyed with the header row */
$this->assertEquals(array('FirstName','Biography','Birthday'), array_keys($record)); $this->assertEquals(array('FirstName','Biography','Birthday','IsRegistered'), array_keys($record));
$firstNames[] = $record['FirstName']; $firstNames[] = $record['FirstName'];
$biographies[] = $record['Biography']; $biographies[] = $record['Biography'];
$birthdays[] = $record['Birthday']; $birthdays[] = $record['Birthday'];
$registered[] = $record['IsRegistered'];
} }
$this->assertEquals(array('John','Jane','Jamie','Järg'), $firstNames); $this->assertEquals(array('John','Jane','Jamie','Järg'), $firstNames);
$this->assertEquals(array( $this->assertEquals(array(
"He's a good guy", "He's a good guy",
"She is awesome.\nSo awesome that she gets multiple rows and \"escaped\" strings in her biography", "She is awesome.\nSo awesome that she gets multiple rows and \"escaped\" strings in her biography",
"Pretty old, with an escaped comma", "Pretty old, with an escaped comma",
"Unicode FTW"), $biographies); "Unicode FTW"), $biographies);
$this->assertEquals(array("31/01/1988","31/01/1982","31/01/1882","31/06/1982"), $birthdays); $this->assertEquals(array("31/01/1988","31/01/1982","31/01/1882","31/06/1982"), $birthdays);
$this->assertEquals(array('1', '0', '1', '1'), $registered);
} }
function testParsingWithHeadersAndColumnMap() { function testParsingWithHeadersAndColumnMap() {
@ -33,13 +36,14 @@ class CSVParserTest extends SapphireTest {
'bIoGrApHy' => '__BG', 'bIoGrApHy' => '__BG',
)); ));
$firstNames = $birthdays = $biographies = array(); $firstNames = $birthdays = $biographies = $registered = array();
foreach($csv as $record) { foreach($csv as $record) {
/* Each row in the CSV file will be keyed with the renamed columns. Any unmapped column names will be left as-is. */ /* Each row in the CSV file will be keyed with the renamed columns. Any unmapped column names will be left as-is. */
$this->assertEquals(array('__fn','__BG','Birthday'), array_keys($record)); $this->assertEquals(array('__fn','__BG','Birthday','IsRegistered'), array_keys($record));
$firstNames[] = $record['__fn']; $firstNames[] = $record['__fn'];
$biographies[] = $record['__BG']; $biographies[] = $record['__BG'];
$birthdays[] = $record['Birthday']; $birthdays[] = $record['Birthday'];
$registered[] = $record['IsRegistered'];
} }
$this->assertEquals(array('John','Jane','Jamie','Järg'), $firstNames); $this->assertEquals(array('John','Jane','Jamie','Järg'), $firstNames);
@ -49,21 +53,23 @@ class CSVParserTest extends SapphireTest {
"Pretty old, with an escaped comma", "Pretty old, with an escaped comma",
"Unicode FTW"), $biographies); "Unicode FTW"), $biographies);
$this->assertEquals(array("31/01/1988","31/01/1982","31/01/1882","31/06/1982"), $birthdays); $this->assertEquals(array("31/01/1988","31/01/1982","31/01/1882","31/06/1982"), $birthdays);
$this->assertEquals(array('1', '0', '1', '1'), $registered);
} }
function testParsingWithExplicitHeaderRow() { function testParsingWithExplicitHeaderRow() {
/* If your CSV file doesn't have a header row */ /* If your CSV file doesn't have a header row */
$csv = new CSVParser('sapphire/tests/dev/CsvBulkLoaderTest_PlayersWithHeader.csv'); $csv = new CSVParser('sapphire/tests/dev/CsvBulkLoaderTest_PlayersWithHeader.csv');
$csv->provideHeaderRow(array('__fn','__bio','__bd')); $csv->provideHeaderRow(array('__fn','__bio','__bd','__reg'));
$firstNames = $birthdays = $biographies = array(); $firstNames = $birthdays = $biographies = $registered = array();
foreach($csv as $record) { foreach($csv as $record) {
/* Each row in the CSV file will be keyed with the header row that you gave */ /* Each row in the CSV file will be keyed with the header row that you gave */
$this->assertEquals(array('__fn','__bio','__bd'), array_keys($record)); $this->assertEquals(array('__fn','__bio','__bd','__reg'), array_keys($record));
$firstNames[] = $record['__fn']; $firstNames[] = $record['__fn'];
$biographies[] = $record['__bio']; $biographies[] = $record['__bio'];
$birthdays[] = $record['__bd']; $birthdays[] = $record['__bd'];
$registered[] = $record['__reg'];
} }
/* And the first row will be returned in the data */ /* And the first row will be returned in the data */
@ -75,6 +81,7 @@ class CSVParserTest extends SapphireTest {
"Pretty old, with an escaped comma", "Pretty old, with an escaped comma",
"Unicode FTW"), $biographies); "Unicode FTW"), $biographies);
$this->assertEquals(array("Birthday","31/01/1988","31/01/1982","31/01/1882","31/06/1982"), $birthdays); $this->assertEquals(array("Birthday","31/01/1988","31/01/1982","31/01/1882","31/06/1982"), $birthdays);
$this->assertEquals(array('IsRegistered', '1', '0', '1', '1'), $registered);
} }
} }

View File

@ -1 +1 @@
John,He's a good guy,ignored,31/01/88,1 Jane,"She is awesome.\nSo awesome that she gets multiple rows and \escaped\"" strings in her biography""",ignored,31/01/82,0 Jamie,"Pretty old\, with an escaped comma",ignored,31/01/1882,1 Järg,Unicode FTW,ignored,31/06/1982,1 "John","He's a good guy","ignored","31/01/1988","1" "Jane","She is awesome.\nSo awesome that she gets multiple rows and \"escaped\" strings in her biography","ignored","31/01/1982","0" "Jamie","Pretty old\, with an escaped comma","ignored","31/01/1882","1" "Järg","Unicode FTW","ignored","31/06/1982","1"
Can't render this file because it contains an unexpected character in line 1 and column 51.

View File

@ -1,2 +1,6 @@
FirstName,Biography,Birthday,IsRegistered John,He's a good guy,31/01/88,1 Jane,"She is awesome. "FirstName","Biography","Birthday","IsRegistered"
So awesome that she gets multiple rows and \escaped\"" strings in her biography""",31/01/82,0 Jamie,"Pretty old\, with an escaped comma",31/01/1882,1 Järg,Unicode FTW,31/06/1982,1 "John","He's a good guy","31/01/1988","1"
"Jane","She is awesome.
So awesome that she gets multiple rows and \"escaped\" strings in her biography","31/01/1982","0"
"Jamie","Pretty old\, with an escaped comma","31/01/1882","1"
"Järg","Unicode FTW","31/06/1982","1"
Can't render this file because it contains an unexpected character in line 4 and column 45.