<?php
namespace nur\data\types;

use nur\b\date\Time;
use nur\b\ValueException;
use nur\t\TestCase;

class TimeTypeTest extends TestCase {
  function testParse() {
    $t = new TimeType();
    
    $input = "";
    self::assertSame(false, $t->parse($input));
    self::assertSame("", $input);

    $input = "  ";
    self::assertSame(false, $t->parse($input));
    self::assertSame("  ", $input);

    $input = "whatever";
    self::assertSame(false, $t->parse($input));
    self::assertSame("whatever", $input);

    $input = "0  ";
    self::assertSame([0, 0, 0], $t->parse($input));
    self::assertSame("", $input);

    $input = "15  ";
    self::assertSame([15, 0, 0], $t->parse($input));
    self::assertSame("", $input);

    $input = "15 : 32 ";
    self::assertSame([15, 32, 0], $t->parse($input));
    self::assertSame("", $input);

    $input = "15 : 32 : 54 ";
    self::assertSame([15, 32, 54], $t->parse($input));
    self::assertSame("", $input);

    $input = "15 : 32 : 54 xxx";
    self::assertSame([15, 32, 54], $t->parse($input));
    self::assertSame("xxx", $input);

    $input = "15 h ";
    self::assertSame([15, 0, 0], $t->parse($input));
    self::assertSame("", $input);

    $input = "15 h 32 ";
    self::assertSame([15, 32, 0], $t->parse($input));
    self::assertSame("", $input);

    $input = "15 h 32 . 54 ";
    self::assertSame([15, 32, 54], $t->parse($input));
    self::assertSame("", $input);

    $input = "15 . 32 : 54 xxx";
    self::assertSame([15, 32, 54], $t->parse($input));
    self::assertSame("xxx", $input);
  }

  function testVerifix() {
    $t = new TimeType();

    $value = null; $t->verifix($value, $result);
    self::assertSame(null, $value);
    self::assertSame(["valid" => true, "value" => null, "value_desc" => null,
      "error_code" => null, "error" => false, "exception" => null,
      "orig" => null, "orig_desc" => null, "parsed" => false, "remains" => false], $result);

    $value = false; $t->verifix($value, $result);
    self::assertSame(Time::undef(), $value);
    self::assertSame(["valid" => true, "value" => Time::undef(), "value_desc" => null,
      "error_code" => null, "error" => false, "exception" => null,
      "orig" => false, "orig_desc" => null, "parsed" => false, "remains" => false], $result);

    $value = ""; $t->verifix($value, $result);
    self::assertSame(Time::undef(), $value);
    self::assertSame(["valid" => true, "value" => Time::undef(), "value_desc" => null,
      "error_code" => null, "error" => false, "exception" => null,
      "orig" => "", "orig_desc" => null, "parsed" => false, "remains" => false], $result);

    $value = "15"; $t->verifix($value, $result);
    self::assertInstanceOf(Time::class, $value);
    self::assertSame("15:00:00", strval($value));
    self::assertSame(["valid" => true, "value" => $value, "value_desc" => null,
      "error_code" => null, "error" => false, "exception" => null,
      "orig" => "15", "orig_desc" => null, "parsed" => "15", "remains" => ""], $result);

    $value = "15h"; $t->verifix($value, $result);
    self::assertInstanceOf(Time::class, $value);
    self::assertSame("15:00:00", strval($value));
    self::assertSame(["valid" => true, "value" => $value, "value_desc" => null,
      "error_code" => null, "error" => false, "exception" => null,
      "orig" => "15h", "orig_desc" => null, "parsed" => "15h", "remains" => ""], $result);

    $value = "15h32"; $t->verifix($value, $result);
    self::assertInstanceOf(Time::class, $value);
    self::assertSame("15:32:00", strval($value));
    self::assertSame(["valid" => true, "value" => $value, "value_desc" => null,
      "error_code" => null, "error" => false, "exception" => null,
      "orig" => "15h32", "orig_desc" => null, "parsed" => "15h32", "remains" => ""], $result);

    $value = "15:32:54"; $t->verifix($value, $result);
    self::assertInstanceOf(Time::class, $value);
    self::assertSame("15:32:54", strval($value));
    self::assertSame(["valid" => true, "value" => $value, "value_desc" => null,
      "error_code" => null, "error" => false, "exception" => null,
      "orig" => "15:32:54", "orig_desc" => null, "parsed" => "15:32:54", "remains" => ""], $result);
  }

  function testWith() {
    $t = new TimeType();

    self::assertSame(null, $t->with(null));
    self::assertSame(Time::undef(), $t->with(false));
    self::assertSame(Time::undef(), $t->with(""));
    self::assertSame("", strval($t->with(null)));
    self::assertSame("", strval($t->with(false)));
    self::assertSame("15:00:00", strval($t->with("15")));
    self::assertSame("15:00:00", strval($t->with("15h")));
    self::assertSame("15:00:00", strval($t->with("15:0")));
    self::assertSame("15:32:00", strval($t->with("15h32")));
    self::assertSame("15:32:54", strval($t->with("15:32:54")));

    self::assertException(ValueException::class, [$t, "with"], true);
    self::assertException(ValueException::class, [$t, "with"], "whatever");
    self::assertException(ValueException::class, [$t, "with"], 12);
    self::assertException(ValueException::class, [$t, "with"], 1.234);
  }
}
