From c55baa30a65215a9d439da0fe2b268dd2858ad31 Mon Sep 17 00:00:00 2001 From: ZhouyihaiDing Date: Fri, 29 Jun 2018 16:18:15 -0700 Subject: [PATCH] move code to another SpannerClient.php --- Spanner/src/SpannerClient.php | 14 +++ Spanner/src/V1/Gapic/SpannerGapicClient.php | 13 --- .../Unit/V1/SpannerClientEnableGCPTest.php | 107 ------------------ spanner.php | 6 + 4 files changed, 20 insertions(+), 120 deletions(-) delete mode 100644 Spanner/tests/Unit/V1/SpannerClientEnableGCPTest.php create mode 100755 spanner.php diff --git a/Spanner/src/SpannerClient.php b/Spanner/src/SpannerClient.php index af63c9c8f4fe..c1af2a32b77b 100644 --- a/Spanner/src/SpannerClient.php +++ b/Spanner/src/SpannerClient.php @@ -140,6 +140,12 @@ public function __construct(array $config = []) 'projectIdRequired' => true ]; + if (isset($config['enable-gcp'])) { + $api = 'spanner'; + $conf_path = "./../../$api.grpc.config"; + $this->enableChonnectionManagement($conf_path, &$config); + } + $this->connection = new Grpc($this->configureAuthentication($config)); $this->returnInt64AsObject = $config['returnInt64AsObject']; @@ -170,6 +176,14 @@ public function __construct(array $config = []) ]); } + private function enableChonnectionManagement($conf_path, $options) { + // TODO: move this function to gax. + $conf = new \Grpc\Gcp\ApiConfig(); + $conf->mergeFromJsonString($string = file_get_contents($conf_path)); + $config = \Google\Cloud\Grpc($conf); + $options['grpc_call_invoker'] = $config->callInvoker(); + } + /** * Get a Batch Client. * diff --git a/Spanner/src/V1/Gapic/SpannerGapicClient.php b/Spanner/src/V1/Gapic/SpannerGapicClient.php index 269358b7d3b3..b6bd68dfb4b4 100644 --- a/Spanner/src/V1/Gapic/SpannerGapicClient.php +++ b/Spanner/src/V1/Gapic/SpannerGapicClient.php @@ -308,23 +308,10 @@ public static function parseName($formattedName, $template = null) */ public function __construct(array $options = []) { - if (isset($options['enable-gcp'])) { - $api = 'spanner'; - $conf_path = "./../../$api.grpc.config"; - $this->enableChonnectionManagement($conf_path, &$options); - } $clientOptions = $this->buildClientOptions($options); $this->setClientOptions($clientOptions); } - private function enableChonnectionManagement($conf_path, $options) { - // TODO: move this function to gax. - $conf = new \Grpc\Gcp\ApiConfig(); - $conf->mergeFromJsonString($string = file_get_contents($conf_path)); - $config = \Google\Cloud\Grpc($conf); - $options['grpc_call_invoker'] = $config->callInvoker(); - } - /** * Creates a new session. A session can be used to perform * transactions that read and/or modify data in a Cloud Spanner database. diff --git a/Spanner/tests/Unit/V1/SpannerClientEnableGCPTest.php b/Spanner/tests/Unit/V1/SpannerClientEnableGCPTest.php deleted file mode 100644 index 75841b9e12a3..000000000000 --- a/Spanner/tests/Unit/V1/SpannerClientEnableGCPTest.php +++ /dev/null @@ -1,107 +0,0 @@ - $this->getMockBuilder(CredentialsWrapper::class) - ->disableOriginalConstructor() - ->getMock(), - 'enable_gcp' => true, - ]; - - return new SpannerClient($options); - } - - /** - * @test - */ - public function createSessionTest() - { - $transport = $this->createTransport(); - $client = $this->createClient(['transport' => $transport]); - - $this->assertTrue($transport->isExhausted()); - - // Mock response - $name = 'name3373707'; - $expectedResponse = new Session(); - $expectedResponse->setName($name); - $transport->addResponse($expectedResponse); - - // Mock request - $formattedDatabase = $client->databaseName('[PROJECT]', '[INSTANCE]', '[DATABASE]'); - - $response = $client->createSession($formattedDatabase); - $this->assertEquals($expectedResponse, $response); - $actualRequests = $transport->popReceivedCalls(); - $this->assertSame(1, count($actualRequests)); - $actualFuncCall = $actualRequests[0]->getFuncCall(); - $actualRequestObject = $actualRequests[0]->getRequestObject(); - $this->assertSame('/google.spanner.v1.Spanner/CreateSession', $actualFuncCall); - - $actualValue = $actualRequestObject->getDatabase(); - - $this->assertProtobufEquals($formattedDatabase, $actualValue); - - $this->assertTrue($transport->isExhausted()); - } -} diff --git a/spanner.php b/spanner.php new file mode 100755 index 000000000000..951385a8fdbd --- /dev/null +++ b/spanner.php @@ -0,0 +1,6 @@ + 'ddyihai-firestore', + 'enable_gcp' => true, + ]); +