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