Skip to content

Commit 91adc9c

Browse files
Support pageSize parameter (#39)
1 parent 478bf1f commit 91adc9c

File tree

8 files changed

+135
-35
lines changed

8 files changed

+135
-35
lines changed

src/FixedSizeCollection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ private static function createPageArray($initialPage, $collectionSize)
152152
$rxElementCount = $currentPage->getPageElementCount();
153153
if ($rxElementCount > $remainingCount) {
154154
throw new LengthException("API returned a number of elements " .
155-
"exceeding the specified page_size limit. page_size: " .
156-
"$page_size, elements received: $rxElementCount");
155+
"exceeding the specified page size limit. page size: " .
156+
"$remainingCount, elements received: $rxElementCount");
157157
}
158158
array_push($pageList, $currentPage);
159159
$itemCount += $rxElementCount;

src/Page.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function getNextPageToken()
8686
/**
8787
* Retrieves the next Page object using the next page token.
8888
*/
89-
public function getNextPage()
89+
public function getNextPage($pageSize = null)
9090
{
9191
if (!$this->hasNextPage()) {
9292
throw new ValidationException(
@@ -100,6 +100,17 @@ public function getNextPage()
100100
$requestPageTokenField = $this->pageStreamingDescriptor->getRequestPageTokenField();
101101
$newRequest->$requestPageTokenField = $this->getNextPageToken();
102102

103+
if (isset($pageSize)) {
104+
if (!$this->pageStreamingDescriptor->requestHasPageSizeField()) {
105+
throw new ValidationException(
106+
'pageSize argument was defined, but the method does not ' .
107+
'support a page size parameter in the optional array argument'
108+
);
109+
}
110+
$requestPageSizeField = $this->pageStreamingDescriptor->getRequestPageSizeField();
111+
$newRequest->$requestPageSizeField = $pageSize;
112+
}
113+
103114
$nextParameters = [$newRequest, $this->parameters[1], $this->parameters[2]];
104115

105116
return new Page($nextParameters, $this->callable, $this->pageStreamingDescriptor);

src/PageStreamingDescriptor.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
class PageStreamingDescriptor
4040
{
4141
private $requestPageTokenField;
42+
private $requestPageSizeField;
4243
private $responsePageTokenField;
4344
private $resourceField;
4445

@@ -55,6 +56,11 @@ public function __construct($descriptor)
5556
{
5657
self::validate($descriptor);
5758
$this->requestPageTokenField = $descriptor['requestPageTokenField'];
59+
if (isset($descriptor['requestPageSizeField'])) {
60+
$this->requestPageSizeField = $descriptor['requestPageSizeField'];
61+
} else {
62+
$this->requestPageSizeField = null;
63+
}
5864
$this->responsePageTokenField = $descriptor['responsePageTokenField'];
5965
$this->resourceField = $descriptor['resourceField'];
6066
}
@@ -64,6 +70,16 @@ public function getRequestPageTokenField()
6470
return $this->requestPageTokenField;
6571
}
6672

73+
public function getRequestPageSizeField()
74+
{
75+
return $this->requestPageSizeField;
76+
}
77+
78+
public function requestHasPageSizeField()
79+
{
80+
return isset($this->requestPageSizeField);
81+
}
82+
6783
public function getResponsePageTokenField()
6884
{
6985
return $this->responsePageTokenField;

src/PagedListResponse.php

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,29 +124,39 @@ public function iteratePages()
124124
* fewer than collectionSize elements if there are no more
125125
* pages to be retrieved from the server.
126126
*
127-
* NOTE: it is an error to call this method if the optional parameter
128-
* 'page_size' has not been set in the original API call. It is also
129-
* an error if the collectionSize parameter is less than the
130-
* page_size.
127+
* NOTE: it is an error to call this method if an optional parameter
128+
* to set the page size is not supported or has not been set in the
129+
* original API call. It is also an error if the collectionSize parameter
130+
* is less than the page size that has been set.
131131
*/
132132
public function expandToFixedSizeCollection($collectionSize)
133133
{
134-
if (!array_key_exists(2, $this->parameters)
135-
|| !array_key_exists('page_size', $this->parameters[2])
136-
) {
134+
if (!$this->pageStreamingDescriptor->requestHasPageSizeField()) {
137135
throw new ValidationException(
138-
"Error while expanding Page to FixedSizeCollection: No page_size " .
139-
"parameter found. The page_size parameter must be set in the API " .
136+
"FixedSizeCollection is not supported for this method, because " .
137+
"the method does not support an optional argument to set the " .
138+
"page size."
139+
);
140+
}
141+
// The first page has been eagerly constructed, so we do not need to
142+
// update the page size parameter before calling getPage
143+
$page = $this->getPage();
144+
$request = $page->getRequestObject();
145+
$pageSizeField = $this->pageStreamingDescriptor->getRequestPageSizeField();
146+
if (!isset($request->$pageSizeField)) {
147+
throw new ValidationException(
148+
"Error while expanding Page to FixedSizeCollection: No page size " .
149+
"parameter found. The page size parameter must be set in the API " .
140150
"optional arguments array, and must be less than the collectionSize " .
141151
"parameter, in order to create a FixedSizeCollection object."
142152
);
143153
}
144-
$page_size = $this->parameters[2]['page_size'];
145-
if ($page_size > $collectionSize) {
154+
$pageSize = $request->$pageSizeField;
155+
if ($pageSize > $collectionSize) {
146156
throw new ValidationException(
147157
"Error while expanding Page to FixedSizeCollection: collectionSize " .
148-
"parameter is less than the page_size optional argument specified in " .
149-
"the API call. collectionSize: $collectionSize, page_size: $page_size"
158+
"parameter is less than the page size optional argument specified in " .
159+
"the API call. collectionSize: $collectionSize, page size: $pageSize"
150160
);
151161
}
152162
return new FixedSizeCollection($this->getPage(), $collectionSize);
@@ -160,10 +170,10 @@ public function expandToFixedSizeCollection($collectionSize)
160170
* exception of the final collection which may contain fewer
161171
* elements.
162172
*
163-
* NOTE: it is an error to call this method if the optional parameter
164-
* 'page_size' has not been set in the original API call. It is also
165-
* an error if the collectionSize parameter is less than the
166-
* page_size.
173+
* NOTE: it is an error to call this method if an optional parameter
174+
* to set the page size is not supported or has not been set in the
175+
* original API call. It is also an error if the collectionSize parameter
176+
* is less than the page size that has been set.
167177
*/
168178
public function iterateFixedSizeCollections($collectionSize)
169179
{

tests/ApiCallableTest.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public function testPageStreamingPageIterationNoTimeout()
267267

268268
public function testPageStreamingFixedSizeIterationNoTimeout()
269269
{
270-
$request = MockRequest::createPageStreamingRequest('token');
270+
$request = MockRequest::createPageStreamingRequest('token', 2);
271271
$responseA = MockResponse::createPageStreamingResponse('nextPageToken1', ['resource1']);
272272
$responseB = MockResponse::createPageStreamingResponse('nextPageToken2', ['resource2']);
273273
$responseC = MockResponse::createPageStreamingResponse(null, ['resource3', 'resource4']);
@@ -279,14 +279,15 @@ public function testPageStreamingFixedSizeIterationNoTimeout()
279279
$stub = MockStub::createWithResponseSequence($responseSequence);
280280
$descriptor = new PageStreamingDescriptor([
281281
'requestPageTokenField' => 'pageToken',
282+
'requestPageSizeField' => 'pageSize',
282283
'responsePageTokenField' => 'nextPageToken',
283284
'resourceField' => 'resource'
284285
]);
285286
$collectionSize = 2;
286287
$callSettings = new CallSettings();
287288
$apiCall = ApiCallable::createApiCall(
288289
$stub, 'takeAction', $callSettings, ['pageStreamingDescriptor' => $descriptor]);
289-
$response = $apiCall($request, [], ['page_size' => $collectionSize]);
290+
$response = $apiCall($request, [], []);
290291
$this->assertEquals(1, count($stub->actualCalls));
291292
$actualResources = [];
292293
$collectionCount = 0;
@@ -303,8 +304,9 @@ public function testPageStreamingFixedSizeIterationNoTimeout()
303304

304305
/**
305306
* @expectedException Google\GAX\ValidationException
307+
* @expectedExceptionMessage FixedSizeCollection is not supported
306308
*/
307-
public function testPageStreamingFixedSizeFailNoPageSize()
309+
public function testPageStreamingFixedSizeFailPageSizeNotSupported()
308310
{
309311
$request = MockRequest::createPageStreamingRequest('token');
310312
$responseA = MockResponse::createPageStreamingResponse('nextPageToken1', ['resource1']);
@@ -327,8 +329,9 @@ public function testPageStreamingFixedSizeFailNoPageSize()
327329

328330
/**
329331
* @expectedException Google\GAX\ValidationException
332+
* @expectedExceptionMessage No page size parameter found
330333
*/
331-
public function testPageStreamingFixedSizeFailPageSizeTooLarge()
334+
public function testPageStreamingFixedSizeFailPageSizeNotSet()
332335
{
333336
$request = MockRequest::createPageStreamingRequest('token');
334337
$responseA = MockResponse::createPageStreamingResponse('nextPageToken1', ['resource1']);
@@ -338,14 +341,41 @@ public function testPageStreamingFixedSizeFailPageSizeTooLarge()
338341
$stub = MockStub::createWithResponseSequence($responseSequence);
339342
$descriptor = new PageStreamingDescriptor([
340343
'requestPageTokenField' => 'pageToken',
344+
'requestPageSizeField' => 'pageSize',
341345
'responsePageTokenField' => 'nextPageToken',
342-
'resourceField' => 'resource'
346+
'resourceField' => 'resource',
343347
]);
344348
$collectionSize = 2;
345349
$callSettings = new CallSettings();
346350
$apiCall = ApiCallable::createApiCall(
347351
$stub, 'takeAction', $callSettings, ['pageStreamingDescriptor' => $descriptor]);
348-
$response = $apiCall($request, [], ['page_size' => ($collectionSize + 1)]);
352+
$response = $apiCall($request, [], []);
353+
$response->expandToFixedSizeCollection($collectionSize);
354+
}
355+
356+
/**
357+
* @expectedException Google\GAX\ValidationException
358+
* @expectedExceptionMessage collectionSize parameter is less than the page size
359+
*/
360+
public function testPageStreamingFixedSizeFailPageSizeTooLarge()
361+
{
362+
$collectionSize = 2;
363+
$request = MockRequest::createPageStreamingRequest('token', $collectionSize + 1);
364+
$responseA = MockResponse::createPageStreamingResponse('nextPageToken1', ['resource1']);
365+
$responseSequence = [
366+
[$responseA, new MockStatus(Grpc\STATUS_OK, '')]
367+
];
368+
$stub = MockStub::createWithResponseSequence($responseSequence);
369+
$descriptor = new PageStreamingDescriptor([
370+
'requestPageTokenField' => 'pageToken',
371+
'requestPageSizeField' => 'pageSize',
372+
'responsePageTokenField' => 'nextPageToken',
373+
'resourceField' => 'resource'
374+
]);
375+
$callSettings = new CallSettings();
376+
$apiCall = ApiCallable::createApiCall(
377+
$stub, 'takeAction', $callSettings, ['pageStreamingDescriptor' => $descriptor]);
378+
$response = $apiCall($request, [], []);
349379
$response->expandToFixedSizeCollection($collectionSize);
350380
}
351381

tests/FixedSizeCollectionTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@
4141
class FixedSizeCollectionTest extends PHPUnit_Framework_TestCase
4242
{
4343
private static function createPage($responseSequence) {
44-
$mockRequest = MockRequest::createPageStreamingRequest('token');
44+
$mockRequest = MockRequest::createPageStreamingRequest('token', 3);
4545
$stub = MockStub::createWithResponseSequence($responseSequence);
4646
$descriptor = new PageStreamingDescriptor([
4747
'requestPageTokenField' => 'pageToken',
48+
'requestPageSizeField' => 'pageSize',
4849
'responsePageTokenField' => 'nextPageToken',
4950
'resourceField' => 'resource'
5051
]);

tests/PageTest.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939

4040
class PageTest extends PHPUnit_Framework_TestCase
4141
{
42-
private static function createPage($responseSequence) {
42+
private static function createPage($responseSequence)
43+
{
4344
$mockRequest = MockRequest::createPageStreamingRequest('token');
4445
$stub = MockStub::createWithResponseSequence($responseSequence);
4546
$descriptor = new PageStreamingDescriptor([
@@ -55,7 +56,8 @@ private static function createPage($responseSequence) {
5556
return new Page([$mockRequest, [], []], $mockApiCall, $descriptor);
5657
}
5758

58-
public function testNextPageMethods() {
59+
public function testNextPageMethods()
60+
{
5961
$responseA = MockResponse::createPageStreamingResponse('nextPageToken1', ['resource1']);
6062
$responseB = MockResponse::createPageStreamingResponse('', ['resource2']);
6163
$page = PageTest::createPage([
@@ -72,7 +74,39 @@ public function testNextPageMethods() {
7274
$this->assertEquals($nextPage->getNextPageToken(), '');
7375
}
7476

75-
public function testPageElementMethods() {
77+
/**
78+
* @expectedException Google\GAX\ValidationException
79+
* @expectedExceptionMessage Could not complete getNextPage operation
80+
*/
81+
public function testNextPageMethodsFailWithNoNextPage()
82+
{
83+
$responseA = MockResponse::createPageStreamingResponse('', ['resource1']);
84+
$page = PageTest::createPage([
85+
[$responseA, new MockStatus(Grpc\STATUS_OK, '')],
86+
]);
87+
88+
$this->assertEquals($page->hasNextPage(), false);
89+
$page->getNextPage();
90+
}
91+
92+
/**
93+
* @expectedException Google\GAX\ValidationException
94+
* @expectedExceptionMessage pageSize argument was defined, but the method does not
95+
*/
96+
public function testNextPageMethodsFailWithPageSizeUnsupported()
97+
{
98+
$responseA = MockResponse::createPageStreamingResponse('nextPageToken1', ['resource1']);
99+
$responseB = MockResponse::createPageStreamingResponse('', ['resource2']);
100+
$page = PageTest::createPage([
101+
[$responseA, new MockStatus(Grpc\STATUS_OK, '')],
102+
[$responseB, new MockStatus(Grpc\STATUS_OK, '')],
103+
]);
104+
105+
$page->getNextPage(3);
106+
}
107+
108+
public function testPageElementMethods()
109+
{
76110
$response = MockResponse::createPageStreamingResponse('nextPageToken1',
77111
['resource1', 'resource2', 'resource3']);
78112
$page = PageTest::createPage([

tests/mocks/MockRequest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,13 @@
3535
class MockRequest
3636
{
3737
public $pageToken;
38+
public $pageSize;
3839

39-
public static function createPageStreamingRequest($pageToken)
40+
public static function createPageStreamingRequest($pageToken, $pageSize = null)
4041
{
4142
$request = new MockRequest();
4243
$request->pageToken = $pageToken;
44+
$request->pageSize = $pageSize;
4345
return $request;
4446
}
45-
46-
public function setPageSize($pageSize) {
47-
// do nothing
48-
}
4947
}

0 commit comments

Comments
 (0)