Skip to content

Commit ac546b1

Browse files
authored
fix: prevent reserved variable names in tests (#785)
1 parent e667743 commit ac546b1

5 files changed

Lines changed: 21 additions & 10 deletions

File tree

src/Generation/TestNameValueProducer.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,21 @@ public function __construct($prod, $f, $forceValues)
9494
});
9595
}
9696

97-
public function perFieldRequest(MethodDetails $method): array
97+
public function perFieldRequest(MethodDetails $method, array $reservedNames = []): array
9898
{
9999
// Handle test request value generation.
100100
$perField = static::filterFirstOneOf($method->allFields)
101-
->map(fn ($f) => new class($this, $method, $f) {
102-
public function __construct($prod, $method, $f)
101+
->map(fn ($f) => new class($this, $method, $f, $reservedNames) {
102+
public function __construct($prod, $method, $f, $reservedNames)
103103
{
104104
// This code is arranged in this way, as a name must not be generated
105105
// if this field ends up not being used.
106106
// Name generation is stateful.
107107
// TODO: Consider refactoring this.
108108
$this->field = $f;
109109
$this->name = ($f->useResourceTestValue ? 'formatted_' : '') . $prod->name($f->name);
110-
$this->var = AST::var(Helpers::toCamelCase($this->name));
110+
$varPrefix = in_array($this->name, $reservedNames) ? 'request_' : '';
111+
$this->var = AST::var(Helpers::toCamelCase($varPrefix . $this->name));
111112
$astAcc = Vector::new([]);
112113
$prod->fieldInit($method, $f, $this->var, $this->name, null, $astAcc);
113114
$this->initCode = $astAcc;

src/Generation/UnitTestsV2Generator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ private function testExceptionalCaseNormal(MethodDetails $method): PhpMethod
309309
$client = AST::var(self::CLIENT_VARIABLE);
310310
$status = AST::var('status');
311311
$expectedExceptionMessage = AST::var('expectedExceptionMessage ');
312-
[$requestPerField, $requestCallArgs] = $prod->perFieldRequest($method);
312+
[$requestPerField, $requestCallArgs] = $prod->perFieldRequest($method, ['status']);
313313
$ex = AST::var('ex');
314314
list($initializedFields, $requestAssignment) = $this->initializeRequest($requestPerField, $method->requestType);
315315
return AST::method($method->testExceptionMethodName)

tests/Unit/ProtoTests/Basic/basic.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ message RequestWithArgs {
4848
repeated PartOfRequestA part_of_request_a = 4 [(google.api.field_behavior) = REQUIRED];
4949
repeated PartOfRequestB part_of_request_b = 5;
5050
PartOfRequestC part_of_request_c = 6;
51+
int32 status = 7 [(google.api.field_behavior) = REQUIRED]; // reserved variable name
5152
}
5253

5354
message Response {

tests/Unit/ProtoTests/Basic/out/samples/BasicClient/method_with_args.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333
* Test including method args.
3434
*
3535
* @param string $aString A required field...
36+
* @param int $status reserved variable name
3637
*/
37-
function method_with_args_sample(string $aString): void
38+
function method_with_args_sample(string $aString, int $status): void
3839
{
3940
// Create a client.
4041
$basicClient = new BasicClient();
@@ -43,7 +44,8 @@ function method_with_args_sample(string $aString): void
4344
$partOfRequestA = [new PartOfRequestA()];
4445
$request = (new RequestWithArgs())
4546
->setAString($aString)
46-
->setPartOfRequestA($partOfRequestA);
47+
->setPartOfRequestA($partOfRequestA)
48+
->setStatus($status);
4749

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

71-
method_with_args_sample($aString);
74+
method_with_args_sample($aString, $status);
7275
}
7376
// [END basic_generated_Basic_MethodWithArgs_sync]

tests/Unit/ProtoTests/Basic/out/tests/Unit/Client/BasicClientTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,11 @@ public function methodWithArgsTest()
129129
// Mock request
130130
$aString = 'aString-929604177';
131131
$partOfRequestA = [];
132+
$status = 892481550;
132133
$request = (new RequestWithArgs())
133134
->setAString($aString)
134-
->setPartOfRequestA($partOfRequestA);
135+
->setPartOfRequestA($partOfRequestA)
136+
->setStatus($status);
135137
$response = $gapicClient->methodWithArgs($request);
136138
$this->assertEquals($expectedResponse, $response);
137139
$actualRequests = $transport->popReceivedCalls();
@@ -143,6 +145,8 @@ public function methodWithArgsTest()
143145
$this->assertProtobufEquals($aString, $actualValue);
144146
$actualValue = $actualRequestObject->getPartOfRequestA();
145147
$this->assertProtobufEquals($partOfRequestA, $actualValue);
148+
$actualValue = $actualRequestObject->getStatus();
149+
$this->assertProtobufEquals($status, $actualValue);
146150
$this->assertTrue($transport->isExhausted());
147151
}
148152

@@ -167,9 +171,11 @@ public function methodWithArgsExceptionTest()
167171
// Mock request
168172
$aString = 'aString-929604177';
169173
$partOfRequestA = [];
174+
$requestStatus = 892481550;
170175
$request = (new RequestWithArgs())
171176
->setAString($aString)
172-
->setPartOfRequestA($partOfRequestA);
177+
->setPartOfRequestA($partOfRequestA)
178+
->setStatus($requestStatus);
173179
try {
174180
$gapicClient->methodWithArgs($request);
175181
// If the $gapicClient method call did not throw, fail the test

0 commit comments

Comments
 (0)