<?php
namespace nur\mapper\csv;

use nur\b\io\StringWriter;
use nur\mapper\line\LineWriter;
use nur\t\TestCase;

class Assoc2CsvMapperTest extends TestCase {
  function testWriter() {
    $w = new StringWriter();
    $consumer = new LineWriter($w);
    $consumer->consume([
      ["nom" => "clain", "prenom" => "jephté", "age" => 45],
      ["nom" => "enpion", "prenom" => "tarte", "age" => 36],
      ["nom" => "cool", "prenom" => "plusieurs
lignes", "age" => 15],
    ], Assoc2CsvMapper::class);
    self::assertSame("nom,prenom,age
clain,jephté,45
enpion,tarte,36
cool,\"plusieurs
lignes\",15
", $w->getString());
  }

  function testMultischema() {
    $w = new StringWriter();
    $consumer = new LineWriter($w);
    $mapper = new Assoc2CsvMapper();
    $mapper->setMultiSchema();
    $consumer->consume([
      ["nom" => "clain", "prenom" => "jephté", "age" => 45],
      ["prenom" => "tarte", "nom" => "enpion", "age" => 36],
      ["a" => "first", "b" => "second"],
    ], $mapper);
    self::assertSame("nom,prenom,age
clain,jephté,45
enpion,tarte,36

a,b
first,second
", $w->getString());
  }

  function testEmpty() {
    $w = new StringWriter();
    $consumer = new LineWriter($w);
    $consumer->consume([], Assoc2CsvMapper::class);
    self::assertSame("", $w->getString());

    $w = new StringWriter();
    $consumer = new LineWriter($w);
    $mapper = new Assoc2CsvMapper();
    $mapper->setHeaders(["nom", "prenom", "age"]);
    $consumer->consume([], $mapper);
    self::assertSame("nom,prenom,age\n", $w->getString());
  }
}
