summaryrefslogtreecommitdiff
path: root/ext/spl/tests/iterator_044.phpt
blob: 66f722af9a1bd6644be67d6346dd2a925a909083 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
--TEST--
SPL: CachingIterator and offsetGet/Exists using flag FULL_CACHE
--FILE--
<?php

class MyFoo
{
    function __toString()
    {
        return 'foo';
    }
}

class MyCachingIterator extends CachingIterator
{
    function __construct(Iterator $it, $flags = 0)
    {
        parent::__construct($it, $flags);
    }

    function test($ar)
    {
        foreach($ar as $k => $v)
        {
            echo "===$k===\n";
            var_dump($v);
            try {
                var_dump($this->offsetExists($v));
            } catch (TypeError $e) {
                echo $e->getMessage(), "\n";
            }
            try {
                var_dump($this->offsetGet($v));
            } catch (TypeError $e) {
                echo $e->getMessage(), "\n";
            }
        }
    }
}

$it = new MyCachingIterator(new ArrayIterator(array(0, 'foo'=>1, 2, 'bar'=>3, 4)));

try
{
    var_dump($it->offsetExists(0));
}
catch(Exception $e)
{
    echo "Exception: " . $e->getMessage() . "\n";
}

try
{
    var_dump($it->offsetGet(0));
}
catch(Exception $e)
{
    echo "Exception: " . $e->getMessage() . "\n";
}

$it = new MyCachingIterator(new ArrayIterator(array(0, 'foo'=>1, 2, 'bar'=>3, 4)), CachingIterator::FULL_CACHE);

$checks = array(0, new stdClass, new MyFoo, NULL, 2, 'foo', 3);

$it->test($checks);

echo "===FILL===\n";

foreach($it as $v); // read all into cache

$it->test($checks);

?>
--EXPECTF--
Exception: MyCachingIterator does not use a full cache (see CachingIterator::__construct)
Exception: MyCachingIterator does not use a full cache (see CachingIterator::__construct)
===0===
int(0)
bool(false)

Warning: Undefined array key "0" in %s on line %d
NULL
===1===
object(stdClass)#%d (0) {
}
CachingIterator::offsetExists(): Argument #1 ($key) must be of type string, stdClass given
CachingIterator::offsetGet(): Argument #1 ($key) must be of type string, stdClass given
===2===
object(MyFoo)#%d (0) {
}
bool(false)

Warning: Undefined array key "foo" in %s on line %d
NULL
===3===
NULL
bool(false)

Warning: Undefined array key "" in %s on line %d
NULL
===4===
int(2)
bool(false)

Warning: Undefined array key "2" in %s on line %d
NULL
===5===
string(3) "foo"
bool(false)

Warning: Undefined array key "foo" in %s on line %d
NULL
===6===
int(3)
bool(false)

Warning: Undefined array key "3" in %s on line %d
NULL
===FILL===
===0===
int(0)
bool(true)
int(0)
===1===
object(stdClass)#1 (0) {
}
CachingIterator::offsetExists(): Argument #1 ($key) must be of type string, stdClass given
CachingIterator::offsetGet(): Argument #1 ($key) must be of type string, stdClass given
===2===
object(MyFoo)#2 (0) {
}
bool(true)
int(1)
===3===
NULL
bool(false)

Warning: Undefined array key "" in %s on line %d
NULL
===4===
int(2)
bool(true)
int(4)
===5===
string(3) "foo"
bool(true)
int(1)
===6===
int(3)
bool(false)

Warning: Undefined array key "3" in %s on line %d
NULL