ajout nur-base/tests

This commit is contained in:
Jephté Clain 2023-12-03 22:11:29 +04:00
parent 34040ece3b
commit 7f05c7eba4
78 changed files with 6206 additions and 0 deletions

337
nur_tests/ATest.php Normal file
View File

@ -0,0 +1,337 @@
<?php
namespace nur;
use nur\impl\GBaseArray;
use nur\impl\GIteratableArray;
use nur\t\TestCase;
use stdClass;
class ATest extends TestCase {
function testEnsureAccess() {
$o = null;
$a =& A::ensure_access($o);
self::assertSame($a, $o);
self::assertSame([], $a);
$o = false;
$a =& A::ensure_access($o);
self::assertSame($a, $o);
self::assertSame([], $a);
$o = [];
$a =& A::ensure_access($o);
self::assertSame($a, $o);
self::assertSame([], $a);
$o = "any";
$a =& A::ensure_access($o);
self::assertSame($a, $o);
self::assertSame(["any"], $a);
$o = 123;
$a =& A::ensure_access($o);
self::assertSame($a, $o);
self::assertSame([123], $a);
$x = new stdClass();
$o = $x;
$a =& A::ensure_access($o);
self::assertSame($a, $o);
self::assertSame([$x], $a);
$o = new GBaseArray([]);
$a =& A::ensure_access($o);
self::assertNotSame($a, $o);
self::assertIsArray($a);
$a[] = "hello";
$a[] = "world";
self::assertSame(["hello", "world"], $o->array());
$o = new GIteratableArray([]);
$a =& A::ensure_access($o);
self::assertNotSame($a, $o);
self::assertIsArray($a);
$a[] = "hello";
$a[] = "world";
self::assertSame(["hello", "world"], $o->array());
}
function testEnsure_size() {
$template = null;
$array = $template; A::ensure_size($array, 0);
self::assertSame([], $array);
$array = $template; A::ensure_size($array, 1);
self::assertSame([null], $array);
$array = $template; A::ensure_size($array, 2);
self::assertSame([null, null], $array);
$template = [];
$array = $template; A::ensure_size($array, 0);
self::assertSame([], $array);
$array = $template; A::ensure_size($array, 1);
self::assertSame([null], $array);
$array = $template; A::ensure_size($array, 2);
self::assertSame([null, null], $array);
$template = [0, 1, "a" => "b", "c" => "d"];
$array = $template; A::ensure_size($array, 0);
self::assertSame([], $array);
$array = $template; A::ensure_size($array, 1);
self::assertSame([0], $array);
$array = $template; A::ensure_size($array, 2);
self::assertSame([0, 1], $array);
$array = $template; A::ensure_size($array, 3);
self::assertSame([0, 1, "a" => "b"], $array);
$array = $template; A::ensure_size($array, 4);
self::assertSame([0, 1, "a" => "b", "c" => "d"], $array);
$array = $template; A::ensure_size($array, 5);
self::assertSame([0, 1, "a" => "b", "c" => "d", null], $array);
}
const A = [
0 => "1st", 1 => "2nd",
10 => "a", 15 => "b", "x" => "y",
"dn" => null, "df" => false,
];
const B = [
0 => "3rd", 1 => "4th",
15 => "c", 20 => "d", "x" => "z",
"sn" => null, "sf" => false,
"dn" => "w", "df" => "t",
];
const MERGE = [
0 => "1st", 1 => "2nd",
2 => "a", 3 => "b", "x" => "z",
"dn" => "w", "df" => "t",
4 => "3rd", 5 => "4th",
6 => "c", 7 => "d",
"sn" => null, "sf" => false,
];
function testMerge() {
$a = self::A; $b = self::B;
A::merge($a, $b);
self::assertSame(self::MERGE, $a);
}
const MERGE2 = [
0 => "3rd", 1 => "4th",
10 => "a", 15 => "c", "x" => "z",
"dn" => "w", "df" => "t",
20 => "d",
"sn" => null, "sf" => false,
];
function testMerge2() {
$a = self::A; $b = self::B;
A::merge2($a, $b);
self::assertSame(self::MERGE2, $a);
}
const MERGE3 = [
0 => "1st", 1 => "2nd",
10 => "a", 15 => "c", "x" => "z",
"dn" => "w", "df" => "t",
2 => "3rd", 3 => "4th",
20 => "d",
"sn" => null, "sf" => false,
];
function testMerge3() {
$a = self::A; $b = self::B;
A::merge3($a, $b);
self::assertSame(self::MERGE3, $a);
}
const MERGE_NN = [
0 => "1st", 1 => "2nd",
2 => "a", 3 => "b", "x" => "z",
"dn" => "w", "df" => "t",
4 => "3rd", 5 => "4th",
6 => "c", 7 => "d",
"sf" => false,
];
function testMerge_nn() {
$a = self::A; $b = self::B;
A::merge_nn($a, $b);
self::assertSame(self::MERGE_NN, $a);
}
const MERGE_NN2 = [
0 => "3rd", 1 => "4th",
10 => "a", 15 => "c", "x" => "z",
"dn" => "w", "df" => "t",
20 => "d",
"sf" => false,
];
function testMerge_nn2() {
$a = self::A; $b = self::B;
A::merge_nn2($a, $b);
self::assertSame(self::MERGE_NN2, $a);
}
const MERGE_NZ = [
0 => "1st", 1 => "2nd",
2 => "a", 3 => "b", "x" => "z",
"dn" => "w", "df" => "t",
4 => "3rd", 5 => "4th",
6 => "c", 7 => "d",
];
function testMerge_nz() {
$a = self::A; $b = self::B;
A::merge_nz($a, $b);
self::assertSame(self::MERGE_NZ, $a);
}
const MERGE_NZ2 = [
0 => "3rd", 1 => "4th",
10 => "a", 15 => "c", "x" => "z",
"dn" => "w", "df" => "t",
20 => "d",
];
function testMerge_nz2() {
$a = self::A; $b = self::B;
A::merge_nz2($a, $b);
self::assertSame(self::MERGE_NZ2, $a);
}
const UPDATE_NX = [
0 => "1st", 1 => "2nd",
2 => "a", 3 => "b", "x" => "y",
"dn" => null, "df" => false,
4 => "d",
"sn" => null, "sf" => false,
];
function testUpdate_nx() {
$a = self::A; $b = self::B;
A::update_nx($a, $b);
self::assertSame(self::UPDATE_NX, $a);
}
const UPDATE_NX2 = [
0 => "1st", 1 => "2nd",
10 => "a", 15 => "b", "x" => "y",
"dn" => null, "df" => false,
20 => "d",
"sn" => null, "sf" => false,
];
function testUpdate_nx2() {
$a = self::A; $b = self::B;
A::update_nx2($a, $b);
self::assertSame(self::UPDATE_NX2, $a);
}
const UPDATE_N = [
0 => "1st", 1 => "2nd",
2 => "a", 3 => "b", "x" => "y",
"dn" => "w", "df" => false,
4 => "d",
"sn" => null, "sf" => false,
];
function testUpdate_n() {
$a = self::A; $b = self::B;
A::update_n($a, $b);
self::assertSame(self::UPDATE_N, $a);
}
const UPDATE_N2 = [
0 => "1st", 1 => "2nd",
10 => "a", 15 => "b", "x" => "y",
"dn" => "w", "df" => false,
20 => "d",
"sn" => null, "sf" => false,
];
function testUpdate_n2() {
$a = self::A; $b = self::B;
A::update_n2($a, $b);
self::assertSame(self::UPDATE_N2, $a);
}
const UPDATE_Z = [
0 => "1st", 1 => "2nd",
2 => "a", 3 => "b", "x" => "y",
"dn" => "w", "df" => "t",
4 => "d",
"sn" => null, "sf" => false,
];
function testUpdate_z() {
$a = self::A; $b = self::B;
A::update_z($a, $b);
self::assertSame(self::UPDATE_Z, $a);
}
const UPDATE_Z2 = [
0 => "1st", 1 => "2nd",
10 => "a", 15 => "b", "x" => "y",
"dn" => "w", "df" => "t",
20 => "d",
"sn" => null, "sf" => false,
];
function testUpdate_z2() {
$a = self::A; $b = self::B;
A::update_z2($a, $b);
self::assertSame(self::UPDATE_Z2, $a);
}
function testFlatten() {
$array = null; A::flatten($array);
self::assertSame([], $array);
$array = []; A::flatten($array);
self::assertSame([], $array);
$array = ["a", "b", "c" => 1, "d"]; A::flatten($array);
self::assertSame(["a", "b", "c" => 1, "d"], $array);
$array = ["a", "b", ["c" => 1, "d"]]; A::flatten($array);
self::assertSame(["a", "b", "c" => 1, "d"], $array);
$array = ["a", "b", ["c" => 1, "d"], ["c" => 2]]; A::flatten($array);
self::assertSame(["a", "b", "c" => [1, 2], "d"], $array);
$array = [0, null, false, "a" => 0, ["a" => null], ["a" => false]]; A::flatten($array);
self::assertSame([0, null, false, "a" => 0], $array);
}
function testExtract_seq() {
self::assertSame(null, A::extract_seq(null));
self::assertSame(null, A::extract_seq([]));
self::assertSame(null, A::extract_seq([1 => "a"]));
self::assertSame(["b"], A::extract_seq([1 => "a", 0 => "b"]));
self::assertSame(["a"], A::extract_seq(["a"]));
self::assertSame(["a", "b"], A::extract_seq(["a", "b"]));
self::assertSame(["a"], A::extract_seq(["a", "b" => "c"]));
self::assertSame(["c"], A::extract_seq(["a" => "b", "c"]));
}
function testExtract_assoc() {
self::assertSame(null, A::extract_assoc(null));
self::assertSame(null, A::extract_assoc([]));
self::assertSame([1 => "a"], A::extract_assoc([1 => "a"]));
self::assertSame([1 => "a"], A::extract_assoc([1 => "a", 0 => "b"]));
self::assertSame(null, A::extract_assoc(["a"]));
self::assertSame(null, A::extract_assoc(["a", "b"]));
self::assertSame(["b" => "c"], A::extract_assoc(["a", "b" => "c"]));
self::assertSame(["a" => "b"], A::extract_assoc(["a" => "b", "c"]));
}
function testSelect_replace() {
$src = null;
self::assertSame([], A::select_replace2($src, null));
self::assertSame([], A::select_replace2($src, []));
self::assertSame(["a" => null], A::select_replace2($src, ["a"]));
$src = [];
self::assertSame([], A::select_replace2($src, null));
self::assertSame([], A::select_replace2($src, []));
self::assertSame(["a" => null], A::select_replace2($src, ["a"]));
$src = ["a" => 1, "b" => 2];
self::assertSame(["a" => 1, "b" => 2], A::select_replace2($src, null));
self::assertSame([], A::select_replace2($src, []));
self::assertSame(["a" => 1], A::select_replace2($src, ["a"]));
$src = ["a" => 1, "b" => 2, "c" => 3];
self::assertSame(
["a" => 1, "x" => 9, "z" => 8, "c" => 3, "d" => null],
A::select_replace2($src, ["a", "x" => 9, ["z" => 8, "c"], "d"]));
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace nur\b\coll;
use nur\t\TestCase;
class ArrayViewStackTest extends TestCase {
function testBasic() {
$first = ["a" => 1, "b"=> 1];
$second = ["b" => 2, "c" => 2];
$third = [];
$stack = new ArrayViewStack();
$stack->push($first)->push($second)->push($third);
self::assertSame(null, $stack->get("d"));
self::assertSame(2, $stack->get("c"));
self::assertSame(2, $stack->get("b"));
self::assertSame(1, $stack->get("a"));
$stack["a"] = "x";
$stack["b"] = "y";
$stack["c"] = "z";
$stack["d"] = "t";
self::assertSame("t", $stack->get("d"));
self::assertSame("z", $stack->get("c"));
self::assertSame("y", $stack->get("b"));
self::assertSame("x", $stack->get("a"));
unset($stack["a"]);
unset($stack["b"]);
unset($stack["c"]);
unset($stack["d"]);
self::assertSame(null, $stack->get("d"));
self::assertSame(2, $stack->get("c"));
self::assertSame(2, $stack->get("b"));
self::assertSame(1, $stack->get("a"));
}
}

View File

@ -0,0 +1,61 @@
<?php
namespace nur\b\coll;
use nur\b\coll\impl\ItemView;
use nur\t\TestCase;
class ArrayViewTest extends TestCase {
function testBasic() {
$array = [];
$v = new ArrayView($array);
self::assertFalse($v->has("x"));
$v["x"] = 2;
self::assertTrue($v->has("x"));
self::assertSame(2, $v["x"]);
self::assertSame(["x" => 2], $array);
$v->del("x");
self::assertFalse($v->has("x"));
self::assertSame([], $array);
$array = [];
$v = new ArrayView($array, "sub");
self::assertFalse($v->has("x"));
$v["x"] = 2;
self::assertTrue($v->has("x"));
self::assertSame(2, $v["x"]);
self::assertSame(["sub" => ["x" => 2]], $array);
$v->del("x");
self::assertFalse($v->has("x"));
self::assertSame(["sub" => []], $array);
$array = new class extends FancyArray {
use TGenericArray;
};
$array["x"] = 0;
$array["sub"] = ["y" => 3];
$v = new ArrayView($array, "sub");
self::assertFalse($v->has("x"));
self::assertTrue($v->has("y"));
$v["x"] = 2;
self::assertTrue($v->has("x"));
self::assertSame(2, $v["x"]);
self::assertSame(["x" => 0, "sub" => ["y" => 3, "x" => 2]], $array->array());
$v->del("x");
self::assertFalse($v->has("x"));
self::assertSame(["x" => 0, "sub" => ["y" => 3]], $array->array());
}
function testItemView() {
$array = ["alice" => null, "bob" => null];
$alice = new ItemView($array, "alice");
$bob = new ItemView($array, "bob");
$alice->setName("Alice");
$alice->setAge(32);
self::assertSame([
"alice" => ["name" => "Alice", "age" => 32],
"bob" => ["name" => "bob", "age" => 0],
], $array);
}
#XXX tester vue d'une vue
}

View File

@ -0,0 +1,61 @@
<?php
namespace nur\b\coll\impl;
use nur\b\coll\ArrayView;
use nur\b\coll\TArrayMd;
use nur\b\coll\TAutoconstsStatic;
/**
* Class Item
*
* --autogen-properties-and-methods--
* @method string getName()
* @method int getAge()
* @method void setName(string $value)
* @method void setAge(int $value)
*/
class ItemView extends ArrayView {
const SCHEMA = [
"name" => "string",
"age" => "int",
];
use TArrayMd, TAutoconstsStatic;
const _AUTOGEN_CONSTS = ["" => [self::class, "_AUTOGEN_CONSTS"]];
# start of --autogen-dynamic-- section
const _AUTO_GETTERS = /*autogen*/[
'getName' => 'name',
'getAge' => 'age',
];
const _AUTO_SETTERS = /*autogen*/[
'setName' => 'name',
'setAge' => 'age',
];
const _AUTO_DELETERS = /*autogen*/[
'delName' => 'name',
'delAge' => 'age',
];
const _AUTO_CI_GETTERS = /*autogen*/[];
const _AUTO_CI_SETTERS = /*autogen*/[];
const _AUTOGEN_LITERALS = /*autogen*/[
[
self::SCHEMA,
'self::SCHEMA',
],
[\nur\php\Autogen::class, '\\nur\\php\\Autogen::class'],
];
const _AUTOGEN_METHODS = /*autogen*/[
[
\nur\php\Autogen::class,
'auto_getters_methods',
self::SCHEMA,
],
[
\nur\php\Autogen::class,
'auto_setters_methods',
self::SCHEMA,
],
];
#--autogen-dynamic--
}

View File

@ -0,0 +1,21 @@
<?php
namespace nur\b\date;
use nur\t\TestCase;
class DateTest extends TestCase {
function test() {
date_default_timezone_set("Indian/Reunion");
self::assertSame("Indian/Reunion", date_default_timezone_get());
$date = new Date(1634098974);
self::assertSame(1634068800, $date->getTime());
self::assertSame("13/10/2021", strval($date));
$date->wrapStart();
self::assertSame(1634068800, $date->getTime());
self::assertSame("13/10/2021 00:00:00", date("d/m/Y H:i:s", $date->getTime()));
$date->wrapEnd();
self::assertSame(1634155199, $date->getTime());
self::assertSame("13/10/2021 23:59:59", date("d/m/Y H:i:s", $date->getTime()));
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace nur\b\date;
use nur\t\TestCase;
class DatetimeTest extends TestCase {
function test() {
date_default_timezone_set("Indian/Reunion");
self::assertSame("Indian/Reunion", date_default_timezone_get());
$datetime = new Datetime(1634100479);
self::assertSame(1634100479, $datetime->getTime());
self::assertSame("13/10/2021 08:47:59", strval($datetime));
$datetime->wrapStart();
self::assertSame(1634068800, $datetime->getTime());
self::assertSame("13/10/2021 00:00:00", strval($datetime));
$datetime->wrapEnd();
self::assertSame(1634155199, $datetime->getTime());
self::assertSame("13/10/2021 23:59:59", strval($datetime));
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace nur\b\date;
use nur\t\TestCase;
class DelayTest extends TestCase {
function testGet() {
self::assertSame(0, (new Delay(0))->getu());
self::assertSame(-1, (new Delay(-1))->getu());
self::assertSame(1, (new Delay(1))->getu());
self::assertSame(120, (new Delay(120))->getu());
self::assertSame(86400, (new Delay(86400))->getu());
self::assertSame(-86400, (new Delay(-86400))->getu());
}
function testAdd() {
$t1 = new Delay(0);
$t2 = $t1->addu(0); self::assertNotSame($t2, $t1); self::assertSame(0, $t2->getu()); $t1 = $t2;
$t2 = $t1->addu(10); self::assertNotSame($t2, $t1); self::assertSame(10, $t2->getu()); $t1 = $t2;
$t2 = $t1->addu(10); self::assertNotSame($t2, $t1); self::assertSame(20, $t2->getu()); $t1 = $t2;
$t2 = $t1->addu(100); self::assertNotSame($t2, $t1); self::assertSame(120, $t2->getu()); $t1 = $t2;
$t2 = $t1->addu(86280); self::assertNotSame($t2, $t1); self::assertSame(86400, $t2->getu()); $t1 = $t2;
$t2 = $t1->addu(86400); self::assertNotSame($t2, $t1); self::assertSame(172800, $t2->getu()); $t1 = $t2;
$t2 = $t1->addu(-86400); self::assertNotSame($t2, $t1); self::assertSame(86400, $t2->getu()); $t1 = $t2;
}
function testSub() {
$t1 = new Delay(0);
$t2 = $t1->subu(-86400); self::assertNotSame($t2, $t1); self::assertSame(86400, $t2->getu()); $t1 = $t2;
$t2 = $t1->subu(86400); self::assertNotSame($t2, $t1); self::assertSame(0, $t2->getu()); $t1 = $t2;
$t1 = new Time(120);
$t2 = $t1->subu(100); self::assertNotSame($t2, $t1); self::assertSame(20, $t2->getu()); $t1 = $t2;
$t2 = $t1->subu(10); self::assertNotSame($t2, $t1); self::assertSame(10, $t2->getu()); $t1 = $t2;
$t2 = $t1->subu(10); self::assertNotSame($t2, $t1); self::assertSame(0, $t2->getu()); $t1 = $t2;
$t2 = $t1->subu(0); self::assertNotSame($t2, $t1); self::assertSame(0, $t2->getu()); $t1 = $t2;
}
function test__toString() {
self::assertSame("0:00:00", strval(new Delay(0)));
self::assertSame("0:00:56", strval(new Delay(56)));
self::assertSame("0:02:00", strval(new Delay(120)));
self::assertSame("23:59:59", strval(new Delay(86399)));
self::assertSame("24:00:00", strval(new Delay(86400)));
self::assertSame("48:00:00", strval(new Delay(172800)));
self::assertSame("-48:00:00", strval(new Delay(-172800)));
self::assertSame("-24:00:00", strval(new Delay(-86400)));
self::assertSame("-0:02:00", strval(new Delay(-120)));
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace nur\b\date;
use nur\t\TestCase;
class ElapsedTest extends TestCase {
const PAST = [
[0, "maintenant"],
[1, "depuis quelques secondes"],
[2, "depuis quelques secondes"],
[3, "depuis quelques secondes"],
[4, "depuis 4 secondes"],
[5, "depuis 5 secondes"],
[59, "depuis 59 secondes"],
[60, "depuis 1 minute"],
[61, "depuis 1 minute"],
[119, "depuis 1 minute"],
[120, "depuis 2 minutes"],
[121, "depuis 2 minutes"],
[3599, "depuis 59 minutes"],
[3600, "depuis 1 heure"],
[3601, "depuis 1 heure"],
[3660, "depuis 1 heure 1 minute"],
[3720, "depuis 1 heure 2 minutes"],
];
function testPast() {
foreach (self::PAST as [$seconds, $string]) {
$elapsed = new Elapsed($seconds);
self::assertSame($string, $elapsed->formatSince(), sprintf("for seconds=%d", $seconds));
}
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace nur\b\date;
use nur\t\TestCase;
class HourTest extends TestCase {
function test__toString() {
self::assertSame("0h00", strval(new Hour(0)));
self::assertSame("0h56", strval(new Hour(56)));
self::assertSame("2h00", strval(new Hour(120)));
self::assertSame("23h59", strval(new Hour(1439)));
self::assertSame("24h00", strval(new Hour(1440)));
self::assertSame("0h01", strval(new Hour(1441)));
}
function testFormat() {
self::assertSame("0h00", (new Hour(0))->format());
self::assertSame("0h56", (new Hour(56))->format());
self::assertSame("2h00", (new Hour(120))->format());
self::assertSame("23h59", (new Hour(1439))->format());
self::assertSame("24h00", (new Hour(1440))->format());
self::assertSame("0h01", (new Hour(1441))->format());
}
function testStep() {
$h = new class extends Hour {
const STEP = 5;
};
$h = $h->newu(10); self::assertSame("0h10", strval($h));
$h = $h->newu(12); self::assertSame("0h10", strval($h));
$h = $h->newu(15); self::assertSame("0h15", strval($h));
$h = $h->newu(17); self::assertSame("0h15", strval($h));
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace nur\b\date;
use nur\t\TestCase;
class TimeTest extends TestCase {
function testGetc() {
self::assertSame(0, (new Time(0))->getu());
self::assertSame(86399, (new Time(-1))->getu());
self::assertSame(1, (new Time(1))->getu());
self::assertSame(120, (new Time(120))->getu());
self::assertSame(86400, (new Time(86400))->getu());
self::assertSame(0, (new Time(-86400))->getu());
}
function testAddc() {
$t1 = new Time(0);
$t2 = $t1->addu(0); self::assertNotSame($t2, $t1); self::assertSame(0, $t2->getu()); $t1 = $t2;
$t2 = $t1->addu(10); self::assertNotSame($t2, $t1); self::assertSame(10, $t2->getu()); $t1 = $t2;
$t2 = $t1->addu(10); self::assertNotSame($t2, $t1); self::assertSame(20, $t2->getu()); $t1 = $t2;
$t2 = $t1->addu(100); self::assertNotSame($t2, $t1); self::assertSame(120, $t2->getu()); $t1 = $t2;
$t2 = $t1->addu(86280); self::assertNotSame($t2, $t1); self::assertSame(86400, $t2->getu()); $t1 = $t2;
$t2 = $t1->addu(86400); self::assertNotSame($t2, $t1); self::assertSame(0, $t2->getu()); $t1 = $t2;
$t2 = $t1->addu(-86400); self::assertNotSame($t2, $t1); self::assertSame(0, $t2->getu()); $t1 = $t2;
}
function testSubc() {
$t1 = new Time(0);
$t2 = $t1->subu(-86400); self::assertNotSame($t2, $t1); self::assertSame(86400, $t2->getu()); $t1 = $t2;
$t2 = $t1->subu(86400); self::assertNotSame($t2, $t1); self::assertSame(0, $t2->getu()); $t1 = $t2;
$t1 = new Time(120);
$t2 = $t1->subu(100); self::assertNotSame($t2, $t1); self::assertSame(20, $t2->getu()); $t1 = $t2;
$t2 = $t1->subu(10); self::assertNotSame($t2, $t1); self::assertSame(10, $t2->getu()); $t1 = $t2;
$t2 = $t1->subu(10); self::assertNotSame($t2, $t1); self::assertSame(0, $t2->getu()); $t1 = $t2;
$t2 = $t1->subu(0); self::assertNotSame($t2, $t1); self::assertSame(0, $t2->getu()); $t1 = $t2;
}
function test__toString() {
self::assertSame("00:00:00", strval(new Time(0)));
self::assertSame("00:00:56", strval(new Time(56)));
self::assertSame("00:02:00", strval(new Time(120)));
self::assertSame("23:59:59", strval(new Time(86399)));
self::assertSame("24:00:00", strval(new Time(86400)));
self::assertSame("00:00:01", strval(new Time(86401)));
}
function testFormat() {
self::assertSame("00:00:00", (new Time(0))->format());
self::assertSame("00:00:56", (new Time(56))->format());
self::assertSame("00:02:00", (new Time(120))->format());
self::assertSame("23:59:59", (new Time(86399))->format());
self::assertSame("24:00:00", (new Time(86400))->format());
self::assertSame("00:00:01", (new Time(86401))->format());
}
}

View File

@ -0,0 +1,99 @@
<?php
namespace nur\b\date;
use nur\t\TestCase;
class TrangeTest extends TestCase {
function testBefore() {
$h0 = new Time(0);
$h12 = new Time(12*60*60);
$h18 = new Time(18*60*60);
$h24 = new Time(24*60*60);
$p1 = new Trange($h0, $h12);
self::assertFalse($p1->before($h0));
self::assertTrue($p1->before($h12));
self::assertTrue($p1->before($h18));
self::assertTrue($p1->before($h24));
$p2 = new Trange($h0, $h18);
self::assertFalse($p2->before($h0));
self::assertFalse($p2->before($h12));
self::assertTrue($p2->before($h18));
self::assertTrue($p2->before($h24));
$p3 = new Trange($h12, $h18);
self::assertFalse($p3->before($h0));
self::assertFalse($p3->before($h12));
self::assertTrue($p3->before($h18));
self::assertTrue($p3->before($h24));
$p4 = new Trange($h12, $h24);
self::assertFalse($p4->before($h0));
self::assertFalse($p4->before($h12));
self::assertFalse($p4->before($h18));
self::assertTrue($p4->before($h24));
$p5 = new Trange($h18, $h24);
self::assertFalse($p5->before($h0));
self::assertFalse($p5->before($h12));
self::assertFalse($p5->before($h18));
self::assertTrue($p5->before($h24));
}
function testContains() {
$h0 = new Time(0);
$h12 = new Time(12*60*60);
$h18 = new Time(18*60*60);
$h24 = new Time(24*60*60);
$p1 = new Trange($h0, $h18);
self::assertTrue($p1->contains($h0));
self::assertTrue($p1->contains($h12));
self::assertFalse($p1->contains($h18));
self::assertFalse($p1->contains($h24));
$p2 = new Trange($h12, $h24);
self::assertFalse($p2->contains($h0));
self::assertTrue($p2->contains($h12));
self::assertTrue($p2->contains($h18));
self::assertFalse($p2->contains($h24));
}
function testAfter() {
$h0 = new Time(0);
$h12 = new Time(12*60*60);
$h18 = new Time(18*60*60);
$h24 = new Time(24*60*60);
$p1 = new Trange($h0, $h12);
self::assertFalse($p1->after($h0));
self::assertFalse($p1->after($h12));
self::assertFalse($p1->after($h18));
self::assertFalse($p1->after($h24));
$p2 = new Trange($h0, $h18);
self::assertFalse($p2->after($h0));
self::assertFalse($p2->after($h12));
self::assertFalse($p2->after($h18));
self::assertFalse($p2->after($h24));
$p3 = new Trange($h12, $h18);
self::assertTrue($p3->after($h0));
self::assertFalse($p3->after($h12));
self::assertFalse($p3->after($h18));
self::assertFalse($p3->after($h24));
$p4 = new Trange($h12, $h24);
self::assertTrue($p4->after($h0));
self::assertFalse($p4->after($h12));
self::assertFalse($p4->after($h18));
self::assertFalse($p4->after($h24));
$p5 = new Trange($h18, $h24);
self::assertTrue($p5->after($h0));
self::assertTrue($p5->after($h12));
self::assertFalse($p5->after($h18));
self::assertFalse($p5->after($h24));
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace nur\b;
use Exception;
use nur\b\impl\One;
use nur\t\TestCase;
class exceptionTest extends TestCase {
function testPlouf() {
$one = new One();
try {
$one->f();
} catch (Exception $e) {
self::assertTrue(true);
$sh = new ExceptionShadow($e);
echo "=== shadow\n";
echo $sh->getTraceAsString()."\n";
echo "=== original\n";
echo $e->getTraceAsString()."\n";
}
}
}

16
nur_tests/b/impl/One.php Normal file
View File

@ -0,0 +1,16 @@
<?php
namespace nur\b\impl;
use nur\b\impl\Two;
class One {
function __construct() {
$this->two = new Two();
}
private $two;
function f() {
$this->two->f();
}
}

10
nur_tests/b/impl/Two.php Normal file
View File

@ -0,0 +1,10 @@
<?php
namespace nur\b\impl;
use Exception;
class Two {
function f() {
throw new Exception("error");
}
}

View File

@ -0,0 +1,120 @@
<?php
namespace nur\b\io;
use nur\t\TestCase;
class StringReaderTest extends TestCase {
function testReadLine() {
$sr = new StringReader();
self::assertException(EOFException::class, [$sr, "readLine"]);
$sr->setString(null);
self::assertException(EOFException::class, [$sr, "readLine"]);
$sr->setString("");
self::assertException(EOFException::class, [$sr, "readLine"]);
$sr->setString("line");
self::assertSame("line", $sr->readLine());
self::assertException(EOFException::class, [$sr, "readLine"]);
$sr->setString("line\r\n");
self::assertSame("line", $sr->readLine());
self::assertException(EOFException::class, [$sr, "readLine"]);
$sr->setString("line\r");
self::assertSame("line", $sr->readLine());
self::assertException(EOFException::class, [$sr, "readLine"]);
$sr->setString("line\n");
self::assertSame("line", $sr->readLine());
self::assertException(EOFException::class, [$sr, "readLine"]);
$sr->setString("line1\nline2");
self::assertSame("line1", $sr->readLine());
self::assertSame("line2", $sr->readLine());
self::assertException(EOFException::class, [$sr, "readLine"]);
$sr->setString("line1\nline2\n");
self::assertSame("line1", $sr->readLine());
self::assertSame("line2", $sr->readLine());
self::assertException(EOFException::class, [$sr, "readLine"]);
$sr->setString("line1\r\nline2\r\n");
self::assertSame("line1", $sr->readLine());
self::assertSame("line2", $sr->readLine());
self::assertException(EOFException::class, [$sr, "readLine"]);
$sr->setString("line1\rline2\n");
self::assertSame("line1", $sr->readLine());
self::assertSame("line2", $sr->readLine());
self::assertException(EOFException::class, [$sr, "readLine"]);
$sr->setString("line1\nline2\r");
self::assertSame("line1", $sr->readLine());
self::assertSame("line2", $sr->readLine());
self::assertException(EOFException::class, [$sr, "readLine"]);
$sr->setString("line1\rline2\nline3\r\n");
self::assertSame("line1", $sr->readLine());
self::assertSame("line2", $sr->readLine());
self::assertSame("line3", $sr->readLine());
self::assertException(EOFException::class, [$sr, "readLine"]);
$sr->setString("\n");
self::assertSame("", $sr->readLine());
self::assertException(EOFException::class, [$sr, "readLine"]);
$sr->setString("\n\n");
self::assertSame("", $sr->readLine());
self::assertSame("", $sr->readLine());
self::assertException(EOFException::class, [$sr, "readLine"]);
}
function testReadLines() {
$sr = new StringReader();
self::assertSame([], $sr->readLines());
$sr->setString(null);
self::assertSame([], $sr->readLines());
$sr->setString("");
self::assertSame([], $sr->readLines());
$sr->setString("line");
self::assertSame(["line"], $sr->readLines());
$sr->setString("line\r\n");
self::assertSame(["line"], $sr->readLines());
$sr->setString("line\r");
self::assertSame(["line"], $sr->readLines());
$sr->setString("line\n");
self::assertSame(["line"], $sr->readLines());
$sr->setString("line1\nline2");
self::assertSame(["line1", "line2"], $sr->readLines());
$sr->setString("line1\nline2\n");
self::assertSame(["line1", "line2"], $sr->readLines());
$sr->setString("line1\r\nline2\r\n");
self::assertSame(["line1", "line2"], $sr->readLines());
$sr->setString("line1\rline2\n");
self::assertSame(["line1", "line2"], $sr->readLines());
$sr->setString("line1\nline2\r");
self::assertSame(["line1", "line2"], $sr->readLines());
$sr->setString("line1\rline2\nline3\r\n");
self::assertSame(["line1", "line2", "line3"], $sr->readLines());
$sr->setString("\n");
self::assertSame([""], $sr->readLines());
$sr->setString("\n\n");
self::assertSame(["", ""], $sr->readLines());
}
}

View File

@ -0,0 +1,158 @@
<?php
namespace nur\b\io;
use nur\t\TestCase;
class StringWriterTest extends TestCase {
function testWrite1() {
$sw = new StringWriter();
self::assertSame("", $sw->getString());
$sw->write();
self::assertSame("", $sw->getString());
$sw->write("line");
self::assertSame("line", $sw->getString());
$sw->write("line1");
$sw->write("line2");
self::assertSame("line1line2", $sw->getString());
$sw->wnl("line");
self::assertSame("line\n", $sw->getString());
$sw->wnl("line1");
$sw->wnl("line2");
self::assertSame("line1\nline2\n", $sw->getString());
}
function testWrite2() {
$sw = new StringWriter();
self::assertSame("", $sw->getString());
$sw->write();
self::assertSame("", $sw->getString());
$sw->write("line");
self::assertSame("line", $sw->getString());
$sw->write("line1", "line2");
self::assertSame("line1line2", $sw->getString());
$sw->write("line1", null, "line2", false, "line3");
self::assertSame("line1line2line3", $sw->getString());
$sw->wnl("line");
self::assertSame("line\n", $sw->getString());
$sw->wnl("line1", "line2");
self::assertSame("line1line2\n", $sw->getString());
$sw->wnl("line1", null, "line2", false, "line3");
self::assertSame("line1line2line3\n", $sw->getString());
}
function testPrint1() {
$sw = new StringWriter();
self::assertSame("", $sw->getString());
$sw->print();
self::assertSame("", $sw->getString());
$sw->print("line");
self::assertSame("line", $sw->getString());
$sw->print("line1");
$sw->print("line2");
self::assertSame("line1line2", $sw->getString());
$sw->pnl("line");
self::assertSame("line\n", $sw->getString());
$sw->pnl("line1");
$sw->pnl("line2");
self::assertSame("line1\nline2\n", $sw->getString());
}
function testPrint2() {
$sw = new StringWriter();
self::assertSame("", $sw->getString());
$sw->print();
self::assertSame("", $sw->getString());
$sw->print("line");
self::assertSame("line", $sw->getString());
$sw->print("line1", "line2");
self::assertSame("line1 line2", $sw->getString());
$sw->print("line1", null, "line2", false, "line3");
self::assertSame("line1 line2 line3", $sw->getString());
$sw->pnl("line");
self::assertSame("line\n", $sw->getString());
$sw->pnl("line1", "line2");
self::assertSame("line1 line2\n", $sw->getString());
$sw->pnl("line1", null, "line2", false, "line3");
self::assertSame("line1 line2 line3\n", $sw->getString());
}
function testWriteLines() {
$sw = new StringWriter();
$sw->writeLines(null);
self::assertSame("", $sw->getString());
$sw->writeLines([]);
self::assertSame("", $sw->getString());
$sw->writeLines(["line"]);
self::assertSame("line\n", $sw->getString());
$sw->writeLines(["line1", "line2"]);
self::assertSame("line1\nline2\n", $sw->getString());
}
function testIndent() {
$sw = new StringWriter();
$sw->pnl("not indented");
$sw->indent();
$sw->pnl("level 1a");
$sw->pnl("level 1b");
$sw->indent();
$sw->pnl("level 2a");
$sw->pnl("level 2b");
$sw->dedent();
$sw->pnl("level 1c");
$sw->dedent();
$sw->pnl("not indented");
self::assertSame("not indented\n level 1a\n level 1b\n level 2a\n level 2b\n level 1c\nnot indented\n", $sw->getString());
$sw = new StringWriter();
$sw->pnl("a1\na2");
$sw->indent();
$sw->pnl("b1\nb2");
$sw->indent();
$sw->pnl("c1\nc2");
$sw->dedent();
$sw->pnl("b3\nb4");
$sw->dedent();
$sw->pnl("a3\na4");
self::assertSame("a1\na2\n b1\n b2\n c1\n c2\n b3\n b4\na3\na4\n", $sw->getString());
$sw = new StringWriter();
$sw->print("a1\na2\n");
$sw->indent();
$sw->print("b1\nb2\n");
$sw->indent();
$sw->print("c1\nc2\n");
$sw->dedent();
$sw->print("b3\nb4\n");
$sw->dedent();
$sw->print("a3\na4\n");
self::assertSame("a1\na2\n b1\n b2\n c1\n c2\n b3\n b4\na3\na4\n", $sw->getString());
}
}

View File

@ -0,0 +1,83 @@
<?php
namespace nur\b\params;
use nur\b\params\impl\BPB2;
use nur\b\params\impl\BPC1;
use nur\b\params\impl\BPC2;
use nur\b\params\impl\BPD1;
use nur\b\params\impl\BPD2;
use nur\b\params\impl\MyParametrable;
use nur\t\TestCase;
class ParametrableTest extends TestCase {
function testDefaults() {
$top = new BPC1();
self::assertSame([null, null, null, null], $top->getTopData());
$top->setParametrableParams(null);
self::assertSame([null, null, null, null], $top->getTopData());
$top->setParametrableParams([null, null, null, null]);
self::assertSame(["", null, false, null], $top->getTopData());
$sub = new BPC2();
self::assertSame([null, null, null, null, null, null, null, null], $sub->getSubData());
$sub->setParametrableParams(null);
self::assertSame([null, null, null, null, null, null, null, null], $sub->getSubData());
$sub->setParametrableParams([null, null, null, null, null, null, null, null]);
self::assertSame(["", null, false, null, "", null, false, null], $sub->getSubData());
}
function testDefaults2() {
Txx("od11");
$od11 = new BPD1();
self::assertSame([null, "boum", null, 18], $od11->od1Values());
Txx("od12");
$od12 = new BPD1([
"string" => "hello",
"nstring" => "world",
"int" => 15,
"nint" => 39,
]);
self::assertSame(["hello", "world", 15, 39], $od12->od1Values());
Txx("od21");
$od21 = new BPD2();
self::assertSame([42, 18, null, null], $od21->od2Values());
Txx("od22");
$od22 = new BPD2([
"int" => 15,
"nint" => 39,
"bool" => true,
"nbool" => false,
]);
self::assertSame([15, 39, true, false], $od22->od2Values());
}
function testProps() {
$second = new BPB2();
$second->setParametrableParams([
"first" => "premier",
"second" => "20",
]);
self::assertSame(["premier", 20], $second->getData());
}
function testMyParametrable() {
$p = new MyParametrable([
"name" => "plouf",
]);
self::assertSame("plouf", $p->getName());
self::assertNull($p->isActive());
self::assertNull($p->getNumber());
self::assertNull($p->whatIsMasked());
$p->setName("pouet");
self::assertSame("pouet", $p->getName());
$p->setNumber(2);
self::assertSame(12, $p->getNumber());
$p->setMasked("hidden");
self::assertNull($p->getMasked());
self::assertSame("hidden", $p->whatIsMasked());
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace nur\b\params\impl;
class BPB1 {
private $ppFirst;
function getFirst(): ?string {
return $this->ppFirst;
}
function pp_setFirst(string $value): void {
$this->ppFirst = $value;
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace nur\b\params\impl;
use nur\b\params\IParametrable;
use nur\b\params\Tparametrable1;
class BPB2 extends BPB1 implements IParametrable {
use Tparametrable1;
const PARAMETRABLE_PARAMS_SCHEMA = [
"first" => "?string",
"second" => "?int",
];
private $ppSecond;
function getData() {
return [$this->getFirst(), $this->ppSecond];
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace nur\b\params\impl;
use nur\b\params\IParametrable;
use nur\b\params\Tparametrable1;
class BPC1 implements IParametrable {
use Tparametrable1;
const PARAMETRABLE_PARAMS_SCHEMA = [
"top-string" => "string",
"top-nstring" => "?string",
"top-bool" => "bool",
"top-nbool" => "?bool",
];
/** @var string */
protected $ppTopString, $ppTopNstring;
/** @var bool */
protected $ppTopBool, $ppTopNbool;
function getTopData(): array {
return [$this->ppTopString, $this->ppTopNstring, $this->ppTopBool, $this->ppTopNbool];
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace nur\b\params\impl;
use nur\b\params\Tparametrable;
class BPC2 extends BPC1 {
use Tparametrable;
const PARAMETRABLE_PARAMS_SCHEMA = [
"sub-string" => "string",
"sub-nstring" => "?string",
"sub-bool" => "bool",
"sub-nbool" => "?bool",
];
/** @var string */
private $ppSubString, $ppSubNstring;
/** @var bool */
private $ppSubBool, $ppSubNbool;
function getSubData(): array {
$topData = $this->getTopData();
$subData = [$this->ppSubString, $this->ppSubNstring, $this->ppSubBool, $this->ppSubNbool];
return array_merge($topData, $subData);
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace nur\b\params\impl;
use nur\b\params\IParametrable;
use nur\b\params\Tparametrable1;
class BPD1 implements IParametrable {
use Tparametrable1;
const PARAMETRABLE_PARAMS_SCHEMA = [
"string" => ["string", null],
"nstring" => ["?string", "boum"],
"int" => ["int", null],
"nint" => ["?int", 18],
];
function __construct(?array $params=null) {
$this->initParametrableParams($params);
}
public $ppString, $ppNstring, $ppInt, $ppNint;
function od1Values(): array {
return [$this->ppString, $this->ppNstring, $this->ppInt, $this->ppNint];
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace nur\b\params\impl;
use nur\b\params\Tparametrable;
class BPD2 extends BPD1 {
use Tparametrable;
const PARAMETRABLE_PARAMS_SCHEMA = [
"int" => ["int", 42],
"nint" => ["?int", null],
"bool" => ["bool", null],
"nbool" => ["?bool", null],
];
public $ppBool, $ppNbool;
function od2Values(): array {
return [$this->ppInt, $this->ppNint, $this->ppBool, $this->ppNbool];
}
}

View File

@ -0,0 +1,91 @@
<?php
namespace nur\b\params\impl;
use nur\b\params\Parametrable;
use nur\b\params\Tparametrable;
/**
* Class MyParametrable: exemple d'implémentation de Parametrable
*
* --autogen-properties-and-methods--
* @method string|null getName()
* @method bool isActive()
* @method int getNumber()
* @method string|null setName(?string $value)
* @method bool setActive(bool $value)
* @method int setNumber(int $value)
* @method string|null setMasked(?string $value)
*/
class MyParametrable extends Parametrable {
use Tparametrable;
const PARAMETRABLE_PARAMS_SCHEMA = [
"name" => "?string",
"active" => "bool",
"number" => "int",
"masked" => "?string",
];
/** @var string */
protected $ppName;
/** @var bool */
protected $ppActive;
protected $ppNumber;
function pp_setNumber(int $number): void {
$this->ppNumber = $number + 10;
}
/** @var string */
protected $masked;
function pp_setMasked(string $masked): void {
$this->masked = $masked;
}
function whatIsMasked(): ?string {
return $this->masked;
}
#############################################################################
const _AUTOGEN_CONSTS = [
"" => [self::class, "_AUTOGEN_CONSTS"],
];
const _AUTOGEN_LITERALS = /*autogen*/[
[
\nur\b\params\parametrable_utils::class,
'\\nur\\b\\params\\parametrable_utils::class',
],
[
self::PARAMETRABLE_PARAMS_SCHEMA,
'self::PARAMETRABLE_PARAMS_SCHEMA',
],
];
const _AUTOGEN_METHODS = /*autogen*/[
[
\nur\b\params\parametrable_utils::class,
'_autogen_methods_getters',
self::PARAMETRABLE_PARAMS_SCHEMA,
],
[
\nur\b\params\parametrable_utils::class,
'_autogen_methods_setters',
self::PARAMETRABLE_PARAMS_SCHEMA,
],
];
const _AUTO_GETTERS = /*autogen*/[
'getName' => 'name',
'isActive' => 'active',
'getNumber' => 'number',
'getMasked' => 'masked',
];
const _AUTO_SETTERS = /*autogen*/[
'setName' => 'name',
'setActive' => 'active',
'setNumber' => 'number',
'setMasked' => 'masked',
];
#--autogen-dynamic--
}

View File

@ -0,0 +1,140 @@
<?php
namespace nur\b\text;
use nur\t\TestCase;
class WordTest extends TestCase {
function testName() {
$o = new Word("l'objet#s|masculin"); # on doit spécifier le genre
$e = new Word("l'école#s|feminin"); # on doit spécifier le genre
$s = new Word("le service#s"); # le genre est implicite
$d = new Word("la direction#s"); # le genre est implicite
$l = new Word("le lieu#x");
self::assertSame("objet", $o->w());
self::assertSame("objets", $o->w(true));
self::assertSame("Objet", $o->u());
self::assertSame("Objets", $o->u(true));
self::assertSame("1 objet", $o->q());
self::assertSame("l'objet", $o->le());
self::assertSame("un objet", $o->un());
self::assertSame("de l'objet", $o->du());
self::assertSame("à l'objet", $o->au());
self::assertSame("0 objet", $o->q(0));
self::assertSame("aucun objet", $o->un(0));
self::assertSame("le 0 objet", $o->le(0));
self::assertSame("du 0 objet", $o->du(0));
self::assertSame("au 0 objet", $o->au(0));
self::assertSame("1 objet", $o->q(1));
self::assertSame("un objet", $o->un(1));
self::assertSame("l'objet", $o->le(1));
self::assertSame("de l'objet", $o->du(1));
self::assertSame("à l'objet", $o->au(1));
self::assertSame("5 objets", $o->q(5));
self::assertSame("les 5 objets", $o->un(5));
self::assertSame("les 5 objets", $o->le(5));
self::assertSame("des 5 objets", $o->du(5));
self::assertSame("aux 5 objets", $o->au(5));
self::assertSame("école", $e->w());
self::assertSame("écoles", $e->w(true));
self::assertSame("École", $e->u());
self::assertSame("Écoles", $e->u(true));
self::assertSame("0 école", $e->q(0));
self::assertSame("aucune école", $e->un(0));
self::assertSame("la 0 école", $e->le(0));
self::assertSame("de la 0 école", $e->du(0));
self::assertSame("à la 0 école", $e->au(0));
self::assertSame("1 école", $e->q(1));
self::assertSame("une école", $e->un(1));
self::assertSame("l'école", $e->le(1));
self::assertSame("de l'école", $e->du(1));
self::assertSame("à l'école", $e->au(1));
self::assertSame("5 écoles", $e->q(5));
self::assertSame("les 5 écoles", $e->un(5));
self::assertSame("les 5 écoles", $e->le(5));
self::assertSame("des 5 écoles", $e->du(5));
self::assertSame("aux 5 écoles", $e->au(5));
self::assertSame("service", $s->w());
self::assertSame("services", $s->w(true));
self::assertSame("Service", $s->u());
self::assertSame("Services", $s->u(true));
self::assertSame("0 service", $s->q(0));
self::assertSame("aucun service", $s->un(0));
self::assertSame("le 0 service", $s->le(0));
self::assertSame("du 0 service", $s->du(0));
self::assertSame("au 0 service", $s->au(0));
self::assertSame("1 service", $s->q(1));
self::assertSame("un service", $s->un(1));
self::assertSame("le service", $s->le(1));
self::assertSame("du service", $s->du(1));
self::assertSame("au service", $s->au(1));
self::assertSame("5 services", $s->q(5));
self::assertSame("les 5 services", $s->un(5));
self::assertSame("les 5 services", $s->le(5));
self::assertSame("des 5 services", $s->du(5));
self::assertSame("aux 5 services", $s->au(5));
self::assertSame("direction", $d->w());
self::assertSame("directions", $d->w(true));
self::assertSame("Direction", $d->u());
self::assertSame("Directions", $d->u(true));
self::assertSame("0 direction", $d->q(0));
self::assertSame("aucune direction", $d->un(0));
self::assertSame("la 0 direction", $d->le(0));
self::assertSame("de la 0 direction", $d->du(0));
self::assertSame("à la 0 direction", $d->au(0));
self::assertSame("1 direction", $d->q(1));
self::assertSame("une direction", $d->un(1));
self::assertSame("la direction", $d->le(1));
self::assertSame("de la direction", $d->du(1));
self::assertSame("à la direction", $d->au(1));
self::assertSame("5 directions", $d->q(5));
self::assertSame("les 5 directions", $d->un(5));
self::assertSame("les 5 directions", $d->le(5));
self::assertSame("des 5 directions", $d->du(5));
self::assertSame("aux 5 directions", $d->au(5));
self::assertSame("lieu", $l->w());
self::assertSame("lieux", $l->w(true));
self::assertSame("Lieu", $l->u());
self::assertSame("Lieux", $l->u(true));
self::assertSame("5 lieux", $l->q(5));
self::assertSame("les 5 lieux", $l->un(5));
self::assertSame("les 5 lieux", $l->le(5));
self::assertSame("des 5 lieux", $l->du(5));
self::assertSame("aux 5 lieux", $l->au(5));
$l = new Word("le lieu#x enchanteur#s");
self::assertSame("lieu enchanteur", $l->w());
self::assertSame("lieux enchanteurs", $l->w(true));
self::assertSame("Lieu enchanteur", $l->w(false, true));
self::assertSame("Lieux enchanteurs", $l->w(true, true));
self::assertSame("Lieu enchanteur", $l->u());
self::assertSame("Lieux enchanteurs", $l->u(true));
$l = new Word("le ^lieu#x ^enchanteur#s");
self::assertSame("Lieu Enchanteur", $l->w(false, true));
self::assertSame("Lieux Enchanteurs", $l->w(true, true));
self::assertSame("Lieu Enchanteur", $l->u());
self::assertSame("Lieux Enchanteurs", $l->u(true));
$l = new Word("la ^démo#s ^évidente#s");
self::assertSame("Démo Évidente", $l->w(false, true));
self::assertSame("Démos Évidentes", $l->w(true, true));
self::assertSame("Démo Évidente", $l->u());
self::assertSame("Démos Évidentes", $l->u(true));
}
function testAdjective() {
$adj = new Word("né#e#s", true);
self::assertSame("", $adj->a());
self::assertSame("", $adj->a(false));
self::assertSame("", $adj->a(false, false));
self::assertSame("née", $adj->a(true));
self::assertSame("née", $adj->a(true, false));
self::assertSame("nés", $adj->a(false, true));
self::assertSame("nées", $adj->a(true, true));
}
}

121
nur_tests/baseTest.php Normal file
View File

@ -0,0 +1,121 @@
<?php
namespace nur;
use nur\t\TestCase;
class baseTest extends TestCase {
function testZ() {
self::assertFalse(base::z("0"));
self::assertFalse(base::z(""));
self::assertFalse(base::z(0));
self::assertTrue(base::z(false));
self::assertTrue(base::z(null));
}
function testNz() {
self::assertTrue(base::nz("0"));
self::assertTrue(base::nz(""));
self::assertTrue(base::nz(0));
self::assertFalse(base::nz(false));
self::assertFalse(base::nz(null));
}
function testT() {
self::assertTrue(base::t("0"));
self::assertFalse(base::t(""));
self::assertFalse(base::t(0));
self::assertFalse(base::t(false));
self::assertFalse(base::t(null));
}
function testF() {
self::assertFalse(base::f("0"));
self::assertTrue(base::f(""));
self::assertTrue(base::f(0));
self::assertTrue(base::f(false));
self::assertTrue(base::f(null));
}
function testVf() {
self::assertEquals("0", base::vf("0"));
self::assertFalse(base::vf(""));
self::assertFalse(base::vf(0));
self::assertFalse(base::vf(false));
self::assertFalse(base::vf(null));
self::assertEquals(true, base::vf(true));
self::assertEquals("a", base::vf("a"));
}
function testVn() {
self::assertEquals("0", base::vn("0"));
self::assertNull(base::vn(""));
self::assertNull(base::vn(0));
self::assertNull(base::vn(false));
self::assertNull(base::vn(null));
self::assertEquals(true, base::vn(true));
self::assertEquals("a", base::vn("a"));
}
function testFirstv() {
self::assertNull(base::firstv());
self::assertEquals("0", base::firstv("0", 1));
self::assertEquals(1, base::firstv("", 1));
self::assertEquals(1, base::firstv(0, 1));
self::assertEquals(1, base::firstv(false, 1));
self::assertEquals(1, base::firstv(null, 1));
self::assertEquals("0", base::firstv("0", "", 0, false, null, 1, 2));
self::assertNull(base::firstv("", 0, false, null));
self::assertEquals(1, base::firstv("", 0, false, null, 1, 2));
self::assertTrue(base::firstv(true, 1));
self::assertEquals("a", base::firstv("a", 1));
}
function testT2() {
self::assertFalse(base::t2("0"));
self::assertFalse(base::t2(""));
self::assertFalse(base::t2(0));
self::assertFalse(base::t2(false));
self::assertFalse(base::t2(null));
}
function testF2() {
self::assertTrue(base::f2("0"));
self::assertTrue(base::f2(""));
self::assertTrue(base::f2(0));
self::assertTrue(base::f2(false));
self::assertTrue(base::f2(null));
}
function testVf2() {
self::assertFalse(base::vf2("0"));
self::assertFalse(base::vf2(""));
self::assertFalse(base::vf2(0));
self::assertFalse(base::vf2(false));
self::assertFalse(base::vf2(null));
self::assertEquals(true, base::vf2(true));
self::assertEquals("a", base::vf2("a"));
}
function testVn2() {
self::assertNull(base::vn2("0"));
self::assertNull(base::vn2(""));
self::assertNull(base::vn2(0));
self::assertNull(base::vn2(false));
self::assertNull(base::vn2(null));
self::assertEquals(true, base::vn2(true));
self::assertEquals("a", base::vn2("a"));
}
function testFirstv2() {
self::assertNull(base::firstv2());
self::assertEquals(1, base::firstv2("0", 1, 2));
self::assertEquals(1, base::firstv2("", 1, 2));
self::assertEquals(1, base::firstv2(0, 1, 2));
self::assertEquals(1, base::firstv2(false, 1, 2));
self::assertEquals(1, base::firstv2(null, 1, 2));
self::assertNull(base::firstv2("0", "", 0, false, null));
self::assertEquals(1, base::firstv2("0", "", 0, false, null, 1, 2));
self::assertTrue(base::firstv2(true, 1, 2));
self::assertEquals("a", base::firstv2("a", 1, 2));
}
}

39
nur_tests/cTest.php Normal file
View File

@ -0,0 +1,39 @@
<?php
namespace nur;
use nur\t\TestCase;
class cTest extends TestCase {
function testFlatten() {
self::assertSame([], c::flatten(null));
self::assertSame([], c::flatten([]));
self::assertSame(["a", "b"], c::flatten(["a", "b"]));
self::assertSame(["a", "b"], c::flatten(["a", ["b"]]));
self::assertSame(["a", "b"], c::flatten([["a"], "b"]));
self::assertSame(["a", "b", "c"], c::flatten(["a", "b", "c"]));
self::assertSame(["a", "b", "c"], c::flatten(["a", ["b", "c"]]));
self::assertSame(["a", "b", "c"], c::flatten(["a", ["b", ["c"]]]));
self::assertSame(["a" => ["x", "y"]], c::flatten(["a" => "x", ["a" => "y"]]));
self::assertSame(["a" => ["x", "y"]], c::flatten([["a" => "x"], "a" => "y"]));
self::assertSame(["a" => ["x", "y"]], c::flatten([["a" => "x"], ["a" => "y"]]));
self::assertSame(["a" => ["x", "y"]], c::flatten(["a" => ["x", ["y"]]]));
}
function testFlattenQuote() {
# flatten ne quote AUCUNE valeur, c'est la responsabilité de \nur\v\v
self::assertSame(
["<a", "<b", "<c", "<d"],
c::flatten(["<a", "<b", ["<c", "<d"]]));
self::assertSame([
"<a" => "x>", "<b" => ["y>"],
"<c" => "z>", "<d" => ["t>"],
"<e" => ["w1>" ,"w2>"],
], c::flatten([
"<a" => "x>", "<b" => ["y>"],
["<c" => "z>", "<d" => ["t>"]],
"<e" => "w1>", ["<e" => "w2>"],
]));
}
}

View File

@ -0,0 +1,582 @@
<?php
namespace nur\cli;
use nur\cli\impl\AbcdDest;
use nur\cli\impl\SimpleDest;
use nur\t\TestCase;
use stdClass;
class ArgsParserTest extends TestCase {
const SIMPLE_DEFS = [
"autohelp" => false,
"autoremains" => false,
["-a", "key" => "enabled"],
["-b", "key" => "enabled", "value" => 52],
["-z", "key" => "enabled", "inverse" => true],
["--keep", "key" => "keep"],
["-s", "--long", "key" => "option"],
["-v", "--value", "arg" => "value", "key" => "value"],
["-f", "--file", "arg" => "path", "key" => "file"],
["-h", "--host", "arg" => "host", "key" => "host"],
];
const DEBUG_ARGS = ["--keep", "-asvvalue"];
const SIMPLE_ARGS0 = [
"--keep",
"--long",
"--value", "value",
"--value=value",
"--file", "file",
"--file=file",
"--host", "host",
"--host=host",
];
const SIMPLE_ARGS1 = [
"-a", "-aa",
"-s", "-ss",
"-aass",
"-v", "value",
"-vvalue",
"-f", "file",
"-ffile",
"-h", "host",
"-hhost",
"-asv", "value",
"-asvvalue",
];
function testNormArgs() {
$parser = new ArgsParser(self::SIMPLE_DEFS);
$args = $parser->normArgs(self::DEBUG_ARGS);
#var_dump($args);
self::assertCount(6, $args);
$args = $parser->normArgs(self::SIMPLE_ARGS0);
#var_dump($args);
self::assertCount(15, $args);
$args = $parser->normArgs(self::SIMPLE_ARGS1);
#var_dump($args);
self::assertCount(31, $args);
self::assertSame(["--"], $parser->normArgs([]));
self::assertSame(["--"], $parser->normArgs(["--"]));
self::assertSame(["--", "a"], $parser->normArgs(["--", "a"]));
self::assertSame(["--", "--"], $parser->normArgs(["--", "--"]));
self::assertSame(["--", "--", "a"], $parser->normArgs(["--", "--", "a"]));
self::assertSame(["--", "--", "--"], $parser->normArgs(["--", "--", "--"]));
self::assertSame(["--", "--", "--", "a"], $parser->normArgs(["--", "--", "--", "a"]));
self::assertSame(["--", "a"], $parser->normArgs(["a"]));
self::assertSame(["--", "a"], $parser->normArgs(["a", "--"]));
self::assertSame(["--", "a", "b"], $parser->normArgs(["a", "--", "b"]));
self::assertSame(["--", "a", "--", "b"], $parser->normArgs(["a", "--", "--", "b"]));
self::assertSame(["--", "a", "--", "--", "b"], $parser->normArgs(["a", "--", "--", "--", "b"]));
}
function testNormArgs2() {
$parser = new ArgsParser(self::SIMPLE_DEFS);
self::assertSame(["-a", "--", "a"],
$parser->normArgs(["a", "-a"]));
self::assertSame(["-a", "--", "a"],
$parser->normArgs(["a", "-a", "//"]));
self::assertSame(["-a", "--", "a", "//", "--", "b"],
$parser->normArgs(["a", "-a", "//", "b"]));
self::assertSame(["-a", "--", "a", "//", "-b", "--", "b"],
$parser->normArgs(["a", "-a", "//", "b", "-b"]));
self::assertSame(["-a", "--", "a", "\\//"],
$parser->normArgs(["a", "-a", "\\//"]));
self::assertSame(["-a", "--", "a", "\\//", "b"],
$parser->normArgs(["a", "-a", "\\//", "b"]));
self::assertSame(["-a", "-b", "--", "a", "\\//", "b"],
$parser->normArgs(["a", "-a", "\\//", "b", "-b"]));
}
function testParseArgs() {
$parser = new ArgsParser(self::SIMPLE_DEFS);
$dest = [];
$parser->parseArgs($parser->normArgs(["-asvvalue", "--keep"]));
#var_dump($dest);
self::assertSame(1, $dest["enabled"]);
self::assertSame(1, $dest["keep"]);
self::assertSame(1, $dest["option"]);
self::assertSame("value", $dest["value"]);
$dest = [];
$parser->parseArgs($parser->normArgs(["-aa"]));
self::assertSame(2, $dest["enabled"]);
$dest = [];
$parser->parseArgs($parser->normArgs(["-aazz"]));
self::assertSame(0, $dest["enabled"]);
$dest = [];
$parser->parseArgs($parser->normArgs(["-b"]));
self::assertSame(52, $dest["enabled"]);
$dest = [];
$parser->parseArgs($parser->normArgs(["-bb"]));
self::assertSame(52, $dest["enabled"]);
self::assertException(ArgsException::class, function()
use ($parser, $dest) {
$parser->parseArgs($parser->normArgs(["-a", "arg"]));
});
}
function testParseArgs2() {
$defs = self::SIMPLE_DEFS;
$defs["autoremains"] = true;
$parser = new ArgsParser($defs);
$dest = []; $parser->parseArgs($parser->normArgs([]));
self::assertSame([], $dest["args"]);
$dest = []; $parser->parseArgs($parser->normArgs(["--"]));
self::assertSame([], $dest["args"]);
$dest = []; $parser->parseArgs($parser->normArgs(["--", "a"]));
self::assertSame(["a"], $dest["args"]);
$dest = []; $parser->parseArgs($parser->normArgs(["--", "--"]));
self::assertSame(["--"], $dest["args"]);
$dest = []; $parser->parseArgs($parser->normArgs(["--", "--", "a"]));
self::assertSame(["--", "a"], $dest["args"]);
$dest = []; $parser->parseArgs($parser->normArgs(["--", "--", "--"]));
self::assertSame(["--", "--"], $dest["args"]);
$dest = []; $parser->parseArgs($parser->normArgs(["--", "--", "--", "a"]));
self::assertSame(["--", "--", "a"], $dest["args"]);
$dest = []; $parser->parseArgs($parser->normArgs(["a"]));
self::assertSame(["a"], $dest["args"]);
$dest = []; $parser->parseArgs($parser->normArgs(["a", "--"]));
self::assertSame(["a"], $dest["args"]);
$dest = []; $parser->parseArgs($parser->normArgs(["a", "--", "b"]));
self::assertSame(["a", "b"], $dest["args"]);
$dest = []; $parser->parseArgs($parser->normArgs(["a", "--", "--", "b"]));
self::assertSame(["a", "--", "b"], $dest["args"]);
$dest = []; $parser->parseArgs($parser->normArgs(["a", "--", "--", "--", "b"]));
self::assertSame(["a", "--", "--", "b"], $dest["args"]);
}
function testParseArgs3() {
$defs = self::SIMPLE_DEFS;
$defs["autoremains"] = true;
$parser = new ArgsParser($defs);
$dest = []; $parser->parseArgs($parser->normArgs(["a", "-a"]));
self::assertSame(["a"], $dest["args"]);
self::assertSame(1, $dest["enabled"]);
$dest = []; $parser->parseArgs($parser->normArgs(["a", "-a", "//"]));
self::assertSame(["a"], $dest["args"]);
self::assertSame(1, $dest["enabled"]);
$dest = []; $parser->parseArgs($parser->normArgs(["a", "-a", "//", "b"]));
self::assertSame(["a", "b"], $dest["args"]);
self::assertSame(1, $dest["enabled"]);
$dest = []; $parser->parseArgs($parser->normArgs(["a", "-a", "//", "b", "-b"]));
self::assertSame(["a", "b"], $dest["args"]);
self::assertSame(52, $dest["enabled"]);
$dest = []; $parser->parseArgs($parser->normArgs(["a", "-a", "\\//"]));
self::assertSame(["a", "//"], $dest["args"]);
self::assertSame(1, $dest["enabled"]);
$dest = []; $parser->parseArgs($parser->normArgs(["a", "-a", "\\//", "b"]));
self::assertSame(["a", "//", "b"], $dest["args"]);
self::assertSame(1, $dest["enabled"]);
$dest = []; $parser->parseArgs($parser->normArgs(["a", "-a", "\\//", "b", "-b"]));
self::assertSame(["a", "//", "b"], $dest["args"]);
self::assertSame(52, $dest["enabled"]);
$dest = []; $parser->parseArgs($parser->normArgs(["a", "-a", "\\//", "\\\\//", "b", "-b"]));
self::assertSame(["a", "//", "\\//", "b"], $dest["args"]);
self::assertSame(52, $dest["enabled"]);
}
private static function parse(&$dest, array $defs, ?array $args=null): void {
$parser = new ArgsParser($defs);
$parser->parse($dest, $args);
}
function testAutoprop() {
unset($dest);
self::parse($dest, [
["-a", "--enable"],
["-b", "--b", "--before", "--before-date", "args" => "value"],
], [
"-aa",
"-b", "15/02/2002",
]);
self::assertSame(2, $dest->enable);
self::assertSame("15/02/2002", $dest->beforeDate);
$dest = new AbcdDest();
self::parse($dest, [
["-a", "--a"],
["-b", "--b"],
["-c", "--c"],
], [
"-abbccc"
]);
self::assertSame(1, $dest->getA());
self::assertSame(2, $dest->getB());
self::assertSame(3, $dest->getC());
}
function testArgs() {
$defs = [
["-a", "--enable"],
];
unset($dest);
self::parse($dest, $defs, []);
self::assertObjectNotHasAttribute("enable", $dest);
self::assertObjectHasAttribute("args", $dest);
self::assertSame([], $dest->args);
unset($dest);
self::parse($dest, $defs, ["x"]);
self::assertObjectNotHasAttribute("enable", $dest);
self::assertObjectHasAttribute("args", $dest);
self::assertSame(["x"], $dest->args);
unset($dest);
self::parse($dest, $defs, ["x", "-a"]);
self::assertObjectHasAttribute("enable", $dest);
self::assertObjectHasAttribute("args", $dest);
self::assertSame(1, $dest->enable);
self::assertSame(["x"], $dest->args);
$dest = [];
self::parse($dest, $defs, []);
self::assertArrayNotHasKey("enable", $dest);
self::assertArrayHasKey("args", $dest);
self::assertSame([], $dest["args"]);
$dest = [];
self::parse($dest, $defs, ["x"]);
self::assertArrayNotHasKey("enable", $dest);
self::assertArrayHasKey("args", $dest);
self::assertSame(["x"], $dest["args"]);
$dest = [];
self::parse($dest, $defs, ["x", "-a"]);
self::assertArrayHasKey("enable", $dest);
self::assertArrayHasKey("args", $dest);
self::assertSame(1, $dest["enable"]);
self::assertSame(["x"], $dest["args"]);
}
function testOpts() {
$defs = [
["-o", "--o", "args" => [["value"]]],
["--mm", "args" => ["value", "value"]],
["--mo", "args" => ["value", ["value"]]],
["--oo", "args" => [["value", "value"]]],
["--ma", "args" => ["value", null]],
["--a", "args" => [null]],
];
unset($dest); self::parse($dest, $defs, ["-o"]);
self::assertSame(null, $dest->o);
self::assertSame([], $dest->args);
unset($dest); self::parse($dest, $defs, ["-o", "1"]);
self::assertSame(null, $dest->o);
self::assertSame(["1"], $dest->args);
unset($dest); self::parse($dest, $defs, ["-o1"]);
self::assertSame("1", $dest->o);
self::assertSame([], $dest->args);
unset($dest); self::parse($dest, $defs, ["-o1", "2"]);
self::assertSame("1", $dest->o);
self::assertSame(["2"], $dest->args);
unset($dest); self::parse($dest, $defs, ["--o"]);
self::assertSame(null, $dest->o);
self::assertSame([], $dest->args);
unset($dest); self::parse($dest, $defs, ["--o", "1"]);
self::assertSame(null, $dest->o);
self::assertSame(["1"], $dest->args);
unset($dest); self::parse($dest, $defs, ["--o=1"]);
self::assertSame("1", $dest->o);
self::assertSame([], $dest->args);
unset($dest); self::parse($dest, $defs, ["--o=1", "2"]);
self::assertSame("1", $dest->o);
self::assertSame(["2"], $dest->args);
unset($dest); self::parse($dest, $defs, ["--mm", "1", "2"]);
self::assertSame(["1", "2"], $dest->mm);
self::assertSame([], $dest->args);
unset($dest); self::parse($dest, $defs, ["--mo", "1"]);
self::assertSame(["1"], $dest->mo);
self::assertSame([], $dest->args);
unset($dest); self::parse($dest, $defs, ["--mo", "1", "2"]);
self::assertSame(["1", "2"], $dest->mo);
self::assertSame([], $dest->args);
unset($dest); self::parse($dest, $defs, ["--mo", "1", "2", "3"]);
self::assertSame(["1", "2"], $dest->mo);
self::assertSame(["3"], $dest->args);
unset($dest); self::parse($dest, $defs, ["--oo"]);
self::assertSame([], $dest->oo);
self::assertSame([], $dest->args);
unset($dest); self::parse($dest, $defs, ["--oo", "1"]);
self::assertSame(["1"], $dest->oo);
self::assertSame([], $dest->args);
unset($dest); self::parse($dest, $defs, ["--oo", "1", "2"]);
self::assertSame(["1", "2"], $dest->oo);
self::assertSame([], $dest->args);
unset($dest); self::parse($dest, $defs, ["--oo", "1", "2", "3"]);
self::assertSame(["1", "2"], $dest->oo);
self::assertSame(["3"], $dest->args);
}
function testDefaultOptArgs() {
$defs = [
["-o", "--o", "args" => [["value"]],
"value" => "1",
],
];
unset($dest); self::parse($dest, $defs, ["-o"]);
self::assertSame("1", $dest->o);
self::assertSame([], $dest->args);
unset($dest); self::parse($dest, $defs, ["-o2"]);
self::assertSame("2", $dest->o);
self::assertSame([], $dest->args);
}
function testParseDefs() {
$defs = [
["-a", "--a"],
["x",
"cmd_args" => [
["-b", "--b"],
],
],
];
$parser = new ArgsParser($defs);
$parser->select(null);
$args = $parser->normArgs(["-a", "x", "-b"]);
self::assertSame(["-a", "x", "-b", "--"], $args);
$dest = new stdClass();
$parser->select(null);
$parser->parseArgs($args);
self::assertSame(["x"], $dest->command);
self::assertSame(1, $dest->a);
self::assertSame(1, $dest->b);
$defs = [
["-a", "--a"],
["x",
"cmd_args" => [
["-b", "--b"],
["y",
"cmd_args" => [
["-c", "--c"],
],
],
],
],
];
$parser = new ArgsParser($defs);
$parser->select(null);
$args = $parser->normArgs(["-a", "x", "-b", "y", "-c"]);
self::assertSame(["-a", "x", "-b", "y", "-c", "--"], $args);
$dest = new stdClass();
$parser->select(null);
$parser->parseArgs($args);
self::assertSame(["x", "y"], $dest->command);
self::assertSame(1, $dest->a);
self::assertSame(1, $dest->b);
self::assertSame(1, $dest->c);
}
function testTypes() {
$parser = new ArgsParser([
["-a:", "type" => "int"],
["-b:", "type" => "bool"],
["-c:", "type" => "string"],
["-d:", "type" => "array"],
]);
$dest = new AbcdDest();
$parser->parse($dest, ["-a0", "-b0", "-c0", "-d", ""]);
self::assertSame(0, $dest->getA());
self::assertSame(false, $dest->getB());
self::assertSame("0", $dest->getC());
self::assertSame([""], $dest->getD());
$dest = new AbcdDest();
$parser->parse($dest, ["-a1", "-b1", "-c1", "-d1"]);
self::assertSame(1, $dest->getA());
self::assertSame(true, $dest->getB());
self::assertSame("1", $dest->getC());
self::assertSame(["1"], $dest->getD());
$dest = new AbcdDest();
$parser->parse($dest, [
"-a1", "-a2",
"-b1", "-b2",
"-c1", "-c2",
"-d1", "-d2",
]);
self::assertSame(2, $dest->getA());
self::assertSame(true, $dest->getB());
self::assertSame("2", $dest->getC());
self::assertSame(["2"], $dest->getD());
$parser = new ArgsParser([
["-a:", "type" => "array", "action" => "--add"],
["-b:", "type" => "array", "action" => "--merge"],
["-c:", "type" => "int", "action" => "--add"],
["-d:", "type" => "int", "action" => "--merge"],
]);
$dest = new AbcdDest();
$parser->parse($dest, [
"-a1", "-a2",
"-b1", "-b2",
"-c1", "-c2",
"-d1", "-d2",
]);
self::assertSame([["1"], ["2"]], $dest->getA());
self::assertSame(["1", "2"], $dest->getB());
self::assertSame([1, 2], $dest->getC());
self::assertSame([1, 2], $dest->getD());
$parser = new ArgsParser([
["-a:", "type" => "array", "action" => "--merge"],
["-b:", "args" => ["value", [null]], "type" => "array", "action" => "--merges"],
]);
$dest = new AbcdDest();
$parser->parse($dest, [
"-a1", "-a2,3", "-a4,5,6",
"-b1", "2,3", "4,5,6", "--",
]);
self::assertSame(["1", "2", "3", "4", "5", "6"], $dest->getA());
self::assertSame(["1", "2", "3", "4", "5", "6"], $dest->getB());
}
function testRemainArgs() {
$dest = new SimpleDest();
## nombre quelconque d'arguments
$parser = new ArgsParser([
["args" => [null], "name" => "args"],
]);
# 0
$dest->reset();
$parser->parse($dest, []);
self::assertSame([], $dest->args);
# 1
$dest->reset();
$parser->parse($dest, ["a"]);
self::assertSame(["a"], $dest->args);
# 2
$dest->reset();
$parser->parse($dest, ["a", "b"]);
self::assertSame(["a", "b"], $dest->args);
# un argument
$parser = new ArgsParser([
["args" => ["value"], "name" => "args"],
]);
# 0
$dest->reset();
self::assertException(ArgsException::class, function () use ($parser, &$dest) {
$parser->parse($dest, []);
});
# 1
$dest->reset();
$parser->parse($dest, ["a"]);
self::assertSame(["a"], $dest->args);
# 2
$dest->reset();
self::assertException(ArgsException::class, function () use ($parser, &$dest) {
$parser->parse($dest, ["a", "b"]);
});
# deux arguments
$parser = new ArgsParser([
["args" => ["value", "value"], "name" => "args"],
]);
# 0
$dest->reset();
self::assertException(ArgsException::class, function () use ($parser, &$dest) {
$parser->parse($dest, []);
});
# 1
$dest->reset();
self::assertException(ArgsException::class, function () use ($parser, &$dest) {
$parser->parse($dest, ["a"]);
});
# 2
$dest->reset();
$parser->parse($dest, ["a", "b"]);
self::assertSame(["a", "b"], $dest->args);
# 3
$dest->reset();
self::assertException(ArgsException::class, function () use ($parser, &$dest) {
$parser->parse($dest, ["a", "b", "c"]);
});
# un arguments obligatoire, un argument facultatif
$parser = new ArgsParser([
["args" => ["value", ["value"]], "name" => "args"],
]);
# 0
$dest->reset();
self::assertException(ArgsException::class, function () use ($parser, &$dest) {
$parser->parse($dest, []);
});
# 1
$dest->reset();
$parser->parse($dest, ["a"]);
self::assertSame(["a"], $dest->args);
# 2
$dest->reset();
$parser->parse($dest, ["a", "b"]);
self::assertSame(["a", "b"], $dest->args);
# 3
$dest->reset();
self::assertException(ArgsException::class, function () use ($parser, &$dest) {
$parser->parse($dest, ["a", "b", "c"]);
});
# un arguments obligatoire, nombre infini d'arguments facultatifs
$parser = new ArgsParser([
["args" => ["value", null], "name" => "args"],
]);
# 0
$dest->reset();
self::assertException(ArgsException::class, function () use ($parser, &$dest) {
$parser->parse($dest, []);
});
# 1
$dest->reset();
$parser->parse($dest, ["a"]);
self::assertSame(["a"], $dest->args);
# 2
$dest->reset();
$parser->parse($dest, ["a", "b"]);
self::assertSame(["a", "b"], $dest->args);
# 3
$dest->reset();
$parser->parse($dest, ["a", "b", "c"]);
self::assertSame(["a", "b", "c"], $dest->args);
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace nur\cli\impl;
class AbcdDest {
private $a;
protected $b;
public $c, $d;
function getA() {
return $this->a;
}
function getB() {
return $this->b;
}
function getC() {
return $this->c;
}
function getD() {
return $this->d;
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace nur\cli\impl;
class SimpleDest {
public $args;
function reset(): void {
$this->args = null;
}
}

View File

@ -0,0 +1,159 @@
<?php
namespace nur\config;
use PHPUnit\Framework\TestCase;
class ConfigManagerTest extends TestCase {
private static $static;
static function inc_static() { self::$static++; }
private $member;
function incMember() { $this->member++; }
function reset() {
self::$static = 0;
$this->member = 0;
}
function testConfigurators() {
$cm = new ConfigManager();
$cm->addConfigurator(new class($this) {
private static $self;
function __construct($self) {
self::$self = $self;
}
static function configure_static() {
self::$self::inc_static();
}
function configureMember() {
self::$self->incMember();
}
});
$this->reset();
$cm->resetConfiguration();
$cm->configure();
self::assertSame(1, self::$static);
self::assertSame(1, $this->member);
$cm->configure();
self::assertSame(1, self::$static);
self::assertSame(1, $this->member);
$this->reset();
$cm->resetConfiguration();
$cm->configure(["exclude" => "static"]);
self::assertSame(0, self::$static);
self::assertSame(1, $this->member);
$cm->configure();
self::assertSame(1, self::$static);
self::assertSame(1, $this->member);
}
function testIsObject() {
$cm = new class extends ConfigManager {
const OBJECT_PKEY_PREFIXES = [
"x.y" => null,
];
};
self::assertFalse($cm->isObject(false, ""));
self::assertFalse($cm->isObject(false, "x"));
self::assertFalse($cm->isObject(false, "x.y"));
self::assertTrue($cm->isObject(false, "x.y.z"));
self::assertFalse($cm->isObject(false, "x.y.z.t"));
self::assertFalse($cm->isObject(false, "x.y.z.t.w"));
}
function testNewObject() {
$cm = new ConfigManager();
$def0 = [[MyClass0::class]];
$obj0 = $cm->newObject($def0);
self::assertInstanceOf(MyClass0::class, $obj0);
$def1 = [[MyClass1::class], "first", "second"];
$obj1 = $cm->newObject($def1);
self::assertInstanceOf(MyClass1::class, $obj1);
self::assertSame("first", $obj1->first);
self::assertSame("second", $obj1->second);
$def2 = [[MyClass2::class], "first" => "a", "second" => "b"];
$obj2 = $cm->newObject($def2);
self::assertInstanceOf(MyClass2::class, $obj2);
self::assertSame(["first" => "a", "second" => "b"], $obj2->options);
}
function testGetValue() {
$cm = new ConfigManager();
$cm->addConfig(DynConfigTest::class);
$cm->addConfig(new DynConfigTest());
self::assertSame("dyn", $cm->getValue("app.ov"));
$cm->addConfig(StaticConfigTest::class);
self::assertSame(1, $cm->getValue("app.dc.first"));
self::assertSame(2, $cm->getValue("app.dc.second"));
self::assertSame("a", $cm->getValue("app.sc.first"));
self::assertSame("b", $cm->getValue("app.sc.second"));
self::assertSame("static", $cm->getValue("app.ov"));
self::assertSame(1, $cm->getValue("app.dfirst_abs"));
self::assertSame(1, $cm->getValue("app.dfirst_rel"));
self::assertSame(2, $cm->getValue("app.dsecond"));
self::assertSame([5, 6, 7], $cm->getValue("app.darr567"));
self::assertSame([3, 4, 5], $cm->getValue("app.darr345"));
self::assertSame([1, 2], $cm->getValue("app.d12"));
putenv('CONFIG_ALL_app__ec__first=Z');
putenv('JSON_CONFIG_ALL_app__ec__array={"a":1,"b":"2"}');
$cm->addConfig(EnvConfig::class);
self::assertSame("Z", $cm->getValue("app.ec.first"));
self::assertSame(["a" => 1, "b" => "2"], $cm->getValue("app.ec.array"));
}
}
class MyClass0 {
function __construct() {
}
}
class MyClass1 {
public $first;
public $second;
function __construct($first, $second) {
$this->first = $first;
$this->second = $second;
}
}
class MyClass2 {
public $options;
function __construct(array $options) {
$this->options = $options;
}
}
class DynConfigTest extends ArrayConfig {
function APP(): array {
return [
"dc" => [
"first" => 1,
"second" => 2,
],
"ov" => "dyn",
"arr" => [5],
"dfirst_abs" => [["ref"], "app.dc.first"],
"dfirst_rel" => [["ref"], ".dc.first"],
"darr567" => [["aref"], ".arr", [6, 7]],
];
}
}
class StaticConfigTest {
const APP = [
"sc" => [
"first" => "a",
"second" => "b",
],
"ov" => "static",
"dsecond" => [["ref"], ".dc.second"],
"darr345" => [["pref"], ".arr", [3, 4]],
"d12" => [["refs"], ".dc", "first", "second"],
];
}

View File

@ -0,0 +1,39 @@
<?php
namespace nur\data\template;
use nur\b\io\StringWriter;
use PHPUnit\Framework\TestCase;
class StreamTemplateTest extends TestCase {
function testApply() {
$sw = new StringWriter();
$t = new MyTemplate($sw);
$t["texte"] = "truc";
$t->apply();
self::assertSame(
"Ceci est un TEST de truc\nqui bouge pas _et_ qui bouge\n",
$sw->getString()
);
$t["texte"] = "machin";
$t->apply();
self::assertSame(
"Ceci est un TEST de machin\nqui bouge pas _et_ qui bouge\n",
$sw->getString()
);
}
}
class MyTemplate extends StreamTemplate {
const INPUT = __DIR__ . "/StreamTemplateTest.txt";
const EXPRS = [
"TEXTE" => "texte",
"statique" => "::get_statique",
"dynamique" => "->getDynamique",
];
static function get_statique(): string {
return "qui bouge pas";
}
function getDynamique(): string {
return "qui bouge";
}
}

View File

@ -0,0 +1,2 @@
Ceci est un TEST de TEXTE
statique _et_ dynamique

View File

@ -0,0 +1,30 @@
<?php
namespace nur\data\template;
use PHPUnit\Framework\TestCase;
class StringTemplateTest extends TestCase {
function testApply() {
$t = new MyStringTemplate();
$t["texte"] = "truc";
self::assertSame("Ceci est un TEST de truc qui bouge pas et qui bouge", $t->apply());
$t["texte"] = "machin";
self::assertSame("Ceci est un TEST de machin qui bouge pas et qui bouge", $t->apply());
}
}
class MyStringTemplate extends StringTemplate {
const TEXT = "Ceci est un TEST de TEXTE statique et dynamique";
const EXPRS = [
"TEXTE" => "texte",
"statique" => "::get_statique",
"dynamique" => "->getDynamique",
];
static function get_statique(): string {
return "qui bouge pas";
}
static function getDynamique(): string {
return "qui bouge";
}
}

View File

@ -0,0 +1,168 @@
<?php
namespace nur\data\types;
use nur\t\TestCase;
class BoolTypeTest extends TestCase {
function testIs_yes() {
self::assertTrue(BoolType::is_yes(true));
self::assertTrue(BoolType::is_yes("true"));
self::assertTrue(BoolType::is_yes("t"));
self::assertTrue(BoolType::is_yes("yes"));
self::assertTrue(BoolType::is_yes("y"));
self::assertTrue(BoolType::is_yes("vrai"));
self::assertTrue(BoolType::is_yes("v"));
self::assertTrue(BoolType::is_yes("oui"));
self::assertTrue(BoolType::is_yes("o"));
self::assertTrue(BoolType::is_yes("1"));
self::assertTrue(BoolType::is_yes("2"));
self::assertTrue(BoolType::is_yes("153"));
self::assertFalse(BoolType::is_yes(null));
self::assertFalse(BoolType::is_yes(false));
self::assertFalse(BoolType::is_yes(""));
self::assertFalse(BoolType::is_yes("0"));
self::assertFalse(BoolType::is_yes("-1"));
}
function testIs_no() {
self::assertTrue(BoolType::is_no(false));
self::assertTrue(BoolType::is_no(null));
self::assertTrue(BoolType::is_no("false"));
self::assertTrue(BoolType::is_no("f"));
self::assertTrue(BoolType::is_no("no"));
self::assertTrue(BoolType::is_no("n"));
self::assertTrue(BoolType::is_no("faux"));
self::assertTrue(BoolType::is_no("non"));
self::assertTrue(BoolType::is_no("0"));
self::assertFalse(BoolType::is_no(true));
self::assertFalse(BoolType::is_no(""));
self::assertFalse(BoolType::is_no("1"));
self::assertFalse(BoolType::is_no("-1"));
}
function testFormat() {
$b = new BoolType();
self::assertSame("Oui", $b->format(true));
self::assertSame("Non", $b->format(false));
self::assertSame("Non", $b->format(null));
$b = new BoolType(["format" => "on"]);
self::assertSame("O", $b->format(true));
self::assertSame("N", $b->format(false));
self::assertSame("N", $b->format(null));
$b = new BoolType(["format" => "onn"]);
self::assertSame("O", $b->format(true));
self::assertSame("N", $b->format(false));
self::assertSame("", $b->format(null));
$b = new BoolType(["format" => "ouinon"]);
self::assertSame("Oui", $b->format(true));
self::assertSame("Non", $b->format(false));
self::assertSame("Non", $b->format(null));
$b = new BoolType(["format" => "ouinonnull"]);
self::assertSame("Oui", $b->format(true));
self::assertSame("Non", $b->format(false));
self::assertSame("", $b->format(null));
$b = new BoolType(["format" => "xn"]);
self::assertSame("X", $b->format(true));
self::assertSame("", $b->format(false));
self::assertSame("", $b->format(null));
}
function testParse() {
$b = new BoolType();
$input = "";
self::assertSame(false, $b->parse($input));
self::assertSame("", $input);
$input = "whatever";
self::assertSame(false, $b->parse($input));
self::assertSame("whatever", $input);
$input = "true";
self::assertSame("true", $b->parse($input));
self::assertSame("", $input);
$input = "false";
self::assertSame("false", $b->parse($input));
self::assertSame("", $input);
$input = "yoyo";
self::assertSame("y", $b->parse($input));
self::assertSame("oyo", $input);
}
function testVerifix() {
$b = new BoolType();
$value = true; $b->verifix($value, $result);
self::assertSame(true, $value);
self::assertSame(["valid" => true, "value" => true, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => true, "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = false; $b->verifix($value, $result);
self::assertSame(false, $value);
self::assertSame(["valid" => true, "value" => false, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => false, "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = null; $b->verifix($value, $result);
self::assertSame(false, $value);
self::assertSame(["valid" => true, "value" => false, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => null, "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = ""; $b->verifix($value, $result);
self::assertSame(false, $value);
self::assertSame(["valid" => true, "value" => false, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "", "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = "0"; $b->verifix($value, $result);
self::assertSame(false, $value);
self::assertSame(["valid" => true, "value" => false, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "0", "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = "no"; $b->verifix($value, $result);
self::assertSame(false, $value);
self::assertSame(["valid" => true, "value" => false, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "no", "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = "yes"; $b->verifix($value, $result);
self::assertSame(true, $value);
self::assertSame(["valid" => true, "value" => true, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "yes", "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = "whatever"; $b->verifix($value, $result);
self::assertSame(true, $value);
self::assertSame(["valid" => true, "value" => true, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "whatever", "orig_desc" => null, "parsed" => false, "remains" => false], $result);
}
function testTo_bool() {
self::assertTrue(BoolType::to_bool(true));
self::assertTrue(BoolType::to_bool(1));
self::assertTrue(BoolType::to_bool("1"));
self::assertTrue(BoolType::to_bool("yes"));
self::assertTrue(BoolType::to_bool("whatever"));
self::assertFalse(BoolType::to_bool(false));
self::assertFalse(BoolType::to_bool(0));
self::assertFalse(BoolType::to_bool("0"));
self::assertFalse(BoolType::to_bool("no"));
self::assertFalse(BoolType::to_bool(null));
self::assertFalse(BoolType::to_bool(""));
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace nur\data\types;
use nur\t\TestCase;
class CTimeslotTypeTest extends TestCase {
function test() {
$type = new CTimeslotType();
self::assertSame(null, $type->with(null));
self::assertSame(false, $type->with(false));
self::assertSame(null, $type->with(""));
self::assertSame([
"ts_start" => "15h00",
"ts_end" => "20h00",
], $type->with("15-20"));
self::assertSame([
"ts_start" => "15h00",
"ts_end" => null,
], $type->with("15"));
self::assertSame([
"ts_start" => "15h00",
"ts_end" => null,
], $type->with("15-"));
self::assertSame([
"ts_start" => null,
"ts_end" => "20h00",
], $type->with("-20"));
}
}

View File

@ -0,0 +1,96 @@
<?php
namespace nur\data\types;
use nur\b\ValueException;
use nur\data\types\impl\CBcType;
use nur\data\types\impl\CSiType;
use nur\data\types\impl\CSniType;
use nur\data\types\impl\CSsType;
use nur\t\TestCase;
class CompositeTypeTest extends TestCase {
function test1() {
$schema = [
"a" => ["string"],
"b" => ["int", "composite" => true],
"c" => ["string", "composite" => true],
"" => [
"ctypes" => [
"bc" => CBcType::class,
],
],
];
$md = new Metadata($schema);
$bcType = $md->getType("bc");
$item = null;
$md->ensureSchema($item);
self::assertSame(["a" => "", "b" => 0, "c" => ""], $item);
$bc = $bcType->with("4 hello");
self::assertSame(["b" => 4, "c" => "hello"], $bc);
$item = ["a" => "first", "b" => 2, "c" => "third"];
self::assertSame("2 third", $bcType->format($item));
$item["b"] = "4";
$item["c"] = " hello ";
$bcType->verifix($item, $result);
self::assertSame(["a" => "first", "b" => 4, "c" => "hello"], $item);
self::assertSame("4 hello", $bcType->format($item));
$item["b"] = "not a number";
self::assertFalse($bcType->verifix($item, $result));
self::assertFalse($result["cresults"]["b"]["valid"]);
self::assertSame(["a" => "first", "b" => "not a number", "c" => "hello"], $item);
}
function test2() {
$schema = [
"x" => ["string"],
"y" => ["int", "composite" => true],
"z" => ["string", "composite" => true],
"" => [
"ctypes" => [
"bc" => [CBcType::class, [
"ckeys" => ["y", "z"]
]],
],
],
];
$md = new Metadata($schema);
$bcType = $md->getType("bc");
$bc = $bcType->with("4 hello");
self::assertSame(["y" => 4, "z" => "hello"], $bc);
$item = [
"x" => "hello",
"y" => 42,
"z" => "world",
];
self::assertSame("42 world", $bcType->format($item));
}
function testConsume1() {
$t = new CSiType();
self::assertSame(["s" => "first", "i" => 15], $t->with("first-15"));
self::assertSame(["s" => "first", "i" => 15], $t->with("first 15"));
self::assertSame(["s" => "first", "i" => 15], $t->with("first - 15"));
self::assertException(ValueException::class, function() use($t) {
$t->with("abc-15");
});
self::assertSame(["s" => "first", "i" => null], $t->with("first"));
self::assertSame(["s" => "first", "i" => null], $t->with("first-"));
}
function testConsume2() {
$t = new CSsType();
self::assertSame(["s1" => "aa", "s2" => "ayes"], $t->with("aaayes"));
self::assertSame(["s1" => "bxx", "s2" => null], $t->with("bxx"));
self::assertSame(["s1" => "bxxx", "s2" => null], $t->with("bxxx"));
self::assertSame(["s1" => "bxxx", "s2" => "x"], $t->with("bxxxx"));
self::assertSame(["s1" => "bxxx", "s2" => "xx"], $t->with("bxxxxx"));
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace nur\data\types;
use nur\b\ValueException;
use nur\t\TestCase;
use stdClass;
class ContentTypeTest extends TestCase {
function testWith() {
$type = new ContentType();
self::assertSame(false, $type->with(false));
self::assertSame(null, $type->with(null));
self::assertSame("", $type->with(""));
self::assertSame(" ", $type->with(" "));
self::assertSame( " abc ", $type->with(" abc "));
self::assertSame("abc", $type->with("abc"));
self::assertSame(-1, $type->with(-1));
self::assertSame(0, $type->with(0));
self::assertSame(1, $type->with(1));
self::assertSame(1.2, $type->with(1.2));
self::assertSame(true, $type->with(true));
self::assertSame([], $type->with([]));
self::assertSame([1], $type->with([1]));
self::assertSame(["abc"], $type->with(["abc"]));
self::assertException(ValueException::class, [$type, "with"], new stdClass());
self::assertException(ValueException::class, [$type, "with"], [new stdClass()]);
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace nur\data\types;
use nur\b\ValueException;
use nur\t\TestCase;
use stdClass;
class FileTypeTest extends TestCase {
function testWith() {
$type = new FileType();
self::assertSame(false, $type->with(false));
self::assertSame(null, $type->with(null));
self::assertSame([], $type->with([]));
self::assertSame([1], $type->with([1]));
self::assertSame(["abc"], $type->with(["abc"]));
$file = [
"name" => "image.jpg",
"type" => "image/jpeg",
"size" => 1537,
"tmp_name" => "/tmp/xyzstu",
"error" => UPLOAD_ERR_OK,
];
self::assertSame($file, $type->with($file));
self::assertException(ValueException::class, [$type, "with"], "");
self::assertException(ValueException::class, [$type, "with"], 1);
self::assertException(ValueException::class, [$type, "with"], 1.2);
self::assertException(ValueException::class, [$type, "with"], true);
self::assertException(ValueException::class, [$type, "with"], new stdClass());
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace nur\data\types;
use nur\data\types\impl\Point;
use nur\t\TestCase;
class GenericTypeTest extends TestCase {
static function assertPoint(?Point $a, ?Point $b) {
if ($a === null) self::assertNull($b);
if ($b === null) self::assertNull($a);
if ($a !== null && $b !== null) {
self::assertSame($a->x, $b->x);
self::assertSame($a->y, $b->y);
}
}
function testVerifix() {
$type = new GenericType(Point::class);
$value = null; $type->verifix($value);
self::assertPoint(null, $value);
$value = 1; $type->verifix($value);
self::assertPoint(new Point(1, Point::Y), $value);
$value = [1]; $type->verifix($value);
self::assertPoint(new Point(1, Point::Y), $value);
$value = [1, 2]; $type->verifix($value);
self::assertPoint(new Point(1, 2), $value);
}
}

View File

@ -0,0 +1,209 @@
<?php
namespace nur\data\types;
use nur\b\ValueException;
use nur\t\TestCase;
class IntTypeTest extends TestCase {
function testFormat() {
$i = new IntType();
self::assertSame("", $i->format(null));
self::assertSame("", $i->format(false));
self::assertSame("0", $i->format(0));
self::assertSame("1", $i->format(1));
self::assertSame("155", $i->format(155));
self::assertSame("-302", $i->format(-302));
}
function testParse() {
$i = new IntType();
$input = "";
self::assertSame(false, $i->parse($input));
self::assertSame("", $input);
$input = "whatever";
self::assertSame(false, $i->parse($input));
self::assertSame("whatever", $input);
$input = "0";
self::assertSame("0", $i->parse($input));
self::assertSame("", $input);
$input = "1";
self::assertSame("1", $i->parse($input));
self::assertSame("", $input);
$input = "155";
self::assertSame("155", $i->parse($input));
self::assertSame("", $input);
$input = "155BAD";
self::assertSame("155", $i->parse($input));
self::assertSame("BAD", $input);
$input = "-302";
self::assertSame("-302", $i->parse($input));
self::assertSame("", $input);
$input = "-302BAD";
self::assertSame("-302", $i->parse($input));
self::assertSame("BAD", $input);
$input = " 257 ";
self::assertSame(false, $i->parse($input));
self::assertSame(" 257 ", $input);
$input = " 257 BAD ";
self::assertSame(false, $i->parse($input));
self::assertSame(" 257 BAD ", $input);
}
function testVerifix() {
$i = new IntType();
$value = null; $i->verifix($value, $result);
self::assertSame(null, $value);
self::assertSame(["valid" => true, "value" => null, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => null, "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = false; $i->verifix($value, $result);
self::assertSame(false, $value);
self::assertSame(["valid" => true, "value" => false, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => false, "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = 0; $i->verifix($value, $result);
self::assertSame(0, $value);
self::assertSame(["valid" => true, "value" => 0, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => 0, "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = 1; $i->verifix($value, $result);
self::assertSame(1, $value);
self::assertSame(["valid" => true, "value" => 1, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => 1, "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = ""; $i->verifix($value, $result);
self::assertSame(null, $value);
self::assertSame(["valid" => true, "value" => null, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "", "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = "whatever"; $i->verifix($value, $result);
$error = str_replace("{value_desc}", var_export($value, true), IntType::MESSAGES["invalid"]);
self::assertSame("whatever", $value);
self::assertSame(["valid" => false, "value" => "whatever", "value_desc" => null,
"error_code" => "invalid", "error" => $error, "exception" => null,
"orig" => "whatever", "orig_desc" => null, "parsed" => '', "remains" => "whatever"], $result);
$value = "0"; $i->verifix($value, $result);
self::assertSame(0, $value);
self::assertSame(["valid" => true, "value" => 0, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "0", "orig_desc" => null, "parsed" => "0", "remains" => ""], $result);
$value = "1"; $i->verifix($value, $result);
self::assertSame(1, $value);
self::assertSame(["valid" => true, "value" => 1, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "1", "orig_desc" => null, "parsed" => "1", "remains" => ""], $result);
$value = "155"; $i->verifix($value, $result);
self::assertSame(155, $value);
self::assertSame(["valid" => true, "value" => 155, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "155", "orig_desc" => null, "parsed" => "155", "remains" => ""], $result);
$value = "155BAD"; $i->verifix($value, $result);
$error = str_replace("{value_desc}", var_export($value, true), IntType::MESSAGES["invalid"]);
self::assertSame("155BAD", $value);
self::assertSame(["valid" => false, "value" => "155BAD", "value_desc" => null,
"error_code" => "invalid", "error" => $error, "exception" => null,
"orig" => "155BAD", "orig_desc" => null, "parsed" => "155", "remains" => "BAD"], $result);
$value = "-302"; $i->verifix($value, $result);
self::assertSame(-302, $value);
self::assertSame(["valid" => true, "value" => -302, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "-302", "orig_desc" => null, "parsed" => "-302", "remains" => ""], $result);
$value = "-302BAD"; $i->verifix($value, $result);
$error = str_replace("{value_desc}", var_export($value, true), IntType::MESSAGES["invalid"]);
self::assertSame("-302BAD", $value);
self::assertSame(["valid" => false, "value" => "-302BAD", "value_desc" => null,
"error_code" => "invalid", "error" => $error, "exception" => null,
"orig" => "-302BAD", "orig_desc" => null, "parsed" => "-302", "remains" => "BAD"], $result);
$value = " 257 "; $i->verifix($value, $result);
self::assertSame(257, $value);
self::assertSame(["valid" => true, "value" => 257, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => " 257 ", "orig_desc" => null, "parsed" => "257", "remains" => ""], $result);
$value = " 257 BAD "; $i->verifix($value, $result);
$error = str_replace("{value_desc}", var_export($value, true), IntType::MESSAGES["invalid"]);
self::assertSame(" 257 BAD ", $value);
self::assertSame(["valid" => false, "value" => " 257 BAD ", "value_desc" => null,
"error_code" => "invalid", "error" => $error, "exception" => null,
"orig" => " 257 BAD ", "orig_desc" => null, "parsed" => "257", "remains" => " BAD"], $result);
}
function testTo_zint() {
self::assertFalse(IntType::to_zint(false));
self::assertNull(IntType::to_zint(null));
self::assertNull(IntType::to_zint(""));
self::assertSame(0, IntType::to_zint(0));
self::assertSame(0, IntType::to_zint("0"));
self::assertSame(0, IntType::to_zint("00"));
self::assertSame(1, IntType::to_int(true));
self::assertSame(1, IntType::to_zint(1));
self::assertSame(1, IntType::to_zint("1"));
self::assertSame(155, IntType::to_zint(155));
self::assertSame(155, IntType::to_zint("155"));
self::assertSame(-302, IntType::to_zint(-302));
self::assertSame(-302, IntType::to_zint("-302"));
self::assertSame(257, IntType::to_zint(257));
self::assertSame(257, IntType::to_zint(" 257 "));
self::assertSame(1, IntType::to_zint(1.234));
self::assertException(ValueException::class, [IntType::class, "to_zint"], "whatever");
self::assertException(ValueException::class, [IntType::class, "to_zint"], "1.234");
}
function testTo_int() {
self::assertSame(0, IntType::to_int(false));
self::assertSame(0, IntType::to_int(null));
self::assertSame(0, IntType::to_int(""));
self::assertSame(0, IntType::to_int(0));
self::assertSame(0, IntType::to_int("0"));
self::assertSame(0, IntType::to_int("00"));
self::assertSame(1, IntType::to_int(true));
self::assertSame(1, IntType::to_int(1));
self::assertSame(1, IntType::to_int("1"));
self::assertSame(155, IntType::to_int(155));
self::assertSame(155, IntType::to_int("155"));
self::assertSame(-302, IntType::to_int(-302));
self::assertSame(-302, IntType::to_int("-302"));
self::assertSame(257, IntType::to_int(257));
self::assertSame(257, IntType::to_int(" 257 "));
self::assertSame(1, IntType::to_int(1.234));
self::assertException(ValueException::class, [IntType::class, "to_int"], "whatever");
self::assertException(ValueException::class, [IntType::class, "to_int"], "1.234");
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace nur\data\types;
use nur\b\ValueException;
use nur\t\TestCase;
use stdClass;
class KeyTypeTest extends TestCase {
function testWith() {
$type = new KeyType();
self::assertSame(false, $type->with(false));
self::assertSame(null, $type->with(null));
self::assertSame("", $type->with(""));
self::assertSame(" ", $type->with(" "));
self::assertSame( " abc ", $type->with(" abc "));
self::assertSame("abc", $type->with("abc"));
self::assertSame(-1, $type->with(-1));
self::assertSame(0, $type->with(0));
self::assertSame(1, $type->with(1));
self::assertException(ValueException::class, [$type, "with"], []);
self::assertException(ValueException::class, [$type, "with"], ["x"]);
self::assertException(ValueException::class, [$type, "with"], 1.2);
self::assertException(ValueException::class, [$type, "with"], true);
self::assertException(ValueException::class, [$type, "with"], new stdClass());
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace nur\data\types;
use nur\t\TestCase;
class MailTypeTest extends TestCase {
function testWith() {
$type = new MailType();
self::assertSame("jephte.clain@univ-reunion.fr", $type->with(" Jephte.Clain@univ-reunion.fr "));
self::assertSame("jephte.clain@univ-reunion.fr", $type->with(" Jephte.Clain @ univ-reunion.fr "));
self::assertSame("ADMIN@univ-reunion.fr", $type->with(" ADMIN@univ-reunion.fr "));
self::assertSame("jephte.clain+MAILBOX@univ-reunion.fr", $type->with("JEPHTE.CLAIN+MAILBOX@UNIV-REUNION.FR"));
self::assertSame("JEPHTECLAIN+MAILBOX@univ-reunion.fr", $type->with("JEPHTECLAIN+MAILBOX@UNIV-REUNION.FR"));
self::assertSame("JEPHTE.CLAIN!+MAILBOX@UNIV-REUNION.FR", $type->with("JEPHTE.CLAIN!+MAILBOX@UNIV-REUNION.FR"));
}
}

View File

@ -0,0 +1,674 @@
<?php
namespace nur\data\types;
use nur\b\coll\FancyArray;
use nur\b\coll\TGenericArray;
use nur\b\ValueException;
use nur\data\types\impl\CAbType;
use nur\php\Autogen;
use nur\t\TestCase;
class MetadataTest extends TestCase {
const ENSURE_STRUCTURE_DATA = [
## type, default, absent, input, present
# null
[null, null, null, null, null],
[null, null, null, false, null],
[null, null, null, "value", "value"],
[null, false, false, null, null],
[null, false, false, false, false],
[null, false, false, "value", "value"],
[null, "default", "default", null, null],
[null, "default", "default", false, "default"],
[null, "default", "default", "value", "value"],
# mixed
["mixed", null, null, null, null],
["mixed", null, null, false, false],
["mixed", null, null, "value", "value"],
["mixed", false, false, null, null],
["mixed", false, false, false, false],
["mixed", false, false, "value", "value"],
["mixed", "default", "default", null, null],
["mixed", "default", "default", false, false],
["mixed", "default", "default", "value", "value"],
# bool
["?bool", null, null, null, null],
["?bool", null, null, false, false],
["?bool", null, null, "value", true],
["?bool", false, false, null, null],
["?bool", false, false, false, false],
["?bool", false, false, "value", true],
["?bool", true, true, null, null],
["?bool", true, true, false, false],
["?bool", true, true, "value", true],
["?bool", "default", true, null, null],
["?bool", "default", true, false, false],
["?bool", "default", true, "value", true],
["bool", null, false, null, false],
["bool", null, false, false, false],
["bool", null, false, "value", true],
["bool", false, false, null, false],
["bool", false, false, false, false],
["bool", false, false, "value", true],
["bool", true, true, null, true],
["bool", true, true, false, false],
["bool", true, true, "value", true],
["bool", "default", true, null, true],
["bool", "default", true, false, false],
["bool", "default", true, "value", true],
# int
["?int", null, null, null, null],
["?int", null, null, false, null],
["?int", null, null, "value", 0],
["?int", null, null, 123, 123],
["?int", false, 0, null, null],
["?int", false, 0, false, 0],
["?int", false, 0, "value", 0],
["?int", false, 0, 123, 123],
["?int", 42, 42, null, null],
["?int", 42, 42, false, 42],
["?int", 42, 42, "value", 0],
["?int", 42, 42, 123, 123],
["?int", "default", 0, null, null],
["?int", "default", 0, false, 0],
["?int", "default", 0, "value", 0],
["?int", "default", 0, 123, 123],
["int", null, 0, null, 0],
["int", null, 0, false, 0],
["int", null, 0, "value", 0],
["int", null, 0, 123, 123],
["int", false, 0, null, 0],
["int", false, 0, false, 0],
["int", false, 0, "value", 0],
["int", false, 0, 123, 123],
["int", 42, 42, null, 42],
["int", 42, 42, false, 42],
["int", 42, 42, "value", 0],
["int", 42, 42, 123, 123],
["int", "default", 0, null, 0],
["int", "default", 0, false, 0],
["int", "default", 0, "value", 0],
["int", "default", 0, 123, 123],
# float
["?float", null, null, null, null],
["?float", null, null, false, null],
["?float", null, null, "value", 0.0],
["?float", null, null, 123, 123.0],
["?float", false, 0.0, null, null],
["?float", false, 0.0, false, 0.0],
["?float", false, 0.0, "value", 0.0],
["?float", false, 0.0, 123, 123.0],
["?float", 42, 42.0, null, null],
["?float", 42, 42.0, false, 42.0],
["?float", 42, 42.0, "value", 0.0],
["?float", 42, 42.0, 123, 123.0],
["?float", "default", 0.0, null, null],
["?float", "default", 0.0, false, 0.0],
["?float", "default", 0.0, "value", 0.0],
["?float", "default", 0.0, 123, 123.0],
["float", null, 0.0, null, 0.0],
["float", null, 0.0, false, 0.0],
["float", null, 0.0, "value", 0.0],
["float", null, 0.0, 123, 123.0],
["float", false, 0.0, null, 0.0],
["float", false, 0.0, false, 0.0],
["float", false, 0.0, "value", 0.0],
["float", false, 0.0, 123, 123.0],
["float", 42, 42.0, null, 42.0],
["float", 42, 42.0, false, 42.0],
["float", 42, 42.0, "value", 0.0],
["float", 42, 42.0, 123, 123.0],
["float", "default", 0.0, null, 0.0],
["float", "default", 0.0, false, 0.0],
["float", "default", 0.0, "value", 0.0],
["float", "default", 0.0, 123, 123.0],
# string
["?string", null, null, null, null],
["?string", null, null, false, null],
["?string", null, null, "value", "value"],
["?string", null, null, 123, "123"],
["?string", false, "", null, null],
["?string", false, "", false, ""],
["?string", false, "", "value", "value"],
["?string", false, "", 123, "123"],
["?string", 42, "42", null, null],
["?string", 42, "42", false, "42"],
["?string", 42, "42", "value", "value"],
["?string", 42, "42", 123, "123"],
["?string", "default", "default", null, null],
["?string", "default", "default", false, "default"],
["?string", "default", "default", "value", "value"],
["?string", "default", "default", 123, "123"],
["string", null, "", null, ""],
["string", null, "", false, ""],
["string", null, "", "value", "value"],
["string", null, "", 123, "123"],
["string", false, "", null, ""],
["string", false, "", false, ""],
["string", false, "", "value", "value"],
["string", false, "", 123, "123"],
["string", 42, "42", null, "42"],
["string", 42, "42", false, "42"],
["string", 42, "42", "value", "value"],
["string", 42, "42", 123, "123"],
["string", "default", "default", null, "default"],
["string", "default", "default", false, "default"],
["string", "default", "default", "value", "value"],
["string", "default", "default", 123, "123"],
# key
["?key", null, null, null, null],
["?key", null, null, false, null],
["?key", null, null, "value", "value"],
["?key", null, null, 123, 123],
["?key", false, "", null, null],
["?key", false, "", false, ""],
["?key", false, "", "value", "value"],
["?key", false, "", 123, 123],
["?key", 42, 42, null, null],
["?key", 42, 42, false, 42],
["?key", 42, 42, "value", "value"],
["?key", 42, 42, 123, 123],
["?key", "42", 42, null, null],
["?key", "42", 42, false, 42],
["?key", "42", 42, "value", "value"],
["?key", "42", 42, 123, 123],
["?key", "default", "default", null, null],
["?key", "default", "default", false, "default"],
["?key", "default", "default", "value", "value"],
["?key", "default", "default", 123, 123],
["key", null, "", null, ""],
["key", null, "", false, ""],
["key", null, "", "value", "value"],
["key", null, "", 123, 123],
["key", false, "", null, ""],
["key", false, "", false, ""],
["key", false, "", "value", "value"],
["key", false, "", 123, 123],
["key", 42, 42, null, 42],
["key", 42, 42, false, 42],
["key", 42, 42, "value", "value"],
["key", 42, 42, 123, 123],
["key", "42", 42, null, 42],
["key", "42", 42, false, 42],
["key", "42", 42, "value", "value"],
["key", "42", 42, 123, 123],
["key", "default", "default", null, "default"],
["key", "default", "default", false, "default"],
["key", "default", "default", "value", "value"],
["key", "default", "default", 123, 123],
# array
["?array", null, null, null, null],
["?array", null, null, false, null],
["?array", null, null, "value", ["value"]],
["?array", null, null, 123, [123]],
["?array", false, [], null, null],
["?array", false, [], false, []],
["?array", false, [], "value", ["value"]],
["?array", false, [], 123, [123]],
["?array", 42, [42], null, null],
["?array", 42, [42], false, [42]],
["?array", 42, [42], "value", ["value"]],
["?array", 42, [42], 123, [123]],
["?array", "default", ["default"], null, null],
["?array", "default", ["default"], false, ["default"]],
["?array", "default", ["default"], "value", ["value"]],
["?array", "default", ["default"], 123, [123]],
["array", null, [], null, []],
["array", null, [], false, []],
["array", null, [], "value", ["value"]],
["array", null, [], 123, [123]],
["array", false, [], null, []],
["array", false, [], false, []],
["array", false, [], "value", ["value"]],
["array", false, [], 123, [123]],
["array", 42, [42], null, [42]],
["array", 42, [42], false, [42]],
["array", 42, [42], "value", ["value"]],
["array", 42, [42], 123, [123]],
["array", "default", ["default"], null, ["default"]],
["array", "default", ["default"], false, ["default"]],
["array", "default", ["default"], "value", ["value"]],
["array", "default", ["default"], 123, [123]],
# array[]
["?array[]", null, null, null, null],
["?array[]", null, null, false, null],
["?array[]", null, null, "value", [["value"]]],
["?array[]", null, null, 123, [[123]]],
["?array[]", false, [], null, null],
["?array[]", false, [], false, []],
["?array[]", false, [], "value", [["value"]]],
["?array[]", false, [], 123, [[123]]],
["?array[]", 42, [[42]], null, null],
["?array[]", 42, [[42]], false, [[42]]],
["?array[]", 42, [[42]], "value", [["value"]]],
["?array[]", 42, [[42]], 123, [[123]]],
["?array[]", "default", [["default"]], null, null],
["?array[]", "default", [["default"]], false, [["default"]]],
["?array[]", "default", [["default"]], "value", [["value"]]],
["?array[]", "default", [["default"]], 123, [[123]]],
["array[]", null, [], null, []],
["array[]", null, [], false, []],
["array[]", null, [], "value", [["value"]]],
["array[]", null, [], 123, [[123]]],
["array[]", false, [], null, []],
["array[]", false, [], false, []],
["array[]", false, [], "value", [["value"]]],
["array[]", false, [], 123, [[123]]],
["array[]", 42, [[42]], null, [[42]]],
["array[]", 42, [[42]], false, [[42]]],
["array[]", 42, [[42]], "value", [["value"]]],
["array[]", 42, [[42]], 123, [[123]]],
["array[]", "default", [["default"]], null, [["default"]]],
["array[]", "default", [["default"]], false, [["default"]]],
["array[]", "default", [["default"]], "value", [["value"]]],
["array[]", "default", [["default"]], 123, [[123]]],
# les types simples de la forme [type] ainsi que les types non simples sont
# laissés en l'état
[["string"], null, null, null, null],
[["string"], null, null, false, false],
[["string"], null, null, "value", "value"],
[["string"], null, null, 123, 123],
[["string"], false, false, null, null],
[["string"], false, false, false, false],
[["string"], false, false, "value", "value"],
[["string"], false, false, 123, 123],
[["string"], 42, 42, null, null],
[["string"], 42, 42, false, false],
[["string"], 42, 42, "value", "value"],
[["string"], 42, 42, 123, 123],
[["string"], "default", "default", null, null],
[["string"], "default", "default", false, false],
[["string"], "default", "default", "value", "value"],
[["string"], "default", "default", 123, 123],
["unknown_type", null, null, null, null],
["unknown_type", null, null, false, false],
["unknown_type", null, null, "value", "value"],
["unknown_type", null, null, 123, 123],
["unknown_type", false, false, null, null],
["unknown_type", false, false, false, false],
["unknown_type", false, false, "value", "value"],
["unknown_type", false, false, 123, 123],
["unknown_type", 42, 42, null, null],
["unknown_type", 42, 42, false, false],
["unknown_type", 42, 42, "value", "value"],
["unknown_type", 42, 42, 123, 123],
["unknown_type", "default", "default", null, null],
["unknown_type", "default", "default", false, false],
["unknown_type", "default", "default", "value", "value"],
["unknown_type", "default", "default", 123, 123],
];
function testEnsureStructure() {
foreach (self::ENSURE_STRUCTURE_DATA as $value) {
[$type, $default, $absent, $input, $present] = $value;
$schema = [
"present" => [$type, $default],
"absent" => [$type, $default],
];
$md = new Metadata($schema);
$data = [$input];
$md->ensureSchema($data);
self::assertSame(["present" => $present, "absent" => $absent], $data, var_export($value, true));
}
}
const ITEM_SCHEMA = [
"id" => null,
"name" => null,
];
const RECURSIVE_SCHEMA = [
"item" => ["array", "default", "schema" => self::ITEM_SCHEMA],
"items" => ["array[]", "default", "schema" => self::ITEM_SCHEMA],
"nitem" => ["?array", "default", "schema" => self::ITEM_SCHEMA],
"nitems" => ["?array[]", "default", "schema" => self::ITEM_SCHEMA],
];
function testRecursiveSchema() {
$md = new Metadata(self::RECURSIVE_SCHEMA);
$data = null;
$md->ensureSchema($data);
self::assertSame([
"item" => ["id" => "default", "name" => null],
"items" => [["id" => "default", "name" => null]],
"nitem" => ["id" => "default", "name" => null],
"nitems" => [["id" => "default", "name" => null]],
], $data);
$data = [false, false, false, false];
$md->ensureSchema($data);
self::assertSame([
"item" => ["id" => "default", "name" => null],
"items" => [["id" => "default", "name" => null]],
"nitem" => ["id" => "default", "name" => null],
"nitems" => [["id" => "default", "name" => null]],
], $data);
$data = [null, null, null, null];
$md->ensureSchema($data);
self::assertSame([
"item" => ["id" => "default", "name" => null],
"items" => [["id" => "default", "name" => null]],
"nitem" => null,
"nitems" => null,
], $data);
$data = ["id", "id", "id", "id"];
$md->ensureSchema($data);
self::assertSame([
"item" => ["id" => "id", "name" => null],
"items" => [["id" => "id", "name" => null]],
"nitem" => ["id" => "id", "name" => null],
"nitems" => [["id" => "id", "name" => null]],
], $data);
}
function testEnsureSchemaWithKey() {
$schema = [
"name" => null,
"title" => null,
"desc" => null,
];
$md = new Metadata($schema);
$item = null;
$md->ensureSchema($item, "key");
self::assertSame(["name" => "key", "title" => null, "desc" => null], $item);
$item = [];
$md->ensureSchema($item, "key");
self::assertSame(["name" => "key", "title" => null, "desc" => null], $item);
$item = "title";
$md->ensureSchema($item, "key");
self::assertSame(["name" => "key", "title" => "title", "desc" => null], $item);
$item = ["title", "desc"];
$md->ensureSchema($item, "key");
self::assertSame(["name" => "key", "title" => "title", "desc" => "desc"], $item);
$item = ["name" => "name", "title", "desc"];
$md->ensureSchema($item, "key");
self::assertSame(["name" => "name", "title" => "title", "desc" => "desc"], $item);
}
const ENSURE_KEYS_DATA = [
[[1], ["first" => 1]],
[[false, 2], ["first" => null, "second" => 2]],
[[1, false], ["first" => 1, "second" => null]],
[[1, false, 3], ["first" => 1, "second" => null, 3]],
[[1, 2], ["first" => 1, "second" => 2]],
[["first" => 1, 2], ["first" => 1, "second" => 2]],
[["first" => 1, "second" => 2], ["first" => 1, "second" => 2]],
[["second" => 2, 1], ["second" => 2, "first" => 1]],
[["second" => 2, "first" => 1], ["second" => 2, "first" => 1]],
[
[1, 2, 3, "x" => "y"],
["x" => "y", "first" => 1, "second" => 2, 3],
],
];
function testEnsureKeys() {
$schema = [
"first" => null,
"second" => null,
];
$md = new Metadata($schema, ["ensure_keys" => false, "order_keys" => false]);
foreach (self::ENSURE_KEYS_DATA as $value) {
[$input, $result] = $value;
$md->ensureSchema($input);
self::assertSame($result, $input, var_export(["input" => $input, "expected" => $result], true));
}
}
const ORDER_KEYS_DATA = [
[
[1, 2],
["first" => 1, "second" => 2],
["first" => 1, "second" => 2],
],
[
["first" => 1, 2],
["first" => 1, "second" => 2],
["first" => 1, "second" => 2],
],
[
["first" => 1, "second" => 2],
["first" => 1, "second" => 2],
["first" => 1, "second" => 2],
],
[
["second" => 2, 1],
["second" => 2, "first" => 1],
["first" => 1, "second" => 2],
],
[
["second" => 2, "first" => 1],
["second" => 2, "first" => 1],
["first" => 1, "second" => 2],
],
[
[1, 2, 3, "x" => "y"],
["x" => "y", "first" => 1, "second" => 2, 3],
["first" => 1, "second" => 2, "x" => "y", 3],
],
];
function testOrderKeys() {
$schema = [
"first" => null,
"second" => null,
];
$umd = new Metadata($schema, ["order_keys" => false]); # non ordonné
$omd = new Metadata($schema, ["order_keys" => true]); # ordonné
foreach (self::ORDER_KEYS_DATA as $value) {
[$input, $uresult, $oresult] = $value;
$uinput = $input;
$umd->ensureSchema($uinput);
self::assertSame($uresult, $uinput, var_export(["input" => $input, "expected" => $uresult], true));
$oinput = $input;
$omd->ensureSchema($oinput);
self::assertSame($oresult, $oinput, var_export(["input" => $input, "expected" => $oresult], true));
}
}
function testCompositeKeys() {
$schema = [
"a" => ["string"],
"b" => ["int"],
"ca" => ["string", "composite" => true],
"cb" => ["int", "composite" => true],
"" => [
"ctypes" => [
"ab" => CAbType::class,
],
],
];
$md = new Metadata($schema);
self::assertSame(["a", "b"], $md->getSikeys());
self::assertSame(["ca", "cb"], $md->getCokeys());
self::assertSame(["a", "b", "ca", "cb"], $md->getKeys());
self::assertSame(["ab"], $md->getCikeys());
self::assertSame(["a", "b", "ca", "cb", "ab"], $md->getAllKeys());
self::assertSame([
"getA" => "a", "getB" => "b", "getCa" => "ca", "getCb" => "cb",
], Autogen::auto_getters($md));
self::assertSame([
"getAb" => "ab",
], Autogen::auto_ci_getters($md));
self::assertSame([
"setA" => "a", "setB" => "b", "setCa" => "ca", "setCb" => "cb",
], Autogen::auto_setters($md));
self::assertSame([
"delA" => "a", "delB" => "b", "delCa" => "ca", "delCb" => "cb",
], Autogen::auto_deleters($md));
self::assertSame([
"getA(): string",
"getB(): int",
"getCa(): string",
"getCb(): int",
"getAb(): ?array", #XXX à remplacer par : array
], Autogen::auto_getters_methods($md));
$fa = new class($schema) extends FancyArray {
use TGenericArray;
function __construct($schema, $data=null, $key=null) {
if (self::$md === null) self::$md = new Metadata($schema);
parent::__construct($data, $key);
}
/** @var Metadata */
private static $md;
protected static function _AUTO_GETTERS(): ?array {
return Autogen::auto_getters(self::$md);
}
protected static function _AUTO_SETTERS(): ?array {
return Autogen::auto_setters(self::$md);
}
protected static function _AUTO_DELETERS(): ?array {
return Autogen::auto_deleters(self::$md);
}
protected static function _AUTO_CI_GETTERS(): ?array {
return Autogen::auto_ci_getters(self::$md);
}
protected static function _AUTO_CI_SETTERS(): ?array {
return Autogen::auto_ci_setters(self::$md);
}
function md(): Metadata {
return self::$md;
}
function ensureData($data, $key=null): array {
self::$md->ensureSchema($data, $key);
return $data;
}
};
$fa->setA("seta");
$fa->setB(10);
$fa->setCa("setca");
// auto-conversion si on passe par la méthode
$fa->setCb("15");
self::assertSame(15, $fa->getCb());
self::assertSame([
"ca" => "setca", "cb" => 15,
], $fa->getAb());
// pas de conversion si on met à jour directement le tableau
$fa["cb"] = "15";
self::assertSame("15", $fa->getCb());
self::assertSame([
"ca" => "setca", "cb" => "15",
], $fa->getAb());
// set
$fa->setAb(null);
$fa->setCa("setca");
$fa->setCb("15");
$fa->setAb(["ca" => "newca"]);
self::assertSame([
"ca" => "newca", "cb" => 15,
], $fa->getAb());
$fa->setAb(["cb" => 20]);
self::assertSame([
"ca" => "newca", "cb" => 20,
], $fa->getAb());
$fa->setAb(["ca" => "newca2", "cb" => 30]);
self::assertSame([
"ca" => "newca2", "cb" => 30,
], $fa->getAb());
}
function testNestedSchemasRequired() {
$schema = [[
"a" => ["?string", "required" => true],
]];
$md = new Metadata($schema);
$item = [[]];
self::assertException(ValueException::class, function () use ($md, $item) {
$md->ensureSchema($item);
});
$item = ["av"];
$md->ensureSchema($item);
self::assertSame([
["a" => "av"],
], $item);
$schema = [
"as" => ["array[]", "schema" => [
"a" => ["?string", "required" => true],
]],
];
$md = new Metadata($schema);
$item = ["as" => [[]]];
self::assertException(ValueException::class, function () use ($md, $item) {
$md->ensureSchema($item);
});
$item = ["av"];
$md->ensureSchema($item);
self::assertSame([
"as" => [["a" => "av"]],
], $item);
}
function testDataWithKeys() {
$schema = [
"key" => "?key",
"value" => "?int",
];
$md = new Metadata($schema);
$items = []; $md->eachEnsureSchema($items);
self::assertSame([], $items);
$items = ["a", "b"]; $md->eachEnsureSchema($items);
self::assertSame([
["key" => "a", "value" => null],
["key" => "b", "value" => null],
], $items);
$items = [["a"], ["b"]]; $md->eachEnsureSchema($items);
self::assertSame([
["key" => "a", "value" => null],
["key" => "b", "value" => null],
], $items);
$items = [["a", 1], ["b", 2]]; $md->eachEnsureSchema($items);
self::assertSame([
["key" => "a", "value" => 1],
["key" => "b", "value" => 2],
], $items);
$items = ["a" => null, "b" => null]; $md->eachEnsureSchema($items);
self::assertSame([
"a" => ["key" => "a", "value" => null],
"b" => ["key" => "b", "value" => null],
], $items);
$items = ["a" => 1, "b" => 2]; $md->eachEnsureSchema($items);
self::assertSame([
"a" => ["key" => "a", "value" => 1],
"b" => ["key" => "b", "value" => 2],
], $items);
$items = ["a", "b" => 2]; $md->eachEnsureSchema($items);
self::assertSame([
["key" => "a", "value" => null],
"b" => ["key" => "b", "value" => 2],
], $items);
$items = [null, "a", "b" => 2]; $md->eachEnsureSchema($items);
self::assertSame([
["key" => null, "value" => null],
["key" => "a", "value" => null],
"b" => ["key" => "b", "value" => 2],
], $items);
}
}

View File

@ -0,0 +1,364 @@
<?php
namespace nur\data\types;
use nur\b\ValueException;
use nur\data\types\impl\FirstType;
use nur\data\types\impl\Point;
use nur\data\types\impl\SecondType;
use nur\data\types\impl\ThirdType;
use nur\ref\ref_type;
use nur\t\TestCase;
class PhpIncarnationTest extends TestCase {
const DEFAULT_TYPES = [
ref_type::MIXED,
ref_type::BOOL, "bool",
ref_type::INT, "int",
ref_type::FLOAT, "flt", "double", "dbl",
ref_type::RAWSTRING, "rawstr",
ref_type::STRING, "str", "text", "txt",
ref_type::KEY,
ref_type::DATETIME,
ref_type::DATE,
ref_type::TIME,
ref_type::HOUR,
ref_type::NMIXED,
"?". ref_type::BOOL, "?bool",
ref_type::TRIBOOL, "trib",
"?". ref_type::TRIBOOL, "?trib",
"?". ref_type::INT, "?int",
"?". ref_type::FLOAT, "?flt", "?double", "?dbl",
"?". ref_type::RAWSTRING, "?rawstr",
"?". ref_type::STRING, "?str", "?text", "?txt",
"?". ref_type::KEY,
"?". ref_type::DATETIME,
"?". ref_type::DATE,
"?". ref_type::TIME,
"?". ref_type::HOUR,
"?array",
"array",
"?array[]",
"array[]",
"?iterable",
"iterable",
"?resource",
"resource",
];
function testHasType() {
$k = new PhpIncarnation();
foreach (self::DEFAULT_TYPES as $name) {
self::assertTrue($k->hasType($name), $name);
}
# string
$k->addType(FirstType::class);
self::assertTrue($k->hasType(FirstType::class), FirstType::class);
$k->addType(FirstType::class, "premier");
self::assertTrue($k->hasType("premier"), "premier");
# array, anonyme
$k->addType([SecondType::class, "args"]);
self::assertFalse($k->hasType(SecondType::class), SecondType::class);
self::assertTrue($k->hasType([SecondType::class, "args"]), SecondType::class);
$k->addType([SecondType::class, "args"], "deuxieme");
self::assertTrue($k->hasType("deuxieme"), "deuxieme");
# array, nommé
$k->addType(["third" => ThirdType::class, "args"]);
self::assertTrue($k->hasType("third"), "third");
$k->addType(["third" => ThirdType::class, "args"], "troisieme");
self::assertTrue($k->hasType("troisieme"), "troisieme");
}
function testGetType() {
$k = new PhpIncarnation();
$stringType = $k->getType("string");
self::assertInstanceOf(StringType::class, $stringType);
self::assertFalse($stringType->isAllowNull());
$stringType = $k->getType("?string");
self::assertInstanceOf(StringType::class, $stringType);
self::assertTrue($stringType->isAllowNull());
$firstType = $k->getType(FirstType::class);
self::assertInstanceOf(FirstType::class, $firstType);
$point = $firstType->with(1);
self::assertPoint(new Point(1, Point::Y), $point);
$secondType = $k->getType([SecondType::class]);
self::assertInstanceOf(SecondType::class, $secondType);
$point = $secondType->with(2);
self::assertPoint(new Point(2, Point::Y), $point);
$secondType2 = $k->getType([SecondType::class]);
self::assertSame($secondType, $secondType2);
$secondType3 = $k->getType([SecondType::class, ["name" => "whatever"]]);
self::assertInstanceOf(SecondType::class, $secondType3);
$point = $secondType3->with(2);
self::assertPoint(new Point(2, Point::Y), $point);
$secondType4 = $k->getType([SecondType::class, ["name" => "whatever"]]);
self::assertSame($secondType3, $secondType4);
self::assertNotSame($secondType3, $secondType);
$thirdType = $k->getType(["third" => ThirdType::class]);
self::assertInstanceOf(ThirdType::class, $thirdType);
$point = $thirdType->with(3);
self::assertPoint(new Point(3, Point::Y), $point);
$thirdType = $k->getType("third");
self::assertInstanceOf(ThirdType::class, $thirdType);
$point = $thirdType->with(4);
self::assertPoint(new Point(4, Point::Y), $point);
}
function testAddType() {
$k = new PhpIncarnation();
$k->addType(Point::class, "point");
$point = $k->getType("point")->with(1);
self::assertPoint(new Point(1, Point::Y), $point);
$k->addType([FirstType::class]);
$type1 = $k->getType([FirstType::class]);
$k->addType([FirstType::class]);
$type2 = $k->getType([FirstType::class]);
self::assertSame($type2, $type1);
}
const BASIC_SCHEMA = [
"any" => null,
"mixed" => "mixed",
"bool" => "bool",
"nbool" => "?bool",
"int" => "int",
"nint" => "?int",
"float" => "float",
"nfloat" => "?float",
"string" => "string",
"nstring" => "?string",
"array" => "array",
"narray" => "?array",
"arrays" => "array[]",
"narrays" => "?array[]",
"iterable" => "iterable",
"niterable" => "?iterable",
];
function testBasic() {
$md = new Metadata(self::BASIC_SCHEMA);
$data = null;
$md->ensureSchema($data);
self::assertSame([
"any" => null,
"mixed" => null,
"bool" => false, "nbool" => null,
"int" => 0, "nint" => null,
"float" => 0.0, "nfloat" => null,
"string" => "", "nstring" => null,
"array" => [], "narray" => null,
"arrays" => [], "narrays" => null,
"iterable" => [], "niterable" => null,
], $data);
$data = [];
$md->ensureSchema($data);
self::assertSame([
"any" => null,
"mixed" => null,
"bool" => false, "nbool" => null,
"int" => 0, "nint" => null,
"float" => 0.0, "nfloat" => null,
"string" => "", "nstring" => null,
"array" => [], "narray" => null,
"arrays" => [], "narrays" => null,
"iterable" => [], "niterable" => null,
], $data);
$data = [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null];
$md->ensureSchema($data);
self::assertSame([
"any" => null,
"mixed" => null,
"bool" => false, "nbool" => null,
"int" => 0, "nint" => null,
"float" => 0.0, "nfloat" => null,
"string" => "", "nstring" => null,
"array" => [], "narray" => null,
"arrays" => [], "narrays" => null,
"iterable" => [], "niterable" => null,
], $data);
$data = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false];
$md->ensureSchema($data);
self::assertSame([
"any" => null,
"mixed" => false,
"bool" => false, "nbool" => false,
"int" => 0, "nint" => null,
"float" => 0.0, "nfloat" => null,
"string" => "", "nstring" => null,
"array" => [], "narray" => null,
"arrays" => [], "narrays" => null,
"iterable" => [], "niterable" => null,
], $data);
$data = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
$md->ensureSchema($data);
self::assertSame([
"any" => 0,
"mixed" => 0,
"bool" => false, "nbool" => false,
"int" => 0, "nint" => 0,
"float" => 0.0, "nfloat" => 0.0,
"string" => "0", "nstring" => "0",
"array" => [0], "narray" => [0],
"arrays" => [[0]], "narrays" => [[0]],
"iterable" => [0], "niterable" => [0],
], $data);
$data = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
$md->ensureSchema($data);
self::assertSame([
"any" => 1,
"mixed" => 1,
"bool" => true, "nbool" => true,
"int" => 1, "nint" => 1,
"float" => 1.0, "nfloat" => 1.0,
"string" => "1", "nstring" => "1",
"array" => [1], "narray" => [1],
"arrays" => [[1]], "narrays" => [[1]],
"iterable" => [1], "niterable" => [1],
], $data);
$data = ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""];
$md->ensureSchema($data);
self::assertSame([
"any" => "",
"mixed" => "",
"bool" => false, "nbool" => false,
"int" => 0, "nint" => 0,
"float" => 0.0, "nfloat" => 0.0,
"string" => "", "nstring" => "",
"array" => [""], "narray" => [""],
"arrays" => [[""]], "narrays" => [[""]],
"iterable" => [""], "niterable" => [""],
], $data);
$data = ["a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a"];
$md->ensureSchema($data);
self::assertSame([
"any" => "a",
"mixed" => "a",
"bool" => true, "nbool" => true,
"int" => 0, "nint" => 0,
"float" => 0.0, "nfloat" => 0.0,
"string" => "a", "nstring" => "a",
"array" => ["a"], "narray" => ["a"],
"arrays" => [["a"]], "narrays" => [["a"]],
"iterable" => ["a"], "niterable" => ["a"],
], $data);
}
const SPECIAL_SCHEMA = [
"resource" => "?resource",
];
function testSpecial() {
$md = new Metadata(self::SPECIAL_SCHEMA);
$data = null;
$md->ensureSchema($data);
self::assertSame(["resource" => null], $data);
$data = [];
$md->ensureSchema($data);
self::assertSame(["resource" => null], $data);
$data = [null];
$md->ensureSchema($data);
self::assertSame(["resource" => null], $data);
$data = [false];
$md->ensureSchema($data);
self::assertSame(["resource" => null], $data);
$data = ["x"];
self::assertException(ValueException::class, function() use ($md, $data) {
$md->ensureSchema($data);
});
$inf = fopen("php://memory", "rb");
$data = [$inf];
$md->ensureSchema($data);
self::assertSame(["resource" => $inf], $data);
fclose($inf);
}
static function assertPoint(?Point $a, ?Point $b) {
if ($a === null) self::assertNull($b);
if ($b === null) self::assertNull($a);
if ($a !== null && $b !== null) {
self::assertSame($a->x, $b->x);
self::assertSame($a->y, $b->y);
}
}
function testGenericVerifix() {
$k = new PhpIncarnation();
$value = null; $k->getType(Point::class)->verifix($value);
self::assertPoint(null, $value);
$value = 1; $k->getType(Point::class)->verifix($value);
self::assertPoint(new Point(1, Point::Y), $value);
$value = [1]; $k->getType(Point::class)->verifix($value);
self::assertPoint(new Point(1, Point::Y), $value);
$value = [1, 2]; $k->getType(Point::class)->verifix($value);
self::assertPoint(new Point(1, 2), $value);
}
function testDynamic() {
$k = new PhpIncarnation();
$typedef = [RawStringType::class, ["allowed_values" => ["first", "second"]]];
$t1 = $k->getType($typedef);
self::assertException(ValueException::class, [$t1, "with"], " first ");
self::assertException(ValueException::class, [$t1, "with"], " second ");
self::assertException(ValueException::class, [$t1, "with"], " third ");
$typedef = [StringType::class, ["allowed_values" => ["first", "second"]]];
$t1 = $k->getType($typedef);
self::assertSame("first", $t1->with(" first "));
self::assertSame("second", $t1->with(" second "));
self::assertException(ValueException::class, [$t1, "with"], " third ");
$t2 = $k->getType($typedef);
self::assertSame($t2, $t1);
}
function testClassAlias() {
$k = new PhpIncarnation();
$t1 = $k->getType("string"); // static
$t2 = $k->getType(["string"]); // dynamique
$t3 = $k->getType(["string"]); // dynamique
self::assertNotSame($t2, $t1);
self::assertSame($t3, $t2);
$typedef = ["rawstring", ["allowed_values" => ["first", "second"]]];
$t1 = $k->getType($typedef);
self::assertException(ValueException::class, [$t1, "with"], " first ");
self::assertException(ValueException::class, [$t1, "with"], " second ");
self::assertException(ValueException::class, [$t1, "with"], " third ");
$typedef = ["string", ["allowed_values" => ["first", "second"]]];
$t1 = $k->getType($typedef);
self::assertSame("first", $t1->with(" first "));
self::assertSame("second", $t1->with(" second "));
self::assertException(ValueException::class, [$t1, "with"], " third ");
$t2 = $k->getType($typedef);
self::assertSame($t2, $t1);
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace nur\data\types;
use nur\b\ValueException;
use nur\md;
use nur\t\TestCase;
class RawStringTypeTest extends TestCase {
function testAllowedValues() {
$t = new RawStringType(["allowed_values" => ["first", "second"]]);
self::assertTrue($t->isInstance(null));
self::assertFalse($t->isInstance(false));
self::assertFalse($t->isInstance(true));
self::assertTrue($t->isInstance("first"));
self::assertTrue($t->isInstance("second"));
self::assertFalse($t->isInstance("third"));
self::assertNull($t->with(null));
self::assertFalse($t->with(false));
self::assertException(ValueException::class, [$t, "with"], true);
self::assertException(ValueException::class, [$t, "with"], " first ");
self::assertException(ValueException::class, [$t, "with"], " second ");
self::assertException(ValueException::class, [$t, "with"], " third ");
}
function testArrayToString() {
$t = new RawStringType();
self::assertSame("one", $t->with("one"));
self::assertSame("one two", $t->with(["one", "two"]));
self::assertSame("one three", $t->with(["one", "two" => false, "three" => true]));
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace nur\data\types;
use nur\b\ValueException;
use nur\t\TestCase;
class SDateTypeTest extends TestCase {
function test() {
$type = new SDateType();
$year = date("Y");
self::assertSame(null, $type->with(null));
self::assertSame(false, $type->with(false));
self::assertSame(null, $type->with(""));
self::assertException(ValueException::class, [$type, "with"], "15");
self::assertSame("15/01/$year", $type->with("15/1"));
self::assertSame("15/01/1999", $type->with("15/1/99"));
self::assertSame("15/01/2020", $type->with("15/1/20"));
self::assertSame("15/01/2069", $type->with("15/1/69"));
self::assertSame("15/01/1970", $type->with("15/1/70"));
self::assertSame("15/01/1971", $type->with("15/1/71"));
self::assertSame("15/12/2000", $type->with("15/0/2001"));
self::assertSame("15/01/2002", $type->with("15/13/2001"));
self::assertSame("31/05/2001", $type->with("0/6/2001"));
self::assertSame("01/07/2001", $type->with("31/6/2001"));
self::assertSame("12/03/2020", $type->with(" 12/03/2020 "));
self::assertException(ValueException::class, [$type, "with"], " 12/03/2020 0");
self::assertSame("12/03/2020", $type->with(" 12/03/2020 0:0"));
self::assertSame("12/03/2020", $type->with(" 12/03/2020 0:0:0"));
self::assertSame("15/01/$year", $type->with("1501"));
self::assertSame("15/01/1999", $type->with("150199"));
self::assertSame("15/01/1999", $type->with("15011999"));
self::assertSame("15/01/1999", $type->with("1999-01-15"));
self::assertSame("15/01/1999", $type->with("1999-01-15 00:00:00"));
}
}

View File

@ -0,0 +1,52 @@
<?php
namespace nur\data\types;
use nur\b\ValueException;
use nur\t\TestCase;
class SDatetimeTypeTest extends TestCase {
function test() {
$type = new SDatetimeType();
$year = date("Y");
self::assertSame(null, $type->with(null));
self::assertSame(false, $type->with(false));
self::assertSame(null, $type->with(""));
self::assertException(ValueException::class, [$type, "with"], "15 8");
self::assertSame("15/01/$year 08:25:00", $type->with("15/1 8:25"));
self::assertSame("15/01/1999 08:25:32", $type->with("15/1/99 8.25.32"));
self::assertSame("15/01/2020 08:25:32", $type->with("15/1/20 08:25:32"));
self::assertSame("16/01/2069 01:20:00", $type->with("15/1/69 25:20"));
self::assertSame("15/01/1970 03:04:00", $type->with("15/1/70 3:4"));
self::assertSame("15/01/1971 20:16:00", $type->with("15/1/71 20:16"));
self::assertSame("15/12/2000 11:35:00", $type->with("15/0/2001 11:35"));
self::assertSame("15/01/2002 11:35:00", $type->with("15/13/2001 11:35"));
self::assertSame("31/05/2001 11:35:00", $type->with("0/6/2001 11:35"));
self::assertSame("01/07/2001 11:35:00", $type->with("31/6/2001 11:35"));
self::assertSame("12/03/2020 12:42:00", $type->with(" 12/03/2020 12:42 "));
self::assertException(ValueException::class, [$type, "with"], " 12/03/2020 1");
self::assertSame("12/03/2020 01:02:00", $type->with(" 12/03/2020 1:2"));
self::assertSame("12/03/2020 01:02:03", $type->with(" 12/03/2020 1:2:3"));
self::assertException(ValueException::class, [$type, "with"], "15");
self::assertSame("15/01/$year 00:00:00", $type->with("15/1"));
self::assertSame("15/01/1999 00:00:00", $type->with("15/1/99"));
self::assertSame("15/01/2020 00:00:00", $type->with("15/1/20"));
self::assertSame("15/01/2069 00:00:00", $type->with("15/1/69"));
self::assertSame("15/01/1970 00:00:00", $type->with("15/1/70"));
self::assertSame("15/01/1971 00:00:00", $type->with("15/1/71"));
self::assertSame("15/12/2000 00:00:00", $type->with("15/0/2001"));
self::assertSame("15/01/2002 00:00:00", $type->with("15/13/2001"));
self::assertSame("31/05/2001 00:00:00", $type->with("0/6/2001"));
self::assertSame("01/07/2001 00:00:00", $type->with("31/6/2001"));
self::assertSame("12/03/2020 00:00:00", $type->with(" 12/03/2020 "));
self::assertException(ValueException::class, [$type, "with"], " 12/03/2020 0");
self::assertSame("12/03/2020 00:00:00", $type->with(" 12/03/2020 0:0"));
self::assertSame("12/03/2020 00:00:00", $type->with(" 12/03/2020 0:0:0"));
}
}

View File

@ -0,0 +1,121 @@
<?php
namespace nur\data\types;
use nur\b\ValueException;
use nur\t\TestCase;
class STimeTypeTest extends TestCase {
function testParse() {
$t = new STimeType();
$input = "";
self::assertSame(false, $t->parse($input));
self::assertSame("", $input);
$input = " ";
self::assertSame(false, $t->parse($input));
self::assertSame(" ", $input);
$input = "whatever";
self::assertSame(false, $t->parse($input));
self::assertSame("whatever", $input);
$input = "0 ";
self::assertSame([0, 0, 0], $t->parse($input));
self::assertSame("", $input);
$input = "15 ";
self::assertSame([15, 0, 0], $t->parse($input));
self::assertSame("", $input);
$input = "15 : 32 ";
self::assertSame([15, 32, 0], $t->parse($input));
self::assertSame("", $input);
$input = "15 : 32 : 54 ";
self::assertSame([15, 32, 54], $t->parse($input));
self::assertSame("", $input);
$input = "15 : 32 : 54 xxx";
self::assertSame([15, 32, 54], $t->parse($input));
self::assertSame("xxx", $input);
$input = "15 h ";
self::assertSame([15, 0, 0], $t->parse($input));
self::assertSame("", $input);
$input = "15 h 32 ";
self::assertSame([15, 32, 0], $t->parse($input));
self::assertSame("", $input);
$input = "15 h 32 . 54 ";
self::assertSame([15, 32, 54], $t->parse($input));
self::assertSame("", $input);
$input = "15 . 32 : 54 xxx";
self::assertSame([15, 32, 54], $t->parse($input));
self::assertSame("xxx", $input);
}
function testVerifix() {
$t = new STimeType();
$value = null; $t->verifix($value, $result);
self::assertSame(null, $value);
self::assertSame(["valid" => true, "value" => null, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => null, "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = false; $t->verifix($value, $result);
self::assertSame(false, $value);
self::assertSame(["valid" => true, "value" => false, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => false, "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = ""; $t->verifix($value, $result);
self::assertSame(null, $value);
self::assertSame(["valid" => true, "value" => null, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "", "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = "15"; $t->verifix($value, $result);
self::assertSame("15:00:00", $value);
self::assertSame(["valid" => true, "value" => $value, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "15", "orig_desc" => null, "parsed" => "15", "remains" => ""], $result);
$value = "15h"; $t->verifix($value, $result);
self::assertSame("15:00:00", $value);
self::assertSame(["valid" => true, "value" => $value, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "15h", "orig_desc" => null, "parsed" => "15h", "remains" => ""], $result);
$value = "15h32"; $t->verifix($value, $result);
self::assertSame("15:32:00", $value);
self::assertSame(["valid" => true, "value" => $value, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "15h32", "orig_desc" => null, "parsed" => "15h32", "remains" => ""], $result);
$value = "15:32:54"; $t->verifix($value, $result);
self::assertSame("15:32:54", $value);
self::assertSame(["valid" => true, "value" => $value, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "15:32:54", "orig_desc" => null, "parsed" => "15:32:54", "remains" => ""], $result);
}
function testTo_time() {
self::assertSame(false, STimeType::to_time(false));
self::assertSame(null, STimeType::to_time(null));
self::assertSame(null, STimeType::to_time(""));
self::assertSame("15:00:00", STimeType::to_time("15"));
self::assertSame("15:00:00", STimeType::to_time("15h"));
self::assertSame("15:00:00", STimeType::to_time("15:0"));
self::assertSame("15:32:00", STimeType::to_time("15h32"));
self::assertSame("15:32:54", STimeType::to_time("15:32:54"));
self::assertException(ValueException::class, [STimeType::class, "to_time"], true);
self::assertException(ValueException::class, [STimeType::class, "to_time"], "whatever");
self::assertException(ValueException::class, [STimeType::class, "to_time"], 12);
self::assertException(ValueException::class, [STimeType::class, "to_time"], 1.234);
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace nur\data\types;
use nur\t\TestCase;
class STimeslotTypeTest extends TestCase {
function test() {
$type = new STimeslotType();
self::assertSame(null, $type->with(null));
self::assertSame(false, $type->with(false));
self::assertSame(null, $type->with(""));
self::assertSame("15h00-20h00", $type->with("15-20"));
self::assertSame("15h00-", $type->with("15"));
self::assertSame("15h00-", $type->with("15-"));
self::assertSame("-20h00", $type->with("-20"));
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace nur\data\types;
use nur\b\ValueException;
use nur\t\TestCase;
class StringTypeTest extends TestCase {
function testAllowedValues() {
$t = new StringType(["allowed_values" => ["first", "second"]]);
self::assertTrue($t->isInstance(null));
self::assertFalse($t->isInstance(false));
self::assertFalse($t->isInstance(true));
self::assertTrue($t->isInstance("first"));
self::assertTrue($t->isInstance("second"));
self::assertFalse($t->isInstance("third"));
self::assertNull($t->with(null));
self::assertFalse($t->with(false));
self::assertException(ValueException::class, [$t, "with"], true);
self::assertSame("first", $t->with(" first "));
self::assertSame("second", $t->with(" second "));
self::assertException(ValueException::class, [$t, "with"], " third ");
}
function testTrim() {
$t = new StringType();
self::assertSame("hello \n world", $t->with(" hello \r\n world "));
self::assertSame("a\nb\nc", $t->with("\ra\rb\r\nc\r\n"));
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace nur\data\types;
use nur\t\TestCase;
class TelephoneTypeTest extends TestCase {
function testWith(): void {
$type = new TelephoneType();
self::assertSame("0262 30 65 00", $type->with("306500"));
self::assertSame("0262 30 65 00", $type->with("0262306500"));
self::assertSame("0262 30 65 00", $type->with("+262262306500"));
self::assertSame("0692 29 58 24", $type->with("+262692295824"));
self::assertSame("0262 30 65 00", $type->with("+33262306500"));
self::assertSame("0692 29 58 24", $type->with("+33692295824"));
self::assertSame("0156 12 34 56", $type->with("0156123456"));
self::assertSame("0156 12 34 56", $type->with("+33156123456"));
}
function testEnsureInternational(): void {
$type = new TelephoneType();
self::assertSame("+262 262 30 65 00", $type->ensureInternational($type->with("306500")));
self::assertSame("+262 262 30 65 00", $type->ensureInternational($type->with("0262306500")));
self::assertSame("+262 262 30 65 00", $type->ensureInternational($type->with("+262262306500")));
self::assertSame("+262 692 29 58 24", $type->ensureInternational($type->with("+262692295824")));
self::assertSame("+262 262 30 65 00", $type->ensureInternational($type->with("+33262306500")));
self::assertSame("+262 692 29 58 24", $type->ensureInternational($type->with("+33692295824")));
self::assertSame("+33 156 12 34 56", $type->ensureInternational($type->with("0156123456")));
self::assertSame("+33 156 12 34 56", $type->ensureInternational($type->with("+33156123456")));
}
function testEnsureLocal(): void {
$type = new TelephoneType();
self::assertSame("2207", $type->ensureLocal($type->with("2207")));
self::assertSame("0262 30 65 00", $type->ensureLocal($type->with("+262 262 30 65 00")));
self::assertSame("0692 29 58 24", $type->ensureLocal($type->with("+262 692 29 58 24")));
self::assertSame("0156 12 34 56", $type->ensureLocal($type->with("+33 156 12 34 56")));
}
}

View File

@ -0,0 +1,130 @@
<?php
namespace nur\data\types;
use nur\b\date\Time;
use nur\b\ValueException;
use nur\t\TestCase;
class TimeTypeTest extends TestCase {
function testParse() {
$t = new TimeType();
$input = "";
self::assertSame(false, $t->parse($input));
self::assertSame("", $input);
$input = " ";
self::assertSame(false, $t->parse($input));
self::assertSame(" ", $input);
$input = "whatever";
self::assertSame(false, $t->parse($input));
self::assertSame("whatever", $input);
$input = "0 ";
self::assertSame([0, 0, 0], $t->parse($input));
self::assertSame("", $input);
$input = "15 ";
self::assertSame([15, 0, 0], $t->parse($input));
self::assertSame("", $input);
$input = "15 : 32 ";
self::assertSame([15, 32, 0], $t->parse($input));
self::assertSame("", $input);
$input = "15 : 32 : 54 ";
self::assertSame([15, 32, 54], $t->parse($input));
self::assertSame("", $input);
$input = "15 : 32 : 54 xxx";
self::assertSame([15, 32, 54], $t->parse($input));
self::assertSame("xxx", $input);
$input = "15 h ";
self::assertSame([15, 0, 0], $t->parse($input));
self::assertSame("", $input);
$input = "15 h 32 ";
self::assertSame([15, 32, 0], $t->parse($input));
self::assertSame("", $input);
$input = "15 h 32 . 54 ";
self::assertSame([15, 32, 54], $t->parse($input));
self::assertSame("", $input);
$input = "15 . 32 : 54 xxx";
self::assertSame([15, 32, 54], $t->parse($input));
self::assertSame("xxx", $input);
}
function testVerifix() {
$t = new TimeType();
$value = null; $t->verifix($value, $result);
self::assertSame(null, $value);
self::assertSame(["valid" => true, "value" => null, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => null, "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = false; $t->verifix($value, $result);
self::assertSame(Time::undef(), $value);
self::assertSame(["valid" => true, "value" => Time::undef(), "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => false, "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = ""; $t->verifix($value, $result);
self::assertSame(Time::undef(), $value);
self::assertSame(["valid" => true, "value" => Time::undef(), "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "", "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = "15"; $t->verifix($value, $result);
self::assertInstanceOf(Time::class, $value);
self::assertSame("15:00:00", strval($value));
self::assertSame(["valid" => true, "value" => $value, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "15", "orig_desc" => null, "parsed" => "15", "remains" => ""], $result);
$value = "15h"; $t->verifix($value, $result);
self::assertInstanceOf(Time::class, $value);
self::assertSame("15:00:00", strval($value));
self::assertSame(["valid" => true, "value" => $value, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "15h", "orig_desc" => null, "parsed" => "15h", "remains" => ""], $result);
$value = "15h32"; $t->verifix($value, $result);
self::assertInstanceOf(Time::class, $value);
self::assertSame("15:32:00", strval($value));
self::assertSame(["valid" => true, "value" => $value, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "15h32", "orig_desc" => null, "parsed" => "15h32", "remains" => ""], $result);
$value = "15:32:54"; $t->verifix($value, $result);
self::assertInstanceOf(Time::class, $value);
self::assertSame("15:32:54", strval($value));
self::assertSame(["valid" => true, "value" => $value, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "15:32:54", "orig_desc" => null, "parsed" => "15:32:54", "remains" => ""], $result);
}
function testWith() {
$t = new TimeType();
self::assertSame(null, $t->with(null));
self::assertSame(Time::undef(), $t->with(false));
self::assertSame(Time::undef(), $t->with(""));
self::assertSame("", strval($t->with(null)));
self::assertSame("", strval($t->with(false)));
self::assertSame("15:00:00", strval($t->with("15")));
self::assertSame("15:00:00", strval($t->with("15h")));
self::assertSame("15:00:00", strval($t->with("15:0")));
self::assertSame("15:32:00", strval($t->with("15h32")));
self::assertSame("15:32:54", strval($t->with("15:32:54")));
self::assertException(ValueException::class, [$t, "with"], true);
self::assertException(ValueException::class, [$t, "with"], "whatever");
self::assertException(ValueException::class, [$t, "with"], 12);
self::assertException(ValueException::class, [$t, "with"], 1.234);
}
}

View File

@ -0,0 +1,130 @@
<?php
namespace nur\data\types;
use nur\t\TestCase;
class TriboolTypeTest extends TestCase {
function testFormat() {
$t = new TriboolType();
self::assertSame("Oui", $t->format(true));
self::assertSame("Non", $t->format(false));
self::assertSame("", $t->format(null));
$t = new TriboolType(["format" => "on"]);
self::assertSame("O", $t->format(true));
self::assertSame("N", $t->format(false));
self::assertSame("N", $t->format(null));
$t = new TriboolType(["format" => "onn"]);
self::assertSame("O", $t->format(true));
self::assertSame("N", $t->format(false));
self::assertSame("", $t->format(null));
$t = new TriboolType(["format" => "ouinon"]);
self::assertSame("Oui", $t->format(true));
self::assertSame("Non", $t->format(false));
self::assertSame("Non", $t->format(null));
$t = new TriboolType(["format" => "ouinonnull"]);
self::assertSame("Oui", $t->format(true));
self::assertSame("Non", $t->format(false));
self::assertSame("", $t->format(null));
$t = new TriboolType(["format" => "xn"]);
self::assertSame("X", $t->format(true));
self::assertSame("", $t->format(false));
self::assertSame("", $t->format(null));
}
function testParse() {
$t = new TriboolType();
$input = "";
self::assertSame(false, $t->parse($input));
self::assertSame("", $input);
$input = "whatever";
self::assertSame(false, $t->parse($input));
self::assertSame("whatever", $input);
$input = "true";
self::assertSame("true", $t->parse($input));
self::assertSame("", $input);
$input = "false";
self::assertSame("false", $t->parse($input));
self::assertSame("", $input);
$input = "yoyo";
self::assertSame("y", $t->parse($input));
self::assertSame("oyo", $input);
}
function testVerifix() {
$t = new TriboolType();
$value = true; $t->verifix($value, $result);
self::assertSame(true, $value);
self::assertSame(["valid" => true, "value" => true, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => true, "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = false; $t->verifix($value, $result);
self::assertSame(false, $value);
self::assertSame(["valid" => true, "value" => false, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => false, "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = null; $t->verifix($value, $result);
self::assertSame(null, $value);
self::assertSame(["valid" => true, "value" => null, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => null, "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = ""; $t->verifix($value, $result);
self::assertSame(null, $value);
self::assertSame(["valid" => true, "value" => null, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "", "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = "0"; $t->verifix($value, $result);
self::assertSame(false, $value);
self::assertSame(["valid" => true, "value" => false, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "0", "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = "no"; $t->verifix($value, $result);
self::assertSame(false, $value);
self::assertSame(["valid" => true, "value" => false, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "no", "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = "yes"; $t->verifix($value, $result);
self::assertSame(true, $value);
self::assertSame(["valid" => true, "value" => true, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "yes", "orig_desc" => null, "parsed" => false, "remains" => false], $result);
$value = "whatever"; $t->verifix($value, $result);
self::assertSame(true, $value);
self::assertSame(["valid" => true, "value" => true, "value_desc" => null,
"error_code" => null, "error" => false, "exception" => null,
"orig" => "whatever", "orig_desc" => null, "parsed" => false, "remains" => false], $result);
}
function testTo_tribool() {
self::assertTrue(TriboolType::to_tribool(true));
self::assertTrue(TriboolType::to_tribool(1));
self::assertTrue(TriboolType::to_tribool("1"));
self::assertTrue(TriboolType::to_tribool("yes"));
self::assertTrue(TriboolType::to_tribool("whatever"));
self::assertFalse(TriboolType::to_tribool(false));
self::assertFalse(TriboolType::to_tribool(0));
self::assertFalse(TriboolType::to_tribool("0"));
self::assertFalse(TriboolType::to_tribool("no"));
self::assertNull(TriboolType::to_tribool(null));
self::assertFalse(TriboolType::to_tribool(""));
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace nur\data\types\impl;
use nur\data\types\AbstractCompositeType;
class CAbType extends AbstractCompositeType {
const COMPONENTS = [
"ca" => ["string"],
"cb" => ["int"],
];
const SEPARATOR = "-";
const SEPARATOR_PATTERN = '/^\s*-\s*/';
}

View File

@ -0,0 +1,11 @@
<?php
namespace nur\data\types\impl;
use nur\data\types\AbstractCompositeType;
class CBcType extends AbstractCompositeType {
const COMPONENTS = [
"b" => ["int"],
"c" => ["string"],
];
}

View File

@ -0,0 +1,17 @@
<?php
namespace nur\data\types\impl;
use nur\data\types\AbstractCompositeType;
class CSiType extends AbstractCompositeType {
const COMPONENTS = [
"s" => [["string", [
"allowed_values" => ["first", "second"]
]]],
"i" => [["int", [
"allow_parse_empty" => true,
]]],
];
const SEPARATOR = "-";
const SEPARATOR_PATTERN = '/^\s*-\s*/';
}

View File

@ -0,0 +1,16 @@
<?php
namespace nur\data\types\impl;
use nur\data\types\AbstractCompositeType;
use nur\data\types\RegexpType;
class CSsType extends AbstractCompositeType {
const COMPONENTS = [
"s1" => [[RegexpType::class],
"pattern" => '/aa|bx{1,3}/',
],
"s2" => [["string"],
"allow_parse_empty" => true,
],
];
}

View File

@ -0,0 +1,5 @@
<?php
namespace nur\data\types\impl;
class FirstType extends PointType {
}

View File

@ -0,0 +1,13 @@
<?php
namespace nur\data\types\impl;
class Point {
const Y = 100;
public $x, $y;
function __construct(int $x, int $y=self::Y) {
$this->x = $x;
$this->y = $y;
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace nur\data\types\impl;
use nur\A;
use nur\data\types\AbstractSimpleType;
class PointType extends AbstractSimpleType {
function getClass(): string {
return Point::class;
}
protected function _verifix(&$value, array &$result = null): void {
$orig = $value;
if ($this->verifixNoParse($value, $result)) return;
if ($this->verifixCheckClass($value, $orig, $result)) return;
$args = A::with($value);
$value = new Point(...$args);
self::result_valid($result, $value, $orig);
}
}

View File

@ -0,0 +1,5 @@
<?php
namespace nur\data\types\impl;
class SecondType extends PointType {
}

View File

@ -0,0 +1,5 @@
<?php
namespace nur\data\types\impl;
class ThirdType extends PointType {
}

248
nur_tests/funcTest.php Normal file
View File

@ -0,0 +1,248 @@
<?php
namespace nur {
use nur\t\TestCase;
class funcTest extends TestCase {
function testIs_static() {
self::assertFalse(func::is_static(null));
self::assertFalse(func::is_static(""));
self::assertFalse(func::is_static("::"));
self::assertFalse(func::is_static("xxx::"));
self::assertTrue(func::is_static("::xxx"));
self::assertFalse(func::is_static([]));
self::assertFalse(func::is_static([""]));
self::assertTrue(func::is_static(["xxx"]));
self::assertFalse(func::is_static([null, ""]));
self::assertTrue(func::is_static([null, "yyy"]));
self::assertFalse(func::is_static(["xxx", ""]));
self::assertTrue(func::is_static(["xxx", "yyy"]));
self::assertTrue(func::is_static([null, "yyy", "aaa"]));
self::assertTrue(func::is_static(["xxx", "yyy", "aaa"]));
}
function testFix_static() {
$class = "class";
$func = null;
func::fix_static($func, $class);
self::assertSame(null, $func);
$func = "";
func::fix_static($func, $class);
self::assertSame("", $func);
$func = "::";
func::fix_static($func, $class);
self::assertSame("class::", $func);
$func = "xxx::";
func::fix_static($func, $class);
self::assertSame("xxx::", $func);
$func = "::xxx";
func::fix_static($func, $class);
self::assertSame("class::xxx", $func);
$func = [];
func::fix_static($func, $class);
self::assertSame([], $func);
$func = [""];
func::fix_static($func, $class);
self::assertSame(["class", ""], $func);
$func = ["xxx"];
func::fix_static($func, $class);
self::assertSame(["class", "xxx"], $func);
$func = ["xxx", ""];
func::fix_static($func, $class);
self::assertSame(["xxx", ""], $func);
$func = [null, "yyy"];
func::fix_static($func, $class);
self::assertSame(["class", "yyy"], $func);
$func = ["xxx", "yyy"];
func::fix_static($func, $class);
self::assertSame(["xxx", "yyy"], $func);
$func = [null, "yyy", "aaa"];
func::fix_static($func, $class);
self::assertSame(["class", "yyy", "aaa"], $func);
$func = ["xxx", "yyy", "aaa"];
func::fix_static($func, $class);
self::assertSame(["xxx", "yyy", "aaa"], $func);
}
function testIs_method() {
self::assertFalse(func::is_method(null));
self::assertFalse(func::is_method(""));
self::assertFalse(func::is_method("->"));
self::assertTrue(func::is_method("->xxx"));
self::assertFalse(func::is_method([]));
self::assertFalse(func::is_method([""]));
self::assertTrue(func::is_method(["->xxx"]));
self::assertTrue(func::is_method(["->xxx", "aaa"]));
self::assertFalse(func::is_method([null, "->"]));
self::assertTrue(func::is_method([null, "->yyy"]));
self::assertFalse(func::is_method(["xxx", "->"]));
self::assertTrue(func::is_method(["xxx", "->yyy"]));
self::assertTrue(func::is_method([null, "->yyy", "aaa"]));
self::assertTrue(func::is_method(["xxx", "->yyy", "aaa"]));
}
function testFix_method() {
$object = new \stdClass();
$func= null;
func::fix_method($func, $object);
self::assertSame(null, $func);
$func= "";
func::fix_method($func, $object);
self::assertSame("", $func);
$func= "->";
func::fix_method($func, $object);
self::assertSame("->", $func);
$func= "->xxx";
func::fix_method($func, $object);
self::assertSame([$object, "xxx"], $func);
$func= [];
func::fix_method($func, $object);
self::assertSame([], $func);
$func= [""];
func::fix_method($func, $object);
self::assertSame([""], $func);
$func= ["->xxx"];
func::fix_method($func, $object);
self::assertSame([$object, "xxx"], $func);
$func= ["->xxx", "aaa"];
func::fix_method($func, $object);
self::assertSame([$object, "xxx", "aaa"], $func);
$func= [null, "->"];
func::fix_method($func, $object);
self::assertSame([null, "->"], $func);
$func= [null, "->yyy"];
func::fix_method($func, $object);
self::assertSame([$object, "yyy"], $func);
$func= ["xxx", "->"];
func::fix_method($func, $object);
self::assertSame(["xxx", "->"], $func);
$func= ["xxx", "->yyy"];
func::fix_method($func, $object);
self::assertSame([$object, "yyy"], $func);
$func= [null, "->yyy", "aaa"];
func::fix_method($func, $object);
self::assertSame([$object, "yyy", "aaa"], $func);
$func= ["xxx", "->yyy", "aaa"];
func::fix_method($func, $object);
self::assertSame([$object, "yyy", "aaa"], $func);
}
function testCall() {
self::assertSame(36, func::call("func36"));
self::assertSame(12, func::call(TC::class."::method"));
self::assertSame(12, func::call([TC::class, "method"]));
$closure = function() {
return 21;
};
self::assertSame(21, func::call($closure));
}
function testCall_all() {
$c1 = new C1();
$c2 = new C2();
$c3 = new C3();
self::assertSameValues([11, 12], func::call_all(C1::class));
self::assertSameValues([11, 12, 21, 22], func::call_all($c1));
self::assertSameValues([13, 11, 12], func::call_all(C2::class));
self::assertSameValues([13, 23, 11, 12, 21, 22], func::call_all($c2));
self::assertSameValues([111, 13, 12], func::call_all(C3::class));
self::assertSameValues([111, 121, 13, 23, 12, 22], func::call_all($c3));
$options = "conf";
self::assertSameValues([11], func::call_all(C1::class, $options));
self::assertSameValues([11, 21], func::call_all($c1, $options));
self::assertSameValues([11], func::call_all(C2::class, $options));
self::assertSameValues([11, 21], func::call_all($c2, $options));
self::assertSameValues([111], func::call_all(C3::class, $options));
self::assertSameValues([111, 121], func::call_all($c3, $options));
$options = ["prefix" => "conf"];
self::assertSameValues([11], func::call_all(C1::class, $options));
self::assertSameValues([11, 21], func::call_all($c1, $options));
self::assertSameValues([11], func::call_all(C2::class, $options));
self::assertSameValues([11, 21], func::call_all($c2, $options));
self::assertSameValues([111], func::call_all(C3::class, $options));
self::assertSameValues([111, 121], func::call_all($c3, $options));
self::assertSameValues([11, 12], func::call_all($c1, ["include" => "x"]));
self::assertSameValues([11, 21], func::call_all($c1, ["include" => "y"]));
self::assertSameValues([11, 12, 21], func::call_all($c1, ["include" => ["x", "y"]]));
self::assertSameValues([21, 22], func::call_all($c1, ["exclude" => "x"]));
self::assertSameValues([12, 22], func::call_all($c1, ["exclude" => "y"]));
self::assertSameValues([22], func::call_all($c1, ["exclude" => ["x", "y"]]));
self::assertSameValues([12], func::call_all($c1, ["include" => "x", "exclude" => "y"]));
}
function testCons() {
$obj1 = func::cons(Wocons::class, 1, 2, 3);
self::assertInstanceOf(Wocons::class, $obj1);
$obj2 = func::cons(Withemptycons::class, 1, 2, 3);
self::assertInstanceOf(Withemptycons::class, $obj2);
$obj3 = func::cons(Withcons::class, 1, 2, 3);
self::assertInstanceOf(Withcons::class, $obj3);
self::assertSame(1, $obj3->first);
}
}
class Wocons {
}
class Withemptycons {
function __construct() {
}
}
class Withcons {
public $first;
function __construct($first) {
$this->first = $first;
}
}
class TC {
static function method() {
return 12;
}
}
class C1 {
static function confps1_xy() {
return 11;
}
static function ps2_x() {
return 12;
}
function confp1_y() {
return 21;
}
function p2() {
return 22;
}
}
class C2 extends C1 {
static function ps3() {
return 13;
}
function p3() {
return 23;
}
}
class C3 extends C2 {
static function confps1_xy() {
return 111;
}
function confp1_y() {
return 121;
}
}
}
namespace {
function func36() {
return 36;
}
}

View File

@ -0,0 +1,9 @@
<?php
namespace nur\impl;
use nur\b\coll\BaseArray;
use nur\b\coll\TGenericArray;
class GBaseArray extends BaseArray {
use TGenericArray;
}

View File

@ -0,0 +1,9 @@
<?php
namespace nur\impl;
use nur\b\coll\IterableArray;
use nur\b\coll\TGenericArray;
class GIteratableArray extends IterableArray {
use TGenericArray;
}

View File

@ -0,0 +1,33 @@
<?php
namespace nur\io;
use nur\b\io\CacheManager;
use nur\t\TestCase;
class CacheManagerTest extends TestCase {
function test() {
$cm = new CacheManager();
self::assertTrue($cm->shouldCache("a"));
self::assertTrue($cm->shouldCache("a"));
self::assertTrue($cm->shouldCache("a"));
$cm->setNoCache();
self::assertFalse($cm->shouldCache("b"));
self::assertTrue($cm->shouldCache("b"));
self::assertTrue($cm->shouldCache("b"));
$cm->setNoCache();
self::assertFalse($cm->shouldCache("c", false));
self::assertFalse($cm->shouldCache("c", false));
self::assertFalse($cm->shouldCache("c", false));
$cm->setNoCache();
self::assertFalse($cm->shouldCache("d"));
self::assertTrue($cm->shouldCache("d"));
$cm->setNoCache();
self::assertFalse($cm->shouldCache("d"));
self::assertTrue($cm->shouldCache("d"));
}
}

View File

@ -0,0 +1,151 @@
<?php
namespace nur\io\csv;
use nur\t\TestCase;
class CsvReaderTest extends TestCase {
function testReader1() {
$rwf = fopen("php://memory", "w+b");
fwrite($rwf, <<<EOT
nom,prenom,age
clain,jephté,45
enpion,tarte,36
x,y
z
a,b
first,second
EOT
);
rewind($rwf);
$reader = new CsvReader($rwf);
$reader->setMultiSchema(true);
$rows = iterator_to_array($reader);
self::assertSame([
["nom" => "clain", "prenom" => "jephté", "age" => "45"],
["nom" => "enpion", "prenom" => "tarte", "age" => "36"],
["nom" => "x", "prenom" => "y", "age" => false],
["nom" => "z", "prenom" => false, "age" => false],
["a" => "first", "b" => "second"],
], $rows);
}
function testReader2() {
$rwf = fopen("php://memory", "w+b");
fwrite($rwf, <<<EOT
nom,comment,num
clain,"ceci est
un commentaire",45
EOT
);
rewind($rwf);
$reader = new CsvReader($rwf);
$rows = iterator_to_array($reader);
self::assertSame([
["nom" => "clain", "comment" => "ceci est
un commentaire", "num" => "45"],
], $rows);
}
function testReader3() {
$rwf = fopen("php://memory", "w+b");
fwrite($rwf, "");
rewind($rwf);
$reader = new CsvReader($rwf);
$rows = iterator_to_array($reader);
self::assertSame([], $rows);
}
function testReader4() {
$rwf = fopen("php://memory", "w+b");
fwrite($rwf, "nom,prenom,age");
rewind($rwf);
$reader = new CsvReader($rwf);
$rows = iterator_to_array($reader);
self::assertSame([], $rows);
}
function testReader5() {
$reader = new CsvReader(__DIR__ . "/utf8.csv");
$rows = iterator_to_array($reader);
self::assertSame([
["nom" => "clain", "prénom" => "jephté", "age" => "45"],
], $rows);
$reader = new CsvReader(__DIR__ . "/cp1252.csv");
$reader->setEncodingFilter("cp1252");
$rows = iterator_to_array($reader);
self::assertSame([
["nom" => "clain", "prénom" => "jephté", "age" => "45"],
], $rows);
}
function testMappings() {
$rwf = fopen("php://memory", "w+b");
fwrite($rwf, <<<EOT
nom,prenom,age,ville
clain,jephté,45,sainte-clotilde
enpion,tarte,36,missile fixe
EOT
);
###########################################################################
## mappings au format chaine
# tester includes + excludes + remap
# ordre du mapping
rewind($rwf);
$reader = new CsvReader($rwf, [
"header_mappings" => "=prenom,annees=age,nom",
]);
$rows = iterator_to_array($reader);
self::assertSame([
["annees" => "45", "nom" => "clain"],
["annees" => "36", "nom" => "enpion"],
], $rows);
# tester excludes + remap
# ordre de la source
rewind($rwf);
$reader = new CsvReader($rwf, [
"header_mappings" => "=prenom,annees=age",
]);
$rows = iterator_to_array($reader);
self::assertSame([
["nom" => "clain", "annees" => "45", "ville" => "sainte-clotilde"],
["nom" => "enpion", "annees" => "36", "ville" => "missile fixe"],
], $rows);
###########################################################################
## mappings au format tableau
# tester includes + excludes + remap
# ordre du mapping
rewind($rwf);
$reader = new CsvReader($rwf, [
"header_mappings" => ["prenom" => null, "annees" => "age", "nom"],
]);
$rows = iterator_to_array($reader);
self::assertSame([
["annees" => "45", "nom" => "clain"],
["annees" => "36", "nom" => "enpion"],
], $rows);
# tester excludes + remap
# ordre de la source
rewind($rwf);
$reader = new CsvReader($rwf, [
"header_mappings" => ["prenom" => null, "annees" => "age"],
]);
$rows = iterator_to_array($reader);
self::assertSame([
["nom" => "clain", "annees" => "45", "ville" => "sainte-clotilde"],
["nom" => "enpion", "annees" => "36", "ville" => "missile fixe"],
], $rows);
}
}

View File

@ -0,0 +1,116 @@
<?php
namespace nur\io\csv;
use nur\b\io\StringWriter;
use nur\t\TestCase;
class CsvWriterTest extends TestCase {
function testWriter() {
$w = new StringWriter();
$writer = new CsvWriter($w);
$writer->writeAll([
["nom" => "clain", "prenom" => "jephté", "age" => 45],
["nom" => "enpion", "prenom" => "tarte", "age" => 36],
["nom" => "cool", "prenom" => "plusieurs
lignes", "age" => 15],
]);
self::assertSame("nom,prenom,age
clain,jephté,45
enpion,tarte,36
cool,\"plusieurs
lignes\",15
", $w->getString());
}
function testResource() {
$w = fopen("php://memory", "w+b");
$writer = new CsvWriter($w);
$writer->writeAll([
["nom" => "clain", "prenom" => "jephté", "age" => 45],
["nom" => "enpion", "prenom" => "tarte", "age" => 36],
["nom" => "cool", "prenom" => "plusieurs
lignes", "age" => 15],
]);
rewind($w);
self::assertSame("nom,prenom,age
clain,jephté,45
enpion,tarte,36
cool,\"plusieurs
lignes\",15
", stream_get_contents($w));
}
function testEmpty() {
$w = new StringWriter();
$writer = new CsvWriter($w);
$writer->writeAll([]);
self::assertSame("", $w->getString());
$w = new StringWriter();
$writer = new CsvWriter($w);
$writer->setHeaders(["nom", "prenom", "age"]);
$writer->writeAll([]);
self::assertSame("nom,prenom,age\n", $w->getString());
}
function testMappings() {
$rows = [
["nom" => "clain", "prenom" => "jephte", "annees" => "45", "ville" => "sainte-clotilde"],
["nom" => "enpion", "prenom" => "tarte", "annees" => "36", "ville" => "missile fixe"],
];
###########################################################################
## mappings au format chaine
# tester includes + excludes + remap
# ordre du mapping
$w = new StringWriter();
$writer = new CsvWriter($w, [
"header_mappings" => "=prenom,annees=age,nom",
]);
$writer->writeAll($rows);
self::assertSame("prenom,age,nom
,45,clain
,36,enpion
", $w->getString());
# tester excludes + remap
# ordre de la source
$w = new StringWriter();
$writer = new CsvWriter($w, [
"header_mappings" => "=prenom,annees=age",
]);
$writer->writeAll($rows);
self::assertSame("nom,prenom,age,ville
clain,,45,sainte-clotilde
enpion,,36,\"missile fixe\"
", $w->getString());
###########################################################################
## mappings au format tableau
# tester includes + excludes + remap
# ordre du mapping
$w = new StringWriter();
$writer = new CsvWriter($w, [
"header_mappings" => ["prenom" => null, "annees" => "age", "nom"],
]);
$writer->writeAll($rows);
self::assertSame("prenom,age,nom
,45,clain
,36,enpion
", $w->getString());
# tester excludes + remap
# ordre de la source
$w = new StringWriter();
$writer = new CsvWriter($w, [
"header_mappings" => ["prenom" => null, "annees" => "age"],
]);
$writer->writeAll($rows);
self::assertSame("nom,prenom,age,ville
clain,,45,sainte-clotilde
enpion,,36,\"missile fixe\"
", $w->getString());
}
}

View File

@ -0,0 +1,2 @@
nom,prénom,age
clain,jephté,45
1 nom prénom age
2 clain jephté 45

View File

@ -0,0 +1,2 @@
nom,prénom,age
clain,jephté,45
1 nom prénom age
2 clain jephté 45

View File

@ -0,0 +1,84 @@
<?php
namespace nur\io\fsv;
use PHPUnit\Framework\TestCase;
class FsvSchemaTest extends TestCase {
protected function setUp(): void {
$this->schema = new FsvSchema([
"string" => [5],
"number" => [5, "number"],
"date6" => [6, "date"],
"date8" => [8, "date"],
]);
}
/** @var FsvSchema */
private $schema;
function testParse() {
$schema = $this->schema;
$row = $schema->parseRow(" ");
self::assertSame("", $row["string"]);
self::assertFalse($row["number"]);
self::assertFalse($row["date6"]);
self::assertFalse($row["date8"]);
$row = $schema->parseRow("ABC 0001212052312052023");
self::assertSame("ABC", $row["string"]);
self::assertSame(12, $row["number"]);
self::assertSame("12/05/2023", $row["date6"]);
self::assertSame("12/05/2023", $row["date8"]);
# si nombre ou date invalides, ne pas modifier
$row = $schema->parseRow(" ABC00A1212AB2312AB2023");
self::assertSame(" ABC", $row["string"]);
self::assertSame("00A12", $row["number"]);
self::assertSame("12AB23", $row["date6"]);
self::assertSame("12AB2023", $row["date8"]);
}
function testFormat() {
$schema = $this->schema;
self::assertSame(" ", $schema->formatRow([
"string" => null,
"number" => null,
"date6" => null,
"date8" => null,
]));
self::assertSame(" ", $schema->formatRow([
"string" => false,
"number" => false,
"date6" => false,
"date8" => false,
]));
self::assertSame(" ", $schema->formatRow([
"string" => "",
"number" => "",
"date6" => "",
"date8" => "",
]));
self::assertSame("ABC 0001212052312052023", $schema->formatRow([
"string" => "ABC",
"number" => 12,
"date6" => "12/05/2023",
"date8" => "12/05/2023",
]));
self::assertSame(" ABC00A1212AB2312AB2023", $schema->formatRow([
"string" => " ABC",
"number" => "A12",
"date6" => "12AB23",
"date8" => "12AB2023",
]));
self::assertSame("ABCDE1234512AB2312AB2023", $schema->formatRow([
"string" => "ABCDEFGHIJ",
"number" => 123456789,
"date6" => "12AB23XYZT",
"date8" => "12AB2023XYZT",
]));
}
}

364
nur_tests/mdTest.php Normal file
View File

@ -0,0 +1,364 @@
<?php
namespace nur;
use nur\b\ValueException;
use nur\t\TestCase;
class mdTest extends TestCase {
const SCHEMA_DATA1 = [
# sequentiel
[null, ["a" => null, "b" => null]],
[[], ["a" => null, "b" => null]],
[[null], ["a" => null, "b" => null]],
["a", ["a" => "a", "b" => null]],
[["a"], ["a" => "a", "b" => null]],
[["a", "b"], ["a" => "a", "b" => "b"]],
# associatif
[["a" => "a"], ["a" => "a", "b" => null]],
[["b" => "b"], ["b" => "b", "a" => null]],
[["a" => "a", "b" => "b"], ["a" => "a", "b" => "b"]],
[["b" => "b", "a" => "a"], ["b" => "b", "a" => "a"]],
# mix séquentiel / associatif
[["a" => "a", "b"], ["a" => "a", "b" => "b"]],
[["a", "b" => "b"], ["b" => "b", "a" => "a"]],
# avec extra
[["a", "b", "x"], ["a" => "a", "b" => "b", "x"]],
[["a", "b", "y" => "y"], ["y" => "y", "a" => "a", "b" => "b"]],
[["a" => "a", "b" => "b", "x"], ["a" => "a", "b" => "b", "x"]],
[["a" => "a", "b" => "b", "y" => "y"], ["a" => "a", "b" => "b", "y" => "y"]],
[["a" => "a", "b" => "b", "x", "y" => "y"], ["a" => "a", "b" => "b", "y" => "y", "x"]],
];
const SCHEMA_DATA2 = [
# sequentiel
["k", null, ["a" => "k", "b" => null]],
["k", [], ["a" => "k", "b" => null]],
["k", [null], ["a" => "k", "b" => null]],
["k", "a", ["a" => "k", "b" => "a"]],
["k", ["a"], ["a" => "k", "b" => "a"]],
["k", ["a", "b"], ["a" => "k", "b" => "a", "b"]],
# associatif
["k", ["a" => "a"], ["a" => "a", "b" => null]],
["k", ["b" => "b"], ["b" => "b", "a" => "k"]],
["k", ["a" => "a", "b" => "b"], ["a" => "a", "b" => "b"]],
["k", ["b" => "b", "a" => "a"], ["b" => "b", "a" => "a"]],
# mix séquentiel / associatif
["k", ["a" => "a", "b"], ["a" => "a", "b" => "b"]],
["k", ["a", "b" => "b"], ["b" => "b", "a" => "k", "a"]],
# avec extra
["k", ["a", "b", "x"], ["a" => "k", "b" => "a", "b", "x"]],
["k", ["a", "b", "y" => "y"], ["y" => "y", "a" => "k", "b" => "a", "b"]],
["k", ["a" => "a", "b" => "b", "x"], ["a" => "a", "b" => "b", "x"]],
["k", ["a" => "a", "b" => "b", "y" => "y"], ["a" => "a", "b" => "b", "y" => "y"]],
["k", ["a" => "a", "b" => "b", "x", "y" => "y"], ["a" => "a", "b" => "b", "y" => "y", "x"]],
];
function testSchema() {
$schema = [
"a" => null,
"b" => null,
];
$index = 0;
foreach (self::SCHEMA_DATA1 as [$data, $result]) {
md::ensure_schema($data, $schema);
self::assertSame($result, $data, "at SCHEMA_DATA1[$index]");
$index++;
}
$index = 0;
foreach (self::SCHEMA_DATA2 as [$key, $data, $result]) {
md::ensure_schema($data, $schema, $key);
self::assertSame($result, $data, "at SCHEMA_DATA2[$index]");
$index++;
}
}
#############################################################################
function testRecursiveSchema() {
$item_schema = [
"id" => null,
"name" => null,
];
$schema = [
"item" => ["array", "default", "schema" => $item_schema],
"items" => ["array[]", "default", "schema" => $item_schema],
"nitem" => ["?array", "default", "schema" => $item_schema],
"nitems" => ["?array[]", "default", "schema" => $item_schema],
];
$data = null;
md::ensure_schema($data, $schema);
self::assertSame([
"item" => ["id" => "default", "name" => null],
"items" => [["id" => "default", "name" => null]],
"nitem" => ["id" => "default", "name" => null],
"nitems" => [["id" => "default", "name" => null]],
], $data);
$data = [false, false, false, false];
md::ensure_schema($data, $schema);
self::assertSame([
"item" => ["id" => "default", "name" => null],
"items" => [["id" => "default", "name" => null]],
"nitem" => ["id" => "default", "name" => null],
"nitems" => [["id" => "default", "name" => null]],
], $data);
$data = [null, null, null, null];
md::ensure_schema($data, $schema);
self::assertSame([
"item" => ["id" => "default", "name" => null],
"items" => [["id" => "default", "name" => null]],
"nitem" => null,
"nitems" => null,
], $data);
$data = ["id", "id", "id", "id"];
md::ensure_schema($data, $schema);
self::assertSame([
"item" => ["id" => "id", "name" => null],
"items" => [["id" => "id", "name" => null]],
"nitem" => ["id" => "id", "name" => null],
"nitems" => [["id" => "id", "name" => null]],
], $data);
}
#############################################################################
function testTypes() {
$schema = [
"regular" => [null, "default"],
"boolt" => ["bool", true],
"boolf" => ["bool", false],
"booln" => ["bool", null],
"nboolt" => ["?bool", true],
"nboolf" => ["?bool", false],
"nbooln" => ["?bool", null],
"mixed" => ["mixed", "default"],
];
$data = [];
md::ensure_schema($data, $schema);
self::assertSame([
"regular" => "default",
"boolt" => true, "boolf" => false, "booln" => false,
"nboolt" => true, "nboolf" => false, "nbooln" => null,
"mixed" => "default",
], $data);
$data = [
false,
false, false, false,
false, false, false,
false,
];
md::ensure_schema($data, $schema);
self::assertSame([
"regular" => "default",
"boolt" => false, "boolf" => false, "booln" => false,
"nboolt" => false, "nboolf" => false, "nbooln" => false,
"mixed" => false,
], $data);
$data = [
null,
null, null, null,
null, null, null,
null,
];
md::ensure_schema($data, $schema);
self::assertSame([
"regular" => null,
"boolt" => true, "boolf" => false, "booln" => false,
"nboolt" => null, "nboolf" => null, "nbooln" => null,
"mixed" => null,
], $data);
$data = [
"x",
"x", "x", "x",
"x", "x", "x",
"x",
];
md::ensure_schema($data, $schema);
self::assertSame([
"regular" => "x",
"boolt" => true, "boolf" => true, "booln" => true,
"nboolt" => true, "nboolf" => true, "nbooln" => true,
"mixed" => "x",
], $data);
}
function testArrayToString() {
$schema = ["value" => "string"];
$data = ["value" => "one"];
md::ensure_schema($data, $schema);
self::assertSame(["value" => "one"], $data);
$data = ["value" => ["one", "two"]];
md::ensure_schema($data, $schema);
self::assertSame(["value" => "one two"], $data);
$data = ["value" => ["one", "two" => false, "three" => true]];
md::ensure_schema($data, $schema);
self::assertSame(["value" => "one three"], $data);
}
#############################################################################
function testRequired() {
$schema = [
"required" => [null, "required" => true],
"nullable" => [null, 1],
];
$data = null;
self::assertException(ValueException::class, function() use (&$data, $schema) {
md::ensure_schema($data, $schema, null, true);
});
self::assertSame(["required" => null, "nullable" => 1], $data);
$data = [];
self::assertException(ValueException::class, function() use (&$data, $schema) {
md::ensure_schema($data, $schema, null, true);
});
self::assertSame(["required" => null, "nullable" => 1], $data);
$data = [null];
self::assertException(ValueException::class, function() use (&$data, $schema) {
md::ensure_schema($data, $schema, null, true);
});
self::assertSame(["required" => null, "nullable" => 1], $data);
$data = [false];
self::assertException(ValueException::class, function() use (&$data, $schema) {
md::ensure_schema($data, $schema, null, true);
});
self::assertSame(["required" => null, "nullable" => 1], $data);
$data = ["x"];
self::assertNotException(function() use (&$data, $schema) {
md::ensure_schema($data, $schema, null, true);
});
self::assertSame(["required" => "x", "nullable" => 1], $data);
$data = ["x", "y", "z"];
self::assertNotException(function() use (&$data, $schema) {
md::ensure_schema($data, $schema, null, true);
});
self::assertSame(["required" => "x", "nullable" => "y", "z"], $data);
$data = ["x", false, "z"];
self::assertNotException(function() use (&$data, $schema) {
md::ensure_schema($data, $schema, null, true);
});
self::assertSame(["required" => "x", "nullable" => 1, "z"], $data);
}
#############################################################################
const GET_DATA = [
[null, [
"a" => null,
"b" => "b",
]],
[false, [
"a" => "a",
"b" => "b",
]],
[1, [
"a" => 1,
"b" => "b",
]],
[[], [
"a" => "a",
"b" => "b",
]],
[[null], [
"a" => null,
"b" => "b",
]],
[[false], [
"a" => "a",
"b" => "b",
]],
[[1], [
"a" => 1,
"b" => "b",
]],
[[1, 2], [
"a" => 1,
"b" => 2,
]],
[["a" => 1], [
"a" => 1,
"b" => "b",
]],
[["b" => 2], [
"a" => "a",
"b" => 2,
]],
[["a" => 1, "b" => 2], [
"a" => 1,
"b" => 2,
]],
[["b" => 2, "a" => 1], [
"a" => 1,
"b" => 2,
]],
];
function testGet() {
$schema = [
"a" => [null, "a"],
"b" => [null, "b"],
];
$i = 0;
foreach (self::GET_DATA as [$data, $results]) {
$j = 0;
foreach ($results as $key => $expected) {
$actual = md::get($data, $key, $schema);
self::assertSame($expected, $actual, "at GET_DATA[$i][$j]");
$j++;
}
$i++;
}
# test default
$data = [];
self::assertSame("a", md::get($data, "a", $schema));
self::assertSame("A", md::get($data, "a", $schema, "A"));
$data = ["a" => "x"];
self::assertSame("x", md::get($data, "a", $schema));
self::assertSame("x", md::get($data, "a", $schema, "A"));
}
#############################################################################
const GET_VALUES_DATA = [
[null, [], []],
[["a", "b"], [], ["a", "b"]],
[["a" => "a"], ["a" => "a"], []],
[["b" => "b"], ["b" => "b"], []],
[["a" => "a", "b" => "b"], ["a" => "a", "b" => "b"], []],
[["b" => "b", "a" => "a"], ["a" => "a", "b" => "b"], []],
];
function testGetValues() {
$schema = [
"a" => null,
"b" => null,
];
$i = 0;
foreach (self::GET_VALUES_DATA as [$data, $xvalues, $xothers]) {
$values = md::get_values($data, $schema);
self::assertSame($xvalues, $values, "values at GET_VALUES_DATA[$i]");
$others = md::get_others($data, $schema);
self::assertSame($xothers, $others, "others at GET_VALUES_DATA[$i]");
$i++;
}
}
}

50
nur_tests/pathTest.php Normal file
View File

@ -0,0 +1,50 @@
<?php
namespace nur;
use nur\t\TestCase;
class pathTest extends TestCase {
function testSplit() {
self::assertSame([false, false], path::split(false));
self::assertSame([null, null], path::split(null));
self::assertSame(["", ""], path::split(""));
self::assertSame(["/", "a"], path::split("/a"));
self::assertSame(["/a", ""], path::split("/a/"));
self::assertSame(["/a", "b"], path::split("/a/b"));
self::assertSame(["/a/b", "c"], path::split("/a/b/c"));
self::assertSame(["", "a"], path::split("a"));
self::assertSame(["a", ""], path::split("a/"));
self::assertSame(["a", "b"], path::split("a/b"));
self::assertSame(["a/b", "c"], path::split("a/b/c"));
}
function testJoin() {
self::assertNull(path::join());
self::assertNull(path::join(null));
self::assertNull(path::join(false));
self::assertNull(path::join(null, null));
self::assertNull(path::join(null, false));
self::assertSame(".", path::join(""));
self::assertSame("a", path::join("a", ""));
self::assertSame("a", path::join("", "a"));
self::assertSame("a/b", path::join("a", "b"));
self::assertSame("a/b", path::join("a/", "b"));
self::assertSame("a/b", path::join("a", "/b"));
self::assertSame("a/b", path::join("a/", "/b"));
}
function testReljoin() {
self::assertNull(path::reljoin());
self::assertNull(path::reljoin(null));
self::assertNull(path::reljoin(false));
self::assertNull(path::reljoin(null, null));
self::assertNull(path::reljoin(null, false));
self::assertSame(".", path::reljoin(""));
self::assertSame("a", path::reljoin("a", ""));
self::assertSame("a", path::reljoin("", "a"));
self::assertSame("a/b", path::reljoin("a", "b"));
self::assertSame("a/b", path::reljoin("a/", "b"));
self::assertSame("/b", path::reljoin("a", "/b"));
self::assertSame("/b", path::reljoin("a/", "/b"));
}
}

View File

@ -0,0 +1,74 @@
<?php
namespace nur\php;
use nur\data\types\Metadata;
use nur\t\TestCase;
use stdClass;
class AutogenTest extends TestCase {
const SCHEMA1 = [
"a" => ["?string"],
"b" => ["string"],
"c" => [["string"], "allow_null" => true],
"d" => [["string"], "allow_null" => false],
];
function test1() {
$schema = self::SCHEMA1;
$md = new Metadata($schema);
self::assertSame("?string", $md->getType("a")->getPhpType());
self::assertSame("string", $md->getType("b")->getPhpType());
self::assertSame("?string", $md->getType("c")->getPhpType());
self::assertSame("string", $md->getType("d")->getPhpType());
self::assertSame([
"getA(): ?string",
"getB(): string",
"getC(): ?string",
"getD(): string",
], Autogen::auto_getters_methods($schema, null, stdClass::class));
}
const SCHEMA2 = [
"a" => ["?array"],
"b" => ["array"],
"c" => [["array"], "allow_null" => true],
"d" => [["array"], "allow_null" => false],
];
function test2() {
$schema = self::SCHEMA2;
$md = new Metadata($schema);
self::assertSame("?array", $md->getType("a")->getPhpType());
self::assertSame("array", $md->getType("b")->getPhpType());
self::assertSame("?array", $md->getType("c")->getPhpType());
self::assertSame("array", $md->getType("d")->getPhpType());
self::assertSame([
"getA(): ?array",
"getB(): array",
"getC(): ?array",
"getD(): array",
], Autogen::auto_getters_methods($schema, null, stdClass::class));
}
const SCHEMA3 = [
"a" => ["?array[]"],
"b" => ["array[]"],
"c" => [["array[]"], "allow_null" => true],
"d" => [["array[]"], "allow_null" => false],
];
function test3() {
$schema = self::SCHEMA3;
$md = new Metadata($schema);
self::assertSame("?array", $md->getType("a")->getPhpType());
self::assertSame("array", $md->getType("b")->getPhpType());
self::assertSame("?array", $md->getType("c")->getPhpType());
self::assertSame("array", $md->getType("d")->getPhpType());
self::assertSame([
"getA(): ?array",
"getB(): array",
"getC(): ?array",
"getD(): array",
], Autogen::auto_getters_methods($schema, null, stdClass::class));
}
}

47
nur_tests/strTest.php Normal file
View File

@ -0,0 +1,47 @@
<?php
namespace nur;
use nur\t\TestCase;
class strTest extends TestCase {
function testSplit_pair() {
self::assertSame([null, null], str::split_pair(null));
self::assertSame([null, null], str::split_pair(false));
self::assertSame(["", null], str::split_pair(""));
self::assertSame(["first", null], str::split_pair("first"));
self::assertSame(["first", ""], str::split_pair("first:"));
self::assertSame(["first", "second"], str::split_pair("first:second"));
self::assertSame(["first", "second:third"], str::split_pair("first:second:third"));
}
function testSplit_nl() {
self::assertSame([], str::split_nl(null));
self::assertSame([], str::split_nl(false));
self::assertSame([], str::split_nl(""));
self::assertSame(["first"], str::split_nl("first"));
self::assertSame(["first"], str::split_nl("first\n"));
self::assertSame(["first", "second"], str::split_nl("first\nsecond"));
}
function testXxxSuffix() {
$s = "blahSUF";
self::assertTrue(str::del_suffix($s, "SUF"));
self::assertSame("blah", $s);
self::assertFalse(str::del_suffix($s, "SUF"));
self::assertTrue(str::add_suffix($s, "SUF"));
self::assertSame("blahSUF", $s);
self::assertFalse(str::add_suffix($s, "SUF"));
}
function testXxxPrefix() {
$s = "PREblah";
self::assertTrue(str::del_prefix($s, "PRE"));
self::assertSame("blah", $s);
self::assertFalse(str::del_prefix($s, "PRE"));
self::assertTrue(str::add_prefix($s, "PRE"));
self::assertSame("PREblah", $s);
self::assertFalse(str::add_prefix($s, "PRE"));
}
}

View File

@ -0,0 +1,56 @@
<?php
namespace nur\v\base;
use nur\b\ui\IContent;
use nur\b\ui\IPrintable;
use nur\c;
use nur\t\TestCase;
use nur\v\vo;
class ComponentTest extends TestCase {
function testGetContent() {
$component = new MyComponent();
$content = c::string($component->getContent());
self::assertSame("<h1>le titre</h1>\n&lt;helloworld&gt;<bonjourmonde>print!static-content!generated-content!", $content);
}
function testPrint() {
$component = new MyComponent();
ob_start(null, 0, PHP_OUTPUT_HANDLER_STDFLAGS ^ PHP_OUTPUT_HANDLER_FLUSHABLE);
$component->print();
$content = ob_get_clean();
self::assertSame("<h1>le titre</h1>\n&lt;helloworld&gt;<bonjourmonde>print!static-content!generated-content!", $content);
}
}
class ZePrint implements IPrintable {
function print(): void {
echo "print!";
}
}
class ZeStaticContent implements IContent {
function getContent(): array {
return ["static-content!"];
}
}
class ZeGeneratedContent implements IContent {
function getContent(): iterable {
yield "generated-content!";
}
}
class MyComponent extends SimplePrintable {
function print(): void {
vo::h1(null);
vo::h1("le titre");
vo::write("<hello");
vo::write("world>");
vo::write(["<bonjour"]);
vo::write(["monde>"]);
vo::write(new ZePrint());
vo::write(new ZeStaticContent());
vo::write(new ZeGeneratedContent());
}
}

View File