xref: /php-src/ext/simplexml/tests/sxe_004.phpt (revision 37a3c9bc)
1--TEST--
2SPL: SimpleXMLIterator and overridden iterator methods()
3--EXTENSIONS--
4simplexml
5libxml
6--FILE--
7<?php
8
9$xml =<<<EOF
10<?xml version='1.0'?>
11<!DOCTYPE sxe SYSTEM "notfound.dtd">
12<sxe id="elem1">
13 Plain text.
14 <elem1 attr1='first'>
15  Bla bla 1.
16  <!-- comment -->
17  <elem2>
18   Here we have some text data.
19   <elem3>
20    And here some more.
21    <elem4>
22     Wow once again.
23    </elem4>
24   </elem3>
25  </elem2>
26 </elem1>
27 <elem11 attr2='second'>
28  Bla bla 2.
29  <elem111>
30   Foo Bar
31  </elem111>
32 </elem11>
33</sxe>
34EOF;
35
36class SXETest extends SimpleXMLIterator
37{
38    function rewind(): void
39    {
40        echo __METHOD__ . "\n";
41        parent::rewind();
42    }
43    function valid(): bool
44    {
45        echo __METHOD__ . "\n";
46        return parent::valid();
47    }
48    function current(): SimpleXMLElement
49    {
50        echo __METHOD__ . "\n";
51        return parent::current();
52    }
53    function key(): string
54    {
55        echo __METHOD__ . "\n";
56        return parent::key();
57    }
58    function next(): void
59    {
60        echo __METHOD__ . "\n";
61        parent::next();
62    }
63    function hasChildren(): bool
64    {
65        echo __METHOD__ . "\n";
66        return parent::hasChildren();
67    }
68    function getChildren(): ?SimpleXMLIterator
69    {
70        echo __METHOD__ . "\n";
71        return parent::getChildren();
72    }
73}
74
75$sxe = new SXETest($xml);
76$rit = new RecursiveIteratorIterator($sxe, RecursiveIteratorIterator::SELF_FIRST);
77
78foreach($rit as $data) {
79    var_dump(get_class($data));
80    var_dump(trim($data));
81}
82
83?>
84--EXPECT--
85SXETest::rewind
86SXETest::valid
87SXETest::hasChildren
88SXETest::valid
89SXETest::current
90string(7) "SXETest"
91string(10) "Bla bla 1."
92SXETest::getChildren
93SXETest::rewind
94SXETest::valid
95SXETest::hasChildren
96SXETest::valid
97SXETest::current
98string(7) "SXETest"
99string(28) "Here we have some text data."
100SXETest::getChildren
101SXETest::rewind
102SXETest::valid
103SXETest::hasChildren
104SXETest::valid
105SXETest::current
106string(7) "SXETest"
107string(19) "And here some more."
108SXETest::getChildren
109SXETest::rewind
110SXETest::valid
111SXETest::hasChildren
112SXETest::valid
113SXETest::current
114string(7) "SXETest"
115string(15) "Wow once again."
116SXETest::next
117SXETest::valid
118SXETest::next
119SXETest::valid
120SXETest::next
121SXETest::valid
122SXETest::next
123SXETest::valid
124SXETest::hasChildren
125SXETest::valid
126SXETest::current
127string(7) "SXETest"
128string(10) "Bla bla 2."
129SXETest::getChildren
130SXETest::rewind
131SXETest::valid
132SXETest::hasChildren
133SXETest::valid
134SXETest::current
135string(7) "SXETest"
136string(7) "Foo Bar"
137SXETest::next
138SXETest::valid
139SXETest::next
140SXETest::valid
141SXETest::valid
142