diff --git a/nur_tests/ATest.php b/nur_tests/ATest.php new file mode 100644 index 0000000..54056bd --- /dev/null +++ b/nur_tests/ATest.php @@ -0,0 +1,337 @@ +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"])); + } +} diff --git a/nur_tests/b/coll/ArrayViewStackTest.php b/nur_tests/b/coll/ArrayViewStackTest.php new file mode 100644 index 0000000..895b01d --- /dev/null +++ b/nur_tests/b/coll/ArrayViewStackTest.php @@ -0,0 +1,39 @@ + 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")); + } +} diff --git a/nur_tests/b/coll/ArrayViewTest.php b/nur_tests/b/coll/ArrayViewTest.php new file mode 100644 index 0000000..7014e20 --- /dev/null +++ b/nur_tests/b/coll/ArrayViewTest.php @@ -0,0 +1,61 @@ +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 +} diff --git a/nur_tests/b/coll/impl/ItemView.php b/nur_tests/b/coll/impl/ItemView.php new file mode 100644 index 0000000..dd7f097 --- /dev/null +++ b/nur_tests/b/coll/impl/ItemView.php @@ -0,0 +1,61 @@ + "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-- +} diff --git a/nur_tests/b/date/DateTest.php b/nur_tests/b/date/DateTest.php new file mode 100644 index 0000000..3c3d6f8 --- /dev/null +++ b/nur_tests/b/date/DateTest.php @@ -0,0 +1,21 @@ +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())); + } +} diff --git a/nur_tests/b/date/DatetimeTest.php b/nur_tests/b/date/DatetimeTest.php new file mode 100644 index 0000000..3b38bbc --- /dev/null +++ b/nur_tests/b/date/DatetimeTest.php @@ -0,0 +1,21 @@ +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)); + } +} diff --git a/nur_tests/b/date/DelayTest.php b/nur_tests/b/date/DelayTest.php new file mode 100644 index 0000000..37d73e1 --- /dev/null +++ b/nur_tests/b/date/DelayTest.php @@ -0,0 +1,49 @@ +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))); + } +} diff --git a/nur_tests/b/date/ElapsedTest.php b/nur_tests/b/date/ElapsedTest.php new file mode 100644 index 0000000..6a6b858 --- /dev/null +++ b/nur_tests/b/date/ElapsedTest.php @@ -0,0 +1,32 @@ +formatSince(), sprintf("for seconds=%d", $seconds)); + } + } +} diff --git a/nur_tests/b/date/HourTest.php b/nur_tests/b/date/HourTest.php new file mode 100644 index 0000000..74314bc --- /dev/null +++ b/nur_tests/b/date/HourTest.php @@ -0,0 +1,34 @@ +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)); + } +} diff --git a/nur_tests/b/date/TimeTest.php b/nur_tests/b/date/TimeTest.php new file mode 100644 index 0000000..c399ae3 --- /dev/null +++ b/nur_tests/b/date/TimeTest.php @@ -0,0 +1,55 @@ +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()); + } +} diff --git a/nur_tests/b/date/TrangeTest.php b/nur_tests/b/date/TrangeTest.php new file mode 100644 index 0000000..1d46366 --- /dev/null +++ b/nur_tests/b/date/TrangeTest.php @@ -0,0 +1,99 @@ +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)); + } +} diff --git a/nur_tests/b/exceptionTest.php b/nur_tests/b/exceptionTest.php new file mode 100644 index 0000000..3bf3e00 --- /dev/null +++ b/nur_tests/b/exceptionTest.php @@ -0,0 +1,24 @@ +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"; + } + } +} diff --git a/nur_tests/b/impl/One.php b/nur_tests/b/impl/One.php new file mode 100644 index 0000000..e3eff4b --- /dev/null +++ b/nur_tests/b/impl/One.php @@ -0,0 +1,16 @@ +two = new Two(); + } + + private $two; + + function f() { + $this->two->f(); + } +} diff --git a/nur_tests/b/impl/Two.php b/nur_tests/b/impl/Two.php new file mode 100644 index 0000000..aa12af3 --- /dev/null +++ b/nur_tests/b/impl/Two.php @@ -0,0 +1,10 @@ +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()); + } +} diff --git a/nur_tests/b/io/StringWriterTest.php b/nur_tests/b/io/StringWriterTest.php new file mode 100644 index 0000000..8a22225 --- /dev/null +++ b/nur_tests/b/io/StringWriterTest.php @@ -0,0 +1,158 @@ +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()); + } +} diff --git a/nur_tests/b/params/ParametrableTest.php b/nur_tests/b/params/ParametrableTest.php new file mode 100644 index 0000000..417d08b --- /dev/null +++ b/nur_tests/b/params/ParametrableTest.php @@ -0,0 +1,83 @@ +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()); + } +} diff --git a/nur_tests/b/params/impl/BPB1.php b/nur_tests/b/params/impl/BPB1.php new file mode 100644 index 0000000..b12e972 --- /dev/null +++ b/nur_tests/b/params/impl/BPB1.php @@ -0,0 +1,14 @@ +ppFirst; + } + + function pp_setFirst(string $value): void { + $this->ppFirst = $value; + } +} diff --git a/nur_tests/b/params/impl/BPB2.php b/nur_tests/b/params/impl/BPB2.php new file mode 100644 index 0000000..d012d71 --- /dev/null +++ b/nur_tests/b/params/impl/BPB2.php @@ -0,0 +1,20 @@ + "?string", + "second" => "?int", + ]; + + private $ppSecond; + + function getData() { + return [$this->getFirst(), $this->ppSecond]; + } +} diff --git a/nur_tests/b/params/impl/BPC1.php b/nur_tests/b/params/impl/BPC1.php new file mode 100644 index 0000000..c2d69f6 --- /dev/null +++ b/nur_tests/b/params/impl/BPC1.php @@ -0,0 +1,25 @@ + "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]; + } +} diff --git a/nur_tests/b/params/impl/BPC2.php b/nur_tests/b/params/impl/BPC2.php new file mode 100644 index 0000000..9b8d253 --- /dev/null +++ b/nur_tests/b/params/impl/BPC2.php @@ -0,0 +1,26 @@ + "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); + } +} diff --git a/nur_tests/b/params/impl/BPD1.php b/nur_tests/b/params/impl/BPD1.php new file mode 100644 index 0000000..cf87d59 --- /dev/null +++ b/nur_tests/b/params/impl/BPD1.php @@ -0,0 +1,26 @@ + ["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]; + } +} diff --git a/nur_tests/b/params/impl/BPD2.php b/nur_tests/b/params/impl/BPD2.php new file mode 100644 index 0000000..0909cf8 --- /dev/null +++ b/nur_tests/b/params/impl/BPD2.php @@ -0,0 +1,21 @@ + ["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]; + } +} diff --git a/nur_tests/b/params/impl/MyParametrable.php b/nur_tests/b/params/impl/MyParametrable.php new file mode 100644 index 0000000..b4b1fb1 --- /dev/null +++ b/nur_tests/b/params/impl/MyParametrable.php @@ -0,0 +1,91 @@ + "?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-- +} diff --git a/nur_tests/b/text/WordTest.php b/nur_tests/b/text/WordTest.php new file mode 100644 index 0000000..51b8e35 --- /dev/null +++ b/nur_tests/b/text/WordTest.php @@ -0,0 +1,140 @@ +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("né", $adj->a()); + self::assertSame("né", $adj->a(false)); + self::assertSame("né", $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)); + } +} diff --git a/nur_tests/baseTest.php b/nur_tests/baseTest.php new file mode 100644 index 0000000..21a85cd --- /dev/null +++ b/nur_tests/baseTest.php @@ -0,0 +1,121 @@ + ["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( + [" "x>", " ["y>"], + " "z>", " ["t>"], + " ["w1>" ,"w2>"], + ], c::flatten([ + " "x>", " ["y>"], + [" "z>", " ["t>"]], + " "w1>", [" "w2>"], + ])); + } +} diff --git a/nur_tests/cli/ArgsParserTest.php b/nur_tests/cli/ArgsParserTest.php new file mode 100644 index 0000000..ce12973 --- /dev/null +++ b/nur_tests/cli/ArgsParserTest.php @@ -0,0 +1,582 @@ + 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); + } +} diff --git a/nur_tests/cli/impl/AbcdDest.php b/nur_tests/cli/impl/AbcdDest.php new file mode 100644 index 0000000..1c8bf4a --- /dev/null +++ b/nur_tests/cli/impl/AbcdDest.php @@ -0,0 +1,24 @@ +a; + } + + function getB() { + return $this->b; + } + + function getC() { + return $this->c; + } + + function getD() { + return $this->d; + } +} \ No newline at end of file diff --git a/nur_tests/cli/impl/SimpleDest.php b/nur_tests/cli/impl/SimpleDest.php new file mode 100644 index 0000000..8f606d7 --- /dev/null +++ b/nur_tests/cli/impl/SimpleDest.php @@ -0,0 +1,10 @@ +args = null; + } +} diff --git a/nur_tests/config/ConfigManagerTest.php b/nur_tests/config/ConfigManagerTest.php new file mode 100644 index 0000000..d1b5ede --- /dev/null +++ b/nur_tests/config/ConfigManagerTest.php @@ -0,0 +1,159 @@ +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"], + ]; +} diff --git a/nur_tests/data/template/StreamTemplateTest.php b/nur_tests/data/template/StreamTemplateTest.php new file mode 100644 index 0000000..e8148ef --- /dev/null +++ b/nur_tests/data/template/StreamTemplateTest.php @@ -0,0 +1,39 @@ +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"; + } +} diff --git a/nur_tests/data/template/StreamTemplateTest.txt b/nur_tests/data/template/StreamTemplateTest.txt new file mode 100644 index 0000000..3846d45 --- /dev/null +++ b/nur_tests/data/template/StreamTemplateTest.txt @@ -0,0 +1,2 @@ +Ceci est un TEST de TEXTE +statique _et_ dynamique diff --git a/nur_tests/data/template/StringTemplateTest.php b/nur_tests/data/template/StringTemplateTest.php new file mode 100644 index 0000000..220dc04 --- /dev/null +++ b/nur_tests/data/template/StringTemplateTest.php @@ -0,0 +1,30 @@ +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"; + } +} diff --git a/nur_tests/data/types/BoolTypeTest.php b/nur_tests/data/types/BoolTypeTest.php new file mode 100644 index 0000000..dbff273 --- /dev/null +++ b/nur_tests/data/types/BoolTypeTest.php @@ -0,0 +1,168 @@ +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("")); + } +} diff --git a/nur_tests/data/types/CTimeslotTypeTest.php b/nur_tests/data/types/CTimeslotTypeTest.php new file mode 100644 index 0000000..ac6a272 --- /dev/null +++ b/nur_tests/data/types/CTimeslotTypeTest.php @@ -0,0 +1,30 @@ +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")); + } +} diff --git a/nur_tests/data/types/CompositeTypeTest.php b/nur_tests/data/types/CompositeTypeTest.php new file mode 100644 index 0000000..f31e06c --- /dev/null +++ b/nur_tests/data/types/CompositeTypeTest.php @@ -0,0 +1,96 @@ + ["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")); + } +} diff --git a/nur_tests/data/types/ContentTypeTest.php b/nur_tests/data/types/ContentTypeTest.php new file mode 100644 index 0000000..791d11a --- /dev/null +++ b/nur_tests/data/types/ContentTypeTest.php @@ -0,0 +1,29 @@ +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()]); + } +} diff --git a/nur_tests/data/types/FileTypeTest.php b/nur_tests/data/types/FileTypeTest.php new file mode 100644 index 0000000..4d3eb9a --- /dev/null +++ b/nur_tests/data/types/FileTypeTest.php @@ -0,0 +1,30 @@ +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()); + } +} diff --git a/nur_tests/data/types/GenericTypeTest.php b/nur_tests/data/types/GenericTypeTest.php new file mode 100644 index 0000000..7ed4ed4 --- /dev/null +++ b/nur_tests/data/types/GenericTypeTest.php @@ -0,0 +1,29 @@ +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); + } +} diff --git a/nur_tests/data/types/IntTypeTest.php b/nur_tests/data/types/IntTypeTest.php new file mode 100644 index 0000000..76c326d --- /dev/null +++ b/nur_tests/data/types/IntTypeTest.php @@ -0,0 +1,209 @@ +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"); + } +} diff --git a/nur_tests/data/types/KeyTypeTest.php b/nur_tests/data/types/KeyTypeTest.php new file mode 100644 index 0000000..d011396 --- /dev/null +++ b/nur_tests/data/types/KeyTypeTest.php @@ -0,0 +1,27 @@ +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()); + } +} diff --git a/nur_tests/data/types/MailTypeTest.php b/nur_tests/data/types/MailTypeTest.php new file mode 100644 index 0000000..812b44a --- /dev/null +++ b/nur_tests/data/types/MailTypeTest.php @@ -0,0 +1,16 @@ +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")); + } +} diff --git a/nur_tests/data/types/MetadataTest.php b/nur_tests/data/types/MetadataTest.php new file mode 100644 index 0000000..ecf8564 --- /dev/null +++ b/nur_tests/data/types/MetadataTest.php @@ -0,0 +1,674 @@ + [$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); + } +} diff --git a/nur_tests/data/types/PhpIncarnationTest.php b/nur_tests/data/types/PhpIncarnationTest.php new file mode 100644 index 0000000..02e6690 --- /dev/null +++ b/nur_tests/data/types/PhpIncarnationTest.php @@ -0,0 +1,364 @@ +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); + } +} diff --git a/nur_tests/data/types/RawStringTypeTest.php b/nur_tests/data/types/RawStringTypeTest.php new file mode 100644 index 0000000..16aa591 --- /dev/null +++ b/nur_tests/data/types/RawStringTypeTest.php @@ -0,0 +1,34 @@ + ["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])); + } +} diff --git a/nur_tests/data/types/SDateTypeTest.php b/nur_tests/data/types/SDateTypeTest.php new file mode 100644 index 0000000..9175e89 --- /dev/null +++ b/nur_tests/data/types/SDateTypeTest.php @@ -0,0 +1,40 @@ +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")); + } +} diff --git a/nur_tests/data/types/SDatetimeTypeTest.php b/nur_tests/data/types/SDatetimeTypeTest.php new file mode 100644 index 0000000..aba6688 --- /dev/null +++ b/nur_tests/data/types/SDatetimeTypeTest.php @@ -0,0 +1,52 @@ +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")); + } +} diff --git a/nur_tests/data/types/STimeTypeTest.php b/nur_tests/data/types/STimeTypeTest.php new file mode 100644 index 0000000..1cb7a92 --- /dev/null +++ b/nur_tests/data/types/STimeTypeTest.php @@ -0,0 +1,121 @@ +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); + } +} diff --git a/nur_tests/data/types/STimeslotTypeTest.php b/nur_tests/data/types/STimeslotTypeTest.php new file mode 100644 index 0000000..bae662d --- /dev/null +++ b/nur_tests/data/types/STimeslotTypeTest.php @@ -0,0 +1,18 @@ +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")); + } +} diff --git a/nur_tests/data/types/StringTypeTest.php b/nur_tests/data/types/StringTypeTest.php new file mode 100644 index 0000000..20a9905 --- /dev/null +++ b/nur_tests/data/types/StringTypeTest.php @@ -0,0 +1,32 @@ + ["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")); + } +} diff --git a/nur_tests/data/types/TelephoneTypeTest.php b/nur_tests/data/types/TelephoneTypeTest.php new file mode 100644 index 0000000..492c439 --- /dev/null +++ b/nur_tests/data/types/TelephoneTypeTest.php @@ -0,0 +1,38 @@ +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"))); + } +} diff --git a/nur_tests/data/types/TimeTypeTest.php b/nur_tests/data/types/TimeTypeTest.php new file mode 100644 index 0000000..a8bcd50 --- /dev/null +++ b/nur_tests/data/types/TimeTypeTest.php @@ -0,0 +1,130 @@ +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); + } +} diff --git a/nur_tests/data/types/TriboolTypeTest.php b/nur_tests/data/types/TriboolTypeTest.php new file mode 100644 index 0000000..b13a957 --- /dev/null +++ b/nur_tests/data/types/TriboolTypeTest.php @@ -0,0 +1,130 @@ +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("")); + } +} diff --git a/nur_tests/data/types/impl/CAbType.php b/nur_tests/data/types/impl/CAbType.php new file mode 100644 index 0000000..a518143 --- /dev/null +++ b/nur_tests/data/types/impl/CAbType.php @@ -0,0 +1,13 @@ + ["string"], + "cb" => ["int"], + ]; + const SEPARATOR = "-"; + const SEPARATOR_PATTERN = '/^\s*-\s*/'; +} diff --git a/nur_tests/data/types/impl/CBcType.php b/nur_tests/data/types/impl/CBcType.php new file mode 100644 index 0000000..7a9d617 --- /dev/null +++ b/nur_tests/data/types/impl/CBcType.php @@ -0,0 +1,11 @@ + ["int"], + "c" => ["string"], + ]; +} diff --git a/nur_tests/data/types/impl/CSiType.php b/nur_tests/data/types/impl/CSiType.php new file mode 100644 index 0000000..8286a34 --- /dev/null +++ b/nur_tests/data/types/impl/CSiType.php @@ -0,0 +1,17 @@ + [["string", [ + "allowed_values" => ["first", "second"] + ]]], + "i" => [["int", [ + "allow_parse_empty" => true, + ]]], + ]; + const SEPARATOR = "-"; + const SEPARATOR_PATTERN = '/^\s*-\s*/'; +} diff --git a/nur_tests/data/types/impl/CSsType.php b/nur_tests/data/types/impl/CSsType.php new file mode 100644 index 0000000..ed750ff --- /dev/null +++ b/nur_tests/data/types/impl/CSsType.php @@ -0,0 +1,16 @@ + [[RegexpType::class], + "pattern" => '/aa|bx{1,3}/', + ], + "s2" => [["string"], + "allow_parse_empty" => true, + ], + ]; +} diff --git a/nur_tests/data/types/impl/FirstType.php b/nur_tests/data/types/impl/FirstType.php new file mode 100644 index 0000000..df43322 --- /dev/null +++ b/nur_tests/data/types/impl/FirstType.php @@ -0,0 +1,5 @@ +x = $x; + $this->y = $y; + } +} diff --git a/nur_tests/data/types/impl/PointType.php b/nur_tests/data/types/impl/PointType.php new file mode 100644 index 0000000..3549fc0 --- /dev/null +++ b/nur_tests/data/types/impl/PointType.php @@ -0,0 +1,20 @@ +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); + } +} diff --git a/nur_tests/data/types/impl/SecondType.php b/nur_tests/data/types/impl/SecondType.php new file mode 100644 index 0000000..a0581f7 --- /dev/null +++ b/nur_tests/data/types/impl/SecondType.php @@ -0,0 +1,5 @@ +")); + 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; + } +} diff --git a/nur_tests/impl/GBaseArray.php b/nur_tests/impl/GBaseArray.php new file mode 100644 index 0000000..3aa1c74 --- /dev/null +++ b/nur_tests/impl/GBaseArray.php @@ -0,0 +1,9 @@ +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")); + } +} diff --git a/nur_tests/io/csv/CsvReaderTest.php b/nur_tests/io/csv/CsvReaderTest.php new file mode 100644 index 0000000..093cd61 --- /dev/null +++ b/nur_tests/io/csv/CsvReaderTest.php @@ -0,0 +1,151 @@ +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, << "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, << "=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); + + } +} diff --git a/nur_tests/io/csv/CsvWriterTest.php b/nur_tests/io/csv/CsvWriterTest.php new file mode 100644 index 0000000..478ae68 --- /dev/null +++ b/nur_tests/io/csv/CsvWriterTest.php @@ -0,0 +1,116 @@ +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()); + } +} diff --git a/nur_tests/io/csv/cp1252.csv b/nur_tests/io/csv/cp1252.csv new file mode 100644 index 0000000..8195f10 --- /dev/null +++ b/nur_tests/io/csv/cp1252.csv @@ -0,0 +1,2 @@ +nom,prénom,age +clain,jephté,45 diff --git a/nur_tests/io/csv/utf8.csv b/nur_tests/io/csv/utf8.csv new file mode 100644 index 0000000..f12f0c3 --- /dev/null +++ b/nur_tests/io/csv/utf8.csv @@ -0,0 +1,2 @@ +nom,prénom,age +clain,jephté,45 diff --git a/nur_tests/io/fsv/FsvSchemaTest.php b/nur_tests/io/fsv/FsvSchemaTest.php new file mode 100644 index 0000000..163abed --- /dev/null +++ b/nur_tests/io/fsv/FsvSchemaTest.php @@ -0,0 +1,84 @@ +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", + ])); + + } +} diff --git a/nur_tests/mdTest.php b/nur_tests/mdTest.php new file mode 100644 index 0000000..eb3fbfe --- /dev/null +++ b/nur_tests/mdTest.php @@ -0,0 +1,364 @@ + 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++; + } + } +} diff --git a/nur_tests/pathTest.php b/nur_tests/pathTest.php new file mode 100644 index 0000000..28f2d8e --- /dev/null +++ b/nur_tests/pathTest.php @@ -0,0 +1,50 @@ + ["?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)); + } +} diff --git a/nur_tests/strTest.php b/nur_tests/strTest.php new file mode 100644 index 0000000..44e1c0a --- /dev/null +++ b/nur_tests/strTest.php @@ -0,0 +1,47 @@ +getContent()); + self::assertSame("

le titre

\n<helloworld>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("

le titre

\n<helloworld>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(""); + vo::write([""]); + vo::write(new ZePrint()); + vo::write(new ZeStaticContent()); + vo::write(new ZeGeneratedContent()); + } +} diff --git a/tests/.keep b/tests/.keep deleted file mode 100644 index e69de29..0000000