]> granicus.if.org Git - php/blob
e245b95688
[php] /
1 --TEST--
2 SPL: CachingIterator and offsetSet/Unset, getCache using flag FULL_CACHE
3 --FILE--
4 <?php
5
6 class MyFoo
7 {
8     function __toString()
9     {
10         return 'foo';
11     }
12 }
13
14 class 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
56 try
57 {
58     var_dump($it->offsetSet(0, 0));
59 }
60 catch(Exception $e)
61 {
62     echo "Exception: " . $e->getMessage() . "\n";
63 }
64
65 try
66 {
67     var_dump($it->offsetUnset(0));
68 }
69 catch(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--
92 Exception: MyCachingIterator does not use a full cache (see CachingIterator::__construct)
93 Exception: MyCachingIterator does not use a full cache (see CachingIterator::__construct)
94 MyCachingIterator::testSet()
95 set(0,25)
96 set(1,42)
97 set(3,FooBar)
98 MyCachingIterator::show()
99 array(3) {
100   [0]=>
101   int(25)
102   [1]=>
103   int(42)
104   [3]=>
105   string(6) "FooBar"
106 }
107 MyCachingIterator::testUnset()
108 unset(0)
109 unset(2)
110 MyCachingIterator::show()
111 array(2) {
112   [1]=>
113   int(42)
114   [3]=>
115   string(6) "FooBar"
116 }
117 MyCachingIterator::fill()
118 MyCachingIterator::show()
119 array(4) {
120   [0]=>
121   int(0)
122   [1]=>
123   int(1)
124   [2]=>
125   int(2)
126   [3]=>
127   int(3)
128 }
129 MyCachingIterator::testSet()
130 set(0,25)
131 set(1,42)
132 set(3,FooBar)
133 MyCachingIterator::show()
134 array(4) {
135   [0]=>
136   int(25)
137   [1]=>
138   int(42)
139   [2]=>
140   int(2)
141   [3]=>
142   string(6) "FooBar"
143 }
144 MyCachingIterator::testUnset()
145 unset(0)
146 unset(2)
147 MyCachingIterator::show()
148 array(2) {
149   [1]=>
150   int(42)
151   [3]=>
152   string(6) "FooBar"
153 }