1--TEST-- 2Object Serializable interface throws exceptions 3--SKIPIF-- 4<?php 5if (version_compare(PHP_VERSION, '5.1.0') < 0) { 6 echo "skip tests in PHP 5.1 or newer"; 7} 8--FILE-- 9<?php 10if(!extension_loaded('msgpack')) { 11 dl('msgpack.' . PHP_SHLIB_SUFFIX); 12} 13 14function test($variable) { 15 $serialized = msgpack_serialize($variable); 16 $unserialized = msgpack_unserialize($serialized); 17 var_dump($unserialized); 18} 19 20class Obj implements Serializable { 21 private static $count = 1; 22 23 var $a; 24 var $b; 25 26 function __construct($a, $b) { 27 $this->a = $a; 28 $this->b = $b; 29 } 30 31 public function serialize() { 32 $c = self::$count++; 33 echo "call serialize, ", ($this->a ? "throw" : "no throw"), PHP_EOL; 34 if ($this->a) { 35 throw new Exception("exception in serialize $c"); 36 } 37 return pack('NN', $this->a, $this->b); 38 } 39 40 public function unserialize($serialized) { 41 $tmp = unpack('N*', $serialized); 42 $this->__construct($tmp[1], $tmp[2]); 43 $c = self::$count++; 44 echo "call unserialize, ", ($this->b ? "throw" : "no throw"), PHP_EOL; 45 if ($this->b) { 46 throw new Exception("exception in unserialize $c"); 47 } 48 } 49} 50 51$a = new Obj(1, 0); 52$a = new Obj(0, 0); 53$b = new Obj(0, 0); 54$c = new Obj(1, 0); 55$d = new Obj(0, 1); 56 57echo "a, a, c", PHP_EOL; 58try { 59 test(array($a, $a, $c)); 60} catch (Exception $e) { 61 if (version_compare(phpversion(), "5.3.0", ">=")) { 62 if ($e->getPrevious()) { 63 $e = $e->getPrevious(); 64 } 65 } 66 67 echo $e->getMessage(), PHP_EOL; 68} 69 70echo "b, b, d", PHP_EOL; 71 72try { 73 test(array($b, $b, $d)); 74} catch (Exception $e) { 75 if (version_compare(phpversion(), "5.3.0", ">=")) { 76 if ($e->getPrevious()) { 77 $e = $e->getPrevious(); 78 } 79 } 80 81 echo $e->getMessage(), PHP_EOL; 82} 83?> 84--EXPECT-- 85a, a, c 86call serialize, no throw 87call serialize, throw 88exception in serialize 2 89b, b, d 90call serialize, no throw 91call serialize, no throw 92call unserialize, no throw 93call unserialize, throw 94exception in unserialize 6 95