<?php
namespace nur\mapper\fsv;

use nur\t\TestCase;

class FsvReaderTest extends TestCase {
  const SCHEMA = [
    "nom" => [10, "string"],
    "prenom" => [10, "string"],
    "date_nai" => [8, "date"],
    "exact" => [6, "number", 2],
    "approx" => [6, "number"],
  ];

  function testReader1() {
    $inf = fopen("php://memory", "w+b");
    fwrite($inf, <<<EOT
Clain     Jephte    29061975001500000153
          Francoise 29051959
EOT
    );

    rewind($inf);
    $reader = new FsvReader($inf, self::SCHEMA);
    $result = iterator_to_array($reader);
    self::assertSame([
      [
        "nom" => "Clain",
        "prenom" => "Jephte",
        "date_nai" => "29/06/1975",
        "exact" => 15.0,
        "approx" => 153,
      ],
      [
        "nom" => "",
        "prenom" => "Francoise",
        "date_nai" => "29/05/1959",
        "exact" => false,
        "approx" => false,
      ],
    ], $result);

    rewind($inf);
    $reader = new FsvReader($inf, self::SCHEMA);
    $reader->setOutputSeq(true);
    $result = iterator_to_array($reader);
    self::assertSame([
      ["nom", "prenom", "date_nai", "exact", "approx"],
      ["Clain", "Jephte", "29/06/1975", 15.0, 153],
      ["", "Francoise", "29/05/1959", false, false],
    ], $result);
  }

  function testReader2() {
    $inf = fopen(__DIR__."/latin1.fsv", "rb");

    rewind($inf);
    $reader = new FsvReader($inf, self::SCHEMA);
    $result = iterator_to_array($reader);
    self::assertSame([
      [
        "nom" => "Clain",
        "prenom" => "Jephté",
        "date_nai" => "29/06/1975",
        "exact" => 15.0,
        "approx" => 153,
      ],
      [
        "nom" => "",
        "prenom" => "Francoise",
        "date_nai" => "29/05/1959",
        "exact" => false,
        "approx" => false,
      ],
    ], $result);

    rewind($inf);
    $reader = new FsvReader($inf, self::SCHEMA);
    $reader->setOutputSeq(true);
    $result = iterator_to_array($reader);
    self::assertSame([
      ["nom", "prenom", "date_nai", "exact", "approx"],
      ["Clain", "Jephté", "29/06/1975", 15.0, 153],
      ["", "Francoise", "29/05/1959", false, false],
    ], $result);
  }
}
