modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2023-12-28 23:53:24 +04:00
parent 6266d3020d
commit afb6581b06
10 changed files with 716 additions and 212 deletions

View File

@ -1,12 +1,127 @@
<?php
namespace nur\sery\schema\types;
abstract class tbool implements IType {
use nulib\cl;
use nulib\ValueException;
use nur\sery\schema\_scalar\ScalarResult;
use nur\sery\schema\_scalar\ScalarSchema;
use nur\sery\schema\input\Input;
use nur\sery\schema\Result;
use nur\sery\schema\Schema;
class tbool implements IType {
/** liste de valeurs chaines à considérer comme 'OUI' */
const YES_VALUES = [
# IMPORTANT: ordonner par taille décroissante pour compatibilité avec parse()
"true", "vrai", "yes", "oui",
"t", "v", "y", "o", "1",
];
/** liste de valeurs chaines à considérer comme 'NON' */
const NO_VALUES = [
# IMPORTANT: ordonner par taille décroissante pour compatibilité avec parse()
"false", "faux", "non", "no",
"f", "n", "0",
];
/** Vérifier si $value est 'OUI' */
static final function is_yes(?string $value): bool {
if ($value === null) return false;
$value = strtolower(trim($value));
if (in_array($value, self::YES_VALUES, true)) return true;
// n'importe quelle valeur numérique
if (is_numeric($value)) return $value != 0;
return false;
}
/** Vérifier si $value est 'NON' */
static final function is_no(?string $value): bool {
if ($value === null) return true;
$value = strtolower(trim($value));
return in_array($value, self::NO_VALUES, true);
}
static function ensure_bool(&$bool): void {
if (is_string($bool)) {
if (self::is_yes($bool)) $bool = true;
elseif (self::is_no($bool)) $bool = false;
}
if (!is_bool($bool)) $bool = boolval($bool);
}
static function ensure_nbool(&$bool): void {
if ($bool !== null) self::ensure_bool($bool);
}
function isAvailable(Input $input, $destKey): bool {
return $input->isAvailable($destKey);
}
function isNull($value): bool {
return $value === null || (is_string($value) && trim($value) === "");
}
function isValid($value, ?bool &$normalized=null): bool {
if (is_string($value)) {
$value = trim($value);
$valid = self::is_yes($value) || self::is_no($value);
} else {
$valid = is_scalar($value);
}
$normalized = is_bool($value);
return $valid;
}
/**
* @var ScalarResult $result
* @var ScalarSchema $schema
*/
function verifix(&$value, Result &$result, Schema $schema): bool {
if (is_bool($value)) {
$result->setNormalized();
return false;
} elseif (is_string($value)) {
$bool = trim($value);
if (self::is_yes($bool)) $value = true;
elseif (self::is_no($bool)) $value = false;
else return $result->setInvalid($value, $schema);
} elseif (is_scalar($value)) {
$value = boolval($value);
} else {
return $result->setInvalid($value, $schema);
}
$result->setValid();
return true;
}
const OUINON_FORMAT = ["Oui", "Non", false];
const OUINONNULL_FORMAT = ["Oui", "Non", ""];
const ON_FORMAT = ["O", "N", false];
const ONN_FORMAT = ["O", "N", ""];
const XN_FORMAT = ["X", "", false];
const OZ_FORMAT = ["1", "", false];
const FORMATS = [
"ouinon" => self::OUINON_FORMAT,
"ouinonnull" => self::OUINONNULL_FORMAT,
"on" => self::ON_FORMAT,
"onn" => self::ONN_FORMAT,
"xn" => self::XN_FORMAT,
"oz" => self::OZ_FORMAT,
];
const DEFAULT_FORMAT = self::OUINON_FORMAT;
function format($value, $format=null): string {
if ($format === null) $format = static::DEFAULT_FORMAT;
if (is_string($format)) {
$oformat = $format;
$format = cl::get(self::FORMATS, strtolower($oformat));
if ($format === null) throw ValueException::invalid_kind($oformat, "format");
}
if ($value === null) {
$null = $format[2];
if ($null !== false) return $null;
}
return $value? $format[0]: $format[1];
}
}

View File

@ -1,5 +1,59 @@
<?php
namespace nur\sery\schema\types;
abstract class tfloat implements IType {
use nur\sery\schema\_scalar\ScalarResult;
use nur\sery\schema\_scalar\ScalarSchema;
use nur\sery\schema\input\Input;
use nur\sery\schema\Result;
use nur\sery\schema\Schema;
class tfloat implements IType {
static function ensure_float(&$float): void {
if (!is_float($float)) $float = floatval($float);
}
static function ensure_nfloat(&$float): void {
if ($float !== null) self::ensure_float($float);
}
function isAvailable(Input $input, $destKey): bool {
return $input->isAvailable($destKey) && $input->get($destKey) !== false;
}
function isNull($value): bool {
return $value === null || (is_string($value) && trim($value) === "");
}
function isValid($value, ?bool &$normalized=null): bool {
if (is_string($value)) $valid = is_numeric(trim($value));
else $valid = is_scalar($value);
$normalized = is_float($value);
return $valid;
}
/**
* @var ScalarResult $result
* @var ScalarSchema $schema
*/
function verifix(&$value, Result &$result, Schema $schema): bool {
if (is_float($value)) {
$result->setNormalized();
return false;
} elseif (is_string($value)) {
$float = trim($value);
if (is_numeric($float)) $value = floatval($float);
else return $result->setInvalid($value, $schema);
} elseif (is_scalar($value)) {
$value = floatval($value);
} else {
return $result->setInvalid($value, $schema);
}
$result->setValid();
return true;
}
function format($value, $format=null): string {
if ($format !== null) return sprintf($format, $value);
else return strval($value);
}
}

View File

@ -27,11 +27,8 @@ class tint implements IType {
const INT_PATTERN = '/^[-+]?[0-9]+(?:\.[0-9]*)?$/';
function isValid($value, ?bool &$normalized=null): bool {
if (is_string($value)) {
$valid = preg_match(self::INT_PATTERN, trim($value));
} else {
$valid = is_scalar($value);
}
if (is_string($value)) $valid = is_numeric(trim($value));
else $valid = is_scalar($value);
$normalized = is_int($value);
return $valid;
}
@ -46,7 +43,7 @@ class tint implements IType {
return false;
} elseif (is_string($value)) {
$int = trim($value);
if (preg_match(self::INT_PATTERN, $int)) $value = intval($int);
if (is_numeric($int)) $value = intval($int);
else return $result->setInvalid($value, $schema);
} elseif (is_scalar($value)) {
$value = intval($value);

View File

@ -48,6 +48,6 @@ class tstring implements IType {
}
function format($value, $format=null): string {
return $value;
return strval($value);
}
}

View File

@ -1,203 +0,0 @@
<?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);
}
}

