From 94635c0c00eb004868f0f1116e3f46e5cd8f9d2a Mon Sep 17 00:00:00 2001 From: Shish Date: Tue, 28 Jan 2020 00:47:30 +0000 Subject: [PATCH] add some tests --- core/polyfills.php | 30 ------------- core/tests/block.test.php | 13 ++++++ core/tests/polyfills.test.php | 85 +++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 30 deletions(-) create mode 100644 core/tests/block.test.php diff --git a/core/polyfills.php b/core/polyfills.php index a4044067..894d6164 100644 --- a/core/polyfills.php +++ b/core/polyfills.php @@ -792,36 +792,6 @@ function iterator_map_to_array(callable $callback, iterator $iter): array return iterator_to_array(iterator_map($callback, $iter)); } -function get_class_from_file(string $file): string -{ - $fp = fopen($file, 'r'); - $class = $buffer = ''; - $i = 0; - while (!$class) { - if (feof($fp)) { - break; - } - - $buffer .= fread($fp, 512); - $tokens = token_get_all($buffer); - - if (strpos($buffer, '{') === false) { - continue; - } - - for (;$iassertEquals( + "

head

body
\n", + $b->get_html() + ); + } +} diff --git a/core/tests/polyfills.test.php b/core/tests/polyfills.test.php index c61705b8..cbd33f40 100644 --- a/core/tests/polyfills.test.php +++ b/core/tests/polyfills.test.php @@ -25,6 +25,40 @@ class PolyfillsTest extends \PHPUnit\Framework\TestCase $this->assertEquals(int_escape("1"), 1); $this->assertEquals(int_escape("-1"), -1); $this->assertEquals(int_escape("-1.5"), -1); + $this->assertEquals(int_escape(null), 0); + } + + public function test_url_escape() + { + $this->assertEquals(url_escape("^\o/^"), "%5E%5E%5Ebo%5Es%5E%5E"); + $this->assertEquals(url_escape(null), ""); + } + + public function test_bool_escape() + { + $this->assertTrue(bool_escape(true)); + $this->assertFalse(bool_escape(false)); + + $this->assertTrue(bool_escape("true")); + $this->assertFalse(bool_escape("false")); + + $this->assertTrue(bool_escape("t")); + $this->assertFalse(bool_escape("f")); + + $this->assertTrue(bool_escape("T")); + $this->assertFalse(bool_escape("F")); + + $this->assertTrue(bool_escape("yes")); + $this->assertFalse(bool_escape("no")); + + $this->assertTrue(bool_escape("Yes")); + $this->assertFalse(bool_escape("No")); + + $this->assertTrue(bool_escape("on")); + $this->assertFalse(bool_escape("off")); + + $this->assertTrue(bool_escape(1)); + $this->assertFalse(bool_escape(0)); } public function test_clamp() @@ -36,9 +70,26 @@ class PolyfillsTest extends \PHPUnit\Framework\TestCase $this->assertEquals(clamp(15, 5, 10), 10); } + public function test_xml_tag() + { + $this->assertEquals( + "\n\n\n", + xml_tag("test", ["foo"=>"bar"], ["cake"]) + ); + } + + public function truncate() + { + $this->assertEquals(truncate("test words", 10), "test words"); + $this->assertEquals(truncate("test words", 6), "test..."); + $this->assertEquals(truncate("test words", 9), "test..."); + $this->assertEquals(truncate("test words", 2), "te..."); + } + public function test_shorthand_int() { $this->assertEquals(to_shorthand_int(1231231231), "1.1GB"); + $this->assertEquals(to_shorthand_int(2), "2"); $this->assertEquals(parse_shorthand_int("foo"), -1); $this->assertEquals(parse_shorthand_int("32M"), 33554432); @@ -46,6 +97,33 @@ class PolyfillsTest extends \PHPUnit\Framework\TestCase $this->assertEquals(parse_shorthand_int("1231231231"), 1231231231); } + public function test_autodate() { + $this->assertEquals( + "", + autodate("2012-06-23 16:14:22") + ); + } + + public function test_validate_input() { + $_POST = [ + "foo" => " bar ", + "to_null" => " ", + "num" => "42", + ]; + $this->assertEquals( + ["foo"=>"bar"], + validate_input(["foo"=>"string,trim,lower"]) + ); + //$this->assertEquals( + // ["to_null"=>null], + // validate_input(["to_null"=>"string,trim,nullify"]) + //); + $this->assertEquals( + ["num"=>42], + validate_input(["num"=>"int"]) + ); + } + public function test_sanitize_path() { $this->assertEquals( @@ -116,4 +194,11 @@ class PolyfillsTest extends \PHPUnit\Framework\TestCase join_path("\\/////\\\\one/\///"."\\//two\/\\//\\//", "//\/\\\/three/\\/\/") ); } + + public function test_stringer() { + $this->assertEquals( + '["foo"=>"bar", "baz"=>[1, 2, 3], "qux"=>["a"=>"b"]]', + stringer(["foo"=>"bar", "baz"=>[1,2,3], "qux"=>["a"=>"b"]]) + ); + } }