nur-sery/tests/schema/_scalar/ScalarSchemaTest.php

65 lines
2.3 KiB
PHP
Raw Normal View History

2023-11-24 22:36:33 +04:00
<?php
2023-12-28 19:33:13 +04:00
namespace nur\sery\schema\_scalar;
2023-11-24 22:36:33 +04:00
use nulib\tests\TestCase;
2024-08-17 22:52:51 +04:00
use nur\sery\wip\schema\_scalar\ScalarSchema;
use nur\sery\wip\schema\SchemaException;
2023-11-24 22:36:33 +04:00
class ScalarSchemaTest extends TestCase {
const NULL_SCHEMA = [
"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,
"" => ["scalar"],
"name" => null,
2023-11-27 22:39:35 +04:00
"pkey" => null,
2023-11-25 10:04:24 +04:00
"header" => null,
"composite" => null,
2023-11-24 22:36:33 +04:00
];
static function schema(array $schema): array {
return array_merge(self::NULL_SCHEMA, $schema);
}
function testNormalize() {
self::assertSame(self::NULL_SCHEMA, ScalarSchema::normalize(null));
self::assertSame(self::NULL_SCHEMA, ScalarSchema::normalize([]));
self::assertSame(self::NULL_SCHEMA, ScalarSchema::normalize([null]));
self::assertException(SchemaException::class, function () {
ScalarSchema::normalize([[]]);
});
self::assertException(SchemaException::class, function () {
ScalarSchema::normalize([[null]]);
});
$string = self::schema(["type" => ["string"], "nullable" => false]);
self::assertSame($string, ScalarSchema::normalize("string"));
self::assertSame($string, ScalarSchema::normalize(["string"]));
$nstring = self::schema(["type" => ["string"]]);
self::assertSame($nstring, ScalarSchema::normalize(["?string"]));
self::assertSame($nstring, ScalarSchema::normalize(["?string|null"]));
self::assertSame($nstring, ScalarSchema::normalize(["string|null"]));
self::assertSame($nstring, ScalarSchema::normalize([["?string", "null"]]));
self::assertSame($nstring, ScalarSchema::normalize([["string", "null"]]));
self::assertSame($nstring, ScalarSchema::normalize([["string", null]]));
$key = self::schema(["type" => ["string", "int"], "nullable" => false]);
2023-12-28 19:33:13 +04:00
self::assertSame($key, ScalarSchema::normalize("string|int"));
2023-11-24 22:36:33 +04:00
$nkey = self::schema(["type" => ["string", "int"], "nullable" => true]);
2023-12-28 19:33:13 +04:00
self::assertSame($nkey, ScalarSchema::normalize("?string|int"));
self::assertSame($nkey, ScalarSchema::normalize("string|?int"));
2023-11-24 22:36:33 +04:00
}
}