<?php
namespace nur\sery\wip\schema\_assoc;

use nulib\ext\yaml;
use nulib\tests\TestCase;
use nur\sery\wip\schema\_scalar\ScalarSchemaTest;

class AssocSchemaTest extends TestCase {
  const NULL_SCHEMA = [
    "" => ["assoc"],
    "schema" => null,
    "type" => [null],
    "default" => null,
    "title" => null,
    "required" => false,
    "nullable" => true,
    "desc" => null,
    "analyzer_func" => null,
    "extractor_func" => null,
    "parser_func" => null,
    "normalizer_func" => null,
    "messages" => null,
    "formatter_func" => null,
    "format" => null,
    "name" => null,
    "pkey" => null,
    "header" => null,
    "computed" => null,
  ];

  static function schema(array $definition, array $keyDefinitions): array {
    $definition = array_merge(self::NULL_SCHEMA, $definition, ["schema" => []]);
    foreach ($keyDefinitions as $key => $keydef) {
      $definition["schema"][$key] = array_merge(ScalarSchemaTest::NULL_SCHEMA, $keydef);
    }; unset($subdef);
    return $definition;
  }

  function testNormalize() {
    self::assertSame(self::schema([
      "type" => ["array"], "nullable" => true,
    ], [
      "a" => [
        "type" => ["string"], "nullable" => false,
        "name" => "a", "pkey" => "a", "header" => "a",
      ],
    ]), AssocSchema::normalize(["a" => "string"]));

    self::assertSame(self::schema([
      "type" => ["array"], "nullable" => true,
    ], [
      "a" => [
        "type" => ["string"], "nullable" => false,
        "name" => "a", "pkey" => "a", "header" => "a",
      ],
      "b" => [
        "type" => ["int"], "nullable" => false,
        "name" => "b", "pkey" => "b", "header" => "b",
      ],
      "c" => [
        "type" => ["bool"], "nullable" => false,
        "name" => "c", "pkey" => "c", "header" => "c",
      ],
    ]), AssocSchema::normalize([
      "a" => "string",
      "b" => "int",
      "c" => "bool",
    ]));
  }

  function testConstructor() {
    $schema = new AssocSchema([
      "a" => "string",
      "b" => "int",
      "c" => "bool",
    ]);
    self::assertSame(self::schema([
      "type" => ["array"], "nullable" => true,
    ], [
      "a" => [
        "type" => ["string"], "nullable" => false,
        "name" => "a", "pkey" => "a", "header" => "a",
      ],
      "b" => [
        "type" => ["int"], "nullable" => false,
        "name" => "b", "pkey" => "b", "header" => "b",
      ],
      "c" => [
        "type" => ["bool"], "nullable" => false,
        "name" => "c", "pkey" => "c", "header" => "c",
      ],
    ]), $schema->getDefinition());
    yaml::dump($schema->getDefinition());
  }
}