Skip to content

Commit

Permalink
fix: examples and tests (#1871)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored Jul 10, 2020
1 parent 8e92159 commit 326e37f
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 641 deletions.
8 changes: 5 additions & 3 deletions examples/batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@
want to execute with keys of our choice - these
keys will be reflected in the returned array.
************************************************/
$batch = $service->createBatch();
$batch = new Google_Http_Batch($client);
$optParams = array('filter' => 'free-ebooks');
$req1 = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
$optParams['q'] = 'Henry David Thoreau';
$req1 = $service->volumes->listVolumes($optParams);
$batch->add($req1, "thoreau");
$req2 = $service->volumes->listVolumes('George Bernard Shaw', $optParams);
$optParams['q'] = 'George Bernard Shaw';
$req2 = $service->volumes->listVolumes($optParams);
$batch->add($req2, "shaw");

/************************************************
Expand Down
7 changes: 5 additions & 2 deletions examples/service-account.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@
We're just going to make the same call as in the
simple query as an example.
************************************************/
$optParams = array('filter' => 'free-ebooks');
$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
$optParams = array(
'q' => 'Henry David Thoreau',
'filter' => 'free-ebooks',
);
$results = $service->volumes->listVolumes($optParams);
?>

<h3>Results Of Call:</h3>
Expand Down
14 changes: 10 additions & 4 deletions examples/simple-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,21 @@
(the query), and an array of named optional
parameters.
************************************************/
$optParams = array('filter' => 'free-ebooks');
$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
$optParams = array(
'q' => 'Henry David Thoreau',
'filter' => 'free-ebooks',
);
$results = $service->volumes->listVolumes($optParams);

/************************************************
This is an example of deferring a call.
***********************************************/
$client->setDefer(true);
$optParams = array('filter' => 'free-ebooks');
$request = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
$optParams = array(
'q' => 'Henry David Thoreau',
'filter' => 'free-ebooks',
);
$request = $service->volumes->listVolumes($optParams);
$resultsDeferred = $client->execute($request);

/************************************************
Expand Down
33 changes: 14 additions & 19 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class BaseTest extends TestCase
{
private $key;
private $client;
protected $testDir = __DIR__;

public function getClient()
{
Expand Down Expand Up @@ -140,21 +139,21 @@ public function tryToGetAnAccessToken(Google_Client $client)

private function getClientIdAndSecret()
{
$clientId = getenv('GCLOUD_CLIENT_ID') ?: null;
$clientSecret = getenv('GCLOUD_CLIENT_SECRET') ?: null;
$clientId = getenv('GOOGLE_CLIENT_ID') ?: null;
$clientSecret = getenv('GOOGLE_CLIENT_SECRET') ?: null;

return array($clientId, $clientSecret);
}

public function checkClientCredentials()
protected function checkClientCredentials()
{
list($clientId, $clientSecret) = $this->getClientIdAndSecret();
if (!($clientId && $clientSecret)) {
$this->markTestSkipped("Test requires GCLOUD_CLIENT_ID and GCLOUD_CLIENT_SECRET to be set");
$this->markTestSkipped("Test requires GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET to be set");
}
}

public function checkServiceAccountCredentials()
protected function checkServiceAccountCredentials()
{
if (!$f = getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
$skip = "This test requires the GOOGLE_APPLICATION_CREDENTIALS environment variable to be set\n"
Expand All @@ -171,21 +170,17 @@ public function checkServiceAccountCredentials()
return true;
}

public function checkKey()
protected function checkKey()
{
$this->key = $this->loadKey();

if (!strlen($this->key)) {
$this->markTestSkipped("Test requires api key\nYou can create one in your developer console");
return false;
}
}

public function loadKey()
{
if (file_exists($f = __DIR__ . DIRECTORY_SEPARATOR . '.apiKey')) {
return file_get_contents($f);
if (file_exists($apiKeyFile = __DIR__ . DIRECTORY_SEPARATOR . '.apiKey')) {
$apiKey = file_get_contents($apiKeyFile);
} elseif (!$apiKey = getenv('GOOGLE_API_KEY')) {
$this->markTestSkipped(
"Test requires api key\nYou can create one in your developer console"
);
file_put_contents($apiKeyFile, $apiKey);
}
$this->key = $apiKey;
}

protected function loadExample($example)
Expand Down
12 changes: 6 additions & 6 deletions tests/Google/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public function testJsonConfig()

public function testIniConfig()
{
$config = parse_ini_file($this->testDir . "/config/test.ini");
$config = parse_ini_file(__DIR__ . '/../config/test.ini');
$client = new Google_Client($config);

$this->assertEquals('My Test application', $client->getConfig('application_name'));
Expand Down Expand Up @@ -663,7 +663,7 @@ public function testBadSubjectThrowsException()
$this->fail('no exception thrown');
} catch (GuzzleHttp\Exception\ClientException $e) {
$response = $e->getResponse();
$this->assertContains('Invalid impersonation prn email address', (string) $response->getBody());
$this->assertContains('Invalid impersonation', (string) $response->getBody());
}
}

Expand All @@ -690,13 +690,13 @@ public function testTokenCallback()
$phpunit = $this;
$called = false;
$callback = function ($key, $value) use ($client, $cache, $phpunit, &$called) {
// go back to the previous cache
$client->setCache($cache);

// assert the expected keys and values
$phpunit->assertContains('https---www.googleapis.com-auth-', $key);
$phpunit->assertNotNull($key);
$phpunit->assertNotNull($value);
$called = true;

// go back to the previous cache
$client->setCache($cache);
};

// set the token callback to the client
Expand Down
17 changes: 17 additions & 0 deletions tests/Google/Http/BatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ public function testBatchRequest()
$this->assertArrayHasKey('response-key3', $result);
}

public function testBatchRequestWithBooksApi()
{
$client = $this->getClient();
$batch = new Google_Http_Batch($client);
$plus = new Google_Service_Plus($client);

$client->setUseBatch(true);
$batch->add($plus->people->get('+LarryPage'), 'key1');
$batch->add($plus->people->get('+LarryPage'), 'key2');
$batch->add($plus->people->get('+LarryPage'), 'key3');

$result = $batch->execute();
$this->assertArrayHasKey('response-key1', $result);
$this->assertArrayHasKey('response-key2', $result);
$this->assertArrayHasKey('response-key3', $result);
}

public function testBatchRequestWithPostBody()
{
$this->checkToken();
Expand Down
Loading

0 comments on commit 326e37f

Please sign in to comment.