fix #6: compatibility with 8.2
[awesomized/ext-ion] / tests / serialize / serializable.phpt
1 --TEST--
2 ion\serialize/serializable
3 --EXTENSIONS--
4 ion
5 --FILE--
6 TEST
7 <?php
8 class test implements Serializable {
9 protected $data;
10 function serialize() : string {
11 return "foobar";
12 }
13 function unserialize(string $data) : void {
14 $this->data = $data;
15 }
16 }
17
18 class recursive implements Serializable {
19 private $id;
20 function __construct(
21 protected $r
22 ) {
23 }
24 function serialize() : string {
25 if ($this->r) {
26 return "node:" . ion\serialize($this->r);
27 } else {
28 return "leaf";
29 }
30 }
31 function unserialize(string $data) : void {
32 if ($data === "leaf") {
33 $this->id = $data;
34 } else {
35 $this->id = substr($data, 0, 4);
36 $this->r = ion\unserialize(substr($data, 5));
37 }
38 }
39 }
40
41 class except implements Serializable {
42 function __construct (public $onserialize = false) {}
43 function serialize() : string {
44 if ($this->onserialize)
45 throw new Exception("except on serialize");
46 return "<<data>>";
47 }
48 function unserialize(string $data) : void {
49 throw new Exception("except on unserialize");
50 }
51 }
52
53 echo "\n";
54
55 $t = new test;
56 $s = ion\serialize([$t, $t]);
57 echo $s, "\n";
58 $c = ion\unserialize($s);
59 debug_zval_dump($c);
60 $tree = new recursive(new recursive(null));
61 var_dump($tree);
62 $s = ion\serialize($tree);
63 echo $s,"\n";
64 debug_zval_dump(ion\unserialize($s));
65
66 try {
67 ion\serialize(new except(true));
68 } catch (Exception $e) {
69 printf("caught %s: %s\n", get_class($e), $e->getMessage());
70 }
71 try {
72 ion\unserialize(ion\serialize(new except()));
73 } catch (Exception $e) {
74 printf("caught %s: %s\n", get_class($e), $e->getMessage());
75 }
76 ?>
77 DONE
78 --EXPECTF--
79 TEST
80
81 Deprecated: test implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %sserialize/serializable.php on line %d
82
83 Deprecated: recursive implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %sserialize/serializable.php on line %d
84
85 Deprecated: except implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %sserialize/serializable.php on line %d
86
87 [S::test::{{"foobar"}},r::1]
88 array(2) refcount(2){
89 [0]=>
90 object(test)#%d (1) refcount(2){
91 ["data":protected]=>
92 string(6) "foobar" refcount(1)
93 }
94 [1]=>
95 object(test)#%d (1) refcount(2){
96 ["data":protected]=>
97 string(6) "foobar" refcount(1)
98 }
99 }
100 object(recursive)#%d (2) {
101 ["id":"recursive":private]=>
102 NULL
103 ["r":protected]=>
104 object(recursive)#%d (2) {
105 ["id":"recursive":private]=>
106 NULL
107 ["r":protected]=>
108 NULL
109 }
110 }
111 S::recursive::{{"node:S::recursive::{{\"leaf\"}}"}}
112 object(recursive)#%d (2) refcount(1){
113 ["id":"recursive":private]=>
114 string(4) "node" refcount(1)
115 ["r":protected]=>
116 object(recursive)#%d (2) refcount(1){
117 ["id":"recursive":private]=>
118 string(4) "leaf" refcount(1)
119 ["r":protected]=>
120 NULL
121 }
122 }
123 caught Exception: except on serialize
124 caught Exception: except on unserialize
125 DONE