summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorandy wharmby <wharmby@php.net>2009-06-14 12:08:32 +0000
committerandy wharmby <wharmby@php.net>2009-06-14 12:08:32 +0000
commit9e6779cfacce79be7c1f725dfa391c8c79ea0f9f (patch)
tree134e5344f846abf92d971adbba262b5a88b0ce84
parent96271ff8fb9e0556e8021683ae2a017758d24b3f (diff)
downloadphp-git-9e6779cfacce79be7c1f725dfa391c8c79ea0f9f.tar.gz
New json extension tests. Tested on Windows, Linux and Linux 64 bit.
-rw-r--r--ext/json/tests/json_decode_basic.phpt187
-rw-r--r--ext/json/tests/json_decode_error.phpt39
-rw-r--r--ext/json/tests/json_encode_basic.phpt158
-rw-r--r--ext/json/tests/json_encode_basic_utf8.phpt26
-rw-r--r--ext/json/tests/json_encode_error.phpt40
5 files changed, 450 insertions, 0 deletions
diff --git a/ext/json/tests/json_decode_basic.phpt b/ext/json/tests/json_decode_basic.phpt
new file mode 100644
index 0000000000..6dbeadb367
--- /dev/null
+++ b/ext/json/tests/json_decode_basic.phpt
@@ -0,0 +1,187 @@
+--TEST--
+Test json_decode() function : basic functionality
+--SKIPIF--
+<?php
+if (!extension_loaded("json")) {
+ die('skip JSON extension not available in this build');
+}
+?>
+--FILE--
+<?php
+/* Prototype : mixed json_decode ( string $json [, bool $assoc ] )
+ * Description: Decodes a JSON string
+ * Source code: ext/json/php_json.c
+ * Alias to functions:
+ */
+echo "*** Testing json_decode() : basic functionality ***\n";
+
+// array with different values for $string
+$inputs = array (
+ '0',
+ '123',
+ '-123',
+ '2147483647',
+ '-2147483648',
+ '123.456',
+ '1230',
+ '-1230',
+ 'true',
+ 'false',
+ 'null',
+ '"abc"',
+ '"Hello World\r\n"',
+ '[]',
+ '[1,2,3,4,5]',
+ '{"myInt":99,"myFloat":123.45,"myNull":null,"myBool":true,"myString":"Hello World"}',
+ '{"Jan":31,"Feb":29,"Mar":31,"April":30,"May":31,"June":30}',
+ '""',
+ '{}'
+);
+
+// loop through with each element of the $inputs array to test json_decode() function
+$count = 1;
+foreach($inputs as $input) {
+ echo "-- Iteration $count --\n";
+ var_dump(json_decode($input));
+ var_dump(json_decode($input, TRUE));
+ $count ++;
+}
+
+?>
+===Done===
+--EXPECTF--
+*** Testing json_decode() : basic functionality ***
+-- Iteration 1 --
+int(0)
+int(0)
+-- Iteration 2 --
+int(123)
+int(123)
+-- Iteration 3 --
+int(-123)
+int(-123)
+-- Iteration 4 --
+int(2147483647)
+int(2147483647)
+-- Iteration 5 --
+int(-2147483648)
+int(-2147483648)
+-- Iteration 6 --
+float(123.456)
+float(123.456)
+-- Iteration 7 --
+int(1230)
+int(1230)
+-- Iteration 8 --
+int(-1230)
+int(-1230)
+-- Iteration 9 --
+bool(true)
+bool(true)
+-- Iteration 10 --
+bool(false)
+bool(false)
+-- Iteration 11 --
+NULL
+NULL
+-- Iteration 12 --
+string(3) "abc"
+string(3) "abc"
+-- Iteration 13 --
+string(13) "Hello World
+"
+string(13) "Hello World
+"
+-- Iteration 14 --
+array(0) {
+}
+array(0) {
+}
+-- Iteration 15 --
+array(5) {
+ [0]=>
+ int(1)
+ [1]=>
+ int(2)
+ [2]=>
+ int(3)
+ [3]=>
+ int(4)
+ [4]=>
+ int(5)
+}
+array(5) {
+ [0]=>
+ int(1)
+ [1]=>
+ int(2)
+ [2]=>
+ int(3)
+ [3]=>
+ int(4)
+ [4]=>
+ int(5)
+}
+-- Iteration 16 --
+object(stdClass)#%d (5) {
+ ["myInt"]=>
+ int(99)
+ ["myFloat"]=>
+ float(123.45)
+ ["myNull"]=>
+ NULL
+ ["myBool"]=>
+ bool(true)
+ ["myString"]=>
+ string(11) "Hello World"
+}
+array(5) {
+ ["myInt"]=>
+ int(99)
+ ["myFloat"]=>
+ float(123.45)
+ ["myNull"]=>
+ NULL
+ ["myBool"]=>
+ bool(true)
+ ["myString"]=>
+ string(11) "Hello World"
+}
+-- Iteration 17 --
+object(stdClass)#%d (6) {
+ ["Jan"]=>
+ int(31)
+ ["Feb"]=>
+ int(29)
+ ["Mar"]=>
+ int(31)
+ ["April"]=>
+ int(30)
+ ["May"]=>
+ int(31)
+ ["June"]=>
+ int(30)
+}
+array(6) {
+ ["Jan"]=>
+ int(31)
+ ["Feb"]=>
+ int(29)
+ ["Mar"]=>
+ int(31)
+ ["April"]=>
+ int(30)
+ ["May"]=>
+ int(31)
+ ["June"]=>
+ int(30)
+}
+-- Iteration 18 --
+string(0) ""
+string(0) ""
+-- Iteration 19 --
+object(stdClass)#%d (0) {
+}
+array(0) {
+}
+===Done===
diff --git a/ext/json/tests/json_decode_error.phpt b/ext/json/tests/json_decode_error.phpt
new file mode 100644
index 0000000000..4cdc05cb48
--- /dev/null
+++ b/ext/json/tests/json_decode_error.phpt
@@ -0,0 +1,39 @@
+--TEST--
+Test json_decode() function : error conditions
+--SKIPIF--
+<?php
+if (!extension_loaded("json")) {
+ die('skip JSON extension not available in this build');
+}
+?>
+--FILE--
+<?php
+/* Prototype : mixed json_decode ( string $json [, bool $assoc ] )
+ * Description: Decodes a JSON string
+ * Source code: ext/json/php_json.c
+ * Alias to functions:
+ */
+echo "*** Testing json_decode() : error conditions ***\n";
+
+echo "\n-- Testing json_decode() function with no arguments --\n";
+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, $extra_arg) );
+
+?>
+===Done===
+--EXPECTF--
+*** Testing json_decode() : error conditions ***
+
+-- Testing json_decode() function with no arguments --
+
+Warning: json_decode() expects at least 1 parameter, 0 given in %s on line %d
+NULL
+
+-- Testing json_decode() function with more than expected no. of arguments --
+
+Warning: json_decode() expects at most 2 parameters, 3 given in %s on line %d
+NULL
+===Done===
diff --git a/ext/json/tests/json_encode_basic.phpt b/ext/json/tests/json_encode_basic.phpt
new file mode 100644
index 0000000000..4124d06588
--- /dev/null
+++ b/ext/json/tests/json_encode_basic.phpt
@@ -0,0 +1,158 @@
+--TEST--
+Test json_encode() function : basic functionality
+--SKIPIF--
+<?php
+if (!extension_loaded("json")) {
+ die('skip JSON extension not available in this build');
+}
+?>
+--FILE--
+<?php
+/* Prototype : string json_encode ( mixed $value )
+ * Description: Returns the JSON representation of a value
+ * Source code: ext/json/php_json.c
+ * Alias to functions:
+ */
+echo "*** Testing json_encode() : basic functionality ***\n";
+
+//get an unset variable
+$unset_var = 10;
+unset ($unset_var);
+
+// get a resource variable
+$fp = fopen(__FILE__, "r");
+
+// get an object
+class sample {
+}
+
+$obj = new sample();
+$obj->MyInt = 99;
+$obj->MyFloat = 123.45;
+$obj->MyBool = true;
+$obj->MyNull = null;
+$obj->MyString = "Hello World";
+
+// array with different values for $string
+$inputs = array (
+
+ // integers
+/*1*/ 0,
+ 123,
+ -123,
+ 2147483647,
+ -2147483648,
+
+ // floats
+/*6*/ 123.456,
+ 1.23E3,
+ -1.23E3,
+
+ // boolean
+/*9*/ TRUE,
+ true,
+ FALSE,
+ false,
+
+ // NULL
+/*13*/ NULL,
+ null,
+
+ // strings
+/*15*/ "abc",
+ 'abc',
+ "Hello\t\tWorld\n",
+
+ // arrays
+/*18*/ array(),
+ array(1,2,3,4,5),
+ array(1 => "Sun", 2=>"Mon", 3 => "Tue", 4 => "Wed", 5 => "Thur", 6 => "Fri", 7 => "Sat"),
+ array("Jan" => 31, "Feb" => 29, "Mar" => 31, "April" => 30, "May" => 31, "June" => 30),
+
+ // empty data
+/*22*/ "",
+ '',
+
+ // undefined data
+/*24*/ @$undefined_var,
+
+ // unset data
+/*25*/ @$unset_var,
+
+ // resource variable
+/*26*/ $fp,
+
+ // object variable
+/*27*/ $obj
+
+);
+
+// loop through with each element of the $inputs array to test json_encode() function
+$count = 1;
+foreach($inputs as $input) {
+ echo "-- Iteration $count --\n";
+ var_dump(json_encode($input));
+ $count ++;
+}
+
+?>
+===Done===
+--EXPECTF--
+*** Testing json_encode() : basic functionality ***
+-- Iteration 1 --
+string(1) "0"
+-- Iteration 2 --
+string(3) "123"
+-- Iteration 3 --
+string(4) "-123"
+-- Iteration 4 --
+string(10) "2147483647"
+-- Iteration 5 --
+string(11) "-2147483648"
+-- Iteration 6 --
+string(7) "123.456"
+-- Iteration 7 --
+string(4) "1230"
+-- Iteration 8 --
+string(5) "-1230"
+-- Iteration 9 --
+string(4) "true"
+-- Iteration 10 --
+string(4) "true"
+-- Iteration 11 --
+string(5) "false"
+-- Iteration 12 --
+string(5) "false"
+-- Iteration 13 --
+string(4) "null"
+-- Iteration 14 --
+string(4) "null"
+-- Iteration 15 --
+string(5) ""abc""
+-- Iteration 16 --
+string(5) ""abc""
+-- Iteration 17 --
+string(18) ""Hello\t\tWorld\n""
+-- Iteration 18 --
+string(2) "[]"
+-- Iteration 19 --
+string(11) "[1,2,3,4,5]"
+-- Iteration 20 --
+string(72) "{"1":"Sun","2":"Mon","3":"Tue","4":"Wed","5":"Thur","6":"Fri","7":"Sat"}"
+-- Iteration 21 --
+string(58) "{"Jan":31,"Feb":29,"Mar":31,"April":30,"May":31,"June":30}"
+-- Iteration 22 --
+string(2) """"
+-- Iteration 23 --
+string(2) """"
+-- Iteration 24 --
+string(4) "null"
+-- Iteration 25 --
+string(4) "null"
+-- Iteration 26 --
+
+Warning: [json] (php_json_encode) type is unsupported, encoded as null in %s on line %d
+string(4) "null"
+-- Iteration 27 --
+string(82) "{"MyInt":99,"MyFloat":123.45,"MyBool":true,"MyNull":null,"MyString":"Hello World"}"
+===Done=== \ No newline at end of file
diff --git a/ext/json/tests/json_encode_basic_utf8.phpt b/ext/json/tests/json_encode_basic_utf8.phpt
new file mode 100644
index 0000000000..a8e8b425ef
--- /dev/null
+++ b/ext/json/tests/json_encode_basic_utf8.phpt
@@ -0,0 +1,26 @@
+--TEST--
+Test json_encode() function : basic functionality with UTF8 string input
+--SKIPIF--
+<?php
+if (!extension_loaded("json")) {
+ die('skip JSON extension not available in this build');
+}
+?>
+--FILE--
+<?php
+/* Prototype : string json_encode ( mixed $value )
+ * Description: Returns the JSON representation of a value
+ * Source code: ext/json/php_json.c
+ * Alias to functions:
+ */
+echo "*** Testing json_encode() : basic functionality with UTF-8 input***\n";
+
+$utf8_string = base64_decode('5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII=');
+var_dump(json_encode($utf8_string));
+
+?>
+===Done===
+--EXPECTF--
+*** Testing json_encode() : basic functionality with UTF-8 input***
+string(103) ""\u65e5\u672c\u8a9e\u30c6\u30ad\u30b9\u30c8\u3067\u3059\u300201234\uff15\uff16\uff17\uff18\uff19\u3002""
+===Done===
diff --git a/ext/json/tests/json_encode_error.phpt b/ext/json/tests/json_encode_error.phpt
new file mode 100644
index 0000000000..b0aaa49361
--- /dev/null
+++ b/ext/json/tests/json_encode_error.phpt
@@ -0,0 +1,40 @@
+--TEST--
+Test json_encode() function : error conditions
+--SKIPIF--
+<?php
+if (!extension_loaded("json")) {
+ die('skip JSON extension not available in this build');
+}
+?>
+--FILE--
+<?php
+/* Prototype : string json_encode ( mixed $value )
+ * Description: Returns the JSON representation of a value
+ * Source code: ext/json/php_json.c
+ * Alias to functions:
+ */
+
+echo "*** Testing json_encode() : error conditions ***\n";
+
+echo "\n-- Testing json_encode() function with no arguments --\n";
+var_dump( json_encode() );
+
+echo "\n-- Testing json_encode() function with more than expected no. of arguments --\n";
+$extra_arg = 10;
+var_dump( json_encode("abc", $extra_arg) );
+
+?>
+===Done===
+--EXPECTF--
+*** Testing json_encode() : error conditions ***
+
+-- Testing json_encode() function with no arguments --
+
+Warning: json_encode() expects exactly 1 parameter, 0 given in %s on line %d
+NULL
+
+-- Testing json_encode() function with more than expected no. of arguments --
+
+Warning: json_encode() expects exactly 1 parameter, 2 given in %s on line %d
+NULL
+===Done===