File tree Expand file tree Collapse file tree 2 files changed +44
-3
lines changed Expand file tree Collapse file tree 2 files changed +44
-3
lines changed Original file line number Diff line number Diff line change @@ -153,12 +153,12 @@ private function exhaustIterator()
153
153
*/
154
154
private function storeCurrentItem ()
155
155
{
156
- $ key = $ this ->iterator ->key ();
157
-
158
- if ($ key === null ) {
156
+ if (! $ this ->iterator ->valid ()) {
159
157
return ;
160
158
}
161
159
160
+ $ key = $ this ->iterator ->key ();
161
+
162
162
$ this ->items [$ key ] = $ this ->iterator ->current ();
163
163
}
164
164
}
Original file line number Diff line number Diff line change 3
3
namespace MongoDB \Tests \Model ;
4
4
5
5
use Exception ;
6
+ use Iterator ;
6
7
use MongoDB \Model \CachingIterator ;
7
8
use MongoDB \Tests \TestCase ;
8
9
use Throwable ;
@@ -110,6 +111,46 @@ public function testCountWithEmptySet()
110
111
$ this ->assertCount (0 , $ iterator );
111
112
}
112
113
114
+ /**
115
+ * This protects against iterators that return valid keys on invalid
116
+ * positions, which was the case in ext-mongodb until PHPC-1748 was fixed.
117
+ */
118
+ public function testWithWrongIterator ()
119
+ {
120
+ $ nestedIterator = new class implements Iterator {
121
+ /** @var int */
122
+ private $ i = 0 ;
123
+
124
+ public function current ()
125
+ {
126
+ return $ this ->i ;
127
+ }
128
+
129
+ public function next ()
130
+ {
131
+ $ this ->i ++;
132
+ }
133
+
134
+ public function key ()
135
+ {
136
+ return $ this ->i ;
137
+ }
138
+
139
+ public function valid ()
140
+ {
141
+ return $ this ->i == 0 ;
142
+ }
143
+
144
+ public function rewind ()
145
+ {
146
+ $ this ->i = 0 ;
147
+ }
148
+ };
149
+
150
+ $ iterator = new CachingIterator ($ nestedIterator );
151
+ $ this ->assertCount (1 , $ iterator );
152
+ }
153
+
113
154
private function getTraversable ($ items )
114
155
{
115
156
foreach ($ items as $ item ) {
You can’t perform that action at this time.
0 commit comments