Skip to content

Commit

Permalink
fix: ensure hasEmulator client option is passed to createTransport (#594
Browse files Browse the repository at this point in the history
)
  • Loading branch information
bshaffer authored Nov 22, 2024
1 parent 21d0380 commit 13bbe8a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 33 deletions.
8 changes: 7 additions & 1 deletion src/GapicClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ private function setClientOptions(array $options)
'libName',
'libVersion',
]);

// "hasEmulator" is not a supported Client Option, but is used
// internally to determine if the client is running in emulator mode.
// Therefore, we need to remove it from the $options array before
// creating the ClientOptions.
$hasEmulator = $this->pluck('hasEmulator', $options, false) ?? false;
if ($this->isBackwardsCompatibilityMode()) {
if (is_string($options['clientConfig'])) {
// perform validation for V1 surfaces which is done in the
Expand Down Expand Up @@ -314,7 +320,7 @@ private function setClientOptions(array $options)
$transport,
$options['transportConfig'],
$options['clientCertSource'],
$options['hasEmulator'] ?? false
$hasEmulator
);
}

Expand Down
74 changes: 42 additions & 32 deletions tests/Tests/Unit/GapicClientTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1655,6 +1655,36 @@ public function testApiKeyOptionAndQuotaProject()

$response = $client->startCall('SimpleMethod', 'decodeType');
}

public function testHasEmulatorOption()
{
$gapic = new class() {
public bool $hasEmulator;

use GapicClientTrait {
buildClientOptions as public;
setClientOptions as public;
}
use ClientDefaultsTrait {
ClientDefaultsTrait::getClientDefaults insteadof GapicClientTrait;
}

private function createTransport(
string $apiEndpoint,
$transport,
$transportConfig,
callable $clientCertSource = null,
bool $hasEmulator = false
) {
$this->hasEmulator = $hasEmulator;
}
};

$options = $gapic->buildClientOptions(['hasEmulator' => true]);
$gapic->setClientOptions($options);

$this->assertTrue($gapic->hasEmulator);
}
}

class StubGapicClient
Expand All @@ -1679,15 +1709,20 @@ class StubGapicClient
startOperationsCall as public;
}
use GapicClientStubTrait;
use ClientDefaultsTrait {
ClientDefaultsTrait::getClientDefaults insteadof GapicClientTrait;
}
}

trait ClientDefaultsTrait
{
public static function getClientDefaults()
{
return [
'apiEndpoint' => 'test.address.com:443',
'serviceName' => 'test.interface.v1.api',
'clientConfig' => __DIR__ . '/testdata/test_service_client_config.json',
'descriptorsConfigPath' => __DIR__ . '/testdata/test_service_descriptor_config.php',
'gcpApiConfigPath' => __DIR__ . '/testdata/test_service_grpc_config.json',
'disableRetries' => false,
'auth' => null,
'authConfig' => null,
Expand Down Expand Up @@ -1798,28 +1833,15 @@ class RestOnlyGapicClient
buildClientOptions as public;
getTransport as public;
}

use ClientDefaultsTrait {
ClientDefaultsTrait::getClientDefaults insteadof GapicClientTrait;
}
public function __construct($options = [])
{
$options['apiEndpoint'] = 'api.example.com';
$this->setClientOptions($this->buildClientOptions($options));
}

public static function getClientDefaults()
{
return [
'apiEndpoint' => 'test.address.com:443',
'serviceName' => 'test.interface.v1.api',
'clientConfig' => __DIR__ . '/testdata/test_service_client_config.json',
'descriptorsConfigPath' => __DIR__ . '/testdata/test_service_descriptor_config.php',
'transportConfig' => [
'rest' => [
'restClientConfigPath' => __DIR__ . '/testdata/test_service_rest_client_config.php',
]
],
];
}

private static function supportedTransports()
{
return ['rest', 'fake-transport'];
Expand Down Expand Up @@ -1859,28 +1881,16 @@ class GapicV2SurfaceClient
startCall as public;
}
use GapicClientStubTrait;
use ClientDefaultsTrait {
ClientDefaultsTrait::getClientDefaults insteadof GapicClientTrait;
}

public function __construct(array $options = [])
{
$clientOptions = $this->buildClientOptions($options);
$this->setClientOptions($clientOptions);
}

public static function getClientDefaults()
{
return [
'apiEndpoint' => 'test.address.com:443',
'serviceName' => 'test.interface.v1.api',
'clientConfig' => __DIR__ . '/testdata/test_service_client_config.json',
'descriptorsConfigPath' => __DIR__ . '/testdata/test_service_descriptor_config.php',
'transportConfig' => [
'rest' => [
'restClientConfigPath' => __DIR__ . '/testdata/test_service_rest_client_config.php',
]
],
];
}

public function getAgentHeader()
{
return $this->agentHeader;
Expand Down

0 comments on commit 13bbe8a

Please sign in to comment.