354 lines
10 KiB
PHP
354 lines
10 KiB
PHP
<?php
|
|
namespace nulib\schema\_assoc;
|
|
|
|
use nulib\ext\yaml;
|
|
use nulib\tests\TestCase;
|
|
use nulib\ValueException;
|
|
use nulib\schema\_scalar\ScalarSchemaTest;
|
|
use nulib\schema\Schema;
|
|
|
|
class AssocSchemaTest extends TestCase {
|
|
const NULL_SCHEMA = [
|
|
"" => [
|
|
"assoc",
|
|
"compute_func" => null,
|
|
"validate_func" => null,
|
|
"ensure_array" => null,
|
|
"ensure_assoc" => null,
|
|
"ensure_keys" => null,
|
|
"ensure_order" => null,
|
|
],
|
|
"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 = ["a" => " string ", "b" => " 42 ", "c" => false];
|
|
$schema->getWrapper($array);
|
|
self::assertSame([
|
|
"a" => "string",
|
|
"b" => 42,
|
|
"c" => false,
|
|
], $array);
|
|
|
|
###########################################################################
|
|
$schema = new AssocSchema([
|
|
"a" => "string",
|
|
"b" => "int",
|
|
"c" => "bool",
|
|
]);
|
|
|
|
$array = ["a" => " string "];
|
|
$schema->getWrapper($array);
|
|
self::assertSame([
|
|
"a" => "string",
|
|
"b" => 0,
|
|
"c" => false,
|
|
], $array);
|
|
|
|
$array = ["c" => false, "a" => " string "];
|
|
$schema->getWrapper($array);
|
|
self::assertSame([
|
|
"a" => "string",
|
|
"b" => 0,
|
|
"c" => false,
|
|
], $array);
|
|
|
|
$array = ["a" => " string "];
|
|
$schema->getWrapper($array, null, ["ensure_order" => false]);
|
|
self::assertSame([
|
|
"a" => "string",
|
|
"b" => 0,
|
|
"c" => false,
|
|
], $array);
|
|
|
|
$array = ["c" => false, "a" => " string "];
|
|
$schema->getWrapper($array, null, ["ensure_order" => false]);
|
|
self::assertSame([
|
|
"c" => false,
|
|
"a" => "string",
|
|
"b" => 0,
|
|
], $array);
|
|
|
|
$array = ["a" => " string "];
|
|
$schema->getWrapper($array, null, ["ensure_keys" => false]);
|
|
self::assertSame([
|
|
"a" => "string",
|
|
], $array);
|
|
|
|
$array = ["c" => false, "a" => " string "];
|
|
$schema->getWrapper($array, null, ["ensure_keys" => false]);
|
|
self::assertSame([
|
|
"a" => "string",
|
|
"c" => false,
|
|
], $array);
|
|
}
|
|
|
|
const STRING_SCHEMA = [
|
|
"s" => "string",
|
|
"f" => "string",
|
|
"m" => "string",
|
|
];
|
|
|
|
const NSTRING_SCHEMA = [
|
|
"s" => "?string",
|
|
"f" => "?string",
|
|
"m" => "?string",
|
|
];
|
|
|
|
const RSTRING_SCHEMA = [
|
|
"s" => ["string", "required" => true],
|
|
"f" => ["string", "required" => true],
|
|
"m" => ["string", "required" => true],
|
|
];
|
|
|
|
const RNSTRING_SCHEMA = [
|
|
"s" => ["?string", "required" => true],
|
|
"f" => ["?string", "required" => true],
|
|
"m" => ["?string", "required" => true],
|
|
];
|
|
|
|
const STRINGS = ["s" => "string", "f" => false];
|
|
const NSTRINGS = ["s" => null, "f" => null];
|
|
|
|
function testString() {
|
|
/** @var AssocSchema $schema */
|
|
$schema = Schema::ns(self::STRING_SCHEMA);
|
|
|
|
$array = self::STRINGS;
|
|
$wrapper = $schema->getWrapper($array, null, ["throw" => false]);
|
|
self::assertSame(["s" => "string", "f" => false, "m" => ""], $array);
|
|
$result = $wrapper->getResult("s");
|
|
self::assertTrue($result->normalized);
|
|
$result = $wrapper->getResult("f");
|
|
self::assertTrue($result->present);
|
|
self::assertFalse($result->available);
|
|
$result = $wrapper->getResult("m");
|
|
self::assertFalse($result->present);
|
|
|
|
self::assertNotException(function() use ($schema) {
|
|
$array = self::STRINGS;
|
|
$schema->getWrapper($array);
|
|
});
|
|
|
|
$array = self::NSTRINGS;
|
|
$wrapper = $schema->getWrapper($array, null, ["throw" => false]);
|
|
self::assertSame(["s" => null, "f" => null, "m" => ""], $array);
|
|
$result = $wrapper->getResult("s");
|
|
self::assertFalse($result->valid);
|
|
self::assertSame("null", $result->messageKey);
|
|
$result = $wrapper->getResult("f");
|
|
self::assertFalse($result->valid);
|
|
self::assertSame("null", $result->messageKey);
|
|
$result = $wrapper->getResult("m");
|
|
self::assertFalse($result->present);
|
|
|
|
self::assertException(ValueException::class, function() use ($schema) {
|
|
$array = self::NSTRINGS;
|
|
$schema->getWrapper($array);
|
|
});
|
|
}
|
|
|
|
function testNstring() {
|
|
/** @var AssocSchema $schema */
|
|
$schema = Schema::ns(self::NSTRING_SCHEMA);
|
|
|
|
$array = self::STRINGS;
|
|
$wrapper = $schema->getWrapper($array, null, ["throw" => false]);
|
|
self::assertSame(["s" => "string", "f" => false, "m" => null], $array);
|
|
$result = $wrapper->getResult("s");
|
|
self::assertTrue($result->normalized);
|
|
$result = $wrapper->getResult("f");
|
|
self::assertTrue($result->present);
|
|
self::assertFalse($result->available);
|
|
$result = $wrapper->getResult("m");
|
|
self::assertFalse($result->present);
|
|
|
|
self::assertNotException(function() use ($schema) {
|
|
$array = self::STRINGS;
|
|
$schema->getWrapper($array);
|
|
});
|
|
|
|
$array = self::NSTRINGS;
|
|
$wrapper = $schema->getWrapper($array, null, ["throw" => false]);
|
|
self::assertSame(["s" => null, "f" => null, "m" => null], $array);
|
|
$result = $wrapper->getResult("s");
|
|
self::assertTrue($result->normalized);
|
|
$result = $wrapper->getResult("f");
|
|
self::assertTrue($result->normalized);
|
|
$result = $wrapper->getResult("m");
|
|
self::assertFalse($result->present);
|
|
|
|
self::assertNotException(function() use ($schema) {
|
|
$array = self::NSTRINGS;
|
|
$schema->getWrapper($array);
|
|
});
|
|
}
|
|
|
|
function testRstring() {
|
|
/** @var AssocSchema $schema */
|
|
$schema = Schema::ns(self::RSTRING_SCHEMA);
|
|
|
|
$array = self::STRINGS;
|
|
$wrapper = $schema->getWrapper($array, null, ["throw" => false]);
|
|
self::assertSame(["s" => "string", "f" => false, "m" => ""], $array);
|
|
$result = $wrapper->getResult("s");
|
|
self::assertTrue($result->normalized);
|
|
$result = $wrapper->getResult("f");
|
|
self::assertTrue($result->present);
|
|
self::assertFalse($result->available);
|
|
self::assertSame("unavailable", $result->messageKey);
|
|
$result = $wrapper->getResult("m");
|
|
self::assertFalse($result->present);
|
|
self::assertSame("missing", $result->messageKey);
|
|
|
|
self::assertException(ValueException::class, function() use ($schema) {
|
|
$array = self::STRINGS;
|
|
$schema->getWrapper($array);
|
|
});
|
|
|
|
$array = self::NSTRINGS;
|
|
$wrapper = $schema->getWrapper($array, null, ["throw" => false]);
|
|
self::assertSame(["s" => null, "f" => null, "m" => ""], $array);
|
|
$result = $wrapper->getResult("s");
|
|
self::assertFalse($result->valid);
|
|
self::assertSame("null", $result->messageKey);
|
|
$result = $wrapper->getResult("f");
|
|
self::assertFalse($result->valid);
|
|
self::assertSame("null", $result->messageKey);
|
|
$result = $wrapper->getResult("m");
|
|
self::assertFalse($result->present);
|
|
self::assertSame("missing", $result->messageKey);
|
|
|
|
self::assertException(ValueException::class, function() use ($schema) {
|
|
$array = self::NSTRINGS;
|
|
$schema->getWrapper($array);
|
|
});
|
|
}
|
|
|
|
function testRnstring() {
|
|
/** @var AssocSchema $schema */
|
|
$schema = Schema::ns(self::RNSTRING_SCHEMA);
|
|
|
|
$array = self::STRINGS;
|
|
$wrapper = $schema->getWrapper($array, null, ["throw" => false]);
|
|
self::assertSame(["s" => "string", "f" => false, "m" => null], $array);
|
|
$result = $wrapper->getResult("s");
|
|
self::assertTrue($result->normalized);
|
|
$result = $wrapper->getResult("f");
|
|
self::assertTrue($result->present);
|
|
self::assertFalse($result->available);
|
|
self::assertSame("unavailable", $result->messageKey);
|
|
$result = $wrapper->getResult("m");
|
|
self::assertFalse($result->present);
|
|
self::assertSame("missing", $result->messageKey);
|
|
|
|
self::assertException(ValueException::class, function() use ($schema) {
|
|
$array = self::STRINGS;
|
|
$schema->getWrapper($array);
|
|
});
|
|
|
|
$array = self::NSTRINGS;
|
|
$wrapper = $schema->getWrapper($array, null, ["throw" => false]);
|
|
self::assertSame(["s" => null, "f" => null, "m" => null], $array);
|
|
$result = $wrapper->getResult("s");
|
|
self::assertTrue($result->normalized);
|
|
$result = $wrapper->getResult("f");
|
|
self::assertTrue($result->normalized);
|
|
$result = $wrapper->getResult("m");
|
|
self::assertFalse($result->present);
|
|
self::assertSame("missing", $result->messageKey);
|
|
|
|
self::assertException(ValueException::class, function() use ($schema) {
|
|
$array = self::NSTRINGS;
|
|
$schema->getWrapper($array);
|
|
});
|
|
}
|
|
}
|