107 lines
2.8 KiB
PHP
107 lines
2.8 KiB
PHP
<?php
|
|
namespace nur\sery\schema;
|
|
|
|
use Exception;
|
|
use nulib\tests\TestCase;
|
|
use nur\sery\schema\values\ScalarValue;
|
|
|
|
class schemaTest extends TestCase {
|
|
function testInt() {
|
|
Schema::ns($schema, "int");
|
|
/** @var ScalarValue $intv */
|
|
$schema->nv($intv, $int);
|
|
$f = function($value) use($intv) {
|
|
return function() use($intv, $value) {
|
|
$intv->set($value);
|
|
};
|
|
};
|
|
|
|
self::assertException(Exception::class, $f(null));
|
|
self::assertException(Exception::class, $f(""));
|
|
self::assertException(Exception::class, $f(" "));
|
|
|
|
$intv->set(12);
|
|
self::assertSame(12, $intv->get());
|
|
self::assertSame(12, $int);
|
|
self::assertSame("12", $intv->format());
|
|
self::assertSame("0012", $intv->format("%4u"));
|
|
|
|
$intv->set("12");
|
|
self::assertSame(12, $intv->get());
|
|
|
|
$intv->set(" 12 ");
|
|
self::assertSame(12, $intv->get());
|
|
|
|
self::assertException(Exception::class, $f(true));
|
|
self::assertException(Exception::class, $f(false));
|
|
self::assertException(Exception::class, $f("a"));
|
|
self::assertException(Exception::class, $f([]));
|
|
self::assertException(Exception::class, $f(["a"]));
|
|
}
|
|
|
|
function testNint() {
|
|
Schema::ns($schema, ["?int"]);
|
|
/** @var ScalarValue $intv */
|
|
$schema->nv($intv, $int);
|
|
$f = function($value) use($intv) {
|
|
return function() use($intv, $value) {
|
|
$intv->set($value);
|
|
};
|
|
};
|
|
|
|
$intv->set(null);
|
|
self::assertNull($intv->get());
|
|
self::assertNull($int);
|
|
self::assertSame("", $intv->format());
|
|
|
|
$intv->set("");
|
|
self::assertNull($intv->get());
|
|
self::assertNull($int);
|
|
self::assertSame("", $intv->format());
|
|
|
|
$intv->set(" ");
|
|
self::assertNull($intv->get());
|
|
self::assertNull($int);
|
|
self::assertSame("", $intv->format());
|
|
|
|
$intv->set(12);
|
|
self::assertSame(12, $intv->get());
|
|
self::assertSame(12, $int);
|
|
self::assertSame("12", $intv->format());
|
|
self::assertSame("0012", $intv->format("%4u"));
|
|
|
|
$intv->set("12");
|
|
self::assertSame(12, $int);
|
|
|
|
$intv->set(" 12 ");
|
|
self::assertSame(12, $int);
|
|
|
|
self::assertException(Exception::class, $f(true));
|
|
self::assertException(Exception::class, $f(false));
|
|
self::assertException(Exception::class, $f("a"));
|
|
self::assertException(Exception::class, $f([]));
|
|
self::assertException(Exception::class, $f(["a"]));
|
|
}
|
|
|
|
function testUnionTypes() {
|
|
# l'ordre des types est respecté
|
|
Schema::ns($schema, ["string|int"]);
|
|
/** @var ScalarValue $siv */
|
|
$schema->nv($siv, $si);
|
|
|
|
$siv->set("12");
|
|
self::assertSame("12", $si);
|
|
$siv->set(12);
|
|
self::assertSame(12, $si);
|
|
|
|
Schema::ns($schema, ["int|string"]);
|
|
/** @var ScalarValue $siv */
|
|
$schema->nv($siv, $si);
|
|
|
|
$siv->set("12");
|
|
self::assertSame(12, $si);
|
|
$siv->set(12);
|
|
self::assertSame(12, $si);
|
|
}
|
|
}
|