Skip to content

Commit b643adf

Browse files
authored
fix: use responseType for custom operations (#413)
1 parent e879d2b commit b643adf

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

src/GapicClientTrait.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,10 @@ private function startApiCall(
589589
$optionalArgs,
590590
$request,
591591
$this->getOperationsClient(),
592-
$interfaceName
592+
$interfaceName,
593+
// Custom operations will define their own operation response type, whereas standard
594+
// LRO defaults to the same type.
595+
$method['responseType'] ?? null
593596
);
594597
}
595598

tests/Tests/Unit/GapicClientTraitTest.php

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
use Google\ApiCore\ServerStream;
4747
use Google\ApiCore\Testing\MockRequest;
4848
use Google\ApiCore\Testing\MockRequestBody;
49+
use Google\ApiCore\Testing\MockResponse;
4950
use Google\ApiCore\Transport\GrpcFallbackTransport;
5051
use Google\ApiCore\Transport\GrpcTransport;
5152
use Google\ApiCore\Transport\RestTransport;
@@ -271,6 +272,59 @@ public function testStartApiCallOperation()
271272
$this->assertEquals($expectedResponse, $response);
272273
}
273274

275+
public function testStartApiCallCustomOperation()
276+
{
277+
$header = AgentHeader::buildAgentHeader([]);
278+
$retrySettings = $this->getMockBuilder(RetrySettings::class)
279+
->disableOriginalConstructor()
280+
->getMock();
281+
282+
$longRunningDescriptors = [
283+
'callType' => Call::LONGRUNNING_CALL,
284+
'responseType' => 'Google\ApiCore\Testing\MockResponse',
285+
'longRunning' => [
286+
'operationReturnType' => 'operationType',
287+
'metadataReturnType' => 'metadataType',
288+
'initialPollDelayMillis' => 100,
289+
'pollDelayMultiplier' => 1.0,
290+
'maxPollDelayMillis' => 200,
291+
'totalPollTimeoutMillis' => 300,
292+
]
293+
];
294+
$expectedPromise = new FulfilledPromise(new MockResponse());
295+
$transport = $this->getMockBuilder(TransportInterface::class)->getMock();
296+
$transport->expects($this->once())
297+
->method('startUnaryCall')
298+
->will($this->returnValue($expectedPromise));
299+
$credentialsWrapper = CredentialsWrapper::build([]);
300+
$client = new GapicClientTraitOperationsStub();
301+
$client->set('transport', $transport);
302+
$client->set('credentialsWrapper', $credentialsWrapper);
303+
$client->set('agentHeader', $header);
304+
$client->set('retrySettings', ['method' => $retrySettings]);
305+
$client->set('descriptors', ['method' => $longRunningDescriptors]);
306+
$operationsClient = $this->getMockBuilder(OperationsClient::class)
307+
->disableOriginalConstructor()
308+
->setMethodsExcept(['validate'])
309+
->getMock();
310+
$client->set('operationsClient', $operationsClient);
311+
312+
$request = new MockRequest();
313+
$response = $client->call('startApiCall', [
314+
'method',
315+
/* interfaceName */ null,
316+
$request,
317+
])->wait();
318+
319+
$expectedResponse = new OperationResponse(
320+
'',
321+
$operationsClient,
322+
$longRunningDescriptors['longRunning'] + ['lastProtoResponse' => new MockResponse()]
323+
);
324+
325+
$this->assertEquals($expectedResponse, $response);
326+
}
327+
274328
/**
275329
* @dataProvider startApiCallExceptions
276330
*/
@@ -339,7 +393,7 @@ public function testStartApiCallUnary()
339393
->getMock();
340394
$unaryDescriptors = [
341395
'callType' => Call::UNARY_CALL,
342-
'responseType' => 'Google\Longrunning\Operation::class'
396+
'responseType' => 'Google\Longrunning\Operation'
343397
];
344398
$expectedPromise = new FulfilledPromise(new Operation());
345399
$transport = $this->getMockBuilder(TransportInterface::class)->getMock();
@@ -370,7 +424,7 @@ public function testStartApiCallPaged()
370424
->getMock();
371425
$pagedDescriptors = [
372426
'callType' => Call::PAGINATED_CALL,
373-
'responseType' => 'Google\Longrunning\ListOperationsResponse::class',
427+
'responseType' => 'Google\Longrunning\ListOperationsResponse',
374428
'pageStreaming' => [
375429
'requestPageTokenGetMethod' => 'getPageToken',
376430
'requestPageTokenSetMethod' => 'setPageToken',

0 commit comments

Comments
 (0)