JsonTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. namespace Sabre\VObject\Parser;
  3. use
  4. Sabre\VObject;
  5. class JsonTest extends \PHPUnit_Framework_TestCase {
  6. function testRoundTripJCard() {
  7. $input = array(
  8. "vcard",
  9. array(
  10. array(
  11. "version",
  12. new \StdClass(),
  13. "text",
  14. "4.0"
  15. ),
  16. array(
  17. "prodid",
  18. new \StdClass(),
  19. "text",
  20. "-//Sabre//Sabre VObject " . VObject\Version::VERSION . "//EN",
  21. ),
  22. array(
  23. "uid",
  24. new \StdClass(),
  25. "text",
  26. "foo",
  27. ),
  28. array(
  29. "bday",
  30. new \StdClass(),
  31. "date-and-or-time",
  32. "1985-04-07",
  33. ),
  34. array(
  35. "rev",
  36. new \StdClass(),
  37. "timestamp",
  38. "1995-10-31T22:27:10Z",
  39. ),
  40. array(
  41. "lang",
  42. new \StdClass(),
  43. "language-tag",
  44. "nl",
  45. ),
  46. array(
  47. "n",
  48. new \StdClass(),
  49. "text",
  50. array("Last", "First", "Middle", "", ""),
  51. ),
  52. array(
  53. "tel",
  54. (object)array(
  55. "group" => "item1",
  56. ),
  57. "text",
  58. "+1 555 123456",
  59. ),
  60. array(
  61. "x-ab-label",
  62. (object)array(
  63. "group" => "item1",
  64. ),
  65. "unknown",
  66. "Walkie Talkie",
  67. ),
  68. array(
  69. "adr",
  70. new \StdClass(),
  71. "text",
  72. array(
  73. "",
  74. "",
  75. array("My Street", "Left Side", "Second Shack"),
  76. "Hometown",
  77. "PA",
  78. "18252",
  79. "U.S.A",
  80. ),
  81. ),
  82. array(
  83. "bday",
  84. (object)array(
  85. 'x-param' => array(1,2),
  86. ),
  87. "date",
  88. "1979-12-25",
  89. ),
  90. array(
  91. "bday",
  92. new \StdClass(),
  93. "date-time",
  94. "1979-12-25T02:00:00",
  95. ),
  96. array(
  97. "x-truncated",
  98. new \StdClass(),
  99. "date",
  100. "--12-25",
  101. ),
  102. array(
  103. "x-time-local",
  104. new \StdClass(),
  105. "time",
  106. "12:30:00"
  107. ),
  108. array(
  109. "x-time-utc",
  110. new \StdClass(),
  111. "time",
  112. "12:30:00Z"
  113. ),
  114. array(
  115. "x-time-offset",
  116. new \StdClass(),
  117. "time",
  118. "12:30:00-08:00"
  119. ),
  120. array(
  121. "x-time-reduced",
  122. new \StdClass(),
  123. "time",
  124. "23"
  125. ),
  126. array(
  127. "x-time-truncated",
  128. new \StdClass(),
  129. "time",
  130. "--30"
  131. ),
  132. array(
  133. "x-karma-points",
  134. new \StdClass(),
  135. "integer",
  136. 42
  137. ),
  138. array(
  139. "x-grade",
  140. new \StdClass(),
  141. "float",
  142. 1.3
  143. ),
  144. array(
  145. "tz",
  146. new \StdClass(),
  147. "utc-offset",
  148. "-05:00",
  149. ),
  150. ),
  151. );
  152. $parser = new Json(json_encode($input));
  153. $vobj = $parser->parse();
  154. $version = VObject\Version::VERSION;
  155. $result = $vobj->serialize();
  156. $expected = <<<VCF
  157. BEGIN:VCARD
  158. VERSION:4.0
  159. PRODID:-//Sabre//Sabre VObject $version//EN
  160. UID:foo
  161. BDAY:1985-04-07
  162. REV:1995-10-31T22:27:10Z
  163. LANG:nl
  164. N:Last;First;Middle;;
  165. item1.TEL:+1 555 123456
  166. item1.X-AB-LABEL:Walkie Talkie
  167. ADR:;;My Street,Left Side,Second Shack;Hometown;PA;18252;U.S.A
  168. BDAY;X-PARAM=1,2;VALUE=DATE:1979-12-25
  169. BDAY;VALUE=DATE-TIME:1979-12-25T02:00:00
  170. X-TRUNCATED;VALUE=DATE:--12-25
  171. X-TIME-LOCAL;VALUE=TIME:12:30:00
  172. X-TIME-UTC;VALUE=TIME:12:30:00Z
  173. X-TIME-OFFSET;VALUE=TIME:12:30:00-08:00
  174. X-TIME-REDUCED;VALUE=TIME:23
  175. X-TIME-TRUNCATED;VALUE=TIME:--30
  176. X-KARMA-POINTS;VALUE=INTEGER:42
  177. X-GRADE;VALUE=FLOAT:1.3
  178. TZ;VALUE=UTC-OFFSET:-05:00
  179. END:VCARD
  180. VCF;
  181. $this->assertEquals($expected, str_replace("\r", "", $result));
  182. $this->assertEquals(
  183. $input,
  184. $vobj->jsonSerialize()
  185. );
  186. }
  187. function testRoundTripJCal() {
  188. $input = array(
  189. "vcalendar",
  190. array(
  191. array(
  192. "version",
  193. new \StdClass(),
  194. "text",
  195. "2.0"
  196. ),
  197. array(
  198. "prodid",
  199. new \StdClass(),
  200. "text",
  201. "-//Sabre//Sabre VObject " . VObject\Version::VERSION . "//EN",
  202. ),
  203. array(
  204. "calscale",
  205. new \StdClass(),
  206. "text",
  207. "GREGORIAN"
  208. ),
  209. ),
  210. array(
  211. array("vevent",
  212. array(
  213. array(
  214. "uid", new \StdClass(), "text", "foo",
  215. ),
  216. array(
  217. "dtstart", new \StdClass(), "date", "2013-05-26",
  218. ),
  219. array(
  220. "duration", new \StdClass(), "duration", "P1D",
  221. ),
  222. array(
  223. "categories", new \StdClass(), "text", "home", "testing",
  224. ),
  225. array(
  226. "created", new \StdClass(), "date-time", "2013-05-26T18:10:00Z",
  227. ),
  228. array(
  229. "attach", new \StdClass(), "binary", base64_encode('attachment')
  230. ),
  231. array(
  232. "attendee", new \StdClass(), "cal-address", "mailto:armin@example.org",
  233. ),
  234. array(
  235. "geo", new \StdClass(), "float", array(51.96668, 7.61876),
  236. ),
  237. array(
  238. "sequence", new \StdClass(), "integer", 5
  239. ),
  240. array(
  241. "freebusy", new \StdClass(), "period", array("2013-05-26T21:02:13", "PT1H"), array("2013-06-26T12:00:00", "2013-06-26T13:00:00"),
  242. ),
  243. array(
  244. "url", new \StdClass(), "uri", "http://example.org/",
  245. ),
  246. array(
  247. "tzoffsetfrom", new \StdClass(), "utc-offset", "+05:00",
  248. ),
  249. array(
  250. "rrule", new \StdClass(), "recur", array(
  251. 'freq' => 'WEEKLY',
  252. 'byday' => array('MO', 'TU'),
  253. ),
  254. ),
  255. array(
  256. "x-bool", new \StdClass(), "boolean", true
  257. ),
  258. array(
  259. "x-time", new \StdClass(), "time", "08:00:00",
  260. ),
  261. array(
  262. "attendee",
  263. (object)array(
  264. "cn" => "Dominik",
  265. "partstat" => "DECLINED",
  266. ),
  267. "cal-address",
  268. "mailto:dominik@example.org"
  269. ),
  270. array(
  271. "request-status",
  272. new \StdClass(),
  273. "text",
  274. array("2.0", "Success"),
  275. ),
  276. array(
  277. "request-status",
  278. new \StdClass(),
  279. "text",
  280. array("3.7", "Invalid Calendar User", "ATTENDEE:mailto:jsmith@example.org"),
  281. ),
  282. ),
  283. array(
  284. array("valarm",
  285. array(
  286. array(
  287. "action", new \StdClass(), "text", "DISPLAY",
  288. ),
  289. ),
  290. array(),
  291. ),
  292. ),
  293. )
  294. ),
  295. );
  296. $parser = new Json(json_encode($input));
  297. $vobj = $parser->parse();
  298. $result = $vobj->serialize();
  299. $version = VObject\Version::VERSION;
  300. $expected = <<<VCF
  301. BEGIN:VCALENDAR
  302. VERSION:2.0
  303. PRODID:-//Sabre//Sabre VObject $version//EN
  304. CALSCALE:GREGORIAN
  305. BEGIN:VEVENT
  306. UID:foo
  307. DTSTART;VALUE=DATE:20130526
  308. DURATION:P1D
  309. CATEGORIES:home,testing
  310. CREATED:20130526T181000Z
  311. ATTACH;VALUE=BINARY:YXR0YWNobWVudA==
  312. ATTENDEE:mailto:armin@example.org
  313. GEO:51.96668;7.61876
  314. SEQUENCE:5
  315. FREEBUSY:20130526T210213/PT1H,20130626T120000/20130626T130000
  316. URL:http://example.org/
  317. TZOFFSETFROM:+05:00
  318. RRULE:FREQ=WEEKLY;BYDAY=MO,TU
  319. X-BOOL;VALUE=BOOLEAN:TRUE
  320. X-TIME;VALUE=TIME:08:00:00
  321. ATTENDEE;CN=Dominik;PARTSTAT=DECLINED:mailto:dominik@example.org
  322. REQUEST-STATUS:2.0;Success
  323. REQUEST-STATUS:3.7;Invalid Calendar User;ATTENDEE:mailto:jsmith@example.org
  324. BEGIN:VALARM
  325. ACTION:DISPLAY
  326. END:VALARM
  327. END:VEVENT
  328. END:VCALENDAR
  329. VCF;
  330. $this->assertEquals($expected, str_replace("\r", "", $result));
  331. $this->assertEquals(
  332. $input,
  333. $vobj->jsonSerialize()
  334. );
  335. }
  336. function testParseStreamArg() {
  337. $input = array(
  338. "vcard",
  339. array(
  340. array(
  341. "FN", new \StdClass(), 'text', "foo",
  342. ),
  343. ),
  344. );
  345. $stream = fopen('php://memory','r+');
  346. fwrite($stream, json_encode($input));
  347. rewind($stream);
  348. $result = VObject\Reader::readJson($stream,0);
  349. $this->assertEquals('foo', $result->FN->getValue());
  350. }
  351. /**
  352. * @expectedException \Sabre\VObject\ParseException
  353. */
  354. function testParseInvalidData() {
  355. $json = new Json();
  356. $input = array(
  357. "vlist",
  358. array(
  359. array(
  360. "FN", new \StdClass(), 'text', "foo",
  361. ),
  362. ),
  363. );
  364. $json->parse(json_encode($input), 0);
  365. }
  366. }