xref: /PHP-8.2/ext/spl/tests/iterator_042.phpt (revision 960318ed)
1--TEST--
2SPL: AppendIterator and its ArrayIterator
3--FILE--
4<?php
5
6function test_error_handler($errno, $msg, $filename, $linenum)
7{
8    echo "Error $msg in $filename on line $linenum\n";
9    return true;
10}
11
12set_error_handler('test_error_handler');
13
14$it = new AppendIterator;
15
16try {
17    $it->append(array());
18} catch (Error $e) {
19    test_error_handler($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
20}
21$it->append(new ArrayIterator(array(1)));
22$it->append(new ArrayIterator(array(21, 22)));
23
24var_dump($it->getArrayIterator());
25
26$it->append(new ArrayIterator(array(31, 32, 33)));
27
28var_dump($it->getArrayIterator());
29
30$idx = 0;
31
32foreach($it as $k => $v)
33{
34    echo '===' . $idx++ . "===\n";
35    var_dump($it->getIteratorIndex());
36    var_dump($k);
37    var_dump($v);
38}
39
40?>
41--EXPECTF--
42Error AppendIterator::append(): Argument #1 ($iterator) must be of type Iterator, array given in %s on line %d
43object(ArrayIterator)#%d (1) {
44  %s"storage"%s"ArrayIterator":private]=>
45  array(2) {
46    [0]=>
47    object(ArrayIterator)#%d (1) {
48      %s"storage"%s"ArrayIterator":private]=>
49      array(1) {
50        [0]=>
51        int(1)
52      }
53    }
54    [1]=>
55    object(ArrayIterator)#%d (1) {
56      %s"storage"%s"ArrayIterator":private]=>
57      array(2) {
58        [0]=>
59        int(21)
60        [1]=>
61        int(22)
62      }
63    }
64  }
65}
66object(ArrayIterator)#%d (1) {
67  %s"storage"%s"ArrayIterator":private]=>
68  array(3) {
69    [0]=>
70    object(ArrayIterator)#%d (1) {
71      %s"storage"%s"ArrayIterator":private]=>
72      array(1) {
73        [0]=>
74        int(1)
75      }
76    }
77    [1]=>
78    object(ArrayIterator)#%d (1) {
79      %s"storage"%s"ArrayIterator":private]=>
80      array(2) {
81        [0]=>
82        int(21)
83        [1]=>
84        int(22)
85      }
86    }
87    [2]=>
88    object(ArrayIterator)#%d (1) {
89      %s"storage"%s"ArrayIterator":private]=>
90      array(3) {
91        [0]=>
92        int(31)
93        [1]=>
94        int(32)
95        [2]=>
96        int(33)
97      }
98    }
99  }
100}
101===0===
102int(0)
103int(0)
104int(1)
105===1===
106int(1)
107int(0)
108int(21)
109===2===
110int(1)
111int(1)
112int(22)
113===3===
114int(2)
115int(0)
116int(31)
117===4===
118int(2)
119int(1)
120int(32)
121===5===
122int(2)
123int(2)
124int(33)
125