@@ -61,6 +61,72 @@ public function testCount()
6161 $ this ->assertCount (5 , $ this ->plugin );
6262 }
6363
64+ public function testIteratorFlow ()
65+ {
66+ $ result = $ this ->getResult ();
67+ $ mockClient = $ this ->createMock (Client::class);
68+
69+ // Important: if prefetch or query settings are not changed, the query should be executed only once!
70+ $ mockClient ->expects ($ this ->exactly (1 ))->method ('execute ' )->willReturn ($ result );
71+
72+ $ this ->plugin ->initPlugin ($ mockClient , []);
73+ $ this ->plugin ->setQuery ($ this ->query );
74+
75+ // run through the entire iterator manually
76+ $ this ->assertTrue ($ this ->plugin ->valid ());
77+ $ this ->assertSame (['id ' => 1 , 'title ' => 'doc1 ' ], $ this ->plugin ->current ()->getFields ());
78+ $ this ->assertSame (0 , $ this ->plugin ->key ());
79+ $ this ->plugin ->next ();
80+ $ this ->assertTrue ($ this ->plugin ->valid ());
81+ $ this ->assertSame (['id ' => 2 , 'title ' => 'doc2 ' ], $ this ->plugin ->current ()->getFields ());
82+ $ this ->assertSame (1 , $ this ->plugin ->key ());
83+ $ this ->plugin ->next ();
84+ $ this ->assertTrue ($ this ->plugin ->valid ());
85+ $ this ->assertSame (['id ' => 3 , 'title ' => 'doc3 ' ], $ this ->plugin ->current ()->getFields ());
86+ $ this ->assertSame (2 , $ this ->plugin ->key ());
87+ $ this ->plugin ->next ();
88+ $ this ->assertTrue ($ this ->plugin ->valid ());
89+ $ this ->assertSame (['id ' => 4 , 'title ' => 'doc4 ' ], $ this ->plugin ->current ()->getFields ());
90+ $ this ->assertSame (3 , $ this ->plugin ->key ());
91+ $ this ->plugin ->next ();
92+ $ this ->assertTrue ($ this ->plugin ->valid ());
93+ $ this ->assertSame (['id ' => 5 , 'title ' => 'doc5 ' ], $ this ->plugin ->current ()->getFields ());
94+ $ this ->assertSame (4 , $ this ->plugin ->key ());
95+ $ this ->plugin ->next ();
96+ $ this ->assertFalse ($ this ->plugin ->valid ());
97+
98+ // rewind at the end and partway through
99+ $ this ->plugin ->rewind ();
100+ $ this ->assertTrue ($ this ->plugin ->valid ());
101+ $ this ->assertSame (['id ' => 1 , 'title ' => 'doc1 ' ], $ this ->plugin ->current ()->getFields ());
102+ $ this ->assertSame (0 , $ this ->plugin ->key ());
103+ $ this ->plugin ->next ();
104+ $ this ->assertTrue ($ this ->plugin ->valid ());
105+ $ this ->assertSame (['id ' => 2 , 'title ' => 'doc2 ' ], $ this ->plugin ->current ()->getFields ());
106+ $ this ->assertSame (1 , $ this ->plugin ->key ());
107+ $ this ->plugin ->rewind ();
108+ $ this ->assertTrue ($ this ->plugin ->valid ());
109+ $ this ->assertSame (['id ' => 1 , 'title ' => 'doc1 ' ], $ this ->plugin ->current ()->getFields ());
110+ $ this ->assertSame (0 , $ this ->plugin ->key ());
111+ }
112+
113+ public function testIteratorEmptyResultFlow ()
114+ {
115+ $ result = $ this ->getEmptyResult ();
116+ $ mockClient = $ this ->createMock (Client::class);
117+
118+ // Important: if prefetch or query settings are not changed, the query should be executed only once!
119+ $ mockClient ->expects ($ this ->exactly (1 ))->method ('execute ' )->willReturn ($ result );
120+
121+ $ this ->plugin ->initPlugin ($ mockClient , []);
122+ $ this ->plugin ->setQuery ($ this ->query );
123+
124+ // there is nothing to run through
125+ $ this ->assertFalse ($ this ->plugin ->valid ());
126+ $ this ->plugin ->rewind ();
127+ $ this ->assertFalse ($ this ->plugin ->valid ());
128+ }
129+
64130 public function testIteratorAndRewind ()
65131 {
66132 $ result = $ this ->getResult ();
@@ -139,6 +205,26 @@ public function testIteratorResetOnSetQuery()
139205 $ this ->assertSame ($ result ->getDocuments (), $ results2 );
140206 }
141207
208+ public function testIteratorEmptyResult ()
209+ {
210+ $ result = $ this ->getEmptyResult ();
211+ $ mockClient = $ this ->createMock (Client::class);
212+
213+ // Important: if prefetch or query settings are not changed, the query should be executed only once!
214+ $ mockClient ->expects ($ this ->exactly (1 ))->method ('execute ' )->willReturn ($ result );
215+
216+ $ this ->plugin ->initPlugin ($ mockClient , []);
217+ $ this ->plugin ->setQuery ($ this ->query );
218+
219+ $ results = [];
220+ foreach ($ this ->plugin as $ doc ) {
221+ $ results [] = $ doc ;
222+ }
223+
224+ $ this ->assertCount (0 , $ this ->plugin );
225+ $ this ->assertSame ([], $ results );
226+ }
227+
142228 public function getResult ()
143229 {
144230 $ numFound = 5 ;
@@ -154,6 +240,15 @@ public function getResult()
154240 return new SelectDummy (1 , 12 , $ numFound , $ docs , []);
155241 }
156242
243+ public function getEmptyResult ()
244+ {
245+ $ numFound = 0 ;
246+
247+ $ docs = [];
248+
249+ return new SelectDummy (1 , 2 , $ numFound , $ docs , []);
250+ }
251+
157252 public function testSetAndGetEndpointAsString ()
158253 {
159254 $ this ->assertNull ($ this ->plugin ->getEndpoint ());
0 commit comments