Skip to content

Commit 44e1d52

Browse files
committed
test(API): satisfy StringCastAssertStringContainsStringRector in paginate overflow test
1 parent 8d7fbae commit 44e1d52

2 files changed

Lines changed: 52 additions & 29 deletions

File tree

‎system/API/ResponseTrait.php‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,13 +416,15 @@ protected function paginate(BaseBuilder|Model $resource, int $perPage = 20, ?str
416416
];
417417
} else {
418418
// Query Builder, we need to handle pagination manually
419-
// The page is clamped to the available range so that the offset
420-
// cannot overflow the int type when a huge page is requested.
421419
$total = (clone $resource)->countAllResults();
422420
$totalPages = (int) ceil($total / $perPage);
423-
$page = max(1, min($page, max(1, $totalPages)));
424-
$offset = ($page - 1) * $perPage;
425-
$data = $resource->limit($perPage, $offset)->get()->getResultArray();
421+
422+
if ($total === 0 || $page > $totalPages) {
423+
$data = [];
424+
} else {
425+
$offset = ($page - 1) * $perPage;
426+
$data = $resource->limit($perPage, $offset)->get()->getResultArray();
427+
}
426428

427429
$meta = [
428430
'page' => $page,

‎tests/system/API/ResponseTraitTest.php‎

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ public function testPaginateWithQueryBuilder(): void
799799
$this->assertSame(3, $responseBody['meta']['totalPages']);
800800
}
801801

802-
public function testPaginateWithQueryBuilderPageOverflow(): void
802+
public function testPaginateWithQueryBuilderOutOfRangePage(): void
803803
{
804804
// Mock the database and builder
805805
$db = $this->createStub(BaseConnection::class);
@@ -809,21 +809,46 @@ public function testPaginateWithQueryBuilderPageOverflow(): void
809809
->onlyMethods(['countAllResults', 'limit', 'get'])
810810
->getMock();
811811

812-
$result = $this->createMock(BaseResult::class);
812+
// Mock the query builder chain
813+
$builder->method('countAllResults')->willReturn(50);
814+
$builder->expects($this->never())
815+
->method('limit');
816+
$builder->expects($this->never())
817+
->method('get');
813818

814-
$data = [
815-
['id' => 1, 'name' => 'Item 1'],
816-
['id' => 2, 'name' => 'Item 2'],
817-
];
819+
$controller = $this->makeController('/api/items?page=4');
820+
$this->request->setGlobal('get', ['page' => '4']);
821+
822+
$this->invoke($controller, 'paginate', [$builder, 20]);
823+
824+
$responseBody = json_decode($this->response->getBody(), true);
825+
826+
$this->assertSame([], $responseBody['data']);
827+
$this->assertSame(4, $responseBody['meta']['page']);
828+
$this->assertSame(20, $responseBody['meta']['perPage']);
829+
$this->assertSame(50, $responseBody['meta']['total']);
830+
$this->assertSame(3, $responseBody['meta']['totalPages']);
831+
$this->assertStringContainsString('page=4', (string) $responseBody['links']['self']);
832+
$this->assertNull($responseBody['links']['next']);
833+
$this->assertStringContainsString('page=3', (string) $responseBody['links']['prev']);
834+
}
835+
836+
public function testPaginateWithQueryBuilderPageOverflow(): void
837+
{
838+
// Mock the database and builder
839+
$db = $this->createStub(BaseConnection::class);
840+
841+
$builder = $this->getMockBuilder(BaseBuilder::class)
842+
->setConstructorArgs(['test_table', $db])
843+
->onlyMethods(['countAllResults', 'limit', 'get'])
844+
->getMock();
818845

819846
// Mock the query builder chain
820847
$builder->method('countAllResults')->willReturn(50);
821-
$builder->expects($this->once())
822-
->method('limit')
823-
->with(20, 40)
824-
->willReturnSelf();
825-
$builder->method('get')->willReturn($result);
826-
$result->method('getResultArray')->willReturn($data);
848+
$builder->expects($this->never())
849+
->method('limit');
850+
$builder->expects($this->never())
851+
->method('get');
827852

828853
$controller = $this->makeController('/api/items?page=' . PHP_INT_MAX);
829854
$this->request->setGlobal('get', ['page' => (string) PHP_INT_MAX]);
@@ -832,12 +857,11 @@ public function testPaginateWithQueryBuilderPageOverflow(): void
832857

833858
$responseBody = json_decode($this->response->getBody(), true);
834859

835-
$this->assertSame(3, $responseBody['meta']['page']);
860+
$this->assertSame(PHP_INT_MAX, $responseBody['meta']['page']);
836861
$this->assertSame(20, $responseBody['meta']['perPage']);
837862
$this->assertSame(50, $responseBody['meta']['total']);
838863
$this->assertSame(3, $responseBody['meta']['totalPages']);
839-
$this->assertSame($data, $responseBody['data']);
840-
$this->assertStringContainsString('page=3', $responseBody['links']['self']);
864+
$this->assertSame([], $responseBody['data']);
841865
$this->assertNull($responseBody['links']['next']);
842866
}
843867

@@ -851,16 +875,12 @@ public function testPaginateWithQueryBuilderZeroResultsPageOverflow(): void
851875
->onlyMethods(['countAllResults', 'limit', 'get'])
852876
->getMock();
853877

854-
$result = $this->createMock(BaseResult::class);
855-
856878
// Mock the query builder chain
857879
$builder->method('countAllResults')->willReturn(0);
858-
$builder->expects($this->once())
859-
->method('limit')
860-
->with(20, 0)
861-
->willReturnSelf();
862-
$builder->method('get')->willReturn($result);
863-
$result->method('getResultArray')->willReturn([]);
880+
$builder->expects($this->never())
881+
->method('limit');
882+
$builder->expects($this->never())
883+
->method('get');
864884

865885
$controller = $this->makeController('/api/items?page=' . PHP_INT_MAX);
866886
$this->request->setGlobal('get', ['page' => (string) PHP_INT_MAX]);
@@ -869,7 +889,8 @@ public function testPaginateWithQueryBuilderZeroResultsPageOverflow(): void
869889

870890
$responseBody = json_decode($this->response->getBody(), true);
871891

872-
$this->assertSame(1, $responseBody['meta']['page']);
892+
$this->assertSame(PHP_INT_MAX, $responseBody['meta']['page']);
893+
$this->assertSame(0, $responseBody['meta']['total']);
873894
$this->assertSame(0, $responseBody['meta']['totalPages']);
874895
$this->assertSame([], $responseBody['data']);
875896
}

0 commit comments

Comments
 (0)