<?php

namespace nur\io\fsv;

use PHPUnit\Framework\TestCase;

class FsvSchemaTest extends TestCase {
  protected function setUp(): void {
    $this->schema = new FsvSchema([
      "string" => [5],
      "number" => [5, "number"],
      "date6" => [6, "date"],
      "date8" => [8, "date"],
    ]);
  }

  /** @var FsvSchema */
  private $schema;

  function testParse() {
    $schema = $this->schema;

    $row = $schema->parseRow("                        ");
    self::assertSame("", $row["string"]);
    self::assertFalse($row["number"]);
    self::assertFalse($row["date6"]);
    self::assertFalse($row["date8"]);

    $row = $schema->parseRow("ABC  0001212052312052023");
    self::assertSame("ABC", $row["string"]);
    self::assertSame(12, $row["number"]);
    self::assertSame("12/05/2023", $row["date6"]);
    self::assertSame("12/05/2023", $row["date8"]);

    # si nombre ou date invalides, ne pas modifier
    $row = $schema->parseRow("  ABC00A1212AB2312AB2023");
    self::assertSame("  ABC", $row["string"]);
    self::assertSame("00A12", $row["number"]);
    self::assertSame("12AB23", $row["date6"]);
    self::assertSame("12AB2023", $row["date8"]);
  }

  function testFormat() {
    $schema = $this->schema;

    self::assertSame("                        ", $schema->formatRow([
      "string" => null,
      "number" => null,
      "date6" => null,
      "date8" => null,
    ]));
    self::assertSame("                        ", $schema->formatRow([
      "string" => false,
      "number" => false,
      "date6" => false,
      "date8" => false,
    ]));
    self::assertSame("                        ", $schema->formatRow([
      "string" => "",
      "number" => "",
      "date6" => "",
      "date8" => "",
    ]));
    self::assertSame("ABC  0001212052312052023", $schema->formatRow([
      "string" => "ABC",
      "number" => 12,
      "date6" => "12/05/2023",
      "date8" => "12/05/2023",
    ]));
    self::assertSame("  ABC00A1212AB2312AB2023", $schema->formatRow([
      "string" => "  ABC",
      "number" => "A12",
      "date6" => "12AB23",
      "date8" => "12AB2023",
    ]));
    self::assertSame("ABCDE1234512AB2312AB2023", $schema->formatRow([
      "string" => "ABCDEFGHIJ",
      "number" => 123456789,
      "date6" => "12AB23XYZT",
      "date8" => "12AB2023XYZT",
    ]));

  }
}