View File

@ -0,0 +1,111 @@
<?php
namespace nur\sery\schema\types;
use Exception;
use nulib\tests\TestCase;
use nur\sery\schema\_scalar\ScalarValue;
use nur\sery\schema\Schema;
class boolTest extends TestCase {
function commonTests($destv, &$dest, callable $destvSetter): void {
$destv->set(true);
self::assertSame(true, $destv->get());
self::assertSame(true, $dest);
self::assertSame("Oui", $destv->format());
self::assertSame("Oui", $destv->format("OuiNonNull"));
self::assertSame("O", $destv->format("ON"));
self::assertSame("O", $destv->format("ONN"));
$destv->set(false);
self::assertSame(false, $destv->get());
self::assertSame(false, $dest);
self::assertSame("Non", $destv->format());
self::assertSame("Non", $destv->format("OuiNonNull"));
self::assertSame("N", $destv->format("ON"));
self::assertSame("N", $destv->format("ONN"));
$destv->set("yes");
self::assertSame(true, $destv->get());
$destv->set(" yes ");
self::assertSame(true, $destv->get());
$destv->set("12");
self::assertSame(true, $destv->get());
$destv->set(12);
self::assertSame(true, $destv->get());
$destv->set("no");
self::assertSame(false, $destv->get());
$destv->set(" no ");
self::assertSame(false, $destv->get());
$destv->set("0");
self::assertSame(false, $destv->get());
$destv->set(0);
self::assertSame(false, $destv->get());
$destv->set(12.34);
self::assertSame(true, $destv->get());
self::assertException(Exception::class, $destvSetter("a"));
self::assertException(Exception::class, $destvSetter([]));
self::assertException(Exception::class, $destvSetter(["a"]));
}
function testBool() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, "bool");
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
};
};
self::assertException(Exception::class, $destvSetter(null));
self::assertException(Exception::class, $destvSetter(""));
self::assertException(Exception::class, $destvSetter(" "));
$this->commonTests($destv, $dest, $destvSetter);
}
function testNbool() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, "?bool");
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
};
};
$destv->set(null);
self::assertNull($destv->get());
self::assertNull($dest);
self::assertSame("Non", $destv->format());
self::assertSame("", $destv->format("OuiNonNull"));
self::assertSame("N", $destv->format("ON"));
self::assertSame("", $destv->format("ONN"));
$destv->set("");
self::assertNull($destv->get());
self::assertNull($dest);
self::assertSame("Non", $destv->format());
self::assertSame("", $destv->format("OuiNonNull"));
self::assertSame("N", $destv->format("ON"));
self::assertSame("", $destv->format("ONN"));
$destv->set(" ");
self::assertNull($destv->get());
self::assertNull($dest);
self::assertSame("Non", $destv->format());
self::assertSame("", $destv->format("OuiNonNull"));
self::assertSame("N", $destv->format("ON"));
self::assertSame("", $destv->format("ONN"));
$this->commonTests($destv, $dest, $destvSetter);
}
}

