Skip to content

Commit 326e37f

Browse files
authored
fix: examples and tests (#1871)
1 parent 8e92159 commit 326e37f

12 files changed

Lines changed: 59 additions & 641 deletions

examples/batch.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@
5858
want to execute with keys of our choice - these
5959
keys will be reflected in the returned array.
6060
************************************************/
61-
$batch = $service->createBatch();
61+
$batch = new Google_Http_Batch($client);
6262
$optParams = array('filter' => 'free-ebooks');
63-
$req1 = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
63+
$optParams['q'] = 'Henry David Thoreau';
64+
$req1 = $service->volumes->listVolumes($optParams);
6465
$batch->add($req1, "thoreau");
65-
$req2 = $service->volumes->listVolumes('George Bernard Shaw', $optParams);
66+
$optParams['q'] = 'George Bernard Shaw';
67+
$req2 = $service->volumes->listVolumes($optParams);
6668
$batch->add($req2, "shaw");
6769

6870
/************************************************

examples/service-account.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@
5959
We're just going to make the same call as in the
6060
simple query as an example.
6161
************************************************/
62-
$optParams = array('filter' => 'free-ebooks');
63-
$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
62+
$optParams = array(
63+
'q' => 'Henry David Thoreau',
64+
'filter' => 'free-ebooks',
65+
);
66+
$results = $service->volumes->listVolumes($optParams);
6467
?>
6568

6669
<h3>Results Of Call:</h3>

examples/simple-query.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,21 @@
4747
(the query), and an array of named optional
4848
parameters.
4949
************************************************/
50-
$optParams = array('filter' => 'free-ebooks');
51-
$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
50+
$optParams = array(
51+
'q' => 'Henry David Thoreau',
52+
'filter' => 'free-ebooks',
53+
);
54+
$results = $service->volumes->listVolumes($optParams);
5255

5356
/************************************************
5457
This is an example of deferring a call.
5558
***********************************************/
5659
$client->setDefer(true);
57-
$optParams = array('filter' => 'free-ebooks');
58-
$request = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
60+
$optParams = array(
61+
'q' => 'Henry David Thoreau',
62+
'filter' => 'free-ebooks',
63+
);
64+
$request = $service->volumes->listVolumes($optParams);
5965
$resultsDeferred = $client->execute($request);
6066

6167
/************************************************

tests/BaseTest.php

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class BaseTest extends TestCase
2626
{
2727
private $key;
2828
private $client;
29-
protected $testDir = __DIR__;
3029

3130
public function getClient()
3231
{
@@ -140,21 +139,21 @@ public function tryToGetAnAccessToken(Google_Client $client)
140139

141140
private function getClientIdAndSecret()
142141
{
143-
$clientId = getenv('GCLOUD_CLIENT_ID') ?: null;
144-
$clientSecret = getenv('GCLOUD_CLIENT_SECRET') ?: null;
142+
$clientId = getenv('GOOGLE_CLIENT_ID') ?: null;
143+
$clientSecret = getenv('GOOGLE_CLIENT_SECRET') ?: null;
145144

146145
return array($clientId, $clientSecret);
147146
}
148147

149-
public function checkClientCredentials()
148+
protected function checkClientCredentials()
150149
{
151150
list($clientId, $clientSecret) = $this->getClientIdAndSecret();
152151
if (!($clientId && $clientSecret)) {
153-
$this->markTestSkipped("Test requires GCLOUD_CLIENT_ID and GCLOUD_CLIENT_SECRET to be set");
152+
$this->markTestSkipped("Test requires GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET to be set");
154153
}
155154
}
156155

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

174-
public function checkKey()
173+
protected function checkKey()
175174
{
176-
$this->key = $this->loadKey();
177-
178-
if (!strlen($this->key)) {
179-
$this->markTestSkipped("Test requires api key\nYou can create one in your developer console");
180-
return false;
181-
}
182-
}
183-
184-
public function loadKey()
185-
{
186-
if (file_exists($f = __DIR__ . DIRECTORY_SEPARATOR . '.apiKey')) {
187-
return file_get_contents($f);
175+
if (file_exists($apiKeyFile = __DIR__ . DIRECTORY_SEPARATOR . '.apiKey')) {
176+
$apiKey = file_get_contents($apiKeyFile);
177+
} elseif (!$apiKey = getenv('GOOGLE_API_KEY')) {
178+
$this->markTestSkipped(
179+
"Test requires api key\nYou can create one in your developer console"
180+
);
181+
file_put_contents($apiKeyFile, $apiKey);
188182
}
183+
$this->key = $apiKey;
189184
}
190185

191186
protected function loadExample($example)

tests/Google/ClientTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ public function testJsonConfig()
373373

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

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

@@ -690,13 +690,13 @@ public function testTokenCallback()
690690
$phpunit = $this;
691691
$called = false;
692692
$callback = function ($key, $value) use ($client, $cache, $phpunit, &$called) {
693-
// go back to the previous cache
694-
$client->setCache($cache);
695-
696693
// assert the expected keys and values
697-
$phpunit->assertContains('https---www.googleapis.com-auth-', $key);
694+
$phpunit->assertNotNull($key);
698695
$phpunit->assertNotNull($value);
699696
$called = true;
697+
698+
// go back to the previous cache
699+
$client->setCache($cache);
700700
};
701701

702702
// set the token callback to the client

tests/Google/Http/BatchTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@ public function testBatchRequest()
5959
$this->assertArrayHasKey('response-key3', $result);
6060
}
6161

62+
public function testBatchRequestWithBooksApi()
63+
{
64+
$client = $this->getClient();
65+
$batch = new Google_Http_Batch($client);
66+
$plus = new Google_Service_Plus($client);
67+
68+
$client->setUseBatch(true);
69+
$batch->add($plus->people->get('+LarryPage'), 'key1');
70+
$batch->add($plus->people->get('+LarryPage'), 'key2');
71+
$batch->add($plus->people->get('+LarryPage'), 'key3');
72+
73+
$result = $batch->execute();
74+
$this->assertArrayHasKey('response-key1', $result);
75+
$this->assertArrayHasKey('response-key2', $result);
76+
$this->assertArrayHasKey('response-key3', $result);
77+
}
78+
6279
public function testBatchRequestWithPostBody()
6380
{
6481
$this->checkToken();

0 commit comments

Comments
 (0)