Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Nov 11, 2024
1 parent 932e36e commit fe8fe94
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 44 deletions.
10 changes: 7 additions & 3 deletions Spanner/src/SpannerClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -111,6 +112,7 @@ class SpannerClient
use ClientTrait;
use ValidateTrait;
use RequestTrait;
use RequestProcessorTrait;

const VERSION = '1.80.0';

Expand Down Expand Up @@ -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);
}

Expand Down
123 changes: 82 additions & 41 deletions Spanner/tests/Unit/SpannerClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@

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;
use Google\Cloud\Core\Testing\Snippet\Fixtures;
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;
Expand Down Expand Up @@ -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
{
Expand All @@ -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()
Expand All @@ -112,24 +127,32 @@ 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);
}),
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();

Expand All @@ -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(
Expand All @@ -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) {
Expand All @@ -185,7 +220,7 @@ public function testPagedInstanceConfigurations()
Argument::type('array')
)
->shouldBeCalled()
->willReturn($secondCall);
->willReturn($pagedListResponse2->reveal());

$configs = $this->spannerClient->instanceConfigurations();

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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(
Expand All @@ -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);
Expand Down

0 comments on commit fe8fe94

Please sign in to comment.