PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/libraries/3rdparty/JSON/Test-JSON.php

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