File tree Expand file tree Collapse file tree 2 files changed +43
-3
lines changed Expand file tree Collapse file tree 2 files changed +43
-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,45 @@ 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
+ private $ i = 0 ;
122
+
123
+ public function current ()
124
+ {
125
+ return $ this ->i ;
126
+ }
127
+
128
+ public function next ()
129
+ {
130
+ $ this ->i ++;
131
+ }
132
+
133
+ public function key ()
134
+ {
135
+ return $ this ->i ;
136
+ }
137
+
138
+ public function valid ()
139
+ {
140
+ return $ this ->i == 0 ;
141
+ }
142
+
143
+ public function rewind ()
144
+ {
145
+ $ this ->i = 0 ;
146
+ }
147
+ };
148
+
149
+ $ iterator = new CachingIterator ($ nestedIterator );
150
+ $ this ->assertCount (1 , $ iterator );
151
+ }
152
+
113
153
private function getTraversable ($ items )
114
154
{
115
155
foreach ($ items as $ item ) {
You can’t perform that action at this time.
0 commit comments