<?php
namespace nur\data\types;

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

class STimeTypeTest extends TestCase {
  function testParse() {
    $t = new STimeType();

    $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 STimeType();

    $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(false, $value);
    self::assertSame(["valid" => true, "value" => false, "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(null, $value);
    self::assertSame(["valid" => true, "value" => null, "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::assertSame("15:00:00", $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::assertSame("15:00:00", $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::assertSame("15:32:00", $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::assertSame("15:32:54", $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 testTo_time() {
    self::assertSame(false, STimeType::to_time(false));
    self::assertSame(null, STimeType::to_time(null));
    self::assertSame(null, STimeType::to_time(""));
    self::assertSame("15:00:00", STimeType::to_time("15"));
    self::assertSame("15:00:00", STimeType::to_time("15h"));
    self::assertSame("15:00:00", STimeType::to_time("15:0"));
    self::assertSame("15:32:00", STimeType::to_time("15h32"));
    self::assertSame("15:32:54", STimeType::to_time("15:32:54"));

    self::assertException(ValueException::class, [STimeType::class, "to_time"], true);
    self::assertException(ValueException::class, [STimeType::class, "to_time"], "whatever");
    self::assertException(ValueException::class, [STimeType::class, "to_time"], 12);
    self::assertException(ValueException::class, [STimeType::class, "to_time"], 1.234);
  }
}
