@@ -382,6 +382,118 @@ public function testChunkPaginatesUsingIdWithCountZero()
382382 }, 'someIdField ' );
383383 }
384384
385+ public function testLazyWithLastChunkComplete ()
386+ {
387+ $ builder = m::mock (Builder::class.'[forPage,get] ' , [$ this ->getMockQueryBuilder ()]);
388+ $ builder ->getQuery ()->orders [] = ['column ' => 'foobar ' , 'direction ' => 'asc ' ];
389+
390+ $ builder ->shouldReceive ('forPage ' )->once ()->with (1 , 2 )->andReturnSelf ();
391+ $ builder ->shouldReceive ('forPage ' )->once ()->with (2 , 2 )->andReturnSelf ();
392+ $ builder ->shouldReceive ('forPage ' )->once ()->with (3 , 2 )->andReturnSelf ();
393+ $ builder ->shouldReceive ('get ' )->times (3 )->andReturn (
394+ new Collection (['foo1 ' , 'foo2 ' ]),
395+ new Collection (['foo3 ' , 'foo4 ' ]),
396+ new Collection ([])
397+ );
398+
399+ $ this ->assertEquals (
400+ ['foo1 ' , 'foo2 ' , 'foo3 ' , 'foo4 ' ],
401+ $ builder ->lazy (2 )->all ()
402+ );
403+ }
404+
405+ public function testLazyWithLastChunkPartial ()
406+ {
407+ $ builder = m::mock (Builder::class.'[forPage,get] ' , [$ this ->getMockQueryBuilder ()]);
408+ $ builder ->getQuery ()->orders [] = ['column ' => 'foobar ' , 'direction ' => 'asc ' ];
409+
410+ $ builder ->shouldReceive ('forPage ' )->once ()->with (1 , 2 )->andReturnSelf ();
411+ $ builder ->shouldReceive ('forPage ' )->once ()->with (2 , 2 )->andReturnSelf ();
412+ $ builder ->shouldReceive ('get ' )->times (2 )->andReturn (
413+ new Collection (['foo1 ' , 'foo2 ' ]),
414+ new Collection (['foo3 ' ])
415+ );
416+
417+ $ this ->assertEquals (
418+ ['foo1 ' , 'foo2 ' , 'foo3 ' ],
419+ $ builder ->lazy (2 )->all ()
420+ );
421+ }
422+
423+ public function testLazyIsLazy ()
424+ {
425+ $ builder = m::mock (Builder::class.'[forPage,get] ' , [$ this ->getMockQueryBuilder ()]);
426+ $ builder ->getQuery ()->orders [] = ['column ' => 'foobar ' , 'direction ' => 'asc ' ];
427+
428+ $ builder ->shouldReceive ('forPage ' )->once ()->with (1 , 2 )->andReturnSelf ();
429+ $ builder ->shouldReceive ('get ' )->once ()->andReturn (new Collection (['foo1 ' , 'foo2 ' ]));
430+
431+ $ this ->assertEquals (['foo1 ' , 'foo2 ' ], $ builder ->lazy (2 )->take (2 )->all ());
432+ }
433+
434+ public function testLazyByIdWithLastChunkComplete ()
435+ {
436+ $ builder = m::mock (Builder::class.'[forPageAfterId,get] ' , [$ this ->getMockQueryBuilder ()]);
437+ $ builder ->getQuery ()->orders [] = ['column ' => 'foobar ' , 'direction ' => 'asc ' ];
438+
439+ $ chunk1 = new Collection ([(object ) ['someIdField ' => 1 ], (object ) ['someIdField ' => 2 ]]);
440+ $ chunk2 = new Collection ([(object ) ['someIdField ' => 10 ], (object ) ['someIdField ' => 11 ]]);
441+ $ chunk3 = new Collection ([]);
442+ $ builder ->shouldReceive ('forPageAfterId ' )->once ()->with (2 , 0 , 'someIdField ' )->andReturnSelf ();
443+ $ builder ->shouldReceive ('forPageAfterId ' )->once ()->with (2 , 2 , 'someIdField ' )->andReturnSelf ();
444+ $ builder ->shouldReceive ('forPageAfterId ' )->once ()->with (2 , 11 , 'someIdField ' )->andReturnSelf ();
445+ $ builder ->shouldReceive ('get ' )->times (3 )->andReturn ($ chunk1 , $ chunk2 , $ chunk3 );
446+
447+ $ this ->assertEquals (
448+ [
449+ (object ) ['someIdField ' => 1 ],
450+ (object ) ['someIdField ' => 2 ],
451+ (object ) ['someIdField ' => 10 ],
452+ (object ) ['someIdField ' => 11 ],
453+ ],
454+ $ builder ->lazyById (2 , 'someIdField ' )->all ()
455+ );
456+ }
457+
458+ public function testLazyByIdWithLastChunkPartial ()
459+ {
460+ $ builder = m::mock (Builder::class.'[forPageAfterId,get] ' , [$ this ->getMockQueryBuilder ()]);
461+ $ builder ->getQuery ()->orders [] = ['column ' => 'foobar ' , 'direction ' => 'asc ' ];
462+
463+ $ chunk1 = new Collection ([(object ) ['someIdField ' => 1 ], (object ) ['someIdField ' => 2 ]]);
464+ $ chunk2 = new Collection ([(object ) ['someIdField ' => 10 ]]);
465+ $ builder ->shouldReceive ('forPageAfterId ' )->once ()->with (2 , 0 , 'someIdField ' )->andReturnSelf ();
466+ $ builder ->shouldReceive ('forPageAfterId ' )->once ()->with (2 , 2 , 'someIdField ' )->andReturnSelf ();
467+ $ builder ->shouldReceive ('get ' )->times (2 )->andReturn ($ chunk1 , $ chunk2 );
468+
469+ $ this ->assertEquals (
470+ [
471+ (object ) ['someIdField ' => 1 ],
472+ (object ) ['someIdField ' => 2 ],
473+ (object ) ['someIdField ' => 10 ],
474+ ],
475+ $ builder ->lazyById (2 , 'someIdField ' )->all ()
476+ );
477+ }
478+
479+ public function testLazyByIdIsLazy ()
480+ {
481+ $ builder = m::mock (Builder::class.'[forPageAfterId,get] ' , [$ this ->getMockQueryBuilder ()]);
482+ $ builder ->getQuery ()->orders [] = ['column ' => 'foobar ' , 'direction ' => 'asc ' ];
483+
484+ $ chunk1 = new Collection ([(object ) ['someIdField ' => 1 ], (object ) ['someIdField ' => 2 ]]);
485+ $ builder ->shouldReceive ('forPageAfterId ' )->once ()->with (2 , 0 , 'someIdField ' )->andReturnSelf ();
486+ $ builder ->shouldReceive ('get ' )->once ()->andReturn ($ chunk1 );
487+
488+ $ this ->assertEquals (
489+ [
490+ (object ) ['someIdField ' => 1 ],
491+ (object ) ['someIdField ' => 2 ],
492+ ],
493+ $ builder ->lazyById (2 , 'someIdField ' )->take (2 )->all ()
494+ );
495+ }
496+
385497 public function testPluckReturnsTheMutatedAttributesOfAModel ()
386498 {
387499 $ builder = $ this ->getBuilder ();
0 commit comments