xref: /PHP-8.1/ext/spl/tests/iterator_045.phpt (revision 5dafd7b4)
1--TEST--
2SPL: CachingIterator and offsetSet/Unset, getCache using flag FULL_CACHE
3--FILE--
4<?php
5
6class MyFoo
7{
8    function __toString()
9    {
10        return 'foo';
11    }
12}
13
14class MyCachingIterator extends CachingIterator
15{
16    function __construct(Iterator $it, $flags = 0)
17    {
18        parent::__construct($it, $flags);
19    }
20
21    function testSet($ar)
22    {
23        echo __METHOD__ . "()\n";
24        foreach($ar as $k => $v)
25        {
26            echo "set($k,$v)\n";
27            $this->offsetSet($k, $v);
28        }
29    }
30
31    function testUnset($ar)
32    {
33        echo __METHOD__ . "()\n";
34        foreach($ar as $k => $v)
35        {
36            echo "unset($v)\n";
37            $this->offsetUnset($v);
38        }
39    }
40
41    function fill()
42    {
43        echo __METHOD__ . "()\n";
44        foreach($this as $v) ;
45    }
46
47    function show()
48    {
49        echo __METHOD__ . "()\n";
50        var_dump($this->getCache());
51    }
52}
53
54$it = new MyCachingIterator(new ArrayIterator(array(0, 'foo'=>1, 2, 'bar'=>3, 4)));
55
56try
57{
58    var_dump($it->offsetSet(0, 0));
59}
60catch(Exception $e)
61{
62    echo "Exception: " . $e->getMessage() . "\n";
63}
64
65try
66{
67    var_dump($it->offsetUnset(0));
68}
69catch(Exception $e)
70{
71    echo "Exception: " . $e->getMessage() . "\n";
72}
73
74$it = new MyCachingIterator(new ArrayIterator(array(0, 1, 2, 3)), CachingIterator::FULL_CACHE);
75
76$checks = array(0 => 25, 1 => 42, 3 => 'FooBar');
77$unsets = array(0, 2);
78
79$it->testSet($checks);
80$it->show();
81$it->testUnset($unsets);
82$it->show();
83$it->fill();
84$it->show();
85$it->testSet($checks);
86$it->show();
87$it->testUnset($unsets);
88$it->show();
89
90?>
91--EXPECT--
92Exception: MyCachingIterator does not use a full cache (see CachingIterator::__construct)
93Exception: MyCachingIterator does not use a full cache (see CachingIterator::__construct)
94MyCachingIterator::testSet()
95set(0,25)
96set(1,42)
97set(3,FooBar)
98MyCachingIterator::show()
99array(3) {
100  [0]=>
101  int(25)
102  [1]=>
103  int(42)
104  [3]=>
105  string(6) "FooBar"
106}
107MyCachingIterator::testUnset()
108unset(0)
109unset(2)
110MyCachingIterator::show()
111array(2) {
112  [1]=>
113  int(42)
114  [3]=>
115  string(6) "FooBar"
116}
117MyCachingIterator::fill()
118MyCachingIterator::show()
119array(4) {
120  [0]=>
121  int(0)
122  [1]=>
123  int(1)
124  [2]=>
125  int(2)
126  [3]=>
127  int(3)
128}
129MyCachingIterator::testSet()
130set(0,25)
131set(1,42)
132set(3,FooBar)
133MyCachingIterator::show()
134array(4) {
135  [0]=>
136  int(25)
137  [1]=>
138  int(42)
139  [2]=>
140  int(2)
141  [3]=>
142  string(6) "FooBar"
143}
144MyCachingIterator::testUnset()
145unset(0)
146unset(2)
147MyCachingIterator::show()
148array(2) {
149  [1]=>
150  int(42)
151  [3]=>
152  string(6) "FooBar"
153}
154