xref: /PHP-7.0/ext/spl/tests/iterator_045.phpt (revision 610c7fbe)
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
76var_dump($it->offsetSet());
77var_dump($it->offsetSet(0));
78var_dump($it->offsetUnset());
79
80$checks = array(0 => 25, 1 => 42, 3 => 'FooBar');
81$unsets = array(0, 2);
82
83$it->testSet($checks);
84$it->show();
85$it->testUnset($unsets);
86$it->show();
87$it->fill();
88$it->show();
89$it->testSet($checks);
90$it->show();
91$it->testUnset($unsets);
92$it->show();
93
94?>
95===DONE===
96<?php exit(0); ?>
97--EXPECTF--
98Exception: MyCachingIterator does not use a full cache (see CachingIterator::__construct)
99Exception: MyCachingIterator does not use a full cache (see CachingIterator::__construct)
100
101Warning: CachingIterator::offsetSet() expects exactly 2 parameters, 0 given in %siterator_045.php on line %d
102NULL
103
104Warning: CachingIterator::offsetSet() expects exactly 2 parameters, 1 given in %siterator_045.php on line %d
105NULL
106
107Warning: CachingIterator::offsetUnset() expects exactly 1 parameter, 0 given in %siterator_045.php on line %d
108NULL
109MyCachingIterator::testSet()
110set(0,25)
111set(1,42)
112set(3,FooBar)
113MyCachingIterator::show()
114array(3) {
115  [0]=>
116  int(25)
117  [1]=>
118  int(42)
119  [3]=>
120  string(6) "FooBar"
121}
122MyCachingIterator::testUnset()
123unset(0)
124unset(2)
125MyCachingIterator::show()
126array(2) {
127  [1]=>
128  int(42)
129  [3]=>
130  string(6) "FooBar"
131}
132MyCachingIterator::fill()
133MyCachingIterator::show()
134array(4) {
135  [0]=>
136  int(0)
137  [1]=>
138  int(1)
139  [2]=>
140  int(2)
141  [3]=>
142  int(3)
143}
144MyCachingIterator::testSet()
145set(0,25)
146set(1,42)
147set(3,FooBar)
148MyCachingIterator::show()
149array(4) {
150  [0]=>
151  int(25)
152  [1]=>
153  int(42)
154  [2]=>
155  int(2)
156  [3]=>
157  string(6) "FooBar"
158}
159MyCachingIterator::testUnset()
160unset(0)
161unset(2)
162MyCachingIterator::show()
163array(2) {
164  [1]=>
165  int(42)
166  [3]=>
167  string(6) "FooBar"
168}
169===DONE===
170