Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure hasEmulator client option is passed to createTransport #594

Merged
merged 4 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 = (bool) $this->pluck('hasEmulator', $options, 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