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