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