133 lines
3.4 KiB
PHP
133 lines
3.4 KiB
PHP
<?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",
|
|
"compute_func" => null,
|
|
"validate_func" => null,
|
|
"ensure_array" => false,
|
|
"ensure_keys" => true,
|
|
"ensure_order" => true,
|
|
],
|
|
"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_definition(["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_definition([
|
|
"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());
|
|
}
|
|
|
|
function testWrapper() {
|
|
$schema = new AssocSchema([
|
|
"a" => "string",
|
|
"b" => "int",
|
|
"c" => "bool",
|
|
]);
|
|
|
|
$array = ["c" => false, "a" => " string "];
|
|
$schema->getWrapper($array);
|
|
self::assertSame([
|
|
"a" => "string",
|
|
"b" => null,
|
|
"c" => false,
|
|
], $array);
|
|
|
|
$array = ["c" => false, "a" => " string "];
|
|
$schema->getWrapper($array, null, ["ensure_order" => false]);
|
|
self::assertSame([
|
|
"c" => false,
|
|
"a" => "string",
|
|
"b" => null,
|
|
], $array);
|
|
|
|
$array = ["a" => " string "];
|
|
$schema->getWrapper($array, null, ["ensure_keys" => false]);
|
|
self::assertSame([
|
|
"a" => "string",
|
|
], $array);
|
|
}
|
|
}
|