xref: /PHP-8.2/ext/spl/tests/iterator_031.phpt (revision b3e08881)
1--TEST--
2SPL: AppendIterator::append() rewinds when necessary
3--FILE--
4<?php
5
6class MyArrayIterator extends ArrayIterator
7{
8    function rewind(): void
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(): void
30    {
31        echo __METHOD__ . "\n";
32        parent::rewind();
33    }
34
35    function valid(): bool
36    {
37        echo __METHOD__ . "\n";
38        return parent::valid();
39    }
40
41    function append(Iterator $what): void
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(\Error $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--EXPECT--
86MyArrayIterator::rewind
870=>1
881=>2
89MyAppendIterator::__construct
90MyAppendIterator::append
91The object is in an invalid state as the parent constructor was not called
92AppendIterator::getIterator() must be called exactly once per instance
93MyAppendIterator::append
94MyArrayIterator::rewind
95MyAppendIterator::append
96MyAppendIterator::append
97MyAppendIterator::rewind
98MyArrayIterator::rewind
99MyAppendIterator::valid
1000=>1
101MyAppendIterator::valid
1021=>2
103MyArrayIterator::rewind
104MyAppendIterator::valid
1050=>1
106MyAppendIterator::valid
1071=>2
108MyArrayIterator::rewind
109MyAppendIterator::valid
1100=>1
111MyAppendIterator::valid
1121=>2
113MyAppendIterator::valid
114