xref: /PHP-7.1/ext/spl/tests/iterator_042.phpt (revision 64b167d2)
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===DONE===
42<?php exit(0); ?>
43--EXPECTF--
44Error Argument 1 passed to AppendIterator::append() must implement interface Iterator, array given in %siterator_042.php on line %d
45object(ArrayIterator)#%d (1) {
46  %s"storage"%s"ArrayIterator":private]=>
47  array(2) {
48    [0]=>
49    object(ArrayIterator)#%d (1) {
50      %s"storage"%s"ArrayIterator":private]=>
51      array(1) {
52        [0]=>
53        int(1)
54      }
55    }
56    [1]=>
57    object(ArrayIterator)#%d (1) {
58      %s"storage"%s"ArrayIterator":private]=>
59      array(2) {
60        [0]=>
61        int(21)
62        [1]=>
63        int(22)
64      }
65    }
66  }
67}
68object(ArrayIterator)#%d (1) {
69  %s"storage"%s"ArrayIterator":private]=>
70  array(3) {
71    [0]=>
72    object(ArrayIterator)#%d (1) {
73      %s"storage"%s"ArrayIterator":private]=>
74      array(1) {
75        [0]=>
76        int(1)
77      }
78    }
79    [1]=>
80    object(ArrayIterator)#%d (1) {
81      %s"storage"%s"ArrayIterator":private]=>
82      array(2) {
83        [0]=>
84        int(21)
85        [1]=>
86        int(22)
87      }
88    }
89    [2]=>
90    object(ArrayIterator)#%d (1) {
91      %s"storage"%s"ArrayIterator":private]=>
92      array(3) {
93        [0]=>
94        int(31)
95        [1]=>
96        int(32)
97        [2]=>
98        int(33)
99      }
100    }
101  }
102}
103===0===
104int(0)
105int(0)
106int(1)
107===1===
108int(1)
109int(0)
110int(21)
111===2===
112int(1)
113int(1)
114int(22)
115===3===
116int(2)
117int(0)
118int(31)
119===4===
120int(2)
121int(1)
122int(32)
123===5===
124int(2)
125int(2)
126int(33)
127===DONE===
128