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