xref: /PHP-8.0/ext/spl/tests/iterator_044.phpt (revision 68195bd4)
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
97bool(false)
98
99Warning: Undefined array key "" in %s on line %d
100NULL
101===4===
102int(2)
103bool(false)
104
105Warning: Undefined array key "2" in %s on line %d
106NULL
107===5===
108string(3) "foo"
109bool(false)
110
111Warning: Undefined array key "foo" in %s on line %d
112NULL
113===6===
114int(3)
115bool(false)
116
117Warning: Undefined array key "3" in %s on line %d
118NULL
119===FILL===
120===0===
121int(0)
122bool(true)
123int(0)
124===1===
125object(stdClass)#1 (0) {
126}
127CachingIterator::offsetExists(): Argument #1 ($key) must be of type string, stdClass given
128CachingIterator::offsetGet(): Argument #1 ($key) must be of type string, stdClass given
129===2===
130object(MyFoo)#2 (0) {
131}
132bool(true)
133int(1)
134===3===
135NULL
136bool(false)
137
138Warning: Undefined array key "" in %s on line %d
139NULL
140===4===
141int(2)
142bool(true)
143int(4)
144===5===
145string(3) "foo"
146bool(true)
147int(1)
148===6===
149int(3)
150bool(false)
151
152Warning: Undefined array key "3" in %s on line %d
153NULL
154