2008-10-03 02:27:58 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Dev\Tests;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\CSVParser;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
2008-10-03 02:27:58 +02:00
|
|
|
class CSVParserTest extends SapphireTest {
|
2013-05-11 08:05:53 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
/**
|
|
|
|
* Name of csv test dir
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $csvPath = null;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->csvPath = __DIR__ . '/CsvBulkLoaderTest/csv/';
|
|
|
|
}
|
2013-05-11 08:05:53 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testParsingWithHeaders() {
|
2008-10-03 02:27:58 +02:00
|
|
|
/* By default, a CSV file will be interpreted as having headers */
|
2016-10-14 03:30:05 +02:00
|
|
|
$csv = new CSVParser($this->csvPath . 'PlayersWithHeader.csv');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-03 05:00:02 +01:00
|
|
|
$firstNames = $birthdays = $biographies = $registered = array();
|
2008-10-03 02:27:58 +02:00
|
|
|
foreach($csv as $record) {
|
|
|
|
/* Each row in the CSV file will be keyed with the header row */
|
2009-02-03 05:00:02 +01:00
|
|
|
$this->assertEquals(array('FirstName','Biography','Birthday','IsRegistered'), array_keys($record));
|
2008-10-03 02:27:58 +02:00
|
|
|
$firstNames[] = $record['FirstName'];
|
|
|
|
$biographies[] = $record['Biography'];
|
|
|
|
$birthdays[] = $record['Birthday'];
|
2009-02-03 05:00:02 +01:00
|
|
|
$registered[] = $record['IsRegistered'];
|
2008-10-03 02:27:58 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-03 02:27:58 +02:00
|
|
|
$this->assertEquals(array('John','Jane','Jamie','Järg'), $firstNames);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-03 02:27:58 +02:00
|
|
|
$this->assertEquals(array(
|
|
|
|
"He's a good guy",
|
2014-08-15 08:53:05 +02:00
|
|
|
"She is awesome." . PHP_EOL
|
2012-09-26 23:34:00 +02:00
|
|
|
. "So awesome that she gets multiple rows and \"escaped\" strings in her biography",
|
2008-10-03 02:27:58 +02:00
|
|
|
"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);
|
2009-02-03 05:00:02 +01:00
|
|
|
$this->assertEquals(array('1', '0', '1', '1'), $registered);
|
2008-10-03 02:27:58 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testParsingWithHeadersAndColumnMap() {
|
2008-10-03 02:27:58 +02:00
|
|
|
/* By default, a CSV file will be interpreted as having headers */
|
2016-10-14 03:30:05 +02:00
|
|
|
$csv = new CSVParser($this->csvPath . 'PlayersWithHeader.csv');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-03 02:27:58 +02:00
|
|
|
/* We can set up column remapping. The keys are case-insensitive. */
|
|
|
|
$csv->mapColumns(array(
|
|
|
|
'FirstName' => '__fn',
|
|
|
|
'bIoGrApHy' => '__BG',
|
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-03 05:00:02 +01:00
|
|
|
$firstNames = $birthdays = $biographies = $registered = array();
|
2008-10-03 02:27:58 +02:00
|
|
|
foreach($csv as $record) {
|
2012-09-26 23:34:00 +02:00
|
|
|
/* Each row in the CSV file will be keyed with the renamed columns. Any unmapped column names will be
|
|
|
|
* left as-is. */
|
2009-02-03 05:00:02 +01:00
|
|
|
$this->assertEquals(array('__fn','__BG','Birthday','IsRegistered'), array_keys($record));
|
2008-10-03 02:27:58 +02:00
|
|
|
$firstNames[] = $record['__fn'];
|
|
|
|
$biographies[] = $record['__BG'];
|
|
|
|
$birthdays[] = $record['Birthday'];
|
2009-02-03 05:00:02 +01:00
|
|
|
$registered[] = $record['IsRegistered'];
|
2008-10-03 02:27:58 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-03 02:27:58 +02:00
|
|
|
$this->assertEquals(array('John','Jane','Jamie','Järg'), $firstNames);
|
|
|
|
$this->assertEquals(array(
|
|
|
|
"He's a good guy",
|
2012-09-26 23:34:00 +02:00
|
|
|
"She is awesome."
|
|
|
|
. PHP_EOL . "So awesome that she gets multiple rows and \"escaped\" strings in her biography",
|
2008-10-03 02:27:58 +02:00
|
|
|
"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);
|
2009-02-03 05:00:02 +01:00
|
|
|
$this->assertEquals(array('1', '0', '1', '1'), $registered);
|
2008-10-03 02:27:58 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testParsingWithExplicitHeaderRow() {
|
2008-10-03 02:27:58 +02:00
|
|
|
/* If your CSV file doesn't have a header row */
|
2016-10-14 03:30:05 +02:00
|
|
|
$csv = new CSVParser($this->csvPath .'PlayersWithHeader.csv');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-03 05:00:02 +01:00
|
|
|
$csv->provideHeaderRow(array('__fn','__bio','__bd','__reg'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-03 05:00:02 +01:00
|
|
|
$firstNames = $birthdays = $biographies = $registered = array();
|
2008-10-03 02:27:58 +02:00
|
|
|
foreach($csv as $record) {
|
|
|
|
/* Each row in the CSV file will be keyed with the header row that you gave */
|
2009-02-03 05:00:02 +01:00
|
|
|
$this->assertEquals(array('__fn','__bio','__bd','__reg'), array_keys($record));
|
2008-10-03 02:27:58 +02:00
|
|
|
$firstNames[] = $record['__fn'];
|
|
|
|
$biographies[] = $record['__bio'];
|
|
|
|
$birthdays[] = $record['__bd'];
|
2009-02-03 05:00:02 +01:00
|
|
|
$registered[] = $record['__reg'];
|
2008-10-03 02:27:58 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
/* And the first row will be returned in the data */
|
2008-10-03 02:27:58 +02:00
|
|
|
$this->assertEquals(array('FirstName','John','Jane','Jamie','Järg'), $firstNames);
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'Biography',
|
|
|
|
"He's a good guy",
|
2012-09-26 23:34:00 +02:00
|
|
|
"She is awesome." . PHP_EOL
|
|
|
|
. "So awesome that she gets multiple rows and \"escaped\" strings in her biography",
|
2008-10-03 02:27:58 +02:00
|
|
|
"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);
|
2009-02-03 05:00:02 +01:00
|
|
|
$this->assertEquals(array('IsRegistered', '1', '0', '1', '1'), $registered);
|
2008-10-03 02:27:58 +02:00
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|