File: 031.phpt

package info (click to toggle)
php-msgpack 1%3A2.2.0~rc2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,200 kB
  • sloc: ansic: 3,817; xml: 416; makefile: 2
file content (102 lines) | stat: -rw-r--r-- 2,292 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
--TEST--
Object Serializable interface throws exceptions
--SKIPIF--
<?php
if (version_compare(PHP_VERSION, '5.1.0') < 0) {
    echo "skip tests in PHP 5.1 or newer";
}
--FILE--
<?php
if(!extension_loaded('msgpack')) {
    dl('msgpack.' . PHP_SHLIB_SUFFIX);
}

function test($variable) {
    $serialized = msgpack_serialize($variable);
    $unserialized = msgpack_unserialize($serialized);
    var_dump($unserialized);
}

class Obj implements Serializable {
    private static $count = 1;

    var $a;
    var $b;

    function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function serialize() {
        $c = self::$count++;
        echo "call serialize, ", ($this->a ? "throw" : "no throw"), PHP_EOL;
        if ($this->a) {
            throw new Exception("exception in serialize $c");
        }
        return pack('NN', $this->a, $this->b);
    }

    public function unserialize($serialized) {
        $tmp = unpack('N*', $serialized);
        $this->__construct($tmp[1], $tmp[2]);
        $c = self::$count++;
        echo "call unserialize, ", ($this->b ? "throw" : "no throw"), PHP_EOL;
        if ($this->b) {
            throw new Exception("exception in unserialize $c");
        }
    }

    public function __serialize() {
        return [$this->serialize()];
    }

    public function __unserialize($serialized) {
        return $this->unserialize($serialized[0]);
    }
}

$a = new Obj(1, 0);
$a = new Obj(0, 0);
$b = new Obj(0, 0);
$c = new Obj(1, 0);
$d = new Obj(0, 1);

echo "a, a, c", PHP_EOL;
try {
    test(array($a, $a, $c));
} catch (Exception $e) {
    if (version_compare(phpversion(), "5.3.0", ">=")) {
        if ($e->getPrevious()) {
            $e = $e->getPrevious();
        }
    }

    echo $e->getMessage(), PHP_EOL;
}

echo "b, b, d", PHP_EOL;

try {
    test(array($b, $b, $d));
} catch (Exception $e) {
    if (version_compare(phpversion(), "5.3.0", ">=")) {
        if ($e->getPrevious()) {
            $e = $e->getPrevious();
        }
    }

    echo $e->getMessage(), PHP_EOL;
}
?>
--EXPECT--
a, a, c
call serialize, no throw
call serialize, throw
exception in serialize 2
b, b, d
call serialize, no throw
call serialize, no throw
call unserialize, no throw
call unserialize, throw
exception in unserialize 6