View File

@ -0,0 +1,139 @@
<?php
namespace nur\sery\schema\types;
use Exception;
use nulib\tests\TestCase;
use nur\sery\schema\_scalar\ScalarValue;
use nur\sery\schema\Schema;
class floatTest extends TestCase {
function commonTests($destv, &$dest, callable $destvSetter): void {
$destv->set(12);
self::assertSame(12.0, $destv->get());
self::assertSame(12.0, $dest);
self::assertSame("12", $destv->format());
self::assertSame("0012", $destv->format("%04u"));
$destv->set("12");
self::assertSame(12.0, $destv->get());
$destv->set(" 12 ");
self::assertSame(12.0, $destv->get());
$destv->set(12.34);
self::assertSame(12.34, $destv->get());
$destv->set(true);
self::assertSame(1.0, $destv->get());
self::assertException(Exception::class, $destvSetter("a"));
self::assertException(Exception::class, $destvSetter([]));
self::assertException(Exception::class, $destvSetter(["a"]));
}
function testFloat() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, "float");
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
};
};
self::assertException(Exception::class, $destvSetter(null));
self::assertException(Exception::class, $destvSetter(""));
self::assertException(Exception::class, $destvSetter(" "));
// valeur non requise donc retourne null
$destv->set(false);
self::assertNull($destv->get());
$this->commonTests($destv, $dest, $destvSetter);
}
function testRequiredFloat() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, [
"float", null,
"required" => true,
]);
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
};
};
self::assertException(Exception::class, $destvSetter(null));
self::assertException(Exception::class, $destvSetter(""));
self::assertException(Exception::class, $destvSetter(" "));
// valeur requise donc lance une exception
self::assertException(Exception::class, $destvSetter(false));
$this->commonTests($destv, $dest, $destvSetter);
}
function testNfloat() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, "?float");
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
};
};
$destv->set(null);
self::assertNull($destv->get());
self::assertNull($dest);
self::assertSame("", $destv->format());
$destv->set("");
self::assertNull($destv->get());
self::assertNull($dest);
self::assertSame("", $destv->format());
$destv->set(" ");
self::assertNull($destv->get());
self::assertNull($dest);
self::assertSame("", $destv->format());
// valeur non requise donc retourne null
$destv->set(false);
self::assertNull($destv->get());
$this->commonTests($destv, $dest, $destvSetter);
}
function testRequiredNfloat() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, [
"?float", null,
"required" => true,
]);
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
};
};
$destv->set(null);
self::assertNull($destv->get());
self::assertNull($dest);
self::assertSame("", $destv->format());
$destv->set("");
self::assertNull($destv->get());
self::assertNull($dest);
self::assertSame("", $destv->format());
$destv->set(" ");
self::assertNull($destv->get());
self::assertNull($dest);
self::assertSame("", $destv->format());
// valeur requise donc lance une exception
self::assertException(Exception::class, $destvSetter(false));
$this->commonTests($destv, $dest, $destvSetter);
}
}

View File

