xref: /PHP-8.2/ext/dom/tests/gh8996.phpt (revision 24e5e4ec)
1--TEST--
2GH-8996: DOMNode serialization on PHP ^8.1
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8echo "=== __sleep and __wakeup ===\n";
9
10class SerializableDomDocumentSleepWakeup extends DOMDocument
11{
12    private $xmlData;
13
14    public function __sleep(): array
15    {
16        $this->xmlData = $this->saveXML();
17        return ['xmlData'];
18    }
19
20    public function __wakeup(): void
21    {
22        $this->loadXML($this->xmlData);
23    }
24}
25
26$dom = new SerializableDomDocumentSleepWakeup('1.0', 'UTF-8');
27$dom->loadXML('<tag>value</tag>');
28
29$serialized = serialize($dom);
30var_dump($serialized);
31$unserialized = unserialize($serialized);
32
33echo "Serialized:\n-----------\n$serialized\n-----------\nRestored:\n-----------\n{$unserialized->saveXml()}";
34
35echo "=== __serialize and __unserialize ===\n";
36
37class SerializableDomDocument__Serialize__Unserialize extends DOMDocument
38{
39    public function __serialize(): array
40    {
41        return ['xmlData' => $this->saveXML()];
42    }
43
44    public function __unserialize(array $data): void
45    {
46        $this->loadXML($data['xmlData']);
47    }
48}
49
50$dom = new SerializableDomDocument__Serialize__Unserialize('1.0', 'UTF-8');
51$dom->loadXML('<tag>value</tag>');
52
53$serialized = serialize($dom);
54$unserialized = unserialize($serialized);
55
56echo "Serialized:\n-----------\n$serialized\n-----------\nRestored:\n-----------\n{$unserialized->saveXml()}";
57
58echo "=== serialize and unserialize ===\n";
59
60class SerializableDomDocumentSerializeUnserialize extends DOMDocument implements Serializable
61{
62    public function serialize(): ?string
63    {
64        return $this->saveXML();
65    }
66
67    public function unserialize(string $data): void
68    {
69        $this->loadXML($data);
70    }
71}
72
73$dom = new SerializableDomDocumentSerializeUnserialize('1.0', 'UTF-8');
74$dom->loadXML('<tag>value</tag>');
75
76$serialized = serialize($dom);
77$unserialized = unserialize($serialized);
78
79echo "Serialized:\n-----------\n$serialized\n-----------\nRestored:\n-----------\n{$unserialized->saveXml()}";
80
81?>
82--EXPECTF--
83=== __sleep and __wakeup ===
84string(144) "O:34:"SerializableDomDocumentSleepWakeup":1:{s:43:"%0SerializableDomDocumentSleepWakeup%0xmlData";s:39:"<?xml version="1.0"?>
85<tag>value</tag>
86";}"
87Serialized:
88-----------
89O:34:"SerializableDomDocumentSleepWakeup":1:{s:43:"%0SerializableDomDocumentSleepWakeup%0xmlData";s:39:"<?xml version="1.0"?>
90<tag>value</tag>
91";}
92-----------
93Restored:
94-----------
95<?xml version="1.0"?>
96<tag>value</tag>
97=== __serialize and __unserialize ===
98Serialized:
99-----------
100O:47:"SerializableDomDocument__Serialize__Unserialize":1:{s:7:"xmlData";s:39:"<?xml version="1.0"?>
101<tag>value</tag>
102";}
103-----------
104Restored:
105-----------
106<?xml version="1.0"?>
107<tag>value</tag>
108=== serialize and unserialize ===
109
110Deprecated: SerializableDomDocumentSerializeUnserialize implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %s on line %d
111Serialized:
112-----------
113C:43:"SerializableDomDocumentSerializeUnserialize":39:{<?xml version="1.0"?>
114<tag>value</tag>
115}
116-----------
117Restored:
118-----------
119<?xml version="1.0"?>
120<tag>value</tag>
121