<?php
namespace nur\php;

use nur\data\types\Metadata;
use nur\t\TestCase;
use stdClass;

class AutogenTest extends TestCase {
  const SCHEMA1 = [
    "a" => ["?string"],
    "b" => ["string"],
    "c" => [["string"], "allow_null" => true],
    "d" => [["string"], "allow_null" => false],
  ];

  function test1() {
    $schema = self::SCHEMA1;
    $md = new Metadata($schema);
    self::assertSame("?string", $md->getType("a")->getPhpType());
    self::assertSame("string", $md->getType("b")->getPhpType());
    self::assertSame("?string", $md->getType("c")->getPhpType());
    self::assertSame("string", $md->getType("d")->getPhpType());
    self::assertSame([
      "getA(): ?string",
      "getB(): string",
      "getC(): ?string",
      "getD(): string",
    ], Autogen::auto_getters_methods($schema, null, stdClass::class));
  }

  const SCHEMA2 = [
    "a" => ["?array"],
    "b" => ["array"],
    "c" => [["array"], "allow_null" => true],
    "d" => [["array"], "allow_null" => false],
  ];

  function test2() {
    $schema = self::SCHEMA2;
    $md = new Metadata($schema);
    self::assertSame("?array", $md->getType("a")->getPhpType());
    self::assertSame("array", $md->getType("b")->getPhpType());
    self::assertSame("?array", $md->getType("c")->getPhpType());
    self::assertSame("array", $md->getType("d")->getPhpType());
    self::assertSame([
      "getA(): ?array",
      "getB(): array",
      "getC(): ?array",
      "getD(): array",
    ], Autogen::auto_getters_methods($schema, null, stdClass::class));
  }

  const SCHEMA3 = [
    "a" => ["?array[]"],
    "b" => ["array[]"],
    "c" => [["array[]"], "allow_null" => true],
    "d" => [["array[]"], "allow_null" => false],
  ];

  function test3() {
    $schema = self::SCHEMA3;
    $md = new Metadata($schema);
    self::assertSame("?array", $md->getType("a")->getPhpType());
    self::assertSame("array", $md->getType("b")->getPhpType());
    self::assertSame("?array", $md->getType("c")->getPhpType());
    self::assertSame("array", $md->getType("d")->getPhpType());
    self::assertSame([
      "getA(): ?array",
      "getB(): array",
      "getC(): ?array",
      "getD(): array",
    ], Autogen::auto_getters_methods($schema, null, stdClass::class));
  }
}
