@@ -809,12 +809,18 @@ public function testPaginateWithQueryBuilderOutOfRangePage(): void
809809 ->onlyMethods (['countAllResults ' , 'limit ' , 'get ' ])
810810 ->getMock ();
811811
812+ $ result = $ this ->createMock (BaseResult::class);
813+
812814 // Mock the query builder chain
813815 $ builder ->method ('countAllResults ' )->willReturn (50 );
814- $ builder ->expects ($ this ->never ())
815- ->method ('limit ' );
816- $ builder ->expects ($ this ->never ())
817- ->method ('get ' );
816+ $ builder ->expects ($ this ->once ())
817+ ->method ('limit ' )
818+ ->with (20 , 50 )
819+ ->willReturnSelf ();
820+ $ builder ->expects ($ this ->once ())
821+ ->method ('get ' )
822+ ->willReturn ($ result );
823+ $ result ->method ('getResultArray ' )->willReturn ([]);
818824
819825 $ controller = $ this ->makeController ('/api/items?page=4 ' );
820826 $ this ->request ->setGlobal ('get ' , ['page ' => '4 ' ]);
@@ -843,12 +849,18 @@ public function testPaginateWithQueryBuilderPageOverflow(): void
843849 ->onlyMethods (['countAllResults ' , 'limit ' , 'get ' ])
844850 ->getMock ();
845851
852+ $ result = $ this ->createMock (BaseResult::class);
853+
846854 // Mock the query builder chain
847855 $ builder ->method ('countAllResults ' )->willReturn (50 );
848- $ builder ->expects ($ this ->never ())
849- ->method ('limit ' );
850- $ builder ->expects ($ this ->never ())
851- ->method ('get ' );
856+ $ builder ->expects ($ this ->once ())
857+ ->method ('limit ' )
858+ ->with (20 , 50 )
859+ ->willReturnSelf ();
860+ $ builder ->expects ($ this ->once ())
861+ ->method ('get ' )
862+ ->willReturn ($ result );
863+ $ result ->method ('getResultArray ' )->willReturn ([]);
852864
853865 $ controller = $ this ->makeController ('/api/items?page= ' . PHP_INT_MAX );
854866 $ this ->request ->setGlobal ('get ' , ['page ' => (string ) PHP_INT_MAX ]);
@@ -875,12 +887,18 @@ public function testPaginateWithQueryBuilderZeroResultsPageOverflow(): void
875887 ->onlyMethods (['countAllResults ' , 'limit ' , 'get ' ])
876888 ->getMock ();
877889
890+ $ result = $ this ->createMock (BaseResult::class);
891+
878892 // Mock the query builder chain
879893 $ builder ->method ('countAllResults ' )->willReturn (0 );
880- $ builder ->expects ($ this ->never ())
881- ->method ('limit ' );
882- $ builder ->expects ($ this ->never ())
883- ->method ('get ' );
894+ $ builder ->expects ($ this ->once ())
895+ ->method ('limit ' )
896+ ->with (20 , 0 )
897+ ->willReturnSelf ();
898+ $ builder ->expects ($ this ->once ())
899+ ->method ('get ' )
900+ ->willReturn ($ result );
901+ $ result ->method ('getResultArray ' )->willReturn ([]);
884902
885903 $ controller = $ this ->makeController ('/api/items?page= ' . PHP_INT_MAX );
886904 $ this ->request ->setGlobal ('get ' , ['page ' => (string ) PHP_INT_MAX ]);
@@ -895,6 +913,84 @@ public function testPaginateWithQueryBuilderZeroResultsPageOverflow(): void
895913 $ this ->assertSame ([], $ responseBody ['data ' ]);
896914 }
897915
916+ public function testPaginateWithQueryBuilderZeroPerPage (): void
917+ {
918+ // Mock the database and builder
919+ $ db = $ this ->createStub (BaseConnection::class);
920+
921+ $ builder = $ this ->getMockBuilder (BaseBuilder::class)
922+ ->setConstructorArgs (['test_table ' , $ db ])
923+ ->onlyMethods (['countAllResults ' , 'limit ' , 'get ' ])
924+ ->getMock ();
925+
926+ $ result = $ this ->createMock (BaseResult::class);
927+
928+ $ data = [
929+ ['id ' => 1 ],
930+ ['id ' => 2 ],
931+ ];
932+
933+ // Mock the query builder chain
934+ $ builder ->method ('countAllResults ' )->willReturn (50 );
935+ $ builder ->expects ($ this ->once ())
936+ ->method ('limit ' )
937+ ->with (0 , 0 )
938+ ->willReturnSelf ();
939+ $ builder ->expects ($ this ->once ())
940+ ->method ('get ' )
941+ ->willReturn ($ result );
942+ $ result ->method ('getResultArray ' )->willReturn ($ data );
943+
944+ $ controller = $ this ->makeController ('/api/items ' );
945+
946+ $ this ->invoke ($ controller , 'paginate ' , [$ builder , 0 ]);
947+
948+ $ responseBody = json_decode ($ this ->response ->getBody (), true );
949+
950+ $ this ->assertSame (200 , $ this ->response ->getStatusCode ());
951+ $ this ->assertSame (1 , $ responseBody ['meta ' ]['page ' ]);
952+ $ this ->assertSame (0 , $ responseBody ['meta ' ]['perPage ' ]);
953+ $ this ->assertSame (50 , $ responseBody ['meta ' ]['total ' ]);
954+ $ this ->assertSame (0 , $ responseBody ['meta ' ]['totalPages ' ]);
955+ $ this ->assertSame ($ data , $ responseBody ['data ' ]);
956+ }
957+
958+ public function testPaginateWithQueryBuilderStringTotalFromCountAllResults (): void
959+ {
960+ $ db = $ this ->createStub (BaseConnection::class);
961+
962+ $ builder = $ this ->getMockBuilder (BaseBuilder::class)
963+ ->setConstructorArgs (['test_table ' , $ db ])
964+ ->onlyMethods (['countAllResults ' , 'limit ' , 'get ' ])
965+ ->getMock ();
966+
967+ $ result = $ this ->createMock (BaseResult::class);
968+
969+ // Mock the query builder chain
970+ $ builder ->method ('countAllResults ' )->willReturn ('50 ' );
971+ $ builder ->expects ($ this ->once ())
972+ ->method ('limit ' )
973+ ->with (20 , 50 )
974+ ->willReturnSelf ();
975+ $ builder ->expects ($ this ->once ())
976+ ->method ('get ' )
977+ ->willReturn ($ result );
978+ $ result ->method ('getResultArray ' )->willReturn ([]);
979+
980+ $ controller = $ this ->makeController ('/api/items?page=4 ' );
981+ $ this ->request ->setGlobal ('get ' , ['page ' => '4 ' ]);
982+
983+ $ this ->invoke ($ controller , 'paginate ' , [$ builder , 20 ]);
984+
985+ $ responseBody = json_decode ($ this ->response ->getBody (), true );
986+
987+ $ this ->assertSame (200 , $ this ->response ->getStatusCode ());
988+ $ this ->assertSame ([], $ responseBody ['data ' ]);
989+ $ this ->assertSame (4 , $ responseBody ['meta ' ]['page ' ]);
990+ $ this ->assertSame (50 , $ responseBody ['meta ' ]['total ' ]);
991+ $ this ->assertSame (3 , $ responseBody ['meta ' ]['totalPages ' ]);
992+ }
993+
898994 public function testPaginateWithCustomPerPage (): void
899995 {
900996 $ data = [
0 commit comments