xref: /php-src/ext/spl/tests/iterator_044.phpt (revision b10416a6)
1--TEST--
2SPL: CachingIterator and offsetGet/Exists 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 test($ar)
22    {
23        foreach($ar as $k => $v)
24        {
25            echo "===$k===\n";
26            var_dump($v);
27            try {
28                var_dump($this->offsetExists($v));
29            } catch (TypeError $e) {
30                echo $e->getMessage(), "\n";
31            }
32            try {
33                var_dump($this->offsetGet($v));
34            } catch (TypeError $e) {
35                echo $e->getMessage(), "\n";
36            }
37        }
38    }
39}
40
41$it = new MyCachingIterator(new ArrayIterator(array(0, 'foo'=>1, 2, 'bar'=>3, 4)));
42
43try
44{
45    var_dump($it->offsetExists(0));
46}
47catch(Exception $e)
48{
49    echo "Exception: " . $e->getMessage() . "\n";
50}
51
52try
53{
54    var_dump($it->offsetGet(0));
55}
56catch(Exception $e)
57{
58    echo "Exception: " . $e->getMessage() . "\n";
59}
60
61$it = new MyCachingIterator(new ArrayIterator(array(0, 'foo'=>1, 2, 'bar'=>3, 4)), CachingIterator::FULL_CACHE);
62
63$checks = array(0, new stdClass, new MyFoo, NULL, 2, 'foo', 3);
64
65$it->test($checks);
66
67echo "===FILL===\n";
68
69foreach($it as $v); // read all into cache
70
71$it->test($checks);
72
73?>
74--EXPECTF--
75Exception: MyCachingIterator does not use a full cache (see CachingIterator::__construct)
76Exception: MyCachingIterator does not use a full cache (see CachingIterator::__construct)
77===0===
78int(0)
79bool(false)
80
81Warning: Undefined array key "0" in %s on line %d
82NULL
83===1===
84object(stdClass)#%d (0) {
85}
86CachingIterator::offsetExists(): Argument #1 ($key) must be of type string, stdClass given
87CachingIterator::offsetGet(): Argument #1 ($key) must be of type string, stdClass given
88===2===
89object(MyFoo)#%d (0) {
90}
91bool(false)
92
93Warning: Undefined array key "foo" in %s on line %d
94NULL
95===3===
96NULL
97
98Deprecated: CachingIterator::offsetExists(): Passing null to parameter #1 ($key) of type string is deprecated in %s on line %d
99bool(false)
100
101Deprecated: CachingIterator::offsetGet(): Passing null to parameter #1 ($key) of type string is deprecated in %s on line %d
102
103Warning: Undefined array key "" in %s on line %d
104NULL
105===4===
106int(2)
107bool(false)
108
109Warning: Undefined array key "2" in %s on line %d
110NULL
111===5===
112string(3) "foo"
113bool(false)
114
115Warning: Undefined array key "foo" in %s on line %d
116NULL
117===6===
118int(3)
119bool(false)
120
121Warning: Undefined array key "3" in %s on line %d
122NULL
123===FILL===
124===0===
125int(0)
126bool(true)
127int(0)
128===1===
129object(stdClass)#1 (0) {
130}
131CachingIterator::offsetExists(): Argument #1 ($key) must be of type string, stdClass given
132CachingIterator::offsetGet(): Argument #1 ($key) must be of type string, stdClass given
133===2===
134object(MyFoo)#2 (0) {
135}
136bool(true)
137int(1)
138===3===
139NULL
140
141Deprecated: CachingIterator::offsetExists(): Passing null to parameter #1 ($key) of type string is deprecated in %s on line %d
142bool(false)
143
144Deprecated: CachingIterator::offsetGet(): Passing null to parameter #1 ($key) of type string is deprecated in %s on line %d
145
146Warning: Undefined array key "" in %s on line %d
147NULL
148===4===
149int(2)
150bool(true)
151int(4)
152===5===
153string(3) "foo"
154bool(true)
155int(1)
156===6===
157int(3)
158bool(false)
159
160Warning: Undefined array key "3" in %s on line %d
161NULL
162