204 lines
5.4 KiB
PHP
204 lines
5.4 KiB
PHP
<?php
|
|
namespace nur\sery\schema;
|
|
|
|
use Exception;
|
|
use nulib\tests\TestCase;
|
|
use nur\sery\schema\_scalar\ScalarValue;
|
|
|
|
class schemaTest extends TestCase {
|
|
function testInt() {
|
|
/** @var ScalarValue $intv */
|
|
Schema::nv($intv, $int, null, $schema, "int");
|
|
$intvSetter = function($value) use($intv) {
|
|
return function() use($intv, $value) {
|
|
$intv->set($value);
|
|
};
|
|
};
|
|
|
|
self::assertException(Exception::class, $intvSetter(null));
|
|
self::assertException(Exception::class, $intvSetter(""));
|
|
self::assertException(Exception::class, $intvSetter(" "));
|
|
|
|
$intv->set(12);
|
|
self::assertSame(12, $intv->get());
|
|
self::assertSame(12, $int);
|
|
self::assertSame("12", $intv->format());
|
|
self::assertSame("0012", $intv->format("%04u"));
|
|
|
|
$intv->set("12");
|
|
self::assertSame(12, $intv->get());
|
|
|
|
$intv->set(" 12 ");
|
|
self::assertSame(12, $intv->get());
|
|
|
|
$intv->set(true);
|
|
self::assertSame(1, $intv->get());
|
|
|
|
// valeur non requise donc retourne null
|
|
$intv->set(false);
|
|
self::assertNull($intv->get());
|
|
|
|
self::assertException(Exception::class, $intvSetter("a"));
|
|
self::assertException(Exception::class, $intvSetter([]));
|
|
self::assertException(Exception::class, $intvSetter(["a"]));
|
|
}
|
|
|
|
function testRequiredInt() {
|
|
/** @var ScalarValue $intv */
|
|
Schema::nv($intv, $int, null, $schema, [
|
|
"int", null,
|
|
"required" => true,
|
|
]);
|
|
$intvSetter = function($value) use($intv) {
|
|
return function() use($intv, $value) {
|
|
$intv->set($value);
|
|
};
|
|
};
|
|
|
|
self::assertException(Exception::class, $intvSetter(null));
|
|
self::assertException(Exception::class, $intvSetter(""));
|
|
self::assertException(Exception::class, $intvSetter(" "));
|
|
|
|
$intv->set(12);
|
|
self::assertSame(12, $intv->get());
|
|
self::assertSame(12, $int);
|
|
self::assertSame("12", $intv->format());
|
|
self::assertSame("0012", $intv->format("%04u"));
|
|
|
|
$intv->set("12");
|
|
self::assertSame(12, $intv->get());
|
|
|
|
$intv->set(" 12 ");
|
|
self::assertSame(12, $intv->get());
|
|
|
|
$intv->set(true);
|
|
self::assertSame(1, $intv->get());
|
|
|
|
// valeur requise donc lance une exception
|
|
self::assertException(Exception::class, $intvSetter(false));
|
|
|
|
self::assertException(Exception::class, $intvSetter("a"));
|
|
self::assertException(Exception::class, $intvSetter([]));
|
|
self::assertException(Exception::class, $intvSetter(["a"]));
|
|
}
|
|
|
|
function testNint() {
|
|
/** @var ScalarValue $intv */
|
|
Schema::nv($intv, $int, null, $schema, "?int");
|
|
$intvSetter = 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("%04u"));
|
|
|
|
$intv->set("12");
|
|
self::assertSame(12, $int);
|
|
|
|
$intv->set(" 12 ");
|
|
self::assertSame(12, $int);
|
|
|
|
$intv->set(true);
|
|
self::assertSame(1, $int);
|
|
|
|
// valeur non requise donc retourne null
|
|
$intv->set(false);
|
|
self::assertNull($intv->get());
|
|
|
|
self::assertException(Exception::class, $intvSetter("a"));
|
|
self::assertException(Exception::class, $intvSetter([]));
|
|
self::assertException(Exception::class, $intvSetter(["a"]));
|
|
}
|
|
|
|
function testRequiredNint() {
|
|
/** @var ScalarValue $intv */
|
|
Schema::nv($intv, $int, null, $schema, [
|
|
"?int", null,
|
|
"required" => true,
|
|
]);
|
|
$intvSetter = 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("%04u"));
|
|
|
|
$intv->set("12");
|
|
self::assertSame(12, $int);
|
|
|
|
$intv->set(" 12 ");
|
|
self::assertSame(12, $int);
|
|
|
|
$intv->set(true);
|
|
self::assertSame(1, $int);
|
|
|
|
// valeur requise donc lance une exception
|
|
self::assertException(Exception::class, $intvSetter(false));
|
|
|
|
self::assertException(Exception::class, $intvSetter("a"));
|
|
self::assertException(Exception::class, $intvSetter([]));
|
|
self::assertException(Exception::class, $intvSetter(["a"]));
|
|
}
|
|
|
|
function testUnionTypes() {
|
|
## l'ordre des types doit être respecté
|
|
|
|
# string puis int
|
|
/** @var ScalarValue $siv */
|
|
Schema::nv($siv, $si, null, $sis, "string|int");
|
|
|
|
$siv->set("12");
|
|
self::assertSame("12", $si);
|
|
$siv->set(12);
|
|
self::assertSame(12, $si);
|
|
|
|
# int puis string
|
|
Schema::nv($isv, $is, null, $iss, "int|string");
|
|
|
|
$isv->set("12");
|
|
self::assertSame("12", $is);
|
|
$isv->set(12);
|
|
self::assertSame(12, $is);
|
|
}
|
|
}
|