1--TEST-- 2unpack of object converter : class unpacker (object: php_only=0) 3--SKIPIF-- 4<?php 5if (version_compare(PHP_VERSION, '5.2.0') < 0) { 6 echo "skip tests in PHP 5.2 or newer"; 7} 8--FILE-- 9<?php 10if(!extension_loaded('msgpack')) 11{ 12 dl('msgpack.' . PHP_SHLIB_SUFFIX); 13} 14 15error_reporting(0); 16ini_set('msgpack.php_only', 0); 17 18function test($type, $variable, $object, $result = null) 19{ 20 $msgpack = new MessagePack(); 21 22 $serialized = $msgpack->pack($variable); 23 24 $unserialized = null; 25 $unpacker = $msgpack->unpacker(); 26 if ($unpacker->execute($serialized)) 27 { 28 $unserialized = $unpacker->data($object); 29 } 30 31 var_dump($unserialized); 32 if ($result) 33 { 34 echo $unserialized == $result ? 'OK' : 'ERROR', PHP_EOL; 35 } 36 else 37 { 38 echo 'SKIP', PHP_EOL; 39 } 40} 41 42class Obj 43{ 44 public $a; 45 protected $b; 46 private $c; 47 48 public function __construct($a = null, $b = null, $c = null, $d = null) 49 { 50 $this->a = $a; 51 $this->b = $b; 52 $this->c = $c; 53 if (is_array($d)) 54 { 55 foreach ($d as $key => $val) 56 { 57 $this->{$key} = $val; 58 } 59 } 60 } 61} 62 63$object = new Obj(); 64 65test('null', null, new Obj(), new Obj(null, null, null)); 66 67test('bool: true', true, new Obj(), new Obj(true, null, null)); 68test('bool: false', false, new Obj(), new Obj(false, null, null)); 69 70test('zero: 0', 0, new Obj(), new Obj(0, null, null)); 71test('small: 1', 1, new Obj(), new Obj(1, null, null)); 72test('small: -1', -1, new Obj(), new Obj(-1, null, null)); 73test('medium: 1000', 1000, new Obj(), new Obj(1000, null, null)); 74test('medium: -1000', -1000, new Obj(), new Obj(-1000, null, null)); 75test('large: 100000', 100000, new Obj(), new Obj(100000, null, null)); 76test('large: -100000', -100000, new Obj(), new Obj(-100000, null, null)); 77 78test('double: 123.456', 123.456, new Obj(), new Obj(123.456, null, null)); 79 80test('empty: ""', "", new Obj(), new Obj("", null, null)); 81test('string: "foobar"', "foobar", new Obj(), new Obj("foobar", null, null)); 82 83test('array: empty', array(), new Obj(), new Obj(null, null, null)); 84test('array(1, 2, 3)', array(1, 2, 3), new Obj(), new Obj(1, 2, 3)); 85test('array(array(1, 2, 3), arr...', array(array(1, 2, 3), array(4, 5, 6), array(7, 8, 9)), new Obj(), new Obj(array(1, 2, 3), array(4, 5, 6), array(7, 8, 9))); 86test('array(1, 2, 3, 4)', array(1, 2, 3, 4), new Obj()); 87 88test('array("foo", "foobar", "foohoge")', array("foo", "foobar", "hoge"), new Obj(), new Obj("foo", "foobar", "hoge")); 89test('array("a" => 1, "b" => 2))', array("a" => 1, "b" => 2), new Obj(), new Obj(1, 2, null)); 90test('array("one" => 1, "two" => 2))', array("one" => 1, "two" => 2), new Obj(), new Obj(null, null, null, array("one" => 1, "two" => 2))); 91 92test('array("a" => 1, "b" => 2, 3))', array("a" => 1, "b" => 2, 3), new Obj(), new Obj(1, 2, 3)); 93test('array(3, "a" => 1, "b" => 2))', array(3, "a" => 1, "b" => 2), new Obj(), new Obj(1, 2, 3)); 94test('array("a" => 1, 3, "b" => 2))', array("a" => 1, 3, "b" => 2), new Obj(), new Obj(1, 2, 3)); 95 96$a = array('foo'); 97test('array($a, $a)', array($a, $a), new Obj(), new Obj($a, $a, null)); 98 99$a = array( 100 'a' => array( 101 'b' => 'c', 102 'd' => 'e' 103 ), 104 'f' => array( 105 'g' => 'h' 106 ) 107 ); 108test('array', $a, new Obj(), new Obj(null, null, null, $a)); 109 110$o = new Obj(1, 2, 3); 111test('object', $o, new Obj(), new Obj(1, 2, 3)); 112 113class Obj2 { 114 public $A; 115 protected $B; 116 private $C; 117 118 function __construct($a, $b, $c) { 119 $this->A = $a; 120 $this->B = $b; 121 $this->C = $c; 122 } 123} 124 125$o = new Obj2(1, 2, 3); 126test('object', $o, new Obj(), new Obj(1, 2, 3)); 127 128$o1 = new Obj2(1, 2, 3); 129$o2 = new Obj2(4, 5, 6); 130test('object', array($o1, $o2), new Obj(), new Obj(array(1, 2, 3), array(4, 5, 6))); 131 132--EXPECTF-- 133object(Obj)#%d (3) { 134 ["a"]=> 135 NULL 136 [%r"?b"?:protected"?%r]=> 137 NULL 138 [%r"?c"?:("Obj":)?private"?%r]=> 139 NULL 140} 141OK 142object(Obj)#%d (3) { 143 ["a"]=> 144 bool(true) 145 [%r"?b"?:protected"?%r]=> 146 NULL 147 [%r"?c"?:("Obj":)?private"?%r]=> 148 NULL 149} 150OK 151object(Obj)#%d (3) { 152 ["a"]=> 153 bool(false) 154 [%r"?b"?:protected"?%r]=> 155 NULL 156 [%r"?c"?:("Obj":)?private"?%r]=> 157 NULL 158} 159OK 160object(Obj)#%d (3) { 161 ["a"]=> 162 int(0) 163 [%r"?b"?:protected"?%r]=> 164 NULL 165 [%r"?c"?:("Obj":)?private"?%r]=> 166 NULL 167} 168OK 169object(Obj)#%d (3) { 170 ["a"]=> 171 int(1) 172 [%r"?b"?:protected"?%r]=> 173 NULL 174 [%r"?c"?:("Obj":)?private"?%r]=> 175 NULL 176} 177OK 178object(Obj)#%d (3) { 179 ["a"]=> 180 int(-1) 181 [%r"?b"?:protected"?%r]=> 182 NULL 183 [%r"?c"?:("Obj":)?private"?%r]=> 184 NULL 185} 186OK 187object(Obj)#%d (3) { 188 ["a"]=> 189 int(1000) 190 [%r"?b"?:protected"?%r]=> 191 NULL 192 [%r"?c"?:("Obj":)?private"?%r]=> 193 NULL 194} 195OK 196object(Obj)#%d (3) { 197 ["a"]=> 198 int(-1000) 199 [%r"?b"?:protected"?%r]=> 200 NULL 201 [%r"?c"?:("Obj":)?private"?%r]=> 202 NULL 203} 204OK 205object(Obj)#%d (3) { 206 ["a"]=> 207 int(100000) 208 [%r"?b"?:protected"?%r]=> 209 NULL 210 [%r"?c"?:("Obj":)?private"?%r]=> 211 NULL 212} 213OK 214object(Obj)#%d (3) { 215 ["a"]=> 216 int(-100000) 217 [%r"?b"?:protected"?%r]=> 218 NULL 219 [%r"?c"?:("Obj":)?private"?%r]=> 220 NULL 221} 222OK 223object(Obj)#%d (3) { 224 ["a"]=> 225 float(123.456) 226 [%r"?b"?:protected"?%r]=> 227 NULL 228 [%r"?c"?:("Obj":)?private"?%r]=> 229 NULL 230} 231OK 232object(Obj)#%d (3) { 233 ["a"]=> 234 string(0) "" 235 [%r"?b"?:protected"?%r]=> 236 NULL 237 [%r"?c"?:("Obj":)?private"?%r]=> 238 NULL 239} 240OK 241object(Obj)#%d (3) { 242 ["a"]=> 243 string(6) "foobar" 244 [%r"?b"?:protected"?%r]=> 245 NULL 246 [%r"?c"?:("Obj":)?private"?%r]=> 247 NULL 248} 249OK 250object(Obj)#%d (3) { 251 ["a"]=> 252 NULL 253 [%r"?b"?:protected"?%r]=> 254 NULL 255 [%r"?c"?:("Obj":)?private"?%r]=> 256 NULL 257} 258OK 259object(Obj)#%d (3) { 260 ["a"]=> 261 int(1) 262 [%r"?b"?:protected"?%r]=> 263 int(2) 264 [%r"?c"?:("Obj":)?private"?%r]=> 265 int(3) 266} 267OK 268object(Obj)#%d (3) { 269 ["a"]=> 270 array(3) { 271 [0]=> 272 int(1) 273 [1]=> 274 int(2) 275 [2]=> 276 int(3) 277 } 278 [%r"?b"?:protected"?%r]=> 279 array(3) { 280 [0]=> 281 int(4) 282 [1]=> 283 int(5) 284 [2]=> 285 int(6) 286 } 287 [%r"?c"?:("Obj":)?private"?%r]=> 288 array(3) { 289 [0]=> 290 int(7) 291 [1]=> 292 int(8) 293 [2]=> 294 int(9) 295 } 296} 297OK 298object(Obj)#%d (4) { 299 ["a"]=> 300 int(1) 301 [%r"?b"?:protected"?%r]=> 302 int(2) 303 [%r"?c"?:("Obj":)?private"?%r]=> 304 int(3) 305 ["3"]=> 306 int(4) 307} 308SKIP 309object(Obj)#%d (3) { 310 ["a"]=> 311 string(3) "foo" 312 [%r"?b"?:protected"?%r]=> 313 string(6) "foobar" 314 [%r"?c"?:("Obj":)?private"?%r]=> 315 string(4) "hoge" 316} 317OK 318object(Obj)#%d (3) { 319 ["a"]=> 320 int(1) 321 [%r"?b"?:protected"?%r]=> 322 int(2) 323 [%r"?c"?:("Obj":)?private"?%r]=> 324 NULL 325} 326OK 327object(Obj)#%d (5) { 328 ["a"]=> 329 NULL 330 [%r"?b"?:protected"?%r]=> 331 NULL 332 [%r"?c"?:("Obj":)?private"?%r]=> 333 NULL 334 ["one"]=> 335 int(1) 336 ["two"]=> 337 int(2) 338} 339OK 340object(Obj)#%d (3) { 341 ["a"]=> 342 int(1) 343 [%r"?b"?:protected"?%r]=> 344 int(2) 345 [%r"?c"?:("Obj":)?private"?%r]=> 346 int(3) 347} 348OK 349object(Obj)#%d (3) { 350 ["a"]=> 351 int(1) 352 [%r"?b"?:protected"?%r]=> 353 int(2) 354 [%r"?c"?:("Obj":)?private"?%r]=> 355 int(3) 356} 357OK 358object(Obj)#%d (3) { 359 ["a"]=> 360 int(1) 361 [%r"?b"?:protected"?%r]=> 362 int(2) 363 [%r"?c"?:("Obj":)?private"?%r]=> 364 int(3) 365} 366OK 367object(Obj)#%d (3) { 368 ["a"]=> 369 array(1) { 370 [0]=> 371 string(3) "foo" 372 } 373 [%r"?b"?:protected"?%r]=> 374 array(1) { 375 [0]=> 376 string(3) "foo" 377 } 378 [%r"?c"?:("Obj":)?private"?%r]=> 379 NULL 380} 381OK 382object(Obj)#%d (4) { 383 ["a"]=> 384 array(2) { 385 ["b"]=> 386 string(1) "c" 387 ["d"]=> 388 string(1) "e" 389 } 390 [%r"?b"?:protected"?%r]=> 391 NULL 392 [%r"?c"?:("Obj":)?private"?%r]=> 393 NULL 394 ["f"]=> 395 array(1) { 396 ["g"]=> 397 string(1) "h" 398 } 399} 400OK 401object(Obj)#%d (3) { 402 ["a"]=> 403 int(1) 404 [%r"?b"?:protected"?%r]=> 405 int(2) 406 [%r"?c"?:("Obj":)?private"?%r]=> 407 int(3) 408} 409OK 410object(Obj)#%d (3) { 411 ["a"]=> 412 int(1) 413 [%r"?b"?:protected"?%r]=> 414 int(2) 415 [%r"?c"?:("Obj":)?private"?%r]=> 416 int(3) 417} 418OK 419object(Obj)#%d (3) { 420 ["a"]=> 421 array(3) { 422 [0]=> 423 int(1) 424 [1]=> 425 int(2) 426 [2]=> 427 int(3) 428 } 429 [%r"?b"?:protected"?%r]=> 430 array(3) { 431 [0]=> 432 int(4) 433 [1]=> 434 int(5) 435 [2]=> 436 int(6) 437 } 438 [%r"?c"?:("Obj":)?private"?%r]=> 439 NULL 440} 441OK 442