Skip to content

Commit 60be60f

Browse files
committed
fix: prevent reserved variable names in tests
1 parent e667743 commit 60be60f

4 files changed

Lines changed: 13 additions & 5 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/GoldenUpdateMain.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ class GoldenUpdateMain
123123
'protoPath' => 'DiregapicPaginated/diregapic-paginated.proto',
124124
'transport' => 'rest',
125125
'migrationMode' => MigrationMode::MIGRATING
126+
],
127+
21 => [
128+
'name' => 'Keywords',
129+
'protoPath' => 'Keywords/keywords.proto',
130+
'migrationMode' => MigrationMode::NEW_SURFACE_ONLY,
126131
]
127132
];
128133

tests/Unit/ProtoTests/Keywords/keywords.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ option php_namespace = "Testing\\Keywords";
66

77
import "google/api/annotations.proto";
88
import "google/api/client.proto";
9+
import "google/api/field_behavior.proto";
910
import "google/api/resource.proto";
1011

1112
service Keywords {
@@ -39,6 +40,7 @@ message Request {
3940
string function = 1 [(google.api.resource_reference).type = "keywords.example.com/Resource"];
4041
int32 switch = 2;
4142
string request = 3; // Also check a field named the same as the enclosing message.
43+
int32 status = 4 [(google.api.field_behavior) = REQUIRED]; // check words in tests
4244
}
4345

4446
message Response{

0 commit comments

Comments
 (0)