Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Generation/TestNameValueProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,21 @@ public function __construct($prod, $f, $forceValues)
});
}

public function perFieldRequest(MethodDetails $method): array
public function perFieldRequest(MethodDetails $method, array $reservedNames = []): array
{
// Handle test request value generation.
$perField = static::filterFirstOneOf($method->allFields)
->map(fn ($f) => new class($this, $method, $f) {
public function __construct($prod, $method, $f)
->map(fn ($f) => new class($this, $method, $f, $reservedNames) {
public function __construct($prod, $method, $f, $reservedNames)
{
// This code is arranged in this way, as a name must not be generated
// if this field ends up not being used.
// Name generation is stateful.
// TODO: Consider refactoring this.
$this->field = $f;
$this->name = ($f->useResourceTestValue ? 'formatted_' : '') . $prod->name($f->name);
$this->var = AST::var(Helpers::toCamelCase($this->name));
$varPrefix = in_array($this->name, $reservedNames) ? 'request_' : '';
$this->var = AST::var(Helpers::toCamelCase($varPrefix . $this->name));
$astAcc = Vector::new([]);
$prod->fieldInit($method, $f, $this->var, $this->name, null, $astAcc);
$this->initCode = $astAcc;
Expand Down
2 changes: 1 addition & 1 deletion src/Generation/UnitTestsV2Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ private function testExceptionalCaseNormal(MethodDetails $method): PhpMethod
$client = AST::var(self::CLIENT_VARIABLE);
$status = AST::var('status');
$expectedExceptionMessage = AST::var('expectedExceptionMessage ');
[$requestPerField, $requestCallArgs] = $prod->perFieldRequest($method);
[$requestPerField, $requestCallArgs] = $prod->perFieldRequest($method, ['status']);
$ex = AST::var('ex');
list($initializedFields, $requestAssignment) = $this->initializeRequest($requestPerField, $method->requestType);
return AST::method($method->testExceptionMethodName)
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/ProtoTests/Basic/basic.proto
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ message RequestWithArgs {
repeated PartOfRequestA part_of_request_a = 4 [(google.api.field_behavior) = REQUIRED];
repeated PartOfRequestB part_of_request_b = 5;
PartOfRequestC part_of_request_c = 6;
int32 status = 7 [(google.api.field_behavior) = REQUIRED]; // reserved variable name
}

message Response {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
* Test including method args.
*
* @param string $aString A required field...
* @param int $status reserved variable name
*/
function method_with_args_sample(string $aString): void
function method_with_args_sample(string $aString, int $status): void
{
// Create a client.
$basicClient = new BasicClient();
Expand All @@ -43,7 +44,8 @@ function method_with_args_sample(string $aString): void
$partOfRequestA = [new PartOfRequestA()];
$request = (new RequestWithArgs())
->setAString($aString)
->setPartOfRequestA($partOfRequestA);
->setPartOfRequestA($partOfRequestA)
->setStatus($status);

// Call the API and handle any network failures.
try {
Expand All @@ -67,7 +69,8 @@ function method_with_args_sample(string $aString): void
function callSample(): void
{
$aString = '[A_STRING]';
$status = 0;

method_with_args_sample($aString);
method_with_args_sample($aString, $status);
}
// [END basic_generated_Basic_MethodWithArgs_sync]
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,11 @@ public function methodWithArgsTest()
// Mock request
$aString = 'aString-929604177';
$partOfRequestA = [];
$status = 892481550;
$request = (new RequestWithArgs())
->setAString($aString)
->setPartOfRequestA($partOfRequestA);
->setPartOfRequestA($partOfRequestA)
->setStatus($status);
$response = $gapicClient->methodWithArgs($request);
$this->assertEquals($expectedResponse, $response);
$actualRequests = $transport->popReceivedCalls();
Expand All @@ -143,6 +145,8 @@ public function methodWithArgsTest()
$this->assertProtobufEquals($aString, $actualValue);
$actualValue = $actualRequestObject->getPartOfRequestA();
$this->assertProtobufEquals($partOfRequestA, $actualValue);
$actualValue = $actualRequestObject->getStatus();
$this->assertProtobufEquals($status, $actualValue);
$this->assertTrue($transport->isExhausted());
}

Expand All @@ -167,9 +171,11 @@ public function methodWithArgsExceptionTest()
// Mock request
$aString = 'aString-929604177';
$partOfRequestA = [];
$requestStatus = 892481550;
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it worked!

$request = (new RequestWithArgs())
->setAString($aString)
->setPartOfRequestA($partOfRequestA);
->setPartOfRequestA($partOfRequestA)
->setStatus($requestStatus);
try {
$gapicClient->methodWithArgs($request);
// If the $gapicClient method call did not throw, fail the test
Expand Down