|
| 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 | +} |
0 commit comments