diff --git a/src/GapicClientTrait.php b/src/GapicClientTrait.php index a3f75e8a2..f326937d1 100644 --- a/src/GapicClientTrait.php +++ b/src/GapicClientTrait.php @@ -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 @@ -314,7 +320,7 @@ private function setClientOptions(array $options) $transport, $options['transportConfig'], $options['clientCertSource'], - $options['hasEmulator'] ?? false + $hasEmulator ); } diff --git a/tests/Tests/Unit/GapicClientTraitTest.php b/tests/Tests/Unit/GapicClientTraitTest.php index a009214a6..3c6b3e6c1 100644 --- a/tests/Tests/Unit/GapicClientTraitTest.php +++ b/tests/Tests/Unit/GapicClientTraitTest.php @@ -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 @@ -1679,7 +1709,13 @@ class StubGapicClient startOperationsCall as public; } use GapicClientStubTrait; + use ClientDefaultsTrait { + ClientDefaultsTrait::getClientDefaults insteadof GapicClientTrait; + } +} +trait ClientDefaultsTrait +{ public static function getClientDefaults() { return [ @@ -1687,7 +1723,6 @@ public static function getClientDefaults() '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, @@ -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']; @@ -1859,6 +1881,9 @@ class GapicV2SurfaceClient startCall as public; } use GapicClientStubTrait; + use ClientDefaultsTrait { + ClientDefaultsTrait::getClientDefaults insteadof GapicClientTrait; + } public function __construct(array $options = []) { @@ -1866,21 +1891,6 @@ public function __construct(array $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;