| 1 | <?php |
|---|
| 2 | // $Id$ |
|---|
| 3 | /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ |
|---|
| 4 | |
|---|
| 5 | /** |
|---|
| 6 | * Unit tests for Services_JSON. |
|---|
| 7 | * @see JSON.php |
|---|
| 8 | * |
|---|
| 9 | * @category |
|---|
| 10 | * @package Services_JSON |
|---|
| 11 | * @author Michal Migurski <[email protected]> |
|---|
| 12 | * @author Matt Knapp <mdknapp[at]gmail[dot]com> |
|---|
| 13 | * @author Brett Stimmerman <brettstimmerman[at]gmail[dot]com> |
|---|
| 14 | * @copyright 2005 Michal Migurski |
|---|
| 15 | * @version CVS: $Id$ |
|---|
| 16 | * @license http://www.opensource.org/licenses/bsd-license.php |
|---|
| 17 | * @link http://pear.php.net/pepr/pepr-proposal-show.php?id=198 |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | error_reporting(E_ALL); |
|---|
| 21 | |
|---|
| 22 | require_once 'PHPUnit.php'; |
|---|
| 23 | require_once 'JSON.php'; |
|---|
| 24 | |
|---|
| 25 | class Services_JSON_EncDec_TestCase extends PHPUnit_TestCase { |
|---|
| 26 | |
|---|
| 27 | function Services_JSON_EncDec_TestCase($name) { |
|---|
| 28 | $this->PHPUnit_TestCase($name); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | function setUp() { |
|---|
| 32 | $this->json = new Services_JSON(); |
|---|
| 33 | |
|---|
| 34 | $obj = new stdClass(); |
|---|
| 35 | $obj->a_string = '"he":llo}:{world'; |
|---|
| 36 | $obj->an_array = array(1, 2, 3); |
|---|
| 37 | $obj->obj = new stdClass(); |
|---|
| 38 | $obj->obj->a_number = 123; |
|---|
| 39 | |
|---|
| 40 | $this->obj = $obj; |
|---|
| 41 | $this->obj_j = '{"a_string":"\"he\":llo}:{world","an_array":[1,2,3],"obj":{"a_number":123}}'; |
|---|
| 42 | $this->obj_d = 'object with properties, nested object and arrays'; |
|---|
| 43 | |
|---|
| 44 | $this->arr = array(null, true, array(1, 2, 3), "hello\"],[world!"); |
|---|
| 45 | $this->arr_j = '[null,true,[1,2,3],"hello\"],[world!"]'; |
|---|
| 46 | $this->arr_d = 'array with elements and nested arrays'; |
|---|
| 47 | |
|---|
| 48 | $this->str1 = 'hello world'; |
|---|
| 49 | $this->str1_j = '"hello world"'; |
|---|
| 50 | $this->str1_j_ = "'hello world'"; |
|---|
| 51 | $this->str1_d = 'hello world'; |
|---|
| 52 | $this->str1_d_ = 'hello world, double quotes'; |
|---|
| 53 | |
|---|
| 54 | $this->str2 = "hello\t\"world\""; |
|---|
| 55 | $this->str2_j = '"hello\\t\\"world\\""'; |
|---|
| 56 | $this->str2_d = 'hello world, with tab, double-quotes'; |
|---|
| 57 | |
|---|
| 58 | $this->str3 = "\\\r\n\t\"/"; |
|---|
| 59 | $this->str3_j = '"\\\\\\r\\n\\t\\"\\/"'; |
|---|
| 60 | $this->str3_d = 'backslash, return, newline, tab, double-quote'; |
|---|
| 61 | |
|---|
| 62 | $this->str4 = 'héllö wørłd'; |
|---|
| 63 | $this->str4_j = '"h\u00e9ll\u00f6 w\u00f8r\u0142d"'; |
|---|
| 64 | $this->str4_j_ = '"héllö wørłd"'; |
|---|
| 65 | $this->str4_d = 'hello world, with unicode'; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | function test_to_JSON() |
|---|
| 69 | { |
|---|
| 70 | $this->assertEquals('null', $this->json->encode(null), 'type case: null'); |
|---|
| 71 | $this->assertEquals('true', $this->json->encode(true), 'type case: boolean true'); |
|---|
| 72 | $this->assertEquals('false', $this->json->encode(false), 'type case: boolean false'); |
|---|
| 73 | |
|---|
| 74 | $this->assertEquals('1', $this->json->encode(1), 'numeric case: 1'); |
|---|
| 75 | $this->assertEquals('-1', $this->json->encode(-1), 'numeric case: -1'); |
|---|
| 76 | $this->assertEquals('1.000000', $this->json->encode(1.0), 'numeric case: 1.0'); |
|---|
| 77 | $this->assertEquals('1.100000', $this->json->encode(1.1), 'numeric case: 1.1'); |
|---|
| 78 | |
|---|
| 79 | $this->assertEquals($this->str1_j, $this->json->encode($this->str1), "string case: {$this->str1_d}"); |
|---|
| 80 | $this->assertEquals($this->str2_j, $this->json->encode($this->str2), "string case: {$this->str2_d}"); |
|---|
| 81 | $this->assertEquals($this->str3_j, $this->json->encode($this->str3), "string case: {$this->str3_d}"); |
|---|
| 82 | $this->assertEquals($this->str4_j, $this->json->encode($this->str4), "string case: {$this->str4_d}"); |
|---|
| 83 | |
|---|
| 84 | $this->assertEquals($this->arr_j, $this->json->encode($this->arr), "array case: {$this->arr_d}"); |
|---|
| 85 | $this->assertEquals($this->obj_j, $this->json->encode($this->obj), "object case: {$this->obj_d}"); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | function test_from_JSON() |
|---|
| 89 | { |
|---|
| 90 | $this->assertEquals(null, $this->json->decode('null'), 'type case: null'); |
|---|
| 91 | $this->assertEquals(true, $this->json->decode('true'), 'type case: boolean true'); |
|---|
| 92 | $this->assertEquals(false, $this->json->decode('false'), 'type case: boolean false'); |
|---|
| 93 | |
|---|
| 94 | $this->assertEquals(1, $this->json->decode('1'), 'numeric case: 1'); |
|---|
| 95 | $this->assertEquals(-1, $this->json->decode('-1'), 'numeric case: -1'); |
|---|
| 96 | $this->assertEquals(1.0, $this->json->decode('1.0'), 'numeric case: 1.0'); |
|---|
| 97 | $this->assertEquals(1.1, $this->json->decode('1.1'), 'numeric case: 1.1'); |
|---|
| 98 | |
|---|
| 99 | $this->assertEquals(11.0, $this->json->decode('1.1e1'), 'numeric case: 1.1e1'); |
|---|
| 100 | $this->assertEquals(11.0, $this->json->decode('1.10e+1'), 'numeric case: 1.10e+1'); |
|---|
| 101 | $this->assertEquals(0.11, $this->json->decode('1.1e-1'), 'numeric case: 1.1e-1'); |
|---|
| 102 | $this->assertEquals(-0.11, $this->json->decode('-1.1e-1'), 'numeric case: -1.1e-1'); |
|---|
| 103 | |
|---|
| 104 | $this->assertEquals($this->str1, $this->json->decode($this->str1_j), "string case: {$this->str1_d}"); |
|---|
| 105 | $this->assertEquals($this->str1, $this->json->decode($this->str1_j_), "string case: {$this->str1_d_}"); |
|---|
| 106 | $this->assertEquals($this->str2, $this->json->decode($this->str2_j), "string case: {$this->str2_d}"); |
|---|
| 107 | $this->assertEquals($this->str3, $this->json->decode($this->str3_j), "string case: {$this->str3_d}"); |
|---|
| 108 | $this->assertEquals($this->str4, $this->json->decode($this->str4_j), "string case: {$this->str4_d}"); |
|---|
| 109 | $this->assertEquals($this->str4, $this->json->decode($this->str4_j_), "string case: {$this->str4_d}"); |
|---|
| 110 | |
|---|
| 111 | $this->assertEquals($this->arr, $this->json->decode($this->arr_j), "array case: {$this->arr_d}"); |
|---|
| 112 | $this->assertEquals($this->obj, $this->json->decode($this->obj_j), "object case: {$this->obj_d}"); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | function test_to_then_from_JSON() |
|---|
| 116 | { |
|---|
| 117 | $this->assertEquals(null, $this->json->decode($this->json->encode(null)), 'type case: null'); |
|---|
| 118 | $this->assertEquals(true, $this->json->decode($this->json->encode(true)), 'type case: boolean true'); |
|---|
| 119 | $this->assertEquals(false, $this->json->decode($this->json->encode(false)), 'type case: boolean false'); |
|---|
| 120 | |
|---|
| 121 | $this->assertEquals(1, $this->json->decode($this->json->encode(1)), 'numeric case: 1'); |
|---|
| 122 | $this->assertEquals(-1, $this->json->decode($this->json->encode(-1)), 'numeric case: -1'); |
|---|
| 123 | $this->assertEquals(1.0, $this->json->decode($this->json->encode(1.0)), 'numeric case: 1.0'); |
|---|
| 124 | $this->assertEquals(1.1, $this->json->decode($this->json->encode(1.1)), 'numeric case: 1.1'); |
|---|
| 125 | |
|---|
| 126 | $this->assertEquals($this->str1, $this->json->decode($this->json->encode($this->str1)), "string case: {$this->str1_d}"); |
|---|
| 127 | $this->assertEquals($this->str2, $this->json->decode($this->json->encode($this->str2)), "string case: {$this->str2_d}"); |
|---|
| 128 | $this->assertEquals($this->str3, $this->json->decode($this->json->encode($this->str3)), "string case: {$this->str3_d}"); |
|---|
| 129 | $this->assertEquals($this->str4, $this->json->decode($this->json->encode($this->str4)), "string case: {$this->str4_d}"); |
|---|
| 130 | |
|---|
| 131 | $this->assertEquals($this->arr, $this->json->decode($this->json->encode($this->arr)), "array case: {$this->arr_d}"); |
|---|
| 132 | $this->assertEquals($this->obj, $this->json->decode($this->json->encode($this->obj)), "object case: {$this->obj_d}"); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | function test_from_then_to_JSON() |
|---|
| 136 | { |
|---|
| 137 | $this->assertEquals('null', $this->json->encode($this->json->decode('null')), 'type case: null'); |
|---|
| 138 | $this->assertEquals('true', $this->json->encode($this->json->decode('true')), 'type case: boolean true'); |
|---|
| 139 | $this->assertEquals('false', $this->json->encode($this->json->decode('false')), 'type case: boolean false'); |
|---|
| 140 | |
|---|
| 141 | $this->assertEquals('1', $this->json->encode($this->json->decode('1')), 'numeric case: 1'); |
|---|
| 142 | $this->assertEquals('-1', $this->json->encode($this->json->decode('-1')), 'numeric case: -1'); |
|---|
| 143 | $this->assertEquals('1.0', $this->json->encode($this->json->decode('1.0')), 'numeric case: 1.0'); |
|---|
| 144 | $this->assertEquals('1.1', $this->json->encode($this->json->decode('1.1')), 'numeric case: 1.1'); |
|---|
| 145 | |
|---|
| 146 | $this->assertEquals($this->str1_j, $this->json->encode($this->json->decode($this->str1_j)), "string case: {$this->str1_d}"); |
|---|
| 147 | $this->assertEquals($this->str2_j, $this->json->encode($this->json->decode($this->str2_j)), "string case: {$this->str2_d}"); |
|---|
| 148 | $this->assertEquals($this->str3_j, $this->json->encode($this->json->decode($this->str3_j)), "string case: {$this->str3_d}"); |
|---|
| 149 | $this->assertEquals($this->str4_j, $this->json->encode($this->json->decode($this->str4_j)), "string case: {$this->str4_d}"); |
|---|
| 150 | $this->assertEquals($this->str4_j, $this->json->encode($this->json->decode($this->str4_j_)), "string case: {$this->str4_d}"); |
|---|
| 151 | |
|---|
| 152 | $this->assertEquals($this->arr_j, $this->json->encode($this->json->decode($this->arr_j)), "array case: {$this->arr_d}"); |
|---|
| 153 | $this->assertEquals($this->obj_j, $this->json->encode($this->json->decode($this->obj_j)), "object case: {$this->obj_d}"); |
|---|
| 154 | } |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | class Services_JSON_AssocArray_TestCase extends PHPUnit_TestCase { |
|---|
| 158 | |
|---|
| 159 | function Services_JSON_AssocArray_TestCase($name) { |
|---|
| 160 | $this->PHPUnit_TestCase($name); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | function setUp() { |
|---|
| 164 | $this->json_l = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); |
|---|
| 165 | $this->json_s = new Services_JSON(); |
|---|
| 166 | |
|---|
| 167 | $this->arr = array('car1'=> array('color'=> 'tan', 'model' => 'sedan'), |
|---|
| 168 | 'car2' => array('color' => 'red', 'model' => 'sports')); |
|---|
| 169 | $this->arr_jo = '{"car1":{"color":"tan","model":"sedan"},"car2":{"color":"red","model":"sports"}}'; |
|---|
| 170 | $this->arr_d = 'associative array with nested associative arrays'; |
|---|
| 171 | |
|---|
| 172 | $this->arn = array(0=> array(0=> 'tan\\', 'model\\' => 'sedan'), 1 => array(0 => 'red', 'model' => 'sports')); |
|---|
| 173 | $this->arn_ja = '[{"0":"tan\\\\","model\\\\":"sedan"},{"0":"red","model":"sports"}]'; |
|---|
| 174 | $this->arn_d = 'associative array with nested associative arrays, and some numeric keys thrown in'; |
|---|
| 175 | |
|---|
| 176 | $this->arrs = array (1 => 'one', 2 => 'two', 5 => 'five'); |
|---|
| 177 | $this->arrs_jo = '{"1":"one","2":"two","5":"five"}'; |
|---|
| 178 | $this->arrs_d = 'associative array numeric keys which are not fully populated in a range of 0 to length-1'; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | function test_type() |
|---|
| 182 | { |
|---|
| 183 | $this->assertEquals('array', gettype($this->json_l->decode($this->arn_ja)), "loose type should be array"); |
|---|
| 184 | $this->assertEquals('array', gettype($this->json_s->decode($this->arn_ja)), "strict type should be array"); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | function test_to_JSON() |
|---|
| 188 | { |
|---|
| 189 | // both strict and loose JSON should result in an object |
|---|
| 190 | $this->assertEquals($this->arr_jo, $this->json_l->encode($this->arr), "array case - loose: {$this->arr_d}"); |
|---|
| 191 | $this->assertEquals($this->arr_jo, $this->json_s->encode($this->arr), "array case - strict: {$this->arr_d}"); |
|---|
| 192 | |
|---|
| 193 | // ...unless the input array has some numeric indeces, in which case the behavior is to degrade to a regular array |
|---|
| 194 | $this->assertEquals($this->arn_ja, $this->json_s->encode($this->arn), "array case - strict: {$this->arn_d}"); |
|---|
| 195 | |
|---|
| 196 | // Test a sparsely populated numerically indexed associative array |
|---|
| 197 | $this->assertEquals($this->arrs_jo, $this->json_l->encode($this->arrs), "sparse numeric assoc array: {$this->arrs_d}"); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | function test_to_then_from_JSON() |
|---|
| 201 | { |
|---|
| 202 | // these tests motivated by a bug in which strings that end |
|---|
| 203 | // with backslashes followed by quotes were incorrectly decoded. |
|---|
| 204 | |
|---|
| 205 | foreach(array('\\"', '\\\\"', '\\"\\"', '\\""\\""', '\\\\"\\\\"') as $v) { |
|---|
| 206 | $this->assertEquals(array($v), $this->json_l->decode($this->json_l->encode(array($v)))); |
|---|
| 207 | $this->assertEquals(array('a' => $v), $this->json_l->decode($this->json_l->encode(array('a' => $v)))); |
|---|
| 208 | } |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | class Services_JSON_NestedArray_TestCase extends PHPUnit_TestCase { |
|---|
| 213 | |
|---|
| 214 | function Services_JSON_NestedArray_TestCase($name) { |
|---|
| 215 | $this->PHPUnit_TestCase($name); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | function setUp() { |
|---|
| 219 | $this->json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); |
|---|
| 220 | |
|---|
| 221 | $this->str1 = '[{"this":"that"}]'; |
|---|
| 222 | $this->arr1 = array(array('this' => 'that')); |
|---|
| 223 | |
|---|
| 224 | $this->str2 = '{"this":["that"]}'; |
|---|
| 225 | $this->arr2 = array('this' => array('that')); |
|---|
| 226 | |
|---|
| 227 | $this->str3 = '{"params":[{"foo":["1"],"bar":"1"}]}'; |
|---|
| 228 | $this->arr3 = array('params' => array(array('foo' => array('1'), 'bar' => '1'))); |
|---|
| 229 | |
|---|
| 230 | $this->str4 = '{"0": {"foo": "bar", "baz": "winkle"}}'; |
|---|
| 231 | $this->arr4 = array('0' => array('foo' => 'bar', 'baz' => 'winkle')); |
|---|
| 232 | |
|---|
| 233 | $this->str5 = '{"params":[{"options": {"old": [ ], "new": {"0": {"elements": {"old": [], "new": {"0": {"elementName": "aa", "isDefault": false, "elementRank": "0", "priceAdjust": "0", "partNumber": ""}}}, "optionName": "aa", "isRequired": false, "optionDesc": null}}}}]}'; |
|---|
| 234 | $this->arr5 = array ( |
|---|
| 235 | 'params' => array ( |
|---|
| 236 | 0 => array ( |
|---|
| 237 | 'options' => |
|---|
| 238 | array ( |
|---|
| 239 | 'old' => array(), |
|---|
| 240 | 'new' => array ( |
|---|
| 241 | 0 => array ( |
|---|
| 242 | 'elements' => array ( |
|---|
| 243 | 'old' => array(), |
|---|
| 244 | 'new' => array ( |
|---|
| 245 | 0 => array ( |
|---|
| 246 | 'elementName' => 'aa', |
|---|
| 247 | 'isDefault' => false, |
|---|
| 248 | 'elementRank' => '0', |
|---|
| 249 | 'priceAdjust' => '0', |
|---|
| 250 | 'partNumber' => '', |
|---|
| 251 | ), |
|---|
| 252 | ), |
|---|
| 253 | ), |
|---|
| 254 | 'optionName' => 'aa', |
|---|
| 255 | 'isRequired' => false, |
|---|
| 256 | 'optionDesc' => NULL, |
|---|
| 257 | ), |
|---|
| 258 | ), |
|---|
| 259 | ), |
|---|
| 260 | ), |
|---|
| 261 | ), |
|---|
| 262 | ); |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | function test_type() |
|---|
| 266 | { |
|---|
| 267 | $this->assertEquals('array', gettype($this->json->decode($this->str1)), "loose type should be array"); |
|---|
| 268 | $this->assertEquals('array', gettype($this->json->decode($this->str2)), "loose type should be array"); |
|---|
| 269 | $this->assertEquals('array', gettype($this->json->decode($this->str3)), "loose type should be array"); |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | function test_from_JSON() |
|---|
| 273 | { |
|---|
| 274 | $this->assertEquals($this->arr1, $this->json->decode($this->str1), "simple compactly-nested array"); |
|---|
| 275 | $this->assertEquals($this->arr2, $this->json->decode($this->str2), "simple compactly-nested array"); |
|---|
| 276 | $this->assertEquals($this->arr3, $this->json->decode($this->str3), "complex compactly nested array"); |
|---|
| 277 | $this->assertEquals($this->arr4, $this->json->decode($this->str4), "complex compactly nested array"); |
|---|
| 278 | $this->assertEquals($this->arr5, $this->json->decode($this->str5), "super complex compactly nested array"); |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | function _test_from_JSON() |
|---|
| 282 | { |
|---|
| 283 | $super = '{"params":[{"options": {"old": {}, "new": {"0": {"elements": {"old": {}, "new": {"0": {"elementName": "aa", "isDefault": false, "elementRank": "0", "priceAdjust": "0", "partNumber": ""}}}, "optionName": "aa", "isRequired": false, "optionDesc": ""}}}}]}'; |
|---|
| 284 | print("trying {$super}...\n"); |
|---|
| 285 | print var_export($this->json->decode($super)); |
|---|
| 286 | } |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | class Services_JSON_Object_TestCase extends PHPUnit_TestCase { |
|---|
| 290 | |
|---|
| 291 | function Services_JSON_Object_TestCase($name) { |
|---|
| 292 | $this->PHPUnit_TestCase($name); |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | function setUp() { |
|---|
| 296 | $this->json_l = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); |
|---|
| 297 | $this->json_s = new Services_JSON(); |
|---|
| 298 | |
|---|
| 299 | $this->obj_j = '{"a_string":"\"he\":llo}:{world","an_array":[1,2,3],"obj":{"a_number":123}}'; |
|---|
| 300 | |
|---|
| 301 | $this->obj1->car1->color = 'tan'; |
|---|
| 302 | $this->obj1->car1->model = 'sedan'; |
|---|
| 303 | $this->obj1->car2->color = 'red'; |
|---|
| 304 | $this->obj1->car2->model = 'sports'; |
|---|
| 305 | $this->obj1_j = '{"car1":{"color":"tan","model":"sedan"},"car2":{"color":"red","model":"sports"}}'; |
|---|
| 306 | $this->obj1_d = 'Object with nested objects'; |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | function test_type() |
|---|
| 310 | { |
|---|
| 311 | $this->assertEquals('object', gettype($this->json_s->decode($this->obj_j)), "checking whether decoded type is object"); |
|---|
| 312 | $this->assertEquals('array', gettype($this->json_l->decode($this->obj_j)), "checking whether decoded type is array"); |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | function test_to_JSON() |
|---|
| 316 | { |
|---|
| 317 | $this->assertEquals($this->obj1_j, $this->json_s->encode($this->obj1), "object - strict: {$this->obj1_d}"); |
|---|
| 318 | $this->assertEquals($this->obj1_j, $this->json_l->encode($this->obj1), "object - loose: {$this->obj1_d}"); |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | function test_from_then_to_JSON() |
|---|
| 322 | { |
|---|
| 323 | $this->assertEquals($this->obj_j, $this->json_s->encode($this->json_s->decode($this->obj_j)), "object case"); |
|---|
| 324 | $this->assertEquals($this->obj_j, $this->json_l->encode($this->json_l->decode($this->obj_j)), "array case"); |
|---|
| 325 | } |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | class Services_JSON_Spaces_Comments_TestCase extends PHPUnit_TestCase { |
|---|
| 329 | |
|---|
| 330 | function Services_JSON_Spaces_Comments_TestCase($name) { |
|---|
| 331 | $this->PHPUnit_TestCase($name); |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | function setUp() { |
|---|
| 335 | $this->json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); |
|---|
| 336 | |
|---|
| 337 | $this->obj_j = '{"a_string":"\"he\":llo}:{world","an_array":[1,2,3],"obj":{"a_number":123}}'; |
|---|
| 338 | |
|---|
| 339 | $this->obj_js = '{"a_string": "\"he\":llo}:{world", |
|---|
| 340 | "an_array":[1, 2, 3], |
|---|
| 341 | "obj": {"a_number":123}}'; |
|---|
| 342 | |
|---|
| 343 | $this->obj_jc1 = '{"a_string": "\"he\":llo}:{world", |
|---|
| 344 | // here is a comment, hoorah |
|---|
| 345 | "an_array":[1, 2, 3], |
|---|
| 346 | "obj": {"a_number":123}}'; |
|---|
| 347 | |
|---|
| 348 | $this->obj_jc2 = '/* this here is the sneetch */ "the sneetch" |
|---|
| 349 | // this has been the sneetch.'; |
|---|
| 350 | |
|---|
| 351 | $this->obj_jc3 = '{"a_string": "\"he\":llo}:{world", |
|---|
| 352 | /* here is a comment, hoorah */ |
|---|
| 353 | "an_array":[1, 2, 3 /* and here is another */], |
|---|
| 354 | "obj": {"a_number":123}}'; |
|---|
| 355 | |
|---|
| 356 | $this->obj_jc4 = '{\'a_string\': "\"he\":llo}:{world", |
|---|
| 357 | /* here is a comment, hoorah */ |
|---|
| 358 | \'an_array\':[1, 2, 3 /* and here is another */], |
|---|
| 359 | "obj": {"a_number":123}}'; |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | function test_spaces() |
|---|
| 363 | { |
|---|
| 364 | $this->assertEquals($this->json->decode($this->obj_j), $this->json->decode($this->obj_js), "checking whether notation with spaces works"); |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | function test_comments() |
|---|
| 368 | { |
|---|
| 369 | $this->assertEquals($this->json->decode($this->obj_j), $this->json->decode($this->obj_jc1), "checking whether notation with single line comments works"); |
|---|
| 370 | $this->assertEquals('the sneetch', $this->json->decode($this->obj_jc2), "checking whether notation with multiline comments works"); |
|---|
| 371 | $this->assertEquals($this->json->decode($this->obj_j), $this->json->decode($this->obj_jc3), "checking whether notation with multiline comments works"); |
|---|
| 372 | $this->assertEquals($this->json->decode($this->obj_j), $this->json->decode($this->obj_jc4), "checking whether notation with single-quotes and multiline comments works"); |
|---|
| 373 | } |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | class Services_JSON_Empties_TestCase extends PHPUnit_TestCase { |
|---|
| 377 | |
|---|
| 378 | function Services_JSON_Empties_TestCase($name) { |
|---|
| 379 | $this->PHPUnit_TestCase($name); |
|---|
| 380 | } |
|---|
| 381 | |
|---|
| 382 | function setUp() { |
|---|
| 383 | $this->json_l = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); |
|---|
| 384 | $this->json_s = new Services_JSON(); |
|---|
| 385 | |
|---|
| 386 | $this->obj0_j = '{}'; |
|---|
| 387 | $this->arr0_j = '[]'; |
|---|
| 388 | |
|---|
| 389 | $this->obj1_j = '{ }'; |
|---|
| 390 | $this->arr1_j = '[ ]'; |
|---|
| 391 | |
|---|
| 392 | $this->obj2_j = '{ /* comment inside */ }'; |
|---|
| 393 | $this->arr2_j = '[ /* comment inside */ ]'; |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | function test_type() |
|---|
| 397 | { |
|---|
| 398 | $this->assertEquals('array', gettype($this->json_l->decode($this->arr0_j)), "should be array"); |
|---|
| 399 | $this->assertEquals('object', gettype($this->json_s->decode($this->obj0_j)), "should be object"); |
|---|
| 400 | |
|---|
| 401 | $this->assertEquals(0, count($this->json_l->decode($this->arr0_j)), "should be empty array"); |
|---|
| 402 | $this->assertEquals(0, count(get_object_vars($this->json_s->decode($this->obj0_j))), "should be empty object"); |
|---|
| 403 | |
|---|
| 404 | $this->assertEquals('array', gettype($this->json_l->decode($this->arr1_j)), "should be array, even with space"); |
|---|
| 405 | $this->assertEquals('object', gettype($this->json_s->decode($this->obj1_j)), "should be object, even with space"); |
|---|
| 406 | |
|---|
| 407 | $this->assertEquals(0, count($this->json_l->decode($this->arr1_j)), "should be empty array, even with space"); |
|---|
| 408 | $this->assertEquals(0, count(get_object_vars($this->json_s->decode($this->obj1_j))), "should be empty object, even with space"); |
|---|
| 409 | |
|---|
| 410 | $this->assertEquals('array', gettype($this->json_l->decode($this->arr2_j)), "should be array, despite comment"); |
|---|
| 411 | $this->assertEquals('object', gettype($this->json_s->decode($this->obj2_j)), "should be object, despite comment"); |
|---|
| 412 | |
|---|
| 413 | $this->assertEquals(0, count($this->json_l->decode($this->arr2_j)), "should be empty array, despite comment"); |
|---|
| 414 | $this->assertEquals(0, count(get_object_vars($this->json_s->decode($this->obj2_j))), "should be empty object, despite commentt"); |
|---|
| 415 | } |
|---|
| 416 | } |
|---|
| 417 | |
|---|
| 418 | class Services_JSON_UnquotedKeys_TestCase extends PHPUnit_TestCase { |
|---|
| 419 | |
|---|
| 420 | function Services_JSON_UnquotedKeys_TestCase($name) { |
|---|
| 421 | $this->PHPUnit_TestCase($name); |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | function setUp() { |
|---|
| 425 | $this->json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); |
|---|
| 426 | |
|---|
| 427 | $this->arn = array(0=> array(0=> 'tan', 'model' => 'sedan'), 1 => array(0 => 'red', 'model' => 'sports')); |
|---|
| 428 | $this->arn_ja = '[{0:"tan","model":"sedan"},{"0":"red",model:"sports"}]'; |
|---|
| 429 | $this->arn_d = 'associative array with unquoted keys, nested associative arrays, and some numeric keys thrown in'; |
|---|
| 430 | |
|---|
| 431 | $this->arrs = array (1 => 'one', 2 => 'two', 5 => 'fi"ve'); |
|---|
| 432 | $this->arrs_jo = '{"1":"one",2:"two","5":\'fi"ve\'}'; |
|---|
| 433 | $this->arrs_d = 'associative array with unquoted keys, single-quoted values, numeric keys which are not fully populated in a range of 0 to length-1'; |
|---|
| 434 | } |
|---|
| 435 | |
|---|
| 436 | function test_from_JSON() |
|---|
| 437 | { |
|---|
| 438 | // ...unless the input array has some numeric indeces, in which case the behavior is to degrade to a regular array |
|---|
| 439 | $this->assertEquals($this->arn, $this->json->decode($this->arn_ja), "array case - strict: {$this->arn_d}"); |
|---|
| 440 | |
|---|
| 441 | // Test a sparsely populated numerically indexed associative array |
|---|
| 442 | $this->assertEquals($this->arrs, $this->json->decode($this->arrs_jo), "sparse numeric assoc array: {$this->arrs_d}"); |
|---|
| 443 | } |
|---|
| 444 | } |
|---|
| 445 | |
|---|
| 446 | class Services_JSON_ErrorSuppression_TestCase extends PHPUnit_TestCase { |
|---|
| 447 | |
|---|
| 448 | function Services_JSON_ErrorSuppression_TestCase($name) { |
|---|
| 449 | $this->PHPUnit_TestCase($name); |
|---|
| 450 | } |
|---|
| 451 | |
|---|
| 452 | function setUp() { |
|---|
| 453 | $this->json = new Services_JSON(); |
|---|
| 454 | $this->json_ = new Services_JSON(SERVICES_JSON_SUPPRESS_ERRORS); |
|---|
| 455 | |
|---|
| 456 | $this->res = tmpfile(); |
|---|
| 457 | $this->res_j_ = 'null'; |
|---|
| 458 | $this->res_d = 'naked resource'; |
|---|
| 459 | |
|---|
| 460 | $this->arr = array('a', 1, tmpfile()); |
|---|
| 461 | $this->arr_j_ = '["a",1,null]'; |
|---|
| 462 | $this->arr_d = 'array with string, number and resource'; |
|---|
| 463 | |
|---|
| 464 | $obj = new stdClass(); |
|---|
| 465 | $obj->a_string = '"he":llo}:{world'; |
|---|
| 466 | $obj->an_array = array(1, 2, 3); |
|---|
| 467 | $obj->resource = tmpfile(); |
|---|
| 468 | |
|---|
| 469 | $this->obj = $obj; |
|---|
| 470 | $this->obj_j_ = '{"a_string":"\"he\":llo}:{world","an_array":[1,2,3],"resource":null}'; |
|---|
| 471 | $this->obj_d = 'object with properties, array, and nested resource'; |
|---|
| 472 | } |
|---|
| 473 | |
|---|
| 474 | function test_to_JSON() |
|---|
| 475 | { |
|---|
| 476 | $this->assertTrue(Services_JSON::isError($this->json->encode($this->res)), "resource case: {$this->res_d}"); |
|---|
| 477 | $this->assertTrue(Services_JSON::isError($this->json->encode($this->arr)), "array case: {$this->arr_d}"); |
|---|
| 478 | $this->assertTrue(Services_JSON::isError($this->json->encode($this->obj)), "object case: {$this->obj_d}"); |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | function test_to_JSON_suppressed() |
|---|
| 482 | { |
|---|
| 483 | $this->assertEquals($this->res_j_, $this->json_->encode($this->res), "resource case: {$this->res_d}"); |
|---|
| 484 | $this->assertEquals($this->arr_j_, $this->json_->encode($this->arr), "array case: {$this->arr_d}"); |
|---|
| 485 | $this->assertEquals($this->obj_j_, $this->json_->encode($this->obj), "object case: {$this->obj_d}"); |
|---|
| 486 | } |
|---|
| 487 | } |
|---|
| 488 | |
|---|
| 489 | $suite = new PHPUnit_TestSuite('Services_JSON_EncDec_TestCase'); |
|---|
| 490 | $result = PHPUnit::run($suite); |
|---|
| 491 | echo $result->toString(); |
|---|
| 492 | |
|---|
| 493 | $suite = new PHPUnit_TestSuite('Services_JSON_AssocArray_TestCase'); |
|---|
| 494 | $result = PHPUnit::run($suite); |
|---|
| 495 | echo $result->toString(); |
|---|
| 496 | |
|---|
| 497 | $suite = new PHPUnit_TestSuite('Services_JSON_NestedArray_TestCase'); |
|---|
| 498 | $result = PHPUnit::run($suite); |
|---|
| 499 | echo $result->toString(); |
|---|
| 500 | |
|---|
| 501 | $suite = new PHPUnit_TestSuite('Services_JSON_Object_TestCase'); |
|---|
| 502 | $result = PHPUnit::run($suite); |
|---|
| 503 | echo $result->toString(); |
|---|
| 504 | |
|---|
| 505 | $suite = new PHPUnit_TestSuite('Services_JSON_Spaces_Comments_TestCase'); |
|---|
| 506 | $result = PHPUnit::run($suite); |
|---|
| 507 | echo $result->toString(); |
|---|
| 508 | |
|---|
| 509 | $suite = new PHPUnit_TestSuite('Services_JSON_Empties_TestCase'); |
|---|
| 510 | $result = PHPUnit::run($suite); |
|---|
| 511 | echo $result->toString(); |
|---|
| 512 | |
|---|
| 513 | $suite = new PHPUnit_TestSuite('Services_JSON_UnquotedKeys_TestCase'); |
|---|
| 514 | $result = PHPUnit::run($suite); |
|---|
| 515 | echo $result->toString(); |
|---|
| 516 | |
|---|
| 517 | $suite = new PHPUnit_TestSuite('Services_JSON_ErrorSuppression_TestCase'); |
|---|
| 518 | $result = PHPUnit::run($suite); |
|---|
| 519 | echo $result->toString(); |
|---|
| 520 | |
|---|
| 521 | ?> |
|---|