diff options
Diffstat (limited to 'ext/json/tests')
-rw-r--r-- | ext/json/tests/008.phpt | 17 | ||||
-rw-r--r-- | ext/json/tests/bug43941.phpt | 20 | ||||
-rw-r--r-- | ext/json/tests/bug53946.phpt | 16 | ||||
-rw-r--r-- | ext/json/tests/bug61978.phpt | 43 | ||||
-rw-r--r-- | ext/json/tests/json_decode_error.phpt | 4 | ||||
-rw-r--r-- | ext/json/tests/json_encode_numeric.phpt | 26 | ||||
-rw-r--r-- | ext/json/tests/json_encode_pretty_print.phpt | 40 | ||||
-rw-r--r-- | ext/json/tests/json_encode_unescaped_slashes.phpt | 12 | ||||
-rw-r--r-- | ext/json/tests/serialize.phpt | 80 |
9 files changed, 256 insertions, 2 deletions
diff --git a/ext/json/tests/008.phpt b/ext/json/tests/008.phpt new file mode 100644 index 0000000000..f2354d381f --- /dev/null +++ b/ext/json/tests/008.phpt @@ -0,0 +1,17 @@ +--TEST-- +json_decode() with large integers +--SKIPIF-- +<?php if (!extension_loaded("json")) print "skip"; ?> +--FILE-- +<?php +$json = '{"largenum":123456789012345678901234567890}'; +$x = json_decode($json); +var_dump($x->largenum); +$x = json_decode($json, false, 512, JSON_BIGINT_AS_STRING); +var_dump($x->largenum); +echo "Done\n"; +?> +--EXPECT-- +float(1.2345678901235E+29) +string(30) "123456789012345678901234567890" +Done diff --git a/ext/json/tests/bug43941.phpt b/ext/json/tests/bug43941.phpt new file mode 100644 index 0000000000..48bd7ad524 --- /dev/null +++ b/ext/json/tests/bug43941.phpt @@ -0,0 +1,20 @@ +--TEST-- +Bug #43941 (json_encode() invalid UTF-8) +--SKIPIF-- +<?php if (!extension_loaded("json")) print "skip"; ?> +--FILE-- +<?php + +var_dump(json_encode("abc")); +var_dump(json_encode("ab\xE0")); +var_dump(json_encode("ab\xE0", JSON_PARTIAL_OUTPUT_ON_ERROR)); +var_dump(json_encode(array("ab\xE0", "ab\xE0c", "abc"), JSON_PARTIAL_OUTPUT_ON_ERROR)); + +echo "Done\n"; +?> +--EXPECTF-- +string(5) ""abc"" +bool(false) +string(4) "null" +string(17) "[null,null,"abc"]" +Done diff --git a/ext/json/tests/bug53946.phpt b/ext/json/tests/bug53946.phpt new file mode 100644 index 0000000000..111438ddc4 --- /dev/null +++ b/ext/json/tests/bug53946.phpt @@ -0,0 +1,16 @@ +--TEST-- +bug #53946 (json_encode() with JSON_UNESCAPED_UNICODE) +--SKIPIF-- +<?php if (!extension_loaded("json")) print "skip"; ?> +--FILE-- +<?php +var_dump(json_encode("latin 1234 -/ russian мама мыла раму specialchars \x02 \x08 \n U+1D11E >𝄞<")); +var_dump(json_encode("latin 1234 -/ russian мама мыла раму specialchars \x02 \x08 \n U+1D11E >𝄞<", JSON_UNESCAPED_UNICODE)); +var_dump(json_encode("ab\xE0")); +var_dump(json_encode("ab\xE0", JSON_UNESCAPED_UNICODE)); +?> +--EXPECTF-- +string(156) ""latin 1234 -\/ russian \u043c\u0430\u043c\u0430 \u043c\u044b\u043b\u0430 \u0440\u0430\u043c\u0443 specialchars \u0002 \b \n U+1D11E >\ud834\udd1e<"" +string(100) ""latin 1234 -\/ russian мама мыла раму specialchars \u0002 \b \n U+1D11E >𝄞<"" +bool(false) +bool(false) diff --git a/ext/json/tests/bug61978.phpt b/ext/json/tests/bug61978.phpt new file mode 100644 index 0000000000..c34b03f8f7 --- /dev/null +++ b/ext/json/tests/bug61978.phpt @@ -0,0 +1,43 @@ +--TEST-- +Bug #61978 (Object recursion not detected for classes that implement JsonSerializable) +--SKIPIF-- +<?php if (!extension_loaded("json")) print "skip"; ?> +--FILE-- +<?php + +class JsonTest1 { + public $test; + public $me; + public function __construct() { + $this->test = '123'; + $this->me = $this; + } +} + +class JsonTest2 implements JsonSerializable { + public $test; + public function __construct() { + $this->test = '123'; + } + public function jsonSerialize() { + return array( + 'test' => $this->test, + 'me' => $this + ); + } +} + + +$obj1 = new JsonTest1(); +var_dump(json_encode($obj1, JSON_PARTIAL_OUTPUT_ON_ERROR)); + +echo "==\n"; + +$obj2 = new JsonTest2(); +var_dump(json_encode($obj2, JSON_PARTIAL_OUTPUT_ON_ERROR)); + +?> +--EXPECTF-- +string(44) "{"test":"123","me":{"test":"123","me":null}}" +== +string(44) "{"test":"123","me":{"test":"123","me":null}}" diff --git a/ext/json/tests/json_decode_error.phpt b/ext/json/tests/json_decode_error.phpt index f3387c21be..4d5d4e4bee 100644 --- a/ext/json/tests/json_decode_error.phpt +++ b/ext/json/tests/json_decode_error.phpt @@ -20,7 +20,7 @@ var_dump( json_decode() ); echo "\n-- Testing json_decode() function with more than expected no. of arguments --\n"; $extra_arg = 10; -var_dump( json_decode('"abc"', TRUE, 512, $extra_arg) ); +var_dump( json_decode('"abc"', TRUE, 512, 0, $extra_arg) ); ?> ===Done=== @@ -34,6 +34,6 @@ NULL -- Testing json_decode() function with more than expected no. of arguments -- -Warning: json_decode() expects at most 3 parameters, 4 given in %s on line %d +Warning: json_decode() expects at most 4 parameters, 5 given in %s on line %d NULL ===Done=== diff --git a/ext/json/tests/json_encode_numeric.phpt b/ext/json/tests/json_encode_numeric.phpt new file mode 100644 index 0000000000..5392350194 --- /dev/null +++ b/ext/json/tests/json_encode_numeric.phpt @@ -0,0 +1,26 @@ +--TEST-- +Test json_encode() function with numeric flag +--SKIPIF-- +<?php +if (!extension_loaded("json")) { + die('skip JSON extension not available in this build'); +} +?> +--FILE-- +<?php +var_dump( + json_encode("1", JSON_NUMERIC_CHECK), + json_encode("9.4324", JSON_NUMERIC_CHECK), + json_encode(array("122321", "3232595.33423"), JSON_NUMERIC_CHECK), + json_encode("1"), + json_encode("9.4324"), + json_encode(array("122321", "3232595.33423")) +); +?> +--EXPECT-- +string(1) "1" +string(6) "9.4324" +string(22) "[122321,3232595.33423]" +string(3) ""1"" +string(8) ""9.4324"" +string(26) "["122321","3232595.33423"]" diff --git a/ext/json/tests/json_encode_pretty_print.phpt b/ext/json/tests/json_encode_pretty_print.phpt new file mode 100644 index 0000000000..43b93aafe6 --- /dev/null +++ b/ext/json/tests/json_encode_pretty_print.phpt @@ -0,0 +1,40 @@ +--TEST-- +json_encode() with JSON_PRETTY_PRINT +--SKIPIF-- +<?php if (!extension_loaded("json")) print "skip"; ?> +--FILE-- +<?php +function encode_decode($json) { + $struct = json_decode($json); + $pretty = json_encode($struct, JSON_PRETTY_PRINT); + echo "$pretty\n"; + $pretty = json_decode($pretty); + printf("Match: %d\n", $pretty == $struct); +} + +encode_decode('[1,2,3,[1,2,3]]'); +encode_decode('{"a":1,"b":[1,2],"c":{"d":42}}'); +?> +--EXPECT-- +[ + 1, + 2, + 3, + [ + 1, + 2, + 3 + ] +] +Match: 1 +{ + "a": 1, + "b": [ + 1, + 2 + ], + "c": { + "d": 42 + } +} +Match: 1 diff --git a/ext/json/tests/json_encode_unescaped_slashes.phpt b/ext/json/tests/json_encode_unescaped_slashes.phpt new file mode 100644 index 0000000000..72ebae927a --- /dev/null +++ b/ext/json/tests/json_encode_unescaped_slashes.phpt @@ -0,0 +1,12 @@ +--TEST-- +json_decode() tests +--SKIPIF-- +<?php if (!extension_loaded("json")) print "skip"; ?> +--FILE-- +<?php +var_dump(json_encode('a/b')); +var_dump(json_encode('a/b', JSON_UNESCAPED_SLASHES)); +?> +--EXPECT-- +string(6) ""a\/b"" +string(5) ""a/b"" diff --git a/ext/json/tests/serialize.phpt b/ext/json/tests/serialize.phpt new file mode 100644 index 0000000000..5c513d58ad --- /dev/null +++ b/ext/json/tests/serialize.phpt @@ -0,0 +1,80 @@ +--TEST-- +json_encode() Serialization tests +--SKIPIF-- +<?php if (!extension_loaded("json")) print "skip"; ?> +--FILE-- +<?php + +class NonSerializingTest +{ + public $data; + + public function __construct($data) + { + $this->data = $data; + } +} + +class SerializingTest extends NonSerializingTest implements JsonSerializable +{ + public function jsonSerialize() + { + return $this->data; + } +} + +class ValueSerializingTest extends SerializingTest +{ + public function jsonSerialize() + { + return array_values(is_array($this->data) ? $this->data : get_object_vars($this->data)); + } +} + +class SelfSerializingTest extends SerializingTest +{ + public function jsonSerialize() + { + return $this; + } +} + +$adata = array( + 'str' => 'foo', + 'int' => 1, + 'float' => 2.3, + 'bool' => false, + 'nil' => null, + 'arr' => array(1,2,3), + 'obj' => new StdClass, +); + +$ndata = array_values($adata); + +$odata = (object)$adata; + +foreach(array('NonSerializingTest','SerializingTest','ValueSerializingTest','SelfSerializingTest') as $class) { + echo "==$class==\n"; + echo json_encode(new $class($adata)), "\n"; + echo json_encode(new $class($ndata)), "\n"; + echo json_encode(new $class($odata)), "\n"; +} +--EXPECT-- +==NonSerializingTest== +{"data":{"str":"foo","int":1,"float":2.3,"bool":false,"nil":null,"arr":[1,2,3],"obj":{}}} +{"data":["foo",1,2.3,false,null,[1,2,3],{}]} +{"data":{"str":"foo","int":1,"float":2.3,"bool":false,"nil":null,"arr":[1,2,3],"obj":{}}} +==SerializingTest== +{"str":"foo","int":1,"float":2.3,"bool":false,"nil":null,"arr":[1,2,3],"obj":{}} +["foo",1,2.3,false,null,[1,2,3],{}] +{"str":"foo","int":1,"float":2.3,"bool":false,"nil":null,"arr":[1,2,3],"obj":{}} +==ValueSerializingTest== +["foo",1,2.3,false,null,[1,2,3],{}] +["foo",1,2.3,false,null,[1,2,3],{}] +["foo",1,2.3,false,null,[1,2,3],{}] +==SelfSerializingTest== +{"data":{"str":"foo","int":1,"float":2.3,"bool":false,"nil":null,"arr":[1,2,3],"obj":{}}} +{"data":["foo",1,2.3,false,null,[1,2,3],{}]} +{"data":{"str":"foo","int":1,"float":2.3,"bool":false,"nil":null,"arr":[1,2,3],"obj":{}}} + + |