115 lines
3.2 KiB
PHP
115 lines
3.2 KiB
PHP
<?php
|
|
namespace nur\mapper\csv;
|
|
|
|
use nur\b\io\FileReader;
|
|
use nur\b\io\StringReader;
|
|
use nur\mapper\line\IconvMapper;
|
|
use nur\mapper\line\LineReader;
|
|
use nur\t\TestCase;
|
|
|
|
class Csv2AssocMapperTest extends TestCase {
|
|
function testMapper1() {
|
|
$reader = new StringReader(<<<EOT
|
|
nom,prenom,age
|
|
clain,jephté,45
|
|
enpion,tarte,36
|
|
x,y
|
|
z
|
|
|
|
a,b
|
|
first,second
|
|
EOT
|
|
);
|
|
|
|
$mapper = new Csv2AssocMapper(new LineReader($reader));
|
|
$mapper->setMultiSchema();
|
|
$rows = iterator_to_array($mapper);
|
|
self::assertSame([
|
|
["nom" => "clain", "prenom" => "jephté", "age" => "45"],
|
|
["nom" => "enpion", "prenom" => "tarte", "age" => "36"],
|
|
["nom" => "x", "prenom" => "y", "age" => false],
|
|
["nom" => "z", "prenom" => false, "age" => false],
|
|
["a" => "first", "b" => "second"],
|
|
], $rows);
|
|
}
|
|
|
|
function testMapper2() {
|
|
$reader = new StringReader(<<<EOT
|
|
nom,comment,num
|
|
clain,"ceci est
|
|
un commentaire",45
|
|
EOT
|
|
);
|
|
|
|
$rows = iterator_to_array(new Csv2AssocMapper(new LineReader($reader)));
|
|
self::assertNotSame([
|
|
["nom" => "clain", "comment" => "\"ceci est", "num" => null],
|
|
["nom" => "un commentaire\"", "comment" => "45", "num" => null],
|
|
], $rows);
|
|
}
|
|
|
|
function testMapper3() {
|
|
$reader = new StringReader("");
|
|
|
|
$rows = iterator_to_array(new Csv2AssocMapper(new LineReader($reader)));
|
|
self::assertSame([], $rows);
|
|
}
|
|
|
|
function testMapper4() {
|
|
$reader = new StringReader("nom,prenom,age");
|
|
|
|
$rows = iterator_to_array(new Csv2AssocMapper(new LineReader($reader)));
|
|
self::assertSame([], $rows);
|
|
}
|
|
|
|
function testMapper5() {
|
|
$reader = new StringReader("nom,prenom,age
|
|
clain,jephté,45
|
|
enpion,,
|
|
flash");
|
|
|
|
$reader->seek();
|
|
$mapper = new Csv2AssocMapper(new LineReader($reader));
|
|
$rows = iterator_to_array($mapper);
|
|
self::assertSame([
|
|
["nom" => "clain", "prenom" => "jephté", "age" => "45"],
|
|
["nom" => "enpion", "prenom" => "", "age" => ""],
|
|
["nom" => "flash", "prenom" => false, "age" => false],
|
|
], $rows);
|
|
|
|
$reader->seek();
|
|
$mapper = new Csv2AssocMapper(new LineReader($reader));
|
|
$mapper->setMapEmpty(false);
|
|
$rows = iterator_to_array($mapper);
|
|
self::assertSame([
|
|
["nom" => "clain", "prenom" => "jephté", "age" => "45"],
|
|
["nom" => "enpion", "prenom" => false, "age" => false],
|
|
["nom" => "flash", "prenom" => false, "age" => false],
|
|
], $rows);
|
|
|
|
$reader->seek();
|
|
$mapper = new Csv2AssocMapper(new LineReader($reader));
|
|
$mapper->setMapEmpty(null);
|
|
$rows = iterator_to_array($mapper);
|
|
self::assertSame([
|
|
["nom" => "clain", "prenom" => "jephté", "age" => "45"],
|
|
["nom" => "enpion", "prenom" => null, "age" => null],
|
|
["nom" => "flash", "prenom" => false, "age" => false],
|
|
], $rows);
|
|
}
|
|
|
|
function testMapper6() {
|
|
$reader = new FileReader(__DIR__ . "/utf8.csv");
|
|
$rows = iterator_to_array(new Csv2AssocMapper(new LineReader($reader)));
|
|
self::assertSame([
|
|
["nom" => "clain", "prénom" => "jephté", "age" => "45"],
|
|
], $rows);
|
|
|
|
$reader = new FileReader(__DIR__ . "/cp1252.csv");
|
|
$rows = iterator_to_array(new Csv2AssocMapper(new IconvMapper("cp1252", new LineReader($reader))));
|
|
self::assertSame([
|
|
["nom" => "clain", "prénom" => "jephté", "age" => "45"],
|
|
], $rows);
|
|
}
|
|
}
|