xref: /PHP-7.4/ext/spl/tests/iterator_031.phpt (revision 782352c5)
1--TEST--
2SPL: AppendIterator::append() rewinds when necessary
3--FILE--
4<?php
5
6class MyArrayIterator extends ArrayIterator
7{
8	function rewind()
9	{
10		echo __METHOD__ . "\n";
11		parent::rewind();
12	}
13}
14
15$it = new MyArrayIterator(array(1,2));
16
17foreach($it as $k=>$v)
18{
19	echo "$k=>$v\n";
20}
21
22class MyAppendIterator extends AppendIterator
23{
24	function __construct()
25	{
26		echo __METHOD__ . "\n";
27	}
28
29	function rewind()
30	{
31		echo __METHOD__ . "\n";
32		parent::rewind();
33	}
34
35	function valid()
36	{
37		echo __METHOD__ . "\n";
38		return parent::valid();
39	}
40
41	function append(Iterator $what)
42	{
43		echo __METHOD__ . "\n";
44		parent::append($what);
45	}
46
47	function parent__construct()
48	{
49		parent::__construct();
50	}
51}
52
53$ap = new MyAppendIterator;
54
55try
56{
57	$ap->append($it);
58}
59catch(LogicException $e)
60{
61	echo $e->getMessage() . "\n";
62}
63
64$ap->parent__construct();
65
66try
67{
68	$ap->parent__construct($it);
69}
70catch(BadMethodCallException $e)
71{
72	echo $e->getMessage() . "\n";
73}
74
75$ap->append($it);
76$ap->append($it);
77$ap->append($it);
78
79foreach($ap as $k=>$v)
80{
81	echo "$k=>$v\n";
82}
83
84?>
85===DONE===
86<?php exit(0); ?>
87--EXPECT--
88MyArrayIterator::rewind
890=>1
901=>2
91MyAppendIterator::__construct
92MyAppendIterator::append
93The object is in an invalid state as the parent constructor was not called
94AppendIterator::getIterator() must be called exactly once per instance
95MyAppendIterator::append
96MyArrayIterator::rewind
97MyAppendIterator::append
98MyAppendIterator::append
99MyAppendIterator::rewind
100MyArrayIterator::rewind
101MyAppendIterator::valid
1020=>1
103MyAppendIterator::valid
1041=>2
105MyArrayIterator::rewind
106MyAppendIterator::valid
1070=>1
108MyAppendIterator::valid
1091=>2
110MyArrayIterator::rewind
111MyAppendIterator::valid
1120=>1
113MyAppendIterator::valid
1141=>2
115MyAppendIterator::valid
116===DONE===
117