From fe8fe94b5f67fe42313b85634e55ab32556a28b9 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 11 Nov 2024 15:57:41 -0800 Subject: [PATCH] WIP --- Spanner/src/SpannerClient.php | 10 +- Spanner/tests/Unit/SpannerClientTest.php | 123 +++++++++++++++-------- 2 files changed, 89 insertions(+), 44 deletions(-) diff --git a/Spanner/src/SpannerClient.php b/Spanner/src/SpannerClient.php index 6ea25f0306db..fac9fce86f84 100644 --- a/Spanner/src/SpannerClient.php +++ b/Spanner/src/SpannerClient.php @@ -30,6 +30,7 @@ use Google\Cloud\Core\Iterator\ItemIterator; use Google\Cloud\Core\Iterator\PageIterator; use Google\Cloud\Core\ValidateTrait; +use Google\Cloud\Core\RequestProcessorTrait; use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient; use Google\Cloud\Spanner\Admin\Instance\V1\Client\InstanceAdminClient; use Google\Cloud\Spanner\Admin\Instance\V1\ListInstanceConfigOperationsRequest; @@ -111,6 +112,7 @@ class SpannerClient use ClientTrait; use ValidateTrait; use RequestTrait; + use RequestProcessorTrait; const VERSION = '1.80.0'; @@ -258,9 +260,11 @@ public function __construct(array $config = []) 'libName' => 'gccl', 'serializer' => $this->serializer, ]; - $this->spannerClient = new GapicSpannerClient($clientConfig); - $this->instanceAdminClient = new InstanceAdminClient($clientConfig); - $this->databaseAdminClient = new DatabaseAdminClient($clientConfig); + $this->spannerClient = $config['gapicSpannerClient'] ?? new GapicSpannerClient($clientConfig); + $this->instanceAdminClient = $config['gapicSpannerInstanceAdminClient'] + ?? new InstanceAdminClient($clientConfig); + $this->databaseAdminClient = $config['gapicSpannerDatabaseAdminClient'] + ?? new DatabaseAdminClient($clientConfig); $this->projectName = InstanceAdminClient::projectName($this->projectId); } diff --git a/Spanner/tests/Unit/SpannerClientTest.php b/Spanner/tests/Unit/SpannerClientTest.php index cab24412dc90..fce9eb805e42 100644 --- a/Spanner/tests/Unit/SpannerClientTest.php +++ b/Spanner/tests/Unit/SpannerClientTest.php @@ -19,6 +19,8 @@ use Google\ApiCore\OperationResponse; use Google\ApiCore\Serializer; +use Google\ApiCore\PagedListResponse; +use Google\ApiCore\Page; use Google\Cloud\Core\Int64; use Google\Cloud\Core\Iterator\ItemIterator; use Google\Cloud\Core\Testing\GrpcTestTrait; @@ -26,6 +28,10 @@ use Google\Cloud\Core\Testing\TestHelpers; use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient; use Google\Cloud\Spanner\Admin\Instance\V1\Client\InstanceAdminClient; +use Google\Cloud\Spanner\Admin\Instance\V1\ListInstanceConfigsResponse; +use Google\Cloud\Spanner\Admin\Instance\V1\ListInstancesResponse; +use Google\Cloud\Spanner\Admin\Instance\V1\InstanceConfig; +use Google\Cloud\Spanner\Admin\Instance\V1\Instance as InstanceProto; use Google\Cloud\Spanner\Batch\BatchClient; use Google\Cloud\Spanner\Bytes; use Google\Cloud\Spanner\CommitTimestamp; @@ -61,10 +67,12 @@ class SpannerClientTest extends TestCase const DATABASE = 'db'; const CONFIG = 'conf'; - private $requestHandler; private $serializer; - private $spannerClient; + private SpannerClient $spannerClient; + private $instanceAdminClient; private $directedReadOptionsIncludeReplicas; + private $operationResponse; + public function setUp(): void { @@ -81,11 +89,18 @@ public function setUp(): void ] ] ]; + + $this->instanceAdminClient = $this->prophesize(InstanceAdminClient::class); $this->spannerClient = new SpannerClient([ 'projectId' => self::PROJECT, 'credentials' => Fixtures::KEYFILE_STUB_FIXTURE(), - 'directedReadOptions' => $this->directedReadOptionsIncludeReplicas + 'directedReadOptions' => $this->directedReadOptionsIncludeReplicas, + 'gapicSpannerInstanceAdminClient' => $this->instanceAdminClient->reveal() ]); + + $this->operationResponse = $this->prophesize(OperationResponse::class); + $this->operationResponse->withResultFunction(Argument::type('callable')) + ->willReturn($this->operationResponse->reveal()); } public function testBatch() @@ -112,6 +127,24 @@ public function testBatch() */ public function testInstanceConfigurations() { + $page = $this->prophesize(Page::class); + $page->getResponseObject() + ->willReturn(new ListInstanceConfigsResponse([ + 'instance_configs' => [ + new InstanceConfig([ + 'name' => InstanceAdminClient::instanceConfigName(self::PROJECT, self::CONFIG), + 'display_name' => 'Bar' + ]), + new InstanceConfig([ + 'name' => InstanceAdminClient::instanceConfigName(self::PROJECT, self::CONFIG), + 'display_name' => 'Bat' + ]), + ] + ])); + $pagedListResponse = $this->prophesize(PagedListResponse::class); + $pagedListResponse->getPage() + ->willReturn($page->reveal()); + $this->instanceAdminClient->listInstanceConfigs( Argument::that(function ($request) { return $request->getParent() == InstanceAdminClient::projectName(self::PROJECT); @@ -119,17 +152,7 @@ public function testInstanceConfigurations() Argument::type('array') ) ->shouldBeCalled() - ->willReturn([ - 'instanceConfigs' => [ - [ - 'name' => InstanceAdminClient::instanceConfigName(self::PROJECT, self::CONFIG), - 'displayName' => 'Bar' - ], [ - 'name' => InstanceAdminClient::instanceConfigName(self::PROJECT, self::CONFIG), - 'displayName' => 'Bat' - ] - ] - ]); + ->willReturn($pagedListResponse->reveal()); $configs = $this->spannerClient->instanceConfigurations(); @@ -146,24 +169,36 @@ public function testInstanceConfigurations() */ public function testPagedInstanceConfigurations() { - $firstCall = [ - 'instanceConfigs' => [ - [ - 'name' => 'projects/foo/instanceConfigs/bar', - 'displayName' => 'Bar' + $page1 = $this->prophesize(Page::class); + $page1->getResponseObject() + ->willReturn(new ListInstanceConfigsResponse([ + 'instance_configs' => [ + new InstanceConfig([ + 'name' => 'projects/foo/instanceConfigs/bar', + 'display_name' => 'Bar' + ]) + ], + 'next_page_token' => 'fooBar' + ])); + + $pagedListResponse1 = $this->prophesize(PagedListResponse::class); + $pagedListResponse1->getPage() + ->willReturn($page1->reveal()); + + $page2 = $this->prophesize(Page::class); + $page2->getResponseObject() + ->willReturn(new ListInstanceConfigsResponse([ + 'instance_configs' => [ + new InstanceConfig([ + 'name' => 'projects/foo/instanceConfigs/bat', + 'display_name' => 'Bat' + ]) ] - ], - 'nextPageToken' => 'fooBar' - ]; + ])); - $secondCall = [ - 'instanceConfigs' => [ - [ - 'name' => 'projects/foo/instanceConfigs/bat', - 'displayName' => 'Bat' - ] - ] - ]; + $pagedListResponse2 = $this->prophesize(PagedListResponse::class); + $pagedListResponse2->getPage() + ->willReturn($page2->reveal()); $iteration = 0; $this->instanceAdminClient->listInstanceConfigs( @@ -175,7 +210,7 @@ public function testPagedInstanceConfigurations() Argument::type('array') ) ->shouldBeCalled() - ->willReturn($firstCall); + ->willReturn($pagedListResponse1->reveal()); $this->instanceAdminClient->listInstanceConfigs( Argument::that(function ($request) use (&$iteration) { @@ -185,7 +220,7 @@ public function testPagedInstanceConfigurations() Argument::type('array') ) ->shouldBeCalled() - ->willReturn($secondCall); + ->willReturn($pagedListResponse2->reveal()); $configs = $this->spannerClient->instanceConfigurations(); @@ -229,7 +264,7 @@ public function testCreateInstance() Argument::type('array') ) ->shouldBeCalled() - ->willReturn($this->getOperationResponseMock()); + ->willReturn($this->operationResponse->reveal()); $config = $this->prophesize(InstanceConfiguration::class); $config->name()->willReturn(InstanceAdminClient::instanceConfigName(self::PROJECT, self::CONFIG)); @@ -263,7 +298,7 @@ public function testCreateInstanceWithNodes() Argument::type('array') ) ->shouldBeCalled() - ->willReturn($this->getOperationResponseMock()); + ->willReturn($this->operationResponse->reveal()); $config = $this->prophesize(InstanceConfiguration::class); $config->name()->willReturn(InstanceAdminClient::instanceConfigName(self::PROJECT, self::CONFIG)); @@ -302,7 +337,7 @@ public function testCreateInstanceWithProcessingUnits() Argument::type('array') ) ->shouldBeCalled() - ->willReturn($this->getOperationResponseMock()); + ->willReturn($this->operationResponse->reveal()); $config = $this->prophesize(InstanceConfiguration::class); $config->name()->willReturn(InstanceAdminClient::instanceConfigName(self::PROJECT, self::CONFIG)); @@ -353,6 +388,17 @@ public function testInstanceWithInstanceArray() */ public function testInstances() { + $page = $this->prophesize(Page::class); + $page->getResponseObject() + ->willReturn(new ListInstancesResponse([ + 'instances' => [ + new InstanceProto(['name' => 'projects/test-project/instances/foo']), + new InstanceProto(['name' => 'projects/test-project/instances/bar']), + ] + ])); + $pagedListResponse = $this->prophesize(PagedListResponse::class); + $pagedListResponse->getPage() + ->willReturn($page->reveal()); $this->instanceAdminClient->listInstances( Argument::that(function ($request) { $this->assertEquals( @@ -364,12 +410,7 @@ public function testInstances() Argument::type('array') ) ->shouldBeCalled() - ->willReturn([ - 'instances' => [ - ['name' => 'projects/test-project/instances/foo'], - ['name' => 'projects/test-project/instances/bar'], - ] - ]); + ->willReturn($pagedListResponse->reveal()); $instances = $this->spannerClient->instances(); $this->assertInstanceOf(ItemIterator::class, $instances);