@ -0,0 +1,139 @@
<?php
namespace nur\sery\schema\types;
use Exception;
use nulib\tests\TestCase;
use nur\sery\schema\_scalar\ScalarValue;
use nur\sery\schema\Schema;
class intTest extends TestCase {
function commonTests($destv, &$dest, callable $destvSetter): void {
$destv->set(12);
self::assertSame(12, $destv->get());
self::assertSame(12, $dest);
self::assertSame("12", $destv->format());
self::assertSame("0012", $destv->format("%04u"));
$destv->set("12");
self::assertSame(12, $destv->get());
$destv->set(" 12 ");
self::assertSame(12, $destv->get());
$destv->set(12.34);
self::assertSame(12, $destv->get());
$destv->set(true);
self::assertSame(1, $destv->get());
self::assertException(Exception::class, $destvSetter("a"));
self::assertException(Exception::class, $destvSetter([]));
self::assertException(Exception::class, $destvSetter(["a"]));
}
function testInt() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, "int");
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
};
};
self::assertException(Exception::class, $destvSetter(null));
self::assertException(Exception::class, $destvSetter(""));
self::assertException(Exception::class, $destvSetter(" "));
// valeur non requise donc retourne null
$destv->set(false);
self::assertNull($destv->get());
$this->commonTests($destv, $dest, $destvSetter);
}
function testRequiredInt() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, [
"int", null,
"required" => true,
]);
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
};
};
self::assertException(Exception::class, $destvSetter(null));
self::assertException(Exception::class, $destvSetter(""));
self::assertException(Exception::class, $destvSetter(" "));
// valeur requise donc lance une exception
self::assertException(Exception::class, $destvSetter(false));
$this->commonTests($destv, $dest, $destvSetter);
}
function testNint() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, "?int");
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
};
};
$destv->set(null);
self::assertNull($destv->get());
self::assertNull($dest);
self::assertSame("", $destv->format());
$destv->set("");
self::assertNull($destv->get());
self::assertNull($dest);
self::assertSame("", $destv->format());
$destv->set(" ");
self::assertNull($destv->get());
self::assertNull($dest);
self::assertSame("", $destv->format());
// valeur non requise donc retourne null
$destv->set(false);
self::assertNull($destv->get());
$this->commonTests($destv, $dest, $destvSetter);
}
function testRequiredNint() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, [
"?int", null,
"required" => true,
]);
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
};
};
$destv->set(null);
self::assertNull($destv->get());
self::assertNull($dest);
self::assertSame("", $destv->format());
$destv->set("");
self::assertNull($destv->get());
self::assertNull($dest);
self::assertSame("", $destv->format());
$destv->set(" ");
self::assertNull($destv->get());
self::assertNull($dest);
self::assertSame("", $destv->format());
// valeur requise donc lance une exception
self::assertException(Exception::class, $destvSetter(false));
$this->commonTests($destv, $dest, $destvSetter);
}
}

View File

@ -0,0 +1,123 @@
<?php
namespace nur\sery\schema\types;
use Exception;
use nulib\tests\TestCase;
use nur\sery\schema\_scalar\ScalarValue;
use nur\sery\schema\Schema;
class strTest extends TestCase {
function commonTests($destv, &$dest, callable $destvSetter): void {
$destv->set("");
self::assertSame("", $destv->get());
self::assertSame("", $dest);
$destv->set(" ");
self::assertSame(" ", $destv->get());
self::assertSame(" ", $dest);
$destv->set("a");
self::assertSame("a", $destv->get());
self::assertSame("a", $dest);
$destv->set("12");
self::assertSame("12", $destv->get());
$destv->set(" 12 ");
self::assertSame(" 12 ", $destv->get());
$destv->set(12);
self::assertSame("12", $destv->get());
$destv->set(12.34);
self::assertSame("12.34", $destv->get());
$destv->set(true);
self::assertSame("1", $destv->get());
self::assertException(Exception::class, $destvSetter([]));
self::assertException(Exception::class, $destvSetter(["a"]));
}
function testStr() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, "string");
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
};
};
self::assertException(Exception::class, $destvSetter(null));
// valeur non requise donc retourne null
$destv->set(false);
self::assertNull($destv->get());
$this->commonTests($destv, $dest, $destvSetter);
}
function testRequiredStr() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, [
"string", null,
"required" => true,
]);
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
};
};
self::assertException(Exception::class, $destvSetter(null));
// valeur requise donc lance une exception
self::assertException(Exception::class, $destvSetter(false));
$this->commonTests($destv, $dest, $destvSetter);
}
function testNstr() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, "?string");
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
};
};
$destv->set(null);
self::assertNull($destv->get());
self::assertNull($dest);
self::assertSame("", $destv->format());
// valeur non requise donc retourne null
$destv->set(false);
self::assertNull($destv->get());
$this->commonTests($destv, $dest, $destvSetter);
}
function testRequiredNstr() {
/** @var ScalarValue $destv */
Schema::nv($destv, $dest, null, $schema, [
"?string", null,
"required" => true,
]);
$destvSetter = function($value) use($destv) {
return function() use($destv, $value) {
$destv->set($value);
};
};
$destv->set(null);
self::assertNull($destv->get());
self::assertNull($dest);
self::assertSame("", $destv->format());
// valeur requise donc lance une exception
self::assertException(Exception::class, $destvSetter(false));
$this->commonTests($destv, $dest, $destvSetter);
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace nur\sery\schema\types;
use nulib\tests\TestCase;
use nur\sery\schema\_scalar\ScalarValue;
use nur\sery\schema\Schema;
class unionTest extends TestCase {
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);
}
}