xref: /php-src/ext/simplexml/tests/bug55098.phpt (revision b842ea4f)
1--TEST--
2Bug #55098 (SimpleXML iteration produces infinite loop)
3--EXTENSIONS--
4simplexml
5--FILE--
6<?php
7$xmlString = "<root><a><b>1</b><b>2</b><b>3</b></a></root>";
8$xml = simplexml_load_string($xmlString);
9
10$nodes = $xml->a->b;
11
12function test($nodes, $name, $callable) {
13    echo "--- $name ---\n";
14    foreach ($nodes as $nodeData) {
15        echo "nodeData: " . $nodeData . "\n";
16        $callable($nodes);
17    }
18}
19
20test($nodes, "asXml", fn ($n) => $n->asXml());
21test($nodes, "attributes", fn ($n) => $n->attributes());
22test($nodes, "children", fn ($n) => $n->children());
23test($nodes, "getNamespaces", fn ($n) => $n->getNamespaces());
24test($nodes, "xpath", fn ($n) => $n->xpath("/root/a/b"));
25test($nodes, "var_dump", fn ($n) => var_dump($n));
26test($nodes, "manipulation combined with querying", function ($n) {
27    $n->addAttribute("attr", "value");
28    (bool) $n["attr"];
29    $n->addChild("child", "value");
30    $n->outer[]->inner = "foo";
31    (bool) $n->outer;
32    (bool) $n;
33    isset($n->outer);
34    isset($n["attr"]);
35    unset($n->outer);
36    unset($n["attr"]);
37    unset($n->child);
38});
39?>
40--EXPECT--
41--- asXml ---
42nodeData: 1
43nodeData: 2
44nodeData: 3
45--- attributes ---
46nodeData: 1
47nodeData: 2
48nodeData: 3
49--- children ---
50nodeData: 1
51nodeData: 2
52nodeData: 3
53--- getNamespaces ---
54nodeData: 1
55nodeData: 2
56nodeData: 3
57--- xpath ---
58nodeData: 1
59nodeData: 2
60nodeData: 3
61--- var_dump ---
62nodeData: 1
63object(SimpleXMLElement)#3 (3) {
64  [0]=>
65  string(1) "1"
66  [1]=>
67  string(1) "2"
68  [2]=>
69  string(1) "3"
70}
71nodeData: 2
72object(SimpleXMLElement)#3 (3) {
73  [0]=>
74  string(1) "1"
75  [1]=>
76  string(1) "2"
77  [2]=>
78  string(1) "3"
79}
80nodeData: 3
81object(SimpleXMLElement)#3 (3) {
82  [0]=>
83  string(1) "1"
84  [1]=>
85  string(1) "2"
86  [2]=>
87  string(1) "3"
88}
89--- manipulation combined with querying ---
90nodeData: 1
91nodeData: 2
92nodeData: 3
93