From b87a0ae1075f5d989628f7ff5cc197d0c58ec8d3 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Mon, 2 Feb 2009 05:50:04 +0000 Subject: [PATCH] MINOR Fixed CSVParserTest with encoding issues git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@71194 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- tests/dev/CSVParserTest.php | 21 ++++++++++++------- tests/dev/CsvBulkLoaderTest_Players.csv | 2 +- .../CsvBulkLoaderTest_PlayersWithHeader.csv | 8 +++++-- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/tests/dev/CSVParserTest.php b/tests/dev/CSVParserTest.php index df172329a..384a96035 100644 --- a/tests/dev/CSVParserTest.php +++ b/tests/dev/CSVParserTest.php @@ -5,22 +5,25 @@ class CSVParserTest extends SapphireTest { /* By default, a CSV file will be interpreted as having headers */ $csv = new CSVParser('sapphire/tests/dev/CsvBulkLoaderTest_PlayersWithHeader.csv'); - $firstNames = $birthdays = $biographies = array(); + $firstNames = $birthdays = $biographies = $registered = array(); foreach($csv as $record) { /* 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']; $biographies[] = $record['Biography']; $birthdays[] = $record['Birthday']; + $registered[] = $record['IsRegistered']; } $this->assertEquals(array('John','Jane','Jamie','Järg'), $firstNames); + $this->assertEquals(array( "He's a good guy", "She is awesome.\nSo awesome that she gets multiple rows and \"escaped\" strings in her biography", "Pretty old, with an escaped comma", "Unicode FTW"), $biographies); $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() { @@ -33,13 +36,14 @@ class CSVParserTest extends SapphireTest { 'bIoGrApHy' => '__BG', )); - $firstNames = $birthdays = $biographies = array(); + $firstNames = $birthdays = $biographies = $registered = array(); 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. */ - $this->assertEquals(array('__fn','__BG','Birthday'), array_keys($record)); + $this->assertEquals(array('__fn','__BG','Birthday','IsRegistered'), array_keys($record)); $firstNames[] = $record['__fn']; $biographies[] = $record['__BG']; $birthdays[] = $record['Birthday']; + $registered[] = $record['IsRegistered']; } $this->assertEquals(array('John','Jane','Jamie','Järg'), $firstNames); @@ -49,21 +53,23 @@ class CSVParserTest extends SapphireTest { "Pretty old, with an escaped comma", "Unicode FTW"), $biographies); $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() { /* If your CSV file doesn't have a header row */ $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) { /* 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']; $biographies[] = $record['__bio']; $birthdays[] = $record['__bd']; + $registered[] = $record['__reg']; } /* And the first row will be returned in the data */ @@ -75,6 +81,7 @@ class CSVParserTest extends SapphireTest { "Pretty old, with an escaped comma", "Unicode FTW"), $biographies); $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); } } \ No newline at end of file diff --git a/tests/dev/CsvBulkLoaderTest_Players.csv b/tests/dev/CsvBulkLoaderTest_Players.csv index 19e8923cc..cd97d08de 100644 --- a/tests/dev/CsvBulkLoaderTest_Players.csv +++ b/tests/dev/CsvBulkLoaderTest_Players.csv @@ -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 \ No newline at end of file +"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" \ No newline at end of file diff --git a/tests/dev/CsvBulkLoaderTest_PlayersWithHeader.csv b/tests/dev/CsvBulkLoaderTest_PlayersWithHeader.csv index a50c7cb47..3cdd7ed72 100644 --- a/tests/dev/CsvBulkLoaderTest_PlayersWithHeader.csv +++ b/tests/dev/CsvBulkLoaderTest_PlayersWithHeader.csv @@ -1,2 +1,6 @@ -FirstName,Biography,Birthday,IsRegistered John,He's a good guy,31/01/88,1 Jane,"She is awesome. -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 \ No newline at end of file +"FirstName","Biography","Birthday","IsRegistered" +"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" \ No newline at end of file