File: __serialize_021.phpt

package info (click to toggle)
php-igbinary 3.2.16-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,024 kB
  • sloc: ansic: 3,373; xml: 1,106; pascal: 10; makefile: 2; php: 2; sh: 1
file content (49 lines) | stat: -rw-r--r-- 1,048 bytes parent folder | download | duplicates (3)
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
--TEST--
__serialize() mechanism (021): Temporary references in serialized arrays should be properly deduplicated
--FILE--
<?php

// This test will also pass before php 7.4 - it will just not call __serialize in older php versions.
// (not a good example, the formats are incompatible)
class Test {
    /** @var int */
    public $i;

    public function __construct(int $i) {
        $this->i = $i;
    }

    public function __serialize() {
        $j = $this->i + 0x7700;
        return [&$j, &$j];
    }

    public function __unserialize(array $data) {
        list($this->i) = $data;
    }
}

$values = [];
for ($i = 0; $i < 256; $i++) {
    $values[] = new Test($i);
}
$ser = igbinary_serialize($values);
$result = igbinary_unserialize($ser);

printf("Count=%d\n", count($values));
$j = 0;
foreach ($values as $i => $v) {
    if ($i !== $j) {
        echo "Unexpected key $i, expected $j\n";
    }
    if ($v->i !== $j) {
        echo "Unexpected value {$v->i}, expected $j\n";
    }
    $j++;
}
echo "Done\n";

?>
--EXPECT--
Count=256
Done