Skip to content

Commit f90ab28

Browse files
authored
fix: ensure compute request build parameters have the operation ID last (#625)
1 parent 81ec910 commit f90ab28

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

src/OperationResponse.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -473,16 +473,28 @@ public function getMetadata()
473473
*/
474474
private function operationsCall(string $method, ?string $requestClass)
475475
{
476-
$args = array_merge([$this->getName()], array_values($this->additionalArgs));
477-
if ($requestClass) {
478-
if (!method_exists($requestClass, 'build')) {
479-
throw new LogicException('Request class must support the static build method');
476+
// V1 GAPIC clients have an empty $requestClass
477+
if (empty($requestClass)) {
478+
if ($this->additionalArgs) {
479+
return $this->operationsClient->$method(
480+
$this->getName(),
481+
...array_values($this->additionalArgs)
482+
);
480483
}
481-
$request = call_user_func_array($requestClass . '::build', $args);
482-
$args = [$request];
484+
return $this->operationsClient->$method($this->getName());
483485
}
484486

485-
return call_user_func_array([$this->operationsClient, $method], $args);
487+
if (!method_exists($requestClass, 'build')) {
488+
throw new LogicException('Request class must support the static build method');
489+
}
490+
// In V2 of Compute, the Request "build" methods contain the operation ID last instead
491+
// of first. Compute is the only API which uses $additionalArgs, so switching the order
492+
// will not break anything.
493+
$request = $requestClass::build(...array_merge(
494+
array_values($this->additionalArgs),
495+
[$this->getName()]
496+
));
497+
return $this->operationsClient->$method($request);
486498
}
487499

488500
private function canHaveResult()

tests/Unit/OperationResponseTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,14 @@ public function testNewSurfaceCustomOperation()
274274
$phpunit->assertEquals('test-123', $request->name);
275275
$phpunit->assertEquals('arg2', $request->arg2);
276276
$phpunit->assertEquals('arg3', $request->arg3);
277-
return new \stdClass();
277+
return new class {
278+
public function getDone() {
279+
return true;
280+
}
281+
public function getError() {
282+
return false;
283+
}
284+
};
278285
});
279286
$operationClient->cancelNewSurfaceOperation(Argument::type(Client\CancelOperationRequest::class))
280287
->shouldBeCalledOnce()
@@ -311,6 +318,11 @@ public function testNewSurfaceCustomOperation()
311318
// Test getOperationMethod
312319
$operationResponse->reload();
313320

321+
// Test operationStatusMethod and operationStatusDoneValue
322+
$this->assertTrue($operationResponse->isDone());
323+
324+
$this->assertTrue($operationResponse->operationSucceeded());
325+
314326
// test cancelOperationMethod
315327
$operationResponse->cancel();
316328

tests/Unit/testdata/src/CustomOperationClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ abstract class BaseOperationRequest
2727
public string $arg2;
2828
public string $arg3;
2929

30-
public static function build(string $name, string $arg2, string $arg3): static
30+
public static function build(string $arg2, string $arg3, string $name): static
3131
{
3232
$request = new static();
3333
$request->name = $name;

0 commit comments

Comments
 (0)