public function testCsvFileParsing() { // + Subtest 1: Unicode CSV file with header row. // -------------------------------------------------------------------------------- $path = dirname(__FILE__) . self::CSV_FILE_USERS_HEADER_UNICODE; $csvFile = new tao_helpers_data_CsvFile(); $csvFile->load($path); // - test column mapping. $expectedMapping = array('label', 'First Name', 'Last Name', 'Login', 'Mail', 'password', 'UserUILg'); $this->assertEquals($expectedMapping, $csvFile->getColumnMapping(), 'The column mapping should be ' . var_export($expectedMapping, true) . '.'); $this->assertEquals($csvFile->count(), 16, 'The CSV file contains ' . $csvFile->count() . ' rows instead of 16.'); $this->assertEquals($csvFile->getColumnCount(), 7, 'The CSV file contains ' . $csvFile->getColumnCount() . ' columns instead of 7.'); // - test some row retrievals. $expectedRow = array('TAO Jérôme Bogaerts', 'Jérôme', 'Bogaerts', 'jbogaerts', '*****@*****.**', 'jbogaerts', 'http://www.tao.lu/Ontologies/TAO.rdf#LangEN'); $this->assertEquals($csvFile->getRow(0), $expectedRow); $expectedRow = array('label' => 'TAO Isabelle Jars', 'First Name' => 'Isabelle', 'Last Name' => 'Jars', 'Login' => 'ijars', 'Mail' => '*****@*****.**', 'password' => 'ijars', 'UserUILg' => 'http://www.tao.lu/Ontologies/TAO.rdf#LangEN'); $this->assertEquals($csvFile->getRow(4, true), $expectedRow); // + Subtest 2: Unicode CSV file withouth header row. // -------------------------------------------------------------------------------- $path = dirname(__FILE__) . self::CSV_FILE_USERS_NO_HEADER_UNICODE; $csvFile = new tao_helpers_data_CsvFile($options = array('first_row_column_names' => false)); $csvFile->load($path); // - test column mapping. $expectedMapping = array(); $this->assertEquals($expectedMapping, $csvFile->getColumnMapping(), 'The column mapping should be ' . var_export($expectedMapping, true) . '.'); $this->assertEquals($csvFile->count(), 16, 'The CSV file contains ' . $csvFile->count() . ' rows instead of 16.'); $this->assertEquals($csvFile->getColumnCount(), 7, 'The CSV file contains ' . $csvFile->getColumnCount() . ' columns instead of 7.'); // - test some row retrievals. $expectedRow = array('TAO Jérôme Bogaerts', 'Jérôme', 'Bogaerts', 'jbogaerts', '*****@*****.**', 'jbogaerts', 'http://www.tao.lu/Ontologies/TAO.rdf#LangEN'); $this->assertEquals($csvFile->getRow(0), $expectedRow); $expectedRow = array('TAO Matteo Mellis', 'Matteo', 'Mellis', 'mmellis', '*****@*****.**', 'mmellis', 'http://www.tao.lu/Ontologies/TAO.rdf#LangEN'); $this->assertEquals($csvFile->getRow(15), $expectedRow); }
public function testCSVFileParsingWithMultipleFieldValues() { $path = dirname(__FILE__) . self::CSV_FILE_USERS_HEADER_UNICODE_WITH_MULTI_FIELD_VALUE; // Without multi values //--------------------------------------------------------------------------------- $csvFile = new tao_helpers_data_CsvFile(); $csvFile->load($path); // - test column mapping. $expectedMapping = array('label', 'First Name', 'Last Name', 'Login', 'Mail', 'password', 'UserUILg'); $this->assertEquals($expectedMapping, $csvFile->getColumnMapping(), 'The column mapping should be ' . var_export($expectedMapping, true) . '.'); $this->assertEquals($csvFile->count(), 16, 'The CSV file contains ' . $csvFile->count() . ' rows instead of 16.'); $this->assertEquals($csvFile->getColumnCount(), 7, 'The CSV file contains ' . $csvFile->getColumnCount() . ' columns instead of 7.'); // - test some row retrievals. $expectedRow = array('TAO Jérôme Bogaerts', 'Jérôme', 'Bogaerts', 'jbogaerts', 'jerome.bogaerts@tudor.lu|jerome@example.com', 'jbogaerts', 'http://www.tao.lu/Ontologies/TAO.rdf#LangEN'); $this->assertEquals($csvFile->getRow(0), $expectedRow); $expectedRow = array('label' => 'TAO Isabelle Jars', 'First Name' => 'Isabelle', 'Last Name' => 'Jars', 'Login' => 'ijars', 'Mail' => '*****@*****.**', 'password' => 'ijars', 'UserUILg' => 'http://www.tao.lu/Ontologies/TAO.rdf#LangEN'); $this->assertEquals($csvFile->getRow(4, true), $expectedRow); $expectedRow = array('label' => 'TAO Igor Ribassin', 'First Name' => 'Igor', 'Last Name' => 'Ribassin', 'Login' => 'iribassin', 'Mail' => '*****@*****.**', 'password' => 'iribassin', 'UserUILg' => 'http://www.tao.lu/Ontologies/TAO.rdf#LangEN|http://www.tao.lu/Ontologies/TAO.rdf#LangAR|http://www.tao.lu/Ontologies/TAO.rdf#LangDE'); $this->assertEquals($csvFile->getRow(13, true), $expectedRow); // With multi values //--------------------------------------------------------------------------------- $csvFile = new tao_helpers_data_CsvFile(['multi_values_delimiter' => '|']); $csvFile->load($path); // - test column mapping. $expectedMapping = array('label', 'First Name', 'Last Name', 'Login', 'Mail', 'password', 'UserUILg'); $this->assertEquals($expectedMapping, $csvFile->getColumnMapping(), 'The column mapping should be ' . var_export($expectedMapping, true) . '.'); $this->assertEquals($csvFile->count(), 16, 'The CSV file contains ' . $csvFile->count() . ' rows instead of 16.'); $this->assertEquals($csvFile->getColumnCount(), 7, 'The CSV file contains ' . $csvFile->getColumnCount() . ' columns instead of 7.'); // - test some row retrievals. $expectedRow = array('TAO Jérôme Bogaerts', 'Jérôme', 'Bogaerts', 'jbogaerts', ['*****@*****.**', '*****@*****.**'], 'jbogaerts', 'http://www.tao.lu/Ontologies/TAO.rdf#LangEN'); $this->assertEquals($csvFile->getRow(0), $expectedRow); $expectedRow = array('label' => 'TAO Isabelle Jars', 'First Name' => 'Isabelle', 'Last Name' => 'Jars', 'Login' => 'ijars', 'Mail' => '*****@*****.**', 'password' => 'ijars', 'UserUILg' => 'http://www.tao.lu/Ontologies/TAO.rdf#LangEN'); $this->assertEquals($csvFile->getRow(4, true), $expectedRow); $expectedRow = array('label' => 'TAO Igor Ribassin', 'First Name' => 'Igor', 'Last Name' => 'Ribassin', 'Login' => 'iribassin', 'Mail' => '*****@*****.**', 'password' => 'iribassin', 'UserUILg' => ['http://www.tao.lu/Ontologies/TAO.rdf#LangEN', 'http://www.tao.lu/Ontologies/TAO.rdf#LangAR', 'http://www.tao.lu/Ontologies/TAO.rdf#LangDE']); $this->assertEquals($csvFile->getRow(13, true), $expectedRow); }
/** * @param \tao_helpers_data_CsvFile $csv_data * @param boolean $firstRowAsColumnName * @return array */ protected function getColumnMapping($csv_data, $firstRowAsColumnName = true) { //build the mapping form if (!$csv_data->count()) { return array(); } // 'class properties' contains an associative array(str:'propertyUri' => 'str:propertyLabel') describing properties belonging to the target class. // 'ranged properties' contains an associative array(str:'propertyUri' => 'str:propertyLabel') describing properties belonging to the target class and that have a range. // 'csv_column' contains an array(int:columnIndex => 'str:columnLabel') that will be used to create the selection of possible CSV column to map in views. // 'csv_column' might have NULL values for 'str:columnLabel' meaning that there was no header row with column names in the CSV file. // Format the column mapping option for the form. if ($firstRowAsColumnName && null != $csv_data->getColumnMapping()) { // set the column label for each entry. // $csvColMapping = array('label', 'comment', ...) return $csv_data->getColumnMapping(); } else { // set an empty value for each entry of the array // to describe that column names are unknown. // $csvColMapping = array(null, null, ...) return array_fill(0, $csv_data->getColumnCount(), null); } }