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

Pass ServiceBuilder construct config into children #816

Merged
merged 1 commit into from
Dec 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 10 additions & 3 deletions src/Core/ServiceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ServiceBuilder
/**
* @var array Configuration options to be used between clients.
*/
private $config;
private $config = [];

/**
* Pass in an array of configuration options which will be shared between
Expand Down Expand Up @@ -396,10 +396,17 @@ public function translate(array $config = [])
return $this->createClient(TranslateClient::class, 'translate', $config);
}

/**
* Create the client library, or error if not installed.
*
* @param string $class The class to create.
* @param string $packageName The name of the package
* @param array $config Configuration options.
*/
private function createClient($class, $packageName, array $config = [])
{
if (class_exists($class)) {
return new $class($config ? $this->resolveConfig($config) : $this->config);
return new $class($this->resolveConfig($config));
}
throw new \Exception(sprintf(
'The google/cloud-%s package is missing and must be installed.',
Expand All @@ -419,6 +426,6 @@ private function resolveConfig(array $config)
$config['httpHandler'] = HttpHandlerFactory::build();
}

return $config;
return array_merge($this->config, $config);
}
}
102 changes: 98 additions & 4 deletions tests/unit/Core/ServiceBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
use Google\Cloud\Tests\GrpcTestTrait;
use Google\Cloud\Translate\TranslateClient;
use Google\Cloud\Vision\VisionClient;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;

/**
* @group servicebuilder
Expand All @@ -44,8 +46,7 @@ class ServiceBuilderTest extends TestCase
*/
public function testBuildsClients($serviceName, $expectedClient, array $args = [], callable $beforeCallable = null)
{
if ($beforeCallable)
{
if ($beforeCallable) {
call_user_func($beforeCallable);
}

Expand All @@ -56,14 +57,14 @@ public function testBuildsClients($serviceName, $expectedClient, array $args = [
'httpHandler' => function() {
return;
}
] + $args;
] + $args;

$localConfigClient = $serviceBuilder->$serviceName($config);

$this->assertInstanceOf($expectedClient, $localConfigClient);
}

public function testBuildsTranslateClient()
public function testTranslateClientWithApiKey()
{
$config = ['key' => 'test_key'];
$serviceBuilder = new ServiceBuilder($config);
Expand All @@ -72,6 +73,77 @@ public function testBuildsTranslateClient()
$this->assertInstanceOf(TranslateClient::class, $serviceBuilder->translate($config));
}

/**
* @dataProvider serviceProvider
*/
public function testKeyfilePathAuthPassthrough(
$serviceName,
$expectedClient,
array $args = [],
callable $beforeCallable = null
) {
if ($beforeCallable) {
call_user_func($beforeCallable);
}

$kfPath = __DIR__ .'/../fixtures/json-key-fixture.json';
$kf = json_decode(file_get_contents($kfPath), true);

$adc = getenv('GOOGLE_APPLICATION_CREDENTIALS');
putenv('GOOGLE_APPLICATION_CREDENTIALS=');

$serviceBuilder = new ServiceBuilder([
'keyFilePath' => $kfPath
]);

$client = $serviceBuilder->$serviceName($args);

$ref = new \ReflectionClass($client);
$prop = $ref->getProperty('connection');
$prop->setAccessible(true);
$conn = $prop->getValue($client);
$conn->requestWrapper()
->getCredentialsFetcher()
->fetchAuthToken($this->stub($kf));

putenv('GOOGLE_APPLICATION_CREDENTIALS='. $adc);
}

/**
* @dataProvider serviceProvider
*/
public function testKeyfileAuthPassthrough(
$serviceName, $expectedClient,
array $args = [],
callable $beforeCallable = null
) {
if ($beforeCallable) {
call_user_func($beforeCallable);
}

$kfPath = __DIR__ .'/../fixtures/json-key-fixture.json';
$kf = json_decode(file_get_contents($kfPath), true);

$adc = getenv('GOOGLE_APPLICATION_CREDENTIALS');
putenv('GOOGLE_APPLICATION_CREDENTIALS=');

$serviceBuilder = new ServiceBuilder([
'keyFile' => $kf
]);

$client = $serviceBuilder->$serviceName($args);

$ref = new \ReflectionClass($client);
$prop = $ref->getProperty('connection');
$prop->setAccessible(true);
$conn = $prop->getValue($client);
$conn->requestWrapper()
->getCredentialsFetcher()
->fetchAuthToken($this->stub($kf));

putenv('GOOGLE_APPLICATION_CREDENTIALS='. $adc);
}

public function serviceProvider()
{
return [
Expand Down Expand Up @@ -107,11 +179,33 @@ public function serviceProvider()
], [
'storage',
StorageClient::class
], [
'translate',
TranslateClient::class
], [
'vision',
VisionClient::class
]
];
}

public function stub($kf)
{
return function (RequestInterface $request) use ($kf) {
parse_str((string)$request->getBody(), $result);

$exp = [
'grant_type' => 'refresh_token',
'refresh_token' => $kf['refresh_token'],
'client_id' => $kf['client_id'],
'client_secret' => $kf['client_secret']
];

$this->assertEquals($result, $exp);

return new Response(200, [], json_encode([
'access_token' => 'foo'
]));
};
}
}
8 changes: 7 additions & 1 deletion tests/unit/fixtures/json-key-fixture.json
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
{"type":"authorized_user","client_id":"example@example.com","client_secret":"example","refresh_token":"abc","project_id":"example_project"}
{
"type": "authorized_user",
"client_id": "example@example.com",
"client_secret": "example",
"refresh_token": "abc",
"project_id": "example_project"
}