Skip to content

Commit f4a8ef0

Browse files
authored
removes stash logic, adds caching test for MemoryCache (#1085)
1 parent c7c1ec9 commit f4a8ef0

File tree

7 files changed

+124
-20
lines changed

7 files changed

+124
-20
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ before_install:
3434
- composer self-update
3535

3636
install:
37+
- if [[ "$TRAVIS_PHP_VERSION" == "5.4" ]]; then composer remove --dev cache/filesystem-adapter; fi
3738
- composer install
3839
- composer require guzzlehttp/guzzle:$GUZZLE_VERSION
3940

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
"squizlabs/php_codesniffer": "~2.3",
2121
"symfony/dom-crawler": "~2.1",
2222
"symfony/css-selector": "~2.1",
23-
"tedivm/stash": "^0.14.1"
23+
"cache/filesystem-adapter": "^0.3.2"
2424
},
2525
"suggest": {
26-
"tedivm/stash": "For caching certs and tokens (using Google_Client::setCache)"
26+
"cache/filesystem-adapter": "For caching certs and tokens (using Google_Client::setCache)"
2727
},
2828
"autoload": {
2929
"psr-0": {

src/Google/AccessToken/Verify.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ public function __construct(ClientInterface $http = null, CacheItemPoolInterface
5757
}
5858

5959
if (is_null($cache)) {
60-
if (class_exists('Stash\Pool')) {
61-
$cache = new Pool(new FileSystem);
62-
} else {
63-
$cache = new MemoryCacheItemPool;
64-
}
60+
$cache = new MemoryCacheItemPool;
6561
}
6662

6763
$this->http = $http;

src/Google/Client.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,14 +1003,7 @@ protected function createDefaultLogger()
10031003

10041004
protected function createDefaultCache()
10051005
{
1006-
// use filesystem cache by default if tedivm/stash exists
1007-
if (class_exists('Stash\Pool')) {
1008-
$cache = new Stash\Pool(new Stash\Driver\FileSystem);
1009-
} else {
1010-
$cache = new MemoryCacheItemPool;
1011-
}
1012-
1013-
return $cache;
1006+
return new MemoryCacheItemPool;
10141007
}
10151008

10161009
/**

tests/BaseTest.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717

1818
use GuzzleHttp\ClientInterface;
1919
use Symfony\Component\DomCrawler\Crawler;
20-
use Stash\Driver\FileSystem;
21-
use Stash\Pool;
20+
use League\Flysystem\Adapter\Local;
21+
use League\Flysystem\Filesystem;
22+
use Cache\Adapter\Filesystem\FilesystemCachePool;
2223

2324
class BaseTest extends PHPUnit_Framework_TestCase
2425
{
@@ -39,8 +40,11 @@ public function getClient()
3940

4041
public function getCache($path = null)
4142
{
42-
$path = $path ?: sys_get_temp_dir().'/google-api-php-client-tests';
43-
return new Pool(new FileSystem(['path' => $path]));
43+
$path = $path ?: sys_get_temp_dir().'/google-api-php-client-tests/';
44+
$filesystemAdapter = new Local($path);
45+
$filesystem = new Filesystem($filesystemAdapter);
46+
47+
return new FilesystemCachePool($filesystem);
4448
}
4549

4650
private function createClient()
@@ -81,7 +85,10 @@ private function createClient()
8185
list($clientId, $clientSecret) = $this->getClientIdAndSecret();
8286
$client->setClientId($clientId);
8387
$client->setClientSecret($clientSecret);
84-
$client->setCache($this->getCache());
88+
if (version_compare(PHP_VERSION, '5.5', '>=')) {
89+
$client->setCache($this->getCache());
90+
}
91+
8592

8693
return $client;
8794
}
@@ -224,6 +231,13 @@ public function onlyGuzzle6()
224231
}
225232
}
226233

234+
public function onlyPhp55AndAbove()
235+
{
236+
if (version_compare(PHP_VERSION, '5.5', '<')) {
237+
$this->markTestSkipped('PHP 5.5 and above only');
238+
}
239+
}
240+
227241
public function onlyGuzzle5()
228242
{
229243
if (!$this->isGuzzle5()) {

tests/Google/CacheTest.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
21+
use GuzzleHttp\Client;
22+
use Google\Auth\Cache\MemoryCacheItemPool;
23+
24+
class Google_CacheTest extends BaseTest
25+
{
26+
public function testInMemoryCache()
27+
{
28+
$this->checkServiceAccountCredentials();
29+
30+
$client = $this->getClient();
31+
$client->useApplicationDefaultCredentials();
32+
$client->setAccessType('offline');
33+
$client->setScopes(['https://www.googleapis.com/auth/drive.readonly']);
34+
$client->setCache(new MemoryCacheItemPool);
35+
36+
/* Refresh token when expired */
37+
if ($client->isAccessTokenExpired()) {
38+
$client->refreshTokenWithAssertion();
39+
}
40+
41+
/* Make a service call */
42+
$service = new Google_Service_Drive($client);
43+
$files = $service->files->listFiles();
44+
$this->assertInstanceOf('Google_Service_Drive_FileList', $files);
45+
}
46+
47+
public function testFileCache()
48+
{
49+
$this->onlyPhp55AndAbove();
50+
$this->checkServiceAccountCredentials();
51+
52+
$client = new Google_Client();
53+
$client->useApplicationDefaultCredentials();
54+
$client->setScopes(['https://www.googleapis.com/auth/drive.readonly']);
55+
// filecache with new cache dir
56+
$cache = $this->getCache(sys_get_temp_dir() . '/cloud-samples-tests-php-cache-test/');
57+
$client->setCache($cache);
58+
59+
$token1 = null;
60+
$client->setTokenCallback(function($cacheKey, $accessToken) use ($cache, &$token1) {
61+
$token1 = $accessToken;
62+
$cacheItem = $cache->getItem($cacheKey);
63+
// expire the item
64+
$cacheItem->expiresAt(new DateTime('now -1 second'));
65+
$cache->save($cacheItem);
66+
67+
$cacheItem2 = $cache->getItem($cacheKey);
68+
});
69+
70+
/* Refresh token when expired */
71+
if ($client->isAccessTokenExpired()) {
72+
$client->refreshTokenWithAssertion();
73+
}
74+
75+
/* Make a service call */
76+
$service = new Google_Service_Drive($client);
77+
$files = $service->files->listFiles();
78+
$this->assertInstanceOf('Google_Service_Drive_FileList', $files);
79+
80+
sleep(1);
81+
82+
// make sure the token expires
83+
$client = new Google_Client();
84+
$client->useApplicationDefaultCredentials();
85+
$client->setScopes(['https://www.googleapis.com/auth/drive.readonly']);
86+
$client->setCache($cache);
87+
$token2 = null;
88+
$client->setTokenCallback(function($cacheKey, $accessToken) use (&$token2) {
89+
$token2 = $accessToken;
90+
});
91+
92+
/* Make another service call */
93+
$service = new Google_Service_Drive($client);
94+
$files = $service->files->listFiles();
95+
$this->assertInstanceOf('Google_Service_Drive_FileList', $files);
96+
97+
$this->assertNotEquals($token1, $token2);
98+
}
99+
}

tests/Google/ClientTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ public function testBadSubjectThrowsException()
527527

528528
public function testTokenCallback()
529529
{
530+
$this->onlyPhp55AndAbove();
530531
$this->checkToken();
531532

532533
$client = $this->getClient();

0 commit comments

Comments
 (0)