From 42fc696a4888efbad4adb11818dc9add80e8042f Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 14 Jul 2016 14:27:18 -0700 Subject: [PATCH 001/301] adds auth code flow to README (#989) --- README.md | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index e307595e6..0f8ad5f70 100644 --- a/README.md +++ b/README.md @@ -83,27 +83,37 @@ foreach ($results as $item) { > An example of this can be seen in [`examples/simple-file-upload.php`](examples/simple-file-upload.php). -**NOTE:** If you are using Google App Engine or Google Compute Engine, you can skip steps 1-3, as Application Default Credentials are included automatically when `useApplicationDefaultCredentials` is called. - 1. Follow the instructions to [Create Web Application Credentials](https://developers.google.com/api-client-library/php/auth/web-app#creatingcred) 1. Download the JSON credentials -1. Set the path to these credentials using the `GOOGLE_APPLICATION_CREDENTIALS` environment variable: +1. Set the path to these credentials using `Google_Client::setAuthConfig`: ```php - putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json'); + $client = new Google_Client(); + $client->setAuthConfig('/path/to/client_credentials.json'); ``` -1. Tell the Google client to use your service account credentials to authenticate: +1. Set the scopes required for the API you are going to call ```php - $client = new Google_Client(); - $client->useApplicationDefaultCredentials(); + $client->addScope(Google_Service_Drive::DRIVE); ``` -1. If you have delegated domain-wide access to the service account and you want to impersonate a user account, specify the email address of the user account using the method setSubject: +1. Set your application's redirect URI ```php - $ client->setSubject($user_to_impersonate); + // Your redirect URI can be any registered URI, but in this example + // we redirect back to this same page + $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; + $client->setRedirectUri($redirect_uri); + ``` + +1. In the script handling the redirect URI, exchange the authorization code for an access token: + + ```php + if (isset($_GET['code'])) { + $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); + $client->setAccessToken($token); + } ``` ### Authentication with Service Accounts ### @@ -189,7 +199,7 @@ $request = new Google_Service_Datastore_RunQueryRequest(['query' => $query]); $response = $datastore->projects->runQuery('YOUR_DATASET_ID', $request); ``` -However, as each property of the JSON API has a corresponding generated class, the above code could also be written lile this: +However, as each property of the JSON API has a corresponding generated class, the above code could also be written like this: ```php // create the datastore service class From 04ba628b4df4032ecdedd28cb5ce30fad79a0b9b Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 15 Jul 2016 15:27:59 -0700 Subject: [PATCH 002/301] allows string tokens in revoke method (#993) --- src/Google/AccessToken/Revoke.php | 16 ++++++++------- tests/Google/AccessToken/RevokeTest.php | 26 +++++++++---------------- 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/Google/AccessToken/Revoke.php b/src/Google/AccessToken/Revoke.php index 807a6d90f..7acaea006 100644 --- a/src/Google/AccessToken/Revoke.php +++ b/src/Google/AccessToken/Revoke.php @@ -45,18 +45,20 @@ public function __construct(ClientInterface $http = null) * Revoke an OAuth2 access token or refresh token. This method will revoke the current access * token, if a token isn't provided. * - * @param string|null $token The token (access token or a refresh token) that should be revoked. + * @param string|array $token The token (access token or a refresh token) that should be revoked. * @return boolean Returns True if the revocation was successful, otherwise False. */ - public function revokeToken(array $token) + public function revokeToken($token) { - if (isset($token['refresh_token'])) { - $tokenString = $token['refresh_token']; - } else { - $tokenString = $token['access_token']; + if (is_array($token)) { + if (isset($token['refresh_token'])) { + $token = $token['refresh_token']; + } else { + $token = $token['access_token']; + } } - $body = Psr7\stream_for(http_build_query(array('token' => $tokenString))); + $body = Psr7\stream_for(http_build_query(array('token' => $token))); $request = new Request( 'POST', Google_Client::OAUTH2_REVOKE_URI, diff --git a/tests/Google/AccessToken/RevokeTest.php b/tests/Google/AccessToken/RevokeTest.php index 0a2ee11df..7ced6befd 100644 --- a/tests/Google/AccessToken/RevokeTest.php +++ b/tests/Google/AccessToken/RevokeTest.php @@ -30,11 +30,11 @@ public function testRevokeAccess() $token = ''; $response = $this->getMock('Psr\Http\Message\ResponseInterface'); - $response->expects($this->exactly(2)) + $response->expects($this->exactly(3)) ->method('getStatusCode') ->will($this->returnValue(200)); $http = $this->getMock('GuzzleHttp\ClientInterface'); - $http->expects($this->exactly(2)) + $http->expects($this->exactly(3)) ->method('send') ->will($this->returnCallback( function ($request) use (&$token, $response) { @@ -49,13 +49,13 @@ function ($request) use (&$token, $response) { if ($this->isGuzzle5()) { $requestToken = null; $request = $this->getMock('GuzzleHttp\Message\RequestInterface'); - $request->expects($this->exactly(2)) + $request->expects($this->exactly(3)) ->method('getBody') ->will($this->returnCallback( function () use (&$requestToken) { return 'token='.$requestToken; })); - $http->expects($this->exactly(2)) + $http->expects($this->exactly(3)) ->method('createRequest') ->will($this->returnCallback( function ($method, $url, $params) use (&$requestToken, $request) { @@ -88,19 +88,11 @@ function ($method, $url, $params) use (&$requestToken, $request) { ); $this->assertTrue($revoke->revokeToken($t)); $this->assertEquals($refreshToken, $token); - } - public function testInvalidStringToken() - { - $phpVersion = phpversion(); - if ('7' === $phpVersion[0]) { - // primitive type hints actually throw exceptions in PHP7 - $this->setExpectedException('TypeError'); - } else { - $this->setExpectedException('PHPUnit_Framework_Error'); - } - // Test with string token - $revoke = new Google_AccessToken_Revoke(); - $revoke->revokeToken('ACCESS_TOKEN'); + // Test with token string. + $revoke = new Google_AccessToken_Revoke($http); + $t = $accessToken; + $this->assertTrue($revoke->revokeToken($t)); + $this->assertEquals($accessToken, $token); } } From e21a5972bc72d8116a52e7b5f5777c06b30ee160 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 21 Jul 2016 11:40:47 -0700 Subject: [PATCH 003/301] tag client services (#1000) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index be5bdaf1e..312c7b437 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "require": { "php": ">=5.4", "google/auth": "0.9", - "google/apiclient-services": "*@dev", + "google/apiclient-services": "^0.5", "firebase/php-jwt": "~2.0|~3.0", "monolog/monolog": "^1.17", "phpseclib/phpseclib": "~2.0", From 18dd5534280318598e8770cb7929e9a542e91c0d Mon Sep 17 00:00:00 2001 From: Timothy Choi Date: Fri, 22 Jul 2016 02:41:10 +0800 Subject: [PATCH 004/301] Fix incorrect function call in UPGRADING.md (#996) --- UPGRADING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UPGRADING.md b/UPGRADING.md index f654570a4..230297000 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -175,7 +175,7 @@ $client->getAuth() **After** ```php -$client->useDefaultApplicationCredentials(); +$client->useApplicationDefaultCredentials(); $client->addScope('https://www.googleapis.com/auth/sqlservice.admin'); ``` From c157f563f17b6c50668c882c4f9b2fd889569f69 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 27 Jul 2016 14:24:51 -0700 Subject: [PATCH 005/301] add issue template and link to CONTRIBUTING.md in README (#1003) --- .github/ISSUE_TEMPLATE.md | 8 ++++++++ README.md | 8 ++++---- 2 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 .github/ISSUE_TEMPLATE.md diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 000000000..47cb46145 --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,8 @@ +**Heads up!** + +We appreciate any bug reports or other contributions, but please note that this issue +tracker is only for this client library. We do not maintain the APIs that this client +library talks to. If you have an issue or questions with how to use a particular API, +you may be better off posting on Stackoverflow under the `google-api` tag. + +Thank you! diff --git a/README.md b/README.md index 0f8ad5f70..e71003449 100644 --- a/README.md +++ b/README.md @@ -304,6 +304,10 @@ One additional step is required in Charles to view SSL requests. Go to **Charles YouTube: https://github.com/youtube/api-samples/tree/master/php +## How Do I Contribute? ## + +Please see the [contributing](CONTRIBUTING.md) page for more information. In particular, we love pull requests - but please make sure to sign the [contributor license agreement](https://developers.google.com/api-client-library/php/contribute). + ## Frequently Asked Questions ## ### What do I do if something isn't working? ### @@ -312,10 +316,6 @@ For support with the library the best place to ask is via the google-api-php-cli If there is a specific bug with the library, please [file a issue](/Google/google-api-php-client/issues) in the Github issues tracker, including an example of the failing code and any specific errors retrieved. Feature requests can also be filed, as long as they are core library requests, and not-API specific: for those, refer to the documentation for the individual APIs for the best place to file requests. Please try to provide a clear statement of the problem that the feature would address. -### How do I contribute? ### - -We accept contributions via Github Pull Requests, but all contributors need to be covered by the standard Google Contributor License Agreement. You can find links, and more instructions, in the documentation: https://developers.google.com/api-client-library/php/contribute - ### I want an example of X! ### If X is a feature of the library, file away! If X is an example of using a specific service, the best place to go is to the teams for those specific APIs - our preference is to link to their examples rather than add them to the library, as they can then pin to specific versions of the library. If you have any examples for other APIs, let us know and we will happily add a link to the README above! From b9410f923f312b2a80c5cb9d10c80ed09b8e5da2 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 27 Jul 2016 14:46:12 -0700 Subject: [PATCH 006/301] bump apiclient-services version (#1006) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 312c7b437..6812d58dc 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "require": { "php": ">=5.4", "google/auth": "0.9", - "google/apiclient-services": "^0.5", + "google/apiclient-services": "^0.6", "firebase/php-jwt": "~2.0|~3.0", "monolog/monolog": "^1.17", "phpseclib/phpseclib": "~2.0", From 98c73dc9c77e37a23af2bf2e2635a22098d34d22 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 10 Aug 2016 09:46:33 -0700 Subject: [PATCH 007/301] updates auth and uses in-memory cache (#1019) --- composer.json | 2 +- src/Google/AccessToken/Verify.php | 9 +++++++-- src/Google/Client.php | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 6812d58dc..47b21750a 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "license": "Apache-2.0", "require": { "php": ">=5.4", - "google/auth": "0.9", + "google/auth": "0.10", "google/apiclient-services": "^0.6", "firebase/php-jwt": "~2.0|~3.0", "monolog/monolog": "^1.17", diff --git a/src/Google/AccessToken/Verify.php b/src/Google/AccessToken/Verify.php index 2e8d4770d..0980136a0 100644 --- a/src/Google/AccessToken/Verify.php +++ b/src/Google/AccessToken/Verify.php @@ -22,6 +22,7 @@ use phpseclib\Crypt\RSA; use phpseclib\Math\BigInteger; use Psr\Cache\CacheItemPoolInterface; +use Google\Auth\Cache\MemoryCacheItemPool; use Stash\Driver\FileSystem; use Stash\Pool; @@ -55,8 +56,12 @@ public function __construct(ClientInterface $http = null, CacheItemPoolInterface $http = new Client(); } - if (is_null($cache) && class_exists('Stash\Pool')) { - $cache = new Pool(new FileSystem); + if (is_null($cache)) { + if (class_exists('Stash\Pool')) { + $cache = new Pool(new FileSystem); + } else { + $cache = new MemoryCacheItemPool; + } } $this->http = $http; diff --git a/src/Google/Client.php b/src/Google/Client.php index c8592c70e..48e6c0b8e 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -16,6 +16,7 @@ */ use Google\Auth\ApplicationDefaultCredentials; +use Google\Auth\Cache\MemoryCacheItemPool; use Google\Auth\CredentialsLoader; use Google\Auth\HttpHandler\HttpHandlerFactory; use Google\Auth\OAuth2; @@ -950,6 +951,10 @@ public function setCache(CacheItemPoolInterface $cache) */ public function getCache() { + if (!$this->cache) { + $this->cache = $this->createDefaultCache(); + } + return $this->cache; } @@ -990,6 +995,18 @@ protected function createDefaultLogger() return $logger; } + protected function createDefaultCache() + { + // use filesystem cache by default if tedivm/stash exists + if (class_exists('Stash\Pool')) { + $cache = new Stash\Pool(new Stash\Driver\FileSystem); + } else { + $cache = new MemoryCacheItemPool; + } + + return $cache; + } + /** * Set the Http Client object * @param GuzzleHttp\ClientInterface $http From cf5975a75fd4edbad00d555e90fada9a5be340b0 Mon Sep 17 00:00:00 2001 From: Menno Holtkamp Date: Sat, 13 Aug 2016 00:45:07 +0200 Subject: [PATCH 008/301] Remove references to Google_Config (#1029) to prevent confusion --- src/Google/Client.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index 48e6c0b8e..ae574cc31 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -66,7 +66,7 @@ class Google_Client private $token; /** - * @var Google_Config $config + * @var array $config */ private $config; @@ -87,9 +87,9 @@ class Google_Client /** * Construct the Google Client. * - * @param $config Google_Config or string for the ini file to load + * @param array $config */ - public function __construct($config = array()) + public function __construct(array $config = array()) { $this->config = array_merge( [ From d86977834d3bb20a35cf7a8a0d94ed307ebc479e Mon Sep 17 00:00:00 2001 From: Malachi Soord Date: Sat, 13 Aug 2016 00:45:24 +0200 Subject: [PATCH 009/301] Update Client.php (#1030) Correct DocBlock @throws to LogicException --- src/Google/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index ae574cc31..d254879c0 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -676,7 +676,7 @@ public function revokeToken($token = null) * Verify an id_token. This method will verify the current id_token, if one * isn't provided. * - * @throws Google_Exception + * @throws LogicException * @param string|null $idToken The token (id_token) that should be verified. * @return array|false Returns the token payload as an array if the verification was * successful, false otherwise. From 8f3f7dbf19d257fb4cfb935f5b63b956454225b9 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 16 Aug 2016 15:23:46 -0700 Subject: [PATCH 010/301] fixes issue with BC breaking google/auth change (#1028) * fixes issue with BC breaking google/auth change --- src/Google/AuthHandler/Guzzle5AuthHandler.php | 13 ++++++++++--- src/Google/AuthHandler/Guzzle6AuthHandler.php | 13 ++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/Google/AuthHandler/Guzzle5AuthHandler.php b/src/Google/AuthHandler/Guzzle5AuthHandler.php index d3a4b0379..4e4a8d68f 100644 --- a/src/Google/AuthHandler/Guzzle5AuthHandler.php +++ b/src/Google/AuthHandler/Guzzle5AuthHandler.php @@ -2,6 +2,7 @@ use Google\Auth\CredentialsLoader; use Google\Auth\HttpHandler\HttpHandlerFactory; +use Google\Auth\FetchAuthTokenCache; use Google\Auth\Subscriber\AuthTokenSubscriber; use Google\Auth\Subscriber\ScopedAccessTokenSubscriber; use Google\Auth\Subscriber\SimpleSubscriber; @@ -28,6 +29,14 @@ public function attachCredentials( CredentialsLoader $credentials, callable $tokenCallback = null ) { + // use the provided cache + if ($this->cache) { + $credentials = new FetchAuthTokenCache( + $credentials, + $this->cacheConfig, + $this->cache + ); + } // if we end up needing to make an HTTP request to retrieve credentials, we // can use our existing one, but we need to throw exceptions so the error // bubbles up. @@ -35,8 +44,6 @@ public function attachCredentials( $authHttpHandler = HttpHandlerFactory::build($authHttp); $subscriber = new AuthTokenSubscriber( $credentials, - $this->cacheConfig, - $this->cache, $authHttpHandler, $tokenCallback ); @@ -56,7 +63,7 @@ public function attachToken(ClientInterface $http, array $token, array $scopes) $subscriber = new ScopedAccessTokenSubscriber( $tokenFunc, $scopes, - [], + $this->cacheConfig, $this->cache ); diff --git a/src/Google/AuthHandler/Guzzle6AuthHandler.php b/src/Google/AuthHandler/Guzzle6AuthHandler.php index fb743e601..fcdfb3b03 100644 --- a/src/Google/AuthHandler/Guzzle6AuthHandler.php +++ b/src/Google/AuthHandler/Guzzle6AuthHandler.php @@ -2,6 +2,7 @@ use Google\Auth\CredentialsLoader; use Google\Auth\HttpHandler\HttpHandlerFactory; +use Google\Auth\FetchAuthTokenCache; use Google\Auth\Middleware\AuthTokenMiddleware; use Google\Auth\Middleware\ScopedAccessTokenMiddleware; use Google\Auth\Middleware\SimpleMiddleware; @@ -28,6 +29,14 @@ public function attachCredentials( CredentialsLoader $credentials, callable $tokenCallback = null ) { + // use the provided cache + if ($this->cache) { + $credentials = new FetchAuthTokenCache( + $credentials, + $this->cacheConfig, + $this->cache + ); + } // if we end up needing to make an HTTP request to retrieve credentials, we // can use our existing one, but we need to throw exceptions so the error // bubbles up. @@ -35,8 +44,6 @@ public function attachCredentials( $authHttpHandler = HttpHandlerFactory::build($authHttp); $middleware = new AuthTokenMiddleware( $credentials, - $this->cacheConfig, - $this->cache, $authHttpHandler, $tokenCallback ); @@ -59,7 +66,7 @@ public function attachToken(ClientInterface $http, array $token, array $scopes) $middleware = new ScopedAccessTokenMiddleware( $tokenFunc, $scopes, - [], + $this->cacheConfig, $this->cache ); From be5f138d6bb871ff9e7eeaa532ebf62196ebe606 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 16 Aug 2016 15:24:27 -0700 Subject: [PATCH 011/301] uses syslog handler on appengine (#1025) * uses syslog handler on appengine --- src/Google/Client.php | 8 +++++++- tests/Google/ClientTest.php | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index d254879c0..6cb7d4de0 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -31,6 +31,7 @@ use Psr\Log\LoggerInterface; use Monolog\Logger; use Monolog\Handler\StreamHandler as MonologStreamHandler; +use Monolog\Handler\SyslogHandler as MonologSyslogHandler; /** * The Google API Client @@ -990,7 +991,12 @@ public function getLogger() protected function createDefaultLogger() { $logger = new Logger('google-api-php-client'); - $logger->pushHandler(new MonologStreamHandler('php://stderr', Logger::NOTICE)); + if ($this->isAppEngine()) { + $handler = new MonologSyslogHandler('app', LOG_USER, Logger::NOTICE); + } else { + $handler = new MonologStreamHandler('php://stderr', Logger::NOTICE); + } + $logger->pushHandler($handler); return $logger; } diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 576daf432..4a7cb65de 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -243,6 +243,27 @@ public function testPrepareService() $this->assertInstanceOf('Google_Model', $dr_service->files->listFiles()); } + public function testDefaultLogger() + { + $client = new Google_Client(); + $logger = $client->getLogger(); + $this->assertInstanceOf('Monolog\Logger', $logger); + $handler = $logger->popHandler(); + $this->assertInstanceOf('Monolog\Handler\StreamHandler', $handler); + } + + public function testDefaultLoggerAppEngine() + { + $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine'; + $client = new Google_Client(); + $logger = $client->getLogger(); + $handler = $logger->popHandler(); + unset($_SERVER['SERVER_SOFTWARE']); + + $this->assertInstanceOf('Monolog\Logger', $logger); + $this->assertInstanceOf('Monolog\Handler\SyslogHandler', $handler); + } + public function testSettersGetters() { $client = new Google_Client(); From 0b6c5b04595682362a17591a44e5df5249ff7779 Mon Sep 17 00:00:00 2001 From: Morton Fox Date: Tue, 23 Aug 2016 03:57:17 -0400 Subject: [PATCH 012/301] Fix issue tracker link (#1039) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e71003449..8d92a26fe 100644 --- a/README.md +++ b/README.md @@ -314,7 +314,7 @@ Please see the [contributing](CONTRIBUTING.md) page for more information. In par For support with the library the best place to ask is via the google-api-php-client tag on StackOverflow: http://stackoverflow.com/questions/tagged/google-api-php-client -If there is a specific bug with the library, please [file a issue](/Google/google-api-php-client/issues) in the Github issues tracker, including an example of the failing code and any specific errors retrieved. Feature requests can also be filed, as long as they are core library requests, and not-API specific: for those, refer to the documentation for the individual APIs for the best place to file requests. Please try to provide a clear statement of the problem that the feature would address. +If there is a specific bug with the library, please [file a issue](https://github.com/google/google-api-php-client/issues) in the Github issues tracker, including an example of the failing code and any specific errors retrieved. Feature requests can also be filed, as long as they are core library requests, and not-API specific: for those, refer to the documentation for the individual APIs for the best place to file requests. Please try to provide a clear statement of the problem that the feature would address. ### I want an example of X! ### From 2f03c901d656e830eb2a830d4f80d4f787c840bf Mon Sep 17 00:00:00 2001 From: Victor Bocharsky Date: Tue, 23 Aug 2016 10:57:57 +0300 Subject: [PATCH 013/301] Highlight class constant in docs on GitHub (#1038) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8d92a26fe..a5b98748b 100644 --- a/README.md +++ b/README.md @@ -340,7 +340,7 @@ $opt_params = array( ### How do I set a field to null? ### -The library strips out nulls from the objects sent to the Google APIs as its the default value of all of the uninitialised properties. To work around this, set the field you want to null to Google_Model::NULL_VALUE. This is a placeholder that will be replaced with a true null when sent over the wire. +The library strips out nulls from the objects sent to the Google APIs as its the default value of all of the uninitialised properties. To work around this, set the field you want to null to `Google_Model::NULL_VALUE`. This is a placeholder that will be replaced with a true null when sent over the wire. ## Code Quality ## From 37adb8a6f4d4b27b9900d62d67bf1d54995a755c Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 14 Sep 2016 12:33:35 -0700 Subject: [PATCH 014/301] updates google/apiclient-services to 0.7 (#1053) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 47b21750a..5b623904d 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "require": { "php": ">=5.4", "google/auth": "0.10", - "google/apiclient-services": "^0.6", + "google/apiclient-services": "^0.7", "firebase/php-jwt": "~2.0|~3.0", "monolog/monolog": "^1.17", "phpseclib/phpseclib": "~2.0", From 91eab51ba51e8b51882f07cf7f0e52683ae09228 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 17 Oct 2016 17:28:33 -0600 Subject: [PATCH 015/301] fixes cached client tests (#1079) --- tests/Google/ClientTest.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 4a7cb65de..1537821b9 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -90,7 +90,13 @@ private function checkCredentials($http, $fetcherClass, $sub = null) $class = new ReflectionClass(get_class($auth)); $property = $class->getProperty('fetcher'); $property->setAccessible(true); - $fetcher = $property->getValue($auth); + $cacheFetcher = $property->getValue($auth); + $this->assertInstanceOf('Google\Auth\FetchAuthTokenCache', $cacheFetcher); + + $class = new ReflectionClass(get_class($cacheFetcher)); + $property = $class->getProperty('fetcher'); + $property->setAccessible(true); + $fetcher = $property->getValue($cacheFetcher); $this->assertInstanceOf($fetcherClass, $fetcher); if ($sub) { From ae82aa20e814656dc484f64a6854a6f5549874f2 Mon Sep 17 00:00:00 2001 From: Nathan Marcos Date: Mon, 17 Oct 2016 21:30:26 -0200 Subject: [PATCH 016/301] Fix the approval_prompt default value. (#1066) --- src/Google/Client.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index 6cb7d4de0..f89314c41 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -552,8 +552,8 @@ public function setAccessType($accessType) /** * @param string $approvalPrompt Possible values for approval_prompt include: - * {@code "force"} to force the approval UI to appear. (This is the default value) - * {@code "auto"} to request auto-approval when possible. + * {@code "force"} to force the approval UI to appear. + * {@code "auto"} to request auto-approval when possible. (This is the default value) */ public function setApprovalPrompt($approvalPrompt) { From c7c1ec9ac43948851a842888c792c9a3ff1203bd Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 24 Oct 2016 10:22:26 -0600 Subject: [PATCH 017/301] updates composer dependencies (#1082) * updates composer dependencies --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 5b623904d..2249afc3d 100644 --- a/composer.json +++ b/composer.json @@ -7,9 +7,9 @@ "license": "Apache-2.0", "require": { "php": ">=5.4", - "google/auth": "0.10", - "google/apiclient-services": "^0.7", - "firebase/php-jwt": "~2.0|~3.0", + "google/auth": "^0.11", + "google/apiclient-services": "^0.8", + "firebase/php-jwt": "~2.0|~3.0|~4.0", "monolog/monolog": "^1.17", "phpseclib/phpseclib": "~2.0", "guzzlehttp/guzzle": "~5.2|~6.0", From f4a8ef0642cdbea0a07f6966158337bab846d4de Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 2 Nov 2016 08:28:02 -0600 Subject: [PATCH 018/301] removes stash logic, adds caching test for MemoryCache (#1085) --- .travis.yml | 1 + composer.json | 4 +- src/Google/AccessToken/Verify.php | 6 +- src/Google/Client.php | 9 +-- tests/BaseTest.php | 24 ++++++-- tests/Google/CacheTest.php | 99 +++++++++++++++++++++++++++++++ tests/Google/ClientTest.php | 1 + 7 files changed, 124 insertions(+), 20 deletions(-) create mode 100644 tests/Google/CacheTest.php diff --git a/.travis.yml b/.travis.yml index 11b8eeddd..09fc33b69 100644 --- a/.travis.yml +++ b/.travis.yml @@ -34,6 +34,7 @@ before_install: - composer self-update install: + - if [[ "$TRAVIS_PHP_VERSION" == "5.4" ]]; then composer remove --dev cache/filesystem-adapter; fi - composer install - composer require guzzlehttp/guzzle:$GUZZLE_VERSION diff --git a/composer.json b/composer.json index 2249afc3d..58d30a18e 100644 --- a/composer.json +++ b/composer.json @@ -20,10 +20,10 @@ "squizlabs/php_codesniffer": "~2.3", "symfony/dom-crawler": "~2.1", "symfony/css-selector": "~2.1", - "tedivm/stash": "^0.14.1" + "cache/filesystem-adapter": "^0.3.2" }, "suggest": { - "tedivm/stash": "For caching certs and tokens (using Google_Client::setCache)" + "cache/filesystem-adapter": "For caching certs and tokens (using Google_Client::setCache)" }, "autoload": { "psr-0": { diff --git a/src/Google/AccessToken/Verify.php b/src/Google/AccessToken/Verify.php index 0980136a0..dd070befc 100644 --- a/src/Google/AccessToken/Verify.php +++ b/src/Google/AccessToken/Verify.php @@ -57,11 +57,7 @@ public function __construct(ClientInterface $http = null, CacheItemPoolInterface } if (is_null($cache)) { - if (class_exists('Stash\Pool')) { - $cache = new Pool(new FileSystem); - } else { - $cache = new MemoryCacheItemPool; - } + $cache = new MemoryCacheItemPool; } $this->http = $http; diff --git a/src/Google/Client.php b/src/Google/Client.php index f89314c41..ec089c14c 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -1003,14 +1003,7 @@ protected function createDefaultLogger() protected function createDefaultCache() { - // use filesystem cache by default if tedivm/stash exists - if (class_exists('Stash\Pool')) { - $cache = new Stash\Pool(new Stash\Driver\FileSystem); - } else { - $cache = new MemoryCacheItemPool; - } - - return $cache; + return new MemoryCacheItemPool; } /** diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 905e8b7c3..feb671446 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -17,8 +17,9 @@ use GuzzleHttp\ClientInterface; use Symfony\Component\DomCrawler\Crawler; -use Stash\Driver\FileSystem; -use Stash\Pool; +use League\Flysystem\Adapter\Local; +use League\Flysystem\Filesystem; +use Cache\Adapter\Filesystem\FilesystemCachePool; class BaseTest extends PHPUnit_Framework_TestCase { @@ -39,8 +40,11 @@ public function getClient() public function getCache($path = null) { - $path = $path ?: sys_get_temp_dir().'/google-api-php-client-tests'; - return new Pool(new FileSystem(['path' => $path])); + $path = $path ?: sys_get_temp_dir().'/google-api-php-client-tests/'; + $filesystemAdapter = new Local($path); + $filesystem = new Filesystem($filesystemAdapter); + + return new FilesystemCachePool($filesystem); } private function createClient() @@ -81,7 +85,10 @@ private function createClient() list($clientId, $clientSecret) = $this->getClientIdAndSecret(); $client->setClientId($clientId); $client->setClientSecret($clientSecret); - $client->setCache($this->getCache()); + if (version_compare(PHP_VERSION, '5.5', '>=')) { + $client->setCache($this->getCache()); + } + return $client; } @@ -224,6 +231,13 @@ public function onlyGuzzle6() } } + public function onlyPhp55AndAbove() + { + if (version_compare(PHP_VERSION, '5.5', '<')) { + $this->markTestSkipped('PHP 5.5 and above only'); + } + } + public function onlyGuzzle5() { if (!$this->isGuzzle5()) { diff --git a/tests/Google/CacheTest.php b/tests/Google/CacheTest.php new file mode 100644 index 000000000..f15c05935 --- /dev/null +++ b/tests/Google/CacheTest.php @@ -0,0 +1,99 @@ +checkServiceAccountCredentials(); + + $client = $this->getClient(); + $client->useApplicationDefaultCredentials(); + $client->setAccessType('offline'); + $client->setScopes(['https://www.googleapis.com/auth/drive.readonly']); + $client->setCache(new MemoryCacheItemPool); + + /* Refresh token when expired */ + if ($client->isAccessTokenExpired()) { + $client->refreshTokenWithAssertion(); + } + + /* Make a service call */ + $service = new Google_Service_Drive($client); + $files = $service->files->listFiles(); + $this->assertInstanceOf('Google_Service_Drive_FileList', $files); + } + + public function testFileCache() + { + $this->onlyPhp55AndAbove(); + $this->checkServiceAccountCredentials(); + + $client = new Google_Client(); + $client->useApplicationDefaultCredentials(); + $client->setScopes(['https://www.googleapis.com/auth/drive.readonly']); + // filecache with new cache dir + $cache = $this->getCache(sys_get_temp_dir() . '/cloud-samples-tests-php-cache-test/'); + $client->setCache($cache); + + $token1 = null; + $client->setTokenCallback(function($cacheKey, $accessToken) use ($cache, &$token1) { + $token1 = $accessToken; + $cacheItem = $cache->getItem($cacheKey); + // expire the item + $cacheItem->expiresAt(new DateTime('now -1 second')); + $cache->save($cacheItem); + + $cacheItem2 = $cache->getItem($cacheKey); + }); + + /* Refresh token when expired */ + if ($client->isAccessTokenExpired()) { + $client->refreshTokenWithAssertion(); + } + + /* Make a service call */ + $service = new Google_Service_Drive($client); + $files = $service->files->listFiles(); + $this->assertInstanceOf('Google_Service_Drive_FileList', $files); + + sleep(1); + + // make sure the token expires + $client = new Google_Client(); + $client->useApplicationDefaultCredentials(); + $client->setScopes(['https://www.googleapis.com/auth/drive.readonly']); + $client->setCache($cache); + $token2 = null; + $client->setTokenCallback(function($cacheKey, $accessToken) use (&$token2) { + $token2 = $accessToken; + }); + + /* Make another service call */ + $service = new Google_Service_Drive($client); + $files = $service->files->listFiles(); + $this->assertInstanceOf('Google_Service_Drive_FileList', $files); + + $this->assertNotEquals($token1, $token2); + } +} diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 1537821b9..1fe4b5081 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -527,6 +527,7 @@ public function testBadSubjectThrowsException() public function testTokenCallback() { + $this->onlyPhp55AndAbove(); $this->checkToken(); $client = $this->getClient(); From f59ce1e16bf55faf572acc8d002d9a94bc9df2ce Mon Sep 17 00:00:00 2001 From: Serge Postrash Date: Wed, 14 Dec 2016 04:32:56 +0300 Subject: [PATCH 019/301] Fixed misnamed param in phpdoc (#1107) --- src/Google/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index ec089c14c..a8b799bc8 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -831,7 +831,7 @@ public function setAuthConfigFile($file) * This structure should match the file downloaded from * the "Download JSON" button on in the Google Developer * Console. - * @param string|array $json the configuration json + * @param string|array $config the configuration json * @throws Google_Exception */ public function setAuthConfig($config) From 8bd58aea3eb602f3d6db16fe724b2ad534a5c0ca Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 14 Dec 2016 17:12:59 -0800 Subject: [PATCH 020/301] fixes issue 1087 - ensures created is set for fetchAccessTokenWithAssertion (#1113) --- src/Google/Client.php | 9 +++++---- tests/Google/ClientTest.php | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index a8b799bc8..c3281ebdd 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -228,12 +228,13 @@ public function fetchAccessTokenWithAssertion(ClientInterface $authHttp = null) $credentials = $this->createApplicationDefaultCredentials(); $httpHandler = HttpHandlerFactory::build($authHttp); - $accessToken = $credentials->fetchAuthToken($httpHandler); - if ($accessToken && isset($accessToken['access_token'])) { - $this->setAccessToken($accessToken); + $creds = $credentials->fetchAuthToken($httpHandler); + if ($creds && isset($creds['access_token'])) { + $creds['created'] = time(); + $this->setAccessToken($creds); } - return $accessToken; + return $creds; } /** diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 1fe4b5081..c7f0a6e32 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -497,6 +497,22 @@ public function testFetchAccessTokenWithAssertionFromFile() $this->assertArrayHasKey('access_token', $token); } + /** + * Test fetching an access token with assertion credentials + * populates the "created" field + */ + public function testFetchAccessTokenWithAssertionAddsCreated() + { + $this->checkServiceAccountCredentials(); + + $client = $this->getClient(); + $client->useApplicationDefaultCredentials(); + $token = $client->fetchAccessTokenWithAssertion(); + + $this->assertNotNull($token); + $this->assertArrayHasKey('created', $token); + } + /** * Test fetching an access token with assertion credentials * using "setAuthConfig" and "setSubject" but with user credentials From f95de907daa8ddd0f8fbd47480ecf377b9622a15 Mon Sep 17 00:00:00 2001 From: Daniele Montesi Date: Thu, 15 Dec 2016 02:25:10 +0100 Subject: [PATCH 021/301] Fix unused params in BaseTest (#1091) --- tests/BaseTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/BaseTest.php b/tests/BaseTest.php index feb671446..233ab4a7a 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -25,8 +25,6 @@ class BaseTest extends PHPUnit_Framework_TestCase { private $key; private $client; - private $memcacheHost; - private $memcachePort; protected $testDir = __DIR__; public function getClient() From 69838fb5037a8f8731fa4a6b033494f49cd303ea Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 28 Dec 2016 13:00:02 -0800 Subject: [PATCH 022/301] allows for older versions of phpseclib (#1118) * allows for older versions of phpseclib * uses --prefer-lowest instead of explicitly setting guzzle versions * remove hhvm, add php 7.1 --- .travis.yml | 18 +++++------ composer.json | 2 +- oauth-credentials.json | 1 + service-account-credentials.json | 1 + src/Google/AccessToken/Verify.php | 43 +++++++++++++++++++++---- tests/Google/AccessToken/VerifyTest.php | 13 +++++++- 6 files changed, 60 insertions(+), 18 deletions(-) create mode 120000 oauth-credentials.json create mode 120000 service-account-credentials.json diff --git a/.travis.yml b/.travis.yml index 09fc33b69..a7869dfee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,9 +7,7 @@ env: global: - MEMCACHE_HOST=127.0.0.1 - MEMCACHE_PORT=11211 - matrix: - - GUZZLE_VERSION=~5.2 - - GUZZLE_VERSION=~6.0 + - COMPOSER_CMD="composer install" sudo: false @@ -22,21 +20,21 @@ php: - 5.5 - 5.6 - 7.0 - - hhvm + - 7.1 -# Guzzle 6.0 is not compatible with PHP 5.4 +# Test lowest dependencies on PHP 5.4 +# (Guzzle 5.2, phpseclib 0.3) matrix: - exclude: + include: - php: 5.4 - env: GUZZLE_VERSION=~6.0 + env: COMPOSER_CMD="composer update --prefer-lowest" RUN_PHP_CS=true before_install: - composer self-update install: - if [[ "$TRAVIS_PHP_VERSION" == "5.4" ]]; then composer remove --dev cache/filesystem-adapter; fi - - composer install - - composer require guzzlehttp/guzzle:$GUZZLE_VERSION + - $(echo $COMPOSER_CMD) before_script: - phpenv version-name | grep ^5.[34] && echo "extension=apc.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini ; true @@ -44,5 +42,5 @@ before_script: script: - vendor/bin/phpunit - - if [[ "$TRAVIS_PHP_VERSION" == "5.4" ]]; then vendor/bin/phpcs src --standard=style/ruleset.xml -np; fi + - if [[ "$RUN_PHP_CS" == "true" ]]; then vendor/bin/phpcs src --standard=style/ruleset.xml -np; fi diff --git a/composer.json b/composer.json index 58d30a18e..87b27db6d 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "google/apiclient-services": "^0.8", "firebase/php-jwt": "~2.0|~3.0|~4.0", "monolog/monolog": "^1.17", - "phpseclib/phpseclib": "~2.0", + "phpseclib/phpseclib": "~0.3.10|~2.0", "guzzlehttp/guzzle": "~5.2|~6.0", "guzzlehttp/psr7": "^1.2" }, diff --git a/oauth-credentials.json b/oauth-credentials.json new file mode 120000 index 000000000..8cbff52a9 --- /dev/null +++ b/oauth-credentials.json @@ -0,0 +1 @@ +/Users/betterbrent/.gcloud/google-api-php-client-tests-credentials.json \ No newline at end of file diff --git a/service-account-credentials.json b/service-account-credentials.json new file mode 120000 index 000000000..bd0b09663 --- /dev/null +++ b/service-account-credentials.json @@ -0,0 +1 @@ +/Users/betterbrent/.gcloud/cloud-samples-tests-php.json \ No newline at end of file diff --git a/src/Google/AccessToken/Verify.php b/src/Google/AccessToken/Verify.php index dd070befc..52bae0d95 100644 --- a/src/Google/AccessToken/Verify.php +++ b/src/Google/AccessToken/Verify.php @@ -19,8 +19,6 @@ use Firebase\JWT\ExpiredException as ExpiredExceptionV3; use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; -use phpseclib\Crypt\RSA; -use phpseclib\Math\BigInteger; use Psr\Cache\CacheItemPoolInterface; use Google\Auth\Cache\MemoryCacheItemPool; use Stash\Driver\FileSystem; @@ -86,10 +84,12 @@ public function verifyIdToken($idToken, $audience = null) // Check signature $certs = $this->getFederatedSignOnCerts(); foreach ($certs as $cert) { - $modulus = new BigInteger($this->jwt->urlsafeB64Decode($cert['n']), 256); - $exponent = new BigInteger($this->jwt->urlsafeB64Decode($cert['e']), 256); + $bigIntClass = $this->getBigIntClass(); + $rsaClass = $this->getRsaClass(); + $modulus = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['n']), 256); + $exponent = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['e']), 256); - $rsa = new RSA(); + $rsa = new $rsaClass(); $rsa->loadKey(array('n' => $modulus, 'e' => $exponent)); try { @@ -213,6 +213,37 @@ private function getJwtService() return new $jwtClass; } + private function getRsaClass() + { + if (class_exists('phpseclib\Crypt\RSA')) { + return 'phpseclib\Crypt\RSA'; + } + + return 'Crypt_RSA'; + } + + private function getBigIntClass() + { + if (class_exists('phpseclib\Math\BigInteger')) { + return 'phpseclib\Math\BigInteger'; + } + + return 'Math_BigInteger'; + } + + private function getOpenSslConstant() + { + if (class_exists('phpseclib\Crypt\RSA')) { + return 'phpseclib\Crypt\RSA::MODE_OPENSSL'; + } + + if (class_exists('Crypt_RSA')) { + return 'CRYPT_RSA_MODE_OPENSSL'; + } + + throw new \Exception('Cannot find RSA class'); + } + /** * phpseclib calls "phpinfo" by default, which requires special * whitelisting in the AppEngine VM environment. This function @@ -228,7 +259,7 @@ private function setPhpsecConstants() define('MATH_BIGINTEGER_OPENSSL_ENABLED', true); } if (!defined('CRYPT_RSA_MODE')) { - define('CRYPT_RSA_MODE', RSA::MODE_OPENSSL); + define('CRYPT_RSA_MODE', constant($this->getOpenSslConstant())); } } } diff --git a/tests/Google/AccessToken/VerifyTest.php b/tests/Google/AccessToken/VerifyTest.php index a6f4ef01d..a641ce843 100644 --- a/tests/Google/AccessToken/VerifyTest.php +++ b/tests/Google/AccessToken/VerifyTest.php @@ -47,7 +47,7 @@ public function testPhpsecConstants() $openSslEnable = constant('MATH_BIGINTEGER_OPENSSL_ENABLED'); $rsaMode = constant('CRYPT_RSA_MODE'); $this->assertEquals(true, $openSslEnable); - $this->assertEquals(phpseclib\Crypt\RSA::MODE_OPENSSL, $rsaMode); + $this->assertEquals(constant($this->getOpenSslConstant()), $rsaMode); } /** @@ -111,4 +111,15 @@ private function getJwtService() return new \JWT; } + + private function getOpenSslConstant() + { + if (class_exists('phpseclib\Crypt\RSA')) { + return 'phpseclib\Crypt\RSA::MODE_OPENSSL'; + } + + if (class_exists('Crypt_RSA')) { + return 'CRYPT_RSA_MODE_OPENSSL'; + } + } } From a79457b1188275baec6f830b3c34d856ab852e4b Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 28 Dec 2016 15:32:53 -0800 Subject: [PATCH 023/301] Update README.md (#1114) --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index a5b98748b..d70d1d4d8 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,9 @@ This library is in Beta. We're comfortable enough with the stability and feature ## Requirements ## * [PHP 5.4.0 or higher](http://www.php.net/) +## Google Cloud Platform APIs +If you're looking to call the **Google Cloud Platform** APIs, you will want to use the [Google Cloud PHP](https://github.com/googlecloudplatform/google-cloud-php) library instead of this one. + ## Developer Documentation ## http://developers.google.com/api-client-library/php From d0fc0e1f0d2db88e066395fa548f9b87d50797fc Mon Sep 17 00:00:00 2001 From: Daniele Montesi Date: Thu, 29 Dec 2016 00:49:12 +0100 Subject: [PATCH 024/301] Remove unused $authHttp to Google_Client::authorize (#1090) --- src/Google/Client.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index c3281ebdd..99fb0125a 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -336,10 +336,9 @@ public function createAuthUrl($scope = null) * set in the Google API Client object * * @param GuzzleHttp\ClientInterface $http the http client object. - * @param GuzzleHttp\ClientInterface $authHttp an http client for authentication. * @return GuzzleHttp\ClientInterface the http client object */ - public function authorize(ClientInterface $http = null, ClientInterface $authHttp = null) + public function authorize(ClientInterface $http = null) { $credentials = null; $token = null; From af31104c7dc1041ada466bf79d583a1e5f88c940 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 4 Jan 2017 11:58:37 -0800 Subject: [PATCH 025/301] update libver version (#1120) --- src/Google/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index 99fb0125a..585a6a47f 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -39,7 +39,7 @@ */ class Google_Client { - const LIBVER = "2.0.0-alpha"; + const LIBVER = "2.1.0"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://accounts.google.com/o/oauth2/revoke'; const OAUTH2_TOKEN_URI = 'https://www.googleapis.com/oauth2/v4/token'; From 195fce80b1f16fe43113b7650cac955044afca1b Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 5 Jan 2017 12:04:35 -0800 Subject: [PATCH 026/301] ensures refresh token is added back to the access token, adds tests (#1121) --- src/Google/Client.php | 3 ++ tests/Google/ClientTest.php | 77 ++++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index 585a6a47f..ea5de6d91 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -272,6 +272,9 @@ public function fetchAccessTokenWithRefreshToken($refreshToken = null) $creds = $auth->fetchAuthToken($httpHandler); if ($creds && isset($creds['access_token'])) { $creds['created'] = time(); + if (!isset($creds['refresh_token'])) { + $creds['refresh_token'] = $refreshToken; + } $this->setAccessToken($creds); } diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index c7f0a6e32..80c732674 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -462,7 +462,82 @@ public function testRefreshTokenSetsValues() $client->setHttpClient($http); $client->fetchAccessTokenWithRefreshToken("REFRESH_TOKEN"); $token = $client->getAccessToken(); - $this->assertEquals($token['id_token'], "ID_TOKEN"); + $this->assertEquals("ID_TOKEN", $token['id_token']); + } + + /** + * Test that the Refresh Token is set when refreshed. + */ + public function testRefreshTokenIsSetOnRefresh() + { + $refreshToken = 'REFRESH_TOKEN'; + $token = json_encode(array( + 'access_token' => 'xyz', + 'id_token' => 'ID_TOKEN', + )); + $postBody = $this->getMock('Psr\Http\Message\StreamInterface'); + $postBody->expects($this->once()) + ->method('__toString') + ->will($this->returnValue($token)); + $response = $this->getMock('Psr\Http\Message\ResponseInterface'); + $response->expects($this->once()) + ->method('getBody') + ->will($this->returnValue($postBody)); + $http = $this->getMock('GuzzleHttp\ClientInterface'); + $http->expects($this->once()) + ->method('send') + ->will($this->returnValue($response)); + + if ($this->isGuzzle5()) { + $guzzle5Request = new GuzzleHttp\Message\Request('POST', '/', ['body' => $token]); + $http->expects($this->once()) + ->method('createRequest') + ->will($this->returnValue($guzzle5Request)); + } + + $client = $this->getClient(); + $client->setHttpClient($http); + $client->fetchAccessTokenWithRefreshToken($refreshToken); + $token = $client->getAccessToken(); + $this->assertEquals($refreshToken, $token['refresh_token']); + } + + /** + * Test that the Refresh Token is not set when a new refresh token is returned. + */ + public function testRefreshTokenIsNotSetWhenNewRefreshTokenIsReturned() + { + $refreshToken = 'REFRESH_TOKEN'; + $token = json_encode(array( + 'access_token' => 'xyz', + 'id_token' => 'ID_TOKEN', + 'refresh_token' => 'NEW_REFRESH_TOKEN' + )); + $postBody = $this->getMock('Psr\Http\Message\StreamInterface'); + $postBody->expects($this->once()) + ->method('__toString') + ->will($this->returnValue($token)); + $response = $this->getMock('Psr\Http\Message\ResponseInterface'); + $response->expects($this->once()) + ->method('getBody') + ->will($this->returnValue($postBody)); + $http = $this->getMock('GuzzleHttp\ClientInterface'); + $http->expects($this->once()) + ->method('send') + ->will($this->returnValue($response)); + + if ($this->isGuzzle5()) { + $guzzle5Request = new GuzzleHttp\Message\Request('POST', '/', ['body' => $token]); + $http->expects($this->once()) + ->method('createRequest') + ->will($this->returnValue($guzzle5Request)); + } + + $client = $this->getClient(); + $client->setHttpClient($http); + $client->fetchAccessTokenWithRefreshToken($refreshToken); + $token = $client->getAccessToken(); + $this->assertEquals('NEW_REFRESH_TOKEN', $token['refresh_token']); } /** From 2a35d62987255328c7ee49da5ddf76d5d7720980 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 5 Jan 2017 16:09:18 -0800 Subject: [PATCH 027/301] ensures JWT leeway is restored after we set it (#1122) --- src/Google/AccessToken/Verify.php | 9 ++++++--- src/Google/Client.php | 7 ++++++- tests/Google/AccessToken/VerifyTest.php | 27 +++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/Google/AccessToken/Verify.php b/src/Google/AccessToken/Verify.php index 52bae0d95..adb2bec90 100644 --- a/src/Google/AccessToken/Verify.php +++ b/src/Google/AccessToken/Verify.php @@ -48,8 +48,11 @@ class Google_AccessToken_Verify * Instantiates the class, but does not initiate the login flow, leaving it * to the discretion of the caller. */ - public function __construct(ClientInterface $http = null, CacheItemPoolInterface $cache = null) - { + public function __construct( + ClientInterface $http = null, + CacheItemPoolInterface $cache = null, + $jwt = null + ) { if (is_null($http)) { $http = new Client(); } @@ -60,7 +63,7 @@ public function __construct(ClientInterface $http = null, CacheItemPoolInterface $this->http = $http; $this->cache = $cache; - $this->jwt = $this->getJwtService(); + $this->jwt = $jwt ?: $this->getJwtService(); } /** diff --git a/src/Google/Client.php b/src/Google/Client.php index ea5de6d91..b11ef6b33 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -138,6 +138,10 @@ public function __construct(array $config = array()) // function to be called when an access token is fetched // follows the signature function ($cacheKey, $accessToken) 'token_callback' => null, + + // Service class used in Google_Client::verifyIdToken. + // Explicitly pass this in to avoid setting JWT::$leeway + 'jwt' => null, ], $config ); @@ -689,7 +693,8 @@ public function verifyIdToken($idToken = null) { $tokenVerifier = new Google_AccessToken_Verify( $this->getHttpClient(), - $this->getCache() + $this->getCache(), + $this->jwt ); if (is_null($idToken)) { diff --git a/tests/Google/AccessToken/VerifyTest.php b/tests/Google/AccessToken/VerifyTest.php index a641ce843..351814324 100644 --- a/tests/Google/AccessToken/VerifyTest.php +++ b/tests/Google/AccessToken/VerifyTest.php @@ -87,6 +87,33 @@ public function testValidateIdToken() $this->assertTrue(strlen($payload['sub']) > 0); } + /** + * Most of the logic for ID token validation is in AuthTest - + * this is just a general check to ensure we verify a valid + * id token if one exists. + */ + public function testLeewayIsUnchangedWhenPassingInJwt() + { + $this->checkToken(); + + $jwt = $this->getJwtService(); + // set arbitrary leeway so we can check this later + $jwt::$leeway = $leeway = 1.5; + $client = $this->getClient(); + $token = $client->getAccessToken(); + if ($client->isAccessTokenExpired()) { + $token = $client->fetchAccessTokenWithRefreshToken(); + } + $segments = explode('.', $token['id_token']); + $this->assertEquals(3, count($segments)); + // Extract the client ID in this case as it wont be set on the test client. + $data = json_decode($jwt->urlSafeB64Decode($segments[1])); + $verify = new Google_AccessToken_Verify($client->getHttpClient(), null, $jwt); + $payload = $verify->verifyIdToken($token['id_token'], $data->aud); + // verify the leeway is set as it was + $this->assertEquals($leeway, $jwt::$leeway); + } + public function testRetrieveCertsFromLocation() { $client = $this->getClient(); From 8e3ac1ae153d536a2eee9e87e3fea097969f69eb Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 6 Jan 2017 14:56:45 -0800 Subject: [PATCH 028/301] fixes reference to jwt config --- src/Google/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index b11ef6b33..3bd6d6319 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -694,7 +694,7 @@ public function verifyIdToken($idToken = null) $tokenVerifier = new Google_AccessToken_Verify( $this->getHttpClient(), $this->getCache(), - $this->jwt + $this->config['jwt'] ); if (is_null($idToken)) { From 7b1abf3b7b7c815c854c8fd60f2601ca0da5b7cb Mon Sep 17 00:00:00 2001 From: Willem Stuursma Date: Mon, 9 Jan 2017 20:32:14 +0100 Subject: [PATCH 029/301] Don't distribute tests, build files etc. for composer (#1069) Don't distribute tests, build files etc. when installing with Composer with `--prefer-dist` (the default). This saves lots of bandwith and file extraction when installing dependencies with default settings. --- .gitattributes | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000..d4e9c2512 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,9 @@ +/.github export-ignore +/.gitignore export-ignore +/.travis.yml export-ignore +/CONTRIBUTING.md export-ignore +/examples export-ignore +/phpunit.xml.dist export-ignore +/style export-ignore +/tests export-ignore +/UPGRADING.md export-ignore From 54abfa1d2b9476466df8f6a5fb00d160fdc4db62 Mon Sep 17 00:00:00 2001 From: AQNOUCH Mohammed Date: Mon, 9 Jan 2017 19:34:32 +0000 Subject: [PATCH 030/301] Fix typos in README(#1081) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d70d1d4d8..28d2f7eda 100644 --- a/README.md +++ b/README.md @@ -343,7 +343,7 @@ $opt_params = array( ### How do I set a field to null? ### -The library strips out nulls from the objects sent to the Google APIs as its the default value of all of the uninitialised properties. To work around this, set the field you want to null to `Google_Model::NULL_VALUE`. This is a placeholder that will be replaced with a true null when sent over the wire. +The library strips out nulls from the objects sent to the Google APIs as its the default value of all of the uninitialized properties. To work around this, set the field you want to null to `Google_Model::NULL_VALUE`. This is a placeholder that will be replaced with a true null when sent over the wire. ## Code Quality ## From 45668e523a8b492a8479b76ac0d19637be367715 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 9 Jan 2017 14:37:09 -0800 Subject: [PATCH 031/301] updates Google_Client::LIBVER to 2.1.1 --- src/Google/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index 3bd6d6319..b266068e0 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -39,7 +39,7 @@ */ class Google_Client { - const LIBVER = "2.1.0"; + const LIBVER = "2.1.1"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://accounts.google.com/o/oauth2/revoke'; const OAUTH2_TOKEN_URI = 'https://www.googleapis.com/oauth2/v4/token'; From e39fe36586407600b0c79623ae4817412a8ccc7e Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 9 Jan 2017 15:08:43 -0800 Subject: [PATCH 032/301] removes errant symlinks --- oauth-credentials.json | 1 - service-account-credentials.json | 1 - 2 files changed, 2 deletions(-) delete mode 120000 oauth-credentials.json delete mode 120000 service-account-credentials.json diff --git a/oauth-credentials.json b/oauth-credentials.json deleted file mode 120000 index 8cbff52a9..000000000 --- a/oauth-credentials.json +++ /dev/null @@ -1 +0,0 @@ -/Users/betterbrent/.gcloud/google-api-php-client-tests-credentials.json \ No newline at end of file diff --git a/service-account-credentials.json b/service-account-credentials.json deleted file mode 120000 index bd0b09663..000000000 --- a/service-account-credentials.json +++ /dev/null @@ -1 +0,0 @@ -/Users/betterbrent/.gcloud/cloud-samples-tests-php.json \ No newline at end of file From d3c97912214abcdb846abadbf8bfd2859e74c3e5 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 11 Jan 2017 11:41:26 -0800 Subject: [PATCH 033/301] adds Large File Download example (#1127) --- examples/index.php | 1 + examples/large-file-download.php | 149 +++++++++++++++++++++++ examples/large-file-upload.php | 1 + tests/examples/indexTest.php | 2 +- tests/examples/largeFileDownloadTest.php | 52 ++++++++ 5 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 examples/large-file-download.php create mode 100644 tests/examples/largeFileDownloadTest.php diff --git a/examples/index.php b/examples/index.php index 5ae2e9cf4..dca566a87 100644 --- a/examples/index.php +++ b/examples/index.php @@ -36,6 +36,7 @@
  • A query using the service account functionality.
  • An example of a small file upload.
  • An example of a large file upload.
  • +
  • An example of a large file download.
  • An example of verifying and retrieving the id token.
  • An example of using multiple APIs.
  • diff --git a/examples/large-file-download.php b/examples/large-file-download.php new file mode 100644 index 000000000..51cf8b0bc --- /dev/null +++ b/examples/large-file-download.php @@ -0,0 +1,149 @@ +setAuthConfig($oauth_credentials); +$client->setRedirectUri($redirect_uri); +$client->addScope("https://www.googleapis.com/auth/drive"); +$service = new Google_Service_Drive($client); + +/************************************************ + * If we have a code back from the OAuth 2.0 flow, + * we need to exchange that with the + * Google_Client::fetchAccessTokenWithAuthCode() + * function. We store the resultant access token + * bundle in the session, and redirect to ourself. + ************************************************/ +if (isset($_GET['code'])) { + $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); + $client->setAccessToken($token); + + // store in the session also + $_SESSION['upload_token'] = $token; + + // redirect back to the example + header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); +} + +// set the access token as part of the client +if (!empty($_SESSION['upload_token'])) { + $client->setAccessToken($_SESSION['upload_token']); + if ($client->isAccessTokenExpired()) { + unset($_SESSION['upload_token']); + } +} else { + $authUrl = $client->createAuthUrl(); +} + +/************************************************ + * If we're signed in then lets try to download our + * file. + ************************************************/ +if ($client->getAccessToken()) { + // Check for "Big File" and include the file ID and size + $files = $service->files->listFiles([ + 'q' => "name='Big File'", + 'fields' => 'files(id,size)' + ]); + + if (count($files) == 0) { + echo " +

    + Before you can use this sample, you need to + upload a large file to Drive. +

    "; + return; + } + + // If this is a POST, download the file + if ($_SERVER['REQUEST_METHOD'] == 'POST') { + // Determine the file's size and ID + $fileId = $files[0]->id; + $fileSize = intval($files[0]->size); + + // Get the authorized Guzzle HTTP client + $http = $client->authorize(); + + // Open a file for writing + $fp = fopen('Big File (downloaded)', 'w'); + + // Download in 1 MB chunks + $chunkSizeBytes = 1 * 1024 * 1024; + $chunkStart = 0; + + // Iterate over each chunk and write it to our file + while ($chunkStart < $fileSize) { + $chunkEnd = $chunkStart + $chunkSizeBytes; + $response = $http->request( + 'GET', + sprintf('/drive/v3/files/%s', $fileId), + [ + 'query' => ['alt' => 'media'], + 'headers' => [ + 'Range' => sprintf('bytes=%s-%s', $chunkStart, $chunkEnd) + ] + ] + ); + $chunkStart = $chunkEnd + 1; + fwrite($fp, $response->getBody()->getContents()); + } + // close the file pointer + fclose($fp); + + // redirect back to this example + header('Location: ' . filter_var($redirect_uri . '?downloaded', FILTER_SANITIZE_URL)); + } +} +?> + +
    + + + +
    +

    Your call was successful! Check your filesystem for the file:

    +

    Big File (downloaded)

    +
    + +
    + +
    + +
    + + diff --git a/examples/large-file-upload.php b/examples/large-file-upload.php index 1afda2cdd..0d966f020 100644 --- a/examples/large-file-upload.php +++ b/examples/large-file-upload.php @@ -157,6 +157,7 @@ function readVideoChunk ($handle, $chunkSize)

    Your call was successful! Check your drive for this file:

    name ?>

    +

    Now try downloading a large file from Drive.

    diff --git a/tests/examples/indexTest.php b/tests/examples/indexTest.php index d869f9145..f0ba28313 100644 --- a/tests/examples/indexTest.php +++ b/tests/examples/indexTest.php @@ -26,7 +26,7 @@ public function testIndex() $crawler = $this->loadExample('index.php'); $nodes = $crawler->filter('li'); - $this->assertEquals(8, count($nodes)); + $this->assertEquals(9, count($nodes)); $this->assertEquals('A query using simple API access', $nodes->first()->text()); } } \ No newline at end of file diff --git a/tests/examples/largeFileDownloadTest.php b/tests/examples/largeFileDownloadTest.php new file mode 100644 index 000000000..764b094f6 --- /dev/null +++ b/tests/examples/largeFileDownloadTest.php @@ -0,0 +1,52 @@ +checkServiceAccountCredentials(); + + $crawler = $this->loadExample('large-file-download.php'); + + $nodes = $crawler->filter('h1'); + $this->assertEquals(1, count($nodes)); + $this->assertEquals('File Download - Downloading a large file', $nodes->first()->text()); + + $nodes = $crawler->filter('a.login'); + $this->assertEquals(1, count($nodes)); + $this->assertEquals('Connect Me!', $nodes->first()->text()); + } + + public function testSimpleFileDownloadWithToken() + { + $this->checkToken(); + + global $_SESSION; + $_SESSION['upload_token'] = $this->getClient()->getAccessToken(); + + $crawler = $this->loadExample('large-file-download.php'); + + $buttonText = 'Click here to download a large (20MB) test file'; + $nodes = $crawler->filter('input'); + $this->assertEquals(1, count($nodes)); + $this->assertEquals($buttonText, $nodes->first()->attr('value')); + } +} \ No newline at end of file From 360e656f48c3262a6f7fefc0207521efa66caedf Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 11 Jan 2017 11:44:46 -0800 Subject: [PATCH 034/301] update google/apiclient-servies to v0.9 (#1128) --- composer.json | 2 +- src/Google/Client.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 87b27db6d..43bb2d8da 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "require": { "php": ">=5.4", "google/auth": "^0.11", - "google/apiclient-services": "^0.8", + "google/apiclient-services": "^0.9", "firebase/php-jwt": "~2.0|~3.0|~4.0", "monolog/monolog": "^1.17", "phpseclib/phpseclib": "~0.3.10|~2.0", diff --git a/src/Google/Client.php b/src/Google/Client.php index b266068e0..5c377bbff 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -39,7 +39,7 @@ */ class Google_Client { - const LIBVER = "2.1.1"; + const LIBVER = "2.1.2"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://accounts.google.com/o/oauth2/revoke'; const OAUTH2_TOKEN_URI = 'https://www.googleapis.com/oauth2/v4/token'; From d1ff8c71cc74e4539a1c4cb76260119ebe7b0970 Mon Sep 17 00:00:00 2001 From: Maxim Date: Wed, 18 Jan 2017 02:00:20 +0400 Subject: [PATCH 035/301] Added @deprecated comments (#1132) --- src/Google/Client.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Google/Client.php b/src/Google/Client.php index 5c377bbff..e04dc3c46 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -163,6 +163,7 @@ public function getLibraryVersion() * * @param $code string code from accounts.google.com * @return array access token + * @deprecated */ public function authenticate($code) { @@ -201,6 +202,7 @@ public function fetchAccessTokenWithAuthCode($code) * alias for fetchAccessTokenWithAssertion * * @return array access token + * @deprecated */ public function refreshTokenWithAssertion() { @@ -828,6 +830,7 @@ public function getConfig($name, $default = null) * * @param string $file the configuration file * @throws Google_Exception + * @deprecated */ public function setAuthConfigFile($file) { From 8f011b582e46285b29689e1b2b53a1965f1b6a86 Mon Sep 17 00:00:00 2001 From: IDLOCKED Date: Fri, 20 Jan 2017 05:49:28 +0900 Subject: [PATCH 036/301] Fix typos in README (#1130) --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 28d2f7eda..e2d6eb85a 100644 --- a/README.md +++ b/README.md @@ -179,7 +179,7 @@ Using this library, the same call would look something like this: ```php // create the datastore service class -$datastore = new Google_Service_Datastore($client) +$datastore = new Google_Service_Datastore($client); // build the query - this maps directly to the JSON $query = new Google_Service_Datastore_Query([ @@ -206,7 +206,7 @@ However, as each property of the JSON API has a corresponding generated class, t ```php // create the datastore service class -$datastore = new Google_Service_Datastore($client) +$datastore = new Google_Service_Datastore($client); // build the query $request = new Google_Service_Datastore_RunQueryRequest(); From c522a3ddf545f3f8c391307782c3429e76c39abd Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 2 Feb 2017 14:41:36 -0800 Subject: [PATCH 037/301] fix createBatch call (#1137) --- examples/batch.php | 2 +- src/Google/Http/Batch.php | 18 ++++++++++++++---- tests/BaseTest.php | 1 - tests/Google/ServiceTest.php | 22 ++++++++++++++++++++++ 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/examples/batch.php b/examples/batch.php index 94118185f..ac9ebbae7 100644 --- a/examples/batch.php +++ b/examples/batch.php @@ -58,7 +58,7 @@ want to execute with keys of our choice - these keys will be reflected in the returned array. ************************************************/ -$batch = new Google_Http_Batch($client); +$batch = $service->createBatch(); $optParams = array('filter' => 'free-ebooks'); $req1 = $service->volumes->listVolumes('Henry David Thoreau', $optParams); $batch->add($req1, "thoreau"); diff --git a/src/Google/Http/Batch.php b/src/Google/Http/Batch.php index e344777b3..b4343067c 100644 --- a/src/Google/Http/Batch.php +++ b/src/Google/Http/Batch.php @@ -42,10 +42,20 @@ class Google_Http_Batch /** @var Google_Client */ private $client; - public function __construct(Google_Client $client) - { + private $rootUrl; + + private $batchPath; + + public function __construct( + Google_Client $client, + $boundary = false, + $rootUrl = null, + $batchPath = null + ) { $this->client = $client; - $this->boundary = mt_rand(); + $this->boundary = $boundary ?: mt_rand(); + $this->rootUrl = rtrim($rootUrl ?: $this->client->getConfig('base_path'), '/'); + $this->batchPath = $batchPath ?: self::BATCH_PATH; } public function add(RequestInterface $request, $key = false) @@ -104,7 +114,7 @@ public function execute() $body .= "--{$this->boundary}--"; $body = trim($body); - $url = Google_Client::API_BASE_PATH . '/' . self::BATCH_PATH; + $url = $this->rootUrl . '/' . $this->batchPath; $headers = array( 'Content-Type' => sprintf('multipart/mixed; boundary=%s', $this->boundary), 'Content-Length' => strlen($body), diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 233ab4a7a..7acb4adec 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -87,7 +87,6 @@ private function createClient() $client->setCache($this->getCache()); } - return $client; } diff --git a/tests/Google/ServiceTest.php b/tests/Google/ServiceTest.php index f119a5c42..a85b147e6 100644 --- a/tests/Google/ServiceTest.php +++ b/tests/Google/ServiceTest.php @@ -31,8 +31,30 @@ public function isAssociativeArray($array) } } +class TestService extends Google_Service +{ + public $batchPath = 'batch/test'; +} + class Google_ServiceTest extends PHPUnit_Framework_TestCase { + public function testCreateBatch() + { + $response = $this->getMock('Psr\Http\Message\ResponseInterface'); + $client = $this->getMock('Google_Client'); + $client + ->expects($this->once()) + ->method('execute') + ->with($this->callback(function ($request) { + $this->assertEquals('/batch/test', $request->getRequestTarget()); + return $request; + })) + ->will($this->returnValue($response)); + $model = new TestService($client); + $batch = $model->createBatch(); + $this->assertInstanceOf('Google_Http_Batch', $batch); + $batch->execute(); + } public function testModel() { From e430670c9220492c2636f4a2c480561aac937100 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 13 Mar 2017 13:47:46 -0700 Subject: [PATCH 038/301] upgrade php client services to 0.11 (#1171) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 43bb2d8da..3f0619c17 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "require": { "php": ">=5.4", "google/auth": "^0.11", - "google/apiclient-services": "^0.9", + "google/apiclient-services": "^0.11", "firebase/php-jwt": "~2.0|~3.0|~4.0", "monolog/monolog": "^1.17", "phpseclib/phpseclib": "~0.3.10|~2.0", From 43996f09df274158fd04fce98e8a82effe5f3717 Mon Sep 17 00:00:00 2001 From: Vladimir Reznichenko Date: Wed, 22 Mar 2017 19:32:04 +0100 Subject: [PATCH 039/301] Static Code Analysis with Php Inspections (EA Extended) (#1174) * language level fixes * dead code dropped * one-time use variables inlined * control flow simplifications * changes code review * unnecessary parenthesises dropped * fixed CS --- src/Google/AccessToken/Revoke.php | 5 +---- src/Google/AccessToken/Verify.php | 4 ++-- src/Google/Client.php | 21 +++++++++------------ src/Google/Collection.php | 2 +- src/Google/Http/Batch.php | 6 +----- src/Google/Http/MediaFileUpload.php | 9 +++------ src/Google/Http/REST.php | 6 +++--- src/Google/Model.php | 3 +-- src/Google/Service/Resource.php | 2 +- src/Google/Task/Runner.php | 11 ++++------- tests/BaseTest.php | 6 +++--- tests/Google/ClientTest.php | 4 ++-- tests/Google/Service/TasksTest.php | 2 +- tests/Google/ServiceTest.php | 2 +- 14 files changed, 33 insertions(+), 50 deletions(-) diff --git a/src/Google/AccessToken/Revoke.php b/src/Google/AccessToken/Revoke.php index 7acaea006..29eb3fb36 100644 --- a/src/Google/AccessToken/Revoke.php +++ b/src/Google/AccessToken/Revoke.php @@ -72,10 +72,7 @@ public function revokeToken($token) $httpHandler = HttpHandlerFactory::build($this->http); $response = $httpHandler($request); - if ($response->getStatusCode() == 200) { - return true; - } - return false; + return $response->getStatusCode() == 200; } } diff --git a/src/Google/AccessToken/Verify.php b/src/Google/AccessToken/Verify.php index adb2bec90..748aa3df8 100644 --- a/src/Google/AccessToken/Verify.php +++ b/src/Google/AccessToken/Verify.php @@ -53,11 +53,11 @@ public function __construct( CacheItemPoolInterface $cache = null, $jwt = null ) { - if (is_null($http)) { + if (null === $http) { $http = new Client(); } - if (is_null($cache)) { + if (null === $cache) { $cache = new MemoryCacheItemPool; } diff --git a/src/Google/Client.php b/src/Google/Client.php index e04dc3c46..2fb29578c 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -262,7 +262,7 @@ public function refreshToken($refreshToken) */ public function fetchAccessTokenWithRefreshToken($refreshToken = null) { - if (is_null($refreshToken)) { + if (null === $refreshToken) { if (!isset($this->token['refresh_token'])) { throw new LogicException( 'refresh token must be passed in or set as part of setAccessToken' @@ -352,7 +352,7 @@ public function authorize(ClientInterface $http = null) $credentials = null; $token = null; $scopes = null; - if (is_null($http)) { + if (null === $http) { $http = $this->getHttpClient(); } @@ -366,7 +366,7 @@ public function authorize(ClientInterface $http = null) } elseif ($token = $this->getAccessToken()) { $scopes = $this->prepareScopes(); // add refresh subscriber to request a new token - if ($this->isAccessTokenExpired() && isset($token['refresh_token'])) { + if (isset($token['refresh_token']) && $this->isAccessTokenExpired()) { $credentials = $this->createUserRefreshCredentials( $scopes, $token['refresh_token'] @@ -477,10 +477,7 @@ public function isAccessTokenExpired() } // If the token is set to expire in the next 30 seconds. - $expired = ($created - + ($this->token['expires_in'] - 30)) < time(); - - return $expired; + return ($created + ($this->token['expires_in'] - 30)) < time(); } public function getAuth() @@ -598,7 +595,7 @@ public function setApplicationName($applicationName) public function setRequestVisibleActions($requestVisibleActions) { if (is_array($requestVisibleActions)) { - $requestVisibleActions = join(" ", $requestVisibleActions); + $requestVisibleActions = implode(" ", $requestVisibleActions); } $this->config['request_visible_actions'] = $requestVisibleActions; } @@ -699,7 +696,7 @@ public function verifyIdToken($idToken = null) $this->config['jwt'] ); - if (is_null($idToken)) { + if (null === $idToken) { $token = $this->getAccessToken(); if (!isset($token['id_token'])) { throw new LogicException( @@ -764,8 +761,8 @@ public function prepareScopes() if (empty($this->requestedScopes)) { return null; } - $scopes = implode(' ', $this->requestedScopes); - return $scopes; + + return implode(' ', $this->requestedScopes); } /** @@ -1031,7 +1028,7 @@ public function setHttpClient(ClientInterface $http) */ public function getHttpClient() { - if (is_null($this->http)) { + if (null === $this->http) { $this->http = $this->createDefaultHttpClient(); } diff --git a/src/Google/Collection.php b/src/Google/Collection.php index b26e9e51d..7c478bc02 100644 --- a/src/Google/Collection.php +++ b/src/Google/Collection.php @@ -1,7 +1,7 @@ expected_classes[$key])) { - $class = $this->expected_classes[$key]; - } try { $response = Google_Http_REST::decodeHttpResponse($response, $requests[$i-1]); diff --git a/src/Google/Http/MediaFileUpload.php b/src/Google/Http/MediaFileUpload.php index 72a6eeaf8..bd38a71b6 100644 --- a/src/Google/Http/MediaFileUpload.php +++ b/src/Google/Http/MediaFileUpload.php @@ -221,9 +221,7 @@ private function process() Uri::withQueryValue($request->getUri(), 'uploadType', $uploadType) ); - $mimeType = $this->mimeType ? - $this->mimeType : - $request->getHeaderLine('content-type'); + $mimeType = $this->mimeType ?: $request->getHeaderLine('content-type'); if (self::UPLOAD_RESUMABLE_TYPE == $uploadType) { $contentType = $mimeType; @@ -233,7 +231,7 @@ private function process() $postBody = $this->data; } else if (self::UPLOAD_MULTIPART_TYPE == $uploadType) { // This is a multipart/related upload. - $boundary = $this->boundary ? $this->boundary : mt_rand(); + $boundary = $this->boundary ?: mt_rand(); $boundary = str_replace('"', '', $boundary); $contentType = 'multipart/related; boundary=' . $boundary; $related = "--$boundary\r\n"; @@ -280,7 +278,7 @@ public function getUploadType($meta) public function getResumeUri() { - if (is_null($this->resumeUri)) { + if (null === $this->resumeUri) { $this->resumeUri = $this->fetchResumeUri(); } @@ -289,7 +287,6 @@ public function getResumeUri() private function fetchResumeUri() { - $result = null; $body = $this->request->getBody(); if ($body) { $headers = array( diff --git a/src/Google/Http/REST.php b/src/Google/Http/REST.php index 7eccc9bee..c2156a2e8 100644 --- a/src/Google/Http/REST.php +++ b/src/Google/Http/REST.php @@ -51,7 +51,7 @@ public static function execute( array($client, $request, $expectedClass) ); - if (!is_null($retryMap)) { + if (null !== $retryMap) { $runner->setRetryMap($retryMap); } @@ -110,7 +110,7 @@ public static function decodeHttpResponse( $code = $response->getStatusCode(); // retry strategy - if ((intVal($code)) >= 400) { + if (intVal($code) >= 400) { // if we errored out, it should be safe to grab the response body $body = (string) $response->getBody(); @@ -149,7 +149,7 @@ private static function determineExpectedClass($expectedClass, RequestInterface } // if we don't have a request, we just use what's passed in - if (is_null($request)) { + if (null === $request) { return $expectedClass; } diff --git a/src/Google/Model.php b/src/Google/Model.php index 7168c945b..cd50c9a5e 100644 --- a/src/Google/Model.php +++ b/src/Google/Model.php @@ -192,8 +192,7 @@ private function nullPlaceholderCheck($value) */ private function getMappedName($key) { - if (isset($this->internal_gapi_mappings) && - isset($this->internal_gapi_mappings[$key])) { + if (isset($this->internal_gapi_mappings, $this->internal_gapi_mappings[$key])) { $key = $this->internal_gapi_mappings[$key]; } return $key; diff --git a/src/Google/Service/Resource.php b/src/Google/Service/Resource.php index 96c24fc1a..a3c57ee2d 100644 --- a/src/Google/Service/Resource.php +++ b/src/Google/Service/Resource.php @@ -267,7 +267,7 @@ public function createRequestUri($restPath, $params) $queryVars = array(); foreach ($params as $paramName => $paramSpec) { if ($paramSpec['type'] == 'boolean') { - $paramSpec['value'] = ($paramSpec['value']) ? 'true' : 'false'; + $paramSpec['value'] = $paramSpec['value'] ? 'true' : 'false'; } if ($paramSpec['location'] == 'path') { $uriTemplateVars[$paramName] = $paramSpec['value']; diff --git a/src/Google/Task/Runner.php b/src/Google/Task/Runner.php index 9d7f6c932..2f25e990e 100644 --- a/src/Google/Task/Runner.php +++ b/src/Google/Task/Runner.php @@ -55,10 +55,6 @@ class Google_Task_Runner */ private $maxAttempts = 1; - /** - * @var string $name The name of the current task (used for logging). - */ - private $name; /** * @var callable $action The task to run and possibly retry. */ @@ -153,7 +149,6 @@ public function __construct( ); } - $this->name = $name; $this->action = $action; $this->arguments = $arguments; } @@ -269,8 +264,10 @@ public function allowedRetries($code, $errors = array()) return $this->retryMap[$code]; } - if (!empty($errors) && isset($errors[0]['reason']) && - isset($this->retryMap[$errors[0]['reason']])) { + if ( + !empty($errors) && + isset($errors[0]['reason'], $this->retryMap[$errors[0]['reason']]) + ) { return $this->retryMap[$errors[0]['reason']]; } diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 7acb4adec..bbbe94fbb 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -139,8 +139,8 @@ public function tryToGetAnAccessToken(Google_Client $client) private function getClientIdAndSecret() { - $clientId = getenv('GCLOUD_CLIENT_ID') ? getenv('GCLOUD_CLIENT_ID') : null; - $clientSecret = getenv('GCLOUD_CLIENT_SECRET') ? getenv('GCLOUD_CLIENT_SECRET') : null; + $clientId = getenv('GCLOUD_CLIENT_ID') ?: null; + $clientSecret = getenv('GCLOUD_CLIENT_SECRET') ?: null; return array($clientId, $clientSecret); } @@ -182,7 +182,7 @@ public function checkKey() public function loadKey() { - if (file_exists($f = dirname(__FILE__) . DIRECTORY_SEPARATOR . '.apiKey')) { + if (file_exists($f = __DIR__ . DIRECTORY_SEPARATOR . '.apiKey')) { return file_get_contents($f); } } diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 80c732674..72e4eab1a 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -50,7 +50,7 @@ private function checkAuthHandler($http, $className) $middlewares = $property->getValue($stack); $middleware = array_pop($middlewares); - if (is_null($className)) { + if (null === $className) { // only the default middlewares have been added $this->assertEquals(3, count($middlewares)); } else { @@ -60,7 +60,7 @@ private function checkAuthHandler($http, $className) } else { $listeners = $http->getEmitter()->listeners('before'); - if (is_null($className)) { + if (null === $className) { $this->assertEquals(0, count($listeners)); } else { $authClass = sprintf('Google\Auth\Subscriber\%sSubscriber', $className); diff --git a/tests/Google/Service/TasksTest.php b/tests/Google/Service/TasksTest.php index 26e8a8fb6..7d7871985 100644 --- a/tests/Google/Service/TasksTest.php +++ b/tests/Google/Service/TasksTest.php @@ -59,7 +59,7 @@ public function testListTask() } $tasksArray = $tasks->listTasks($list['id']); - $this->assertTrue(sizeof($tasksArray) > 1); + $this->assertTrue(count($tasksArray) > 1); foreach ($tasksArray['items'] as $task) { $this->assertIsTask($task); } diff --git a/tests/Google/ServiceTest.php b/tests/Google/ServiceTest.php index a85b147e6..b0e49c3f3 100644 --- a/tests/Google/ServiceTest.php +++ b/tests/Google/ServiceTest.php @@ -108,7 +108,7 @@ class_exists($class), public function serviceProvider() { $classes = array(); - $path = dirname(dirname(dirname(__FILE__))) . '/src/Google/Service'; + $path = dirname(dirname(__DIR__)) . '/src/Google/Service'; foreach (glob($path . "/*.php") as $file) { $classes[] = array('Google_Service_' . basename($file, '.php')); } From a7a6cc6d24a664f1658e5b8cbd408ba989340a78 Mon Sep 17 00:00:00 2001 From: Zarko Stankovic Date: Fri, 31 Mar 2017 20:44:45 +0200 Subject: [PATCH 040/301] Fixed logger property docblock. (#1177) Logger is an implementation of `Psr\Log\LoggerInterface`. --- src/Google/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index 2fb29578c..732148d10 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -72,7 +72,7 @@ class Google_Client private $config; /** - * @var Google_Logger_Abstract $logger + * @var Psr\Log\LoggerInterface $logger */ private $logger; From d317569eb5b374fa522c1de2d186a3010795b92d Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 31 Mar 2017 11:45:07 -0700 Subject: [PATCH 041/301] Improve model contructor api (#1191) * improves Model construction by allowing nested properties through array passing * adds support for passing in created instances of properties in constructor --- src/Google/Collection.php | 36 ++++++++--------- src/Google/Model.php | 21 +++++++++- tests/Google/ModelTest.php | 81 +++++++++++++++++++++++++++++++++++++- 3 files changed, 117 insertions(+), 21 deletions(-) diff --git a/src/Google/Collection.php b/src/Google/Collection.php index 7c478bc02..315dce601 100644 --- a/src/Google/Collection.php +++ b/src/Google/Collection.php @@ -15,31 +15,31 @@ class Google_Collection extends Google_Model implements Iterator, Countable public function rewind() { - if (isset($this->modelData[$this->collection_key]) - && is_array($this->modelData[$this->collection_key])) { - reset($this->modelData[$this->collection_key]); + if (isset($this->{$this->collection_key}) + && is_array($this->{$this->collection_key})) { + reset($this->{$this->collection_key}); } } public function current() { $this->coerceType($this->key()); - if (is_array($this->modelData[$this->collection_key])) { - return current($this->modelData[$this->collection_key]); + if (is_array($this->{$this->collection_key})) { + return current($this->{$this->collection_key}); } } public function key() { - if (isset($this->modelData[$this->collection_key]) - && is_array($this->modelData[$this->collection_key])) { - return key($this->modelData[$this->collection_key]); + if (isset($this->{$this->collection_key}) + && is_array($this->{$this->collection_key})) { + return key($this->{$this->collection_key}); } } public function next() { - return next($this->modelData[$this->collection_key]); + return next($this->{$this->collection_key}); } public function valid() @@ -50,10 +50,10 @@ public function valid() public function count() { - if (!isset($this->modelData[$this->collection_key])) { + if (!isset($this->{$this->collection_key})) { return 0; } - return count($this->modelData[$this->collection_key]); + return count($this->{$this->collection_key}); } public function offsetExists($offset) @@ -61,7 +61,7 @@ public function offsetExists($offset) if (!is_numeric($offset)) { return parent::offsetExists($offset); } - return isset($this->modelData[$this->collection_key][$offset]); + return isset($this->{$this->collection_key}[$offset]); } public function offsetGet($offset) @@ -70,7 +70,7 @@ public function offsetGet($offset) return parent::offsetGet($offset); } $this->coerceType($offset); - return $this->modelData[$this->collection_key][$offset]; + return $this->{$this->collection_key}[$offset]; } public function offsetSet($offset, $value) @@ -78,7 +78,7 @@ public function offsetSet($offset, $value) if (!is_numeric($offset)) { return parent::offsetSet($offset, $value); } - $this->modelData[$this->collection_key][$offset] = $value; + $this->{$this->collection_key}[$offset] = $value; } public function offsetUnset($offset) @@ -86,16 +86,16 @@ public function offsetUnset($offset) if (!is_numeric($offset)) { return parent::offsetUnset($offset); } - unset($this->modelData[$this->collection_key][$offset]); + unset($this->{$this->collection_key}[$offset]); } private function coerceType($offset) { $typeKey = $this->keyType($this->collection_key); - if (isset($this->$typeKey) && !is_object($this->modelData[$this->collection_key][$offset])) { + if (isset($this->$typeKey) && !is_object($this->{$this->collection_key}[$offset])) { $type = $this->$typeKey; - $this->modelData[$this->collection_key][$offset] = - new $type($this->modelData[$this->collection_key][$offset]); + $this->{$this->collection_key}[$offset] = + new $type($this->{$this->collection_key}[$offset]); } } } diff --git a/src/Google/Model.php b/src/Google/Model.php index cd50c9a5e..61ea70a79 100644 --- a/src/Google/Model.php +++ b/src/Google/Model.php @@ -98,8 +98,25 @@ protected function mapTypes($array) { // Hard initialise simple types, lazy load more complex ones. foreach ($array as $key => $val) { - if ( !property_exists($this, $this->keyType($key)) && - property_exists($this, $key)) { + if (property_exists($this, $this->keyType($key))) { + $propertyClass = $this->{$this->keyType($key)}; + $dataType = $this->{$this->dataType($key)}; + if ($dataType == 'array' || $dataType == 'map') { + $this->$key = array(); + foreach ($val as $itemKey => $itemVal) { + if ($itemVal instanceof $propertyClass) { + $this->{$key}[$itemKey] = $itemVal; + } else { + $this->{$key}[$itemKey] = new $propertyClass($itemVal); + } + } + } elseif ($val instanceof $propertyClass) { + $this->$key = $val; + } else { + $this->$key = new $propertyClass($val); + } + unset($array[$key]); + } elseif (property_exists($this, $key)) { $this->$key = $val; unset($array[$key]); } elseif (property_exists($this, $camelKey = $this->camelCase($key))) { diff --git a/tests/Google/ModelTest.php b/tests/Google/ModelTest.php index ec8465773..688115f28 100644 --- a/tests/Google/ModelTest.php +++ b/tests/Google/ModelTest.php @@ -159,7 +159,7 @@ public function testUnsetPropertyOnModel() $this->assertFalse(isset($model->foo)); } - public function testCollection() + public function testCollectionWithItemsFromConstructor() { $data = json_decode( '{ @@ -185,4 +185,83 @@ public function testCollection() $this->assertEquals(4, $count); $this->assertEquals(1, $collection[0]->id); } + + public function testCollectionWithItemsFromSetter() + { + $data = json_decode( + '{ + "kind": "calendar#events", + "id": "1234566", + "etag": "abcdef", + "totalItems": 4 + }', + true + ); + $collection = new Google_Service_Calendar_Events($data); + $collection->setItems([ + new Google_Service_Calendar_Event(['id' => 1]), + new Google_Service_Calendar_Event(['id' => 2]), + new Google_Service_Calendar_Event(['id' => 3]), + new Google_Service_Calendar_Event(['id' => 4]), + ]); + $this->assertEquals(4, count($collection)); + $count = 0; + foreach ($collection as $col) { + $count++; + } + $this->assertEquals(4, $count); + $this->assertEquals(1, $collection[0]->id); + } + + public function testMapDataType() + { + $data = json_decode( + '{ + "calendar": { + "regular": { "background": "#FFF", "foreground": "#000" }, + "inverted": { "background": "#000", "foreground": "#FFF" } + } + }', + true + ); + $collection = new Google_Service_Calendar_Colors($data); + $this->assertEquals(2, count($collection->calendar)); + $this->assertTrue(isset($collection->calendar['regular'])); + $this->assertTrue(isset($collection->calendar['inverted'])); + $this->assertInstanceOf('Google_Service_Calendar_ColorDefinition', $collection->calendar['regular']); + $this->assertEquals('#FFF', $collection->calendar['regular']->getBackground()); + $this->assertEquals('#FFF', $collection->calendar['inverted']->getForeground()); + } + + public function testPassingInstanceInConstructor() + { + $creator = new Google_Service_Calendar_EventCreator(); + $creator->setDisplayName('Brent Shaffer'); + $data = [ + "creator" => $creator + ]; + $event = new Google_Service_Calendar_Event($data); + $this->assertInstanceOf('Google_Service_Calendar_EventCreator', $event->getCreator()); + $this->assertEquals('Brent Shaffer', $event->creator->getDisplayName()); + } + + public function testPassingInstanceInConstructorForMap() + { + $regular = new Google_Service_Calendar_ColorDefinition(); + $regular->setBackground('#FFF'); + $regular->setForeground('#000'); + $data = [ + "calendar" => [ + "regular" => $regular, + "inverted" => [ "background" => "#000", "foreground" => "#FFF" ], + ] + ]; + $collection = new Google_Service_Calendar_Colors($data); + $this->assertEquals(2, count($collection->calendar)); + $this->assertTrue(isset($collection->calendar['regular'])); + $this->assertTrue(isset($collection->calendar['inverted'])); + $this->assertInstanceOf('Google_Service_Calendar_ColorDefinition', $collection->calendar['regular']); + $this->assertEquals('#FFF', $collection->calendar['regular']->getBackground()); + $this->assertEquals('#FFF', $collection->calendar['inverted']->getForeground()); + } } From 37f9ea7532728bb68d476977a4fafee82e4855e3 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 12 Jun 2017 16:41:30 -0700 Subject: [PATCH 042/301] Michaelbausor fix url shortener sample (#1240) --- examples/templates/base.php | 26 ++++++++++++++++++++++++++ examples/url-shortener.php | 21 ++++++++++++++++----- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/examples/templates/base.php b/examples/templates/base.php index 2d3a79f69..7a6f6e6ad 100644 --- a/examples/templates/base.php +++ b/examples/templates/base.php @@ -99,6 +99,16 @@ function missingOAuth2CredentialsWarning() return $ret; } +function invalidCsrfTokenWarning() +{ + $ret = " +

    + The CSRF token is invalid, your session probably expired. Please refresh the page. +

    "; + + return $ret; +} + function checkServiceAccountCredentialsFile() { // service account creds @@ -107,6 +117,22 @@ function checkServiceAccountCredentialsFile() return file_exists($application_creds) ? $application_creds : false; } +function getCsrfToken() +{ + if (!isset($_SESSION['csrf_token'])) { + $_SESSION['csrf_token'] = bin2hex(openssl_random_pseudo_bytes(32)); + } + + return $_SESSION['csrf_token']; +} + +function validateCsrfToken() +{ + return isset($_REQUEST['csrf_token']) + && isset($_SESSION['csrf_token']) + && $_REQUEST['csrf_token'] === $_SESSION['csrf_token']; +} + function getOAuthCredentialsFile() { // oauth2 creds diff --git a/examples/url-shortener.php b/examples/url-shortener.php index f9110984f..1c3deb322 100644 --- a/examples/url-shortener.php +++ b/examples/url-shortener.php @@ -61,6 +61,7 @@ ************************************************/ if (isset($_REQUEST['logout'])) { unset($_SESSION['access_token']); + unset($_SESSION['csrf_token']); } /************************************************ @@ -101,12 +102,17 @@ might happen here is the access token itself is refreshed if the application has offline access. ************************************************/ -if ($client->getAccessToken() && isset($_GET['url'])) { +if ($client->getAccessToken() && isset($_REQUEST['url'])) { + if (!validateCsrfToken()) { + echo invalidCsrfTokenWarning(); + return; + } $url = new Google_Service_Urlshortener_Url(); - $url->longUrl = $_GET['url']; + $url->longUrl = $_REQUEST['url']; $short = $service->url->insert($url); $_SESSION['access_token'] = $client->getAccessToken(); } + ?>
    @@ -115,18 +121,23 @@
    - + + - Logout +
    + + + +
    You created a short link!
    - Create another + Create another From b6969c0b0c1ee5bc35abc43240f758fbe60877b3 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 13 Jun 2017 11:11:25 -0700 Subject: [PATCH 043/301] bumps to latest version of auth client and services (#1242) --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 3f0619c17..5318c4b18 100644 --- a/composer.json +++ b/composer.json @@ -7,8 +7,8 @@ "license": "Apache-2.0", "require": { "php": ">=5.4", - "google/auth": "^0.11", - "google/apiclient-services": "^0.11", + "google/auth": "^1.0", + "google/apiclient-services": "^0.12", "firebase/php-jwt": "~2.0|~3.0|~4.0", "monolog/monolog": "^1.17", "phpseclib/phpseclib": "~0.3.10|~2.0", From bb58ef574a9f012d6aad0a10db87cb7a155e3b76 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 13 Jun 2017 18:32:14 -0700 Subject: [PATCH 044/301] bumps minimum guzzle version to 5.3.1 for php7.1 compatibility (#1241) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 5318c4b18..d86655dd6 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "firebase/php-jwt": "~2.0|~3.0|~4.0", "monolog/monolog": "^1.17", "phpseclib/phpseclib": "~0.3.10|~2.0", - "guzzlehttp/guzzle": "~5.2|~6.0", + "guzzlehttp/guzzle": "~5.3.1|~6.0", "guzzlehttp/psr7": "^1.2" }, "require-dev": { From 7c17a717edf6bc9075d4f3b71a405b5e11b3584c Mon Sep 17 00:00:00 2001 From: ytn3rd Date: Fri, 30 Jun 2017 02:30:07 +1000 Subject: [PATCH 045/301] Added check for if range exists when getting progress for resumable upload. (#1209) --- src/Google/Http/MediaFileUpload.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Google/Http/MediaFileUpload.php b/src/Google/Http/MediaFileUpload.php index bd38a71b6..1ce9bb1d3 100644 --- a/src/Google/Http/MediaFileUpload.php +++ b/src/Google/Http/MediaFileUpload.php @@ -165,8 +165,11 @@ private function makePutRequest(RequestInterface $request) if (308 == $this->httpResultCode) { // Track the amount uploaded. - $range = explode('-', $response->getHeaderLine('range')); - $this->progress = $range[1] + 1; + $range = $response->getHeaderLine('range'); + if ($range) { + $range_array = explode('-', $range); + $this->progress = $range_array[1] + 1; + } // Allow for changing upload URLs. $location = $response->getHeaderLine('location'); From 43612c5a5bec4fce1073e211c0f8b3ed8c342df2 Mon Sep 17 00:00:00 2001 From: Amos Shacham Date: Thu, 29 Jun 2017 19:33:09 +0300 Subject: [PATCH 046/301] fetchAccessTokenWithAuthCode already sets the token (#1251) --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index e2d6eb85a..7fc319705 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,6 @@ foreach ($results as $item) { ```php if (isset($_GET['code'])) { $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); - $client->setAccessToken($token); } ``` From 291c5e852826efa05129a9363077638f2261a639 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 5 Jul 2017 22:31:22 -0700 Subject: [PATCH 047/301] allows for firebase 5.0 (#1258) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d86655dd6..8e217bf59 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "php": ">=5.4", "google/auth": "^1.0", "google/apiclient-services": "^0.12", - "firebase/php-jwt": "~2.0|~3.0|~4.0", + "firebase/php-jwt": "~2.0|~3.0|~4.0|~5.0", "monolog/monolog": "^1.17", "phpseclib/phpseclib": "~0.3.10|~2.0", "guzzlehttp/guzzle": "~5.3.1|~6.0", From 57f9ac3b61dc96ee8e02e3a8d85f45ab8357b7a5 Mon Sep 17 00:00:00 2001 From: Dan O'Meara Date: Fri, 7 Jul 2017 09:35:55 -0700 Subject: [PATCH 048/301] Add notice about maintenance mode (#1259) --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 7fc319705..03cea13d6 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,9 @@ # Google APIs Client Library for PHP # +## Library maintenance +This client library is supported but in maintenance mode only. We are fixing necessary bugs and adding essential features to ensure this library continues to meet your needs for accessing Google APIs. Non-critical issues will be closed. Any issue may be reopened if it is causing ongoing problems. + ## Description ## The Google API Client Library enables you to work with Google APIs such as Google+, Drive, or YouTube on your server. From 8d2d75ff43d864b21a3b6a8e20414bbc4eca4d98 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 10 Jul 2017 08:34:24 -0700 Subject: [PATCH 049/301] allow for any pre-1.0 version of apiclient-services (#1260) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8e217bf59..55129fd68 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "require": { "php": ">=5.4", "google/auth": "^1.0", - "google/apiclient-services": "^0.12", + "google/apiclient-services": "~0.13", "firebase/php-jwt": "~2.0|~3.0|~4.0|~5.0", "monolog/monolog": "^1.17", "phpseclib/phpseclib": "~0.3.10|~2.0", From f3fadd538315d62ebd1191d89ac791468c617260 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 10 Jul 2017 08:34:54 -0700 Subject: [PATCH 050/301] update LIBVER constant (#1243) --- src/Google/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index 732148d10..18955c193 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -39,7 +39,7 @@ */ class Google_Client { - const LIBVER = "2.1.2"; + const LIBVER = "2.2.0"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://accounts.google.com/o/oauth2/revoke'; const OAUTH2_TOKEN_URI = 'https://www.googleapis.com/oauth2/v4/token'; From 9bef2490ed545c9deac78b12efa38ec01527d5f3 Mon Sep 17 00:00:00 2001 From: Claire Ryan Date: Wed, 12 Jul 2017 12:40:33 -0700 Subject: [PATCH 051/301] Added missing is_array check (#1249) --- src/Google/Service/Resource.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Service/Resource.php b/src/Google/Service/Resource.php index a3c57ee2d..fee039b3d 100644 --- a/src/Google/Service/Resource.php +++ b/src/Google/Service/Resource.php @@ -272,7 +272,7 @@ public function createRequestUri($restPath, $params) if ($paramSpec['location'] == 'path') { $uriTemplateVars[$paramName] = $paramSpec['value']; } else if ($paramSpec['location'] == 'query') { - if (isset($paramSpec['repeated']) && is_array($paramSpec['value'])) { + if (is_array($paramSpec['value'])) { foreach ($paramSpec['value'] as $value) { $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($value)); } From ee03fa4d8e9192c2000170b397d08cc08f83f1ee Mon Sep 17 00:00:00 2001 From: Gaspar Zaragoza Date: Thu, 14 Sep 2017 07:04:37 +1200 Subject: [PATCH 052/301] Add missing exception to the catch chain (#1284) --- src/Google/AccessToken/Verify.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Google/AccessToken/Verify.php b/src/Google/AccessToken/Verify.php index 748aa3df8..33f11797d 100644 --- a/src/Google/AccessToken/Verify.php +++ b/src/Google/AccessToken/Verify.php @@ -17,6 +17,7 @@ */ use Firebase\JWT\ExpiredException as ExpiredExceptionV3; +use Firebase\JWT\SignatureInvalidException; use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; use Psr\Cache\CacheItemPoolInterface; @@ -120,6 +121,8 @@ public function verifyIdToken($idToken, $audience = null) return false; } catch (ExpiredExceptionV3 $e) { return false; + } catch (SignatureInvalidException $e) { + // continue } catch (DomainException $e) { // continue } From c144e9926578f1f054aabeaddf6d94b3a3ba600c Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Thu, 14 Sep 2017 07:05:20 +1200 Subject: [PATCH 053/301] Add missing space to README (#1282) --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e852a93ac..d8deaf3a0 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -7,7 +7,7 @@ We'd love to accept your code patches! However, before we can take them, we have Please fill out either the individual or corporate Contributor License Agreement (CLA). * If you are an individual writing original source code and you're sure you own the intellectual property, then you'll need to sign an [individual CLA](http://code.google.com/legal/individual-cla-v1.0.html). - * If you work for a company that wants to allow you to contribute your work to this client library, then you'll need to sign a[corporate CLA](http://code.google.com/legal/corporate-cla-v1.0.html). + * If you work for a company that wants to allow you to contribute your work to this client library, then you'll need to sign a [corporate CLA](http://code.google.com/legal/corporate-cla-v1.0.html). Follow either of the two links above to access the appropriate CLA and instructions for how to sign and return it. Once we receive it, we'll add you to the official list of contributors and be able to accept your patches. From f3a84d107c09d92d7c160b1bc51d025dc108c29b Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 25 Sep 2017 14:37:27 -0700 Subject: [PATCH 054/301] addresse keytype conflict (#1310) --- src/Google/Collection.php | 7 +++-- src/Google/Model.php | 55 +++++++++++++++++--------------------- tests/Google/ModelTest.php | 14 ++++++++++ 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/src/Google/Collection.php b/src/Google/Collection.php index 315dce601..df8d444d3 100644 --- a/src/Google/Collection.php +++ b/src/Google/Collection.php @@ -91,11 +91,10 @@ public function offsetUnset($offset) private function coerceType($offset) { - $typeKey = $this->keyType($this->collection_key); - if (isset($this->$typeKey) && !is_object($this->{$this->collection_key}[$offset])) { - $type = $this->$typeKey; + $keyType = $this->keyType($this->collection_key); + if ($keyType && !is_object($this->{$this->collection_key}[$offset])) { $this->{$this->collection_key}[$offset] = - new $type($this->{$this->collection_key}[$offset]); + new $keyType($this->{$this->collection_key}[$offset]); } } } diff --git a/src/Google/Model.php b/src/Google/Model.php index 61ea70a79..18262608b 100644 --- a/src/Google/Model.php +++ b/src/Google/Model.php @@ -53,32 +53,30 @@ final public function __construct() */ public function __get($key) { - $keyTypeName = $this->keyType($key); + $keyType = $this->keyType($key); $keyDataType = $this->dataType($key); - if (isset($this->$keyTypeName) && !isset($this->processed[$key])) { + if ($keyType && !isset($this->processed[$key])) { if (isset($this->modelData[$key])) { $val = $this->modelData[$key]; - } else if (isset($this->$keyDataType) && - ($this->$keyDataType == 'array' || $this->$keyDataType == 'map')) { + } elseif ($keyDataType == 'array' || $keyDataType == 'map') { $val = array(); } else { $val = null; } if ($this->isAssociativeArray($val)) { - if (isset($this->$keyDataType) && 'map' == $this->$keyDataType) { + if ($keyDataType && 'map' == $keyDataType) { foreach ($val as $arrayKey => $arrayItem) { $this->modelData[$key][$arrayKey] = - $this->createObjectFromName($keyTypeName, $arrayItem); + new $keyType($arrayItem); } } else { - $this->modelData[$key] = $this->createObjectFromName($keyTypeName, $val); + $this->modelData[$key] = new $keyType($val); } } else if (is_array($val)) { $arrayObject = array(); foreach ($val as $arrayIndex => $arrayItem) { - $arrayObject[$arrayIndex] = - $this->createObjectFromName($keyTypeName, $arrayItem); + $arrayObject[$arrayIndex] = new $keyType($arrayItem); } $this->modelData[$key] = $arrayObject; } @@ -98,22 +96,21 @@ protected function mapTypes($array) { // Hard initialise simple types, lazy load more complex ones. foreach ($array as $key => $val) { - if (property_exists($this, $this->keyType($key))) { - $propertyClass = $this->{$this->keyType($key)}; - $dataType = $this->{$this->dataType($key)}; + if ($keyType = $this->keyType($key)) { + $dataType = $this->dataType($key); if ($dataType == 'array' || $dataType == 'map') { $this->$key = array(); foreach ($val as $itemKey => $itemVal) { - if ($itemVal instanceof $propertyClass) { + if ($itemVal instanceof $keyType) { $this->{$key}[$itemKey] = $itemVal; } else { - $this->{$key}[$itemKey] = new $propertyClass($itemVal); + $this->{$key}[$itemKey] = new $keyType($itemVal); } } - } elseif ($val instanceof $propertyClass) { + } elseif ($val instanceof $keyType) { $this->$key = $val; } else { - $this->$key = new $propertyClass($val); + $this->$key = new $keyType($val); } unset($array[$key]); } elseif (property_exists($this, $key)) { @@ -234,19 +231,6 @@ protected function isAssociativeArray($array) return false; } - /** - * Given a variable name, discover its type. - * - * @param $name - * @param $item - * @return object The object from the item. - */ - private function createObjectFromName($name, $item) - { - $type = $this->$name; - return new $type($item); - } - /** * Verify if $obj is an array. * @throws Google_Exception Thrown if $obj isn't an array. @@ -291,12 +275,21 @@ public function offsetUnset($offset) protected function keyType($key) { - return $key . "Type"; + $keyType = $key . "Type"; + + // ensure keyType is a valid class + if (property_exists($this, $keyType) && class_exists($this->$keyType)) { + return $this->$keyType; + } } protected function dataType($key) { - return $key . "DataType"; + $dataType = $key . "DataType"; + + if (property_exists($this, $dataType)) { + return $this->$dataType; + } } public function __isset($key) diff --git a/tests/Google/ModelTest.php b/tests/Google/ModelTest.php index 688115f28..64f306015 100644 --- a/tests/Google/ModelTest.php +++ b/tests/Google/ModelTest.php @@ -264,4 +264,18 @@ public function testPassingInstanceInConstructorForMap() $this->assertEquals('#FFF', $collection->calendar['regular']->getBackground()); $this->assertEquals('#FFF', $collection->calendar['inverted']->getForeground()); } + + /** + * @see https://github.com/google/google-api-php-client/issues/1308 + */ + public function testKeyTypePropertyConflict() + { + $data = [ + "duration" => 0, + "durationType" => "unknown", + ]; + $creativeAsset = new Google_Service_Dfareporting_CreativeAsset($data); + $this->assertEquals(0, $creativeAsset->getDuration()); + $this->assertEquals('unknown', $creativeAsset->getDurationType()); + } } From b69b8ac4bf6501793c389d4e013a79d09c85c5f2 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 2 Nov 2017 18:19:53 -0700 Subject: [PATCH 055/301] increment Google_Client::LIBVER to 2.2.1 (#1311) --- src/Google/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index 18955c193..8f8329315 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -39,7 +39,7 @@ */ class Google_Client { - const LIBVER = "2.2.0"; + const LIBVER = "2.2.1"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://accounts.google.com/o/oauth2/revoke'; const OAUTH2_TOKEN_URI = 'https://www.googleapis.com/oauth2/v4/token'; From 0c4a7b6eaefe8b0a6334c044feab9790095b2ddb Mon Sep 17 00:00:00 2001 From: Gabriel Caruso Date: Wed, 8 Nov 2017 17:28:50 -0200 Subject: [PATCH 056/301] Use PHPUnit\Framework\TestCase instead of PHPUnit_Framework_TestCase (#1336) --- composer.json | 2 +- tests/BaseTest.php | 3 ++- tests/Google/ServiceTest.php | 4 +++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 55129fd68..9f00dab71 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "guzzlehttp/psr7": "^1.2" }, "require-dev": { - "phpunit/phpunit": "~4", + "phpunit/phpunit": "~4.8.36", "squizlabs/php_codesniffer": "~2.3", "symfony/dom-crawler": "~2.1", "symfony/css-selector": "~2.1", diff --git a/tests/BaseTest.php b/tests/BaseTest.php index bbbe94fbb..b1c3fc906 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -20,8 +20,9 @@ use League\Flysystem\Adapter\Local; use League\Flysystem\Filesystem; use Cache\Adapter\Filesystem\FilesystemCachePool; +use PHPUnit\Framework\TestCase; -class BaseTest extends PHPUnit_Framework_TestCase +class BaseTest extends TestCase { private $key; private $client; diff --git a/tests/Google/ServiceTest.php b/tests/Google/ServiceTest.php index b0e49c3f3..b6b5d218c 100644 --- a/tests/Google/ServiceTest.php +++ b/tests/Google/ServiceTest.php @@ -18,6 +18,8 @@ * under the License. */ +use PHPUnit\Framework\TestCase; + class TestModel extends Google_Model { public function mapTypes($array) @@ -36,7 +38,7 @@ class TestService extends Google_Service public $batchPath = 'batch/test'; } -class Google_ServiceTest extends PHPUnit_Framework_TestCase +class Google_ServiceTest extends TestCase { public function testCreateBatch() { From 120181cdcb90e198efd19fd8c87b9cc639002974 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 30 Nov 2017 15:03:31 -0600 Subject: [PATCH 057/301] Add PHP 7.2 in preparation of the release tomorrow (#1351) --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index a7869dfee..c3b1c8e1a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,6 +21,7 @@ php: - 5.6 - 7.0 - 7.1 + - 7.2 # Test lowest dependencies on PHP 5.4 # (Guzzle 5.2, phpseclib 0.3) From a11ca6c55cb3527ee0d14dc402ab7ea0e1d12ca4 Mon Sep 17 00:00:00 2001 From: Gabriel Caruso Date: Thu, 30 Nov 2017 19:04:26 -0200 Subject: [PATCH 058/301] Refactoring tests (#1352) --- tests/Google/AccessToken/VerifyTest.php | 6 +++--- tests/Google/ClientTest.php | 10 +++++----- tests/Google/ModelTest.php | 12 ++++++------ tests/Google/ServiceTest.php | 16 ++++++++-------- tests/examples/batchTest.php | 4 ++-- tests/examples/idTokenTest.php | 4 ++-- tests/examples/indexTest.php | 2 +- tests/examples/largeFileDownloadTest.php | 6 +++--- tests/examples/largeFileUploadTest.php | 4 ++-- tests/examples/multiApiTest.php | 2 +- tests/examples/simpleFileUploadTest.php | 6 +++--- tests/examples/simpleQueryTest.php | 2 +- tests/examples/urlShortenerTest.php | 2 +- 13 files changed, 38 insertions(+), 38 deletions(-) diff --git a/tests/Google/AccessToken/VerifyTest.php b/tests/Google/AccessToken/VerifyTest.php index 351814324..aef6ec4de 100644 --- a/tests/Google/AccessToken/VerifyTest.php +++ b/tests/Google/AccessToken/VerifyTest.php @@ -46,7 +46,7 @@ public function testPhpsecConstants() $openSslEnable = constant('MATH_BIGINTEGER_OPENSSL_ENABLED'); $rsaMode = constant('CRYPT_RSA_MODE'); - $this->assertEquals(true, $openSslEnable); + $this->assertTrue($openSslEnable); $this->assertEquals(constant($this->getOpenSslConstant()), $rsaMode); } @@ -67,7 +67,7 @@ public function testValidateIdToken() $token = $client->fetchAccessTokenWithRefreshToken(); } $segments = explode('.', $token['id_token']); - $this->assertEquals(3, count($segments)); + $this->assertCount(3, $segments); // Extract the client ID in this case as it wont be set on the test client. $data = json_decode($jwt->urlSafeB64Decode($segments[1])); $verify = new Google_AccessToken_Verify($http); @@ -105,7 +105,7 @@ public function testLeewayIsUnchangedWhenPassingInJwt() $token = $client->fetchAccessTokenWithRefreshToken(); } $segments = explode('.', $token['id_token']); - $this->assertEquals(3, count($segments)); + $this->assertCount(3, $segments); // Extract the client ID in this case as it wont be set on the test client. $data = json_decode($jwt->urlSafeB64Decode($segments[1])); $verify = new Google_AccessToken_Verify($client->getHttpClient(), null, $jwt); diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 72e4eab1a..ef61de326 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -52,7 +52,7 @@ private function checkAuthHandler($http, $className) if (null === $className) { // only the default middlewares have been added - $this->assertEquals(3, count($middlewares)); + $this->assertCount(3, $middlewares); } else { $authClass = sprintf('Google\Auth\Middleware\%sMiddleware', $className); $this->assertInstanceOf($authClass, $middleware[0]); @@ -61,11 +61,11 @@ private function checkAuthHandler($http, $className) $listeners = $http->getEmitter()->listeners('before'); if (null === $className) { - $this->assertEquals(0, count($listeners)); + $this->assertCount(0, $listeners); } else { $authClass = sprintf('Google\Auth\Subscriber\%sSubscriber', $className); - $this->assertEquals(1, count($listeners)); - $this->assertEquals(2, count($listeners[0])); + $this->assertCount(1, $listeners); + $this->assertCount(2, $listeners[0]); $this->assertInstanceOf($authClass, $listeners[0][0]); } } @@ -180,7 +180,7 @@ public function testPrepareNoScopes() $client = new Google_Client(); $scopes = $client->prepareScopes(); - $this->assertEquals(null, $scopes); + $this->assertNull($scopes); } public function testNoAuthIsNull() diff --git a/tests/Google/ModelTest.php b/tests/Google/ModelTest.php index 64f306015..d1480868b 100644 --- a/tests/Google/ModelTest.php +++ b/tests/Google/ModelTest.php @@ -138,9 +138,9 @@ public function testJsonStructure() $this->assertArrayHasKey("publicE", $data['publicG'][0]); $this->assertArrayNotHasKey("publicF", $data['publicG'][0]); $this->assertEquals("hello", $data['publicG'][1]); - $this->assertEquals(false, $data['publicG'][2]); + $this->assertFalse($data['publicG'][2]); $this->assertArrayNotHasKey("data", $data); - $this->assertEquals(false, $data['publicH']); + $this->assertFalse($data['publicH']); $this->assertEquals(0, $data['publicI']); } @@ -177,7 +177,7 @@ public function testCollectionWithItemsFromConstructor() true ); $collection = new Google_Service_Calendar_Events($data); - $this->assertEquals(4, count($collection)); + $this->assertCount(4, $collection); $count = 0; foreach ($collection as $col) { $count++; @@ -204,7 +204,7 @@ public function testCollectionWithItemsFromSetter() new Google_Service_Calendar_Event(['id' => 3]), new Google_Service_Calendar_Event(['id' => 4]), ]); - $this->assertEquals(4, count($collection)); + $this->assertCount(4, $collection); $count = 0; foreach ($collection as $col) { $count++; @@ -225,7 +225,7 @@ public function testMapDataType() true ); $collection = new Google_Service_Calendar_Colors($data); - $this->assertEquals(2, count($collection->calendar)); + $this->assertCount(2, $collection->calendar); $this->assertTrue(isset($collection->calendar['regular'])); $this->assertTrue(isset($collection->calendar['inverted'])); $this->assertInstanceOf('Google_Service_Calendar_ColorDefinition', $collection->calendar['regular']); @@ -257,7 +257,7 @@ public function testPassingInstanceInConstructorForMap() ] ]; $collection = new Google_Service_Calendar_Colors($data); - $this->assertEquals(2, count($collection->calendar)); + $this->assertCount(2, $collection->calendar); $this->assertTrue(isset($collection->calendar['regular'])); $this->assertTrue(isset($collection->calendar['inverted'])); $this->assertInstanceOf('Google_Service_Calendar_ColorDefinition', $collection->calendar['regular']); diff --git a/tests/Google/ServiceTest.php b/tests/Google/ServiceTest.php index b6b5d218c..ba5cc0831 100644 --- a/tests/Google/ServiceTest.php +++ b/tests/Google/ServiceTest.php @@ -85,15 +85,15 @@ public function testModel() $this->assertEquals('asdf', $model->name); $this->assertEquals('z', $model->gender); - $this->assertEquals(false, $model->isAssociativeArray("")); - $this->assertEquals(false, $model->isAssociativeArray(false)); - $this->assertEquals(false, $model->isAssociativeArray(null)); - $this->assertEquals(false, $model->isAssociativeArray(array())); - $this->assertEquals(false, $model->isAssociativeArray(array(1, 2))); - $this->assertEquals(false, $model->isAssociativeArray(array(1 => 2))); + $this->assertFalse($model->isAssociativeArray("")); + $this->assertFalse($model->isAssociativeArray(false)); + $this->assertFalse($model->isAssociativeArray(null)); + $this->assertFalse($model->isAssociativeArray(array())); + $this->assertFalse($model->isAssociativeArray(array(1, 2))); + $this->assertFalse($model->isAssociativeArray(array(1 => 2))); - $this->assertEquals(true, $model->isAssociativeArray(array('test' => 'a'))); - $this->assertEquals(true, $model->isAssociativeArray(array("a", "b" => 2))); + $this->assertTrue($model->isAssociativeArray(array('test' => 'a'))); + $this->assertTrue($model->isAssociativeArray(array("a", "b" => 2))); } /** diff --git a/tests/examples/batchTest.php b/tests/examples/batchTest.php index c16ef8bea..1db795925 100644 --- a/tests/examples/batchTest.php +++ b/tests/examples/batchTest.php @@ -28,8 +28,8 @@ public function testBatch() $crawler = $this->loadExample('batch.php'); $nodes = $crawler->filter('br'); - $this->assertEquals(20, count($nodes)); + $this->assertCount(20, $nodes); $this->assertContains('Life of Henry David Thoreau', $crawler->text()); $this->assertContains('George Bernard Shaw His Life and Works', $crawler->text()); } -} \ No newline at end of file +} diff --git a/tests/examples/idTokenTest.php b/tests/examples/idTokenTest.php index 4f44bc3a4..acc9fd1d8 100644 --- a/tests/examples/idTokenTest.php +++ b/tests/examples/idTokenTest.php @@ -28,11 +28,11 @@ public function testIdToken() $crawler = $this->loadExample('idtoken.php'); $nodes = $crawler->filter('h1'); - $this->assertEquals(1, count($nodes)); + $this->assertCount(1, $nodes); $this->assertEquals('Retrieving An Id Token', $nodes->first()->text()); $nodes = $crawler->filter('a.login'); - $this->assertEquals(1, count($nodes)); + $this->assertCount(1, $nodes); $this->assertEquals('Connect Me!', $nodes->first()->text()); } } \ No newline at end of file diff --git a/tests/examples/indexTest.php b/tests/examples/indexTest.php index f0ba28313..d6a8cba67 100644 --- a/tests/examples/indexTest.php +++ b/tests/examples/indexTest.php @@ -26,7 +26,7 @@ public function testIndex() $crawler = $this->loadExample('index.php'); $nodes = $crawler->filter('li'); - $this->assertEquals(9, count($nodes)); + $this->assertCount(9, $nodes); $this->assertEquals('A query using simple API access', $nodes->first()->text()); } } \ No newline at end of file diff --git a/tests/examples/largeFileDownloadTest.php b/tests/examples/largeFileDownloadTest.php index 764b094f6..538b3ff70 100644 --- a/tests/examples/largeFileDownloadTest.php +++ b/tests/examples/largeFileDownloadTest.php @@ -27,11 +27,11 @@ public function testSimpleFileDownloadNoToken() $crawler = $this->loadExample('large-file-download.php'); $nodes = $crawler->filter('h1'); - $this->assertEquals(1, count($nodes)); + $this->assertCount(1, $nodes); $this->assertEquals('File Download - Downloading a large file', $nodes->first()->text()); $nodes = $crawler->filter('a.login'); - $this->assertEquals(1, count($nodes)); + $this->assertCount(1, $nodes); $this->assertEquals('Connect Me!', $nodes->first()->text()); } @@ -46,7 +46,7 @@ public function testSimpleFileDownloadWithToken() $buttonText = 'Click here to download a large (20MB) test file'; $nodes = $crawler->filter('input'); - $this->assertEquals(1, count($nodes)); + $this->assertCount(1, $nodes); $this->assertEquals($buttonText, $nodes->first()->attr('value')); } } \ No newline at end of file diff --git a/tests/examples/largeFileUploadTest.php b/tests/examples/largeFileUploadTest.php index edbc0a259..ca9848879 100644 --- a/tests/examples/largeFileUploadTest.php +++ b/tests/examples/largeFileUploadTest.php @@ -28,11 +28,11 @@ public function testLargeFileUpload() $crawler = $this->loadExample('large-file-upload.php'); $nodes = $crawler->filter('h1'); - $this->assertEquals(1, count($nodes)); + $this->assertCount(1, $nodes); $this->assertEquals('File Upload - Uploading a large file', $nodes->first()->text()); $nodes = $crawler->filter('a.login'); - $this->assertEquals(1, count($nodes)); + $this->assertCount(1, $nodes); $this->assertEquals('Connect Me!', $nodes->first()->text()); } } \ No newline at end of file diff --git a/tests/examples/multiApiTest.php b/tests/examples/multiApiTest.php index 47d2b178b..51e0a9b2b 100644 --- a/tests/examples/multiApiTest.php +++ b/tests/examples/multiApiTest.php @@ -28,7 +28,7 @@ public function testMultiApi() $crawler = $this->loadExample('multi-api.php'); $nodes = $crawler->filter('h1'); - $this->assertEquals(1, count($nodes)); + $this->assertCount(1, $nodes); $this->assertEquals('User Query - Multiple APIs', $nodes->first()->text()); } } \ No newline at end of file diff --git a/tests/examples/simpleFileUploadTest.php b/tests/examples/simpleFileUploadTest.php index d81ccef01..54dce650a 100644 --- a/tests/examples/simpleFileUploadTest.php +++ b/tests/examples/simpleFileUploadTest.php @@ -28,11 +28,11 @@ public function testSimpleFileUploadNoToken() $crawler = $this->loadExample('simple-file-upload.php'); $nodes = $crawler->filter('h1'); - $this->assertEquals(1, count($nodes)); + $this->assertCount(1, $nodes); $this->assertEquals('File Upload - Uploading a simple file', $nodes->first()->text()); $nodes = $crawler->filter('a.login'); - $this->assertEquals(1, count($nodes)); + $this->assertCount(1, $nodes); $this->assertEquals('Connect Me!', $nodes->first()->text()); } @@ -47,7 +47,7 @@ public function testSimpleFileUploadWithToken() $buttonText = 'Click here to upload two small (1MB) test files'; $nodes = $crawler->filter('input'); - $this->assertEquals(1, count($nodes)); + $this->assertCount(1, $nodes); $this->assertEquals($buttonText, $nodes->first()->attr('value')); } } \ No newline at end of file diff --git a/tests/examples/simpleQueryTest.php b/tests/examples/simpleQueryTest.php index ad0f3b34e..3a302475c 100644 --- a/tests/examples/simpleQueryTest.php +++ b/tests/examples/simpleQueryTest.php @@ -31,7 +31,7 @@ public function testSimpleQuery() $this->assertEquals(20, count($nodes)); $nodes = $crawler->filter('h1'); - $this->assertEquals(1, count($nodes)); + $this->assertCount(1, $nodes); $this->assertEquals('Simple API Access', $nodes->first()->text()); } } \ No newline at end of file diff --git a/tests/examples/urlShortenerTest.php b/tests/examples/urlShortenerTest.php index ea9d60c29..fc4b9aa7f 100644 --- a/tests/examples/urlShortenerTest.php +++ b/tests/examples/urlShortenerTest.php @@ -28,7 +28,7 @@ public function testUrlShortener() $crawler = $this->loadExample('url-shortener.php'); $nodes = $crawler->filter('h1'); - $this->assertEquals(1, count($nodes)); + $this->assertCount(1, $nodes); $this->assertEquals('User Query - URL Shortener', $nodes->first()->text()); } } \ No newline at end of file From 4b0ebf8b7e63a7a70cd844ab2e4895124dfbeca1 Mon Sep 17 00:00:00 2001 From: Andrew Berry Date: Tue, 5 Dec 2017 09:12:30 -0500 Subject: [PATCH 059/301] Document that not all APIs support service accounts --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 03cea13d6..0a34966e2 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,11 @@ foreach ($results as $item) { > An example of this can be seen in [`examples/service-account.php`](examples/service-account.php). +Some APIs +(such as the [YouTube Data API](https://developers.google.com/youtube/v3/)) do +not support service accounts. Check with the specific API documentation if API +calls return unexpected 401 or 403 errors. + 1. Follow the instructions to [Create a Service Account](https://developers.google.com/api-client-library/php/auth/service-accounts#creatinganaccount) 1. Download the JSON credentials 1. Set the path to these credentials using the `GOOGLE_APPLICATION_CREDENTIALS` environment variable: From 3eea433cec0816860583440ec48b25a91bcf8cde Mon Sep 17 00:00:00 2001 From: Galymzhan Date: Wed, 6 Dec 2017 01:45:34 +0000 Subject: [PATCH 060/301] fix certificate caching, fixes #1033 (#1322) --- src/Google/AccessToken/Verify.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Google/AccessToken/Verify.php b/src/Google/AccessToken/Verify.php index 33f11797d..09d71b9dd 100644 --- a/src/Google/AccessToken/Verify.php +++ b/src/Google/AccessToken/Verify.php @@ -178,7 +178,7 @@ private function getFederatedSignOnCerts() { $certs = null; if ($cache = $this->getCache()) { - $cacheItem = $cache->getItem('federated_signon_certs_v3', 3600); + $cacheItem = $cache->getItem('federated_signon_certs_v3'); $certs = $cacheItem->get(); } @@ -189,6 +189,7 @@ private function getFederatedSignOnCerts() ); if ($cache) { + $cacheItem->expiresAt(new DateTime('+1 hour')); $cacheItem->set($certs); $cache->save($cacheItem); } From c4449fb364e572218b833861b750696cd8c62b81 Mon Sep 17 00:00:00 2001 From: Gabriel Caruso Date: Wed, 6 Dec 2017 19:21:10 -0200 Subject: [PATCH 061/301] Refactoring tests (#1355) --- tests/Google/AccessToken/VerifyTest.php | 8 ++++---- tests/Google/Http/BatchTest.php | 21 +++++++++++---------- tests/Google/Http/MediaFileUploadTest.php | 2 +- tests/Google/Service/PlusTest.php | 8 ++++---- tests/Google/Service/TasksTest.php | 2 +- tests/examples/serviceAccountTest.php | 4 ++-- tests/examples/simpleQueryTest.php | 4 ++-- 7 files changed, 25 insertions(+), 24 deletions(-) diff --git a/tests/Google/AccessToken/VerifyTest.php b/tests/Google/AccessToken/VerifyTest.php index aef6ec4de..e72c14897 100644 --- a/tests/Google/AccessToken/VerifyTest.php +++ b/tests/Google/AccessToken/VerifyTest.php @@ -72,8 +72,8 @@ public function testValidateIdToken() $data = json_decode($jwt->urlSafeB64Decode($segments[1])); $verify = new Google_AccessToken_Verify($http); $payload = $verify->verifyIdToken($token['id_token'], $data->aud); - $this->assertTrue(isset($payload['sub'])); - $this->assertTrue(strlen($payload['sub']) > 0); + $this->assertArrayHasKey('sub', $payload); + $this->assertGreaterThan(0, strlen($payload['sub'])); // TODO: Need to be smart about testing/disabling the // caching for this test to make sense. Not sure how to do that @@ -83,8 +83,8 @@ public function testValidateIdToken() $data = json_decode($jwt->urlSafeB64Decode($segments[1])); $verify = new Google_AccessToken_Verify($http); $payload = $verify->verifyIdToken($token['id_token'], $data->aud); - $this->assertTrue(isset($payload['sub'])); - $this->assertTrue(strlen($payload['sub']) > 0); + $this->assertArrayHasKey('sub', $payload); + $this->assertGreaterThan(0, strlen($payload['sub'])); } /** diff --git a/tests/Google/Http/BatchTest.php b/tests/Google/Http/BatchTest.php index 78a947eed..4ca7d18f2 100644 --- a/tests/Google/Http/BatchTest.php +++ b/tests/Google/Http/BatchTest.php @@ -36,9 +36,10 @@ public function testBatchRequestWithAuth() $batch->add($plus->people->get('me'), 'key3'); $result = $batch->execute(); - $this->assertTrue(isset($result['response-key1'])); - $this->assertTrue(isset($result['response-key2'])); - $this->assertTrue(isset($result['response-key3'])); + $this->assertArrayHasKey('response-key1', $result); + $this->assertArrayHasKey('response-key2', $result); + $this->assertArrayHasKey('response-key3', $result); + } public function testBatchRequest() @@ -53,9 +54,9 @@ public function testBatchRequest() $batch->add($plus->people->get('+LarryPage'), 'key3'); $result = $batch->execute(); - $this->assertTrue(isset($result['response-key1'])); - $this->assertTrue(isset($result['response-key2'])); - $this->assertTrue(isset($result['response-key3'])); + $this->assertArrayHasKey('response-key1', $result); + $this->assertArrayHasKey('response-key2', $result); + $this->assertArrayHasKey('response-key3', $result); } public function testBatchRequestWithPostBody() @@ -78,9 +79,9 @@ public function testBatchRequestWithPostBody() $batch->add($shortener->url->insert($url3), 'key3'); $result = $batch->execute(); - $this->assertTrue(isset($result['response-key1'])); - $this->assertTrue(isset($result['response-key2'])); - $this->assertTrue(isset($result['response-key3'])); + $this->assertArrayHasKey('response-key1', $result); + $this->assertArrayHasKey('response-key2', $result); + $this->assertArrayHasKey('response-key3', $result); } public function testInvalidBatchRequest() @@ -94,7 +95,7 @@ public function testInvalidBatchRequest() $batch->add($plus->people->get('+LarryPage'), 'key2'); $result = $batch->execute(); - $this->assertTrue(isset($result['response-key2'])); + $this->assertArrayHasKey('response-key2', $result); $this->assertInstanceOf( 'Google_Service_Exception', $result['response-key1'] diff --git a/tests/Google/Http/MediaFileUploadTest.php b/tests/Google/Http/MediaFileUploadTest.php index a53663326..911424ad8 100644 --- a/tests/Google/Http/MediaFileUploadTest.php +++ b/tests/Google/Http/MediaFileUploadTest.php @@ -114,7 +114,7 @@ public function testGetResumeUri() // request the resumable url $uri = $media->getResumeUri(); - $this->assertTrue(is_string($uri)); + $this->assertInternalType('string', $uri); // parse the URL $parts = parse_url(http://github.com/$uri); diff --git a/tests/Google/Service/PlusTest.php b/tests/Google/Service/PlusTest.php index edd012f52..c3e62a249 100644 --- a/tests/Google/Service/PlusTest.php +++ b/tests/Google/Service/PlusTest.php @@ -58,12 +58,12 @@ public function assertItem($item) { // assertArrayHasKey uses array_key_exists, which is not great: // it doesn't understand SPL ArrayAccess - $this->assertTrue(isset($item['actor'])); + $this->assertArrayHasKey('actor', $item); $this->assertInstanceOf('Google_Service_Plus_ActivityActor', $item->actor); $this->assertTrue(isset($item['actor']['displayName'])); $this->assertTrue(isset($item['actor']->url)); - $this->assertTrue(isset($item['object'])); - $this->assertTrue(isset($item['access'])); - $this->assertTrue(isset($item['provider'])); + $this->assertArrayHasKey('object', $item); + $this->assertArrayHasKey('access', $item); + $this->assertArrayHasKey('provider', $item); } } diff --git a/tests/Google/Service/TasksTest.php b/tests/Google/Service/TasksTest.php index 7d7871985..0cae03732 100644 --- a/tests/Google/Service/TasksTest.php +++ b/tests/Google/Service/TasksTest.php @@ -59,7 +59,7 @@ public function testListTask() } $tasksArray = $tasks->listTasks($list['id']); - $this->assertTrue(count($tasksArray) > 1); + $this->assertGreaterThan(1, count($tasksArray)); foreach ($tasksArray['items'] as $task) { $this->assertIsTask($task); } diff --git a/tests/examples/serviceAccountTest.php b/tests/examples/serviceAccountTest.php index b12f6557d..261059d8a 100644 --- a/tests/examples/serviceAccountTest.php +++ b/tests/examples/serviceAccountTest.php @@ -28,7 +28,7 @@ public function testServiceAccount() $crawler = $this->loadExample('service-account.php'); $nodes = $crawler->filter('br'); - $this->assertEquals(10, count($nodes)); + $this->assertCount(10, $nodes); $this->assertContains('Life of Henry David Thoreau', $crawler->text()); } -} \ No newline at end of file +} diff --git a/tests/examples/simpleQueryTest.php b/tests/examples/simpleQueryTest.php index 3a302475c..274332aca 100644 --- a/tests/examples/simpleQueryTest.php +++ b/tests/examples/simpleQueryTest.php @@ -28,10 +28,10 @@ public function testSimpleQuery() $crawler = $this->loadExample('simple-query.php'); $nodes = $crawler->filter('br'); - $this->assertEquals(20, count($nodes)); + $this->assertCount(20, $nodes); $nodes = $crawler->filter('h1'); $this->assertCount(1, $nodes); $this->assertEquals('Simple API Access', $nodes->first()->text()); } -} \ No newline at end of file +} From 2f44829a6a3cc42cb9b4bb9f407306a368589b3b Mon Sep 17 00:00:00 2001 From: Michael Bausor Date: Fri, 5 Jan 2018 08:58:00 -0800 Subject: [PATCH 062/301] Remove beta --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 03cea13d6..3cfd7a3ad 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,6 @@ This client library is supported but in maintenance mode only. We are fixing ne ## Description ## The Google API Client Library enables you to work with Google APIs such as Google+, Drive, or YouTube on your server. -## Beta ## -This library is in Beta. We're comfortable enough with the stability and features of the library that we want you to build real production applications on it. We will make an effort to support the public and protected surface of the library and maintain backwards compatibility in the future. While we are still in Beta, we reserve the right to make incompatible changes. - ## Requirements ## * [PHP 5.4.0 or higher](http://www.php.net/) From b071f1f8474d8073a35d95a7e4690ef0ecf9d0c3 Mon Sep 17 00:00:00 2001 From: Phobetor Date: Fri, 26 Jan 2018 21:12:17 +0100 Subject: [PATCH 063/301] Fix method documentation in Client.php (#1317) --- src/Google/Client.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index 8f8329315..e88dda98a 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -25,7 +25,6 @@ use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; use GuzzleHttp\Ring\Client\StreamHandler; -use GuzzleHttp\Psr7; use Psr\Cache\CacheItemPoolInterface; use Psr\Http\Message\RequestInterface; use Psr\Log\LoggerInterface; @@ -211,7 +210,7 @@ public function refreshTokenWithAssertion() /** * Fetches a fresh access token with a given assertion token. - * @param $assertionCredentials optional. + * @param ClientInterface $authHttp optional. * @return array access token */ public function fetchAccessTokenWithAssertion(ClientInterface $authHttp = null) @@ -441,11 +440,16 @@ public function getAccessToken() return $this->token; } + /** + * @return string|null + */ public function getRefreshToken() { if (isset($this->token['refresh_token'])) { return $this->token['refresh_token']; } + + return null; } /** @@ -480,6 +484,9 @@ public function isAccessTokenExpired() return ($created + ($this->token['expires_in'] - 30)) < time(); } + /** + * @deprecated See UPGRADING.md for more information + */ public function getAuth() { throw new BadMethodCallException( @@ -487,6 +494,9 @@ public function getAuth() ); } + /** + * @deprecated See UPGRADING.md for more information + */ public function setAuth($auth) { throw new BadMethodCallException( @@ -753,7 +763,7 @@ public function getScopes() } /** - * @return array + * @return string|null * @visible For Testing */ public function prepareScopes() @@ -886,7 +896,7 @@ public function setAuthConfig($config) /** * Use when the service account has been delegated domain wide access. * - * @param string subject an email address account to impersonate + * @param string $subject an email address account to impersonate */ public function setSubject($subject) { @@ -968,7 +978,7 @@ public function getCache() } /** - * @return Google\Auth\CacheInterface Cache implementation + * @param array $cacheConfig */ public function setCacheConfig(array $cacheConfig) { From ffb8b9ba2d5c212c34e0f00d93b48ea4bceda882 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 26 Jan 2018 12:12:54 -0800 Subject: [PATCH 064/301] Fixes guzzle5 tests (#1378) --- tests/Google/AccessToken/RevokeTest.php | 96 +++++++++++++++++++------ tests/Google/ClientTest.php | 21 ++++-- tests/Google/Service/ResourceTest.php | 51 ++++++++----- tests/Google/Task/RunnerTest.php | 11 ++- 4 files changed, 134 insertions(+), 45 deletions(-) diff --git a/tests/Google/AccessToken/RevokeTest.php b/tests/Google/AccessToken/RevokeTest.php index 7ced6befd..099b9bcd7 100644 --- a/tests/Google/AccessToken/RevokeTest.php +++ b/tests/Google/AccessToken/RevokeTest.php @@ -23,13 +23,15 @@ class Google_AccessToken_RevokeTest extends BaseTest { - public function testRevokeAccess() + public function testRevokeAccessGuzzle5() { + $this->onlyGuzzle5(); + $accessToken = 'ACCESS_TOKEN'; $refreshToken = 'REFRESH_TOKEN'; $token = ''; - $response = $this->getMock('Psr\Http\Message\ResponseInterface'); + $response = $this->getMock('GuzzleHttp\Message\ResponseInterface'); $response->expects($this->exactly(3)) ->method('getStatusCode') ->will($this->returnValue(200)); @@ -45,27 +47,77 @@ function ($request) use (&$token, $response) { } )); - // adds support for extra "createRequest" step (required for Guzzle 5) - if ($this->isGuzzle5()) { - $requestToken = null; - $request = $this->getMock('GuzzleHttp\Message\RequestInterface'); - $request->expects($this->exactly(3)) - ->method('getBody') - ->will($this->returnCallback( - function () use (&$requestToken) { - return 'token='.$requestToken; - })); - $http->expects($this->exactly(3)) - ->method('createRequest') + $requestToken = null; + $request = $this->getMock('GuzzleHttp\Message\RequestInterface'); + $request->expects($this->exactly(3)) + ->method('getBody') ->will($this->returnCallback( - function ($method, $url, $params) use (&$requestToken, $request) { - parse_str((string) $params['body'], $fields); - $requestToken = isset($fields['token']) ? $fields['token'] : null; - - return $request; - } - )); - } + function () use (&$requestToken) { + return 'token='.$requestToken; + })); + $http->expects($this->exactly(3)) + ->method('createRequest') + ->will($this->returnCallback( + function ($method, $url, $params) use (&$requestToken, $request) { + parse_str((string) $params['body'], $fields); + $requestToken = isset($fields['token']) ? $fields['token'] : null; + + return $request; + } + )); + + $t = array( + 'access_token' => $accessToken, + 'created' => time(), + 'expires_in' => '3600' + ); + + // Test with access token. + $revoke = new Google_AccessToken_Revoke($http); + $this->assertTrue($revoke->revokeToken($t)); + $this->assertEquals($accessToken, $token); + + // Test with refresh token. + $revoke = new Google_AccessToken_Revoke($http); + $t = array( + 'access_token' => $accessToken, + 'refresh_token' => $refreshToken, + 'created' => time(), + 'expires_in' => '3600' + ); + $this->assertTrue($revoke->revokeToken($t)); + $this->assertEquals($refreshToken, $token); + + // Test with token string. + $revoke = new Google_AccessToken_Revoke($http); + $t = $accessToken; + $this->assertTrue($revoke->revokeToken($t)); + $this->assertEquals($accessToken, $token); + } + + public function testRevokeAccessGuzzle6() + { + $this->onlyGuzzle6(); + + $accessToken = 'ACCESS_TOKEN'; + $refreshToken = 'REFRESH_TOKEN'; + $token = ''; + + $response = $this->getMock('Psr\Http\Message\ResponseInterface'); + $response->expects($this->exactly(3)) + ->method('getStatusCode') + ->will($this->returnValue(200)); + $http = $this->getMock('GuzzleHttp\ClientInterface'); + $http->expects($this->exactly(3)) + ->method('send') + ->will($this->returnCallback( + function ($request) use (&$token, $response) { + parse_str((string) $request->getBody(), $fields); + $token = isset($fields['token']) ? $fields['token'] : null; + + return $response; + } + )); $t = array( 'access_token' => $accessToken, diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index ef61de326..3d317335c 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -192,6 +192,8 @@ public function testNoAuthIsNull() public function testPrepareService() { + $this->onlyGuzzle6(); + $client = new Google_Client(); $client->setScopes(array("scope1", "scope2")); $scopes = $client->prepareScopes(); @@ -243,7 +245,6 @@ public function testPrepareService() ->will($this->returnValue($guzzle5Request)); } - $client->setHttpClient($http); $dr_service = new Google_Service_Drive($client); $this->assertInstanceOf('Google_Model', $dr_service->files->listFiles()); @@ -442,7 +443,11 @@ public function testRefreshTokenSetsValues() $postBody->expects($this->once()) ->method('__toString') ->will($this->returnValue($token)); - $response = $this->getMock('Psr\Http\Message\ResponseInterface'); + if ($this->isGuzzle5()) { + $response = $this->getMock('GuzzleHttp\Message\ResponseInterface'); + } else { + $response = $this->getMock('Psr\Http\Message\ResponseInterface'); + } $response->expects($this->once()) ->method('getBody') ->will($this->returnValue($postBody)); @@ -479,7 +484,11 @@ public function testRefreshTokenIsSetOnRefresh() $postBody->expects($this->once()) ->method('__toString') ->will($this->returnValue($token)); - $response = $this->getMock('Psr\Http\Message\ResponseInterface'); + if ($this->isGuzzle5()) { + $response = $this->getMock('GuzzleHttp\Message\ResponseInterface'); + } else { + $response = $this->getMock('Psr\Http\Message\ResponseInterface'); + } $response->expects($this->once()) ->method('getBody') ->will($this->returnValue($postBody)); @@ -517,7 +526,11 @@ public function testRefreshTokenIsNotSetWhenNewRefreshTokenIsReturned() $postBody->expects($this->once()) ->method('__toString') ->will($this->returnValue($token)); - $response = $this->getMock('Psr\Http\Message\ResponseInterface'); + if ($this->isGuzzle5()) { + $response = $this->getMock('GuzzleHttp\Message\ResponseInterface'); + } else { + $response = $this->getMock('Psr\Http\Message\ResponseInterface'); + } $response->expects($this->once()) ->method('getBody') ->will($this->returnValue($postBody)); diff --git a/tests/Google/Service/ResourceTest.php b/tests/Google/Service/ResourceTest.php index 5bb7d9172..f8114ca90 100644 --- a/tests/Google/Service/ResourceTest.php +++ b/tests/Google/Service/ResourceTest.php @@ -22,6 +22,8 @@ use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Stream; +use GuzzleHttp\Message\Response as Guzzle5Response; +use GuzzleHttp\Stream\Stream as Guzzle5Stream; class Test_Google_Service extends Google_Service { @@ -188,8 +190,13 @@ public function testNoExpectedClassForAltMediaWithHttpSuccess() // set the "alt" parameter to "media" $arguments = [['alt' => 'media']]; $request = new Request('GET', '/?alt=media'); - $body = Psr7\stream_for('thisisnotvalidjson'); - $response = new Response(200, [], $body); + if ($this->isGuzzle5()) { + $body = Guzzle5Stream::factory('thisisnotvalidjson'); + $response = new Guzzle5Response(200, [], $body); + } else { + $body = Psr7\stream_for('thisisnotvalidjson'); + $response = new Response(200, [], $body); + } $http = $this->getMockBuilder("GuzzleHttp\Client") ->disableOriginalConstructor() @@ -235,8 +242,13 @@ public function testNoExpectedClassForAltMediaWithHttpFail() // set the "alt" parameter to "media" $arguments = [['alt' => 'media']]; $request = new Request('GET', '/?alt=media'); - $body = Psr7\stream_for('thisisnotvalidjson'); - $response = new Response(400, [], $body); + if ($this->isGuzzle5()) { + $body = Guzzle5Stream::factory('thisisnotvalidjson'); + $response = new Guzzle5Response(400, [], $body); + } else { + $body = Psr7\stream_for('thisisnotvalidjson'); + $response = new Response(400, [], $body); + } $http = $this->getMockBuilder("GuzzleHttp\Client") ->disableOriginalConstructor() @@ -286,8 +298,13 @@ public function testErrorResponseWithVeryLongBody() // set the "alt" parameter to "media" $arguments = [['alt' => 'media']]; $request = new Request('GET', '/?alt=media'); - $body = Psr7\stream_for('this will be pulled into memory'); - $response = new Response(400, [], $body); + if ($this->isGuzzle5()) { + $body = Guzzle5Stream::factory('this will be pulled into memory'); + $response = new Guzzle5Response(400, [], $body); + } else { + $body = Psr7\stream_for('this will be pulled into memory'); + $response = new Response(400, [], $body); + } $http = $this->getMockBuilder("GuzzleHttp\Client") ->disableOriginalConstructor() @@ -334,6 +351,8 @@ public function testErrorResponseWithVeryLongBody() public function testSuccessResponseWithVeryLongBody() { + $this->onlyGuzzle6(); + // set the "alt" parameter to "media" $arguments = [['alt' => 'media']]; $request = new Request('GET', '/?alt=media'); @@ -348,12 +367,6 @@ public function testSuccessResponseWithVeryLongBody() ->method('send') ->will($this->returnValue($response)); - if ($this->isGuzzle5()) { - $http->expects($this->once()) - ->method('createRequest') - ->will($this->returnValue(new GuzzleHttp\Message\Request('GET', '/?alt=media'))); - } - $client = new Google_Client(); $client->setHttpClient($http); $service = new Test_Google_Service($client); @@ -386,14 +399,18 @@ public function testExceptionMessage() // set the "alt" parameter to "media" $request = new Request('GET', '/'); $errors = [ ["domain" => "foo"] ]; - - $body = Psr7\stream_for(json_encode([ + $content = json_encode([ 'error' => [ 'errors' => $errors ] - ])); - - $response = new Response(400, [], $body); + ]); + if ($this->isGuzzle5()) { + $body = Guzzle5Stream::factory($content); + $response = new Guzzle5Response(400, [], $body); + } else { + $body = Psr7\stream_for($content); + $response = new Response(400, [], $body); + } $http = $this->getMockBuilder("GuzzleHttp\Client") ->disableOriginalConstructor() diff --git a/tests/Google/Task/RunnerTest.php b/tests/Google/Task/RunnerTest.php index 9f03d1180..08f3dcefa 100644 --- a/tests/Google/Task/RunnerTest.php +++ b/tests/Google/Task/RunnerTest.php @@ -18,6 +18,8 @@ use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; +use GuzzleHttp\Message\Response as Guzzle5Response; +use GuzzleHttp\Stream\Stream as Guzzle5Stream; class Google_Task_RunnerTest extends BaseTest { @@ -655,8 +657,13 @@ public function getNextMockedCall($request) throw $current; } - $stream = Psr7\stream_for($current['body']); - $response = new Response($current['code'], $current['headers'], $stream); + if ($this->isGuzzle5()) { + $stream = Guzzle5Stream::factory($current['body']); + $response = new Guzzle5Response($current['code'], $current['headers'], $stream); + } else { + $stream = Psr7\stream_for($current['body']); + $response = new Response($current['code'], $current['headers'], $stream); + } return $response; } From 464f13124282ad64cef48ae94c16a07b53c46723 Mon Sep 17 00:00:00 2001 From: Ricardo Fontanelli Date: Fri, 25 Aug 2017 16:53:53 -0300 Subject: [PATCH 065/301] Fix directory and reports stop channel urls Report stop channel and Directory stop channel methods uses a different service path then their default class. When the lib build the full service url, the service path is duplicated. --- src/Google/Service/Resource.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Google/Service/Resource.php b/src/Google/Service/Resource.php index fee039b3d..7340af50d 100644 --- a/src/Google/Service/Resource.php +++ b/src/Google/Service/Resource.php @@ -255,6 +255,13 @@ protected function convertToArrayAndStripNulls($o) */ public function createRequestUri($restPath, $params) { + // Override the default servicePath address if the $restPath use a / + if ('/' == substr($restPath, 0, 1)) { + $requestUrl = substr($restPath, 1); + } else { + $requestUrl = $this->servicePath . $restPath; + } + // code for leading slash $requestUrl = $this->servicePath . $restPath; if ($this->rootUrl) { From 3e19a6ec9a9f25967bd3ac46c9afd066dfdd190a Mon Sep 17 00:00:00 2001 From: Ricardo Fontanelli Date: Fri, 25 Aug 2017 17:05:18 -0300 Subject: [PATCH 066/301] Remove white spaces at the end of the lines 258 and 262 --- src/Google/Service/Resource.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Google/Service/Resource.php b/src/Google/Service/Resource.php index 7340af50d..113439fa0 100644 --- a/src/Google/Service/Resource.php +++ b/src/Google/Service/Resource.php @@ -255,13 +255,13 @@ protected function convertToArrayAndStripNulls($o) */ public function createRequestUri($restPath, $params) { - // Override the default servicePath address if the $restPath use a / + // Override the default servicePath address if the $restPath use a / if ('/' == substr($restPath, 0, 1)) { $requestUrl = substr($restPath, 1); } else { - $requestUrl = $this->servicePath . $restPath; + $requestUrl = $this->servicePath . $restPath; } - + // code for leading slash $requestUrl = $this->servicePath . $restPath; if ($this->rootUrl) { From a46bc7f2fef966489011ea3b679ea1131fef0f92 Mon Sep 17 00:00:00 2001 From: Ricardo Fontanelli Date: Sat, 26 Aug 2017 17:48:18 -0300 Subject: [PATCH 067/301] Fix a requestUrl composition according the pattern used by google-api-php-client-services and provide a test to ensure that all changes doesn't break things --- src/Google/Service/Resource.php | 1 - tests/Google/Service/ResourceTest.php | 35 ++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/Google/Service/Resource.php b/src/Google/Service/Resource.php index 113439fa0..1f3d3710b 100644 --- a/src/Google/Service/Resource.php +++ b/src/Google/Service/Resource.php @@ -263,7 +263,6 @@ public function createRequestUri($restPath, $params) } // code for leading slash - $requestUrl = $this->servicePath . $restPath; if ($this->rootUrl) { if ('/' !== substr($this->rootUrl, -1) && '/' !== substr($requestUrl, 0, 1)) { $requestUrl = '/' . $requestUrl; diff --git a/tests/Google/Service/ResourceTest.php b/tests/Google/Service/ResourceTest.php index f8114ca90..564412483 100644 --- a/tests/Google/Service/ResourceTest.php +++ b/tests/Google/Service/ResourceTest.php @@ -140,12 +140,35 @@ public function testCallServiceDefinedRoot() $this->assertEquals("https://sample.example.com/method/path", (string) $request->getUri()); $this->assertEquals("POST", $request->getMethod()); } - + + /* Some Google Service (Google_Service_Directory_Resource_Channels and Google_Service_Reports_Resource_Channels) use a different servicePath value that should override + the default servicePath value, it's represented by a / before the resource path. All other Services have no / before the path*/ + public function testCreateRequestUriForASelfDefinedServicePath() + { + $this->service->servicePath = '/admin/directory/v1/'; + $resource = new Google_Service_Resource( + $this->service, + 'test', + 'testResource', + array("methods" => + array( + 'testMethod' => array( + 'parameters' => array(), + 'path' => '/admin/directory_v1/watch/stop', + 'httpMethod' => 'POST', + ) + ) + ) + ); + $request = $resource->call('testMethod', array(array())); + $this->assertEquals('https://test.example.com/admin/directory_v1/watch/stop', (string) $request->getUri()); + } + public function testCreateRequestUri() { - $restPath = "/plus/{u}"; + $restPath = "plus/{u}"; $service = new Google_Service($this->client); - $service->servicePath = "http://localhost/"; + $service->servicePath = "http://localhost/"; $resource = new Google_Service_Resource($service, 'test', 'testResource', array()); // Test Path @@ -161,7 +184,7 @@ public function testCreateRequestUri() $params['u']['type'] = 'string'; $params['u']['location'] = 'query'; $params['u']['value'] = 'me'; - $value = $resource->createRequestUri('/plus', $params); + $value = $resource->createRequestUri('plus', $params); $this->assertEquals("http://localhost/plus?u=me", $value); // Test Booleans @@ -173,7 +196,7 @@ public function testCreateRequestUri() $this->assertEquals("http://localhost/plus/true", $value); $params['u']['location'] = 'query'; - $value = $resource->createRequestUri('/plus', $params); + $value = $resource->createRequestUri('plus', $params); $this->assertEquals("http://localhost/plus?u=true", $value); // Test encoding @@ -181,7 +204,7 @@ public function testCreateRequestUri() $params['u']['type'] = 'string'; $params['u']['location'] = 'query'; $params['u']['value'] = '@me/'; - $value = $resource->createRequestUri('/plus', $params); + $value = $resource->createRequestUri('plus', $params); $this->assertEquals("http://localhost/plus?u=%40me%2F", $value); } From d669e0f3b92da94d5b4ed960d462dd3b0de920ea Mon Sep 17 00:00:00 2001 From: Ricardo Fontanelli Date: Sun, 4 Feb 2018 00:07:51 -0200 Subject: [PATCH 068/301] Update ResourceTest.php CHANGE: use proper comment blocks --- tests/Google/Service/ResourceTest.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/Google/Service/ResourceTest.php b/tests/Google/Service/ResourceTest.php index 564412483..019cb553d 100644 --- a/tests/Google/Service/ResourceTest.php +++ b/tests/Google/Service/ResourceTest.php @@ -141,8 +141,12 @@ public function testCallServiceDefinedRoot() $this->assertEquals("POST", $request->getMethod()); } - /* Some Google Service (Google_Service_Directory_Resource_Channels and Google_Service_Reports_Resource_Channels) use a different servicePath value that should override - the default servicePath value, it's represented by a / before the resource path. All other Services have no / before the path*/ + /** + * Some Google Service (Google_Service_Directory_Resource_Channels and + * Google_Service_Reports_Resource_Channels) use a different servicePath value + * that should override the default servicePath value, it's represented by a / + * before the resource path. All other Services have no / before the path + */ public function testCreateRequestUriForASelfDefinedServicePath() { $this->service->servicePath = '/admin/directory/v1/'; From ceb9e53f70bf16a39f7334f7910ac8c5180f1760 Mon Sep 17 00:00:00 2001 From: Matt Whisenhunt Date: Fri, 13 Apr 2018 11:18:59 -0700 Subject: [PATCH 069/301] Promote GCP APIs (#1416) --- README.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3cfd7a3ad..a97bcc835 100644 --- a/README.md +++ b/README.md @@ -2,18 +2,17 @@ # Google APIs Client Library for PHP # -## Library maintenance -This client library is supported but in maintenance mode only. We are fixing necessary bugs and adding essential features to ensure this library continues to meet your needs for accessing Google APIs. Non-critical issues will be closed. Any issue may be reopened if it is causing ongoing problems. - -## Description ## The Google API Client Library enables you to work with Google APIs such as Google+, Drive, or YouTube on your server. +These client libraries are officially supported by Google. However, the libraries are considered complete and are in maintenance mode. This means that we will address critical bugs and security issues but will not add any new features. + +## Google Cloud Platform + +For Google Cloud Platform APIs such as Datastore, Cloud Storage or Pub/Sub, we recommend using [GoogleCloudPlatform/google-cloud-php](https://github.com/GoogleCloudPlatform/google-cloud-php) which is under active development. + ## Requirements ## * [PHP 5.4.0 or higher](http://www.php.net/) -## Google Cloud Platform APIs -If you're looking to call the **Google Cloud Platform** APIs, you will want to use the [Google Cloud PHP](https://github.com/googlecloudplatform/google-cloud-php) library instead of this one. - ## Developer Documentation ## http://developers.google.com/api-client-library/php From a68d080658241cd059cd3434c55193630197f47f Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 13 Jun 2018 19:36:54 +0200 Subject: [PATCH 070/301] ensures leeway is at least 1, instead of setting it to 1 (#1455) --- src/Google/AccessToken/Verify.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Google/AccessToken/Verify.php b/src/Google/AccessToken/Verify.php index 09d71b9dd..bc0afcb39 100644 --- a/src/Google/AccessToken/Verify.php +++ b/src/Google/AccessToken/Verify.php @@ -211,8 +211,8 @@ private function getJwtService() $jwtClass = 'Firebase\JWT\JWT'; } - if (property_exists($jwtClass, 'leeway')) { - // adds 1 second to JWT leeway + if (property_exists($jwtClass, 'leeway') && $jwtClass::$leeway < 1) { + // Ensures JWT leeway is at least 1 // @see https://github.com/google/google-api-php-client/issues/827 $jwtClass::$leeway = 1; } From 1d2331093f39c9fd8b85b038b55cf7aa884fbf95 Mon Sep 17 00:00:00 2001 From: Matt Whisenhunt Date: Wed, 13 Jun 2018 13:33:11 -0700 Subject: [PATCH 071/301] Update readme.md, remove Stash, add Cache (#1458) fixes #1101 --- README.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3b21873ee..0926d4fa5 100644 --- a/README.md +++ b/README.md @@ -265,14 +265,21 @@ $response = $httpClient->get('https://www.googleapis.com/plus/v1/people/me'); It is recommended to use another caching library to improve performance. This can be done by passing a [PSR-6](http://www.php-fig.org/psr/psr-6/) compatible library to the client: ```php -$cache = new Stash\Pool(new Stash\Driver\FileSystem); +use League\Flysystem\Adapter\Local; +use League\Flysystem\Filesystem; +use Cache\Adapter\Filesystem\FilesystemCachePool; + +$filesystemAdapter = new Local(__DIR__.'/'); +$filesystem = new Filesystem($filesystemAdapter); + +$cache = new FilesystemCachePool($filesystem); $client->setCache($cache); ``` -In this example we use [StashPHP](http://www.stashphp.com/). Add this to your project with composer: +In this example we use [PHP Cache](http://www.php-cache.com/). Add this to your project with composer: ``` -composer require tedivm/stash +composer require cache/filesystem-adapter ``` ### Updating Tokens ### From 4e0fd83510e579043e10e565528b323b7c2b3c81 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 20 Jun 2018 17:52:20 +0200 Subject: [PATCH 072/301] increment libver to 2.2.2 (#1462) --- src/Google/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index e88dda98a..0f29a10ab 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -38,7 +38,7 @@ */ class Google_Client { - const LIBVER = "2.2.1"; + const LIBVER = "2.2.2"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://accounts.google.com/o/oauth2/revoke'; const OAUTH2_TOKEN_URI = 'https://www.googleapis.com/oauth2/v4/token'; From f88a98dbaac0207e177419a15214ab4fcf30c47a Mon Sep 17 00:00:00 2001 From: Matt Whisenhunt Date: Tue, 26 Jun 2018 17:23:02 -0700 Subject: [PATCH 073/301] examples: Remove extra setAccessKey call. (#1469) * examples: Remove extra setAccessKey call. * examples: fix broken redirect --- examples/idtoken.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/idtoken.php b/examples/idtoken.php index f369bca67..74bf7ae64 100644 --- a/examples/idtoken.php +++ b/examples/idtoken.php @@ -58,13 +58,13 @@ ************************************************/ if (isset($_GET['code'])) { $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); - $client->setAccessToken($token); // store in the session also $_SESSION['id_token_token'] = $token; // redirect back to the example header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); + return; } /************************************************ From 09a0c02bb3ded15e0d3209507bed1030064dc07c Mon Sep 17 00:00:00 2001 From: Thea Flowers Date: Mon, 2 Jul 2018 12:46:49 -0700 Subject: [PATCH 074/301] Add Code of Conduct --- CODE_OF_CONDUCT.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 CODE_OF_CONDUCT.md diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 000000000..46b2a08ea --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,43 @@ +# Contributor Code of Conduct + +As contributors and maintainers of this project, +and in the interest of fostering an open and welcoming community, +we pledge to respect all people who contribute through reporting issues, +posting feature requests, updating documentation, +submitting pull requests or patches, and other activities. + +We are committed to making participation in this project +a harassment-free experience for everyone, +regardless of level of experience, gender, gender identity and expression, +sexual orientation, disability, personal appearance, +body size, race, ethnicity, age, religion, or nationality. + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery +* Personal attacks +* Trolling or insulting/derogatory comments +* Public or private harassment +* Publishing other's private information, +such as physical or electronic +addresses, without explicit permission +* Other unethical or unprofessional conduct. + +Project maintainers have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions +that are not aligned to this Code of Conduct. +By adopting this Code of Conduct, +project maintainers commit themselves to fairly and consistently +applying these principles to every aspect of managing this project. +Project maintainers who do not follow or enforce the Code of Conduct +may be permanently removed from the project team. + +This code of conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. + +Instances of abusive, harassing, or otherwise unacceptable behavior +may be reported by opening an issue +or contacting one or more of the project maintainers. + +This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, +available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/) From 593058802a4bada447e541a454601025c38bb2a9 Mon Sep 17 00:00:00 2001 From: Nanne Huiges Date: Wed, 25 Jul 2018 01:07:38 +0200 Subject: [PATCH 075/301] Updates phpdoc for Google_Client::setScopes (#1465) --- src/Google/Client.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index 0f29a10ab..1f3820c84 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -725,13 +725,13 @@ public function verifyIdToken($idToken = null) /** * Set the scopes to be requested. Must be called before createAuthUrl(). * Will remove any previously configured scopes. - * @param array $scopes, ie: array('https://www.googleapis.com/auth/plus.login', + * @param string|array $scope_or_scopes, ie: array('https://www.googleapis.com/auth/plus.login', * 'https://www.googleapis.com/auth/moderator') */ - public function setScopes($scopes) + public function setScopes($scope_or_scopes) { $this->requestedScopes = array(); - $this->addScope($scopes); + $this->addScope($scope_or_scopes); } /** From 4c2c1ee320ff0992cfa290292a9be4e07b9a6101 Mon Sep 17 00:00:00 2001 From: Konstantin Schmidt <31983699+Assimilationstheorie@users.noreply.github.com> Date: Wed, 25 Jul 2018 01:09:32 +0200 Subject: [PATCH 076/301] Small changes to examples/index.php (#1460) --- examples/index.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/index.php b/examples/index.php index dca566a87..10fc2f1f6 100644 --- a/examples/index.php +++ b/examples/index.php @@ -21,9 +21,9 @@
    You have not entered your API key -
    - API Key: - + " method="POST"> + API Key: +
    This can be found in the Google API Console
    From 123ef5ac2d187d01540fa9c8c04d37313e016bea Mon Sep 17 00:00:00 2001 From: Konstantin Schmidt <31983699+Assimilationstheorie@users.noreply.github.com> Date: Wed, 25 Jul 2018 01:10:20 +0200 Subject: [PATCH 077/301] misc updates to examples/url-shortener.php (#1456) --- examples/url-shortener.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/url-shortener.php b/examples/url-shortener.php index 1c3deb322..4cef77aae 100644 --- a/examples/url-shortener.php +++ b/examples/url-shortener.php @@ -122,7 +122,7 @@
    - +
    From 9edd15c7a046ebb8bf047d2c0fa61f14c6886653 Mon Sep 17 00:00:00 2001 From: Yahav Date: Wed, 8 Aug 2018 00:44:49 +0300 Subject: [PATCH 078/301] Fixing the ability to provide custom retryMap (#1488) Fixes #1487 --- src/Google/Client.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index 1f3820c84..224247140 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -130,6 +130,7 @@ public function __construct(array $config = array()) // Task Runner retry configuration // @see Google_Task_Runner 'retry' => array(), + 'retry_map' => null, // cache config for downstream auth caching 'cache_config' => [], @@ -795,7 +796,13 @@ public function execute(RequestInterface $request, $expectedClass = null) // this is where most of the grunt work is done $http = $this->authorize(); - return Google_Http_REST::execute($http, $request, $expectedClass, $this->config['retry']); + return Google_Http_REST::execute( + $http, + $request, + $expectedClass, + $this->config['retry'], + $this->config['retry_map'] + ); } /** From 787d68d528d382bccc6aea504d7296024380d7d1 Mon Sep 17 00:00:00 2001 From: Maxim Rubis Date: Fri, 10 Aug 2018 14:05:44 -0400 Subject: [PATCH 079/301] Update docs to install latest 2.x version (#1491) Escape caret symbol properly --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0926d4fa5..8a8d16d82 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ composer installed. Once composer is installed, execute the following command in your project root to install this library: ```sh -composer require google/apiclient:^2.0 +composer require google/apiclient:"^2.0" ``` Finally, be sure to include the autoloader: From 6fe60b1b5780925a23c71e2c86be09b50da46540 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Mon, 13 Aug 2018 11:26:37 -0700 Subject: [PATCH 080/301] Use new auth urls (#1492) --- src/Google/Client.php | 4 ++-- tests/BaseTest.php | 2 +- tests/Google/ClientTest.php | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index 224247140..f280ef045 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -40,8 +40,8 @@ class Google_Client { const LIBVER = "2.2.2"; const USER_AGENT_SUFFIX = "google-api-php-client/"; - const OAUTH2_REVOKE_URI = 'https://accounts.google.com/o/oauth2/revoke'; - const OAUTH2_TOKEN_URI = 'https://www.googleapis.com/oauth2/v4/token'; + const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; + const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; const OAUTH2_AUTH_URL = 'https://accounts.google.com/o/oauth2/auth'; const API_BASE_PATH = 'https://www.googleapis.com/'; diff --git a/tests/BaseTest.php b/tests/BaseTest.php index b1c3fc906..92328283e 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -123,7 +123,7 @@ public function tryToGetAnAccessToken(Google_Client $client) $client->setRedirectUri("urn:ietf:wg:oauth:2.0:oob"); $client->setConfig('access_type', 'offline'); $authUrl = $client->createAuthUrl(); - + echo "\nGo to: $authUrl\n"; echo "\nPlease enter the auth code:\n"; ob_flush(); `open '$authUrl'`; diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 3d317335c..df9253075 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -342,7 +342,7 @@ public function testJsonConfig() $client = new Google_Client(); $device = '{"installed":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret"'. - ':"N0aHCBT1qX1VAcF5J1pJAn6S","token_uri":"https://accounts.google.com/o/oauth2/token",'. + ':"N0aHCBT1qX1VAcF5J1pJAn6S","token_uri":"https://oauth2.googleapis.com/token",'. '"client_email":"","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","oob"],"client_x509_cert_url"'. ':"","client_id":"123456789.apps.googleusercontent.com","auth_provider_x509_cert_url":'. '"https://www.googleapis.com/oauth2/v1/certs"}}'; @@ -355,7 +355,7 @@ public function testJsonConfig() // Web config $client = new Google_Client(); $web = '{"web":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret"' . - ':"lpoubuib8bj-Fmke_YhhyHGgXc","token_uri":"https://accounts.google.com/o/oauth2/token"' . + ':"lpoubuib8bj-Fmke_YhhyHGgXc","token_uri":"https://oauth2.googleapis.com/token"' . ',"client_email":"123456789@developer.gserviceaccount.com","client_x509_cert_url":'. '"https://www.googleapis.com/robot/v1/metadata/x509/123456789@developer.gserviceaccount.com"'. ',"client_id":"123456789.apps.googleusercontent.com","auth_provider_x509_cert_url":'. From 89b3b8c28cdaa709950995f375539429c064d2e5 Mon Sep 17 00:00:00 2001 From: Matt Whisenhunt Date: Tue, 16 Oct 2018 17:07:47 -0700 Subject: [PATCH 081/301] Update default and doc values for setApprovalPrompt. (#1535) * Update default and doc values for setApprovalPrompt. --- src/Google/Client.php | 7 ++++--- tests/Google/ClientTest.php | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index f280ef045..7f75841e5 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -125,7 +125,7 @@ public function __construct(array $config = array()) 'login_hint' => '', 'request_visible_actions' => '', 'access_type' => 'online', - 'approval_prompt' => 'auto', + 'approval_prompt' => 'consent', // Task Runner retry configuration // @see Google_Task_Runner @@ -569,8 +569,9 @@ public function setAccessType($accessType) /** * @param string $approvalPrompt Possible values for approval_prompt include: - * {@code "force"} to force the approval UI to appear. - * {@code "auto"} to request auto-approval when possible. (This is the default value) + * {@code "none"} Do not display any authentication or consent screens. Must not be specified with other values. + * {@code "consent"} Prompt the user for consent. + * {@code "select_account"} Prompt the user to select an account. */ public function setApprovalPrompt($approvalPrompt) { diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index df9253075..38d964bfa 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -224,7 +224,7 @@ public function testPrepareService() . '&redirect_uri=http%3A%2F%2Flocalhost%2F' . '&state=xyz' . '&scope=http%3A%2F%2Ftest.com%20scope2' - . '&approval_prompt=auto', + . '&approval_prompt=consent', $client->createAuthUrl() ); From 8865db9b279f06421b73f0fd0f513280251e433e Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Wed, 7 Nov 2018 14:57:57 -0800 Subject: [PATCH 082/301] Update GitHub issue templates --- CONTRIBUTING.md => .github/CONTRIBUTING.md | 0 .github/ISSUE_TEMPLATE.md | 8 ----- .github/ISSUE_TEMPLATE/bug_report.md | 36 ++++++++++++++++++++++ .github/ISSUE_TEMPLATE/feature_request.md | 21 +++++++++++++ .github/ISSUE_TEMPLATE/support_request.md | 7 +++++ 5 files changed, 64 insertions(+), 8 deletions(-) rename CONTRIBUTING.md => .github/CONTRIBUTING.md (100%) delete mode 100644 .github/ISSUE_TEMPLATE.md create mode 100644 .github/ISSUE_TEMPLATE/bug_report.md create mode 100644 .github/ISSUE_TEMPLATE/feature_request.md create mode 100644 .github/ISSUE_TEMPLATE/support_request.md diff --git a/CONTRIBUTING.md b/.github/CONTRIBUTING.md similarity index 100% rename from CONTRIBUTING.md rename to .github/CONTRIBUTING.md diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md deleted file mode 100644 index 47cb46145..000000000 --- a/.github/ISSUE_TEMPLATE.md +++ /dev/null @@ -1,8 +0,0 @@ -**Heads up!** - -We appreciate any bug reports or other contributions, but please note that this issue -tracker is only for this client library. We do not maintain the APIs that this client -library talks to. If you have an issue or questions with how to use a particular API, -you may be better off posting on Stackoverflow under the `google-api` tag. - -Thank you! diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 000000000..e70487ba1 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,36 @@ +--- +name: Bug report +about: Create a report to help us improve + +--- + +Thanks for stopping by to let us know something could be better! + +**PLEASE READ**: If you have a support contract with Google, please create an issue in the [support console](https://cloud.google.com/support/) instead of filing on GitHub. This will ensure a timely response. + +Please run down the following list and make sure you've tried the usual "quick fixes": + + - Search the issues already opened: https://github.com/googleapis/google-api-php-client/issues + - Search StackOverflow: http://stackoverflow.com/questions/tagged/google-cloud-platform+php + +If you are still having issues, please be sure to include as much information as possible: + +#### Environment details + + - OS: + - PHP version: + - Package name and version: + +#### Steps to reproduce + + 1. ... + +#### Code example + +```php +# example +``` + +Making sure to follow these steps will guarantee the quickest resolution possible. + +Thanks! diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 000000000..20d075c25 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,21 @@ +--- +name: Feature request +about: Suggest an idea for this library + +--- + +Thanks for stopping by to let us know something could be better! + +**PLEASE READ**: If you have a support contract with Google, please create an issue in the [support console](https://cloud.google.com/support/) instead of filing on GitHub. This will ensure a timely response. + + **Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + + **Describe the solution you'd like** +A clear and concise description of what you want to happen. + + **Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + + **Additional context** +Add any other context or screenshots about the feature request here. diff --git a/.github/ISSUE_TEMPLATE/support_request.md b/.github/ISSUE_TEMPLATE/support_request.md new file mode 100644 index 000000000..995869032 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/support_request.md @@ -0,0 +1,7 @@ +--- +name: Support request +about: If you have a support contract with Google, please create an issue in the Google Cloud Support console. + +--- + +**PLEASE READ**: If you have a support contract with Google, please create an issue in the [support console](https://cloud.google.com/support/) instead of filing on GitHub. This will ensure a timely response. From 736ffceb170db1ec16e4fba83d0065996b23e855 Mon Sep 17 00:00:00 2001 From: Matt Whisenhunt Date: Thu, 8 Nov 2018 10:57:15 -0800 Subject: [PATCH 083/301] Update README.md (#1547) --- .gitattributes | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitattributes b/.gitattributes index d4e9c2512..3ed46ef83 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,7 +1,7 @@ +/.gitattributes export-ignore /.github export-ignore /.gitignore export-ignore /.travis.yml export-ignore -/CONTRIBUTING.md export-ignore /examples export-ignore /phpunit.xml.dist export-ignore /style export-ignore diff --git a/README.md b/README.md index 8a8d16d82..57ab2f27a 100644 --- a/README.md +++ b/README.md @@ -319,7 +319,7 @@ YouTube: https://github.com/youtube/api-samples/tree/master/php ## How Do I Contribute? ## -Please see the [contributing](CONTRIBUTING.md) page for more information. In particular, we love pull requests - but please make sure to sign the [contributor license agreement](https://developers.google.com/api-client-library/php/contribute). +Please see the [contributing](.github/CONTRIBUTING.md) page for more information. In particular, we love pull requests - but please make sure to sign the [contributor license agreement](https://developers.google.com/api-client-library/php/contribute). ## Frequently Asked Questions ## From baf254f1aec93ad4fe897035e8fe35e01b93daa0 Mon Sep 17 00:00:00 2001 From: Dan Bruce Date: Fri, 7 Dec 2018 16:26:21 -0600 Subject: [PATCH 084/301] Fix build status image (#1549) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 57ab2f27a..b6feab76e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Build Status](https://travis-ci.org/google/google-api-php-client.svg?branch=master)](https://travis-ci.org/google/google-api-php-client) +[![Build Status](https://travis-ci.org/googleapis/google-api-php-client.svg?branch=master)](https://travis-ci.org/googleapis/google-api-php-client) # Google APIs Client Library for PHP # From 8213cb813c196e6287055fcf008de3539a25d1ab Mon Sep 17 00:00:00 2001 From: David Supplee Date: Fri, 14 Dec 2018 10:08:34 -0800 Subject: [PATCH 085/301] fix tests (#1560) --- tests/Google/ClientTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 38d964bfa..c6a1fe242 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -445,6 +445,9 @@ public function testRefreshTokenSetsValues() ->will($this->returnValue($token)); if ($this->isGuzzle5()) { $response = $this->getMock('GuzzleHttp\Message\ResponseInterface'); + $response->expects($this->once()) + ->method('getStatusCode') + ->will($this->returnValue(200)); } else { $response = $this->getMock('Psr\Http\Message\ResponseInterface'); } @@ -486,6 +489,9 @@ public function testRefreshTokenIsSetOnRefresh() ->will($this->returnValue($token)); if ($this->isGuzzle5()) { $response = $this->getMock('GuzzleHttp\Message\ResponseInterface'); + $response->expects($this->once()) + ->method('getStatusCode') + ->will($this->returnValue(200)); } else { $response = $this->getMock('Psr\Http\Message\ResponseInterface'); } @@ -528,6 +534,9 @@ public function testRefreshTokenIsNotSetWhenNewRefreshTokenIsReturned() ->will($this->returnValue($token)); if ($this->isGuzzle5()) { $response = $this->getMock('GuzzleHttp\Message\ResponseInterface'); + $response->expects($this->once()) + ->method('getStatusCode') + ->will($this->returnValue(200)); } else { $response = $this->getMock('Psr\Http\Message\ResponseInterface'); } From dee3a8b840d87635b0e26da63202b163512f2d4e Mon Sep 17 00:00:00 2001 From: Matt Whisenhunt Date: Fri, 14 Dec 2018 10:19:46 -0800 Subject: [PATCH 086/301] Revert #1535. (#1556) --- src/Google/Client.php | 10 ++++++---- tests/Google/ClientTest.php | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index 7f75841e5..17a3c9d07 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -125,7 +125,7 @@ public function __construct(array $config = array()) 'login_hint' => '', 'request_visible_actions' => '', 'access_type' => 'online', - 'approval_prompt' => 'consent', + 'approval_prompt' => 'auto', // Task Runner retry configuration // @see Google_Task_Runner @@ -569,9 +569,8 @@ public function setAccessType($accessType) /** * @param string $approvalPrompt Possible values for approval_prompt include: - * {@code "none"} Do not display any authentication or consent screens. Must not be specified with other values. - * {@code "consent"} Prompt the user for consent. - * {@code "select_account"} Prompt the user to select an account. + * {@code "force"} to force the approval UI to appear. + * {@code "auto"} to request auto-approval when possible. (This is the default value) */ public function setApprovalPrompt($approvalPrompt) { @@ -638,6 +637,9 @@ public function setHostedDomain($hd) * If no value is specified and the user has not previously authorized * access, then the user is shown a consent screen. * @param $prompt string + * {@code "none"} Do not display any authentication or consent screens. Must not be specified with other values. + * {@code "consent"} Prompt the user for consent. + * {@code "select_account"} Prompt the user to select an account. */ public function setPrompt($prompt) { diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index c6a1fe242..70360d224 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -224,7 +224,7 @@ public function testPrepareService() . '&redirect_uri=http%3A%2F%2Flocalhost%2F' . '&state=xyz' . '&scope=http%3A%2F%2Ftest.com%20scope2' - . '&approval_prompt=consent', + . '&approval_prompt=auto', $client->createAuthUrl() ); From 463d053d71ac826449cb42aeb942ea5f3a94367e Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Fri, 14 Dec 2018 11:29:41 -0700 Subject: [PATCH 087/301] Use double pipes in composer dependency file (#1505) use of single pipes is deprecated, see https://github.com/composer/composer/issues/6755#issue-266478234 --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 9f00dab71..5cb3ca2e7 100644 --- a/composer.json +++ b/composer.json @@ -9,10 +9,10 @@ "php": ">=5.4", "google/auth": "^1.0", "google/apiclient-services": "~0.13", - "firebase/php-jwt": "~2.0|~3.0|~4.0|~5.0", + "firebase/php-jwt": "~2.0||~3.0||~4.0||~5.0", "monolog/monolog": "^1.17", - "phpseclib/phpseclib": "~0.3.10|~2.0", - "guzzlehttp/guzzle": "~5.3.1|~6.0", + "phpseclib/phpseclib": "~0.3.10||~2.0", + "guzzlehttp/guzzle": "~5.3.1||~6.0", "guzzlehttp/psr7": "^1.2" }, "require-dev": { From 7e1f6fde5b5b62f6880e12fc5ad08f0aaec3a913 Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Mon, 25 Feb 2019 12:29:17 -0500 Subject: [PATCH 088/301] Add UnexpectedValueException to documentation (#1592) --- src/Google/Client.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index 17a3c9d07..00e731bc5 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -697,7 +697,8 @@ public function revokeToken($token = null) * Verify an id_token. This method will verify the current id_token, if one * isn't provided. * - * @throws LogicException + * @throws LogicException If no token was provided and no token was set using `setAccessToken`. + * @throws UnexpectedValueException If the token is not a valid JWT. * @param string|null $idToken The token (id_token) that should be verified. * @return array|false Returns the token payload as an array if the verification was * successful, false otherwise. From 967ca9d565596f32cf12126e1394cfbc83d2ffed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Tue, 9 Apr 2019 16:17:43 +0200 Subject: [PATCH 089/301] Update `Google_Client::revokeToken()` docblock. (#1609) as Google_AccessToken_Revoke::revokeToken() accepts it --- src/Google/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index 00e731bc5..d0a97bded 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -681,7 +681,7 @@ public function setTokenCallback(callable $tokenCallback) * Revoke an OAuth2 access token or refresh token. This method will revoke the current access * token, if a token isn't provided. * - * @param string|null $token The token (access token or a refresh token) that should be revoked. + * @param string|array|null $token The token (access token or a refresh token) that should be revoked. * @return boolean Returns True if the revocation was successful, otherwise False. */ public function revokeToken($token = null) From 48ddf6c7c485d95ffdccaf61663d63c8ea843f69 Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Tue, 16 Apr 2019 15:47:05 -0400 Subject: [PATCH 090/301] Add batch reset warning (#1613) --- src/Google/Http/Batch.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Google/Http/Batch.php b/src/Google/Http/Batch.php index 4c4868484..a808f696f 100644 --- a/src/Google/Http/Batch.php +++ b/src/Google/Http/Batch.php @@ -23,6 +23,10 @@ /** * Class to handle batched requests to the Google API service. + * + * Note that calls to `Google_Http_Batch::execute()` do not clear the queued + * requests. To start a new batch, be sure to create a new instance of this + * class. */ class Google_Http_Batch { From 6e72a864aad23cc020a097d6b0b650874e972c6b Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Fri, 26 Apr 2019 16:21:31 +0200 Subject: [PATCH 091/301] Fix usage of undefined constants (#1621) --- src/Google/Http/MediaFileUpload.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Http/MediaFileUpload.php b/src/Google/Http/MediaFileUpload.php index 1ce9bb1d3..600e6789e 100644 --- a/src/Google/Http/MediaFileUpload.php +++ b/src/Google/Http/MediaFileUpload.php @@ -317,7 +317,7 @@ private function fetchResumeUri() if (isset($body['error']['errors'])) { $message .= ': '; foreach ($body['error']['errors'] as $error) { - $message .= "{$error[domain]}, {$error[message]};"; + $message .= "{$error['domain']}, {$error['message']};"; } $message = rtrim($message, ';'); } From 1b76fa672c31e4cab58427f413315356682ba68a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire?= Date: Fri, 3 May 2019 15:41:41 +0200 Subject: [PATCH 092/301] Specify configuration filepath when config file is not found (#1626) --- src/Google/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index d0a97bded..832d7757a 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -867,7 +867,7 @@ public function setAuthConfig($config) { if (is_string($config)) { if (!file_exists($config)) { - throw new InvalidArgumentException('file does not exist'); + throw new InvalidArgumentException(sprintf('file "%s" does not exist', $config)); } $json = file_get_contents($config); From 537f0fbcbc7185bfaeeb5060994e662fb2cf04e7 Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Thu, 16 May 2019 17:54:44 -0400 Subject: [PATCH 093/301] Add guzzle config to readme (#1625) --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index b6feab76e..75d57ad83 100644 --- a/README.md +++ b/README.md @@ -313,6 +313,27 @@ Now all calls made by this library will appear in the Charles UI. One additional step is required in Charles to view SSL requests. Go to **Charles > Proxy > SSL Proxying Settings** and add the domain you'd like captured. In the case of the Google APIs, this is usually `*.googleapis.com`. +### Controlling HTTP Client Configuration Directly + +Google API Client uses [Guzzle](http://docs.guzzlephp.org) as its default HTTP client. That means that you can control your HTTP requests in the same manner you would for any application using Guzzle. + +Let's say, for instance, we wished to apply a referrer to each request. + +```php +use GuzzleHttp\Client; + +$httpClient = new Client([ + 'headers' => [ + 'referer' => 'mysite.com' + ] +]); + +$client = new Google_Client(); +$client->setHttpClient($httpClient); +``` + +Other Guzzle features such as [Handlers and Middleware](http://docs.guzzlephp.org/en/stable/handlers-and-middleware.html) offer even more control. + ### Service Specific Examples ### YouTube: https://github.com/youtube/api-samples/tree/master/php From 4bfd3edc21cceaf45fc216a4196e1993cf7a89eb Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 21 May 2019 11:06:55 -0700 Subject: [PATCH 094/301] Prepare for v2.2.3 (#1633) --- src/Google/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index 832d7757a..e1acef167 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -38,7 +38,7 @@ */ class Google_Client { - const LIBVER = "2.2.2"; + const LIBVER = "2.2.3"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; From 9b293e91c6ff7790f7bb8a96a13738202b97ff57 Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Fri, 7 Jun 2019 12:05:04 -0700 Subject: [PATCH 095/301] Delete url-shortener.php (#1646) --- examples/README.md | 6 ++ examples/index.php | 1 - examples/url-shortener.php | 144 ---------------------------- tests/examples/indexTest.php | 4 +- tests/examples/urlShortenerTest.php | 34 ------- 5 files changed, 8 insertions(+), 181 deletions(-) delete mode 100644 examples/url-shortener.php delete mode 100644 tests/examples/urlShortenerTest.php diff --git a/examples/README.md b/examples/README.md index 447a93a22..e62da9cf2 100644 --- a/examples/README.md +++ b/examples/README.md @@ -11,3 +11,9 @@ 1. Point your browser to the host and port you specified, i.e `http://localhost:8000`. +## G Suite OAuth Samples + +G Suite APIs such as Slides, Sheets, and Drive use 3-legged OAuth authentification. +Samples using OAuth can be found here: + +https://github.com/gsuitedevs/php-samples diff --git a/examples/index.php b/examples/index.php index 10fc2f1f6..89192bb2a 100644 --- a/examples/index.php +++ b/examples/index.php @@ -31,7 +31,6 @@
    • A query using simple API access
    • -
    • Authorize a url shortener, using OAuth 2.0 authentication.
    • An example of combining multiple calls into a batch request
    • A query using the service account functionality.
    • An example of a small file upload.
    • diff --git a/examples/url-shortener.php b/examples/url-shortener.php deleted file mode 100644 index 4cef77aae..000000000 --- a/examples/url-shortener.php +++ /dev/null @@ -1,144 +0,0 @@ -setAuthConfig($oauth_credentials); -$client->setRedirectUri($redirect_uri); -$client->addScope("https://www.googleapis.com/auth/urlshortener"); - -/************************************************ - * When we create the service here, we pass the - * client to it. The client then queries the service - * for the required scopes, and uses that when - * generating the authentication URL later. - ************************************************/ -$service = new Google_Service_Urlshortener($client); - -/************************************************ - * If we're logging out we just need to clear our - * local access token in this case - ************************************************/ -if (isset($_REQUEST['logout'])) { - unset($_SESSION['access_token']); - unset($_SESSION['csrf_token']); -} - -/************************************************ - * If we have a code back from the OAuth 2.0 flow, - * we need to exchange that with the - * Google_Client::fetchAccessTokenWithAuthCode() - * function. We store the resultant access token - * bundle in the session, and redirect to ourself. - ************************************************/ -if (isset($_GET['code'])) { - $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); - $client->setAccessToken($token); - - // store in the session also - $_SESSION['access_token'] = $token; - - // redirect back to the example - header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); -} - -/************************************************ - If we have an access token, we can make - requests, else we generate an authentication URL. - ************************************************/ -if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { - $client->setAccessToken($_SESSION['access_token']); -} else { - $authUrl = $client->createAuthUrl(); -} - -/************************************************ - If we're signed in and have a request to shorten - a URL, then we create a new URL object, set the - unshortened URL, and call the 'insert' method on - the 'url' resource. Note that we re-store the - access_token bundle, just in case anything - changed during the request - the main thing that - might happen here is the access token itself is - refreshed if the application has offline access. - ************************************************/ -if ($client->getAccessToken() && isset($_REQUEST['url'])) { - if (!validateCsrfToken()) { - echo invalidCsrfTokenWarning(); - return; - } - $url = new Google_Service_Urlshortener_Url(); - $url->longUrl = $_REQUEST['url']; - $short = $service->url->insert($url); - $_SESSION['access_token'] = $client->getAccessToken(); -} - -?> - -
      - - - -
      - - - -
      -
      - - - -
      - - You created a short link!
      - -
      -
      -
      - Create another - -
      - -loadExample('index.php'); $nodes = $crawler->filter('li'); - $this->assertCount(9, $nodes); + $this->assertCount(8, $nodes); $this->assertEquals('A query using simple API access', $nodes->first()->text()); } -} \ No newline at end of file +} diff --git a/tests/examples/urlShortenerTest.php b/tests/examples/urlShortenerTest.php deleted file mode 100644 index fc4b9aa7f..000000000 --- a/tests/examples/urlShortenerTest.php +++ /dev/null @@ -1,34 +0,0 @@ -checkKey(); - - $crawler = $this->loadExample('url-shortener.php'); - - $nodes = $crawler->filter('h1'); - $this->assertCount(1, $nodes); - $this->assertEquals('User Query - URL Shortener', $nodes->first()->text()); - } -} \ No newline at end of file From 2d11de34a0dab8dcb872908f11e006c799b710f5 Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Fri, 14 Jun 2019 09:59:47 -0700 Subject: [PATCH 096/301] Add option to toggle API format version (#1644) * Add option to toggle API format version * Fix tests * Use phpunit mock * Force trusty in builds * Fix style --- .travis.yml | 2 +- src/Google/Client.php | 23 +++++++++++++++++++++++ tests/Google/ClientTest.php | 27 +++++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index c3b1c8e1a..6ac93c160 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,5 @@ language: php +dist: trusty services: - memcached @@ -44,4 +45,3 @@ before_script: script: - vendor/bin/phpunit - if [[ "$RUN_PHP_CS" == "true" ]]; then vendor/bin/phpcs src --standard=style/ruleset.xml -np; fi - diff --git a/src/Google/Client.php b/src/Google/Client.php index e1acef167..92c3b2328 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -142,6 +142,10 @@ public function __construct(array $config = array()) // Service class used in Google_Client::verifyIdToken. // Explicitly pass this in to avoid setting JWT::$leeway 'jwt' => null, + + // Setting api_format_v2 will return more detailed error messages + // from certain APIs. + 'api_format_v2' => false ], $config ); @@ -796,6 +800,13 @@ public function execute(RequestInterface $request, $expectedClass = null) . $this->getLibraryVersion() ); + if ($this->config['api_format_v2']) { + $request = $request->withHeader( + 'X-GOOG-API-FORMAT-VERSION', + 2 + ); + } + // call the authorize method // this is where most of the grunt work is done $http = $this->authorize(); @@ -1056,6 +1067,18 @@ public function getHttpClient() return $this->http; } + /** + * Set the API format version. + * + * `true` will use V2, which may return more useful error messages. + * + * @param bool $value + */ + public function setApiFormatV2($value) + { + $this->config['api_format_v2'] = (bool) $value; + } + protected function createDefaultHttpClient() { $options = ['exceptions' => false]; diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 70360d224..4152871a1 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -19,8 +19,10 @@ */ use GuzzleHttp\Client; -use GuzzleHttp\Event\RequestEvents; -use Psr\Http\Message\Request; +use GuzzleHttp\Psr7\Request; +use GuzzleHttp\Psr7\Response; +use Prophecy\Argument; +use Psr\Http\Message\RequestInterface; class Google_ClientTest extends BaseTest { @@ -684,4 +686,25 @@ public function testTokenCallback() $this->assertTrue($called); } + + public function testExecuteWithFormat() + { + $this->onlyGuzzle6(); + + $client = new Google_Client([ + 'api_format_v2' => true + ]); + + $guzzle = $this->getMock('GuzzleHttp\Client'); + $guzzle->expects($this->once()) + ->method('send') + ->with($this->callback(function (RequestInterface $request) { + return $request->getHeaderLine('X-GOOG-API-FORMAT-VERSION') === '2'; + }))->will($this->returnValue(new Response(200, [], null))); + + $client->setHttpClient($guzzle); + + $request = new Request('POST', 'http://foo.bar/'); + $client->execute($request); + } } From f449f53201e4172faa2e0560feb7e3055d8664ed Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Wed, 19 Jun 2019 11:21:28 -0700 Subject: [PATCH 097/301] =?UTF-8?q?docs:=20Adds=20developer=20docs=20to=20?= =?UTF-8?q?repo:=20Fixes=20#1648=20Fixes=20#1649=20Fixes=20#165=E2=80=A6?= =?UTF-8?q?=20(#1668)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * docs: Adds developer docs to repo: Fixes #1648 Fixes #1649 Fixes #1650 Fixes #1651 Fixes #1652 Fixes #1653 * docs: Add guide for OAuth web * docs: Fix oauth-server doc * docs: Fix docs links * docs: Address @eesheesh's comments * docs: Fix bad link --- README.md | 3 +- docs/README.md | 15 ++ docs/api-keys.md | 13 ++ docs/auth.md | 11 ++ docs/id-token.md | 22 +++ docs/install.md | 39 ++++ docs/media.md | 75 ++++++++ docs/oauth-server.md | 144 +++++++++++++++ docs/oauth-web.md | 424 +++++++++++++++++++++++++++++++++++++++++++ docs/pagination.md | 10 + docs/parameters.md | 14 ++ docs/start.md | 91 ++++++++++ 12 files changed, 860 insertions(+), 1 deletion(-) create mode 100644 docs/README.md create mode 100644 docs/api-keys.md create mode 100644 docs/auth.md create mode 100644 docs/id-token.md create mode 100644 docs/install.md create mode 100644 docs/media.md create mode 100644 docs/oauth-server.md create mode 100644 docs/oauth-web.md create mode 100644 docs/pagination.md create mode 100644 docs/parameters.md create mode 100644 docs/start.md diff --git a/README.md b/README.md index 75d57ad83..4911151b0 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,8 @@ For Google Cloud Platform APIs such as Datastore, Cloud Storage or Pub/Sub, we r * [PHP 5.4.0 or higher](http://www.php.net/) ## Developer Documentation ## -http://developers.google.com/api-client-library/php + +The [docs folder](docs/) provides detailed guides for using this library. ## Installation ## diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 000000000..b7ad418c4 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,15 @@ +# Google API Client LIbrary for PHP Docs + +The Google API Client Library for PHP offers simple, flexible access to many Google APIs. + +## Documentation + +- [Getting Started](start.md) +- [API Keys](api-keys.md) +- [Auth](auth.md) +- [Installation](install.md) +- [Media](media.md) +- [OAuth Server](oauth-server.md) +- [OAuth Web](oauth-web.md) +- [Pagination](pagination.md) +- [Parameters](parameters.md) \ No newline at end of file diff --git a/docs/api-keys.md b/docs/api-keys.md new file mode 100644 index 000000000..c4a0da1f2 --- /dev/null +++ b/docs/api-keys.md @@ -0,0 +1,13 @@ +# API Keys + +When calling APIs that do not access private user data, you can use simple API keys. These keys are used to authenticate your application for accounting purposes. The Google Developers Console documentation also describes [API keys](https://developers.google.com/console/help/using-keys). + +> Note: If you do need to access private user data, you must use OAuth 2.0. See [Using OAuth 2.0 for Web Server Applications](docs/oauth-server.md) and [Using OAuth 2.0 for Server to Server Applications](docs/oauth-web.md) for more information. + +## Using API Keys + +To use API keys, call the `setDeveloperKey()` method of the `Google_Client` object before making any API calls. For example: + +```php +$client->setDeveloperKey($api_key); +``` \ No newline at end of file diff --git a/docs/auth.md b/docs/auth.md new file mode 100644 index 000000000..65dc24630 --- /dev/null +++ b/docs/auth.md @@ -0,0 +1,11 @@ +# Authorization + +The Google PHP Client Library supports several methods for making authenticated calls to the Google APIs. + +- [API Keys](api-keys.md) +- [OAuth 2.0 For Webservers](oauth-web.md) +- [OAuth 2.0 Service Accounts](oauth-server.md) + +In addition, it supports a method of identifying users without granting access to make Google API calls. + +- [ID Token Verification](id-token.md) \ No newline at end of file diff --git a/docs/id-token.md b/docs/id-token.md new file mode 100644 index 000000000..56456fa88 --- /dev/null +++ b/docs/id-token.md @@ -0,0 +1,22 @@ +# ID Token Authentication + +ID tokens allow authenticating a user securely without requiring a network call (in many cases), and without granting the server access to request user information from the Google APIs. + +> For a complete example, see the [idtoken.php](https://github.com/googleapis/google-api-php-client/blob/master/examples/idtoken.php) sample in the examples/ directory of the client library. + +This is accomplished because each ID token is a cryptographically signed, base64 encoded JSON structure. The token payload includes the Google user ID, the client ID of the application the user signed in to, and the issuer (in this case, Google). It also contains a cryptographic signature which can be verified with the public Google certificates to ensure that the token was created by Google. If the user has granted permission to view their email address to the application, the ID token will additionally include their email address. + +The token can be easily and securely verified with the PHP client library + +```php +function getUserFromToken($token) { + $ticket = $client->verifyIdToken($token); + if ($ticket) { + $data = $ticket->getAttributes(); + return $data['payload']['sub']; // user ID + } + return false +} +``` + +The library will automatically download and cache the certificate required for verification, and refresh it if it has expired. diff --git a/docs/install.md b/docs/install.md new file mode 100644 index 000000000..84b9208d5 --- /dev/null +++ b/docs/install.md @@ -0,0 +1,39 @@ +# Installation + +This page contains information about installing the Google APIs Client Library for PHP. + +## Requirements + +* PHP version 5.4 or greater. + +## Obtaining the client library + +There are two options for obtaining the files for the client library. + +### Using Composer + +You can install the library by adding it as a dependency to your composer.json. + +```json +"require": { + "google/apiclient": "^2.0" +} +``` + +### Downloading from GitHub + +Follow [the instructions in the README](https://github.com/google/google-api-php-client#download-the-release) to download the package locally. + +### What to do with the files + +After obtaining the files, include the autloader. If you used Composer, your require statement will look like this: + +```php +require_once '/path/to/your-project/vendor/autoload.php'; +``` + +If you downloaded the package separately, your require statement will look like this: + +```php +require_once '/path/to/google-api-php-client/vendor/autoload.php'; +``` \ No newline at end of file diff --git a/docs/media.md b/docs/media.md new file mode 100644 index 000000000..783e632bc --- /dev/null +++ b/docs/media.md @@ -0,0 +1,75 @@ +# Media Upload + +The PHP client library allows for uploading large files for use with APIs such as Drive or YouTube. There are three different methods available. + +## Simple Upload + +In the simple upload case, the data is passed as the body of the request made to the server. This limits the ability to specify metadata, but is very easy to use. + +```php +$file = new Google_Service_Drive_DriveFile(); +$result = $service->files->insert($file, array( + 'data' => file_get_contents("path/to/file"), + 'mimeType' => 'application/octet-stream', + 'uploadType' => 'media' +)); +``` + +## Multipart File Upload + +With multipart file uploads, the uploaded file is sent as one part of a multipart form post. This allows metadata about the file object to be sent as part of the post as well. This is triggered by specifying the _multipart_ uploadType. + +```php +$file = new Google_Service_Drive_DriveFile(); +$file->setTitle("Hello World!"); +$result = $service->files->insert($file, array( + 'data' => file_get_contents("path/to/file"), + 'mimeType' => 'application/octet-stream', + 'uploadType' => 'multipart' +)); +``` + +## Resumable File Upload + +It is also possible to split the upload across multiple requests. This is convenient for larger files, and allows resumption of the upload if there is a problem. Resumable uploads can be sent with separate metadata. + +```php +$file = new Google_Service_Drive_DriveFile(); +$file->title = "Big File"; +$chunkSizeBytes = 1 * 1024 * 1024; + +// Call the API with the media upload, defer so it doesn't immediately return. +$client->setDefer(true); +$request = $service->files->insert($file); + +// Create a media file upload to represent our upload process. +$media = new Google_Http_MediaFileUpload( + $client, + $request, + 'text/plain', + null, + true, + $chunkSizeBytes +); +$media->setFileSize(filesize("path/to/file")); + +// Upload the various chunks. $status will be false until the process is +// complete. +$status = false; +$handle = fopen("path/to/file", "rb"); +while (!$status && !feof($handle)) { + $chunk = fread($handle, $chunkSizeBytes); + $status = $media->nextChunk($chunk); + } + +// The final value of $status will be the data from the API for the object +// that has been uploaded. +$result = false; +if($status != false) { + $result = $status; +} + +fclose($handle); +// Reset to the client to execute requests immediately in the future. +$client->setDefer(false); +``` \ No newline at end of file diff --git a/docs/oauth-server.md b/docs/oauth-server.md new file mode 100644 index 000000000..f8db920be --- /dev/null +++ b/docs/oauth-server.md @@ -0,0 +1,144 @@ +# Using OAuth 2.0 for Server to Server Applications + +The Google APIs Client Library for PHP supports using OAuth 2.0 for server-to-server interactions such as those between a web application and a Google service. For this scenario you need a service account, which is an account that belongs to your application instead of to an individual end user. Your application calls Google APIs on behalf of the service account, so users aren't directly involved. This scenario is sometimes called "two-legged OAuth," or "2LO." (The related term "three-legged OAuth" refers to scenarios in which your application calls Google APIs on behalf of end users, and in which user consent is sometimes required.) + +Typically, an application uses a service account when the application uses Google APIs to work with its own data rather than a user's data. For example, an application that uses [Google Cloud Datastore](https://cloud.google.com/datastore/) for data persistence would use a service account to authenticate its calls to the Google Cloud Datastore API. + +If you have a G Suite domain—if you use [G Suite Business](https://gsuite.google.com), for example—an administrator of the G Suite domain can authorize an application to access user data on behalf of users in the G Suite domain. For example, an application that uses the [Google Calendar API](https://developers.google.com/google-apps/calendar/) to add events to the calendars of all users in a G Suite domain would use a service account to access the Google Calendar API on behalf of users. Authorizing a service account to access data on behalf of users in a domain is sometimes referred to as "delegating domain-wide authority" to a service account. + +> **Note:** When you use [G Suite Marketplace](https://www.google.com/enterprise/marketplace/) to install an application for your domain, the required permissions are automatically granted to the application. You do not need to manually authorize the service accounts that the application uses. + +> **Note:** Although you can use service accounts in applications that run from a G Suite domain, service accounts are not members of your G Suite account and aren't subject to domain policies set by G Suite administrators. For example, a policy set in the G Suite admin console to restrict the ability of Apps end users to share documents outside of the domain would not apply to service accounts. + +This document describes how an application can complete the server-to-server OAuth 2.0 flow by using the Google APIs Client Library for PHP. + +## Overview + +To support server-to-server interactions, first create a service account for your project in the Developers Console. If you want to access user data for users in your G Suite domain, then delegate domain-wide access to the service account. + +Then, your application prepares to make authorized API calls by using the service account's credentials to request an access token from the OAuth 2.0 auth server. + +Finally, your application can use the access token to call Google APIs. + +## Creating a service account + +A service account's credentials include a generated email address that is unique, a client ID, and at least one public/private key pair. + +If your application runs on Google App Engine, a service account is set up automatically when you create your project. + +If your application doesn't run on Google App Engine or Google Compute Engine, you must obtain these credentials in the Google Developers Console. To generate service-account credentials, or to view the public credentials that you've already generated, do the following: + +If your application runs on Google App Engine, a service account is set up automatically when you create your project. + +If your application doesn't run on Google App Engine or Google Compute Engine, you must obtain these credentials in the Google Developers Console. To generate service-account credentials, or to view the public credentials that you've already generated, do the following: + +1. Open the [**Service accounts** section](https://console.developers.google.com/permissions/serviceaccounts?project=_) of the Developers Console's **Permissions** page. +2. Click **Create service account**. +3. In the **Create service account** window, type a name for the service account and select **Furnish a new private key**. If you want to [grant G Suite domain-wide authority](https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority) to the service account, also select **Enable G Suite Domain-wide Delegation**. Then, click **Create**. + +Your new public/private key pair is generated and downloaded to your machine; it serves as the only copy of this key. You are responsible for storing it securely. + +You can return to the [Developers Console](https://console.developers.google.com/) at any time to view the client ID, email address, and public key fingerprints, or to generate additional public/private key pairs. For more details about service account credentials in the Developers Console, see [Service accounts](https://developers.google.com/console/help/service-accounts) in the Developers Console help file. + +Take note of the service account's email address and store the service account's private key file in a location accessible to your application. Your application needs them to make authorized API calls. + +**Note:** You must store and manage private keys securely in both development and production environments. Google does not keep a copy of your private keys, only your public keys. + +### Delegating domain-wide authority to the service account + +If your application runs in a G Suite domain and accesses user data, the service account that you created needs to be granted access to the user data that you want to access. + +The following steps must be performed by an administrator of the G Suite domain: + +1. Go to your G Suite domain’s [Admin console](http://admin.google.com). +2. Select **Security** from the list of controls. If you don't see **Security** listed, select **More controls** from the gray bar at the bottom of the page, then select **Security** from the list of controls. If you can't see the controls, make sure you're signed in as an administrator for the domain. +3. Select **Advanced settings** from the list of options. +4. Select **Manage third party OAuth Client access** in the **Authentication** section. +5. In the **Client name** field enter the service account's **Client ID**. +6. In the **One or More API Scopes** field enter the list of scopes that your application should be granted access to. For example, if your application needs domain-wide access to the Google Drive API and the Google Calendar API, enter: https://www.googleapis.com/auth/drive, https://www.googleapis.com/auth/calendar. +7. Click **Authorize**. + +Your application now has the authority to make API calls as users in your domain (to "impersonate" users). When you prepare to make authorized API calls, you specify the user to impersonate. + +[](#top_of_page)Preparing to make an authorized API call +-------------------------------------------------------- + +After you have obtained the client email address and private key from the Developers Console, set the path to these credentials in the `GOOGLE_APPLICATION_CREDENTIALS` environment variable ( **Note:** This is not required in the App Engine environment): + +```php +putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json'); +``` + +Call the `useApplicationDefaultCredentials` to use your service account credentials to authenticate: + +```php +$client = new Google_Client(); +$client->useApplicationDefaultCredentials(); +``` + +If you have delegated domain-wide access to the service account and you want to impersonate a user account, specify the email address of the user account using the method `setSubject`: + +```php +$client->setSubject($user_to_impersonate); +``` + +Use the authorized `Google_Client` object to call Google APIs in your application. + +## Calling Google APIs + +Use the authorized `Google_Client` object to call Google APIs by completing the following steps: + +1. Build a service object for the API that you want to call, providing the authorized `Google_Client` object. For example, to call the Cloud SQL Administration API: + + ```php + $sqladmin = new Google_Service_SQLAdmin($client); + ``` + +2. Make requests to the API service using the [interface provided by the service object](https://developers.google.com/api-client-library/php/start/get_started#build). For example, to list the instances of Cloud SQL databases in the examinable-example-123 project: + + ```php + $response = $sqladmin->instances->listInstances('examinable-example-123')->getItems(); + ``` + +## Complete example + +The following example prints a JSON-formatted list of Cloud SQL instances in a project. + +To run this example: + +1. Create a new directory and change to it. For example: + + ```sh + mkdir ~/php-oauth2-example + cd ~/php-oauth2-example + ``` + +2. Install the [Google API Client Library](https://github.com/google/google-api-php-client) for PHP using [Composer](https://getcomposer.org): + + ```sh + composer require google/apiclient:^2.0 + ``` + +3. Create the file sqlinstances.php with the content below. +4. Run the example from the command line: + + ``` + php ~/php-oauth2-example/sqlinstances.php + ``` + +### sqlinstances.php + +```php +useApplicationDefaultCredentials(); + +$sqladmin = new Google_Service_SQLAdmin($client); +$response = $sqladmin->instances + ->listInstances('examinable-example-123')->getItems(); +echo json_encode($response) . "\n"; +``` \ No newline at end of file diff --git a/docs/oauth-web.md b/docs/oauth-web.md new file mode 100644 index 000000000..96314a76a --- /dev/null +++ b/docs/oauth-web.md @@ -0,0 +1,424 @@ +# Using OAuth 2.0 for Web Server Applications + +This document explains how web server applications use the Google API Client Library for PHP to implement OAuth 2.0 authorization to access Google APIs. OAuth 2.0 allows users to share specific data with an application while keeping their usernames, passwords, and other information private. For example, an application can use OAuth 2.0 to obtain permission from users to store files in their Google Drives. + +This OAuth 2.0 flow is specifically for user authorization. It is designed for applications that can store confidential information and maintain state. A properly authorized web server application can access an API while the user interacts with the application or after the user has left the application. + +Web server applications frequently also use [service accounts](oauth-server.md) to authorize API requests, particularly when calling Cloud APIs to access project-based data rather than user-specific data. Web server applications can use service accounts in conjunction with user authorization. + +## Prerequisites + +### Enable APIs for your project + +Any application that calls Google APIs needs to enable those APIs in the API Console. To enable the appropriate APIs for your project: + +1. Open the [Library](https://console.developers.google.com/apis/library) page in the API Console. +2. Select the project associated with your application. Create a project if you do not have one already. +3. Use the **Library** page to find each API that your application will use. Click on each API and enable it for your project. + +### Create authorization credentials + +Any application that uses OAuth 2.0 to access Google APIs must have authorization credentials that identify the application to Google's OAuth 2.0 server. The following steps explain how to create credentials for your project. Your applications can then use the credentials to access APIs that you have enabled for that project. + +1. Open the [Credentials page](https://console.developers.google.com/apis/credentials) in the API Console. +2. Click **Create credentials > OAuth client ID**. +3. Complete the form. Set the application type to `Web application`. Applications that use languages and frameworks like PHP, Java, Python, Ruby, and .NET must specify authorized **redirect URIs**. The redirect URIs are the endpoints to which the OAuth 2.0 server can send responses. + + For testing, you can specify URIs that refer to the local machine, such as `http://localhost:8080`. With that in mind, please note that all of the examples in this document use `http://localhost:8080` as the redirect URI. + + We recommend that you design your app's auth endpoints so that your application does not expose authorization codes to other resources on the page. + +After creating your credentials, download the **client_secret.json** file from the API Console. Securely store the file in a location that only your application can access. + +> **Important:** Do not store the **client_secret.json** file in a publicly-accessible location. In addition, if you share the source code to your application—for example, on GitHub—store the **client_secret.json** file outside of your source tree to avoid inadvertently sharing your client credentials. + +### Identify access scopes + +Scopes enable your application to only request access to the resources that it needs while also enabling users to control the amount of access that they grant to your application. Thus, there may be an inverse relationship between the number of scopes requested and the likelihood of obtaining user consent. + +Before you start implementing OAuth 2.0 authorization, we recommend that you identify the scopes that your app will need permission to access. + +We also recommend that your application request access to authorization scopes via an [incremental authorization](#incremental-authorization) process, in which your application requests access to user data in context. This best practice helps users to more easily understand why your application needs the access it is requesting. + +The [OAuth 2.0 API Scopes](https://developers.google.com/identity/protocols/googlescopes) document contains a full list of scopes that you might use to access Google APIs. + +> If your public application uses scopes that permit access to certain user data, it must pass review. If you see **unverified app** on the screen when testing your application, you must submit a verification request to remove it. Find out more about [unverified apps](https://support.google.com/cloud/answer/7454865) and get answers to [frequently asked questions about app verification](https://support.google.com/cloud/answer/9110914) in the Help Center. + +### Language-specific requirements + +To run any of the code samples in this document, you'll need a Google account, access to the Internet, and a web browser. If you are using one of the API client libraries, also see the language-specific requirements below. + +To run the PHP code samples in this document, you'll need: + +* PHP 5.4 or greater with the command-line interface (CLI) and JSON extension installed. +* The [Composer](https://getcomposer.org/) dependency management tool. +* The Google APIs Client Library for PHP: + ```sh + php composer.phar require google/apiclient:^2.0 + ``` + +## Obtaining OAuth 2.0 access tokens + +The following steps show how your application interacts with Google's OAuth 2.0 server to obtain a user's consent to perform an API request on the user's behalf. Your application must have that consent before it can execute a Google API request that requires user authorization. + +The list below quickly summarizes these steps: + +1. Your application identifies the permissions it needs. +2. Your application redirects the user to Google along with the list of requested permissions. +3. The user decides whether to grant the permissions to your application. +4. Your application finds out what the user decided. +5. If the user granted the requested permissions, your application retrieves tokens needed to make API requests on the user's behalf. + +### Step 1: Set authorization parameters + +Your first step is to create the authorization request. That request sets parameters that identify your application and define the permissions that the user will be asked to grant to your application. + +The code snippet below creates a `Google_Client()` object, which defines the parameters in the authorization request. + +That object uses information from your **client_secret.json** file to identify your application. The object also identifies the scopes that your application is requesting permission to access and the URL to your application's auth endpoint, which will handle the response from Google's OAuth 2.0 server. Finally, the code sets the optional access_type and include_granted_scopes parameters. + +For example, this code requests read-only, offline access to a user's Google Drive: + +```php +$client = new Google_Client(); +$client->setAuthConfig('client_secret.json'); +$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY); +$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php'); +$client->setAccessType('offline'); // offline access +$client->setIncludeGrantedScopes(true); // incremental auth +``` + +The request specifies the following information: + +#### Parameters + +##### `client_id` + +**Required**. The client ID for your application. You can find this value in the [API Console](https://console.developers.google.com/). In PHP, call the `setAuthConfig` function to load authorization credentials from a **client_secret.json** file. + +```php +$client = new Google_Client(); +$client->setAuthConfig('client_secret.json'); +``` + +##### `redirect_uri` + +**Required**. Determines where the API server redirects the user after the user completes the authorization flow. The value must exactly match one of the authorized redirect URIs for the OAuth 2.0 client, which you configured in the [API Console](https://console.developers.google.com/). If this value doesn't match an authorized URI, you will get a 'redirect_uri_mismatch' error. Note that the `http` or `https` scheme, case, and trailing slash ('`/`') must all match. + +To set this value in PHP, call the `setRedirectUri` function. Note that you must specify a valid redirect URI for your API Console project. + +```php +$client->setRedirectUri('http://localhost:8080/oauth2callback.php'); +``` + +##### `scope` + +**Required**. A space-delimited list of scopes that identify the resources that your application could access on the user's behalf. These values inform the consent screen that Google displays to the user. + +Scopes enable your application to only request access to the resources that it needs while also enabling users to control the amount of access that they grant to your application. Thus, there is an inverse relationship between the number of scopes requested and the likelihood of obtaining user consent. To set this value in PHP, call the `addScope` function: + +```php +$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY); +``` + +The [OAuth 2.0 API Scopes](https://developers.google.com/identity/protocols/googlescopes) document provides a full list of scopes that you might use to access Google APIs. + +We recommend that your application request access to authorization scopes in context whenever possible. By requesting access to user data in context, via [incremental authorization](#Incremental-authorization), you help users to more easily understand why your application needs the access it is requesting. + +##### `access_type` + +**Recommended**. Indicates whether your application can refresh access tokens when the user is not present at the browser. Valid parameter values are `online`, which is the default value, and `offline`. + +Set the value to `offline` if your application needs to refresh access tokens when the user is not present at the browser. This is the method of refreshing access tokens described later in this document. This value instructs the Google authorization server to return a refresh token _and_ an access token the first time that your application exchanges an authorization code for tokens. + +To set this value in PHP, call the `setAccessType` function: + +```php +$client->setAccessType('offline'); +``` + +##### `state` + +**Recommended**. Specifies any string value that your application uses to maintain state between your authorization request and the authorization server's response. The server returns the exact value that you send as a `name=value` pair in the hash (`#`) fragment of the `redirect_uri` after the user consents to or denies your application's access request. + +You can use this parameter for several purposes, such as directing the user to the correct resource in your application, sending nonces, and mitigating cross-site request forgery. Since your `redirect_uri` can be guessed, using a `state` value can increase your assurance that an incoming connection is the result of an authentication request. If you generate a random string or encode the hash of a cookie or another value that captures the client's state, you can validate the response to additionally ensure that the request and response originated in the same browser, providing protection against attacks such as cross-site request forgery. See the [OpenID Connect](https://developers.google.com/identity/protocols/OpenIDConnect#createxsrftoken) documentation for an example of how to create and confirm a `state` token. + +To set this value in PHP, call the `setState` function: + +```php +$client->setState($sample_passthrough_value); +``` + +##### `include_granted_scopes` + +**Optional**. Enables applications to use incremental authorization to request access to additional scopes in context. If you set this parameter's value to `true` and the authorization request is granted, then the new access token will also cover any scopes to which the user previously granted the application access. See the [incremental authorization](#Incremental-authorization) section for examples. + +To set this value in PHP, call the `setIncludeGrantedScopes` function: + +```php +$client->setIncludeGrantedScopes(true); +``` + +##### `login_hint` + +**Optional**. If your application knows which user is trying to authenticate, it can use this parameter to provide a hint to the Google Authentication Server. The server uses the hint to simplify the login flow either by prefilling the email field in the sign-in form or by selecting the appropriate multi-login session. + +Set the parameter value to an email address or `sub` identifier, which is equivalent to the user's Google ID. + +To set this value in PHP, call the `setLoginHint` function: + +```php +$client->setLoginHint('timmerman@google.com'); +``` + +##### `prompt` + +**Optional**. A space-delimited, case-sensitive list of prompts to present the user. If you don't specify this parameter, the user will be prompted only the first time your app requests access. + +To set this value in PHP, call the `setApprovalPrompt` function: + +```php +$client->setApprovalPrompt('consent'); +``` + +Possible values are: + +`none` + +Do not display any authentication or consent screens. Must not be specified with other values. + +`consent` + +Prompt the user for consent. + +`select_account` + +Prompt the user to select an account. + +### Step 2: Redirect to Google's OAuth 2.0 server + +Redirect the user to Google's OAuth 2.0 server to initiate the authentication and authorization process. Typically, this occurs when your application first needs to access the user's data. In the case of [incremental authorization](#incremental-authorization), this step also occurs when your application first needs to access additional resources that it does not yet have permission to access. + +1. Generate a URL to request access from Google's OAuth 2.0 server: + + ```php + $auth_url = $client->createAuthUrl(); + ``` + +2. Redirect the user to `$auth_url`: + + ```php + header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL)); + ``` + +Google's OAuth 2.0 server authenticates the user and obtains consent from the user for your application to access the requested scopes. The response is sent back to your application using the redirect URL you specified. + +### Step 3: Google prompts user for consent + +In this step, the user decides whether to grant your application the requested access. At this stage, Google displays a consent window that shows the name of your application and the Google API services that it is requesting permission to access with the user's authorization credentials. The user can then consent or refuse to grant access to your application. + +Your application doesn't need to do anything at this stage as it waits for the response from Google's OAuth 2.0 server indicating whether the access was granted. That response is explained in the following step. + +### Step 4: Handle the OAuth 2.0 server response + +The OAuth 2.0 server responds to your application's access request by using the URL specified in the request. + +If the user approves the access request, then the response contains an authorization code. If the user does not approve the request, the response contains an error message. The authorization code or error message that is returned to the web server appears on the query string, as shown below: + +An error response: + + https://oauth2.example.com/auth?error=access_denied + +An authorization code response: + + https://oauth2.example.com/auth?code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7 + +> **Important**: If your response endpoint renders an HTML page, any resources on that page will be able to see the authorization code in the URL. Scripts can read the URL directly, and the URL in the `Referer` HTTP header may be sent to any or all resources on the page. +> +> Carefully consider whether you want to send authorization credentials to all resources on that page (especially third-party scripts such as social plugins and analytics). To avoid this issue, we recommend that the server first handle the request, then redirect to another URL that doesn't include the response parameters. + +#### Sample OAuth 2.0 server response + +You can test this flow by clicking on the following sample URL, which requests read-only access to view metadata for files in your Google Drive: + +``` +https://accounts.google.com/o/oauth2/v2/auth? + scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.metadata.readonly& + access_type=offline& + include_granted_scopes=true& + state=state_parameter_passthrough_value& + redirect_uri=http%3A%2F%2Foauth2.example.com%2Fcallback& + response_type=code& + client_id=client_id +``` + +After completing the OAuth 2.0 flow, you should be redirected to `http://localhost/oauth2callback`, which will likely yield a `404 NOT FOUND` error unless your local machine serves a file at that address. The next step provides more detail about the information returned in the URI when the user is redirected back to your application. + +### Step 5: Exchange authorization code for refresh and access tokens + +After the web server receives the authorization code, it can exchange the authorization code for an access token. + +To exchange an authorization code for an access token, use the `authenticate` method: + +```php +$client->authenticate($_GET['code']); +``` + +You can retrieve the access token with the `getAccessToken` method: + +```php +$access_token = $client->getAccessToken(); +``` + +[](#top_of_page)Calling Google APIs +----------------------------------- + +Use the access token to call Google APIs by completing the following steps: + +1. If you need to apply an access token to a new `Google_Client` object—for example, if you stored the access token in a user session—use the `setAccessToken` method: + + ```php + $client->setAccessToken($access_token); + ``` + +2. Build a service object for the API that you want to call. You build a a service object by providing an authorized `Google_Client` object to the constructor for the API you want to call. For example, to call the Drive API: + + ```php + $drive = new Google_Service_Drive($client); + ``` + +3. Make requests to the API service using the [interface provided by the service object](start.md). For example, to list the files in the authenticated user's Google Drive: + + ```php + $files = $drive->files->listFiles(array())->getItems(); + ``` + +[](#top_of_page)Complete example +-------------------------------- + +The following example prints a JSON-formatted list of files in a user's Google Drive after the user authenticates and gives consent for the application to access the user's Drive files. + +To run this example: + +1. In the API Console, add the URL of the local machine to the list of redirect URLs. For example, add `http://localhost:8080`. +2. Create a new directory and change to it. For example: + + ```sh + mkdir ~/php-oauth2-example + cd ~/php-oauth2-example + ``` + +3. Install the [Google API Client Library](https://github.com/google/google-api-php-client) for PHP using [Composer](https://getcomposer.org): + + ```sh + composer require google/apiclient:^2.0 + ``` + +4. Create the files `index.php` and `oauth2callback.php` with the content below. +5. Run the example with a web server configured to serve PHP. If you use PHP 5.4 or newer, you can use PHP's built-in test web server: + + ```sh + php -S localhost:8080 ~/php-oauth2-example + ``` + +#### index.php + +```php +setAuthConfig('client_secrets.json'); +$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY); + +if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { + $client->setAccessToken($_SESSION['access_token']); + $drive = new Google_Service_Drive($client); + $files = $drive->files->listFiles(array())->getItems(); + echo json_encode($files); +} else { + $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php'; + header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); +} +``` + +#### oauth2callback.php + +```php +setAuthConfigFile('client_secrets.json'); +$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php'); +$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY); + +if (! isset($_GET['code'])) { + $auth_url = $client->createAuthUrl(); + header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL)); +} else { + $client->authenticate($_GET['code']); + $_SESSION['access_token'] = $client->getAccessToken(); + $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/'; + header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); +} +``` + +## Incremental authorization + +In the OAuth 2.0 protocol, your app requests authorization to access resources, which are identified by scopes. It is considered a best user-experience practice to request authorization for resources at the time you need them. To enable that practice, Google's authorization server supports incremental authorization. This feature lets you request scopes as they are needed and, if the user grants permission, add those scopes to your existing access token for that user. + +For example, an app that lets people sample music tracks and create mixes might need very few resources at sign-in time, perhaps nothing more than the name of the person signing in. However, saving a completed mix would require access to their Google Drive. Most people would find it natural if they only were asked for access to their Google Drive at the time the app actually needed it. + +In this case, at sign-in time the app might request the `profile` scope to perform basic sign-in, and then later request the `https://www.googleapis.com/auth/drive.file` scope at the time of the first request to save a mix. + +To implement incremental authorization, you complete the normal flow for requesting an access token but make sure that the authorization request includes previously granted scopes. This approach allows your app to avoid having to manage multiple access tokens. + +The following rules apply to an access token obtained from an incremental authorization: + +* The token can be used to access resources corresponding to any of the scopes rolled into the new, combined authorization. +* When you use the refresh token for the combined authorization to obtain an access token, the access token represents the combined authorization and can be used for any of its scopes. +* The combined authorization includes all scopes that the user granted to the API project even if the grants were requested from different clients. For example, if a user granted access to one scope using an application's desktop client and then granted another scope to the same application via a mobile client, the combined authorization would include both scopes. +* If you revoke a token that represents a combined authorization, access to all of that authorization's scopes on behalf of the associated user are revoked simultaneously. + +The example for [setting authorization parameters](#Step-1-Set-authorization-parameters) demonstrates how to ensure authorization requests follow this best practice. The code snippet below also shows the code that you need to add to use incremental authorization. + +```php +$client->setIncludeGrantedScopes(true); +``` + +## Refreshing an access token (offline access) + +Access tokens periodically expire. You can refresh an access token without prompting the user for permission (including when the user is not present) if you requested offline access to the scopes associated with the token. + +If you use a Google API Client Library, the [client object](#Step-1-Set-authorization-parameters) refreshes the access token as needed as long as you configure that object for offline access. + +Requesting offline access is a requirement for any application that needs to access a Google API when the user is not present. For example, an app that performs backup services or executes actions at predetermined times needs to be able to refresh its access token when the user is not present. The default style of access is called `online`. + +Server-side web applications, installed applications, and devices all obtain refresh tokens during the authorization process. Refresh tokens are not typically used in client-side (JavaScript) web applications. + +If your application needs offline access to a Google API, set the API client's access type to `offline`: + +```php +$client->setAccessType("offline"); +``` + +After a user grants offline access to the requested scopes, you can continue to use the API client to access Google APIs on the user's behalf when the user is offline. The client object will refresh the access token as needed. + +## Revoking a token + +In some cases a user may wish to revoke access given to an application. A user can revoke access by visiting [Account Settings](https://security.google.com/settings/security/permissions). It is also possible for an application to programmatically revoke the access given to it. Programmatic revocation is important in instances where a user unsubscribes or removes an application. In other words, part of the removal process can include an API request to ensure the permissions granted to the application are removed. + +To programmatically revoke a token, call `revokeToken()`: + +```php +$client->revokeToken(); +``` + +**Note:** Following a successful revocation response, it might take some time before the revocation has full effect. + +Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see our [Site Policies](https://developers.google.com/terms/site-policies). Java is a registered trademark of Oracle and/or its affiliates. \ No newline at end of file diff --git a/docs/pagination.md b/docs/pagination.md new file mode 100644 index 000000000..22bf78721 --- /dev/null +++ b/docs/pagination.md @@ -0,0 +1,10 @@ +# Pagination + +Most list API calls have a maximum limit of results they will return in a single response. To allow retrieving more than this number of results, responses may return a pagination token which can be passed with a request in order to access subsequent pages. + +The token for the page will normally be found on list response objects, normally `nextPageToken`. This can be passed in the optional params. + +```php +$token = $results->getNextPageToken(); +$server->listActivities('me', 'public', array('pageToken' => $token)); +``` \ No newline at end of file diff --git a/docs/parameters.md b/docs/parameters.md new file mode 100644 index 000000000..029ebe4ec --- /dev/null +++ b/docs/parameters.md @@ -0,0 +1,14 @@ +# Standard Parameters + +Many API methods include support for certain optional parameters. In addition to these there are several standard parameters that can be applied to any API call. These are defined in the `Google_Service_Resource` class. + +## Parameters + +- **alt**: Specify an alternative response type, for example csv. +- **fields**: A comma separated list of fields that should be included in the response. Nested parameters can be specified with parens, e.g. key,parent(child/subsection). +- **userIp**: The IP of the end-user making the request. This is used in per-user request quotas, as defined in the Google Developers Console +- **quotaUser**: A user ID for the end user, an alternative to userIp for applying per-user request quotas +- **data**: Used as part of [media](media.md) +- **mimeType**: Used as part of [media](media.md) +- **uploadType**: Used as part of [media](media.md) +- **mediaUpload**: Used as part of [media](media.md) \ No newline at end of file diff --git a/docs/start.md b/docs/start.md new file mode 100644 index 000000000..a315d665e --- /dev/null +++ b/docs/start.md @@ -0,0 +1,91 @@ +# Getting Started + +This document provides all the basic information you need to start using the library. It covers important library concepts, shows examples for various use cases, and gives links to more information. + +## Setup + +There are a few setup steps you need to complete before you can use this library: + +1. If you don't already have a Google account, [sign up](https://www.google.com/accounts). +2. If you have never created a Google API project, read the [Managing Projects page](https://developers.google.com/console/help/#managingprojects) and create a project in the [Google Developers Console](https://console.developers.google.com/) +3. [Install](install.md) the library. + +## Authentication and authorization + +It is important to understand the basics of how API authentication and authorization are handled. All API calls must use either simple or authorized access (defined below). Many API methods require authorized access, but some can use either. Some API methods that can use either behave differently, depending on whether you use simple or authorized access. See the API's method documentation to determine the appropriate access type. + +### 1. Simple API access (API keys) + +These API calls do not access any private user data. Your application must authenticate itself as an application belonging to your Google Cloud project. This is needed to measure project usage for accounting purposes. + +#### Important concepts + +* **API key**: To authenticate your application, use an [API key](https://cloud.google.com/docs/authentication/api-keys) for your Google Cloud Console project. Every simple access call your application makes must include this key. + +> **Warning**: Keep your API key private. If someone obtains your key, they could use it to consume your quota or incur charges against your Google Cloud project. + + +### 2. Authorized API access (OAuth 2.0) + +These API calls access private user data. Before you can call them, the user that has access to the private data must grant your application access. Therefore, your application must be authenticated, the user must grant access for your application, and the user must be authenticated in order to grant that access. All of this is accomplished with [OAuth 2.0](https://developers.google.com/identity/protocols/OAuth2) and libraries written for it. + +#### Important concepts + +* **Scope**: Each API defines one or more scopes that declare a set of operations permitted. For example, an API might have read-only and read-write scopes. When your application requests access to user data, the request must include one or more scopes. The user needs to approve the scope of access your application is requesting. +* **Refresh and access tokens**: When a user grants your application access, the OAuth 2.0 authorization server provides your application with refresh and access tokens. These tokens are only valid for the scope requested. Your application uses access tokens to authorize API calls. Access tokens expire, but refresh tokens do not. Your application can use a refresh token to acquire a new access token. + + > **Warning**: Keep refresh and access tokens private. If someone obtains your tokens, they could use them to access private user data. + +* **Client ID and client secret**: These strings uniquely identify your application and are used to acquire tokens. They are created for your Google Cloud project on the [API Access pane](https://code.google.com/apis/console#:access) of the Google Cloud. There are three types of client IDs, so be sure to get the correct type for your application: + + * Web application client IDs + * Installed application client IDs + * [Service Account](https://developers.google.com/identity/protocols/OAuth2ServiceAccount) client IDs + + > **Warning**: Keep your client secret private. If someone obtains your client secret, they could use it to consume your quota, incur charges against your Google Cloud project, and request access to user data. + + +## Building and calling a service + +This section described how to build an API-specific service object, make calls to the service, and process the response. + +### Build the client object + +The client object is the primary container for classes and configuration in the library. + +```php +$client = new Google_Client(); +$client->setApplicationName("My Application"); +$client->setDeveloperKey("MY_SIMPLE_API_KEY"); +``` + +### Build the service object + +Services are called through queries to service specific objects. These are created by constructing the service object, and passing an instance of `Google_Client` to it. `Google_Client` contains the IO, authentication and other classes required by the service to function, and the service informs the client which scopes it uses to provide a default when authenticating a user. + +```php +$service = new Google_Service_Books($client); +``` + +### Calling an API + +Each API provides resources and methods, usually in a chain. These can be accessed from the service object in the form `$service->resource->method(args)`. Most method require some arguments, then accept a final parameter of an array containing optional parameters. For example, with the Google Books API, we can make a call to list volumes matching a certain string, and add an optional _filter_ parameter. + +```php +$optParams = array('filter' => 'free-ebooks'); +$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams); +``` + +### Handling the result + +There are two main types of response - items and collections of items. Each can be accessed either as an object or as an array. Collections implement the `Iterator` interface so can be used in foreach and other constructs. + +```php +foreach ($results as $item) { + echo $item['volumeInfo']['title'], "
      \n"; +} +``` + +## Google App Engine support + +This library works well with Google App Engine applications. The Memcache class is automatically used for caching, and the file IO is implemented with the use of the Streams API. \ No newline at end of file From a8a4d8187d33ac6e6eb484ec8285131666cb8c2e Mon Sep 17 00:00:00 2001 From: David Supplee Date: Thu, 20 Jun 2019 08:28:04 -0700 Subject: [PATCH 098/301] docs: Update links in README to point to new docs living inside repo (#1670) * docs: Update links in README to point to new docs living inside repo * docs: use correct link --- README.md | 8 ++++---- docs/oauth-server.md | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4911151b0..9d64d855a 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Uncompress the zip file you download, and include the autoloader in your project require_once '/path/to/google-api-php-client/vendor/autoload.php'; ``` -For additional installation and setup instructions, see [the documentation](https://developers.google.com/api-client-library/php/start/installation). +For additional installation and setup instructions, see [the documentation](docs/). ## Examples ## See the [`examples/`](examples) directory for examples of the key client features. You can @@ -86,7 +86,7 @@ foreach ($results as $item) { > An example of this can be seen in [`examples/simple-file-upload.php`](examples/simple-file-upload.php). -1. Follow the instructions to [Create Web Application Credentials](https://developers.google.com/api-client-library/php/auth/web-app#creatingcred) +1. Follow the instructions to [Create Web Application Credentials](docs/oauth-web.md#create-authorization-credentials) 1. Download the JSON credentials 1. Set the path to these credentials using `Google_Client::setAuthConfig`: @@ -127,7 +127,7 @@ Some APIs not support service accounts. Check with the specific API documentation if API calls return unexpected 401 or 403 errors. -1. Follow the instructions to [Create a Service Account](https://developers.google.com/api-client-library/php/auth/service-accounts#creatinganaccount) +1. Follow the instructions to [Create a Service Account](docs/oauth-server.md#creating-a-service-account) 1. Download the JSON credentials 1. Set the path to these credentials using the `GOOGLE_APPLICATION_CREDENTIALS` environment variable: @@ -341,7 +341,7 @@ YouTube: https://github.com/youtube/api-samples/tree/master/php ## How Do I Contribute? ## -Please see the [contributing](.github/CONTRIBUTING.md) page for more information. In particular, we love pull requests - but please make sure to sign the [contributor license agreement](https://developers.google.com/api-client-library/php/contribute). +Please see the [contributing](.github/CONTRIBUTING.md) page for more information. In particular, we love pull requests - but please make sure to sign the contributor license agreement. ## Frequently Asked Questions ## diff --git a/docs/oauth-server.md b/docs/oauth-server.md index f8db920be..cba985435 100644 --- a/docs/oauth-server.md +++ b/docs/oauth-server.md @@ -94,7 +94,7 @@ Use the authorized `Google_Client` object to call Google APIs by completing the $sqladmin = new Google_Service_SQLAdmin($client); ``` -2. Make requests to the API service using the [interface provided by the service object](https://developers.google.com/api-client-library/php/start/get_started#build). For example, to list the instances of Cloud SQL databases in the examinable-example-123 project: +2. Make requests to the API service using the [interface provided by the service object](https://github.com/googleapis/google-api-php-client/blob/master/docs/start.md#build-the-service-object). For example, to list the instances of Cloud SQL databases in the examinable-example-123 project: ```php $response = $sqladmin->instances->listInstances('examinable-example-123')->getItems(); @@ -141,4 +141,4 @@ $sqladmin = new Google_Service_SQLAdmin($client); $response = $sqladmin->instances ->listInstances('examinable-example-123')->getItems(); echo json_encode($response) . "\n"; -``` \ No newline at end of file +``` From e26bd86e877f347072319e4cdd144fd89b05114e Mon Sep 17 00:00:00 2001 From: Kslr Date: Sat, 29 Jun 2019 22:19:01 +0800 Subject: [PATCH 099/301] fix docs link (#1673) --- docs/api-keys.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/api-keys.md b/docs/api-keys.md index c4a0da1f2..8b109e669 100644 --- a/docs/api-keys.md +++ b/docs/api-keys.md @@ -2,7 +2,7 @@ When calling APIs that do not access private user data, you can use simple API keys. These keys are used to authenticate your application for accounting purposes. The Google Developers Console documentation also describes [API keys](https://developers.google.com/console/help/using-keys). -> Note: If you do need to access private user data, you must use OAuth 2.0. See [Using OAuth 2.0 for Web Server Applications](docs/oauth-server.md) and [Using OAuth 2.0 for Server to Server Applications](docs/oauth-web.md) for more information. +> Note: If you do need to access private user data, you must use OAuth 2.0. See [Using OAuth 2.0 for Web Server Applications](/docs/oauth-server.md) and [Using OAuth 2.0 for Server to Server Applications](/docs/oauth-web.md) for more information. ## Using API Keys @@ -10,4 +10,4 @@ To use API keys, call the `setDeveloperKey()` method of the `Google_Client` obje ```php $client->setDeveloperKey($api_key); -``` \ No newline at end of file +``` From 0c98c266da1b16611b88cf40981a57b4a56d2584 Mon Sep 17 00:00:00 2001 From: Niall Kennedy Date: Mon, 15 Jul 2019 16:22:39 -0700 Subject: [PATCH 100/301] Update README links (#1677) Updated links for correct URL fragment, scheme, of linked URLs. If a link leads to a redirect, update to its final location. --- README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 9d64d855a..bb4227ce3 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,10 @@ These client libraries are officially supported by Google. However, the librari ## Google Cloud Platform -For Google Cloud Platform APIs such as Datastore, Cloud Storage or Pub/Sub, we recommend using [GoogleCloudPlatform/google-cloud-php](https://github.com/GoogleCloudPlatform/google-cloud-php) which is under active development. +For Google Cloud Platform APIs such as Datastore, Cloud Storage or Pub/Sub, we recommend using [GoogleCloudPlatform/google-cloud-php](https://github.com/googleapis/google-cloud-php) which is under active development. ## Requirements ## -* [PHP 5.4.0 or higher](http://www.php.net/) +* [PHP 5.4.0 or higher](https://www.php.net/) ## Developer Documentation ## @@ -23,7 +23,7 @@ You can use **Composer** or simply **Download the Release** ### Composer -The preferred method is via [composer](https://getcomposer.org). Follow the +The preferred method is via [composer](https://getcomposer.org/). Follow the [installation instructions](https://getcomposer.org/doc/00-intro.md) if you do not already have composer installed. @@ -41,7 +41,7 @@ require_once '/path/to/your-project/vendor/autoload.php'; ### Download the Release -If you abhor using composer, you can download the package in its entirety. The [Releases](https://github.com/google/google-api-php-client/releases) page lists all stable versions. Download any file +If you abhor using composer, you can download the package in its entirety. The [Releases](https://github.com/googleapis/google-api-php-client/releases) page lists all stable versions. Download any file with the name `google-api-php-client-[RELEASE_NAME].zip` for a package including this library and its dependencies. Uncompress the zip file you download, and include the autoloader in your project: @@ -156,7 +156,7 @@ calls return unexpected 401 or 403 errors. ### Making Requests ### -The classes used to call the API in [google-api-php-client-services](https://github.com/Google/google-api-php-client-services) are autogenerated. They map directly to the JSON requests and responses found in the [APIs Explorer](https://developers.google.com/apis-explorer/#p/). +The classes used to call the API in [google-api-php-client-services](https://github.com/googleapis/google-api-php-client-services) are autogenerated. They map directly to the JSON requests and responses found in the [APIs Explorer](https://developers.google.com/apis-explorer/#p/). A JSON request to the [Datastore API](https://developers.google.com/apis-explorer/#p/datastore/v1beta3/datastore.projects.runQuery) would look like this: @@ -263,7 +263,7 @@ $response = $httpClient->get('https://www.googleapis.com/plus/v1/people/me'); ### Caching ### -It is recommended to use another caching library to improve performance. This can be done by passing a [PSR-6](http://www.php-fig.org/psr/psr-6/) compatible library to the client: +It is recommended to use another caching library to improve performance. This can be done by passing a [PSR-6](https://www.php-fig.org/psr/psr-6/) compatible library to the client: ```php use League\Flysystem\Adapter\Local; @@ -285,7 +285,7 @@ composer require cache/filesystem-adapter ### Updating Tokens ### -When using [Refresh Tokens](https://developers.google.com/identity/protocols/OAuth2InstalledApp#refresh) or [Service Account Credentials](https://developers.google.com/identity/protocols/OAuth2ServiceAccount#overview), it may be useful to perform some action when a new access token is granted. To do this, pass a callable to the `setTokenCallback` method on the client: +When using [Refresh Tokens](https://developers.google.com/identity/protocols/OAuth2InstalledApp#offline) or [Service Account Credentials](https://developers.google.com/identity/protocols/OAuth2ServiceAccount#overview), it may be useful to perform some action when a new access token is granted. To do this, pass a callable to the `setTokenCallback` method on the client: ```php $logger = new Monolog\Logger; @@ -316,7 +316,7 @@ One additional step is required in Charles to view SSL requests. Go to **Charles ### Controlling HTTP Client Configuration Directly -Google API Client uses [Guzzle](http://docs.guzzlephp.org) as its default HTTP client. That means that you can control your HTTP requests in the same manner you would for any application using Guzzle. +Google API Client uses [Guzzle](http://docs.guzzlephp.org/) as its default HTTP client. That means that you can control your HTTP requests in the same manner you would for any application using Guzzle. Let's say, for instance, we wished to apply a referrer to each request. @@ -347,9 +347,9 @@ Please see the [contributing](.github/CONTRIBUTING.md) page for more information ### What do I do if something isn't working? ### -For support with the library the best place to ask is via the google-api-php-client tag on StackOverflow: http://stackoverflow.com/questions/tagged/google-api-php-client +For support with the library the best place to ask is via the google-api-php-client tag on StackOverflow: https://stackoverflow.com/questions/tagged/google-api-php-client -If there is a specific bug with the library, please [file a issue](https://github.com/google/google-api-php-client/issues) in the Github issues tracker, including an example of the failing code and any specific errors retrieved. Feature requests can also be filed, as long as they are core library requests, and not-API specific: for those, refer to the documentation for the individual APIs for the best place to file requests. Please try to provide a clear statement of the problem that the feature would address. +If there is a specific bug with the library, please [file a issue](https://github.com/googleapis/google-api-php-client/issues) in the GitHub issues tracker, including an example of the failing code and any specific errors retrieved. Feature requests can also be filed, as long as they are core library requests, and not-API specific: for those, refer to the documentation for the individual APIs for the best place to file requests. Please try to provide a clear statement of the problem that the feature would address. ### I want an example of X! ### @@ -357,7 +357,7 @@ If X is a feature of the library, file away! If X is an example of using a speci ### Why do you still support 5.2? ### -When we started working on the 1.0.0 branch we knew there were several fundamental issues to fix with the 0.6 releases of the library. At that time we looked at the usage of the library, and other related projects, and determined that there was still a large and active base of PHP 5.2 installs. You can see this in statistics such as the PHP versions chart in the WordPress stats: http://wordpress.org/about/stats/. We will keep looking at the types of usage we see, and try to take advantage of newer PHP features where possible. +When we started working on the 1.0.0 branch we knew there were several fundamental issues to fix with the 0.6 releases of the library. At that time we looked at the usage of the library, and other related projects, and determined that there was still a large and active base of PHP 5.2 installs. You can see this in statistics such as the PHP versions chart in the WordPress stats: https://wordpress.org/about/stats/. We will keep looking at the types of usage we see, and try to take advantage of newer PHP features where possible. ### Why does Google_..._Service have weird names? ### From 803d146de921957e9e0de78fb7148f1a8a32dfce Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Fri, 19 Jul 2019 15:58:22 -0400 Subject: [PATCH 101/301] Remove note about PHP 5.2 support (#1678) * Remove note about PHP 5.2 support * Add v2 and v1 note --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index bb4227ce3..d4df596eb 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ The Google API Client Library enables you to work with Google APIs such as Googl These client libraries are officially supported by Google. However, the libraries are considered complete and are in maintenance mode. This means that we will address critical bugs and security issues but will not add any new features. +**NOTE** The actively maintained (v2) version of this client requires PHP 5.4 or above. If you require support for PHP 5.2 or 5.3, use the v1 branch. + ## Google Cloud Platform For Google Cloud Platform APIs such as Datastore, Cloud Storage or Pub/Sub, we recommend using [GoogleCloudPlatform/google-cloud-php](https://github.com/googleapis/google-cloud-php) which is under active development. @@ -355,10 +357,6 @@ If there is a specific bug with the library, please [file a issue](https://githu If X is a feature of the library, file away! If X is an example of using a specific service, the best place to go is to the teams for those specific APIs - our preference is to link to their examples rather than add them to the library, as they can then pin to specific versions of the library. If you have any examples for other APIs, let us know and we will happily add a link to the README above! -### Why do you still support 5.2? ### - -When we started working on the 1.0.0 branch we knew there were several fundamental issues to fix with the 0.6 releases of the library. At that time we looked at the usage of the library, and other related projects, and determined that there was still a large and active base of PHP 5.2 installs. You can see this in statistics such as the PHP versions chart in the WordPress stats: https://wordpress.org/about/stats/. We will keep looking at the types of usage we see, and try to take advantage of newer PHP features where possible. - ### Why does Google_..._Service have weird names? ### The _Service classes are generally automatically generated from the API discovery documents: https://developers.google.com/discovery/. Sometimes new features are added to APIs with unusual names, which can cause some unexpected or non-standard style naming in the PHP classes. From 5f98e13a007644d8faac97ea931e08e2a57c5d0b Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 29 Jul 2019 14:59:14 +0200 Subject: [PATCH 102/301] Fix order of arguments for implode in Google_Service_Resource (#1683) --- src/Google/Service/Resource.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Service/Resource.php b/src/Google/Service/Resource.php index 1f3d3710b..77070fc41 100644 --- a/src/Google/Service/Resource.php +++ b/src/Google/Service/Resource.php @@ -294,7 +294,7 @@ public function createRequestUri($restPath, $params) } if (count($queryVars)) { - $requestUrl .= '?' . implode($queryVars, '&'); + $requestUrl .= '?' . implode('&', $queryVars); } return $requestUrl; From 7826dd0c141280b7b882831ed3313343ce0e710b Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 31 Jul 2019 20:30:17 +0200 Subject: [PATCH 103/301] =?UTF-8?q?Run=20tests=20against=20PHP=207.3+=20an?= =?UTF-8?q?d=20check=20for=20cross-version=20support=E2=80=A6=20(#1684)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Move phpcs config to root folder * Run tests against PHP 7.3 and PHP 7.4 * Check for cross-version support for PHP 5.4 and higher * Only downgrade guzzle and phpseclib on Travis Prevents downgrading dev dependencies --- .gitignore | 1 + .travis.yml | 8 ++++++-- composer.json | 4 +++- style/ruleset.xml => phpcs.xml.dist | 5 +++++ 4 files changed, 15 insertions(+), 3 deletions(-) rename style/ruleset.xml => phpcs.xml.dist (98%) diff --git a/.gitignore b/.gitignore index 0a70c65d2..9b429eae6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .DS_Store phpunit.xml +phpcs.xml composer.lock vendor examples/testfile-small.txt diff --git a/.travis.yml b/.travis.yml index 6ac93c160..80e158254 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,13 +23,17 @@ php: - 7.0 - 7.1 - 7.2 + - 7.3 + - 7.4snapshot # Test lowest dependencies on PHP 5.4 # (Guzzle 5.2, phpseclib 0.3) matrix: include: - php: 5.4 - env: COMPOSER_CMD="composer update --prefer-lowest" RUN_PHP_CS=true + env: COMPOSER_CMD="composer update phpseclib/phpseclib guzzlehttp/guzzle guzzlehttp/psr7 --prefer-lowest" RUN_PHP_CS=true + allow_failures: + - php: 7.4snapshot before_install: - composer self-update @@ -44,4 +48,4 @@ before_script: script: - vendor/bin/phpunit - - if [[ "$RUN_PHP_CS" == "true" ]]; then vendor/bin/phpcs src --standard=style/ruleset.xml -np; fi + - if [[ "$RUN_PHP_CS" == "true" ]]; then vendor/bin/phpcs src -np; fi diff --git a/composer.json b/composer.json index 5cb3ca2e7..ce1e12e73 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,9 @@ "squizlabs/php_codesniffer": "~2.3", "symfony/dom-crawler": "~2.1", "symfony/css-selector": "~2.1", - "cache/filesystem-adapter": "^0.3.2" + "cache/filesystem-adapter": "^0.3.2", + "phpcompatibility/php-compatibility": "^9.2", + "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0" }, "suggest": { "cache/filesystem-adapter": "For caching certs and tokens (using Google_Client::setCache)" diff --git a/style/ruleset.xml b/phpcs.xml.dist similarity index 98% rename from style/ruleset.xml rename to phpcs.xml.dist index d61844541..4e4306986 100644 --- a/style/ruleset.xml +++ b/phpcs.xml.dist @@ -10,6 +10,11 @@ + + + + + From 7ea102e2a17a0d173929097f3ebb4663067ba571 Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Fri, 16 Aug 2019 15:16:22 -0400 Subject: [PATCH 104/301] docs: Add note about cached access token (#1696) * docs: Add note about cached access token * warn about clear cache --- src/Google/Client.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Google/Client.php b/src/Google/Client.php index 92c3b2328..02d7ac873 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -416,6 +416,17 @@ public function isUsingApplicationDefaultCredentials() } /** + * Set the access token used for requests. + * + * Note that at the time requests are sent, tokens are cached. A token will be + * cached for each combination of service and authentication scopes. If a + * cache pool is not provided, creating a new instance of the client will + * allow modification of access tokens. If a persistent cache pool is + * provided, in order to change the access token, you must clear the cached + * token by calling `$client->getCache()->clear()`. (Use caution in this case, + * as calling `clear()` will remove all cache items, including any items not + * related to Google API PHP Client.) + * * @param string|array $token * @throws InvalidArgumentException */ From 5c2c77aa067c508c6adfba796e04ce102fc2844c Mon Sep 17 00:00:00 2001 From: Tchafack Duhamel Date: Mon, 19 Aug 2019 16:19:35 +0200 Subject: [PATCH 105/301] Update api-keys.md (#1699) --- docs/api-keys.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api-keys.md b/docs/api-keys.md index 8b109e669..7828651f7 100644 --- a/docs/api-keys.md +++ b/docs/api-keys.md @@ -2,7 +2,7 @@ When calling APIs that do not access private user data, you can use simple API keys. These keys are used to authenticate your application for accounting purposes. The Google Developers Console documentation also describes [API keys](https://developers.google.com/console/help/using-keys). -> Note: If you do need to access private user data, you must use OAuth 2.0. See [Using OAuth 2.0 for Web Server Applications](/docs/oauth-server.md) and [Using OAuth 2.0 for Server to Server Applications](/docs/oauth-web.md) for more information. +> Note: If you do need to access private user data, you must use OAuth 2.0. See [Using OAuth 2.0 for Web Server Applications](/docs/oauth-web.md) and [Using OAuth 2.0 for Server to Server Applications](/docs/oauth-server.md) for more information. ## Using API Keys From 862042027be2c5741f019922e6d3e6f7ad678888 Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Mon, 19 Aug 2019 13:59:21 -0400 Subject: [PATCH 106/301] docs: Change composer alternative wording. (#1697) Replaces #1685. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d4df596eb..36f04c81f 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ require_once '/path/to/your-project/vendor/autoload.php'; ### Download the Release -If you abhor using composer, you can download the package in its entirety. The [Releases](https://github.com/googleapis/google-api-php-client/releases) page lists all stable versions. Download any file +If you prefer not to use composer, you can download the package in its entirety. The [Releases](https://github.com/googleapis/google-api-php-client/releases) page lists all stable versions. Download any file with the name `google-api-php-client-[RELEASE_NAME].zip` for a package including this library and its dependencies. Uncompress the zip file you download, and include the autoloader in your project: From d6c7563bdf88d6a0719ea63e21c74dc86032364e Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Mon, 19 Aug 2019 14:09:46 -0400 Subject: [PATCH 107/301] Prepare v2.2.4 (#1698) --- src/Google/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index 02d7ac873..218aae5d2 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -38,7 +38,7 @@ */ class Google_Client { - const LIBVER = "2.2.3"; + const LIBVER = "2.2.4"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; From e415916fbcba93910f737eb617dec0399afd9e1c Mon Sep 17 00:00:00 2001 From: Pierre Grimaud Date: Tue, 3 Sep 2019 16:38:35 +0200 Subject: [PATCH 108/301] Fix indefinite article in README.md (#1702) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 36f04c81f..c0267f7ec 100644 --- a/README.md +++ b/README.md @@ -351,7 +351,7 @@ Please see the [contributing](.github/CONTRIBUTING.md) page for more information For support with the library the best place to ask is via the google-api-php-client tag on StackOverflow: https://stackoverflow.com/questions/tagged/google-api-php-client -If there is a specific bug with the library, please [file a issue](https://github.com/googleapis/google-api-php-client/issues) in the GitHub issues tracker, including an example of the failing code and any specific errors retrieved. Feature requests can also be filed, as long as they are core library requests, and not-API specific: for those, refer to the documentation for the individual APIs for the best place to file requests. Please try to provide a clear statement of the problem that the feature would address. +If there is a specific bug with the library, please [file an issue](https://github.com/googleapis/google-api-php-client/issues) in the GitHub issues tracker, including an example of the failing code and any specific errors retrieved. Feature requests can also be filed, as long as they are core library requests, and not-API specific: for those, refer to the documentation for the individual APIs for the best place to file requests. Please try to provide a clear statement of the problem that the feature would address. ### I want an example of X! ### From 4b7c6abf154c84f885069c8f8475d11de83eb1e1 Mon Sep 17 00:00:00 2001 From: Stefan Giehl Date: Thu, 5 Sep 2019 14:22:28 +0200 Subject: [PATCH 109/301] chore: Adds phpcs.xml.dist as export-ignore (#1703) --- .gitattributes | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitattributes b/.gitattributes index 3ed46ef83..68a0191a5 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4,6 +4,7 @@ /.travis.yml export-ignore /examples export-ignore /phpunit.xml.dist export-ignore +/phpcs.xml.dist export-ignore /style export-ignore /tests export-ignore /UPGRADING.md export-ignore From 76436eb1dcb8a0c3ffe51080640fb8707af0361e Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Thu, 5 Sep 2019 16:25:11 -0400 Subject: [PATCH 110/301] feat: Support Monolog v2 (#1705) --- composer.json | 2 +- tests/Google/Service/ResourceTest.php | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index ce1e12e73..d69dca5a8 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ "google/auth": "^1.0", "google/apiclient-services": "~0.13", "firebase/php-jwt": "~2.0||~3.0||~4.0||~5.0", - "monolog/monolog": "^1.17", + "monolog/monolog": "^1.17|^2.0", "phpseclib/phpseclib": "~0.3.10||~2.0", "guzzlehttp/guzzle": "~5.3.1||~6.0", "guzzlehttp/psr7": "^1.2" diff --git a/tests/Google/Service/ResourceTest.php b/tests/Google/Service/ResourceTest.php index 019cb553d..9401bd6c9 100644 --- a/tests/Google/Service/ResourceTest.php +++ b/tests/Google/Service/ResourceTest.php @@ -60,9 +60,8 @@ public function setUp() $this->client = $this->getMockBuilder("Google_Client") ->disableOriginalConstructor() ->getMock(); - $this->logger = $this->getMockBuilder("Monolog\Logger") - ->disableOriginalConstructor() - ->getMock(); + $logger = $this->prophesize("Monolog\Logger"); + $this->logger = $logger->reveal(); $this->client->expects($this->any()) ->method("getLogger") ->will($this->returnValue($this->logger)); @@ -140,11 +139,11 @@ public function testCallServiceDefinedRoot() $this->assertEquals("https://sample.example.com/method/path", (string) $request->getUri()); $this->assertEquals("POST", $request->getMethod()); } - + /** - * Some Google Service (Google_Service_Directory_Resource_Channels and - * Google_Service_Reports_Resource_Channels) use a different servicePath value - * that should override the default servicePath value, it's represented by a / + * Some Google Service (Google_Service_Directory_Resource_Channels and + * Google_Service_Reports_Resource_Channels) use a different servicePath value + * that should override the default servicePath value, it's represented by a / * before the resource path. All other Services have no / before the path */ public function testCreateRequestUriForASelfDefinedServicePath() @@ -167,7 +166,7 @@ public function testCreateRequestUriForASelfDefinedServicePath() $request = $resource->call('testMethod', array(array())); $this->assertEquals('https://test.example.com/admin/directory_v1/watch/stop', (string) $request->getUri()); } - + public function testCreateRequestUri() { $restPath = "plus/{u}"; From 3215f78329910fd410456f71cf89ea32383d4f6d Mon Sep 17 00:00:00 2001 From: leo108 Date: Mon, 9 Sep 2019 23:14:40 +0800 Subject: [PATCH 111/301] docs: Fix annotation (#1706) --- src/Google/Http/MediaFileUpload.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Google/Http/MediaFileUpload.php b/src/Google/Http/MediaFileUpload.php index 600e6789e..f4ee97683 100644 --- a/src/Google/Http/MediaFileUpload.php +++ b/src/Google/Http/MediaFileUpload.php @@ -67,9 +67,11 @@ class Google_Http_MediaFileUpload private $httpResultCode; /** - * @param $mimeType string - * @param $data string The bytes you want to upload. - * @param $resumable bool + * @param Google_Client $client + * @param RequestInterface $request + * @param string $mimeType + * @param string $data The bytes you want to upload. + * @param bool $resumable * @param bool $chunkSize File will be uploaded in chunks of this many bytes. * only used if resumable=True */ From ea5c94d975c150b6eb892abb5536d24205376d4e Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Mon, 9 Sep 2019 12:04:18 -0400 Subject: [PATCH 112/301] Prepare v2.3.0 (#1707) --- src/Google/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index 218aae5d2..eb79a48cc 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -38,7 +38,7 @@ */ class Google_Client { - const LIBVER = "2.2.4"; + const LIBVER = "2.3.0"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; From e9e6d9033c49265c64a649be11f21e7f555b317b Mon Sep 17 00:00:00 2001 From: David Supplee Date: Tue, 10 Sep 2019 17:12:49 -0700 Subject: [PATCH 113/301] feat: Add x-goog-api-client header to requests (#1709) --- src/Google/Client.php | 24 ++++++++++++++++------ tests/Google/ClientTest.php | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index eb79a48cc..d94698394 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -804,12 +804,24 @@ public function prepareScopes() */ public function execute(RequestInterface $request, $expectedClass = null) { - $request = $request->withHeader( - 'User-Agent', - $this->config['application_name'] - . " " . self::USER_AGENT_SUFFIX - . $this->getLibraryVersion() - ); + $request = $request + ->withHeader( + 'User-Agent', + sprintf( + '%s %s%s', + $this->config['application_name'], + self::USER_AGENT_SUFFIX, + $this->getLibraryVersion() + ) + ) + ->withHeader( + 'x-goog-api-client', + sprintf( + 'gl-php/%s gdcl/%s', + phpversion(), + $this->getLibraryVersion() + ) + ); if ($this->config['api_format_v2']) { $request = $request->withHeader( diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 4152871a1..3b118acf1 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -707,4 +707,45 @@ public function testExecuteWithFormat() $request = new Request('POST', 'http://foo.bar/'); $client->execute($request); } + + public function testExecuteSetsCorrectHeaders() + { + $this->onlyGuzzle6(); + + $client = new Google_Client(); + $guzzle = $this->getMock('GuzzleHttp\Client'); + $guzzle->expects($this->once()) + ->method('send') + ->with( + $this->callback( + function (RequestInterface $request) { + $userAgent = sprintf( + '%s%s', + Google_Client::USER_AGENT_SUFFIX, + Google_Client::LIBVER + ); + $xGoogApiClient = sprintf( + 'gl-php/%s gdcl/%s', + phpversion(), + Google_Client::LIBVER + ); + + if ($request->getHeaderLine('User-Agent') !== $userAgent) { + return false; + } + + if ($request->getHeaderLine('x-goog-api-client') !== $xGoogApiClient) { + return false; + } + + return true; + } + ) + )->will($this->returnValue(new Response(200, [], null))); + + $client->setHttpClient($guzzle); + + $request = new Request('POST', 'http://foo.bar/'); + $client->execute($request); + } } From cd3c37998020d91ae4eafca4f26a92da4dabba83 Mon Sep 17 00:00:00 2001 From: David Supplee Date: Wed, 11 Sep 2019 10:38:10 -0700 Subject: [PATCH 114/301] Prepare v2.4.0 (#1711) --- src/Google/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index d94698394..5785ac1f5 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -38,7 +38,7 @@ */ class Google_Client { - const LIBVER = "2.3.0"; + const LIBVER = "2.4.0"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; From 7478bc811dde1fd2b4c33403647e945a218f502a Mon Sep 17 00:00:00 2001 From: Gert de Pagter Date: Mon, 4 Nov 2019 19:27:32 +0100 Subject: [PATCH 115/301] chore: remove sudo:false (#1732) This has been deprecated by travis and doesnt do anything anymore --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 80e158254..941ded4b2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,8 +10,6 @@ env: - MEMCACHE_PORT=11211 - COMPOSER_CMD="composer install" -sudo: false - cache: directories: - $HOME/.composer/cache From cc98cfb202fdcbe36fdf7c7ba28d94df9657bb40 Mon Sep 17 00:00:00 2001 From: Ajaz Ur Rehman Date: Fri, 8 Nov 2019 00:12:16 +0530 Subject: [PATCH 116/301] chore: fix documentation typo (#1741) fixed spelling of autoloader --- docs/install.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/install.md b/docs/install.md index 84b9208d5..36bb6591b 100644 --- a/docs/install.md +++ b/docs/install.md @@ -26,7 +26,7 @@ Follow [the instructions in the README](https://github.com/google/google-api-php ### What to do with the files -After obtaining the files, include the autloader. If you used Composer, your require statement will look like this: +After obtaining the files, include the autoloader. If you used Composer, your require statement will look like this: ```php require_once '/path/to/your-project/vendor/autoload.php'; @@ -36,4 +36,4 @@ If you downloaded the package separately, your require statement will look like ```php require_once '/path/to/google-api-php-client/vendor/autoload.php'; -``` \ No newline at end of file +``` From 27ed75d7234a04e18b19138ea1e07b8efc5f9bbd Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Wed, 4 Dec 2019 10:27:44 -0600 Subject: [PATCH 117/301] docs: remove shutdown G+ API reference (#1751) * docs: remove shutdown G+ API reference * Update README.md Co-Authored-By: John Pedrie --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c0267f7ec..faa17a03b 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ # Google APIs Client Library for PHP # -The Google API Client Library enables you to work with Google APIs such as Google+, Drive, or YouTube on your server. +The Google API Client Library enables you to work with Google APIs such as Gmail, Drive or YouTube on your server. These client libraries are officially supported by Google. However, the libraries are considered complete and are in maintenance mode. This means that we will address critical bugs and security issues but will not add any new features. From aebe1d1f3df510969fca0269a29d83ed81a65d10 Mon Sep 17 00:00:00 2001 From: Miguel Barrero Date: Mon, 16 Dec 2019 13:52:04 -0400 Subject: [PATCH 118/301] docs: remove duplicate lines from oauth-server.md (#1761) removing duplicate text --- docs/oauth-server.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/docs/oauth-server.md b/docs/oauth-server.md index cba985435..906fd082f 100644 --- a/docs/oauth-server.md +++ b/docs/oauth-server.md @@ -28,10 +28,6 @@ If your application runs on Google App Engine, a service account is set up autom If your application doesn't run on Google App Engine or Google Compute Engine, you must obtain these credentials in the Google Developers Console. To generate service-account credentials, or to view the public credentials that you've already generated, do the following: -If your application runs on Google App Engine, a service account is set up automatically when you create your project. - -If your application doesn't run on Google App Engine or Google Compute Engine, you must obtain these credentials in the Google Developers Console. To generate service-account credentials, or to view the public credentials that you've already generated, do the following: - 1. Open the [**Service accounts** section](https://console.developers.google.com/permissions/serviceaccounts?project=_) of the Developers Console's **Permissions** page. 2. Click **Create service account**. 3. In the **Create service account** window, type a name for the service account and select **Furnish a new private key**. If you want to [grant G Suite domain-wide authority](https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority) to the service account, also select **Enable G Suite Domain-wide Delegation**. Then, click **Create**. From 2a652bfaab250ea7b6dfae46a917f2a6d7997f6f Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Wed, 18 Dec 2019 17:28:25 -0500 Subject: [PATCH 119/301] tests: Upgrade unit tests for PHP 7.4 compat (#1710) * fix: Upgrade unit tests for PHP 7.4 compat * switch from 7.4 snapshot to 7.4 stable --- .travis.yml | 8 +- composer.json | 2 +- tests/BaseTest.php | 14 ++ tests/Google/AccessToken/RevokeTest.php | 255 +++++++++++------------ tests/Google/AccessToken/VerifyTest.php | 2 - tests/Google/ClientTest.php | 257 +++++++++++++----------- tests/Google/Service/ResourceTest.php | 156 +++++++------- tests/Google/ServiceTest.php | 22 +- tests/Google/Task/RunnerTest.php | 34 ++-- 9 files changed, 393 insertions(+), 357 deletions(-) diff --git a/.travis.yml b/.travis.yml index 941ded4b2..0f09f4137 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,7 +22,7 @@ php: - 7.1 - 7.2 - 7.3 - - 7.4snapshot + - 7.4 # Test lowest dependencies on PHP 5.4 # (Guzzle 5.2, phpseclib 0.3) @@ -30,8 +30,6 @@ matrix: include: - php: 5.4 env: COMPOSER_CMD="composer update phpseclib/phpseclib guzzlehttp/guzzle guzzlehttp/psr7 --prefer-lowest" RUN_PHP_CS=true - allow_failures: - - php: 7.4snapshot before_install: - composer self-update @@ -40,10 +38,6 @@ install: - if [[ "$TRAVIS_PHP_VERSION" == "5.4" ]]; then composer remove --dev cache/filesystem-adapter; fi - $(echo $COMPOSER_CMD) -before_script: - - phpenv version-name | grep ^5.[34] && echo "extension=apc.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini ; true - - phpenv version-name | grep ^5.[34] && echo "apc.enable_cli=1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini ; true - script: - vendor/bin/phpunit - if [[ "$RUN_PHP_CS" == "true" ]]; then vendor/bin/phpcs src -np; fi diff --git a/composer.json b/composer.json index d69dca5a8..19f0e2f8d 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "guzzlehttp/psr7": "^1.2" }, "require-dev": { - "phpunit/phpunit": "~4.8.36", + "phpunit/phpunit": "^4.8|^5.0", "squizlabs/php_codesniffer": "~2.3", "symfony/dom-crawler": "~2.1", "symfony/css-selector": "~2.1", diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 92328283e..4c0afec55 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -242,4 +242,18 @@ public function onlyGuzzle5() $this->markTestSkipped('Guzzle 5 only'); } } + + protected function getGuzzle5ResponseMock() + { + $response = $this->prophesize('GuzzleHttp\Message\ResponseInterface'); + $response->getStatusCode() + ->willReturn(200); + + $response->getHeaders()->willReturn([]); + $response->getBody()->willReturn(''); + $response->getProtocolVersion()->willReturn(''); + $response->getReasonPhrase()->willReturn(''); + + return $response; + } } diff --git a/tests/Google/AccessToken/RevokeTest.php b/tests/Google/AccessToken/RevokeTest.php index 099b9bcd7..26461da38 100644 --- a/tests/Google/AccessToken/RevokeTest.php +++ b/tests/Google/AccessToken/RevokeTest.php @@ -1,6 +1,6 @@ onlyGuzzle5(); - - $accessToken = 'ACCESS_TOKEN'; - $refreshToken = 'REFRESH_TOKEN'; - $token = ''; - - $response = $this->getMock('GuzzleHttp\Message\ResponseInterface'); - $response->expects($this->exactly(3)) - ->method('getStatusCode') - ->will($this->returnValue(200)); - $http = $this->getMock('GuzzleHttp\ClientInterface'); - $http->expects($this->exactly(3)) - ->method('send') - ->will($this->returnCallback( - function ($request) use (&$token, $response) { - parse_str((string) $request->getBody(), $fields); - $token = isset($fields['token']) ? $fields['token'] : null; - - return $response; - } - )); - - $requestToken = null; - $request = $this->getMock('GuzzleHttp\Message\RequestInterface'); - $request->expects($this->exactly(3)) - ->method('getBody') - ->will($this->returnCallback( - function () use (&$requestToken) { - return 'token='.$requestToken; - })); - $http->expects($this->exactly(3)) - ->method('createRequest') - ->will($this->returnCallback( - function ($method, $url, $params) use (&$requestToken, $request) { - parse_str((string) $params['body'], $fields); - $requestToken = isset($fields['token']) ? $fields['token'] : null; - - return $request; - } - )); - - $t = array( - 'access_token' => $accessToken, - 'created' => time(), - 'expires_in' => '3600' - ); - - // Test with access token. - $revoke = new Google_AccessToken_Revoke($http); - $this->assertTrue($revoke->revokeToken($t)); - $this->assertEquals($accessToken, $token); - - // Test with refresh token. - $revoke = new Google_AccessToken_Revoke($http); - $t = array( - 'access_token' => $accessToken, - 'refresh_token' => $refreshToken, - 'created' => time(), - 'expires_in' => '3600' - ); - $this->assertTrue($revoke->revokeToken($t)); - $this->assertEquals($refreshToken, $token); - - // Test with token string. - $revoke = new Google_AccessToken_Revoke($http); - $t = $accessToken; - $this->assertTrue($revoke->revokeToken($t)); - $this->assertEquals($accessToken, $token); - } - - public function testRevokeAccessGuzzle6() - { - $this->onlyGuzzle6(); - - $accessToken = 'ACCESS_TOKEN'; - $refreshToken = 'REFRESH_TOKEN'; - $token = ''; - - $response = $this->getMock('Psr\Http\Message\ResponseInterface'); - $response->expects($this->exactly(3)) - ->method('getStatusCode') - ->will($this->returnValue(200)); - $http = $this->getMock('GuzzleHttp\ClientInterface'); - $http->expects($this->exactly(3)) - ->method('send') - ->will($this->returnCallback( - function ($request) use (&$token, $response) { - parse_str((string) $request->getBody(), $fields); - $token = isset($fields['token']) ? $fields['token'] : null; - - return $response; - } - )); - - $t = array( - 'access_token' => $accessToken, - 'created' => time(), - 'expires_in' => '3600' - ); - - // Test with access token. - $revoke = new Google_AccessToken_Revoke($http); - $this->assertTrue($revoke->revokeToken($t)); - $this->assertEquals($accessToken, $token); - - // Test with refresh token. - $revoke = new Google_AccessToken_Revoke($http); - $t = array( - 'access_token' => $accessToken, - 'refresh_token' => $refreshToken, - 'created' => time(), - 'expires_in' => '3600' - ); - $this->assertTrue($revoke->revokeToken($t)); - $this->assertEquals($refreshToken, $token); - - // Test with token string. - $revoke = new Google_AccessToken_Revoke($http); - $t = $accessToken; - $this->assertTrue($revoke->revokeToken($t)); - $this->assertEquals($accessToken, $token); - } + public function testRevokeAccessGuzzle5() + { + $this->onlyGuzzle5(); + + $accessToken = 'ACCESS_TOKEN'; + $refreshToken = 'REFRESH_TOKEN'; + $token = ''; + + $response = $this->prophesize('GuzzleHttp\Message\ResponseInterface'); + $response->getStatusCode() + ->shouldBeCalledTimes(3) + ->willReturn(200); + + $response->getHeaders()->willReturn([]); + $response->getBody()->willReturn(''); + $response->getProtocolVersion()->willReturn(''); + $response->getReasonPhrase()->willReturn(''); + + $http = $this->prophesize('GuzzleHttp\ClientInterface'); + $http->send(Argument::type('GuzzleHttp\Message\RequestInterface')) + ->shouldBeCalledTimes(3) + ->will(function ($args) use (&$token, $response) { + $request = $args[0]; + parse_str((string) $request->getBody(), $fields); + $token = isset($fields['token']) ? $fields['token'] : null; + + return $response->reveal(); + }); + + $requestToken = null; + $request = $this->prophesize('GuzzleHttp\Message\RequestInterface'); + $request->getBody() + ->shouldBeCalledTimes(3) + ->will(function () use (&$requestToken) { + return 'token='.$requestToken; + }); + + $http->createRequest(Argument::any(), Argument::any(), Argument::any()) + ->shouldBeCalledTimes(3) + ->will(function ($args) use (&$requestToken, $request) { + $params = $args[2]; + parse_str((string) $params['body'], $fields); + $requestToken = isset($fields['token']) ? $fields['token'] : null; + + return $request; + }); + + $t = [ + 'access_token' => $accessToken, + 'created' => time(), + 'expires_in' => '3600' + ]; + + // Test with access token. + $revoke = new Google_AccessToken_Revoke($http->reveal()); + $this->assertTrue($revoke->revokeToken($t)); + $this->assertEquals($accessToken, $token); + + // Test with refresh token. + $revoke = new Google_AccessToken_Revoke($http->reveal()); + $t = [ + 'access_token' => $accessToken, + 'refresh_token' => $refreshToken, + 'created' => time(), + 'expires_in' => '3600' + ]; + + $this->assertTrue($revoke->revokeToken($t)); + $this->assertEquals($refreshToken, $token); + + // Test with token string. + $revoke = new Google_AccessToken_Revoke($http->reveal()); + $t = $accessToken; + $this->assertTrue($revoke->revokeToken($t)); + $this->assertEquals($accessToken, $token); + } + + public function testRevokeAccessGuzzle6() + { + $this->onlyGuzzle6(); + + $accessToken = 'ACCESS_TOKEN'; + $refreshToken = 'REFRESH_TOKEN'; + $token = ''; + + $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); + $response->getStatusCode() + ->shouldBeCalledTimes(3) + ->willReturn(200); + + $http = $this->prophesize('GuzzleHttp\ClientInterface'); + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(3) + ->will(function ($args) use (&$token, $response) { + parse_str((string) $args[0]->getBody(), $fields); + $token = isset($fields['token']) ? $fields['token'] : null; + + return $response->reveal(); + }); + + $t = [ + 'access_token' => $accessToken, + 'created' => time(), + 'expires_in' => '3600' + ]; + + // Test with access token. + $revoke = new Google_AccessToken_Revoke($http->reveal()); + $this->assertTrue($revoke->revokeToken($t)); + $this->assertEquals($accessToken, $token); + + // Test with refresh token. + $revoke = new Google_AccessToken_Revoke($http->reveal()); + $t = [ + 'access_token' => $accessToken, + 'refresh_token' => $refreshToken, + 'created' => time(), + 'expires_in' => '3600' + ]; + + $this->assertTrue($revoke->revokeToken($t)); + $this->assertEquals($refreshToken, $token); + + // Test with token string. + $revoke = new Google_AccessToken_Revoke($http->reveal()); + $t = $accessToken; + $this->assertTrue($revoke->revokeToken($t)); + $this->assertEquals($accessToken, $token); + } } diff --git a/tests/Google/AccessToken/VerifyTest.php b/tests/Google/AccessToken/VerifyTest.php index e72c14897..bce9501b4 100644 --- a/tests/Google/AccessToken/VerifyTest.php +++ b/tests/Google/AccessToken/VerifyTest.php @@ -1,7 +1,5 @@ createAuthUrl() ); - $response = $this->getMock('Psr\Http\Message\ResponseInterface'); - $response->expects($this->once()) - ->method('getBody') - ->will($this->returnValue($this->getMock('Psr\Http\Message\StreamInterface'))); - $http = $this->getMock('GuzzleHttp\ClientInterface'); - $http->expects($this->once()) - ->method('send') - ->will($this->returnValue($response)); + $stream = $this->prophesize('GuzzleHttp\Psr7\Stream'); + $stream->__toString()->willReturn(''); - if ($this->isGuzzle5()) { - $guzzle5Request = new GuzzleHttp\Message\Request('POST', '/'); - $http->expects($this->once()) - ->method('createRequest') - ->will($this->returnValue($guzzle5Request)); - } + $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); + $response->getBody() + ->shouldBeCalledTimes(1) + ->willReturn($stream->reveal()); + + $response->getStatusCode()->willReturn(200); + + $http = $this->prophesize('GuzzleHttp\ClientInterface'); + + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response->reveal()); - $client->setHttpClient($http); + $client->setHttpClient($http->reveal()); $dr_service = new Google_Service_Drive($client); $this->assertInstanceOf('Google_Model', $dr_service->files->listFiles()); } @@ -284,8 +284,10 @@ public function testSettersGetters() $client->setRedirectUri('localhost'); $client->setConfig('application_name', 'me'); - $client->setCache($this->getMock('Psr\Cache\CacheItemPoolInterface')); - $this->assertEquals('object', gettype($client->getCache())); + + $cache = $this->prophesize('Psr\Cache\CacheItemPoolInterface'); + $client->setCache($cache->reveal()); + $this->assertInstanceOf('Psr\Cache\CacheItemPoolInterface', $client->getCache()); try { $client->setAccessToken(null); @@ -437,39 +439,49 @@ public function testApplicationDefaultCredentialsWithSubject() */ public function testRefreshTokenSetsValues() { - $token = json_encode(array( + $token = json_encode([ 'access_token' => 'xyz', 'id_token' => 'ID_TOKEN', - )); - $postBody = $this->getMock('Psr\Http\Message\StreamInterface'); - $postBody->expects($this->once()) - ->method('__toString') - ->will($this->returnValue($token)); + ]); + $postBody = $this->prophesize('GuzzleHttp\Psr7\Stream'); + $postBody->__toString() + ->shouldBeCalledTimes(1) + ->willReturn($token); + if ($this->isGuzzle5()) { - $response = $this->getMock('GuzzleHttp\Message\ResponseInterface'); - $response->expects($this->once()) - ->method('getStatusCode') - ->will($this->returnValue(200)); + $response = $this->getGuzzle5ResponseMock(); + $response->getStatusCode() + ->shouldBeCalledTimes(1) + ->willReturn(200); } else { - $response = $this->getMock('Psr\Http\Message\ResponseInterface'); + $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); } - $response->expects($this->once()) - ->method('getBody') - ->will($this->returnValue($postBody)); - $http = $this->getMock('GuzzleHttp\ClientInterface'); - $http->expects($this->once()) - ->method('send') - ->will($this->returnValue($response)); + + $response->getBody() + ->shouldBeCalledTimes(1) + ->willReturn($postBody->reveal()); + + $response->hasHeader('Content-Type')->willReturn(false); + + $http = $this->prophesize('GuzzleHttp\ClientInterface'); if ($this->isGuzzle5()) { $guzzle5Request = new GuzzleHttp\Message\Request('POST', '/', ['body' => $token]); - $http->expects($this->once()) - ->method('createRequest') - ->will($this->returnValue($guzzle5Request)); + $http->createRequest(Argument::any(), Argument::any(), Argument::any()) + ->shouldBeCalledTimes(1) + ->willReturn($guzzle5Request); + + $http->send(Argument::type('GuzzleHttp\Message\Request')) + ->shouldBeCalledTimes(1) + ->willReturn($response->reveal()); + } else { + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response->reveal()); } $client = $this->getClient(); - $client->setHttpClient($http); + $client->setHttpClient($http->reveal()); $client->fetchAccessTokenWithRefreshToken("REFRESH_TOKEN"); $token = $client->getAccessToken(); $this->assertEquals("ID_TOKEN", $token['id_token']); @@ -485,35 +497,44 @@ public function testRefreshTokenIsSetOnRefresh() 'access_token' => 'xyz', 'id_token' => 'ID_TOKEN', )); - $postBody = $this->getMock('Psr\Http\Message\StreamInterface'); - $postBody->expects($this->once()) - ->method('__toString') - ->will($this->returnValue($token)); + $postBody = $this->prophesize('Psr\Http\Message\StreamInterface'); + $postBody->__toString() + ->shouldBeCalledTimes(1) + ->willReturn($token); + if ($this->isGuzzle5()) { - $response = $this->getMock('GuzzleHttp\Message\ResponseInterface'); - $response->expects($this->once()) - ->method('getStatusCode') - ->will($this->returnValue(200)); + $response = $this->getGuzzle5ResponseMock(); + $response->getStatusCode() + ->shouldBeCalledTimes(1) + ->willReturn(200); } else { - $response = $this->getMock('Psr\Http\Message\ResponseInterface'); + $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); } - $response->expects($this->once()) - ->method('getBody') - ->will($this->returnValue($postBody)); - $http = $this->getMock('GuzzleHttp\ClientInterface'); - $http->expects($this->once()) - ->method('send') - ->will($this->returnValue($response)); + + $response->getBody() + ->shouldBeCalledTimes(1) + ->willReturn($postBody->reveal()); + + $response->hasHeader('Content-Type')->willReturn(false); + + $http = $this->prophesize('GuzzleHttp\ClientInterface'); if ($this->isGuzzle5()) { $guzzle5Request = new GuzzleHttp\Message\Request('POST', '/', ['body' => $token]); - $http->expects($this->once()) - ->method('createRequest') - ->will($this->returnValue($guzzle5Request)); + $http->createRequest(Argument::any(), Argument::any(), Argument::any()) + ->willReturn($guzzle5Request); + + $http->send(Argument::type('GuzzleHttp\Message\Request')) + ->shouldBeCalledTimes(1) + ->willReturn($response->reveal()); + } else { + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response->reveal()); } $client = $this->getClient(); - $client->setHttpClient($http); + $client->setHttpClient($http->reveal()); $client->fetchAccessTokenWithRefreshToken($refreshToken); $token = $client->getAccessToken(); $this->assertEquals($refreshToken, $token['refresh_token']); @@ -530,35 +551,41 @@ public function testRefreshTokenIsNotSetWhenNewRefreshTokenIsReturned() 'id_token' => 'ID_TOKEN', 'refresh_token' => 'NEW_REFRESH_TOKEN' )); - $postBody = $this->getMock('Psr\Http\Message\StreamInterface'); - $postBody->expects($this->once()) - ->method('__toString') - ->will($this->returnValue($token)); + + $postBody = $this->prophesize('GuzzleHttp\Psr7\Stream'); + $postBody->__toString() + ->wilLReturn($token); + if ($this->isGuzzle5()) { - $response = $this->getMock('GuzzleHttp\Message\ResponseInterface'); - $response->expects($this->once()) - ->method('getStatusCode') - ->will($this->returnValue(200)); + $response = $this->getGuzzle5ResponseMock(); + $response->getStatusCode() + ->willReturn(200); } else { - $response = $this->getMock('Psr\Http\Message\ResponseInterface'); + $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); } - $response->expects($this->once()) - ->method('getBody') - ->will($this->returnValue($postBody)); - $http = $this->getMock('GuzzleHttp\ClientInterface'); - $http->expects($this->once()) - ->method('send') - ->will($this->returnValue($response)); + + $response->getBody() + ->willReturn($postBody->reveal()); + + $response->hasHeader('Content-Type')->willReturn(false); + + $http = $this->prophesize('GuzzleHttp\ClientInterface'); if ($this->isGuzzle5()) { $guzzle5Request = new GuzzleHttp\Message\Request('POST', '/', ['body' => $token]); - $http->expects($this->once()) - ->method('createRequest') - ->will($this->returnValue($guzzle5Request)); + $http->createRequest(Argument::any(), Argument::any(), Argument::any()) + ->willReturn($guzzle5Request); + + $http->send(Argument::type('GuzzleHttp\Message\Request')) + ->willReturn($response->reveal()); + } else { + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response->reveal()); } $client = $this->getClient(); - $client->setHttpClient($http); + $client->setHttpClient($http->reveal()); $client->fetchAccessTokenWithRefreshToken($refreshToken); $token = $client->getAccessToken(); $this->assertEquals('NEW_REFRESH_TOKEN', $token['refresh_token']); @@ -695,14 +722,15 @@ public function testExecuteWithFormat() 'api_format_v2' => true ]); - $guzzle = $this->getMock('GuzzleHttp\Client'); - $guzzle->expects($this->once()) - ->method('send') - ->with($this->callback(function (RequestInterface $request) { - return $request->getHeaderLine('X-GOOG-API-FORMAT-VERSION') === '2'; - }))->will($this->returnValue(new Response(200, [], null))); + $guzzle = $this->prophesize('GuzzleHttp\Client'); + $guzzle->send(Argument::allOf( + Argument::type('Psr\Http\Message\RequestInterface'), + Argument::that(function (RequestInterface $request) { + return $request->getHeaderLine('X-GOOG-API-FORMAT-VERSION') === '2'; + }) + ), [])->willReturn(new Response(200, [], null)); - $client->setHttpClient($guzzle); + $client->setHttpClient($guzzle->reveal()); $request = new Request('POST', 'http://foo.bar/'); $client->execute($request); @@ -713,37 +741,32 @@ public function testExecuteSetsCorrectHeaders() $this->onlyGuzzle6(); $client = new Google_Client(); - $guzzle = $this->getMock('GuzzleHttp\Client'); - $guzzle->expects($this->once()) - ->method('send') - ->with( - $this->callback( - function (RequestInterface $request) { - $userAgent = sprintf( - '%s%s', - Google_Client::USER_AGENT_SUFFIX, - Google_Client::LIBVER - ); - $xGoogApiClient = sprintf( - 'gl-php/%s gdcl/%s', - phpversion(), - Google_Client::LIBVER - ); - - if ($request->getHeaderLine('User-Agent') !== $userAgent) { - return false; - } - - if ($request->getHeaderLine('x-goog-api-client') !== $xGoogApiClient) { - return false; - } - - return true; - } - ) - )->will($this->returnValue(new Response(200, [], null))); - - $client->setHttpClient($guzzle); + + $guzzle = $this->prophesize('GuzzleHttp\Client'); + $guzzle->send(Argument::that(function (RequestInterface $request) { + $userAgent = sprintf( + '%s%s', + Google_Client::USER_AGENT_SUFFIX, + Google_Client::LIBVER + ); + $xGoogApiClient = sprintf( + 'gl-php/%s gdcl/%s', + phpversion(), + Google_Client::LIBVER + ); + + if ($request->getHeaderLine('User-Agent') !== $userAgent) { + return false; + } + + if ($request->getHeaderLine('x-goog-api-client') !== $xGoogApiClient) { + return false; + } + + return true; + }), [])->shouldBeCalledTimes(1)->willReturn(new Response(200, [], null)); + + $client->setHttpClient($guzzle->reveal()); $request = new Request('POST', 'http://foo.bar/'); $client->execute($request); diff --git a/tests/Google/Service/ResourceTest.php b/tests/Google/Service/ResourceTest.php index 9401bd6c9..df08569df 100644 --- a/tests/Google/Service/ResourceTest.php +++ b/tests/Google/Service/ResourceTest.php @@ -18,12 +18,13 @@ * under the License. */ +use GuzzleHttp\Message\Response as Guzzle5Response; use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Stream; -use GuzzleHttp\Message\Response as Guzzle5Response; use GuzzleHttp\Stream\Stream as Guzzle5Stream; +use Prophecy\Argument; class Test_Google_Service extends Google_Service { @@ -53,27 +54,24 @@ class Google_Service_ResourceTest extends BaseTest { private $client; private $service; - private $logger; public function setUp() { - $this->client = $this->getMockBuilder("Google_Client") - ->disableOriginalConstructor() - ->getMock(); + $this->client = $this->prophesize("Google_Client"); + $logger = $this->prophesize("Monolog\Logger"); - $this->logger = $logger->reveal(); - $this->client->expects($this->any()) - ->method("getLogger") - ->will($this->returnValue($this->logger)); - $this->client->expects($this->any()) - ->method("shouldDefer") - ->will($this->returnValue(true)); - $this->client->expects($this->any()) - ->method("getHttpClient") - ->will($this->returnValue(new GuzzleHttp\Client())); - $this->service = new Test_Google_Service($this->client); + + $this->client->getLogger()->willReturn($logger->reveal()); + $this->client->shouldDefer()->willReturn(true); + $this->client->getHttpClient()->willReturn(new GuzzleHttp\Client()); + + $this->service = new Test_Google_Service($this->client->reveal()); } + /** + * @expectedException Google_Exception + * @expectedExceptionMessage Unknown function: test->testResource->someothermethod() + */ public function testCallFailure() { $resource = new Google_Service_Resource( @@ -90,10 +88,6 @@ public function testCallFailure() ) ) ); - $this->setExpectedException( - "Google_Exception", - "Unknown function: test->testResource->someothermethod()" - ); $resource->call("someothermethod", array()); } @@ -170,7 +164,7 @@ public function testCreateRequestUriForASelfDefinedServicePath() public function testCreateRequestUri() { $restPath = "plus/{u}"; - $service = new Google_Service($this->client); + $service = new Google_Service($this->client->reveal()); $service->servicePath = "http://localhost/"; $resource = new Google_Service_Resource($service, 'test', 'testResource', array()); @@ -216,29 +210,30 @@ public function testNoExpectedClassForAltMediaWithHttpSuccess() // set the "alt" parameter to "media" $arguments = [['alt' => 'media']]; $request = new Request('GET', '/?alt=media'); + + $http = $this->prophesize("GuzzleHttp\Client"); + if ($this->isGuzzle5()) { $body = Guzzle5Stream::factory('thisisnotvalidjson'); $response = new Guzzle5Response(200, [], $body); + + $http->createRequest(Argument::any(), Argument::any(), Argument::any()) + ->willReturn(new GuzzleHttp\Message\Request('GET', '/?alt=media')); + + $http->send(Argument::type('GuzzleHttp\Message\Request')) + ->shouldBeCalledTimes(1) + ->willReturn($response); } else { $body = Psr7\stream_for('thisisnotvalidjson'); $response = new Response(200, [], $body); - } - $http = $this->getMockBuilder("GuzzleHttp\Client") - ->disableOriginalConstructor() - ->getMock(); - $http->expects($this->once()) - ->method('send') - ->will($this->returnValue($response)); - - if ($this->isGuzzle5()) { - $http->expects($this->once()) - ->method('createRequest') - ->will($this->returnValue(new GuzzleHttp\Message\Request('GET', '/?alt=media'))); + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response); } $client = new Google_Client(); - $client->setHttpClient($http); + $client->setHttpClient($http->reveal()); $service = new Test_Google_Service($client); // set up mock objects @@ -268,29 +263,30 @@ public function testNoExpectedClassForAltMediaWithHttpFail() // set the "alt" parameter to "media" $arguments = [['alt' => 'media']]; $request = new Request('GET', '/?alt=media'); + + $http = $this->prophesize("GuzzleHttp\Client"); + if ($this->isGuzzle5()) { $body = Guzzle5Stream::factory('thisisnotvalidjson'); $response = new Guzzle5Response(400, [], $body); + + $http->createRequest(Argument::any(), Argument::any(), Argument::any()) + ->willReturn(new GuzzleHttp\Message\Request('GET', '/?alt=media')); + + $http->send(Argument::type('GuzzleHttp\Message\Request')) + ->shouldBeCalledTimes(1) + ->willReturn($response); } else { $body = Psr7\stream_for('thisisnotvalidjson'); $response = new Response(400, [], $body); - } - - $http = $this->getMockBuilder("GuzzleHttp\Client") - ->disableOriginalConstructor() - ->getMock(); - $http->expects($this->once()) - ->method('send') - ->will($this->returnValue($response)); - if ($this->isGuzzle5()) { - $http->expects($this->once()) - ->method('createRequest') - ->will($this->returnValue(new GuzzleHttp\Message\Request('GET', '/?alt=media'))); + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response); } $client = new Google_Client(); - $client->setHttpClient($http); + $client->setHttpClient($http->reveal()); $service = new Test_Google_Service($client); // set up mock objects @@ -324,29 +320,30 @@ public function testErrorResponseWithVeryLongBody() // set the "alt" parameter to "media" $arguments = [['alt' => 'media']]; $request = new Request('GET', '/?alt=media'); + + $http = $this->prophesize("GuzzleHttp\Client"); + if ($this->isGuzzle5()) { $body = Guzzle5Stream::factory('this will be pulled into memory'); $response = new Guzzle5Response(400, [], $body); + + $http->createRequest(Argument::any(), Argument::any(), Argument::any()) + ->willReturn(new GuzzleHttp\Message\Request('GET', '/?alt=media')); + + $http->send(Argument::type('GuzzleHttp\Message\Request')) + ->shouldBeCalledTimes(1) + ->willReturn($response); } else { $body = Psr7\stream_for('this will be pulled into memory'); $response = new Response(400, [], $body); - } - - $http = $this->getMockBuilder("GuzzleHttp\Client") - ->disableOriginalConstructor() - ->getMock(); - $http->expects($this->once()) - ->method('send') - ->will($this->returnValue($response)); - if ($this->isGuzzle5()) { - $http->expects($this->once()) - ->method('createRequest') - ->will($this->returnValue(new GuzzleHttp\Message\Request('GET', '/?alt=media'))); + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response); } $client = new Google_Client(); - $client->setHttpClient($http); + $client->setHttpClient($http->reveal()); $service = new Test_Google_Service($client); // set up mock objects @@ -386,15 +383,13 @@ public function testSuccessResponseWithVeryLongBody() $stream = new Test_MediaType_Stream($resource); $response = new Response(200, [], $stream); - $http = $this->getMockBuilder("GuzzleHttp\Client") - ->disableOriginalConstructor() - ->getMock(); - $http->expects($this->once()) - ->method('send') - ->will($this->returnValue($response)); + $http = $this->prophesize("GuzzleHttp\Client"); + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response); $client = new Google_Client(); - $client->setHttpClient($http); + $client->setHttpClient($http->reveal()); $service = new Test_Google_Service($client); // set up mock objects @@ -430,29 +425,30 @@ public function testExceptionMessage() 'errors' => $errors ] ]); + + $http = $this->prophesize("GuzzleHttp\Client"); + if ($this->isGuzzle5()) { $body = Guzzle5Stream::factory($content); $response = new Guzzle5Response(400, [], $body); + + $http->createRequest(Argument::any(), Argument::any(), Argument::any()) + ->willReturn(new GuzzleHttp\Message\Request('GET', '/?alt=media')); + + $http->send(Argument::type('GuzzleHttp\Message\Request')) + ->shouldBeCalledTimes(1) + ->willReturn($response); } else { $body = Psr7\stream_for($content); $response = new Response(400, [], $body); - } - $http = $this->getMockBuilder("GuzzleHttp\Client") - ->disableOriginalConstructor() - ->getMock(); - $http->expects($this->once()) - ->method('send') - ->will($this->returnValue($response)); - - if ($this->isGuzzle5()) { - $http->expects($this->once()) - ->method('createRequest') - ->will($this->returnValue(new GuzzleHttp\Message\Request('GET', '/?alt=media'))); + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response); } $client = new Google_Client(); - $client->setHttpClient($http); + $client->setHttpClient($http->reveal()); $service = new Test_Google_Service($client); // set up mock objects diff --git a/tests/Google/ServiceTest.php b/tests/Google/ServiceTest.php index ba5cc0831..ec89bb011 100644 --- a/tests/Google/ServiceTest.php +++ b/tests/Google/ServiceTest.php @@ -19,6 +19,7 @@ */ use PHPUnit\Framework\TestCase; +use Prophecy\Argument; class TestModel extends Google_Model { @@ -42,17 +43,20 @@ class Google_ServiceTest extends TestCase { public function testCreateBatch() { - $response = $this->getMock('Psr\Http\Message\ResponseInterface'); - $client = $this->getMock('Google_Client'); - $client - ->expects($this->once()) - ->method('execute') - ->with($this->callback(function ($request) { + $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); + $client = $this->prophesize('Google_Client'); + + $client->execute(Argument::allOf( + Argument::type('Psr\Http\Message\RequestInterface'), + Argument::that(function ($request) { $this->assertEquals('/batch/test', $request->getRequestTarget()); return $request; - })) - ->will($this->returnValue($response)); - $model = new TestService($client); + }) + ), Argument::any())->willReturn($response->reveal()); + + $client->getConfig('base_path')->willReturn(''); + + $model = new TestService($client->reveal()); $batch = $model->createBatch(); $this->assertInstanceOf('Google_Http_Batch', $batch); $batch->execute(); diff --git a/tests/Google/Task/RunnerTest.php b/tests/Google/Task/RunnerTest.php index 08f3dcefa..1dd93a0ec 100644 --- a/tests/Google/Task/RunnerTest.php +++ b/tests/Google/Task/RunnerTest.php @@ -15,11 +15,12 @@ * limitations under the License. */ +use GuzzleHttp\Message\Response as Guzzle5Response; use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; -use GuzzleHttp\Message\Response as Guzzle5Response; use GuzzleHttp\Stream\Stream as Guzzle5Stream; +use Prophecy\Argument; class Google_Task_RunnerTest extends BaseTest { @@ -628,18 +629,23 @@ private function setNextResponseThrows($message, $code) private function makeRequest() { $request = new Request('GET', '/test'); - $http = $this->getMock('GuzzleHttp\ClientInterface'); - $http->expects($this->exactly($this->mockedCallsCount)) - ->method('send') - ->will($this->returnCallback(array($this, 'getNextMockedCall'))); + $http = $this->prophesize('GuzzleHttp\ClientInterface'); if ($this->isGuzzle5()) { - $http->expects($this->exactly($this->mockedCallsCount)) - ->method('createRequest') - ->will($this->returnValue(new GuzzleHttp\Message\Request('GET', '/test'))); + $http->createRequest(Argument::any(), Argument::any(), Argument::any()) + ->shouldBeCalledTimes($this->mockedCallsCount) + ->willReturn(new GuzzleHttp\Message\Request('GET', '/test')); + + $http->send(Argument::type('GuzzleHttp\Message\Request')) + ->shouldBeCalledTimes($this->mockedCallsCount) + ->will([$this, 'getNextMockedCall']); + } else { + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes($this->mockedCallsCount) + ->will([$this, 'getNextMockedCall']); } - return Google_Http_REST::execute($http, $request, '', $this->retryConfig, $this->retryMap); + return Google_Http_REST::execute($http->reveal(), $request, '', $this->retryConfig, $this->retryMap); } /** @@ -728,19 +734,15 @@ private function runTask() $task->setRetryMap($this->retryMap); } - $exception = $this->getMockBuilder('Google_Service_Exception') - // HHVM blows up unless this is set - // @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/207 - ->setMethods(array('setTraceOptions')) - ->disableOriginalConstructor() - ->getMock(); + $exception = $this->prophesize('Google_Service_Exception'); + $exceptionCount = 0; $exceptionCalls = array(); for ($i = 0; $i < $this->mockedCallsCount; $i++) { if (is_int($this->mockedCalls[$i])) { $exceptionCalls[$exceptionCount++] = $this->mockedCalls[$i]; - $this->mockedCalls[$i] = $exception; + $this->mockedCalls[$i] = $exception->reveal(); } } From afd3b55207efd691564c6e8e8b9e9bb39f6ce4f4 Mon Sep 17 00:00:00 2001 From: Levi Durfee Date: Mon, 27 Jan 2020 10:26:57 -0500 Subject: [PATCH 120/301] Update readme.md (#1779) --- docs/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index b7ad418c4..c97236175 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,4 +1,4 @@ -# Google API Client LIbrary for PHP Docs +# Google API Client Library for PHP Docs The Google API Client Library for PHP offers simple, flexible access to many Google APIs. @@ -12,4 +12,4 @@ The Google API Client Library for PHP offers simple, flexible access to many Goo - [OAuth Server](oauth-server.md) - [OAuth Web](oauth-web.md) - [Pagination](pagination.md) -- [Parameters](parameters.md) \ No newline at end of file +- [Parameters](parameters.md) From 2be3b2fe17ec6fdca8a2dc671613acd5a0731dd0 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 16 Mar 2020 08:46:46 -0700 Subject: [PATCH 121/301] fix: change setApprovalPrompt to setPrompt (#1796) --- docs/oauth-web.md | 90 +++++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/docs/oauth-web.md b/docs/oauth-web.md index 96314a76a..699dc3220 100644 --- a/docs/oauth-web.md +++ b/docs/oauth-web.md @@ -22,10 +22,10 @@ Any application that uses OAuth 2.0 to access Google APIs must have authorizatio 1. Open the [Credentials page](https://console.developers.google.com/apis/credentials) in the API Console. 2. Click **Create credentials > OAuth client ID**. -3. Complete the form. Set the application type to `Web application`. Applications that use languages and frameworks like PHP, Java, Python, Ruby, and .NET must specify authorized **redirect URIs**. The redirect URIs are the endpoints to which the OAuth 2.0 server can send responses. - - For testing, you can specify URIs that refer to the local machine, such as `http://localhost:8080`. With that in mind, please note that all of the examples in this document use `http://localhost:8080` as the redirect URI. - +3. Complete the form. Set the application type to `Web application`. Applications that use languages and frameworks like PHP, Java, Python, Ruby, and .NET must specify authorized **redirect URIs**. The redirect URIs are the endpoints to which the OAuth 2.0 server can send responses. + + For testing, you can specify URIs that refer to the local machine, such as `http://localhost:8080`. With that in mind, please note that all of the examples in this document use `http://localhost:8080` as the redirect URI. + We recommend that you design your app's auth endpoints so that your application does not expose authorization codes to other resources on the page. After creating your credentials, download the **client_secret.json** file from the API Console. Securely store the file in a location that only your application can access. @@ -56,7 +56,7 @@ To run the PHP code samples in this document, you'll need: ```sh php composer.phar require google/apiclient:^2.0 ``` - + ## Obtaining OAuth 2.0 access tokens The following steps show how your application interacts with Google's OAuth 2.0 server to obtain a user's consent to perform an API request on the user's behalf. Your application must have that consent before it can execute a Google API request that requires user authorization. @@ -103,8 +103,8 @@ $client->setAuthConfig('client_secret.json'); ##### `redirect_uri` -**Required**. Determines where the API server redirects the user after the user completes the authorization flow. The value must exactly match one of the authorized redirect URIs for the OAuth 2.0 client, which you configured in the [API Console](https://console.developers.google.com/). If this value doesn't match an authorized URI, you will get a 'redirect_uri_mismatch' error. Note that the `http` or `https` scheme, case, and trailing slash ('`/`') must all match. - +**Required**. Determines where the API server redirects the user after the user completes the authorization flow. The value must exactly match one of the authorized redirect URIs for the OAuth 2.0 client, which you configured in the [API Console](https://console.developers.google.com/). If this value doesn't match an authorized URI, you will get a 'redirect_uri_mismatch' error. Note that the `http` or `https` scheme, case, and trailing slash ('`/`') must all match. + To set this value in PHP, call the `setRedirectUri` function. Note that you must specify a valid redirect URI for your API Console project. ```php @@ -113,24 +113,24 @@ $client->setRedirectUri('http://localhost:8080/oauth2callback.php'); ##### `scope` -**Required**. A space-delimited list of scopes that identify the resources that your application could access on the user's behalf. These values inform the consent screen that Google displays to the user. - +**Required**. A space-delimited list of scopes that identify the resources that your application could access on the user's behalf. These values inform the consent screen that Google displays to the user. + Scopes enable your application to only request access to the resources that it needs while also enabling users to control the amount of access that they grant to your application. Thus, there is an inverse relationship between the number of scopes requested and the likelihood of obtaining user consent. To set this value in PHP, call the `addScope` function: ```php $client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY); ``` -The [OAuth 2.0 API Scopes](https://developers.google.com/identity/protocols/googlescopes) document provides a full list of scopes that you might use to access Google APIs. - +The [OAuth 2.0 API Scopes](https://developers.google.com/identity/protocols/googlescopes) document provides a full list of scopes that you might use to access Google APIs. + We recommend that your application request access to authorization scopes in context whenever possible. By requesting access to user data in context, via [incremental authorization](#Incremental-authorization), you help users to more easily understand why your application needs the access it is requesting. ##### `access_type` -**Recommended**. Indicates whether your application can refresh access tokens when the user is not present at the browser. Valid parameter values are `online`, which is the default value, and `offline`. - -Set the value to `offline` if your application needs to refresh access tokens when the user is not present at the browser. This is the method of refreshing access tokens described later in this document. This value instructs the Google authorization server to return a refresh token _and_ an access token the first time that your application exchanges an authorization code for tokens. - +**Recommended**. Indicates whether your application can refresh access tokens when the user is not present at the browser. Valid parameter values are `online`, which is the default value, and `offline`. + +Set the value to `offline` if your application needs to refresh access tokens when the user is not present at the browser. This is the method of refreshing access tokens described later in this document. This value instructs the Google authorization server to return a refresh token _and_ an access token the first time that your application exchanges an authorization code for tokens. + To set this value in PHP, call the `setAccessType` function: ```php @@ -139,10 +139,10 @@ $client->setAccessType('offline'); ##### `state` -**Recommended**. Specifies any string value that your application uses to maintain state between your authorization request and the authorization server's response. The server returns the exact value that you send as a `name=value` pair in the hash (`#`) fragment of the `redirect_uri` after the user consents to or denies your application's access request. - -You can use this parameter for several purposes, such as directing the user to the correct resource in your application, sending nonces, and mitigating cross-site request forgery. Since your `redirect_uri` can be guessed, using a `state` value can increase your assurance that an incoming connection is the result of an authentication request. If you generate a random string or encode the hash of a cookie or another value that captures the client's state, you can validate the response to additionally ensure that the request and response originated in the same browser, providing protection against attacks such as cross-site request forgery. See the [OpenID Connect](https://developers.google.com/identity/protocols/OpenIDConnect#createxsrftoken) documentation for an example of how to create and confirm a `state` token. - +**Recommended**. Specifies any string value that your application uses to maintain state between your authorization request and the authorization server's response. The server returns the exact value that you send as a `name=value` pair in the hash (`#`) fragment of the `redirect_uri` after the user consents to or denies your application's access request. + +You can use this parameter for several purposes, such as directing the user to the correct resource in your application, sending nonces, and mitigating cross-site request forgery. Since your `redirect_uri` can be guessed, using a `state` value can increase your assurance that an incoming connection is the result of an authentication request. If you generate a random string or encode the hash of a cookie or another value that captures the client's state, you can validate the response to additionally ensure that the request and response originated in the same browser, providing protection against attacks such as cross-site request forgery. See the [OpenID Connect](https://developers.google.com/identity/protocols/OpenIDConnect#createxsrftoken) documentation for an example of how to create and confirm a `state` token. + To set this value in PHP, call the `setState` function: ```php @@ -151,8 +151,8 @@ $client->setState($sample_passthrough_value); ##### `include_granted_scopes` -**Optional**. Enables applications to use incremental authorization to request access to additional scopes in context. If you set this parameter's value to `true` and the authorization request is granted, then the new access token will also cover any scopes to which the user previously granted the application access. See the [incremental authorization](#Incremental-authorization) section for examples. - +**Optional**. Enables applications to use incremental authorization to request access to additional scopes in context. If you set this parameter's value to `true` and the authorization request is granted, then the new access token will also cover any scopes to which the user previously granted the application access. See the [incremental authorization](#Incremental-authorization) section for examples. + To set this value in PHP, call the `setIncludeGrantedScopes` function: ```php @@ -161,10 +161,10 @@ $client->setIncludeGrantedScopes(true); ##### `login_hint` -**Optional**. If your application knows which user is trying to authenticate, it can use this parameter to provide a hint to the Google Authentication Server. The server uses the hint to simplify the login flow either by prefilling the email field in the sign-in form or by selecting the appropriate multi-login session. - -Set the parameter value to an email address or `sub` identifier, which is equivalent to the user's Google ID. - +**Optional**. If your application knows which user is trying to authenticate, it can use this parameter to provide a hint to the Google Authentication Server. The server uses the hint to simplify the login flow either by prefilling the email field in the sign-in form or by selecting the appropriate multi-login session. + +Set the parameter value to an email address or `sub` identifier, which is equivalent to the user's Google ID. + To set this value in PHP, call the `setLoginHint` function: ```php @@ -173,12 +173,12 @@ $client->setLoginHint('timmerman@google.com'); ##### `prompt` -**Optional**. A space-delimited, case-sensitive list of prompts to present the user. If you don't specify this parameter, the user will be prompted only the first time your app requests access. - +**Optional**. A space-delimited, case-sensitive list of prompts to present the user. If you don't specify this parameter, the user will be prompted only the first time your app requests access. + To set this value in PHP, call the `setApprovalPrompt` function: ```php -$client->setApprovalPrompt('consent'); +$client->setPrompt('consent'); ``` Possible values are: @@ -200,16 +200,16 @@ Prompt the user to select an account. Redirect the user to Google's OAuth 2.0 server to initiate the authentication and authorization process. Typically, this occurs when your application first needs to access the user's data. In the case of [incremental authorization](#incremental-authorization), this step also occurs when your application first needs to access additional resources that it does not yet have permission to access. 1. Generate a URL to request access from Google's OAuth 2.0 server: - + ```php $auth_url = $client->createAuthUrl(); ``` - + 2. Redirect the user to `$auth_url`: - + ```php header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL)); - ``` + ``` Google's OAuth 2.0 server authenticates the user and obtains consent from the user for your application to access the requested scopes. The response is sent back to your application using the redirect URL you specified. @@ -233,7 +233,7 @@ An authorization code response: https://oauth2.example.com/auth?code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7 -> **Important**: If your response endpoint renders an HTML page, any resources on that page will be able to see the authorization code in the URL. Scripts can read the URL directly, and the URL in the `Referer` HTTP header may be sent to any or all resources on the page. +> **Important**: If your response endpoint renders an HTML page, any resources on that page will be able to see the authorization code in the URL. Scripts can read the URL directly, and the URL in the `Referer` HTTP header may be sent to any or all resources on the page. > > Carefully consider whether you want to send authorization credentials to all resources on that page (especially third-party scripts such as social plugins and analytics). To avoid this issue, we recommend that the server first handle the request, then redirect to another URL that doesn't include the response parameters. @@ -276,22 +276,22 @@ $access_token = $client->getAccessToken(); Use the access token to call Google APIs by completing the following steps: 1. If you need to apply an access token to a new `Google_Client` object—for example, if you stored the access token in a user session—use the `setAccessToken` method: - + ```php $client->setAccessToken($access_token); ``` - + 2. Build a service object for the API that you want to call. You build a a service object by providing an authorized `Google_Client` object to the constructor for the API you want to call. For example, to call the Drive API: - + ```php $drive = new Google_Service_Drive($client); ``` - + 3. Make requests to the API service using the [interface provided by the service object](start.md). For example, to list the files in the authenticated user's Google Drive: - + ```php $files = $drive->files->listFiles(array())->getItems(); - ``` + ``` [](#top_of_page)Complete example -------------------------------- @@ -302,24 +302,24 @@ To run this example: 1. In the API Console, add the URL of the local machine to the list of redirect URLs. For example, add `http://localhost:8080`. 2. Create a new directory and change to it. For example: - + ```sh mkdir ~/php-oauth2-example cd ~/php-oauth2-example ``` - + 3. Install the [Google API Client Library](https://github.com/google/google-api-php-client) for PHP using [Composer](https://getcomposer.org): - + ```sh composer require google/apiclient:^2.0 ``` - + 4. Create the files `index.php` and `oauth2callback.php` with the content below. 5. Run the example with a web server configured to serve PHP. If you use PHP 5.4 or newer, you can use PHP's built-in test web server: - + ```sh php -S localhost:8080 ~/php-oauth2-example - ``` + ``` #### index.php From e95b0c362f0374116dce8c10922bf07b897dcf34 Mon Sep 17 00:00:00 2001 From: David Supplee Date: Wed, 25 Mar 2020 11:36:10 -0700 Subject: [PATCH 122/301] =?UTF-8?q?docs:=20use=20explicit=20getter=20in=20?= =?UTF-8?q?sample=20over=20iterator=20interface=20met=E2=80=A6=20(#1783)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * docs: use explicit getter in sample over iterator interface methods * trigger travis Co-authored-by: John Pedrie --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index faa17a03b..c7327b69b 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ $service = new Google_Service_Books($client); $optParams = array('filter' => 'free-ebooks'); $results = $service->volumes->listVolumes('Henry David Thoreau', $optParams); -foreach ($results as $item) { +foreach ($results->getItems() as $item) { echo $item['volumeInfo']['title'], "
      \n"; } ``` From 1fdfe942f9aaf3064e621834a5e3047fccb3a6da Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Thu, 26 Mar 2020 11:30:32 -0400 Subject: [PATCH 123/301] Prepare v2.4.1 (#1808) --- src/Google/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index 5785ac1f5..6f1fcfd3a 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -38,7 +38,7 @@ */ class Google_Client { - const LIBVER = "2.4.0"; + const LIBVER = "2.4.1"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; From 64d1b33d67abe2a1e1b9071c61775e93aa4266a7 Mon Sep 17 00:00:00 2001 From: Ivan Soshin <1255458+inogo@users.noreply.github.com> Date: Tue, 21 Apr 2020 19:50:05 +0300 Subject: [PATCH 124/301] docs: change setApprovalPrompt to setPrompt in oauth-web.md (#1822) --- docs/oauth-web.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/oauth-web.md b/docs/oauth-web.md index 699dc3220..84222e9ab 100644 --- a/docs/oauth-web.md +++ b/docs/oauth-web.md @@ -175,7 +175,7 @@ $client->setLoginHint('timmerman@google.com'); **Optional**. A space-delimited, case-sensitive list of prompts to present the user. If you don't specify this parameter, the user will be prompted only the first time your app requests access. -To set this value in PHP, call the `setApprovalPrompt` function: +To set this value in PHP, call the `setPrompt` function: ```php $client->setPrompt('consent'); From 19c033ff3cc3c1b55f6a920d16f894d3fe31f9fd Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Mon, 4 May 2020 15:26:55 -0400 Subject: [PATCH 125/301] docs: update readme with info on services repo (#1823) * docs: update readme with info on services repo * fix replace * update --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index c7327b69b..9a33f3563 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,8 @@ Finally, be sure to include the autoloader: require_once '/path/to/your-project/vendor/autoload.php'; ``` +This library relies on `google/apiclient-services`. That library provides up-to-date API wrappers for a large number of Google APIs. In order that users may make use of the latest API clients, this library does not pin to a specific version of `google/apiclient-services`. **In order to prevent the accidental installation of API wrappers with breaking changes**, it is highly recommended that you pin to the latest version yourself prior to using this library in production. + ### Download the Release If you prefer not to use composer, you can download the package in its entirety. The [Releases](https://github.com/googleapis/google-api-php-client/releases) page lists all stable versions. Download any file @@ -242,6 +244,8 @@ The method used is a matter of preference, but *it will be very difficult to use If Google Authentication is desired for external applications, or a Google API is not available yet in this library, HTTP requests can be made directly. +If you are installing this client only to authenticate your own HTTP client requests, you should use [`google/auth`](https://github.com/googleapis/google-auth-library-php#call-the-apis) instead. + The `authorize` method returns an authorized [Guzzle Client](http://docs.guzzlephp.org/), so any request made using the client will contain the corresponding authorization. ```php From 290ad568ff125d630fdb8c2e74c86d40503f593f Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 14 May 2020 07:49:53 -0700 Subject: [PATCH 126/301] docs: add link to google/apiclient-services release page (#1830) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9a33f3563..737aa57aa 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ Finally, be sure to include the autoloader: require_once '/path/to/your-project/vendor/autoload.php'; ``` -This library relies on `google/apiclient-services`. That library provides up-to-date API wrappers for a large number of Google APIs. In order that users may make use of the latest API clients, this library does not pin to a specific version of `google/apiclient-services`. **In order to prevent the accidental installation of API wrappers with breaking changes**, it is highly recommended that you pin to the latest version yourself prior to using this library in production. +This library relies on `google/apiclient-services`. That library provides up-to-date API wrappers for a large number of Google APIs. In order that users may make use of the latest API clients, this library does not pin to a specific version of `google/apiclient-services`. **In order to prevent the accidental installation of API wrappers with breaking changes**, it is highly recommended that you pin to the [latest version](https://github.com/googleapis/google-api-php-client-services/releases) yourself prior to using this library in production. ### Download the Release From 019012e9f69d59922cb8e6358160ff8c604e7a89 Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Thu, 14 May 2020 15:01:25 -0400 Subject: [PATCH 127/301] docs: fix upgrade guide cache note (#1834) --- UPGRADING.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/UPGRADING.md b/UPGRADING.md index 230297000..853088e2e 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -308,20 +308,23 @@ setting the Guzzle `GuzzleHttp\ClientInterface` object. 1. Automatically refreshes access tokens if one is set and the access token is expired - Removed `Google_Config` - Removed `Google_Utils` - - [`Google\Auth\CacheInterface`][Google Auth CacheInterface] is used for all caching. As a result: + - [`PSR-6`][PSR 6] cache is used for all caching. As a result: 1. Removed `Google_Cache_Abstract` 1. Classes `Google_Cache_Apc`, `Google_Cache_File`, `Google_Cache_Memcache`, and `Google_Cache_Null` now implement `Google\Auth\CacheInterface`. + 1. Google Auth provides simple [caching utilities][Google Auth Cache] which + are used by default unless you provide alternatives. - Removed `$boundary` constructor argument for `Google_Http_MediaFileUpload` -[PSR 3]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md +[PSR 3]: https://www.php-fig.org/psr/psr-3/ +[PSR 6]: https://www.php-fig.org/psr/psr-6/ [Guzzle 5]: https://github.com/guzzle/guzzle [Guzzle 6]: http://docs.guzzlephp.org/en/latest/psr7.html [Monolog]: https://github.com/Seldaek/monolog [Google Auth]: https://github.com/google/google-auth-library-php +[Google Auth Cache]: https://github.com/googleapis/google-auth-library-php/tree/master/src/Cache [Google Auth GCE]: https://github.com/google/google-auth-library-php/blob/master/src/GCECredentials.php [Google Auth OAuth2]: https://github.com/google/google-auth-library-php/blob/master/src/OAuth2.php [Google Auth Simple]: https://github.com/google/google-auth-library-php/blob/master/src/Simple.php [Google Auth AppIdentity]: https://github.com/google/google-auth-library-php/blob/master/src/AppIdentityCredentials.php -[Google Auth CacheInterface]: https://github.com/google/google-auth-library-php/blob/master/src/CacheInterface.php [Firebase JWT]: https://github.com/firebase/php-jwt From 1fd764531784f6f32aa226560c88d41fefc48276 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 18 May 2020 10:54:50 -0700 Subject: [PATCH 128/301] feat: adds new client options (#1829) --- composer.json | 2 +- src/Google/Client.php | 34 +++++++++++++++++++++++++-- tests/Google/ClientTest.php | 46 +++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 19f0e2f8d..fdf119578 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "license": "Apache-2.0", "require": { "php": ">=5.4", - "google/auth": "^1.0", + "google/auth": "^1.9", "google/apiclient-services": "~0.13", "firebase/php-jwt": "~2.0||~3.0||~4.0||~5.0", "monolog/monolog": "^1.17|^2.0", diff --git a/src/Google/Client.php b/src/Google/Client.php index 6f1fcfd3a..27435f101 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -102,6 +102,16 @@ public function __construct(array $config = array()) // https://developers.google.com/console 'client_id' => '', 'client_secret' => '', + + // Path to JSON credentials or an array representing those credentials + // @see Google_Client::setAuthConfig + 'credentials' => null, + // @see Google_Client::setScopes + 'scopes' => null, + // Sets X-Goog-User-Project, which specifies a user project to bill + // for access charges associated with the request + 'quota_project' => null, + 'redirect_uri' => null, 'state' => null, @@ -149,6 +159,16 @@ public function __construct(array $config = array()) ], $config ); + + if (!is_null($this->config['credentials'])) { + $this->setAuthConfig($this->config['credentials']); + unset($this->config['credentials']); + } + + if (!is_null($this->config['scopes'])) { + $this->setScopes($this->config['scopes']); + unset($this->config['scopes']); + } } /** @@ -1138,10 +1158,20 @@ private function createApplicationDefaultCredentials() 'client_email' => $this->config['client_email'], 'private_key' => $signingKey, 'type' => 'service_account', + 'quota_project' => $this->config['quota_project'], + ); + $credentials = CredentialsLoader::makeCredentials( + $scopes, + $serviceAccountCredentials ); - $credentials = CredentialsLoader::makeCredentials($scopes, $serviceAccountCredentials); } else { - $credentials = ApplicationDefaultCredentials::getCredentials($scopes); + $credentials = ApplicationDefaultCredentials::getCredentials( + $scopes, + null, + null, + null, + $this->config['quota_project'] + ); } // for service account domain-wide authority (impersonating a user) diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 14fcd207b..ab8b56838 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -771,4 +771,50 @@ public function testExecuteSetsCorrectHeaders() $request = new Request('POST', 'http://foo.bar/'); $client->execute($request); } + + public function testClientOptions() + { + // Test credential file + $tmpCreds = [ + 'type' => 'service_account', + 'client_id' => 'foo', + 'client_email' => '', + 'private_key' => '' + ]; + $tmpCredFile = tempnam(sys_get_temp_dir(), 'creds') . '.json'; + file_put_contents($tmpCredFile, json_encode($tmpCreds)); + $client = new Google_Client([ + 'credentials' => $tmpCredFile + ]); + $this->assertEquals('foo', $client->getClientId()); + + // Test credentials array + $client = new Google_Client([ + 'credentials' => $tmpCredFile + ]); + $this->assertEquals('foo', $client->getClientId()); + + // Test singular scope + $client = new Google_Client([ + 'scopes' => 'a-scope' + ]); + $this->assertEquals(['a-scope'], $client->getScopes()); + + // Test multiple scopes + $client = new Google_Client([ + 'scopes' => ['one-scope', 'two-scope'] + ]); + $this->assertEquals(['one-scope', 'two-scope'], $client->getScopes()); + + // Test quota project + $client = new Google_Client([ + 'quota_project' => 'some-quota-project' + ]); + $this->assertEquals('some-quota-project', $client->getConfig('quota_project')); + // Test quota project in google/auth dependency + $method = new ReflectionMethod($client, 'createApplicationDefaultCredentials'); + $method->setAccessible(true); + $credentials = $method->invoke($client); + $this->assertEquals('some-quota-project', $credentials->getQuotaProject()); + } } From 9ab9cc07f66e2c7274ea2753f102ae24d1271410 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 26 May 2020 15:29:38 -0700 Subject: [PATCH 129/301] Prepare v2.5.0 (#1841) --- src/Google/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index 27435f101..99dfde4a7 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -38,7 +38,7 @@ */ class Google_Client { - const LIBVER = "2.4.1"; + const LIBVER = "2.5.0"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; From 8e03c1ebd632b3a02a2c42744bf405a2ba414f4f Mon Sep 17 00:00:00 2001 From: Simon Schaufelberger Date: Mon, 8 Jun 2020 19:26:18 +0200 Subject: [PATCH 130/301] chore: update ignored files for exporting package (#1848) This commit is part of a campaign to reduce the amount of data transferred to save global bandwidth and reduce the amount of CO2. See https://github.com/Codeception/Codeception/pull/5527 for more info. --- .gitattributes | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitattributes b/.gitattributes index 68a0191a5..c8e4a6fca 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,6 +2,8 @@ /.github export-ignore /.gitignore export-ignore /.travis.yml export-ignore +/CODE_OF_CONDUCT.md +/docs export-ignore /examples export-ignore /phpunit.xml.dist export-ignore /phpcs.xml.dist export-ignore From c7f965f0f2a3d12089a0ee1fa5911c39324e1ec8 Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Thu, 18 Jun 2020 13:54:38 -0400 Subject: [PATCH 131/301] chore: automate asset release (#1852) --- .github/workflows/asset-release.yml | 66 +++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 .github/workflows/asset-release.yml diff --git a/.github/workflows/asset-release.yml b/.github/workflows/asset-release.yml new file mode 100644 index 000000000..9a1c1c73b --- /dev/null +++ b/.github/workflows/asset-release.yml @@ -0,0 +1,66 @@ +name: Add Release Assets + +on: + release: + types: [published] + +jobs: + asset: + runs-on: ${{ matrix.operating-system }} + strategy: + matrix: + operating-system: [ ubuntu-latest ] + php: [ "5.4", "5.6", "7.0", "7.4" ] + + name: Upload Release Assets + steps: + - uses: olegtarasov/get-tag@v2 + id: tagName + + - uses: octokit/request-action@v2.x + id: getLatestRelease + with: + route: GET /repos/:repository/releases/tags/:tag + repository: ${{ github.repository }} + tag: ${{ steps.tagName.outputs.tag }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Checkout + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + + - name: Install Dependencies + uses: nick-invision/retry@v1 + with: + timeout_minutes: 10 + max_attempts: 3 + command: composer remove --dev cache/filesystem-adapter && composer install --no-dev --prefer-dist + + - name: Create Archive + run: | + zip -r ${fileName} . && + zip -d ${fileName} ".git*" && + zip -d ${fileName} "tests*" && + zip -d ${fileName} "docs*" && + zip -d ${fileName} ".travis.yml" && + zip -d ${fileName} "phpcs.xml.dist" && + zip -d ${fileName} "phpunit.xml.dist" && + zip -d ${fileName} "tests*" && + zip -d ${fileName} "examples*" + env: + fileName: google-api-php-client-${{ steps.tagName.outputs.tag }}-PHP${{ matrix.php }}.zip + + - name: Upload Release Archive + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} + with: + upload_url: ${{ fromJson(steps.getLatestRelease.outputs.data).upload_url }} + asset_path: ./google-api-php-client-${{ steps.tagName.outputs.tag }}-PHP${{ matrix.php }}.zip + asset_name: google-api-php-client-${{ steps.tagName.outputs.tag }}-PHP${{ matrix.php }}.zip + asset_content_type: application/zip From b1f0964528446dac8e979b6b3babf8a185fa538a Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 9 Jul 2020 11:04:21 -0700 Subject: [PATCH 132/301] feat: Guzzle 7 support (#1868) --- composer.json | 4 ++-- src/Google/AuthHandler/AuthHandlerFactory.php | 15 ++++++++---- src/Google/AuthHandler/Guzzle6AuthHandler.php | 2 +- src/Google/AuthHandler/Guzzle7AuthHandler.php | 23 +++++++++++++++++++ src/Google/Client.php | 19 ++++++++++----- tests/BaseTest.php | 18 ++++++++++++++- tests/Google/ClientTest.php | 4 ++-- 7 files changed, 69 insertions(+), 16 deletions(-) create mode 100644 src/Google/AuthHandler/Guzzle7AuthHandler.php diff --git a/composer.json b/composer.json index fdf119578..fa912c4f6 100644 --- a/composer.json +++ b/composer.json @@ -7,12 +7,12 @@ "license": "Apache-2.0", "require": { "php": ">=5.4", - "google/auth": "^1.9", + "google/auth": "^1.10", "google/apiclient-services": "~0.13", "firebase/php-jwt": "~2.0||~3.0||~4.0||~5.0", "monolog/monolog": "^1.17|^2.0", "phpseclib/phpseclib": "~0.3.10||~2.0", - "guzzlehttp/guzzle": "~5.3.1||~6.0", + "guzzlehttp/guzzle": "~5.3.1||~6.0||~7.0", "guzzlehttp/psr7": "^1.2" }, "require-dev": { diff --git a/src/Google/AuthHandler/AuthHandlerFactory.php b/src/Google/AuthHandler/AuthHandlerFactory.php index f1a3229ae..1a15f7a89 100644 --- a/src/Google/AuthHandler/AuthHandlerFactory.php +++ b/src/Google/AuthHandler/AuthHandlerFactory.php @@ -28,13 +28,20 @@ class Google_AuthHandler_AuthHandlerFactory */ public static function build($cache = null, array $cacheConfig = []) { - $version = ClientInterface::VERSION; + $guzzleVersion = null; + if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) { + $guzzleVersion = ClientInterface::MAJOR_VERSION; + } elseif (defined('\GuzzleHttp\ClientInterface::VERSION')) { + $guzzleVersion = (int) substr(ClientInterface::VERSION, 0, 1); + } - switch ($version[0]) { - case '5': + switch ($guzzleVersion) { + case 5: return new Google_AuthHandler_Guzzle5AuthHandler($cache, $cacheConfig); - case '6': + case 6: return new Google_AuthHandler_Guzzle6AuthHandler($cache, $cacheConfig); + case 7: + return new Google_AuthHandler_Guzzle7AuthHandler($cache, $cacheConfig); default: throw new Exception('Version not supported'); } diff --git a/src/Google/AuthHandler/Guzzle6AuthHandler.php b/src/Google/AuthHandler/Guzzle6AuthHandler.php index fcdfb3b03..d1c16e6fc 100644 --- a/src/Google/AuthHandler/Guzzle6AuthHandler.php +++ b/src/Google/AuthHandler/Guzzle6AuthHandler.php @@ -11,7 +11,7 @@ use Psr\Cache\CacheItemPoolInterface; /** -* +* This supports Guzzle 6 */ class Google_AuthHandler_Guzzle6AuthHandler { diff --git a/src/Google/AuthHandler/Guzzle7AuthHandler.php b/src/Google/AuthHandler/Guzzle7AuthHandler.php new file mode 100644 index 000000000..6f8d04d60 --- /dev/null +++ b/src/Google/AuthHandler/Guzzle7AuthHandler.php @@ -0,0 +1,23 @@ + false]; + $guzzleVersion = null; + if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) { + $guzzleVersion = ClientInterface::MAJOR_VERSION; + } elseif (defined('\GuzzleHttp\ClientInterface::VERSION')) { + $guzzleVersion = (int)substr(ClientInterface::VERSION, 0, 1); + } - $version = ClientInterface::VERSION; - if ('5' === $version[0]) { + $options = ['exceptions' => false]; + if (5 === $guzzleVersion) { $options = [ 'base_url' => $this->config['base_path'], 'defaults' => $options, ]; if ($this->isAppEngine()) { // set StreamHandler on AppEngine by default - $options['handler'] = new StreamHandler(); + $options['handler'] = new StreamHandler(); $options['defaults']['verify'] = '/etc/ca-certificates.crt'; } - } else { - // guzzle 6 + } elseif (6 === $guzzleVersion || 7 === $guzzleVersion) { + // guzzle 6 or 7 $options['base_uri'] = $this->config['base_path']; + } else { + throw new LogicException('Could not find supported version of Guzzle.'); } return new Client($options); diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 4c0afec55..650ae9ebb 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -59,7 +59,7 @@ private function createClient() } // adjust constructor depending on guzzle version - if (!$this->isGuzzle6()) { + if ($this->isGuzzle5()) { $options = ['defaults' => $options]; } @@ -208,8 +208,20 @@ protected function loadExample($example) return false; } + protected function isGuzzle7() + { + if (!defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) { + return false; + } + + return (7 === ClientInterface::MAJOR_VERSION); + } + protected function isGuzzle6() { + if (!defined('\GuzzleHttp\ClientInterface::VERSION')) { + return false; + } $version = ClientInterface::VERSION; return ('6' === $version[0]); @@ -217,6 +229,10 @@ protected function isGuzzle6() protected function isGuzzle5() { + if (!defined('\GuzzleHttp\ClientInterface::VERSION')) { + return false; + } + $version = ClientInterface::VERSION; return ('5' === $version[0]); diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index ab8b56838..11d3a46a5 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -44,7 +44,7 @@ public function testSignAppKey() private function checkAuthHandler($http, $className) { - if ($this->isGuzzle6()) { + if ($this->isGuzzle6() || $this->isGuzzle7()) { $stack = $http->getConfig('handler'); $class = new ReflectionClass(get_class($stack)); $property = $class->getProperty('stack'); @@ -75,7 +75,7 @@ private function checkAuthHandler($http, $className) private function checkCredentials($http, $fetcherClass, $sub = null) { - if ($this->isGuzzle6()) { + if ($this->isGuzzle6() || $this->isGuzzle7()) { $stack = $http->getConfig('handler'); $class = new ReflectionClass(get_class($stack)); $property = $class->getProperty('stack'); From 217399617868465ed7e1f43de9962ee0beaf2f09 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 9 Jul 2020 12:40:41 -0700 Subject: [PATCH 133/301] chore: Move from Travis to GitHub Actions (#1836) --- .github/actions/unittest/entrypoint.sh | 21 +++++++ .github/actions/unittest/retry.php | 24 ++++++++ .github/workflows/tests.yml | 79 ++++++++++++++++++++++++++ .travis.yml | 43 -------------- README.md | 2 +- composer.json | 2 +- tests/Google/ClientTest.php | 4 ++ 7 files changed, 130 insertions(+), 45 deletions(-) create mode 100755 .github/actions/unittest/entrypoint.sh create mode 100644 .github/actions/unittest/retry.php create mode 100644 .github/workflows/tests.yml delete mode 100644 .travis.yml diff --git a/.github/actions/unittest/entrypoint.sh b/.github/actions/unittest/entrypoint.sh new file mode 100755 index 000000000..e66f52674 --- /dev/null +++ b/.github/actions/unittest/entrypoint.sh @@ -0,0 +1,21 @@ +#!/bin/sh -l + +apt-get update && \ +apt-get install -y --no-install-recommends \ + git \ + zip \ + curl \ + unzip \ + wget + +curl --silent --show-error https://getcomposer.org/installer | php +php composer.phar self-update + +echo "---Installing dependencies ---" +echo "Removing cache/filesystem-adapter for PHP 5.4" +bash -c "if [[ $(php -r 'echo PHP_VERSION;') =~ \"5.4\" ]]; then php composer.phar remove --dev cache/filesystem-adapter; fi" +echo ${composerargs} +php $(dirname $0)/retry.php "php composer.phar update $composerargs" + +echo "---Running unit tests ---" +vendor/bin/phpunit diff --git a/.github/actions/unittest/retry.php b/.github/actions/unittest/retry.php new file mode 100644 index 000000000..c6525abe8 --- /dev/null +++ b/.github/actions/unittest/retry.php @@ -0,0 +1,24 @@ + 0) { + sleep($delay); + return retry($f, $delay, $retries - 1); + } else { + throw $e; + } + } +} + +retry(function () { + global $argv; + passthru($argv[1], $ret); + + if ($ret != 0) { + throw new \Exception('err'); + } +}, 1); diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 000000000..ba68dfd2b --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,79 @@ +name: Test Suite +on: + push: + branches: + - master + pull_request: + +jobs: + test: + runs-on: ${{matrix.operating-system}} + strategy: + matrix: + operating-system: [ ubuntu-latest ] + php: [ "5.6", "7.0", "7.1", "7.2", "7.3", "7.4" ] + name: PHP ${{matrix.php }} Unit Test + steps: + - uses: actions/checkout@v2 + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + - name: Install Dependencies + uses: nick-invision/retry@v1 + with: + timeout_minutes: 10 + max_attempts: 3 + command: composer install + - name: Run Script + run: vendor/bin/phpunit + # use dockerfiles for oooooolllllldddd versions of php, setup-php times out for those. + test_php55: + name: "PHP 5.5 Unit Test" + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Run Unit Tests + uses: docker://php:5.5-cli + with: + entrypoint: ./.github/actions/unittest/entrypoint.sh + test_php54: + name: "PHP 5.4 Unit Test" + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Run Unit Tests + uses: docker://php:5.4-cli + with: + entrypoint: ./.github/actions/unittest/entrypoint.sh + test_php54_lowest: + name: "PHP 5.4 Unit Test Prefer Lowest" + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Run Unit Tests + uses: docker://php:5.4-cli + env: + composerargs: "--prefer-lowest" + with: + entrypoint: ./.github/actions/unittest/entrypoint.sh + style: + runs-on: ubuntu-latest + name: PHP Style Check + steps: + - uses: actions/checkout@v2 + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "7.4" + - name: Install Dependencies + uses: nick-invision/retry@v1 + with: + timeout_minutes: 10 + max_attempts: 3 + command: composer install + - name: Run Script + run: vendor/bin/phpcs src --standard=phpcs.xml.dist -np diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 0f09f4137..000000000 --- a/.travis.yml +++ /dev/null @@ -1,43 +0,0 @@ -language: php -dist: trusty - -services: - - memcached - -env: - global: - - MEMCACHE_HOST=127.0.0.1 - - MEMCACHE_PORT=11211 - - COMPOSER_CMD="composer install" - -cache: - directories: - - $HOME/.composer/cache - -php: - - 5.4 - - 5.5 - - 5.6 - - 7.0 - - 7.1 - - 7.2 - - 7.3 - - 7.4 - -# Test lowest dependencies on PHP 5.4 -# (Guzzle 5.2, phpseclib 0.3) -matrix: - include: - - php: 5.4 - env: COMPOSER_CMD="composer update phpseclib/phpseclib guzzlehttp/guzzle guzzlehttp/psr7 --prefer-lowest" RUN_PHP_CS=true - -before_install: - - composer self-update - -install: - - if [[ "$TRAVIS_PHP_VERSION" == "5.4" ]]; then composer remove --dev cache/filesystem-adapter; fi - - $(echo $COMPOSER_CMD) - -script: - - vendor/bin/phpunit - - if [[ "$RUN_PHP_CS" == "true" ]]; then vendor/bin/phpcs src -np; fi diff --git a/README.md b/README.md index 737aa57aa..0cc92f599 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Build Status](https://travis-ci.org/googleapis/google-api-php-client.svg?branch=master)](https://travis-ci.org/googleapis/google-api-php-client) +![](https://github.com/googleapis/google-api-php-client/workflows/.github/workflows/tests.yml/badge.svg) # Google APIs Client Library for PHP # diff --git a/composer.json b/composer.json index fa912c4f6..e75ac4255 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "guzzlehttp/psr7": "^1.2" }, "require-dev": { - "phpunit/phpunit": "^4.8|^5.0", + "phpunit/phpunit": "^4.8.36|^5.0", "squizlabs/php_codesniffer": "~2.3", "symfony/dom-crawler": "~2.1", "symfony/css-selector": "~2.1", diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 11d3a46a5..62f630adf 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -772,6 +772,9 @@ public function testExecuteSetsCorrectHeaders() $client->execute($request); } + /** + * @runInSeparateProcess + */ public function testClientOptions() { // Test credential file @@ -812,6 +815,7 @@ public function testClientOptions() ]); $this->assertEquals('some-quota-project', $client->getConfig('quota_project')); // Test quota project in google/auth dependency + putenv('GOOGLE_APPLICATION_CREDENTIALS='.$tmpCredFile); $method = new ReflectionMethod($client, 'createApplicationDefaultCredentials'); $method->setAccessible(true); $credentials = $method->invoke($client); From 8c991eb8df594c29ddd15a9a990806decf515130 Mon Sep 17 00:00:00 2001 From: Simon Schaufelberger Date: Fri, 10 Jul 2020 00:33:41 +0200 Subject: [PATCH 134/301] fix: add missing export-ignore (#1873) --- .gitattributes | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitattributes b/.gitattributes index c8e4a6fca..e98a4d1b3 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,7 +2,7 @@ /.github export-ignore /.gitignore export-ignore /.travis.yml export-ignore -/CODE_OF_CONDUCT.md +/CODE_OF_CONDUCT.md export-ignore /docs export-ignore /examples export-ignore /phpunit.xml.dist export-ignore From 8e9215910703f72b29f50d3aa7f67545f4c56ff4 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 10 Jul 2020 09:32:24 -0700 Subject: [PATCH 135/301] feat: add docs generation action and fix docs (#1872) --- .github/actions/docs/entrypoint.sh | 21 +++++++++++++++++++++ .github/actions/docs/sami.php | 29 +++++++++++++++++++++++++++++ .github/actions/docs/sami.php.dist | 29 +++++++++++++++++++++++++++++ .github/workflows/docs.yml | 28 ++++++++++++++++++++++++++++ .gitignore | 1 + src/Google/AccessToken/Verify.php | 3 ++- src/Google/Client.php | 8 ++++++-- src/Google/Http/MediaFileUpload.php | 4 ++-- src/Google/Http/REST.php | 5 +++++ src/Google/Service/Exception.php | 1 - 10 files changed, 123 insertions(+), 6 deletions(-) create mode 100755 .github/actions/docs/entrypoint.sh create mode 100644 .github/actions/docs/sami.php create mode 100644 .github/actions/docs/sami.php.dist create mode 100644 .github/workflows/docs.yml diff --git a/.github/actions/docs/entrypoint.sh b/.github/actions/docs/entrypoint.sh new file mode 100755 index 000000000..af8227fd3 --- /dev/null +++ b/.github/actions/docs/entrypoint.sh @@ -0,0 +1,21 @@ +#!/bin/sh -l + +apt-get update +apt-get install -y git +git reset --hard HEAD + +# Required so sami.php is available for previous versions +cp .github/actions/docs/sami.php.dist .github/actions/docs/sami.php + +# Run the docs generation command +php vendor/bin/sami.php update .github/actions/docs/sami.php + +cd ./.docs + +git init +git config user.name "GitHub Actions" +git config user.email "actions@github.com" + +git add . +git commit -m "Updating docs" +git push -q https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/${GITHUB_REPOSITORY} HEAD:gh-pages --force diff --git a/.github/actions/docs/sami.php b/.github/actions/docs/sami.php new file mode 100644 index 000000000..bf44e3ee8 --- /dev/null +++ b/.github/actions/docs/sami.php @@ -0,0 +1,29 @@ +files() + ->name('*.php') + ->exclude('vendor') + ->exclude('tests') + ->in($projectRoot); + +$versions = GitVersionCollection::create($projectRoot) + ->addFromTags(function($tag) { + return 0 === strpos($tag, 'v2.') && false === strpos($tag, 'RC'); + }) + ->add('master', 'master branch'); + +return new Sami($iterator, [ + 'title' => 'Google APIs Client Library for PHP API Reference', + 'build_dir' => $projectRoot . '/.docs/%version%', + 'cache_dir' => $projectRoot . '/.cache/%version%', + 'remote_repository' => new GitHubRemoteRepository('googleapis/google-api-php-client', $projectRoot), + 'versions' => $versions +]); diff --git a/.github/actions/docs/sami.php.dist b/.github/actions/docs/sami.php.dist new file mode 100644 index 000000000..bf44e3ee8 --- /dev/null +++ b/.github/actions/docs/sami.php.dist @@ -0,0 +1,29 @@ +files() + ->name('*.php') + ->exclude('vendor') + ->exclude('tests') + ->in($projectRoot); + +$versions = GitVersionCollection::create($projectRoot) + ->addFromTags(function($tag) { + return 0 === strpos($tag, 'v2.') && false === strpos($tag, 'RC'); + }) + ->add('master', 'master branch'); + +return new Sami($iterator, [ + 'title' => 'Google APIs Client Library for PHP API Reference', + 'build_dir' => $projectRoot . '/.docs/%version%', + 'cache_dir' => $projectRoot . '/.cache/%version%', + 'remote_repository' => new GitHubRemoteRepository('googleapis/google-api-php-client', $projectRoot), + 'versions' => $versions +]); diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml new file mode 100644 index 000000000..cf3f13e46 --- /dev/null +++ b/.github/workflows/docs.yml @@ -0,0 +1,28 @@ +name: Generate Documentation +on: + push: + branches: + - master + tags: + - "*" + +jobs: + docs: + name: "Generate Project Documentation" + runs-on: ubuntu-16.04 + steps: + - name: Checkout + uses: actions/checkout@v2 + - run: git fetch --depth=1 origin +refs/tags/*:refs/tags/* + - name: Install Dependencies + uses: nick-invision/retry@v1 + with: + timeout_minutes: 10 + max_attempts: 3 + command: composer config repositories.sami vcs https://${{ secrets.GITHUB_TOKEN }}@github.com/jdpedrie/sami.git && composer require sami/sami:dev-master && git reset --hard HEAD + - name: Generate and Push Documentation + uses: docker://php:7.3-cli + env: + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} + with: + entrypoint: ./.github/actions/docs/entrypoint.sh diff --git a/.gitignore b/.gitignore index 9b429eae6..837e0b5be 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .DS_Store +.github/actions/docs/sami.php phpunit.xml phpcs.xml composer.lock diff --git a/src/Google/AccessToken/Verify.php b/src/Google/AccessToken/Verify.php index bc0afcb39..e8067c5a1 100644 --- a/src/Google/AccessToken/Verify.php +++ b/src/Google/AccessToken/Verify.php @@ -73,7 +73,8 @@ public function __construct( * The audience parameter can be used to control which id tokens are * accepted. By default, the id token must have been issued to this OAuth2 client. * - * @param $audience + * @param string $idToken the ID token in JWT format + * @param string $audience Optional. The audience to verify against JWt "aud" * @return array the token payload, if successful */ public function verifyIdToken($idToken, $audience = null) diff --git a/src/Google/Client.php b/src/Google/Client.php index 532cdfc7d..763e474e4 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -765,8 +765,11 @@ public function verifyIdToken($idToken = null) /** * Set the scopes to be requested. Must be called before createAuthUrl(). * Will remove any previously configured scopes. - * @param string|array $scope_or_scopes, ie: array('https://www.googleapis.com/auth/plus.login', - * 'https://www.googleapis.com/auth/moderator') + * @param string|array $scope_or_scopes, ie: + * array( + * 'https://www.googleapis.com/auth/plus.login', + * 'https://www.googleapis.com/auth/moderator' + * ); */ public function setScopes($scope_or_scopes) { @@ -819,6 +822,7 @@ public function prepareScopes() * Helper method to execute deferred HTTP requests. * * @param $request Psr\Http\Message\RequestInterface|Google_Http_Batch + * @param string $expectedClass * @throws Google_Exception * @return object of the type of the expected class or Psr\Http\Message\ResponseInterface. */ diff --git a/src/Google/Http/MediaFileUpload.php b/src/Google/Http/MediaFileUpload.php index f4ee97683..7a0f4e0bb 100644 --- a/src/Google/Http/MediaFileUpload.php +++ b/src/Google/Http/MediaFileUpload.php @@ -114,8 +114,8 @@ public function getProgress() /** * Send the next part of the file to upload. - * @param [$chunk] the next set of bytes to send. If false will used $data passed - * at construct time. + * @param bool $chunk Optional. The next set of bytes to send. If false will + * use $data passed at construct time. */ public function nextChunk($chunk = false) { diff --git a/src/Google/Http/REST.php b/src/Google/Http/REST.php index c2156a2e8..c495ed9fe 100644 --- a/src/Google/Http/REST.php +++ b/src/Google/Http/REST.php @@ -33,6 +33,9 @@ class Google_Http_REST * * @param Google_Client $client * @param Psr\Http\Message\RequestInterface $req + * @param string $expectedClass + * @param array $config + * @param array $retryMap * @return array decoded result * @throws Google_Service_Exception on server side error (ie: not authenticated, * invalid or malformed post body, invalid url) @@ -63,6 +66,7 @@ public static function execute( * * @param Google_Client $client * @param Psr\Http\Message\RequestInterface $request + * @param string $expectedClass * @return array decoded result * @throws Google_Service_Exception on server side error (ie: not authenticated, * invalid or malformed post body, invalid url) @@ -100,6 +104,7 @@ public static function doExecute(ClientInterface $client, RequestInterface $requ * @throws Google_Service_Exception * @param Psr\Http\Message\RequestInterface $response The http response to be decoded. * @param Psr\Http\Message\ResponseInterface $response + * @param string $expectedClass * @return mixed|null */ public static function decodeHttpResponse( diff --git a/src/Google/Service/Exception.php b/src/Google/Service/Exception.php index abfd3f7f1..3ab28aed3 100644 --- a/src/Google/Service/Exception.php +++ b/src/Google/Service/Exception.php @@ -31,7 +31,6 @@ class Google_Service_Exception extends Google_Exception * @param Exception|null $previous * @param [{string, string}] errors List of errors returned in an HTTP * response. Defaults to []. - * @param array|null $retryMap Map of errors with retry counts. */ public function __construct( $message, From 326e37fde5145079b74f1ce7249d242739d53cbc Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 10 Jul 2020 10:05:22 -0700 Subject: [PATCH 136/301] fix: examples and tests (#1871) --- examples/batch.php | 8 +- examples/service-account.php | 7 +- examples/simple-query.php | 14 +- tests/BaseTest.php | 33 +- tests/Google/ClientTest.php | 12 +- tests/Google/Http/BatchTest.php | 17 + tests/Google/Service/AdSenseTest.php | 487 ------------------- tests/Google/Service/PagespeedonlineTest.php | 5 - tests/Google/Service/PlusTest.php | 69 --- tests/Google/Service/UrlshortenerTest.php | 44 -- tests/examples/batchTest.php | 2 +- tests/examples/serviceAccountTest.php | 2 +- 12 files changed, 59 insertions(+), 641 deletions(-) delete mode 100644 tests/Google/Service/AdSenseTest.php delete mode 100644 tests/Google/Service/PlusTest.php delete mode 100644 tests/Google/Service/UrlshortenerTest.php diff --git a/examples/batch.php b/examples/batch.php index ac9ebbae7..23cf9856f 100644 --- a/examples/batch.php +++ b/examples/batch.php @@ -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"); /************************************************ diff --git a/examples/service-account.php b/examples/service-account.php index 6c23f0d52..2d723a1f9 100644 --- a/examples/service-account.php +++ b/examples/service-account.php @@ -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); ?>

      Results Of Call:

      diff --git a/examples/simple-query.php b/examples/simple-query.php index 3242be5e8..5358b5045 100644 --- a/examples/simple-query.php +++ b/examples/simple-query.php @@ -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); /************************************************ diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 650ae9ebb..36d613b75 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -26,7 +26,6 @@ class BaseTest extends TestCase { private $key; private $client; - protected $testDir = __DIR__; public function getClient() { @@ -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" @@ -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) diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 62f630adf..870510f65 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -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')); @@ -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()); } } @@ -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 diff --git a/tests/Google/Http/BatchTest.php b/tests/Google/Http/BatchTest.php index 4ca7d18f2..8445a21ea 100644 --- a/tests/Google/Http/BatchTest.php +++ b/tests/Google/Http/BatchTest.php @@ -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(); diff --git a/tests/Google/Service/AdSenseTest.php b/tests/Google/Service/AdSenseTest.php deleted file mode 100644 index 14dbb3b1e..000000000 --- a/tests/Google/Service/AdSenseTest.php +++ /dev/null @@ -1,487 +0,0 @@ -checkToken(); - $this->adsense = new Google_Service_AdSense($this->getClient()); - } - - public function testAccountsList() - { - $accounts = $this->adsense->accounts->listAccounts(); - $this->assertArrayHasKey('kind', $accounts); - $this->assertEquals($accounts['kind'], 'adsense#accounts'); - $account = $this->getRandomElementFromArray($accounts['items']); - $this->checkAccountElement($account); - } - - /** - * @depends testAccountsList - */ - public function testAccountsGet() - { - $accounts = $this->adsense->accounts->listAccounts(); - $account = $this->getRandomElementFromArray($accounts['items']); - $retrievedAccount = $this->adsense->accounts->get($account['id']); - $this->checkAccountElement($retrievedAccount); - } - - /** - * @depends testAccountsList - */ - public function testAccountsReportGenerate() - { - $startDate = '2011-01-01'; - $endDate = '2011-01-31'; - $optParams = $this->getReportOptParams(); - $accounts = $this->adsense->accounts->listAccounts(); - $accountId = $accounts['items'][0]['id']; - $report = $this->adsense->accounts_reports->generate( - $accountId, - $startDate, - $endDate, - $optParams - ); - $this->checkReport($report); - } - - /** - * @depends testAccountsList - */ - public function testAccountsAdClientsList() - { - $accounts = $this->adsense->accounts->listAccounts(); - $account = $this->getRandomElementFromArray($accounts['items']); - $adClients = - $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); - $this->checkAdClientsCollection($adClients); - } - - /** - * @depends testAccountsList - * @depends testAccountsAdClientsList - */ - public function testAccountsAdUnitsList() - { - $accounts = $this->adsense->accounts->listAccounts(); - foreach ($accounts['items'] as $account) { - $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); - foreach ($adClients['items'] as $adClient) { - $adUnits = $this->adsense->accounts_adunits->listAccountsAdunits( - $account['id'], - $adClient['id'] - ); - $this->checkAdUnitsCollection($adUnits); - break 2; - } - } - } - - /** - * @depends testAccountsList - * @depends testAccountsAdClientsList - */ - public function testAccountsAdUnitsGet() - { - $accounts = $this->adsense->accounts->listAccounts(); - foreach ($accounts['items'] as $account) { - $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); - foreach ($adClients['items'] as $adClient) { - $adUnits = $this->adsense->accounts_adunits->listAccountsAdunits( - $account['id'], - $adClient['id'] - ); - if (array_key_exists('items', $adUnits)) { - $adUnit = $this->getRandomElementFromArray($adUnits['items']); - $this->checkAdUnitElement($adUnit); - break 2; - } - } - } - } - - /** - * @depends testAccountsList - * @depends testAccountsAdClientsList - */ - public function testAccountsCustomChannelsList() - { - $accounts = $this->adsense->accounts->listAccounts(); - foreach ($accounts['items'] as $account) { - $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); - foreach ($adClients['items'] as $adClient) { - $customChannels = $this->adsense->accounts_customchannels - ->listAccountsCustomchannels($account['id'], $adClient['id']); - $this->checkCustomChannelsCollection($customChannels); - break 2; - } - } - } - - /** - * @depends testAccountsList - * @depends testAccountsAdClientsList - */ - public function testAccountsCustomChannelsGet() - { - $accounts = $this->adsense->accounts->listAccounts(); - foreach ($accounts['items'] as $account) { - $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); - foreach ($adClients['items'] as $adClient) { - $customChannels = - $this->adsense->accounts_customchannels->listAccountsCustomchannels( - $account['id'], - $adClient['id'] - ); - if (array_key_exists('items', $customChannels)) { - $customChannel = - $this->getRandomElementFromArray($customChannels['items']); - $this->checkCustomChannelElement($customChannel); - break 2; - } - } - } - } - - /** - * @depends testAccountsList - * @depends testAccountsAdClientsList - */ - public function testAccountsUrlChannelsList() - { - $accounts = $this->adsense->accounts->listAccounts(); - foreach ($accounts['items'] as $account) { - $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); - foreach ($adClients['items'] as $adClient) { - $urlChannels = - $this->adsense->accounts_urlchannels->listAccountsUrlchannels( - $account['id'], - $adClient['id'] - ); - $this->checkUrlChannelsCollection($urlChannels); - break 2; - } - } - } - - /** - * @depends testAccountsList - * @depends testAccountsAdClientsList - * @depends testAccountsAdUnitsList - */ - public function testAccountsAdUnitsCustomChannelsList() - { - $accounts = $this->adsense->accounts->listAccounts(); - foreach ($accounts['items'] as $account) { - $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); - foreach ($adClients['items'] as $adClient) { - $adUnits = - $this->adsense->accounts_adunits->listAccountsAdunits($account['id'], $adClient['id']); - if (array_key_exists('items', $adUnits)) { - foreach ($adUnits['items'] as $adUnit) { - $customChannels = - $this->adsense->accounts_adunits_customchannels->listAccountsAdunitsCustomchannels( - $account['id'], - $adClient['id'], - $adUnit['id'] - ); - $this->checkCustomChannelsCollection($customChannels); - // it's too expensive to go through each, if one is correct good - break 3; - } - } - } - } - } - - /** - * @depends testAccountsList - * @depends testAccountsAdClientsList - * @depends testAccountsCustomChannelsList - */ - public function testAccountsCustomChannelsAdUnitsList() - { - $accounts = $this->adsense->accounts->listAccounts(); - foreach ($accounts['items'] as $account) { - $adClients = - $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); - foreach ($adClients['items'] as $adClient) { - $customChannels = - $this->adsense->accounts_customchannels->listAccountsCustomchannels( - $account['id'], - $adClient['id'] - ); - if (array_key_exists('items', $customChannels)) { - foreach ($customChannels['items'] as $customChannel) { - $adUnits = - $this->adsense->accounts_customchannels_adunits->listAccountsCustomchannelsAdunits( - $account['id'], - $adClient['id'], - $customChannel['id'] - ); - $this->checkAdUnitsCollection($adUnits); - // it's too expensive to go through each, if one is correct good - break 3; - } - } - } - } - } - - public function testAdClientsList() - { - $adClients = $this->adsense->adclients->listAdclients(); - $this->checkAdClientsCollection($adClients); - } - - /** - * @depends testAdClientsList - */ - public function testAdUnitsList() - { - $adClients = $this->adsense->adclients->listAdclients(); - foreach ($adClients['items'] as $adClient) { - $adUnits = $this->adsense->adunits->listAdunits($adClient['id']); - $this->checkAdUnitsCollection($adUnits); - } - } - - /** - * @depends testAdClientsList - */ - public function testAdUnitsGet() - { - $adClients = $this->adsense->adclients->listAdclients(); - foreach ($adClients['items'] as $adClient) { - $adUnits = $this->adsense->adunits->listAdunits($adClient['id']); - if (array_key_exists('items', $adUnits)) { - $adUnit = $this->getRandomElementFromArray($adUnits['items']); - $this->checkAdUnitElement($adUnit); - break 1; - } - } - } - - /** - * @depends testAdClientsList - * @depends testAdUnitsList - */ - public function testAdUnitsCustomChannelsList() - { - $adClients = $this->adsense->adclients->listAdclients(); - foreach ($adClients['items'] as $adClient) { - $adUnits = $this->adsense->adunits->listAdunits($adClient['id']); - if (array_key_exists('items', $adUnits)) { - foreach ($adUnits['items'] as $adUnit) { - $customChannels = - $this->adsense->adunits_customchannels->listAdunitsCustomchannels( - $adClient['id'], - $adUnit['id'] - ); - $this->checkCustomChannelsCollection($customChannels); - break 2; - } - } - } - } - - /** - * @depends testAdClientsList - */ - public function testCustomChannelsList() - { - $adClients = $this->adsense->adclients->listAdclients(); - foreach ($adClients['items'] as $adClient) { - $customChannels = - $this->adsense->customchannels->listCustomchannels($adClient['id']); - $this->checkCustomChannelsCollection($customChannels); - } - } - - /** - * @depends testAdClientsList - */ - public function testCustomChannelsGet() - { - $adClients = $this->adsense->adclients->listAdclients(); - foreach ($adClients['items'] as $adClient) { - $customChannels = $this->adsense->customchannels->listCustomchannels($adClient['id']); - if (array_key_exists('items', $customChannels)) { - $customChannel = $this->getRandomElementFromArray($customChannels['items']); - $this->checkCustomChannelElement($customChannel); - break 1; - } - } - } - - /** - * @depends testAdClientsList - * @depends testCustomChannelsList - */ - public function testCustomChannelsAdUnitsList() - { - $adClients = $this->adsense->adclients->listAdclients(); - foreach ($adClients['items'] as $adClient) { - $customChannels = $this->adsense->customchannels->listCustomchannels($adClient['id']); - if (array_key_exists('items', $customChannels)) { - foreach ($customChannels['items'] as $customChannel) { - $adUnits = - $this->adsense->customchannels_adunits->listCustomchannelsAdunits( - $adClient['id'], - $customChannel['id'] - ); - $this->checkAdUnitsCollection($adUnits); - break 2; - } - } - } - } - - /** - * @depends testAdClientsList - */ - public function testUrlChannelsList() - { - $adClients = $this->adsense->adclients->listAdclients(); - foreach ($adClients['items'] as $adClient) { - $urlChannels = $this->adsense->urlchannels->listUrlchannels($adClient['id']); - $this->checkUrlChannelsCollection($urlChannels); - } - } - - public function testReportsGenerate() - { - if (!$this->checkToken()) { - return; - } - $startDate = '2011-01-01'; - $endDate = '2011-01-31'; - $optParams = $this->getReportOptParams(); - $report = $this->adsense->reports->generate($startDate, $endDate, $optParams); - $this->checkReport($report); - } - - private function checkAccountElement($account) - { - $this->assertArrayHasKey('kind', $account); - $this->assertArrayHasKey('id', $account); - $this->assertArrayHasKey('name', $account); - } - - private function checkAdClientsCollection($adClients) - { - $this->assertArrayHasKey('kind', $adClients); - $this->assertEquals($adClients['kind'], 'adsense#adClients'); - foreach ($adClients['items'] as $adClient) { - $this->assertArrayHasKey('id', $adClient); - $this->assertArrayHasKey('kind', $adClient); - $this->assertArrayHasKey('productCode', $adClient); - $this->assertArrayHasKey('supportsReporting', $adClient); - } - } - - private function checkAdUnitsCollection($adUnits) - { - $this->assertArrayHasKey('kind', $adUnits); - $this->assertEquals($adUnits['kind'], 'adsense#adUnits'); - if (array_key_exists('items', $adUnits)) { - foreach ($adUnits['items'] as $adUnit) { - $this->checkAdUnitElement($adUnit); - } - } - } - - private function checkAdUnitElement($adUnit) - { - $this->assertArrayHasKey('code', $adUnit); - $this->assertArrayHasKey('id', $adUnit); - $this->assertArrayHasKey('kind', $adUnit); - $this->assertArrayHasKey('name', $adUnit); - $this->assertArrayHasKey('status', $adUnit); - } - - private function checkCustomChannelsCollection($customChannels) - { - $this->assertArrayHasKey('kind', $customChannels); - $this->assertEquals($customChannels['kind'], 'adsense#customChannels'); - if (array_key_exists('items', $customChannels)) { - foreach ($customChannels['items'] as $customChannel) { - $this->checkCustomChannelElement($customChannel); - } - } - } - - private function checkCustomChannelElement($customChannel) - { - $this->assertArrayHasKey('kind', $customChannel); - $this->assertArrayHasKey('id', $customChannel); - $this->assertArrayHasKey('code', $customChannel); - $this->assertArrayHasKey('name', $customChannel); - } - - private function checkUrlChannelsCollection($urlChannels) - { - $this->assertArrayHasKey('kind', $urlChannels); - $this->assertEquals($urlChannels['kind'], 'adsense#urlChannels'); - if (array_key_exists('items', $urlChannels)) { - foreach ($urlChannels['items'] as $urlChannel) { - $this->assertArrayHasKey('kind', $urlChannel); - $this->assertArrayHasKey('id', $urlChannel); - $this->assertArrayHasKey('urlPattern', $urlChannel); - } - } - } - - private function getReportOptParams() - { - return array( - 'metric' => array('PAGE_VIEWS', 'AD_REQUESTS'), - 'dimension' => array ('DATE', 'AD_CLIENT_ID'), - 'sort' => array('DATE'), - 'filter' => array('COUNTRY_NAME==United States'), - ); - } - - private function checkReport($report) - { - $this->assertArrayHasKey('kind', $report); - $this->assertEquals($report['kind'], 'adsense#report'); - $this->assertArrayHasKey('totalMatchedRows', $report); - $this->assertGreaterThan(0, count($report->headers)); - foreach ($report['headers'] as $header) { - $this->assertArrayHasKey('name', $header); - $this->assertArrayHasKey('type', $header); - } - if (array_key_exists('items', $report)) { - foreach ($report['items'] as $row) { - $this->assertCount(4, $row); - } - } - $this->assertArrayHasKey('totals', $report); - $this->assertArrayHasKey('averages', $report); - } - - private function getRandomElementFromArray($array) - { - $elementKey = array_rand($array); - return $array[$elementKey]; - } -} diff --git a/tests/Google/Service/PagespeedonlineTest.php b/tests/Google/Service/PagespeedonlineTest.php index e63a8780c..4ebc1837a 100644 --- a/tests/Google/Service/PagespeedonlineTest.php +++ b/tests/Google/Service/PagespeedonlineTest.php @@ -25,10 +25,5 @@ public function testPageSpeed() $result = $psapi->runpagespeed('http://code.google.com/'); $this->assertArrayHasKey('kind', $result); $this->assertArrayHasKey('id', $result); - $this->assertArrayHasKey('responseCode', $result); - $this->assertArrayHasKey('title', $result); - $this->assertArrayHasKey('score', $result->ruleGroups['SPEED']); - $this->assertInstanceOf('Google_Service_Pagespeedonline_ResultPageStats', $result->pageStats); - $this->assertArrayHasKey('minor', $result['version']); } } diff --git a/tests/Google/Service/PlusTest.php b/tests/Google/Service/PlusTest.php deleted file mode 100644 index c3e62a249..000000000 --- a/tests/Google/Service/PlusTest.php +++ /dev/null @@ -1,69 +0,0 @@ -checkToken(); - $this->plus = new Google_Service_Plus($this->getClient()); - } - - public function testGetPerson() - { - $person = $this->plus->people->get("118051310819094153327"); - $this->assertArrayHasKey('kind', $person); - $this->assertArrayHasKey('displayName', $person); - $this->assertArrayHasKey('gender', $person); - $this->assertArrayHasKey('id', $person); - } - - public function testListActivities() - { - $activities = $this->plus->activities - ->listActivities("118051310819094153327", "public"); - - $this->assertArrayHasKey('kind', $activities); - $this->assertGreaterThan(0, count($activities)); - - // Test a variety of access methods. - $this->assertItem($activities['items'][0]); - $this->assertItem($activities[0]); - foreach ($activities as $item) { - $this->assertItem($item); - break; - } - - // Test deeper type transformations - $this->assertGreaterThan(0, strlen($activities[0]->actor->displayName)); - } - - public function assertItem($item) - { - // assertArrayHasKey uses array_key_exists, which is not great: - // it doesn't understand SPL ArrayAccess - $this->assertArrayHasKey('actor', $item); - $this->assertInstanceOf('Google_Service_Plus_ActivityActor', $item->actor); - $this->assertTrue(isset($item['actor']['displayName'])); - $this->assertTrue(isset($item['actor']->url)); - $this->assertArrayHasKey('object', $item); - $this->assertArrayHasKey('access', $item); - $this->assertArrayHasKey('provider', $item); - } -} diff --git a/tests/Google/Service/UrlshortenerTest.php b/tests/Google/Service/UrlshortenerTest.php deleted file mode 100644 index 9eacd0e35..000000000 --- a/tests/Google/Service/UrlshortenerTest.php +++ /dev/null @@ -1,44 +0,0 @@ -checkKey(); - - $service = new Google_Service_Urlshortener($this->getClient()); - $url = new Google_Service_Urlshortener_Url(); - $url->longUrl = "http://google.com/"; - - $shortUrl = $service->url->insert($url); - $this->assertEquals('urlshortener#url', $shortUrl['kind']); - $this->assertEquals('http://google.com/', $shortUrl['longUrl']); - } - - public function testEmptyJsonResponse() - { - $this->checkKey(); - - $service = new Google_Service_Urlshortener($this->getClient()); - - $optParams = array('fields' => ''); - $resp = $service->url->get('http://goo.gl/KkHq8', $optParams); - - $this->assertEquals("", $resp->longUrl); - } -} diff --git a/tests/examples/batchTest.php b/tests/examples/batchTest.php index 1db795925..212defdf7 100644 --- a/tests/examples/batchTest.php +++ b/tests/examples/batchTest.php @@ -29,7 +29,7 @@ public function testBatch() $nodes = $crawler->filter('br'); $this->assertCount(20, $nodes); - $this->assertContains('Life of Henry David Thoreau', $crawler->text()); + $this->assertContains('Walden', $crawler->text()); $this->assertContains('George Bernard Shaw His Life and Works', $crawler->text()); } } diff --git a/tests/examples/serviceAccountTest.php b/tests/examples/serviceAccountTest.php index 261059d8a..81521c149 100644 --- a/tests/examples/serviceAccountTest.php +++ b/tests/examples/serviceAccountTest.php @@ -29,6 +29,6 @@ public function testServiceAccount() $nodes = $crawler->filter('br'); $this->assertCount(10, $nodes); - $this->assertContains('Life of Henry David Thoreau', $crawler->text()); + $this->assertContains('Walden', $crawler->text()); } } From 71ce14ec3389e415b72867c8a15619a741956c14 Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Thu, 23 Jul 2020 14:06:33 -0400 Subject: [PATCH 137/301] test: fix batch tests (#1889) --- tests/Google/Http/BatchTest.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/Google/Http/BatchTest.php b/tests/Google/Http/BatchTest.php index 8445a21ea..ed3f25f63 100644 --- a/tests/Google/Http/BatchTest.php +++ b/tests/Google/Http/BatchTest.php @@ -44,11 +44,11 @@ public function testBatchRequestWithAuth() public function testBatchRequest() { + $this->checkKey(); $client = $this->getClient(); - $batch = new Google_Http_Batch($client); $plus = new Google_Service_Plus($client); + $batch = $plus->createBatch(); - $client->setUseBatch(true); $batch->add($plus->people->get('+LarryPage'), 'key1'); $batch->add($plus->people->get('+LarryPage'), 'key2'); $batch->add($plus->people->get('+LarryPage'), 'key3'); @@ -61,11 +61,11 @@ public function testBatchRequest() public function testBatchRequestWithBooksApi() { + $this->checkKey(); $client = $this->getClient(); - $batch = new Google_Http_Batch($client); $plus = new Google_Service_Plus($client); + $batch = $plus->createBatch(); - $client->setUseBatch(true); $batch->add($plus->people->get('+LarryPage'), 'key1'); $batch->add($plus->people->get('+LarryPage'), 'key2'); $batch->add($plus->people->get('+LarryPage'), 'key3'); @@ -81,8 +81,9 @@ public function testBatchRequestWithPostBody() $this->checkToken(); $client = $this->getClient(); - $batch = new Google_Http_Batch($client); $shortener = new Google_Service_Urlshortener($client); + $batch = $shortener->createBatch(); + $url1 = new Google_Service_Urlshortener_Url; $url2 = new Google_Service_Urlshortener_Url; $url3 = new Google_Service_Urlshortener_Url; @@ -90,7 +91,6 @@ public function testBatchRequestWithPostBody() $url2->setLongUrl('http://morehazards.com/'); $url3->setLongUrl('http://github.com/bshaffer'); - $client->setUseBatch(true); $batch->add($shortener->url->insert($url1), 'key1'); $batch->add($shortener->url->insert($url2), 'key2'); $batch->add($shortener->url->insert($url3), 'key3'); @@ -103,11 +103,12 @@ public function testBatchRequestWithPostBody() public function testInvalidBatchRequest() { + $this->checkKey(); $client = $this->getClient(); - $batch = new Google_Http_Batch($client); $plus = new Google_Service_Plus($client); - $client->setUseBatch(true); + $batch = $plus->createBatch(); + $batch->add($plus->people->get('123456789987654321'), 'key1'); $batch->add($plus->people->get('+LarryPage'), 'key2'); From 4a2591197dd9da23ac20cca0aad61dd0f3a6079c Mon Sep 17 00:00:00 2001 From: davidbrnovjak <36956973+davidbrnovjak@users.noreply.github.com> Date: Thu, 23 Jul 2020 20:11:07 +0200 Subject: [PATCH 138/301] docs: fix invalid parameter type-hint for nextChunk (#1888) Co-authored-by: John Pedrie --- src/Google/Http/MediaFileUpload.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Http/MediaFileUpload.php b/src/Google/Http/MediaFileUpload.php index 7a0f4e0bb..d868a9986 100644 --- a/src/Google/Http/MediaFileUpload.php +++ b/src/Google/Http/MediaFileUpload.php @@ -114,7 +114,7 @@ public function getProgress() /** * Send the next part of the file to upload. - * @param bool $chunk Optional. The next set of bytes to send. If false will + * @param string|bool $chunk Optional. The next set of bytes to send. If false will * use $data passed at construct time. */ public function nextChunk($chunk = false) From 173923ba0e94257dfa73491ca0a1b8ddc949dd09 Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Thu, 23 Jul 2020 15:27:17 -0400 Subject: [PATCH 139/301] chore: update test matrix (#1890) --- .github/actions/unittest/entrypoint.sh | 21 -------- .github/actions/unittest/retry.php | 24 --------- .github/workflows/tests.yml | 74 +++++++++++++++++--------- 3 files changed, 50 insertions(+), 69 deletions(-) delete mode 100755 .github/actions/unittest/entrypoint.sh delete mode 100644 .github/actions/unittest/retry.php diff --git a/.github/actions/unittest/entrypoint.sh b/.github/actions/unittest/entrypoint.sh deleted file mode 100755 index e66f52674..000000000 --- a/.github/actions/unittest/entrypoint.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/sh -l - -apt-get update && \ -apt-get install -y --no-install-recommends \ - git \ - zip \ - curl \ - unzip \ - wget - -curl --silent --show-error https://getcomposer.org/installer | php -php composer.phar self-update - -echo "---Installing dependencies ---" -echo "Removing cache/filesystem-adapter for PHP 5.4" -bash -c "if [[ $(php -r 'echo PHP_VERSION;') =~ \"5.4\" ]]; then php composer.phar remove --dev cache/filesystem-adapter; fi" -echo ${composerargs} -php $(dirname $0)/retry.php "php composer.phar update $composerargs" - -echo "---Running unit tests ---" -vendor/bin/phpunit diff --git a/.github/actions/unittest/retry.php b/.github/actions/unittest/retry.php deleted file mode 100644 index c6525abe8..000000000 --- a/.github/actions/unittest/retry.php +++ /dev/null @@ -1,24 +0,0 @@ - 0) { - sleep($delay); - return retry($f, $delay, $retries - 1); - } else { - throw $e; - } - } -} - -retry(function () { - global $argv; - passthru($argv[1], $ret); - - if ($ret != 0) { - throw new \Exception('err'); - } -}, 1); diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ba68dfd2b..cbd2c316b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -27,39 +27,65 @@ jobs: command: composer install - name: Run Script run: vendor/bin/phpunit - # use dockerfiles for oooooolllllldddd versions of php, setup-php times out for those. + test_php55: - name: "PHP 5.5 Unit Test" runs-on: ubuntu-latest + name: PHP 5.5 Unit Test steps: - - name: Checkout - uses: actions/checkout@v2 - - name: Run Unit Tests - uses: docker://php:5.5-cli - with: - entrypoint: ./.github/actions/unittest/entrypoint.sh + - uses: actions/checkout@v2 + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 5.5 + - name: Install Dependencies + uses: nick-invision/retry@v1 + with: + timeout_minutes: 10 + max_attempts: 3 + command: composer install + - name: Run Script + run: vendor/bin/phpunit + test_php54: - name: "PHP 5.4 Unit Test" runs-on: ubuntu-latest + name: PHP 5.4 Unit Test steps: - - name: Checkout - uses: actions/checkout@v2 - - name: Run Unit Tests - uses: docker://php:5.4-cli - with: - entrypoint: ./.github/actions/unittest/entrypoint.sh + - uses: actions/checkout@v2 + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 5.4 + - name: Remove cache library + run: composer remove --dev cache/filesystem-adapter + - name: Install Dependencies + uses: nick-invision/retry@v1 + with: + timeout_minutes: 10 + max_attempts: 3 + command: composer update + - name: Run Script + run: vendor/bin/phpunit + test_php54_lowest: - name: "PHP 5.4 Unit Test Prefer Lowest" runs-on: ubuntu-latest + name: "PHP 5.4 Unit Test (with `--prefer-lowest`)" steps: - - name: Checkout - uses: actions/checkout@v2 - - name: Run Unit Tests - uses: docker://php:5.4-cli - env: - composerargs: "--prefer-lowest" - with: - entrypoint: ./.github/actions/unittest/entrypoint.sh + - uses: actions/checkout@v2 + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 5.4 + - name: Remove cache library + run: composer remove --dev cache/filesystem-adapter + - name: Install Dependencies + uses: nick-invision/retry@v1 + with: + timeout_minutes: 10 + max_attempts: 3 + command: composer update --prefer-lowest + - name: Run Script + run: vendor/bin/phpunit + style: runs-on: ubuntu-latest name: PHP Style Check From 7b57cd74d54896d8f65600c43c0d057d25eff822 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 23 Jul 2020 12:33:43 -0700 Subject: [PATCH 140/301] feat: composer cleanup task (#1877) --- README.md | 55 +++++++- composer.json | 3 +- src/Google/Task/Composer.php | 104 +++++++++++++++ tests/Google/Task/ComposerTest.php | 206 +++++++++++++++++++++++++++++ 4 files changed, 366 insertions(+), 2 deletions(-) create mode 100644 src/Google/Task/Composer.php create mode 100644 tests/Google/Task/ComposerTest.php diff --git a/README.md b/README.md index 0cc92f599..a26a0f41b 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ composer installed. Once composer is installed, execute the following command in your project root to install this library: ```sh -composer require google/apiclient:"^2.0" +composer require google/apiclient:"^2.7" ``` Finally, be sure to include the autoloader: @@ -43,6 +43,59 @@ require_once '/path/to/your-project/vendor/autoload.php'; This library relies on `google/apiclient-services`. That library provides up-to-date API wrappers for a large number of Google APIs. In order that users may make use of the latest API clients, this library does not pin to a specific version of `google/apiclient-services`. **In order to prevent the accidental installation of API wrappers with breaking changes**, it is highly recommended that you pin to the [latest version](https://github.com/googleapis/google-api-php-client-services/releases) yourself prior to using this library in production. +#### Cleaning up unused services + +There are over 200 Google API services. The chances are good that you will not +want them all. In order to avoid shipping these dependencies with your code, +you can run the `Google_Task_Composer::cleanup` task and specify the services +you want to keep in `composer.json`: + +```json +{ + "require": { + "google/apiclient": "^2.7" + }, + "scripts": { + "post-update-cmd": "Google_Task_Composer::cleanup" + }, + "extra": { + "google/apiclient-services": [ + "Drive", + "YouTube" + ] + } +} +``` + +This example will remove all services other than "Drive" and "YouTube" when +`composer update` or a fresh `composer install` is run. + +**IMPORTANT**: If you add any services back in `composer.json`, you will need to +remove the `vendor/google/apiclient-services` directory explicity for the +change you made to have effect: + +```sh +rm -r vendor/google/apiclient-services +composer update +``` + +**NOTE**: This command performs an exact match on the service name, so to keep +`YouTubeReporting` and `YouTubeAnalytics` as well, you'd need to add each of +them explicitly: + +```json +{ + "extra": { + "google/apiclient-services": [ + "Drive", + "YouTube", + "YouTubeAnalytics", + "YouTubeReporting" + ] + } +} +``` + ### Download the Release If you prefer not to use composer, you can download the package in its entirety. The [Releases](https://github.com/googleapis/google-api-php-client/releases) page lists all stable versions. Download any file diff --git a/composer.json b/composer.json index e75ac4255..e0caa3113 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,8 @@ "symfony/css-selector": "~2.1", "cache/filesystem-adapter": "^0.3.2", "phpcompatibility/php-compatibility": "^9.2", - "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0" + "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0", + "composer/composer": "^1.10" }, "suggest": { "cache/filesystem-adapter": "For caching certs and tokens (using Google_Client::setCache)" diff --git a/src/Google/Task/Composer.php b/src/Google/Task/Composer.php new file mode 100644 index 000000000..865fc7af4 --- /dev/null +++ b/src/Google/Task/Composer.php @@ -0,0 +1,104 @@ +getComposer(); + $extra = $composer->getPackage()->getExtra(); + $servicesToKeep = isset($extra['google/apiclient-services']) ? + $extra['google/apiclient-services'] : []; + if ($servicesToKeep) { + $serviceDir = sprintf( + '%s/google/apiclient-services/src/Google/Service', + $composer->getConfig()->get('vendor-dir') + ); + self::verifyServicesToKeep($serviceDir, $servicesToKeep); + $finder = self::getServicesToRemove($serviceDir, $servicesToKeep); + $filesystem = $filesystem ?: new Filesystem(); + if (0 !== $count = count($finder)) { + $event->getIO()->write( + sprintf( + 'Removing %s google services', + $count + ) + ); + foreach ($finder as $file) { + $realpath = $file->getRealPath(); + $filesystem->remove($realpath); + $filesystem->remove($realpath . '.php'); + } + } + } + } + + /** + * @throws InvalidArgumentException when the service doesn't exist + */ + private static function verifyServicesToKeep( + $serviceDir, + array $servicesToKeep + ) { + $finder = (new Finder()) + ->directories() + ->depth('== 0'); + + foreach ($servicesToKeep as $service) { + if (!preg_match('/^[a-zA-Z0-9]*$/', $service)) { + throw new \InvalidArgumentException( + sprintf( + 'Invalid Google service name "%s"', + $service + ) + ); + } + try { + $finder->in($serviceDir . '/' . $service); + } catch (\InvalidArgumentException $e) { + throw new \InvalidArgumentException( + sprintf( + 'Google service "%s" does not exist or was removed previously', + $service + ) + ); + } + } + } + + private static function getServicesToRemove( + $serviceDir, + array $servicesToKeep + ) { + // find all files in the current directory + return (new Finder()) + ->directories() + ->depth('== 0') + ->in($serviceDir) + ->exclude($servicesToKeep); + } +} diff --git a/tests/Google/Task/ComposerTest.php b/tests/Google/Task/ComposerTest.php new file mode 100644 index 000000000..63b36e1bd --- /dev/null +++ b/tests/Google/Task/ComposerTest.php @@ -0,0 +1,206 @@ +createMockEvent(['Foo'])); + } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage Invalid Google service name "../YouTube" + */ + public function testRelatePathServiceName() + { + Google_Task_Composer::cleanup($this->createMockEvent(['../YouTube'])); + } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage Google service "" does not exist + */ + public function testEmptyServiceName() + { + Google_Task_Composer::cleanup($this->createMockEvent([''])); + } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage Invalid Google service name "YouTube*" + */ + public function testWildcardServiceName() + { + Google_Task_Composer::cleanup($this->createMockEvent(['YouTube*'])); + } + + public function testRemoveServices() + { + $vendorDir = sys_get_temp_dir() . '/rand-' . rand(); + $serviceDir = sprintf( + '%s/google/apiclient-services/src/Google/Service/', + $vendorDir + ); + $dirs = [ + 'ServiceToKeep', + 'ServiceToDelete1', + 'ServiceToDelete2', + ]; + $files = [ + 'ServiceToKeep/ServiceFoo.php', + 'ServiceToKeep.php', + 'SomeRandomFile.txt', + 'ServiceToDelete1/ServiceFoo.php', + 'ServiceToDelete1.php', + 'ServiceToDelete2/ServiceFoo.php', + 'ServiceToDelete2.php', + ]; + foreach ($dirs as $dir) { + @mkdir($serviceDir . $dir, 0777, true); + } + foreach ($files as $file) { + touch($serviceDir . $file); + } + $print = 'Removing 2 google services'; + Google_Task_Composer::cleanup( + $this->createMockEvent(['ServiceToKeep'], $vendorDir, $print), + $this->createMockFilesystem([ + 'ServiceToDelete2', + 'ServiceToDelete2.php', + 'ServiceToDelete1', + 'ServiceToDelete1.php', + ], $serviceDir) + ); + } + + private function createMockFilesystem(array $files, $serviceDir) + { + $mockFilesystem = $this->prophesize('Symfony\Component\Filesystem\Filesystem'); + foreach ($files as $filename) { + $file = new \SplFileInfo($serviceDir . $filename); + $mockFilesystem->remove($file->getRealPath()) + ->shouldBeCalledTimes(1); + } + + return $mockFilesystem->reveal(); + } + + private function createMockEvent( + array $servicesToKeep, + $vendorDir = '', + $print = null + ) { + $mockPackage = $this->prophesize('Composer\Package\RootPackage'); + $mockPackage->getExtra() + ->shouldBeCalledTimes(1) + ->willReturn(['google/apiclient-services' => $servicesToKeep]); + + $mockConfig = $this->prophesize('Composer\Config'); + $mockConfig->get('vendor-dir') + ->shouldBeCalledTimes(1) + ->willReturn($vendorDir); + + $mockComposer = $this->prophesize('Composer\Composer'); + $mockComposer->getPackage() + ->shouldBeCalledTimes(1) + ->willReturn($mockPackage->reveal()); + $mockComposer->getConfig() + ->shouldBeCalledTimes(1) + ->willReturn($mockConfig->reveal()); + + $mockEvent = $this->prophesize('Composer\Script\Event'); + $mockEvent->getComposer() + ->shouldBeCalledTimes(1) + ->willReturn($mockComposer); + + if ($print) { + $mockIO = $this->prophesize('Composer\IO\ConsoleIO'); + $mockIO->write($print) + ->shouldBeCalledTimes(1); + + $mockEvent->getIO() + ->shouldBeCalledTimes(1) + ->willReturn($mockIO->reveal()); + } + + return $mockEvent->reveal(); + } + + public function testE2E() + { + $composerJson = json_encode([ + 'require' => [ + 'google/apiclient' => 'dev-add-composer-cleanup' + ], + 'scripts' => [ + 'post-update-cmd' => 'Google_Task_Composer::cleanup' + ], + 'extra' => [ + 'google/apiclient-services' => [ + 'Drive', + 'YouTube' + ] + ] + ]); + + $tmpDir = sys_get_temp_dir() . '/test-' . rand(); + $serviceDir = $tmpDir . '/vendor/google/apiclient-services/src/Google/Service'; + + mkdir($tmpDir); + file_put_contents($tmpDir . '/composer.json', $composerJson); + passthru('composer install -d ' . $tmpDir); + + $this->assertFileExists($serviceDir . '/Drive.php'); + $this->assertFileExists($serviceDir . '/Drive'); + $this->assertFileExists($serviceDir . '/YouTube.php'); + $this->assertFileExists($serviceDir . '/YouTube'); + $this->assertFileNotExists($serviceDir . '/YouTubeReporting.php'); + $this->assertFileNotExists($serviceDir . '/YouTubeReporting'); + + $composerJson = json_encode([ + 'require' => [ + 'google/apiclient' => 'dev-add-composer-cleanup' + ], + 'scripts' => [ + 'post-update-cmd' => 'Google_Task_Composer::cleanup' + ], + 'extra' => [ + 'google/apiclient-services' => [ + 'Drive', + 'YouTube', + 'YouTubeReporting', + ] + ] + ]); + + file_put_contents($tmpDir . '/composer.json', $composerJson); + passthru('rm -r ' . $tmpDir . '/vendor/google/apiclient-services'); + passthru('composer update -d ' . $tmpDir); + + $this->assertFileExists($serviceDir . '/Drive.php'); + $this->assertFileExists($serviceDir . '/Drive'); + $this->assertFileExists($serviceDir . '/YouTube.php'); + $this->assertFileExists($serviceDir . '/YouTube'); + $this->assertFileExists($serviceDir . '/YouTubeReporting.php'); + $this->assertFileExists($serviceDir . '/YouTubeReporting'); + } +} From e03170a37fbe05612d0ef4c90e9637cb1bc696fa Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Thu, 23 Jul 2020 15:41:33 -0400 Subject: [PATCH 141/301] chore: fix release asset generator (#1886) --- .github/workflows/asset-release.yml | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/.github/workflows/asset-release.yml b/.github/workflows/asset-release.yml index 9a1c1c73b..b4efbcbf7 100644 --- a/.github/workflows/asset-release.yml +++ b/.github/workflows/asset-release.yml @@ -44,14 +44,13 @@ jobs: - name: Create Archive run: | zip -r ${fileName} . && - zip -d ${fileName} ".git*" && - zip -d ${fileName} "tests*" && - zip -d ${fileName} "docs*" && - zip -d ${fileName} ".travis.yml" && - zip -d ${fileName} "phpcs.xml.dist" && - zip -d ${fileName} "phpunit.xml.dist" && - zip -d ${fileName} "tests*" && - zip -d ${fileName} "examples*" + zip -d ${fileName} ".git*" || true && + zip -d ${fileName} "tests*" || true && + zip -d ${fileName} "docs*" || true && + zip -d ${fileName} "phpcs.xml.dist" || true && + zip -d ${fileName} "phpunit.xml.dist" || true && + zip -d ${fileName} "tests*" || true && + zip -d ${fileName} "examples*" || true env: fileName: google-api-php-client-${{ steps.tagName.outputs.tag }}-PHP${{ matrix.php }}.zip From 5e7f50d15eae697aaf803a54d0ca6c7e4c5ddf0d Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 23 Jul 2020 14:19:32 -0700 Subject: [PATCH 142/301] test: add adsense test back (#1876) --- tests/Google/Service/AdSenseTest.php | 487 +++++++++++++++++++++++++++ 1 file changed, 487 insertions(+) create mode 100644 tests/Google/Service/AdSenseTest.php diff --git a/tests/Google/Service/AdSenseTest.php b/tests/Google/Service/AdSenseTest.php new file mode 100644 index 000000000..14dbb3b1e --- /dev/null +++ b/tests/Google/Service/AdSenseTest.php @@ -0,0 +1,487 @@ +checkToken(); + $this->adsense = new Google_Service_AdSense($this->getClient()); + } + + public function testAccountsList() + { + $accounts = $this->adsense->accounts->listAccounts(); + $this->assertArrayHasKey('kind', $accounts); + $this->assertEquals($accounts['kind'], 'adsense#accounts'); + $account = $this->getRandomElementFromArray($accounts['items']); + $this->checkAccountElement($account); + } + + /** + * @depends testAccountsList + */ + public function testAccountsGet() + { + $accounts = $this->adsense->accounts->listAccounts(); + $account = $this->getRandomElementFromArray($accounts['items']); + $retrievedAccount = $this->adsense->accounts->get($account['id']); + $this->checkAccountElement($retrievedAccount); + } + + /** + * @depends testAccountsList + */ + public function testAccountsReportGenerate() + { + $startDate = '2011-01-01'; + $endDate = '2011-01-31'; + $optParams = $this->getReportOptParams(); + $accounts = $this->adsense->accounts->listAccounts(); + $accountId = $accounts['items'][0]['id']; + $report = $this->adsense->accounts_reports->generate( + $accountId, + $startDate, + $endDate, + $optParams + ); + $this->checkReport($report); + } + + /** + * @depends testAccountsList + */ + public function testAccountsAdClientsList() + { + $accounts = $this->adsense->accounts->listAccounts(); + $account = $this->getRandomElementFromArray($accounts['items']); + $adClients = + $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); + $this->checkAdClientsCollection($adClients); + } + + /** + * @depends testAccountsList + * @depends testAccountsAdClientsList + */ + public function testAccountsAdUnitsList() + { + $accounts = $this->adsense->accounts->listAccounts(); + foreach ($accounts['items'] as $account) { + $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); + foreach ($adClients['items'] as $adClient) { + $adUnits = $this->adsense->accounts_adunits->listAccountsAdunits( + $account['id'], + $adClient['id'] + ); + $this->checkAdUnitsCollection($adUnits); + break 2; + } + } + } + + /** + * @depends testAccountsList + * @depends testAccountsAdClientsList + */ + public function testAccountsAdUnitsGet() + { + $accounts = $this->adsense->accounts->listAccounts(); + foreach ($accounts['items'] as $account) { + $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); + foreach ($adClients['items'] as $adClient) { + $adUnits = $this->adsense->accounts_adunits->listAccountsAdunits( + $account['id'], + $adClient['id'] + ); + if (array_key_exists('items', $adUnits)) { + $adUnit = $this->getRandomElementFromArray($adUnits['items']); + $this->checkAdUnitElement($adUnit); + break 2; + } + } + } + } + + /** + * @depends testAccountsList + * @depends testAccountsAdClientsList + */ + public function testAccountsCustomChannelsList() + { + $accounts = $this->adsense->accounts->listAccounts(); + foreach ($accounts['items'] as $account) { + $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); + foreach ($adClients['items'] as $adClient) { + $customChannels = $this->adsense->accounts_customchannels + ->listAccountsCustomchannels($account['id'], $adClient['id']); + $this->checkCustomChannelsCollection($customChannels); + break 2; + } + } + } + + /** + * @depends testAccountsList + * @depends testAccountsAdClientsList + */ + public function testAccountsCustomChannelsGet() + { + $accounts = $this->adsense->accounts->listAccounts(); + foreach ($accounts['items'] as $account) { + $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); + foreach ($adClients['items'] as $adClient) { + $customChannels = + $this->adsense->accounts_customchannels->listAccountsCustomchannels( + $account['id'], + $adClient['id'] + ); + if (array_key_exists('items', $customChannels)) { + $customChannel = + $this->getRandomElementFromArray($customChannels['items']); + $this->checkCustomChannelElement($customChannel); + break 2; + } + } + } + } + + /** + * @depends testAccountsList + * @depends testAccountsAdClientsList + */ + public function testAccountsUrlChannelsList() + { + $accounts = $this->adsense->accounts->listAccounts(); + foreach ($accounts['items'] as $account) { + $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); + foreach ($adClients['items'] as $adClient) { + $urlChannels = + $this->adsense->accounts_urlchannels->listAccountsUrlchannels( + $account['id'], + $adClient['id'] + ); + $this->checkUrlChannelsCollection($urlChannels); + break 2; + } + } + } + + /** + * @depends testAccountsList + * @depends testAccountsAdClientsList + * @depends testAccountsAdUnitsList + */ + public function testAccountsAdUnitsCustomChannelsList() + { + $accounts = $this->adsense->accounts->listAccounts(); + foreach ($accounts['items'] as $account) { + $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); + foreach ($adClients['items'] as $adClient) { + $adUnits = + $this->adsense->accounts_adunits->listAccountsAdunits($account['id'], $adClient['id']); + if (array_key_exists('items', $adUnits)) { + foreach ($adUnits['items'] as $adUnit) { + $customChannels = + $this->adsense->accounts_adunits_customchannels->listAccountsAdunitsCustomchannels( + $account['id'], + $adClient['id'], + $adUnit['id'] + ); + $this->checkCustomChannelsCollection($customChannels); + // it's too expensive to go through each, if one is correct good + break 3; + } + } + } + } + } + + /** + * @depends testAccountsList + * @depends testAccountsAdClientsList + * @depends testAccountsCustomChannelsList + */ + public function testAccountsCustomChannelsAdUnitsList() + { + $accounts = $this->adsense->accounts->listAccounts(); + foreach ($accounts['items'] as $account) { + $adClients = + $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); + foreach ($adClients['items'] as $adClient) { + $customChannels = + $this->adsense->accounts_customchannels->listAccountsCustomchannels( + $account['id'], + $adClient['id'] + ); + if (array_key_exists('items', $customChannels)) { + foreach ($customChannels['items'] as $customChannel) { + $adUnits = + $this->adsense->accounts_customchannels_adunits->listAccountsCustomchannelsAdunits( + $account['id'], + $adClient['id'], + $customChannel['id'] + ); + $this->checkAdUnitsCollection($adUnits); + // it's too expensive to go through each, if one is correct good + break 3; + } + } + } + } + } + + public function testAdClientsList() + { + $adClients = $this->adsense->adclients->listAdclients(); + $this->checkAdClientsCollection($adClients); + } + + /** + * @depends testAdClientsList + */ + public function testAdUnitsList() + { + $adClients = $this->adsense->adclients->listAdclients(); + foreach ($adClients['items'] as $adClient) { + $adUnits = $this->adsense->adunits->listAdunits($adClient['id']); + $this->checkAdUnitsCollection($adUnits); + } + } + + /** + * @depends testAdClientsList + */ + public function testAdUnitsGet() + { + $adClients = $this->adsense->adclients->listAdclients(); + foreach ($adClients['items'] as $adClient) { + $adUnits = $this->adsense->adunits->listAdunits($adClient['id']); + if (array_key_exists('items', $adUnits)) { + $adUnit = $this->getRandomElementFromArray($adUnits['items']); + $this->checkAdUnitElement($adUnit); + break 1; + } + } + } + + /** + * @depends testAdClientsList + * @depends testAdUnitsList + */ + public function testAdUnitsCustomChannelsList() + { + $adClients = $this->adsense->adclients->listAdclients(); + foreach ($adClients['items'] as $adClient) { + $adUnits = $this->adsense->adunits->listAdunits($adClient['id']); + if (array_key_exists('items', $adUnits)) { + foreach ($adUnits['items'] as $adUnit) { + $customChannels = + $this->adsense->adunits_customchannels->listAdunitsCustomchannels( + $adClient['id'], + $adUnit['id'] + ); + $this->checkCustomChannelsCollection($customChannels); + break 2; + } + } + } + } + + /** + * @depends testAdClientsList + */ + public function testCustomChannelsList() + { + $adClients = $this->adsense->adclients->listAdclients(); + foreach ($adClients['items'] as $adClient) { + $customChannels = + $this->adsense->customchannels->listCustomchannels($adClient['id']); + $this->checkCustomChannelsCollection($customChannels); + } + } + + /** + * @depends testAdClientsList + */ + public function testCustomChannelsGet() + { + $adClients = $this->adsense->adclients->listAdclients(); + foreach ($adClients['items'] as $adClient) { + $customChannels = $this->adsense->customchannels->listCustomchannels($adClient['id']); + if (array_key_exists('items', $customChannels)) { + $customChannel = $this->getRandomElementFromArray($customChannels['items']); + $this->checkCustomChannelElement($customChannel); + break 1; + } + } + } + + /** + * @depends testAdClientsList + * @depends testCustomChannelsList + */ + public function testCustomChannelsAdUnitsList() + { + $adClients = $this->adsense->adclients->listAdclients(); + foreach ($adClients['items'] as $adClient) { + $customChannels = $this->adsense->customchannels->listCustomchannels($adClient['id']); + if (array_key_exists('items', $customChannels)) { + foreach ($customChannels['items'] as $customChannel) { + $adUnits = + $this->adsense->customchannels_adunits->listCustomchannelsAdunits( + $adClient['id'], + $customChannel['id'] + ); + $this->checkAdUnitsCollection($adUnits); + break 2; + } + } + } + } + + /** + * @depends testAdClientsList + */ + public function testUrlChannelsList() + { + $adClients = $this->adsense->adclients->listAdclients(); + foreach ($adClients['items'] as $adClient) { + $urlChannels = $this->adsense->urlchannels->listUrlchannels($adClient['id']); + $this->checkUrlChannelsCollection($urlChannels); + } + } + + public function testReportsGenerate() + { + if (!$this->checkToken()) { + return; + } + $startDate = '2011-01-01'; + $endDate = '2011-01-31'; + $optParams = $this->getReportOptParams(); + $report = $this->adsense->reports->generate($startDate, $endDate, $optParams); + $this->checkReport($report); + } + + private function checkAccountElement($account) + { + $this->assertArrayHasKey('kind', $account); + $this->assertArrayHasKey('id', $account); + $this->assertArrayHasKey('name', $account); + } + + private function checkAdClientsCollection($adClients) + { + $this->assertArrayHasKey('kind', $adClients); + $this->assertEquals($adClients['kind'], 'adsense#adClients'); + foreach ($adClients['items'] as $adClient) { + $this->assertArrayHasKey('id', $adClient); + $this->assertArrayHasKey('kind', $adClient); + $this->assertArrayHasKey('productCode', $adClient); + $this->assertArrayHasKey('supportsReporting', $adClient); + } + } + + private function checkAdUnitsCollection($adUnits) + { + $this->assertArrayHasKey('kind', $adUnits); + $this->assertEquals($adUnits['kind'], 'adsense#adUnits'); + if (array_key_exists('items', $adUnits)) { + foreach ($adUnits['items'] as $adUnit) { + $this->checkAdUnitElement($adUnit); + } + } + } + + private function checkAdUnitElement($adUnit) + { + $this->assertArrayHasKey('code', $adUnit); + $this->assertArrayHasKey('id', $adUnit); + $this->assertArrayHasKey('kind', $adUnit); + $this->assertArrayHasKey('name', $adUnit); + $this->assertArrayHasKey('status', $adUnit); + } + + private function checkCustomChannelsCollection($customChannels) + { + $this->assertArrayHasKey('kind', $customChannels); + $this->assertEquals($customChannels['kind'], 'adsense#customChannels'); + if (array_key_exists('items', $customChannels)) { + foreach ($customChannels['items'] as $customChannel) { + $this->checkCustomChannelElement($customChannel); + } + } + } + + private function checkCustomChannelElement($customChannel) + { + $this->assertArrayHasKey('kind', $customChannel); + $this->assertArrayHasKey('id', $customChannel); + $this->assertArrayHasKey('code', $customChannel); + $this->assertArrayHasKey('name', $customChannel); + } + + private function checkUrlChannelsCollection($urlChannels) + { + $this->assertArrayHasKey('kind', $urlChannels); + $this->assertEquals($urlChannels['kind'], 'adsense#urlChannels'); + if (array_key_exists('items', $urlChannels)) { + foreach ($urlChannels['items'] as $urlChannel) { + $this->assertArrayHasKey('kind', $urlChannel); + $this->assertArrayHasKey('id', $urlChannel); + $this->assertArrayHasKey('urlPattern', $urlChannel); + } + } + } + + private function getReportOptParams() + { + return array( + 'metric' => array('PAGE_VIEWS', 'AD_REQUESTS'), + 'dimension' => array ('DATE', 'AD_CLIENT_ID'), + 'sort' => array('DATE'), + 'filter' => array('COUNTRY_NAME==United States'), + ); + } + + private function checkReport($report) + { + $this->assertArrayHasKey('kind', $report); + $this->assertEquals($report['kind'], 'adsense#report'); + $this->assertArrayHasKey('totalMatchedRows', $report); + $this->assertGreaterThan(0, count($report->headers)); + foreach ($report['headers'] as $header) { + $this->assertArrayHasKey('name', $header); + $this->assertArrayHasKey('type', $header); + } + if (array_key_exists('items', $report)) { + foreach ($report['items'] as $row) { + $this->assertCount(4, $row); + } + } + $this->assertArrayHasKey('totals', $report); + $this->assertArrayHasKey('averages', $report); + } + + private function getRandomElementFromArray($array) + { + $elementKey = array_rand($array); + return $array[$elementKey]; + } +} From 074daa249365e2401164cadb4f88a25fa65db063 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 23 Jul 2020 14:30:24 -0700 Subject: [PATCH 143/301] chore: add comment for batching example (#1891) --- examples/batch.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/examples/batch.php b/examples/batch.php index 23cf9856f..ea663d8c3 100644 --- a/examples/batch.php +++ b/examples/batch.php @@ -58,7 +58,11 @@ want to execute with keys of our choice - these keys will be reflected in the returned array. ************************************************/ + +// NOTE: Some services use `$service->createBatch();` instead of +// `new Google_Http_Batch($client);` $batch = new Google_Http_Batch($client); + $optParams = array('filter' => 'free-ebooks'); $optParams['q'] = 'Henry David Thoreau'; $req1 = $service->volumes->listVolumes($optParams); From 48ec94577b51bde415270116118b07a294e07c43 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 23 Jul 2020 14:37:43 -0700 Subject: [PATCH 144/301] chore: increment LIBVER constant (#1892) --- src/Google/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index 763e474e4..b687805e1 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -38,7 +38,7 @@ */ class Google_Client { - const LIBVER = "2.5.0"; + const LIBVER = "2.7.0"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; From e959a9d66d1eb9d88c4c35eac6ea367699e94574 Mon Sep 17 00:00:00 2001 From: David Supplee Date: Thu, 20 Aug 2020 16:51:46 -0400 Subject: [PATCH 145/301] docs: update example (#1897) --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a26a0f41b..d732797d2 100644 --- a/README.md +++ b/README.md @@ -131,8 +131,11 @@ $client->setApplicationName("Client_Library_Examples"); $client->setDeveloperKey("YOUR_APP_KEY"); $service = new Google_Service_Books($client); -$optParams = array('filter' => 'free-ebooks'); -$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams); +$optParams = array( + 'filter' => 'free-ebooks', + 'q' => 'Henry David Thoreau' +); +$results = $service->volumes->listVolumes($optParams); foreach ($results->getItems() as $item) { echo $item['volumeInfo']['title'], "
      \n"; From 5546728695cd0ee0ad23acca653f73ccd6c680d8 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 27 Aug 2020 11:51:28 -0600 Subject: [PATCH 146/301] test: composer cleanup task branch (#1921) --- tests/Google/Task/ComposerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Google/Task/ComposerTest.php b/tests/Google/Task/ComposerTest.php index 63b36e1bd..2c37f3194 100644 --- a/tests/Google/Task/ComposerTest.php +++ b/tests/Google/Task/ComposerTest.php @@ -149,7 +149,7 @@ public function testE2E() { $composerJson = json_encode([ 'require' => [ - 'google/apiclient' => 'dev-add-composer-cleanup' + 'google/apiclient' => 'dev-master' ], 'scripts' => [ 'post-update-cmd' => 'Google_Task_Composer::cleanup' @@ -178,7 +178,7 @@ public function testE2E() $composerJson = json_encode([ 'require' => [ - 'google/apiclient' => 'dev-add-composer-cleanup' + 'google/apiclient' => 'dev-master' ], 'scripts' => [ 'post-update-cmd' => 'Google_Task_Composer::cleanup' From 9ffad2ab8f97bc66d6e2db2b013b909e8e9fa207 Mon Sep 17 00:00:00 2001 From: David Supplee Date: Thu, 3 Sep 2020 08:29:24 -0700 Subject: [PATCH 147/301] fix: use quota_project_id over quota_project (#1914) --- src/Google/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index b687805e1..573669fe8 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -1169,7 +1169,7 @@ private function createApplicationDefaultCredentials() 'client_email' => $this->config['client_email'], 'private_key' => $signingKey, 'type' => 'service_account', - 'quota_project' => $this->config['quota_project'], + 'quota_project_id' => $this->config['quota_project'], ); $credentials = CredentialsLoader::makeCredentials( $scopes, From 6686ee7b7b99ae453859e4f7c1d09872865bf4ac Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 3 Sep 2020 14:45:38 -0600 Subject: [PATCH 148/301] fix: ensure in-memory access token is updated when refresh token is used (#1926) --- src/Google/Client.php | 12 ++++++++++++ tests/Google/ClientTest.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/Google/Client.php b/src/Google/Client.php index 573669fe8..be4af2390 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -169,6 +169,18 @@ public function __construct(array $config = array()) $this->setScopes($this->config['scopes']); unset($this->config['scopes']); } + + // Set a default token callback to update the in-memory access token + if (is_null($this->config['token_callback'])) { + $this->config['token_callback'] = function ($cacheKey, $newAccessToken) { + $this->setAccessToken( + [ + 'access_token' => $newAccessToken, + 'created' => time(), + ] + ); + }; + } } /** diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 870510f65..fa489f90f 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -714,6 +714,36 @@ public function testTokenCallback() $this->assertTrue($called); } + public function testDefaultTokenCallback() + { + $this->onlyPhp55AndAbove(); + $this->checkToken(); + + $client = $this->getClient(); + $accessToken = $client->getAccessToken(); + + if (!isset($accessToken['refresh_token'])) { + $this->markTestSkipped('Refresh Token required'); + } + + // make the auth library think the token is expired + $accessToken['expires_in'] = 0; + $client->setAccessToken($accessToken); + + // make a silly request to obtain a new token (it's ok if it fails) + $http = $client->authorize(); + try { + $http->get('https://www.googleapis.com/books/v1/volumes?q=Voltaire'); + } catch (Exception $e) {} + + // Assert the in-memory token has been updated + $newToken = $client->getAccessToken(); + $this->assertNotEquals( + $accessToken['access_token'], + $newToken['access_token'] + ); + } + public function testExecuteWithFormat() { $this->onlyGuzzle6(); From e748d1d5a51166754f13809d35f1fa162cbec530 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 8 Sep 2020 10:38:08 -0600 Subject: [PATCH 149/301] chore: prepare v2.7.1 (#1927) --- src/Google/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index be4af2390..f33558f77 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -38,7 +38,7 @@ */ class Google_Client { - const LIBVER = "2.7.0"; + const LIBVER = "2.7.1"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; From c33e4e8b3ce6d8219b6ea3b34536213bca3c82fe Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 11 Sep 2020 15:33:42 -0600 Subject: [PATCH 150/301] fix: create dirs for docs generator (#1930) --- .github/actions/docs/entrypoint.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/actions/docs/entrypoint.sh b/.github/actions/docs/entrypoint.sh index af8227fd3..6f9895c2b 100755 --- a/.github/actions/docs/entrypoint.sh +++ b/.github/actions/docs/entrypoint.sh @@ -7,6 +7,10 @@ git reset --hard HEAD # Required so sami.php is available for previous versions cp .github/actions/docs/sami.php.dist .github/actions/docs/sami.php +# Create the directories +mkdir .docs +mkdir .cache + # Run the docs generation command php vendor/bin/sami.php update .github/actions/docs/sami.php From 8ad15a898e26017520e31f396a020244d0b82b50 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 14 Sep 2020 15:28:19 -0600 Subject: [PATCH 151/301] tests: allow guzzle7 (#1939) --- tests/BaseTest.php | 7 +++++++ tests/Google/AccessToken/RevokeTest.php | 4 ++-- tests/Google/ClientTest.php | 6 +++--- tests/Google/Service/ResourceTest.php | 2 +- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 36d613b75..4e51e8bb4 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -254,6 +254,13 @@ public function onlyGuzzle5() } } + public function onlyGuzzle6Or7() + { + if (!$this->isGuzzle6() && !$this->isGuzzle7()) { + $this->markTestSkipped('Guzzle 6 or 7 only'); + } + } + protected function getGuzzle5ResponseMock() { $response = $this->prophesize('GuzzleHttp\Message\ResponseInterface'); diff --git a/tests/Google/AccessToken/RevokeTest.php b/tests/Google/AccessToken/RevokeTest.php index 26461da38..b2972ee74 100644 --- a/tests/Google/AccessToken/RevokeTest.php +++ b/tests/Google/AccessToken/RevokeTest.php @@ -100,9 +100,9 @@ public function testRevokeAccessGuzzle5() $this->assertEquals($accessToken, $token); } - public function testRevokeAccessGuzzle6() + public function testRevokeAccessGuzzle6Or7() { - $this->onlyGuzzle6(); + $this->onlyGuzzle6Or7(); $accessToken = 'ACCESS_TOKEN'; $refreshToken = 'REFRESH_TOKEN'; diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index fa489f90f..3440102a3 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -194,7 +194,7 @@ public function testNoAuthIsNull() public function testPrepareService() { - $this->onlyGuzzle6(); + $this->onlyGuzzle6Or7(); $client = new Google_Client(); $client->setScopes(array("scope1", "scope2")); @@ -746,7 +746,7 @@ public function testDefaultTokenCallback() public function testExecuteWithFormat() { - $this->onlyGuzzle6(); + $this->onlyGuzzle6Or7(); $client = new Google_Client([ 'api_format_v2' => true @@ -768,7 +768,7 @@ public function testExecuteWithFormat() public function testExecuteSetsCorrectHeaders() { - $this->onlyGuzzle6(); + $this->onlyGuzzle6Or7(); $client = new Google_Client(); diff --git a/tests/Google/Service/ResourceTest.php b/tests/Google/Service/ResourceTest.php index df08569df..74cdc711d 100644 --- a/tests/Google/Service/ResourceTest.php +++ b/tests/Google/Service/ResourceTest.php @@ -374,7 +374,7 @@ public function testErrorResponseWithVeryLongBody() public function testSuccessResponseWithVeryLongBody() { - $this->onlyGuzzle6(); + $this->onlyGuzzle6Or7(); // set the "alt" parameter to "media" $arguments = [['alt' => 'media']]; From 215f8da53b06c520ad46ea8c903ecf305f418358 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 14 Sep 2020 15:39:02 -0600 Subject: [PATCH 152/301] fix: removes old autoload warning from v1 (#1938) --- src/Google/Collection.php | 4 ---- src/Google/autoload.php | 21 --------------------- 2 files changed, 25 deletions(-) delete mode 100644 src/Google/autoload.php diff --git a/src/Google/Collection.php b/src/Google/Collection.php index df8d444d3..ce8a2aa3d 100644 --- a/src/Google/Collection.php +++ b/src/Google/Collection.php @@ -1,9 +1,5 @@ Date: Tue, 15 Sep 2020 13:22:27 -0600 Subject: [PATCH 153/301] fix: make docs generation identical to google/auth, which works (#1933) --- .github/actions/docs/entrypoint.sh | 13 ------------- .github/actions/docs/sami.php | 2 +- .github/actions/docs/sami.php.dist | 29 ----------------------------- .github/workflows/docs.yml | 6 ++++++ .gitignore | 1 - 5 files changed, 7 insertions(+), 44 deletions(-) delete mode 100644 .github/actions/docs/sami.php.dist diff --git a/.github/actions/docs/entrypoint.sh b/.github/actions/docs/entrypoint.sh index 6f9895c2b..4d9c79fb4 100755 --- a/.github/actions/docs/entrypoint.sh +++ b/.github/actions/docs/entrypoint.sh @@ -4,22 +4,9 @@ apt-get update apt-get install -y git git reset --hard HEAD -# Required so sami.php is available for previous versions -cp .github/actions/docs/sami.php.dist .github/actions/docs/sami.php - # Create the directories mkdir .docs mkdir .cache # Run the docs generation command php vendor/bin/sami.php update .github/actions/docs/sami.php - -cd ./.docs - -git init -git config user.name "GitHub Actions" -git config user.email "actions@github.com" - -git add . -git commit -m "Updating docs" -git push -q https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/${GITHUB_REPOSITORY} HEAD:gh-pages --force diff --git a/.github/actions/docs/sami.php b/.github/actions/docs/sami.php index bf44e3ee8..b607837df 100644 --- a/.github/actions/docs/sami.php +++ b/.github/actions/docs/sami.php @@ -5,7 +5,7 @@ use Sami\Version\GitVersionCollection; use Symfony\Component\Finder\Finder; -$projectRoot = __DIR__ . '/../../..'; +$projectRoot = realpath(__DIR__ . '/../../..'); $iterator = Finder::create() ->files() diff --git a/.github/actions/docs/sami.php.dist b/.github/actions/docs/sami.php.dist deleted file mode 100644 index bf44e3ee8..000000000 --- a/.github/actions/docs/sami.php.dist +++ /dev/null @@ -1,29 +0,0 @@ -files() - ->name('*.php') - ->exclude('vendor') - ->exclude('tests') - ->in($projectRoot); - -$versions = GitVersionCollection::create($projectRoot) - ->addFromTags(function($tag) { - return 0 === strpos($tag, 'v2.') && false === strpos($tag, 'RC'); - }) - ->add('master', 'master branch'); - -return new Sami($iterator, [ - 'title' => 'Google APIs Client Library for PHP API Reference', - 'build_dir' => $projectRoot . '/.docs/%version%', - 'cache_dir' => $projectRoot . '/.cache/%version%', - 'remote_repository' => new GitHubRemoteRepository('googleapis/google-api-php-client', $projectRoot), - 'versions' => $versions -]); diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index cf3f13e46..8992af044 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -26,3 +26,9 @@ jobs: GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} with: entrypoint: ./.github/actions/docs/entrypoint.sh + - name: Deploy 🚀 + uses: JamesIves/github-pages-deploy-action@releases/v3 + with: + ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }} + BRANCH: gh-pages + FOLDER: .docs diff --git a/.gitignore b/.gitignore index 837e0b5be..9b429eae6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ .DS_Store -.github/actions/docs/sami.php phpunit.xml phpcs.xml composer.lock From 752850bdd9f6595e7b520a23030e97d52bbc4b13 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 16 Sep 2020 07:52:03 -0600 Subject: [PATCH 154/301] docs: adds reference docs link (#1941) --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index d732797d2..1274cdf2e 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,11 @@ # Google APIs Client Library for PHP # +
      +
      Reference Docs
      https://googleapis.github.io/google-api-php-client/master/
      +
      License
      Apache 2.0
      +
      + The Google API Client Library enables you to work with Google APIs such as Gmail, Drive or YouTube on your server. These client libraries are officially supported by Google. However, the libraries are considered complete and are in maintenance mode. This means that we will address critical bugs and security issues but will not add any new features. From f441bcf0315ba8d81ed233eec6927e397240ee81 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 18 Sep 2020 13:46:26 -0600 Subject: [PATCH 155/301] fix: add expires_in to default token callback (#1932) --- src/Google/Client.php | 1 + tests/Google/ClientTest.php | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index f33558f77..ee70dd48b 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -176,6 +176,7 @@ public function __construct(array $config = array()) $this->setAccessToken( [ 'access_token' => $newAccessToken, + 'expires_in' => 3600, // Google default 'created' => time(), ] ); diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 3440102a3..3652b3ca6 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -702,9 +702,11 @@ public function testTokenCallback() // set the token callback to the client $client->setTokenCallback($callback); - // make a silly request to obtain a new token + // make a silly request to obtain a new token (it's ok if it fails) $http = $client->authorize(); - $http->get('https://www.googleapis.com/books/v1/volumes?q=Voltaire'); + try { + $http->get('https://www.googleapis.com/books/v1/volumes?q=Voltaire'); + } catch (Exception $e) {} $newToken = $client->getAccessToken(); // go back to the previous cache @@ -742,6 +744,8 @@ public function testDefaultTokenCallback() $accessToken['access_token'], $newToken['access_token'] ); + + $this->assertFalse($client->isAccessTokenExpired()); } public function testExecuteWithFormat() From 9df720d72c59456b5466e3f66e1e78cfe422a5ba Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 18 Sep 2020 14:02:04 -0600 Subject: [PATCH 156/301] chore: prepare v2.7.2 (#1946) --- src/Google/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/Client.php b/src/Google/Client.php index ee70dd48b..2e3d48a79 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -38,7 +38,7 @@ */ class Google_Client { - const LIBVER = "2.7.1"; + const LIBVER = "2.7.2"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; From 61517efe678b3c20fe806e76d506204261ca2863 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 23 Sep 2020 11:45:23 -0600 Subject: [PATCH 157/301] feat: allow array/null config constructors for services (#1929) --- src/Google/Service.php | 17 +++++++++-- tests/Google/ServiceTest.php | 56 ++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/Google/Service.php b/src/Google/Service.php index d3fd3b49d..43979428b 100644 --- a/src/Google/Service.php +++ b/src/Google/Service.php @@ -25,9 +25,22 @@ class Google_Service public $resource; private $client; - public function __construct(Google_Client $client) + /** + * @param Google_Client|array $clientOrConfig Optional + */ + public function __construct($clientOrConfig = []) { - $this->client = $client; + if ($clientOrConfig instanceof Google_Client) { + $this->client = $clientOrConfig; + } elseif (is_array($clientOrConfig)) { + $this->client = new Google_Client($clientOrConfig ?: []); + } else { + $errorMessage = 'constructor must be array or instance of Google_Client'; + if (class_exists('TypeError')) { + throw new TypeError($errorMessage); + } + trigger_error($errorMessage, E_USER_ERROR); + } } /** diff --git a/tests/Google/ServiceTest.php b/tests/Google/ServiceTest.php index ec89bb011..b1130b65f 100644 --- a/tests/Google/ServiceTest.php +++ b/tests/Google/ServiceTest.php @@ -121,4 +121,60 @@ public function serviceProvider() return $classes; } + + public function testConfigConstructor() + { + $clientId = 'test-client-id'; + $service = new TestService(['client_id' => $clientId]); + $this->assertEquals($clientId, $service->getClient()->getClientId()); + } + + public function testNoConstructor() + { + $service = new TestService(); + $this->assertInstanceOf('Google_Client', $service->getClient()); + } + + public function testInvalidConstructorPhp7Plus() + { + if (!class_exists('TypeError')) { + $this->markTestSkipped('PHP 7+ only'); + } + + try { + $service = new TestService('foo'); + } catch (TypeError $e) {} + + $this->assertInstanceOf('TypeError', $e); + $this->assertEquals( + 'constructor must be array or instance of Google_Client', + $e->getMessage() + ); + } + + private static $errorMessage; + + /** @runInSeparateProcess */ + public function testInvalidConstructorPhp5() + { + if (class_exists('TypeError')) { + $this->markTestSkipped('PHP 5 only'); + } + + set_error_handler('Google_ServiceTest::handlePhp5Error'); + + $service = new TestService('foo'); + + $this->assertEquals( + 'constructor must be array or instance of Google_Client', + self::$errorMessage + ); + } + + public static function handlePhp5Error($errno, $errstr, $errfile, $errline) + { + self::assertEquals(E_USER_ERROR, $errno); + self::$errorMessage = $errstr; + return true; + } } From d0dcc7054faad3cdda5844e15103a59984969744 Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Wed, 30 Sep 2020 10:19:56 -0700 Subject: [PATCH 158/301] chore: add CODEOWNERS and repo settings config (#1956) --- .github/CODEOWNERS | 7 +++++++ .github/sync-repo-settings.yaml | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 .github/CODEOWNERS create mode 100644 .github/sync-repo-settings.yaml diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 000000000..c20bc3167 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,7 @@ +# Code owners file. +# This file controls who is tagged for review for any given pull request. +# +# For syntax help see: +# https://help.github.com/en/github/creating-cloning-and-archiving-repositories/about-code-owners#codeowners-syntax + +* @googleapis/yoshi-php diff --git a/.github/sync-repo-settings.yaml b/.github/sync-repo-settings.yaml new file mode 100644 index 000000000..c2b2689b5 --- /dev/null +++ b/.github/sync-repo-settings.yaml @@ -0,0 +1,26 @@ +rebaseMergeAllowed: true +squashMergeAllowed: true +mergeCommitAllowed: false +branchProtectionRules: +- pattern: master + isAdminEnforced: true + requiredStatusCheckContexts: + - 'PHP 5.4 Unit Test' + - 'PHP 5.4 Unit Test (with `--prefer-lowest`)' + - 'PHP 5.5 Unit Test' + - 'PHP 5.6 Unit Test' + - 'PHP 7.0 Unit Test' + - 'PHP 7.1 Unit Test' + - 'PHP 7.2 Unit Test' + - 'PHP 7.3 Unit Test' + - 'PHP 7.4 Unit Test' + - 'PHP Style Check' + - 'cla/google' + requiredApprovingReviewCount: 1 + requiresCodeOwnerReviews: true + requiresStrictStatusChecks: true +permissionRules: + - team: yoshi-php-admins + permission: admin + - team: yoshi-php + permission: push From e6d64bef20317adfbb0a8bf8a09352b8d87a2e09 Mon Sep 17 00:00:00 2001 From: sylvain-msl <56234755+sylvain-msl@users.noreply.github.com> Date: Tue, 20 Oct 2020 22:06:44 +0200 Subject: [PATCH 159/301] fix: exception option fallback is http_errors on guzzle 6 and 7 (#1966) --- src/Google/AuthHandler/Guzzle6AuthHandler.php | 2 +- src/Google/Client.php | 8 +++++--- tests/Google/ClientTest.php | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Google/AuthHandler/Guzzle6AuthHandler.php b/src/Google/AuthHandler/Guzzle6AuthHandler.php index d1c16e6fc..43e213896 100644 --- a/src/Google/AuthHandler/Guzzle6AuthHandler.php +++ b/src/Google/AuthHandler/Guzzle6AuthHandler.php @@ -97,7 +97,7 @@ private function createAuthHttp(ClientInterface $http) return new Client( [ 'base_uri' => $http->getConfig('base_uri'), - 'exceptions' => true, + 'http_errors' => true, 'verify' => $http->getConfig('verify'), 'proxy' => $http->getConfig('proxy'), ] diff --git a/src/Google/Client.php b/src/Google/Client.php index 2e3d48a79..d7f4848f4 100644 --- a/src/Google/Client.php +++ b/src/Google/Client.php @@ -1148,11 +1148,10 @@ protected function createDefaultHttpClient() $guzzleVersion = (int)substr(ClientInterface::VERSION, 0, 1); } - $options = ['exceptions' => false]; if (5 === $guzzleVersion) { $options = [ 'base_url' => $this->config['base_path'], - 'defaults' => $options, + 'defaults' => ['exceptions' => false], ]; if ($this->isAppEngine()) { // set StreamHandler on AppEngine by default @@ -1161,7 +1160,10 @@ protected function createDefaultHttpClient() } } elseif (6 === $guzzleVersion || 7 === $guzzleVersion) { // guzzle 6 or 7 - $options['base_uri'] = $this->config['base_path']; + $options = [ + 'base_uri' => $this->config['base_path'], + 'http_errors' => false, + ]; } else { throw new LogicException('Could not find supported version of Guzzle.'); } diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 3652b3ca6..fe3b69c51 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -301,6 +301,21 @@ public function testSettersGetters() $this->assertEquals($token, $client->getAccessToken()); } + public function testDefaultConfigOptions() + { + $client = new Google_Client(); + if ($this->isGuzzle6() || $this->isGuzzle7()) { + $this->assertArrayHasKey('http_errors', $client->getHttpClient()->getConfig()); + $this->assertArrayNotHasKey('exceptions', $client->getHttpClient()->getConfig()); + $this->assertFalse($client->getHttpClient()->getConfig()['http_errors']); + } + if ($this->isGuzzle5()) { + $this->assertArrayHasKey('exceptions', $client->getHttpClient()->getDefaultOption()); + $this->assertArrayNotHasKey('http_errors', $client->getHttpClient()->getDefaultOption()); + $this->assertFalse($client->getHttpClient()->getDefaultOption()['exceptions']); + } + } + public function testAppEngineStreamHandlerConfig() { $this->onlyGuzzle5(); From 4d3932ecaef5329ebf6eb952334669317d187079 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 20 Oct 2020 13:27:37 -0700 Subject: [PATCH 160/301] feat: rename namespaces and provide aliases (#1937) --- composer.json | 8 ++-- src/{Google => }/AccessToken/Revoke.php | 7 ++- src/{Google => }/AccessToken/Verify.php | 18 ++++++-- .../AuthHandler/AuthHandlerFactory.php | 11 +++-- .../AuthHandler/Guzzle5AuthHandler.php | 4 +- .../AuthHandler/Guzzle6AuthHandler.php | 4 +- .../AuthHandler/Guzzle7AuthHandler.php | 4 +- src/{Google => }/Client.php | 46 +++++++++++-------- src/{Google => }/Collection.php | 6 ++- src/{Google => }/Exception.php | 6 ++- src/{Google => }/Http/Batch.php | 19 +++++--- src/{Google => }/Http/MediaFileUpload.php | 19 +++++--- src/{Google => }/Http/REST.php | 21 +++++---- src/{Google => }/Model.php | 17 +++++-- src/{Google => }/Service.php | 22 +++++---- src/{Google => }/Service/Exception.php | 6 ++- src/{Google => }/Service/README.md | 0 src/{Google => }/Service/Resource.php | 26 +++++++---- src/{Google => }/Task/Composer.php | 11 +++-- src/{Google => }/Task/Exception.php | 6 ++- src/{Google => }/Task/Retryable.php | 4 +- src/{Google => }/Task/Runner.php | 25 ++++++---- src/{Google => }/Utils/UriTemplate.php | 4 +- src/aliases.php | 29 ++++++++++++ tests/Google/ServiceTest.php | 26 +---------- tests/examples/batchTest.php | 2 +- 26 files changed, 221 insertions(+), 130 deletions(-) rename src/{Google => }/AccessToken/Revoke.php (95%) rename src/{Google => }/AccessToken/Verify.php (95%) rename src/{Google => }/AuthHandler/AuthHandlerFactory.php (83%) rename src/{Google => }/AuthHandler/Guzzle5AuthHandler.php (97%) rename src/{Google => }/AuthHandler/Guzzle6AuthHandler.php (98%) rename src/{Google => }/AuthHandler/Guzzle7AuthHandler.php (87%) rename src/{Google => }/Client.php (97%) rename src/{Google => }/Collection.php (93%) rename src/{Google => }/Exception.php (87%) rename src/{Google => }/Http/Batch.php (94%) rename src/{Google => }/Http/MediaFileUpload.php (96%) rename src/{Google => }/Http/REST.php (91%) rename src/{Google => }/Model.php (96%) rename src/{Google => }/Service.php (81%) rename src/{Google => }/Service/Exception.php (94%) rename src/{Google => }/Service/README.md (100%) rename src/{Google => }/Service/Resource.php (93%) rename src/{Google => }/Task/Composer.php (93%) rename src/{Google => }/Task/Exception.php (85%) rename src/{Google => }/Task/Retryable.php (94%) rename src/{Google => }/Task/Runner.php (92%) rename src/{Google => }/Utils/UriTemplate.php (99%) create mode 100644 src/aliases.php diff --git a/composer.json b/composer.json index e0caa3113..43a8ac01f 100644 --- a/composer.json +++ b/composer.json @@ -29,11 +29,11 @@ "cache/filesystem-adapter": "For caching certs and tokens (using Google_Client::setCache)" }, "autoload": { - "psr-0": { - "Google_": "src/" + "psr-4": { + "Google\\": "src/" }, - "classmap": [ - "src/Google/Service/" + "files": [ + "src/aliases.php" ] }, "extra": { diff --git a/src/Google/AccessToken/Revoke.php b/src/AccessToken/Revoke.php similarity index 95% rename from src/Google/AccessToken/Revoke.php rename to src/AccessToken/Revoke.php index 29eb3fb36..d2d272454 100644 --- a/src/Google/AccessToken/Revoke.php +++ b/src/AccessToken/Revoke.php @@ -16,7 +16,10 @@ * limitations under the License. */ +namespace Google\AccessToken; + use Google\Auth\HttpHandler\HttpHandlerFactory; +use Google\Client; use GuzzleHttp\ClientInterface; use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\Request; @@ -25,7 +28,7 @@ * Wrapper around Google Access Tokens which provides convenience functions * */ -class Google_AccessToken_Revoke +class Revoke { /** * @var GuzzleHttp\ClientInterface The http client @@ -61,7 +64,7 @@ public function revokeToken($token) $body = Psr7\stream_for(http_build_query(array('token' => $token))); $request = new Request( 'POST', - Google_Client::OAUTH2_REVOKE_URI, + Client::OAUTH2_REVOKE_URI, [ 'Cache-Control' => 'no-store', 'Content-Type' => 'application/x-www-form-urlencoded', diff --git a/src/Google/AccessToken/Verify.php b/src/AccessToken/Verify.php similarity index 95% rename from src/Google/AccessToken/Verify.php rename to src/AccessToken/Verify.php index e8067c5a1..c9ddf8541 100644 --- a/src/Google/AccessToken/Verify.php +++ b/src/AccessToken/Verify.php @@ -16,20 +16,28 @@ * limitations under the License. */ +namespace Google\AccessToken; + use Firebase\JWT\ExpiredException as ExpiredExceptionV3; use Firebase\JWT\SignatureInvalidException; use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; use Psr\Cache\CacheItemPoolInterface; use Google\Auth\Cache\MemoryCacheItemPool; +use Google\Exception as GoogleException; use Stash\Driver\FileSystem; use Stash\Pool; +use DateTime; +use DomainException; +use Exception; +use ExpiredException; // Firebase v2 +use LogicException; /** * Wrapper around Google Access Tokens which provides convenience functions * */ -class Google_AccessToken_Verify +class Verify { const FEDERATED_SIGNON_CERT_URL = 'https://www.googleapis.com/oauth2/v3/certs'; const OAUTH2_ISSUER = 'accounts.google.com'; @@ -141,7 +149,7 @@ private function getCache() * Retrieve and cache a certificates file. * * @param $url string location - * @throws Google_Exception + * @throws GoogleException * @return array certificates */ private function retrieveCertsFromLocation($url) @@ -149,7 +157,7 @@ private function retrieveCertsFromLocation($url) // If we're retrieving a local file, just grab it. if (0 !== strpos($url, 'http')) { if (!$file = file_get_contents($url)) { - throw new Google_Exception( + throw new GoogleException( "Failed to retrieve verification certificates: '" . $url . "'." ); @@ -163,7 +171,7 @@ private function retrieveCertsFromLocation($url) if ($response->getStatusCode() == 200) { return json_decode((string) $response->getBody(), true); } - throw new Google_Exception( + throw new GoogleException( sprintf( 'Failed to retrieve verification certificates: "%s".', $response->getBody()->getContents() @@ -249,7 +257,7 @@ private function getOpenSslConstant() return 'CRYPT_RSA_MODE_OPENSSL'; } - throw new \Exception('Cannot find RSA class'); + throw new Exception('Cannot find RSA class'); } /** diff --git a/src/Google/AuthHandler/AuthHandlerFactory.php b/src/AuthHandler/AuthHandlerFactory.php similarity index 83% rename from src/Google/AuthHandler/AuthHandlerFactory.php rename to src/AuthHandler/AuthHandlerFactory.php index 1a15f7a89..1ae9d8a9e 100644 --- a/src/Google/AuthHandler/AuthHandlerFactory.php +++ b/src/AuthHandler/AuthHandlerFactory.php @@ -15,10 +15,13 @@ * limitations under the License. */ +namespace Google\AuthHandler; + use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; +use Exception; -class Google_AuthHandler_AuthHandlerFactory +class AuthHandlerFactory { /** * Builds out a default http handler for the installed version of guzzle. @@ -37,11 +40,11 @@ public static function build($cache = null, array $cacheConfig = []) switch ($guzzleVersion) { case 5: - return new Google_AuthHandler_Guzzle5AuthHandler($cache, $cacheConfig); + return new Guzzle5AuthHandler($cache, $cacheConfig); case 6: - return new Google_AuthHandler_Guzzle6AuthHandler($cache, $cacheConfig); + return new Guzzle6AuthHandler($cache, $cacheConfig); case 7: - return new Google_AuthHandler_Guzzle7AuthHandler($cache, $cacheConfig); + return new Guzzle7AuthHandler($cache, $cacheConfig); default: throw new Exception('Version not supported'); } diff --git a/src/Google/AuthHandler/Guzzle5AuthHandler.php b/src/AuthHandler/Guzzle5AuthHandler.php similarity index 97% rename from src/Google/AuthHandler/Guzzle5AuthHandler.php rename to src/AuthHandler/Guzzle5AuthHandler.php index 4e4a8d68f..92d226821 100644 --- a/src/Google/AuthHandler/Guzzle5AuthHandler.php +++ b/src/AuthHandler/Guzzle5AuthHandler.php @@ -1,5 +1,7 @@ '', // Path to JSON credentials or an array representing those credentials - // @see Google_Client::setAuthConfig + // @see Google\Client::setAuthConfig 'credentials' => null, - // @see Google_Client::setScopes + // @see Google\Client::setScopes 'scopes' => null, // Sets X-Goog-User-Project, which specifies a user project to bill // for access charges associated with the request @@ -138,7 +148,7 @@ public function __construct(array $config = array()) 'approval_prompt' => 'auto', // Task Runner retry configuration - // @see Google_Task_Runner + // @see Google\Task\Runner 'retry' => array(), 'retry_map' => null, @@ -149,7 +159,7 @@ public function __construct(array $config = array()) // follows the signature function ($cacheKey, $accessToken) 'token_callback' => null, - // Service class used in Google_Client::verifyIdToken. + // Service class used in Google\Client::verifyIdToken. // Explicitly pass this in to avoid setting JWT::$leeway 'jwt' => null, @@ -256,9 +266,9 @@ public function fetchAccessTokenWithAssertion(ClientInterface $authHttp = null) if (!$this->isUsingApplicationDefaultCredentials()) { throw new DomainException( 'set the JSON service account credentials using' - . ' Google_Client::setAuthConfig or set the path to your JSON file' + . ' Google\Client::setAuthConfig or set the path to your JSON file' . ' with the "GOOGLE_APPLICATION_CREDENTIALS" environment variable' - . ' and call Google_Client::useApplicationDefaultCredentials to' + . ' and call Google\Client::useApplicationDefaultCredentials to' . ' refresh a token with assertion.' ); } @@ -734,9 +744,7 @@ public function setTokenCallback(callable $tokenCallback) */ public function revokeToken($token = null) { - $tokenRevoker = new Google_AccessToken_Revoke( - $this->getHttpClient() - ); + $tokenRevoker = new Revoke($this->getHttpClient()); return $tokenRevoker->revokeToken($token ?: $this->getAccessToken()); } @@ -753,7 +761,7 @@ public function revokeToken($token = null) */ public function verifyIdToken($idToken = null) { - $tokenVerifier = new Google_AccessToken_Verify( + $tokenVerifier = new Verify( $this->getHttpClient(), $this->getCache(), $this->config['jwt'] @@ -834,9 +842,9 @@ public function prepareScopes() /** * Helper method to execute deferred HTTP requests. * - * @param $request Psr\Http\Message\RequestInterface|Google_Http_Batch + * @param $request Psr\Http\Message\RequestInterface|Google\Http\Batch * @param string $expectedClass - * @throws Google_Exception + * @throws Google\Exception * @return object of the type of the expected class or Psr\Http\Message\ResponseInterface. */ public function execute(RequestInterface $request, $expectedClass = null) @@ -871,7 +879,7 @@ public function execute(RequestInterface $request, $expectedClass = null) // this is where most of the grunt work is done $http = $this->authorize(); - return Google_Http_REST::execute( + return REST::execute( $http, $request, $expectedClass, @@ -918,7 +926,7 @@ public function getConfig($name, $default = null) * alias for setAuthConfig * * @param string $file the configuration file - * @throws Google_Exception + * @throws Google\Exception * @deprecated */ public function setAuthConfigFile($file) @@ -932,7 +940,7 @@ public function setAuthConfigFile($file) * the "Download JSON" button on in the Google Developer * Console. * @param string|array $config the configuration json - * @throws Google_Exception + * @throws Google\Exception */ public function setAuthConfig($config) { @@ -1168,7 +1176,7 @@ protected function createDefaultHttpClient() throw new LogicException('Could not find supported version of Guzzle.'); } - return new Client($options); + return new GuzzleClient($options); } private function createApplicationDefaultCredentials() @@ -1220,7 +1228,7 @@ protected function getAuthHandler() // sessions. // // @see https://github.com/google/google-api-php-client/issues/821 - return Google_AuthHandler_AuthHandlerFactory::build( + return AuthHandlerFactory::build( $this->getCache(), $this->config['cache_config'] ); diff --git a/src/Google/Collection.php b/src/Collection.php similarity index 93% rename from src/Google/Collection.php rename to src/Collection.php index ce8a2aa3d..3d61bea80 100644 --- a/src/Google/Collection.php +++ b/src/Collection.php @@ -1,11 +1,13 @@ requests as $key => $request) { $firstLine = sprintf( '%s %s HTTP/%s', @@ -176,8 +181,8 @@ public function parseResponse(ResponseInterface $response, $classes = array()) $key = $headers['content-id']; try { - $response = Google_Http_REST::decodeHttpResponse($response, $requests[$i-1]); - } catch (Google_Service_Exception $e) { + $response = REST::decodeHttpResponse($response, $requests[$i-1]); + } catch (GoogleServiceException $e) { // Store the exception as the response, so successful responses // can be processed. $response = $e; diff --git a/src/Google/Http/MediaFileUpload.php b/src/Http/MediaFileUpload.php similarity index 96% rename from src/Google/Http/MediaFileUpload.php rename to src/Http/MediaFileUpload.php index d868a9986..c98169d18 100644 --- a/src/Google/Http/MediaFileUpload.php +++ b/src/Http/MediaFileUpload.php @@ -15,6 +15,11 @@ * limitations under the License. */ +namespace Google\Http; + +use Google\Client; +use Google\Http\REST; +use Google\Exception as GoogleException; use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Uri; @@ -24,7 +29,7 @@ * Manage large file uploads, which may be media but can be any type * of sizable data. */ -class Google_Http_MediaFileUpload +class MediaFileUpload { const UPLOAD_MEDIA_TYPE = 'media'; const UPLOAD_MULTIPART_TYPE = 'multipart'; @@ -51,7 +56,7 @@ class Google_Http_MediaFileUpload /** @var int $progress */ private $progress; - /** @var Google_Client */ + /** @var Google\Client */ private $client; /** @var Psr\Http\Message\RequestInterface */ @@ -67,7 +72,7 @@ class Google_Http_MediaFileUpload private $httpResultCode; /** - * @param Google_Client $client + * @param Client $client * @param RequestInterface $request * @param string $mimeType * @param string $data The bytes you want to upload. @@ -76,7 +81,7 @@ class Google_Http_MediaFileUpload * only used if resumable=True */ public function __construct( - Google_Client $client, + Client $client, RequestInterface $request, $mimeType, $data, @@ -155,7 +160,7 @@ public function getHttpResultCode() * Sends a PUT-Request to google drive and parses the response, * setting the appropiate variables from the response() * - * @param Google_Http_Request $httpRequest the Reuqest which will be send + * @param Google\Http\Request $httpRequest the Reuqest which will be send * * @return false|mixed false when the upload is unfinished or the decoded http response * @@ -183,7 +188,7 @@ private function makePutRequest(RequestInterface $request) return false; } - return Google_Http_REST::decodeHttpResponse($response, $this->request); + return REST::decodeHttpResponse($response, $this->request); } /** @@ -327,7 +332,7 @@ private function fetchResumeUri() $error = "Failed to start the resumable upload (HTTP {$message})"; $this->client->getLogger()->error($error); - throw new Google_Exception($error); + throw new GoogleException($error); } private function transformToUploadUrl() diff --git a/src/Google/Http/REST.php b/src/Http/REST.php similarity index 91% rename from src/Google/Http/REST.php rename to src/Http/REST.php index c495ed9fe..014932809 100644 --- a/src/Google/Http/REST.php +++ b/src/Http/REST.php @@ -15,7 +15,12 @@ * limitations under the License. */ +namespace Google\Http; + use Google\Auth\HttpHandler\HttpHandlerFactory; +use Google\Client; +use Google\Task\Runner; +use Google\Service\Exception as GoogleServiceException; use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\Response; @@ -25,19 +30,19 @@ /** * This class implements the RESTful transport of apiServiceRequest()'s */ -class Google_Http_REST +class REST { /** * Executes a Psr\Http\Message\RequestInterface and (if applicable) automatically retries * when errors occur. * - * @param Google_Client $client + * @param Google\Client $client * @param Psr\Http\Message\RequestInterface $req * @param string $expectedClass * @param array $config * @param array $retryMap * @return array decoded result - * @throws Google_Service_Exception on server side error (ie: not authenticated, + * @throws Google\Service\Exception on server side error (ie: not authenticated, * invalid or malformed post body, invalid url) */ public static function execute( @@ -47,7 +52,7 @@ public static function execute( $config = array(), $retryMap = null ) { - $runner = new Google_Task_Runner( + $runner = new Runner( $config, sprintf('%s %s', $request->getMethod(), (string) $request->getUri()), array(get_class(), 'doExecute'), @@ -64,11 +69,11 @@ public static function execute( /** * Executes a Psr\Http\Message\RequestInterface * - * @param Google_Client $client + * @param Google\Client $client * @param Psr\Http\Message\RequestInterface $request * @param string $expectedClass * @return array decoded result - * @throws Google_Service_Exception on server side error (ie: not authenticated, + * @throws Google\Service\Exception on server side error (ie: not authenticated, * invalid or malformed post body, invalid url) */ public static function doExecute(ClientInterface $client, RequestInterface $request, $expectedClass = null) @@ -101,7 +106,7 @@ public static function doExecute(ClientInterface $client, RequestInterface $requ /** * Decode an HTTP Response. * @static - * @throws Google_Service_Exception + * @throws Google\Service\Exception * @param Psr\Http\Message\RequestInterface $response The http response to be decoded. * @param Psr\Http\Message\ResponseInterface $response * @param string $expectedClass @@ -120,7 +125,7 @@ public static function decodeHttpResponse( $body = (string) $response->getBody(); // Check if we received errors, and add those to the Exception for convenience - throw new Google_Service_Exception($body, $code, null, self::getResponseErrors($body)); + throw new GoogleServiceException($body, $code, null, self::getResponseErrors($body)); } // Ensure we only pull the entire body into memory if the request is not diff --git a/src/Google/Model.php b/src/Model.php similarity index 96% rename from src/Google/Model.php rename to src/Model.php index 18262608b..667899fe4 100644 --- a/src/Google/Model.php +++ b/src/Model.php @@ -15,16 +15,23 @@ * limitations under the License. */ +namespace Google; + +use Google\Exception as GoogleException; +use ReflectionObject; +use ReflectionProperty; +use stdClass; + /** * This class defines attributes, valid values, and usage which is generated * from a given json schema. * http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5 * */ -class Google_Model implements ArrayAccess +class Model implements \ArrayAccess { /** - * If you need to specify a NULL JSON value, use Google_Model::NULL_VALUE + * If you need to specify a NULL JSON value, use Google\Model::NULL_VALUE * instead - it will be replaced when converting to JSON with a real null. */ const NULL_VALUE = "{}gapi-php-null"; @@ -174,7 +181,7 @@ public function toSimpleObject() */ private function getSimpleValue($value) { - if ($value instanceof Google_Model) { + if ($value instanceof Model) { return $value->toSimpleObject(); } else if (is_array($value)) { $return = array(); @@ -233,14 +240,14 @@ protected function isAssociativeArray($array) /** * Verify if $obj is an array. - * @throws Google_Exception Thrown if $obj isn't an array. + * @throws Google\Exception Thrown if $obj isn't an array. * @param array $obj Items that should be validated. * @param string $method Method expecting an array as an argument. */ public function assertIsArray($obj, $method) { if ($obj && !is_array($obj)) { - throw new Google_Exception( + throw new GoogleException( "Incorrect parameter type passed to $method(). Expected an array." ); } diff --git a/src/Google/Service.php b/src/Service.php similarity index 81% rename from src/Google/Service.php rename to src/Service.php index 43979428b..d967f4cc3 100644 --- a/src/Google/Service.php +++ b/src/Service.php @@ -15,7 +15,12 @@ * limitations under the License. */ -class Google_Service +namespace Google; + +use Google\Http\Batch; +use TypeError; + +class Service { public $batchPath; public $rootUrl; @@ -25,17 +30,14 @@ class Google_Service public $resource; private $client; - /** - * @param Google_Client|array $clientOrConfig Optional - */ public function __construct($clientOrConfig = []) { - if ($clientOrConfig instanceof Google_Client) { + if ($clientOrConfig instanceof Client) { $this->client = $clientOrConfig; } elseif (is_array($clientOrConfig)) { - $this->client = new Google_Client($clientOrConfig ?: []); + $this->client = new Client($clientOrConfig ?: []); } else { - $errorMessage = 'constructor must be array or instance of Google_Client'; + $errorMessage = 'constructor must be array or instance of Google\Client'; if (class_exists('TypeError')) { throw new TypeError($errorMessage); } @@ -44,8 +46,8 @@ public function __construct($clientOrConfig = []) } /** - * Return the associated Google_Client class. - * @return Google_Client + * Return the associated Google\Client class. + * @return Google\Client */ public function getClient() { @@ -59,7 +61,7 @@ public function getClient() */ public function createBatch() { - return new Google_Http_Batch( + return new Batch( $this->client, false, $this->rootUrl, diff --git a/src/Google/Service/Exception.php b/src/Service/Exception.php similarity index 94% rename from src/Google/Service/Exception.php rename to src/Service/Exception.php index 3ab28aed3..12464d4af 100644 --- a/src/Google/Service/Exception.php +++ b/src/Service/Exception.php @@ -15,7 +15,11 @@ * limitations under the License. */ -class Google_Service_Exception extends Google_Exception +namespace Google\Service; + +use Google\Exception as GoogleException; + +class Exception extends GoogleException { /** * Optional list of errors returned in a JSON body of an HTTP error response. diff --git a/src/Google/Service/README.md b/src/Service/README.md similarity index 100% rename from src/Google/Service/README.md rename to src/Service/README.md diff --git a/src/Google/Service/Resource.php b/src/Service/Resource.php similarity index 93% rename from src/Google/Service/Resource.php rename to src/Service/Resource.php index 77070fc41..8d1ea8bc0 100644 --- a/src/Google/Service/Resource.php +++ b/src/Service/Resource.php @@ -15,6 +15,12 @@ * limitations under the License. */ +namespace Google\Service; + +use Google\Model; +use Google\Http\MediaFileUpload; +use Google\Exception as GoogleException; +use Google\Utils\UriTemplate; use GuzzleHttp\Psr7\Request; /** @@ -23,7 +29,7 @@ * is available in this service, and if so construct an apiHttpRequest representing it. * */ -class Google_Service_Resource +class Resource { // Valid query parameters that work, but don't appear in discovery. private $stackParameters = array( @@ -42,7 +48,7 @@ class Google_Service_Resource /** @var string $rootUrl */ private $rootUrl; - /** @var Google_Client $client */ + /** @var Google\Client $client */ private $client; /** @var string $serviceName */ @@ -74,8 +80,8 @@ public function __construct($service, $serviceName, $resourceName, $resource) * @param $name * @param $arguments * @param $expectedClass - optional, the expected class name - * @return Google_Http_Request|expectedClass - * @throws Google_Exception + * @return Google\Http\Request|expectedClass + * @throws Google\Exception */ public function call($name, $arguments, $expectedClass = null) { @@ -89,7 +95,7 @@ public function call($name, $arguments, $expectedClass = null) ) ); - throw new Google_Exception( + throw new GoogleException( "Unknown function: " . "{$this->serviceName}->{$this->resourceName}->{$name}()" ); @@ -101,7 +107,7 @@ public function call($name, $arguments, $expectedClass = null) // document as parameter, but we abuse the param entry for storing it. $postBody = null; if (isset($parameters['postBody'])) { - if ($parameters['postBody'] instanceof Google_Model) { + if ($parameters['postBody'] instanceof Model) { // In the cases the post body is an existing object, we want // to use the smart method to create a simple object for // for JSONification. @@ -144,7 +150,7 @@ public function call($name, $arguments, $expectedClass = null) 'parameter' => $key ) ); - throw new Google_Exception("($name) unknown parameter: '$key'"); + throw new GoogleException("($name) unknown parameter: '$key'"); } } @@ -162,7 +168,7 @@ public function call($name, $arguments, $expectedClass = null) 'parameter' => $paramName ) ); - throw new Google_Exception("($name) missing required param: '$paramName'"); + throw new GoogleException("($name) missing required param: '$paramName'"); } if (isset($parameters[$paramName])) { $value = $parameters[$paramName]; @@ -207,7 +213,7 @@ public function call($name, $arguments, $expectedClass = null) ? $parameters['mimeType']['value'] : 'application/octet-stream'; $data = $parameters['data']['value']; - $upload = new Google_Http_MediaFileUpload($this->client, $request, $mimeType, $data); + $upload = new MediaFileUpload($this->client, $request, $mimeType, $data); // pull down the modified request $request = $upload->getRequest(); @@ -289,7 +295,7 @@ public function createRequestUri($restPath, $params) } if (count($uriTemplateVars)) { - $uriTemplateParser = new Google_Utils_UriTemplate(); + $uriTemplateParser = new UriTemplate(); $requestUrl = $uriTemplateParser->parse($requestUrl, $uriTemplateVars); } diff --git a/src/Google/Task/Composer.php b/src/Task/Composer.php similarity index 93% rename from src/Google/Task/Composer.php rename to src/Task/Composer.php index 865fc7af4..32ca2de75 100644 --- a/src/Google/Task/Composer.php +++ b/src/Task/Composer.php @@ -15,11 +15,14 @@ * the License. */ +namespace Google\Task; + use Composer\Script\Event; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Finder\Finder; +use InvalidArgumentException; -class Google_Task_Composer +class Composer { /** * @param Event $event Composer event passed in for any script method @@ -70,7 +73,7 @@ private static function verifyServicesToKeep( foreach ($servicesToKeep as $service) { if (!preg_match('/^[a-zA-Z0-9]*$/', $service)) { - throw new \InvalidArgumentException( + throw new InvalidArgumentException( sprintf( 'Invalid Google service name "%s"', $service @@ -79,8 +82,8 @@ private static function verifyServicesToKeep( } try { $finder->in($serviceDir . '/' . $service); - } catch (\InvalidArgumentException $e) { - throw new \InvalidArgumentException( + } catch (InvalidArgumentException $e) { + throw new InvalidArgumentException( sprintf( 'Google service "%s" does not exist or was removed previously', $service diff --git a/src/Google/Task/Exception.php b/src/Task/Exception.php similarity index 85% rename from src/Google/Task/Exception.php rename to src/Task/Exception.php index 5422e6fc4..9e0d436b5 100644 --- a/src/Google/Task/Exception.php +++ b/src/Task/Exception.php @@ -15,6 +15,10 @@ * limitations under the License. */ -class Google_Task_Exception extends Google_Exception +namespace Google\Task; + +use Google\Exception as GoogleException; + +class Exception extends GoogleException { } diff --git a/src/Google/Task/Retryable.php b/src/Task/Retryable.php similarity index 94% rename from src/Google/Task/Retryable.php rename to src/Task/Retryable.php index 19aa4ddc2..5f67af8ac 100644 --- a/src/Google/Task/Retryable.php +++ b/src/Task/Retryable.php @@ -15,10 +15,12 @@ * limitations under the License. */ +namespace Google\Task; + /** * Interface for checking how many times a given task can be retried following * a failure. */ -interface Google_Task_Retryable +interface Retryable { } diff --git a/src/Google/Task/Runner.php b/src/Task/Runner.php similarity index 92% rename from src/Google/Task/Runner.php rename to src/Task/Runner.php index 2f25e990e..ad1c56ab1 100644 --- a/src/Google/Task/Runner.php +++ b/src/Task/Runner.php @@ -15,12 +15,17 @@ * limitations under the License. */ +namespace Google\Task; + +use Google\Service\Exception as GoogleServiceException; +use Google\Task\Exception as GoogleTaskException; + /** * A task runner with exponential backoff support. * * @see https://developers.google.com/drive/web/handle-errors#implementing_exponential_backoff */ -class Google_Task_Runner +class Runner { const TASK_RETRY_NEVER = 0; const TASK_RETRY_ONCE = 1; @@ -86,7 +91,7 @@ class Google_Task_Runner * @param string $name The name of the current task (used for logging) * @param callable $action The task to run and possibly retry * @param array $arguments The task arguments - * @throws Google_Task_Exception when misconfigured + * @throws Google\Task\Exception when misconfigured */ public function __construct( $config, @@ -96,7 +101,7 @@ public function __construct( ) { if (isset($config['initial_delay'])) { if ($config['initial_delay'] < 0) { - throw new Google_Task_Exception( + throw new GoogleTaskException( 'Task configuration `initial_delay` must not be negative.' ); } @@ -106,7 +111,7 @@ public function __construct( if (isset($config['max_delay'])) { if ($config['max_delay'] <= 0) { - throw new Google_Task_Exception( + throw new GoogleTaskException( 'Task configuration `max_delay` must be greater than 0.' ); } @@ -116,7 +121,7 @@ public function __construct( if (isset($config['factor'])) { if ($config['factor'] <= 0) { - throw new Google_Task_Exception( + throw new GoogleTaskException( 'Task configuration `factor` must be greater than 0.' ); } @@ -126,7 +131,7 @@ public function __construct( if (isset($config['jitter'])) { if ($config['jitter'] <= 0) { - throw new Google_Task_Exception( + throw new GoogleTaskException( 'Task configuration `jitter` must be greater than 0.' ); } @@ -136,7 +141,7 @@ public function __construct( if (isset($config['retries'])) { if ($config['retries'] < 0) { - throw new Google_Task_Exception( + throw new GoogleTaskException( 'Task configuration `retries` must not be negative.' ); } @@ -144,7 +149,7 @@ public function __construct( } if (!is_callable($action)) { - throw new Google_Task_Exception( + throw new GoogleTaskException( 'Task argument `$action` must be a valid callable.' ); } @@ -167,14 +172,14 @@ public function canAttempt() * Runs the task and (if applicable) automatically retries when errors occur. * * @return mixed - * @throws Google_Task_Retryable on failure when no retries are available. + * @throws Google\Task\Retryable on failure when no retries are available. */ public function run() { while ($this->attempt()) { try { return call_user_func_array($this->action, $this->arguments); - } catch (Google_Service_Exception $exception) { + } catch (GoogleServiceException $exception) { $allowedRetries = $this->allowedRetries( $exception->getCode(), $exception->getErrors() diff --git a/src/Google/Utils/UriTemplate.php b/src/Utils/UriTemplate.php similarity index 99% rename from src/Google/Utils/UriTemplate.php rename to src/Utils/UriTemplate.php index e59fe9f21..1f0c6b31d 100644 --- a/src/Google/Utils/UriTemplate.php +++ b/src/Utils/UriTemplate.php @@ -15,11 +15,13 @@ * limitations under the License. */ +namespace Google\Utils; + /** * Implementation of levels 1-3 of the URI Template spec. * @see http://tools.ietf.org/html/rfc6570 */ -class Google_Utils_UriTemplate +class UriTemplate { const TYPE_MAP = "1"; const TYPE_LIST = "2"; diff --git a/src/aliases.php b/src/aliases.php new file mode 100644 index 000000000..6e9c01ee9 --- /dev/null +++ b/src/aliases.php @@ -0,0 +1,29 @@ + 'Google_Client', + 'Google\\Service' => 'Google_Service', + 'Google\\AccessToken\\Revoke' => 'Google_AccessToken_Revoke', + 'Google\\AccessToken\\Verify' => 'Google_AccessToken_Verify', + 'Google\\Model' => 'Google_Model', + 'Google\\Utils\\UriTemplate' => 'Google_Utils_UriTemplate', + 'Google\\AuthHandler\\Guzzle6AuthHandler' => 'Google_AuthHandler_Guzzle6AuthHandler', + 'Google\\AuthHandler\\Guzzle7AuthHandler' => 'Google_AuthHandler_Guzzle7AuthHandler', + 'Google\\AuthHandler\\Guzzle5AuthHandler' => 'Google_AuthHandler_Guzzle5AuthHandler', + 'Google\\AuthHandler\\AuthHandlerFactory' => 'Google_AuthHandler_AuthHandlerFactory', + 'Google\\Http\\Batch' => 'Google_Http_Batch', + 'Google\\Http\\MediaFileUpload' => 'Google_Http_MediaFileUpload', + 'Google\\Http\\REST' => 'Google_Http_REST', + 'Google\\Task\\Composer' => 'Google_Task_Composer', + 'Google\\Task\\Retryable' => 'Google_Task_Retryable', + 'Google\\Task\\Exception' => 'Google_Task_Exception', + 'Google\\Task\\Runner' => 'Google_Task_Runner', + 'Google\\Collection' => 'Google_Collection', + 'Google\\Service\\Exception' => 'Google_Service_Exception', + 'Google\\Service\\Resource' => 'Google_Service_Resource', + 'Google\\Exception' => 'Google_Exception', +]; + +foreach ($classMap as $class => $alias) { + class_alias($class, $alias); +} diff --git a/tests/Google/ServiceTest.php b/tests/Google/ServiceTest.php index b1130b65f..72b9a10d2 100644 --- a/tests/Google/ServiceTest.php +++ b/tests/Google/ServiceTest.php @@ -100,28 +100,6 @@ public function testModel() $this->assertTrue($model->isAssociativeArray(array("a", "b" => 2))); } - /** - * @dataProvider serviceProvider - */ - public function testIncludes($class) - { - $this->assertTrue( - class_exists($class), - sprintf('Failed asserting class %s exists.', $class) - ); - } - - public function serviceProvider() - { - $classes = array(); - $path = dirname(dirname(__DIR__)) . '/src/Google/Service'; - foreach (glob($path . "/*.php") as $file) { - $classes[] = array('Google_Service_' . basename($file, '.php')); - } - - return $classes; - } - public function testConfigConstructor() { $clientId = 'test-client-id'; @@ -147,7 +125,7 @@ public function testInvalidConstructorPhp7Plus() $this->assertInstanceOf('TypeError', $e); $this->assertEquals( - 'constructor must be array or instance of Google_Client', + 'constructor must be array or instance of Google\Client', $e->getMessage() ); } @@ -166,7 +144,7 @@ public function testInvalidConstructorPhp5() $service = new TestService('foo'); $this->assertEquals( - 'constructor must be array or instance of Google_Client', + 'constructor must be array or instance of Google\Client', self::$errorMessage ); } diff --git a/tests/examples/batchTest.php b/tests/examples/batchTest.php index 212defdf7..425d9de2e 100644 --- a/tests/examples/batchTest.php +++ b/tests/examples/batchTest.php @@ -30,6 +30,6 @@ public function testBatch() $nodes = $crawler->filter('br'); $this->assertCount(20, $nodes); $this->assertContains('Walden', $crawler->text()); - $this->assertContains('George Bernard Shaw His Life and Works', $crawler->text()); + $this->assertContains('George Bernard Shaw', $crawler->text()); } } From 2ab61398fa6c7e319f3665fbf0c01df6f1f683ec Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 22 Oct 2020 10:18:24 -0700 Subject: [PATCH 161/301] docs: update readme with namespaced class names (#1968) --- README.md | 22 +++++++++---------- composer.json | 2 +- docs/api-keys.md | 2 +- docs/media.md | 4 ++-- docs/oauth-server.md | 26 +++++++++++----------- docs/oauth-web.md | 14 ++++++------ docs/start.md | 30 +++++++++++++------------- examples/batch.php | 6 +++--- examples/idtoken.php | 4 ++-- examples/large-file-download.php | 4 ++-- examples/large-file-upload.php | 6 +++--- examples/multi-api.php | 4 ++-- examples/service-account.php | 2 +- examples/simple-file-upload.php | 4 ++-- examples/simple-query.php | 2 +- src/AuthHandler/AuthHandlerFactory.php | 2 +- src/Service.php | 2 +- tests/Google/Task/ComposerTest.php | 4 ++-- 18 files changed, 70 insertions(+), 70 deletions(-) diff --git a/README.md b/README.md index 1274cdf2e..2d9df6d78 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ This library relies on `google/apiclient-services`. That library provides up-to- There are over 200 Google API services. The chances are good that you will not want them all. In order to avoid shipping these dependencies with your code, -you can run the `Google_Task_Composer::cleanup` task and specify the services +you can run the `Google\Task\Composer::cleanup` task and specify the services you want to keep in `composer.json`: ```json @@ -61,7 +61,7 @@ you want to keep in `composer.json`: "google/apiclient": "^2.7" }, "scripts": { - "post-update-cmd": "Google_Task_Composer::cleanup" + "post-update-cmd": "Google\\Task\\Composer::cleanup" }, "extra": { "google/apiclient-services": [ @@ -131,7 +131,7 @@ And then browsing to the host and port you specified // include your composer dependencies require_once 'vendor/autoload.php'; -$client = new Google_Client(); +$client = new Google\Client(); $client->setApplicationName("Client_Library_Examples"); $client->setDeveloperKey("YOUR_APP_KEY"); @@ -153,10 +153,10 @@ foreach ($results->getItems() as $item) { 1. Follow the instructions to [Create Web Application Credentials](docs/oauth-web.md#create-authorization-credentials) 1. Download the JSON credentials -1. Set the path to these credentials using `Google_Client::setAuthConfig`: +1. Set the path to these credentials using `Google\Client::setAuthConfig`: ```php - $client = new Google_Client(); + $client = new Google\Client(); $client->setAuthConfig('/path/to/client_credentials.json'); ``` @@ -203,7 +203,7 @@ calls return unexpected 401 or 403 errors. 1. Tell the Google client to use your service account credentials to authenticate: ```php - $client = new Google_Client(); + $client = new Google\Client(); $client->useApplicationDefaultCredentials(); ``` @@ -311,7 +311,7 @@ The `authorize` method returns an authorized [Guzzle Client](http://docs.guzzlep ```php // create the Google client -$client = new Google_Client(); +$client = new Google\Client(); /** * Set your method for authentication. Depending on the API, This could be @@ -355,7 +355,7 @@ composer require cache/filesystem-adapter When using [Refresh Tokens](https://developers.google.com/identity/protocols/OAuth2InstalledApp#offline) or [Service Account Credentials](https://developers.google.com/identity/protocols/OAuth2ServiceAccount#overview), it may be useful to perform some action when a new access token is granted. To do this, pass a callable to the `setTokenCallback` method on the client: ```php -$logger = new Monolog\Logger; +$logger = new Monolog\Logger(); $tokenCallback = function ($cacheKey, $accessToken) use ($logger) { $logger->debug(sprintf('new access token received at cache key %s', $cacheKey)); }; @@ -373,7 +373,7 @@ $httpClient = new GuzzleHttp\Client([ 'verify' => false, // otherwise HTTPS requests will fail. ]); -$client = new Google_Client(); +$client = new Google\Client(); $client->setHttpClient($httpClient); ``` @@ -396,7 +396,7 @@ $httpClient = new Client([ ] ]); -$client = new Google_Client(); +$client = new Google\Client(); $client->setHttpClient($httpClient); ``` @@ -438,7 +438,7 @@ $opt_params = array( ### How do I set a field to null? ### -The library strips out nulls from the objects sent to the Google APIs as its the default value of all of the uninitialized properties. To work around this, set the field you want to null to `Google_Model::NULL_VALUE`. This is a placeholder that will be replaced with a true null when sent over the wire. +The library strips out nulls from the objects sent to the Google APIs as its the default value of all of the uninitialized properties. To work around this, set the field you want to null to `Google\Model::NULL_VALUE`. This is a placeholder that will be replaced with a true null when sent over the wire. ## Code Quality ## diff --git a/composer.json b/composer.json index 43a8ac01f..14654d5b5 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "composer/composer": "^1.10" }, "suggest": { - "cache/filesystem-adapter": "For caching certs and tokens (using Google_Client::setCache)" + "cache/filesystem-adapter": "For caching certs and tokens (using Google\\Client::setCache)" }, "autoload": { "psr-4": { diff --git a/docs/api-keys.md b/docs/api-keys.md index 7828651f7..2e7d771e5 100644 --- a/docs/api-keys.md +++ b/docs/api-keys.md @@ -6,7 +6,7 @@ When calling APIs that do not access private user data, you can use simple API k ## Using API Keys -To use API keys, call the `setDeveloperKey()` method of the `Google_Client` object before making any API calls. For example: +To use API keys, call the `setDeveloperKey()` method of the `Google\Client` object before making any API calls. For example: ```php $client->setDeveloperKey($api_key); diff --git a/docs/media.md b/docs/media.md index 783e632bc..66148c387 100644 --- a/docs/media.md +++ b/docs/media.md @@ -26,7 +26,7 @@ $result = $service->files->insert($file, array( 'data' => file_get_contents("path/to/file"), 'mimeType' => 'application/octet-stream', 'uploadType' => 'multipart' -)); +)); ``` ## Resumable File Upload @@ -43,7 +43,7 @@ $client->setDefer(true); $request = $service->files->insert($file); // Create a media file upload to represent our upload process. -$media = new Google_Http_MediaFileUpload( +$media = new Google\Http\MediaFileUpload( $client, $request, 'text/plain', diff --git a/docs/oauth-server.md b/docs/oauth-server.md index 906fd082f..ad57b0c78 100644 --- a/docs/oauth-server.md +++ b/docs/oauth-server.md @@ -68,7 +68,7 @@ putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json'); Call the `useApplicationDefaultCredentials` to use your service account credentials to authenticate: ```php -$client = new Google_Client(); +$client = new Google\Client(); $client->useApplicationDefaultCredentials(); ``` @@ -78,20 +78,20 @@ If you have delegated domain-wide access to the service account and you want to $client->setSubject($user_to_impersonate); ``` -Use the authorized `Google_Client` object to call Google APIs in your application. +Use the authorized `Google\Client` object to call Google APIs in your application. ## Calling Google APIs -Use the authorized `Google_Client` object to call Google APIs by completing the following steps: +Use the authorized `Google\Client` object to call Google APIs by completing the following steps: + +1. Build a service object for the API that you want to call, providing the authorized `Google\Client` object. For example, to call the Cloud SQL Administration API: -1. Build a service object for the API that you want to call, providing the authorized `Google_Client` object. For example, to call the Cloud SQL Administration API: - ```php $sqladmin = new Google_Service_SQLAdmin($client); ``` - + 2. Make requests to the API service using the [interface provided by the service object](https://github.com/googleapis/google-api-php-client/blob/master/docs/start.md#build-the-service-object). For example, to list the instances of Cloud SQL databases in the examinable-example-123 project: - + ```php $response = $sqladmin->instances->listInstances('examinable-example-123')->getItems(); ``` @@ -103,21 +103,21 @@ The following example prints a JSON-formatted list of Cloud SQL instances in a p To run this example: 1. Create a new directory and change to it. For example: - + ```sh mkdir ~/php-oauth2-example cd ~/php-oauth2-example ``` - + 2. Install the [Google API Client Library](https://github.com/google/google-api-php-client) for PHP using [Composer](https://getcomposer.org): - + ```sh composer require google/apiclient:^2.0 ``` - + 3. Create the file sqlinstances.php with the content below. 4. Run the example from the command line: - + ``` php ~/php-oauth2-example/sqlinstances.php ``` @@ -130,7 +130,7 @@ To run this example: require_once __DIR__.'/vendor/autoload.php'; putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json'); -$client = new Google_Client(); +$client = new Google\Client(); $client->useApplicationDefaultCredentials(); $sqladmin = new Google_Service_SQLAdmin($client); diff --git a/docs/oauth-web.md b/docs/oauth-web.md index 84222e9ab..3fef5edef 100644 --- a/docs/oauth-web.md +++ b/docs/oauth-web.md @@ -73,14 +73,14 @@ The list below quickly summarizes these steps: Your first step is to create the authorization request. That request sets parameters that identify your application and define the permissions that the user will be asked to grant to your application. -The code snippet below creates a `Google_Client()` object, which defines the parameters in the authorization request. +The code snippet below creates a `Google\Client()` object, which defines the parameters in the authorization request. That object uses information from your **client_secret.json** file to identify your application. The object also identifies the scopes that your application is requesting permission to access and the URL to your application's auth endpoint, which will handle the response from Google's OAuth 2.0 server. Finally, the code sets the optional access_type and include_granted_scopes parameters. For example, this code requests read-only, offline access to a user's Google Drive: ```php -$client = new Google_Client(); +$client = new Google\Client(); $client->setAuthConfig('client_secret.json'); $client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY); $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php'); @@ -97,7 +97,7 @@ The request specifies the following information: **Required**. The client ID for your application. You can find this value in the [API Console](https://console.developers.google.com/). In PHP, call the `setAuthConfig` function to load authorization credentials from a **client_secret.json** file. ```php -$client = new Google_Client(); +$client = new Google\Client(); $client->setAuthConfig('client_secret.json'); ``` @@ -275,13 +275,13 @@ $access_token = $client->getAccessToken(); Use the access token to call Google APIs by completing the following steps: -1. If you need to apply an access token to a new `Google_Client` object—for example, if you stored the access token in a user session—use the `setAccessToken` method: +1. If you need to apply an access token to a new `Google\Client` object—for example, if you stored the access token in a user session—use the `setAccessToken` method: ```php $client->setAccessToken($access_token); ``` -2. Build a service object for the API that you want to call. You build a a service object by providing an authorized `Google_Client` object to the constructor for the API you want to call. For example, to call the Drive API: +2. Build a service object for the API that you want to call. You build a a service object by providing an authorized `Google\Client` object to the constructor for the API you want to call. For example, to call the Drive API: ```php $drive = new Google_Service_Drive($client); @@ -329,7 +329,7 @@ require_once __DIR__.'/vendor/autoload.php'; session_start(); -$client = new Google_Client(); +$client = new Google\Client(); $client->setAuthConfig('client_secrets.json'); $client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY); @@ -352,7 +352,7 @@ require_once __DIR__.'/vendor/autoload.php'; session_start(); -$client = new Google_Client(); +$client = new Google\Client(); $client->setAuthConfigFile('client_secrets.json'); $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php'); $client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY); diff --git a/docs/start.md b/docs/start.md index a315d665e..d22b49d0c 100644 --- a/docs/start.md +++ b/docs/start.md @@ -21,9 +21,9 @@ These API calls do not access any private user data. Your application must authe #### Important concepts * **API key**: To authenticate your application, use an [API key](https://cloud.google.com/docs/authentication/api-keys) for your Google Cloud Console project. Every simple access call your application makes must include this key. - + > **Warning**: Keep your API key private. If someone obtains your key, they could use it to consume your quota or incur charges against your Google Cloud project. - + ### 2. Authorized API access (OAuth 2.0) @@ -33,17 +33,17 @@ These API calls access private user data. Before you can call them, the user tha * **Scope**: Each API defines one or more scopes that declare a set of operations permitted. For example, an API might have read-only and read-write scopes. When your application requests access to user data, the request must include one or more scopes. The user needs to approve the scope of access your application is requesting. * **Refresh and access tokens**: When a user grants your application access, the OAuth 2.0 authorization server provides your application with refresh and access tokens. These tokens are only valid for the scope requested. Your application uses access tokens to authorize API calls. Access tokens expire, but refresh tokens do not. Your application can use a refresh token to acquire a new access token. - + > **Warning**: Keep refresh and access tokens private. If someone obtains your tokens, they could use them to access private user data. - + * **Client ID and client secret**: These strings uniquely identify your application and are used to acquire tokens. They are created for your Google Cloud project on the [API Access pane](https://code.google.com/apis/console#:access) of the Google Cloud. There are three types of client IDs, so be sure to get the correct type for your application: - + * Web application client IDs * Installed application client IDs * [Service Account](https://developers.google.com/identity/protocols/OAuth2ServiceAccount) client IDs - + > **Warning**: Keep your client secret private. If someone obtains your client secret, they could use it to consume your quota, incur charges against your Google Cloud project, and request access to user data. - + ## Building and calling a service @@ -54,16 +54,16 @@ This section described how to build an API-specific service object, make calls t The client object is the primary container for classes and configuration in the library. ```php -$client = new Google_Client(); +$client = new Google\Client(); $client->setApplicationName("My Application"); $client->setDeveloperKey("MY_SIMPLE_API_KEY"); -``` +``` ### Build the service object -Services are called through queries to service specific objects. These are created by constructing the service object, and passing an instance of `Google_Client` to it. `Google_Client` contains the IO, authentication and other classes required by the service to function, and the service informs the client which scopes it uses to provide a default when authenticating a user. +Services are called through queries to service specific objects. These are created by constructing the service object, and passing an instance of `Google\Client` to it. `Google\Client` contains the IO, authentication and other classes required by the service to function, and the service informs the client which scopes it uses to provide a default when authenticating a user. -```php +```php $service = new Google_Service_Books($client); ``` @@ -71,20 +71,20 @@ $service = new Google_Service_Books($client); Each API provides resources and methods, usually in a chain. These can be accessed from the service object in the form `$service->resource->method(args)`. Most method require some arguments, then accept a final parameter of an array containing optional parameters. For example, with the Google Books API, we can make a call to list volumes matching a certain string, and add an optional _filter_ parameter. -```php +```php $optParams = array('filter' => 'free-ebooks'); $results = $service->volumes->listVolumes('Henry David Thoreau', $optParams); -``` +``` ### Handling the result There are two main types of response - items and collections of items. Each can be accessed either as an object or as an array. Collections implement the `Iterator` interface so can be used in foreach and other constructs. -```php +```php foreach ($results as $item) { echo $item['volumeInfo']['title'], "
      \n"; } -``` +``` ## Google App Engine support diff --git a/examples/batch.php b/examples/batch.php index ea663d8c3..2285e5dd3 100644 --- a/examples/batch.php +++ b/examples/batch.php @@ -32,7 +32,7 @@ setDeveloperKey, the request may still succeed using the anonymous quota. ************************************************/ -$client = new Google_Client(); +$client = new Google\Client(); $client->setApplicationName("Client_Library_Examples"); // Warn if the API key isn't set. @@ -60,8 +60,8 @@ ************************************************/ // NOTE: Some services use `$service->createBatch();` instead of -// `new Google_Http_Batch($client);` -$batch = new Google_Http_Batch($client); +// `new Google\Http\Batch($client);` +$batch = new Google\Http\Batch($client); $optParams = array('filter' => 'free-ebooks'); $optParams['q'] = 'Henry David Thoreau'; diff --git a/examples/idtoken.php b/examples/idtoken.php index 74bf7ae64..988bd6e9c 100644 --- a/examples/idtoken.php +++ b/examples/idtoken.php @@ -35,7 +35,7 @@ ************************************************/ $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; -$client = new Google_Client(); +$client = new Google\Client(); $client->setAuthConfig($oauth_credentials); $client->setRedirectUri($redirect_uri); $client->setScopes('email'); @@ -52,7 +52,7 @@ /************************************************ * If we have a code back from the OAuth 2.0 flow, * we need to exchange that with the - * Google_Client::fetchAccessTokenWithAuthCode() + * Google\Client::fetchAccessTokenWithAuthCode() * function. We store the resultant access token * bundle in the session, and redirect to ourself. ************************************************/ diff --git a/examples/large-file-download.php b/examples/large-file-download.php index 51cf8b0bc..1ab30e735 100644 --- a/examples/large-file-download.php +++ b/examples/large-file-download.php @@ -34,7 +34,7 @@ ************************************************/ $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; -$client = new Google_Client(); +$client = new Google\Client(); $client->setAuthConfig($oauth_credentials); $client->setRedirectUri($redirect_uri); $client->addScope("https://www.googleapis.com/auth/drive"); @@ -43,7 +43,7 @@ /************************************************ * If we have a code back from the OAuth 2.0 flow, * we need to exchange that with the - * Google_Client::fetchAccessTokenWithAuthCode() + * Google\Client::fetchAccessTokenWithAuthCode() * function. We store the resultant access token * bundle in the session, and redirect to ourself. ************************************************/ diff --git a/examples/large-file-upload.php b/examples/large-file-upload.php index 0d966f020..ba4a96589 100644 --- a/examples/large-file-upload.php +++ b/examples/large-file-upload.php @@ -34,7 +34,7 @@ ************************************************/ $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; -$client = new Google_Client(); +$client = new Google\Client(); $client->setAuthConfig($oauth_credentials); $client->setRedirectUri($redirect_uri); $client->addScope("https://www.googleapis.com/auth/drive"); @@ -48,7 +48,7 @@ /************************************************ * If we have a code back from the OAuth 2.0 flow, * we need to exchange that with the - * Google_Client::fetchAccessTokenWithAuthCode() + * Google\Client::fetchAccessTokenWithAuthCode() * function. We store the resultant access token * bundle in the session, and redirect to ourself. ************************************************/ @@ -98,7 +98,7 @@ $request = $service->files->create($file); // Create a media file upload to represent our upload process. - $media = new Google_Http_MediaFileUpload( + $media = new Google\Http\MediaFileUpload( $client, $request, 'text/plain', diff --git a/examples/multi-api.php b/examples/multi-api.php index 97c33135b..573e6bc8f 100644 --- a/examples/multi-api.php +++ b/examples/multi-api.php @@ -35,7 +35,7 @@ ************************************************/ $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; -$client = new Google_Client(); +$client = new Google\Client(); $client->setAuthConfig($oauth_credentials); $client->setRedirectUri($redirect_uri); $client->addScope("https://www.googleapis.com/auth/drive"); @@ -49,7 +49,7 @@ /************************************************ * If we have a code back from the OAuth 2.0 flow, * we need to exchange that with the - * Google_Client::fetchAccessTokenWithAuthCode() + * Google\Client::fetchAccessTokenWithAuthCode() * function. We store the resultant access token * bundle in the session, and redirect to ourself. ************************************************/ diff --git a/examples/service-account.php b/examples/service-account.php index 2d723a1f9..c86300116 100644 --- a/examples/service-account.php +++ b/examples/service-account.php @@ -25,7 +25,7 @@ account. ************************************************/ -$client = new Google_Client(); +$client = new Google\Client(); /************************************************ ATTENTION: Fill in these values, or make sure you diff --git a/examples/simple-file-upload.php b/examples/simple-file-upload.php index 21cb50f28..6e666a9cb 100644 --- a/examples/simple-file-upload.php +++ b/examples/simple-file-upload.php @@ -34,7 +34,7 @@ ************************************************/ $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; -$client = new Google_Client(); +$client = new Google\Client(); $client->setAuthConfig($oauth_credentials); $client->setRedirectUri($redirect_uri); $client->addScope("https://www.googleapis.com/auth/drive"); @@ -48,7 +48,7 @@ /************************************************ * If we have a code back from the OAuth 2.0 flow, * we need to exchange that with the - * Google_Client::fetchAccessTokenWithAuthCode() + * Google\Client::fetchAccessTokenWithAuthCode() * function. We store the resultant access token * bundle in the session, and redirect to ourself. ************************************************/ diff --git a/examples/simple-query.php b/examples/simple-query.php index 5358b5045..bf49581b1 100644 --- a/examples/simple-query.php +++ b/examples/simple-query.php @@ -26,7 +26,7 @@ setDeveloperKey, the request may still succeed using the anonymous quota. ************************************************/ -$client = new Google_Client(); +$client = new Google\Client(); $client->setApplicationName("Client_Library_Examples"); // Warn if the API key isn't set. diff --git a/src/AuthHandler/AuthHandlerFactory.php b/src/AuthHandler/AuthHandlerFactory.php index 1ae9d8a9e..dced77a17 100644 --- a/src/AuthHandler/AuthHandlerFactory.php +++ b/src/AuthHandler/AuthHandlerFactory.php @@ -26,7 +26,7 @@ class AuthHandlerFactory /** * Builds out a default http handler for the installed version of guzzle. * - * @return Google_AuthHandler_Guzzle5AuthHandler|Google_AuthHandler_Guzzle6AuthHandler + * @return Guzzle5AuthHandler|Guzzle6AuthHandler|Guzzle7AuthHandler * @throws Exception */ public static function build($cache = null, array $cacheConfig = []) diff --git a/src/Service.php b/src/Service.php index d967f4cc3..0257abd43 100644 --- a/src/Service.php +++ b/src/Service.php @@ -57,7 +57,7 @@ public function getClient() /** * Create a new HTTP Batch handler for this service * - * @return Google_Http_Batch + * @return Google\Http\Batch */ public function createBatch() { diff --git a/tests/Google/Task/ComposerTest.php b/tests/Google/Task/ComposerTest.php index 2c37f3194..bb1cfbde9 100644 --- a/tests/Google/Task/ComposerTest.php +++ b/tests/Google/Task/ComposerTest.php @@ -152,7 +152,7 @@ public function testE2E() 'google/apiclient' => 'dev-master' ], 'scripts' => [ - 'post-update-cmd' => 'Google_Task_Composer::cleanup' + 'post-update-cmd' => 'Google\Task\Composer::cleanup' ], 'extra' => [ 'google/apiclient-services' => [ @@ -181,7 +181,7 @@ public function testE2E() 'google/apiclient' => 'dev-master' ], 'scripts' => [ - 'post-update-cmd' => 'Google_Task_Composer::cleanup' + 'post-update-cmd' => 'Google\Task\Composer::cleanup' ], 'extra' => [ 'google/apiclient-services' => [ From b39ee320efbfb4096b4c4771a9b43d9d14d11746 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 23 Oct 2020 10:46:44 -0700 Subject: [PATCH 162/301] feat: add caching for GCE and access tokens by default (#1935) --- src/AuthHandler/Guzzle5AuthHandler.php | 9 ++++ src/AuthHandler/Guzzle6AuthHandler.php | 9 ++++ src/Client.php | 53 +++++++++++++++------ tests/Google/ClientTest.php | 65 ++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 14 deletions(-) diff --git a/src/AuthHandler/Guzzle5AuthHandler.php b/src/AuthHandler/Guzzle5AuthHandler.php index 92d226821..bf7440df1 100644 --- a/src/AuthHandler/Guzzle5AuthHandler.php +++ b/src/AuthHandler/Guzzle5AuthHandler.php @@ -39,6 +39,15 @@ public function attachCredentials( $this->cache ); } + + return $this->attachCredentialsCache($http, $credentials, $tokenCallback); + } + + public function attachCredentialsCache( + ClientInterface $http, + FetchAuthTokenCache $credentials, + callable $tokenCallback = null + ) { // if we end up needing to make an HTTP request to retrieve credentials, we // can use our existing one, but we need to throw exceptions so the error // bubbles up. diff --git a/src/AuthHandler/Guzzle6AuthHandler.php b/src/AuthHandler/Guzzle6AuthHandler.php index c1f2c3b51..35de17ce7 100644 --- a/src/AuthHandler/Guzzle6AuthHandler.php +++ b/src/AuthHandler/Guzzle6AuthHandler.php @@ -39,6 +39,15 @@ public function attachCredentials( $this->cache ); } + + return $this->attachCredentialsCache($http, $credentials, $tokenCallback); + } + + public function attachCredentialsCache( + ClientInterface $http, + FetchAuthTokenCache $credentials, + callable $tokenCallback = null + ) { // if we end up needing to make an HTTP request to retrieve credentials, we // can use our existing one, but we need to throw exceptions so the error // bubbles up. diff --git a/src/Client.php b/src/Client.php index 550fb311f..9871e5ff2 100644 --- a/src/Client.php +++ b/src/Client.php @@ -22,6 +22,7 @@ use Google\Auth\ApplicationDefaultCredentials; use Google\Auth\Cache\MemoryCacheItemPool; use Google\Auth\CredentialsLoader; +use Google\Auth\FetchAuthTokenCache; use Google\Auth\HttpHandler\HttpHandlerFactory; use Google\Auth\OAuth2; use Google\Auth\Credentials\ServiceAccountCredentials; @@ -152,6 +153,9 @@ public function __construct(array $config = array()) 'retry' => array(), 'retry_map' => null, + // Cache class implementing Psr\Cache\CacheItemPoolInterface. + // Defaults to Google\Auth\Cache\MemoryCacheItemPool. + 'cache' => null, // cache config for downstream auth caching 'cache_config' => [], @@ -192,6 +196,11 @@ public function __construct(array $config = array()) ); }; } + + if (!is_null($this->config['cache'])) { + $this->setCache($this->config['cache']); + unset($this->config['cache']); + } } /** @@ -399,9 +408,8 @@ public function authorize(ClientInterface $http = null) $credentials = null; $token = null; $scopes = null; - if (null === $http) { - $http = $this->getHttpClient(); - } + $http = $http ?: $this->getHttpClient(); + $authHandler = $this->getAuthHandler(); // These conditionals represent the decision tree for authentication // 1. Check for Application Default Credentials @@ -410,6 +418,11 @@ public function authorize(ClientInterface $http = null) // 3b. If access token exists but is expired, try to refresh it if ($this->isUsingApplicationDefaultCredentials()) { $credentials = $this->createApplicationDefaultCredentials(); + $http = $authHandler->attachCredentialsCache( + $http, + $credentials, + $this->config['token_callback'] + ); } elseif ($token = $this->getAccessToken()) { $scopes = $this->prepareScopes(); // add refresh subscriber to request a new token @@ -418,16 +431,14 @@ public function authorize(ClientInterface $http = null) $scopes, $token['refresh_token'] ); + $http = $authHandler->attachCredentials( + $http, + $credentials, + $this->config['token_callback'] + ); + } else { + $http = $authHandler->attachToken($http, $token, (array) $scopes); } - } - - $authHandler = $this->getAuthHandler(); - - if ($credentials) { - $callback = $this->config['token_callback']; - $http = $authHandler->attachCredentials($http, $credentials, $callback); - } elseif ($token) { - $http = $authHandler->attachToken($http, $token, (array) $scopes); } elseif ($key = $this->config['developer_key']) { $http = $authHandler->attachKey($http, $key); } @@ -1179,6 +1190,9 @@ protected function createDefaultHttpClient() return new GuzzleClient($options); } + /** + * @return FetchAuthTokenCache + */ private function createApplicationDefaultCredentials() { $scopes = $this->prepareScopes(); @@ -1199,11 +1213,14 @@ private function createApplicationDefaultCredentials() $serviceAccountCredentials ); } else { + // When $sub is provided, we cannot pass cache classes to ::getCredentials + // because FetchAuthTokenCache::setSub does not exist. + // The result is when $sub is provided, calls to ::onGce are not cached. $credentials = ApplicationDefaultCredentials::getCredentials( $scopes, null, - null, - null, + $sub ? null : $this->config['cache_config'], + $sub ? null : $this->getCache(), $this->config['quota_project'] ); } @@ -1218,6 +1235,14 @@ private function createApplicationDefaultCredentials() $credentials->setSub($sub); } + // If we are not using FetchAuthTokenCache yet, create it now + if (!$credentials instanceof FetchAuthTokenCache) { + $credentials = new FetchAuthTokenCache( + $credentials, + $this->config['cache_config'], + $this->getCache() + ); + } return $credentials; } diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index fe3b69c51..b53a0a397 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -763,6 +763,71 @@ public function testDefaultTokenCallback() $this->assertFalse($client->isAccessTokenExpired()); } + /** @runInSeparateProcess */ + public function testOnGceCacheAndCacheOptions() + { + if (!class_exists('Google\Auth\GCECache')) { + $this->markTestSkipped('Requires google/auth >= 1.12'); + } + + putenv('HOME='); + putenv('GOOGLE_APPLICATION_CREDENTIALS='); + $prefix = 'test_prefix_'; + $cacheConfig = ['gce_prefix' => $prefix]; + + $mockCacheItem = $this->prophesize('Psr\Cache\CacheItemInterface'); + $mockCacheItem->isHit() + ->willReturn(true); + $mockCacheItem->get() + ->shouldBeCalledTimes(1) + ->willReturn(true); + + $mockCache = $this->prophesize('Psr\Cache\CacheItemPoolInterface'); + $mockCache->getItem($prefix . Google\Auth\GCECache::GCE_CACHE_KEY) + ->shouldBeCalledTimes(1) + ->willReturn($mockCacheItem->reveal()); + + $client = new Google_Client(['cache_config' => $cacheConfig]); + $client->setCache($mockCache->reveal()); + $client->useApplicationDefaultCredentials(); + $client->authorize(); + } + + /** @runInSeparateProcess */ + public function testFetchAccessTokenWithAssertionCache() + { + $this->checkServiceAccountCredentials(); + $cachedValue = ['access_token' => '2/abcdef1234567890']; + $mockCacheItem = $this->prophesize('Psr\Cache\CacheItemInterface'); + $mockCacheItem->isHit() + ->shouldBeCalledTimes(1) + ->willReturn(true); + $mockCacheItem->get() + ->shouldBeCalledTimes(1) + ->willReturn($cachedValue); + + $mockCache = $this->prophesize('Psr\Cache\CacheItemPoolInterface'); + $mockCache->getItem(Argument::any()) + ->shouldBeCalledTimes(1) + ->willReturn($mockCacheItem->reveal()); + + $client = new Google_Client(); + $client->setCache($mockCache->reveal()); + $client->useApplicationDefaultCredentials(); + $token = $client->fetchAccessTokenWithAssertion(); + $this->assertArrayHasKey('access_token', $token); + $this->assertEquals($cachedValue['access_token'], $token['access_token']); + } + + public function testCacheClientOption() + { + $mockCache = $this->prophesize('Psr\Cache\CacheItemPoolInterface'); + $client = new Google_Client([ + 'cache' => $mockCache->reveal() + ]); + $this->assertEquals($mockCache->reveal(), $client->getCache()); + } + public function testExecuteWithFormat() { $this->onlyGuzzle6Or7(); From 2cad956f29f6587302d2dbed555d808253c8ced0 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 23 Oct 2020 11:41:58 -0700 Subject: [PATCH 163/301] fix: preserve BC for using Google_Task_Cleanup in composer.json (#1973) --- composer.json | 3 +++ src/aliases.php | 9 ++++++++- tests/Google/Task/ComposerTest.php | 25 +++++++++++++++---------- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index 14654d5b5..da143edb2 100644 --- a/composer.json +++ b/composer.json @@ -34,6 +34,9 @@ }, "files": [ "src/aliases.php" + ], + "classmap": [ + "src/aliases.php" ] }, "extra": { diff --git a/src/aliases.php b/src/aliases.php index 6e9c01ee9..a3a49aa53 100644 --- a/src/aliases.php +++ b/src/aliases.php @@ -14,7 +14,6 @@ 'Google\\Http\\Batch' => 'Google_Http_Batch', 'Google\\Http\\MediaFileUpload' => 'Google_Http_MediaFileUpload', 'Google\\Http\\REST' => 'Google_Http_REST', - 'Google\\Task\\Composer' => 'Google_Task_Composer', 'Google\\Task\\Retryable' => 'Google_Task_Retryable', 'Google\\Task\\Exception' => 'Google_Task_Exception', 'Google\\Task\\Runner' => 'Google_Task_Runner', @@ -27,3 +26,11 @@ foreach ($classMap as $class => $alias) { class_alias($class, $alias); } + +/** + * This class needs to be defined explicitly as scripts must be recognized by + * the autoloader. + */ +class Google_Task_Composer extends \Google\Task\Composer +{ +} diff --git a/tests/Google/Task/ComposerTest.php b/tests/Google/Task/ComposerTest.php index bb1cfbde9..d080aed32 100644 --- a/tests/Google/Task/ComposerTest.php +++ b/tests/Google/Task/ComposerTest.php @@ -147,13 +147,25 @@ private function createMockEvent( public function testE2E() { - $composerJson = json_encode([ + $composer = [ + 'repositories' => [ + [ + 'type' => 'path', + 'url' => __DIR__ . '/../../..', + 'options' => [ + 'symlink' => false + ] + ] + ], 'require' => [ 'google/apiclient' => 'dev-master' ], 'scripts' => [ 'post-update-cmd' => 'Google\Task\Composer::cleanup' ], + ]; + + $composerJson = json_encode($composer + [ 'extra' => [ 'google/apiclient-services' => [ 'Drive', @@ -163,12 +175,11 @@ public function testE2E() ]); $tmpDir = sys_get_temp_dir() . '/test-' . rand(); - $serviceDir = $tmpDir . '/vendor/google/apiclient-services/src/Google/Service'; - mkdir($tmpDir); file_put_contents($tmpDir . '/composer.json', $composerJson); passthru('composer install -d ' . $tmpDir); + $serviceDir = $tmpDir . '/vendor/google/apiclient-services/src/Google/Service'; $this->assertFileExists($serviceDir . '/Drive.php'); $this->assertFileExists($serviceDir . '/Drive'); $this->assertFileExists($serviceDir . '/YouTube.php'); @@ -176,13 +187,7 @@ public function testE2E() $this->assertFileNotExists($serviceDir . '/YouTubeReporting.php'); $this->assertFileNotExists($serviceDir . '/YouTubeReporting'); - $composerJson = json_encode([ - 'require' => [ - 'google/apiclient' => 'dev-master' - ], - 'scripts' => [ - 'post-update-cmd' => 'Google\Task\Composer::cleanup' - ], + $composerJson = json_encode($composer + [ 'extra' => [ 'google/apiclient-services' => [ 'Drive', From b2bf3609a746e58a99b70638005757eb78a47e24 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 23 Oct 2020 13:00:58 -0700 Subject: [PATCH 164/301] chore: add composer task test case (#1974) --- tests/Google/Task/ComposerTest.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/Google/Task/ComposerTest.php b/tests/Google/Task/ComposerTest.php index d080aed32..985120d4b 100644 --- a/tests/Google/Task/ComposerTest.php +++ b/tests/Google/Task/ComposerTest.php @@ -207,5 +207,26 @@ public function testE2E() $this->assertFileExists($serviceDir . '/YouTube'); $this->assertFileExists($serviceDir . '/YouTubeReporting.php'); $this->assertFileExists($serviceDir . '/YouTubeReporting'); + + // Test BC Task name + $composer['scripts']['post-update-cmd'] = 'Google_Task_Composer::cleanup'; + $composerJson = json_encode($composer + [ + 'extra' => [ + 'google/apiclient-services' => [ + 'Drive', + ] + ] + ]); + + file_put_contents($tmpDir . '/composer.json', $composerJson); + passthru('rm -r ' . $tmpDir . '/vendor/google/apiclient-services'); + passthru('composer update -d ' . $tmpDir); + + $this->assertFileExists($serviceDir . '/Drive.php'); + $this->assertFileExists($serviceDir . '/Drive'); + $this->assertFileNotExists($serviceDir . '/YouTube.php'); + $this->assertFileNotExists($serviceDir . '/YouTube'); + $this->assertFileNotExists($serviceDir . '/YouTubeReporting.php'); + $this->assertFileNotExists($serviceDir . '/YouTubeReporting'); } } From cf9a070f9da78cd207a69b2a94832d381c8c4163 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 23 Oct 2020 13:29:29 -0700 Subject: [PATCH 165/301] chore: prepare v2.8.0 (#1972) --- src/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index 9871e5ff2..9353c921e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -49,7 +49,7 @@ */ class Client { - const LIBVER = "2.7.2"; + const LIBVER = "2.8.0"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; From d4eb15d8a2eee321fe8963db6f3c98e14cb19c32 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 27 Oct 2020 13:12:54 -0700 Subject: [PATCH 166/301] chore: GH actions fixes for composer 2 (#1979) --- .github/workflows/tests.yml | 6 +++--- composer.json | 2 +- tests/Google/Task/ComposerTest.php | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index cbd2c316b..141fcd774 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -56,7 +56,7 @@ jobs: with: php-version: 5.4 - name: Remove cache library - run: composer remove --dev cache/filesystem-adapter + run: composer remove --dev --no-update cache/filesystem-adapter - name: Install Dependencies uses: nick-invision/retry@v1 with: @@ -76,7 +76,7 @@ jobs: with: php-version: 5.4 - name: Remove cache library - run: composer remove --dev cache/filesystem-adapter + run: composer remove --dev --no-update cache/filesystem-adapter - name: Install Dependencies uses: nick-invision/retry@v1 with: @@ -94,7 +94,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: "7.4" + php-version: "7.3" - name: Install Dependencies uses: nick-invision/retry@v1 with: diff --git a/composer.json b/composer.json index da143edb2..3f058bbc6 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "symfony/css-selector": "~2.1", "cache/filesystem-adapter": "^0.3.2", "phpcompatibility/php-compatibility": "^9.2", - "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0", + "dealerdirect/phpcodesniffer-composer-installer": "^0.7", "composer/composer": "^1.10" }, "suggest": { diff --git a/tests/Google/Task/ComposerTest.php b/tests/Google/Task/ComposerTest.php index 985120d4b..faf9f32dc 100644 --- a/tests/Google/Task/ComposerTest.php +++ b/tests/Google/Task/ComposerTest.php @@ -158,11 +158,12 @@ public function testE2E() ] ], 'require' => [ - 'google/apiclient' => 'dev-master' + 'google/apiclient' => '*' ], 'scripts' => [ 'post-update-cmd' => 'Google\Task\Composer::cleanup' ], + 'minimum-stability' => 'dev', ]; $composerJson = json_encode($composer + [ From 9ee25612858b9d8b1524469a5c4eeb37bc0ee805 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 27 Oct 2020 13:17:48 -0700 Subject: [PATCH 167/301] fix: preload error with aliases.php (#1978) --- src/aliases.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/aliases.php b/src/aliases.php index a3a49aa53..c5f34755c 100644 --- a/src/aliases.php +++ b/src/aliases.php @@ -1,5 +1,11 @@ 'Google_Client', 'Google\\Service' => 'Google_Service', From 80fa4c919e407d4851eb5f65097b2b650a64b4c3 Mon Sep 17 00:00:00 2001 From: Dmitry Khaperets Date: Tue, 27 Oct 2020 22:58:34 +0200 Subject: [PATCH 168/301] chore: updated phpdoc classnames (#1977) --- src/Client.php | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Client.php b/src/Client.php index 9353c921e..efc1d1240 100644 --- a/src/Client.php +++ b/src/Client.php @@ -57,17 +57,17 @@ class Client const API_BASE_PATH = 'https://www.googleapis.com/'; /** - * @var Google\Auth\OAuth2 $auth + * @var OAuth2 $auth */ private $auth; /** - * @var GuzzleHttp\ClientInterface $http + * @var ClientInterface $http */ private $http; /** - * @var Psr\Cache\CacheItemPoolInterface $cache + * @var CacheItemPoolInterface $cache */ private $cache; @@ -82,7 +82,7 @@ class Client private $config; /** - * @var Psr\Log\LoggerInterface $logger + * @var LoggerInterface $logger */ private $logger; @@ -400,8 +400,8 @@ public function createAuthUrl($scope = null) * Adds auth listeners to the HTTP client based on the credentials * set in the Google API Client object * - * @param GuzzleHttp\ClientInterface $http the http client object. - * @return GuzzleHttp\ClientInterface the http client object + * @param ClientInterface $http the http client object. + * @return ClientInterface the http client object */ public function authorize(ClientInterface $http = null) { @@ -853,9 +853,9 @@ public function prepareScopes() /** * Helper method to execute deferred HTTP requests. * - * @param $request Psr\Http\Message\RequestInterface|Google\Http\Batch + * @param $request RequestInterface|\Google\Http\Batch * @param string $expectedClass - * @throws Google\Exception + * @throws \Google\Exception * @return object of the type of the expected class or Psr\Http\Message\ResponseInterface. */ public function execute(RequestInterface $request, $expectedClass = null) @@ -1025,7 +1025,7 @@ public function shouldDefer() } /** - * @return Google\Auth\OAuth2 implementation + * @return OAuth2 implementation */ public function getOAuth2Service() { @@ -1059,7 +1059,7 @@ protected function createOAuth2Service() /** * Set the Cache object - * @param Psr\Cache\CacheItemPoolInterface $cache + * @param CacheItemPoolInterface $cache */ public function setCache(CacheItemPoolInterface $cache) { @@ -1067,7 +1067,7 @@ public function setCache(CacheItemPoolInterface $cache) } /** - * @return Psr\Cache\CacheItemPoolInterface Cache implementation + * @return CacheItemPoolInterface Cache implementation */ public function getCache() { @@ -1088,7 +1088,7 @@ public function setCacheConfig(array $cacheConfig) /** * Set the Logger object - * @param Psr\Log\LoggerInterface $logger + * @param LoggerInterface $logger */ public function setLogger(LoggerInterface $logger) { @@ -1096,7 +1096,7 @@ public function setLogger(LoggerInterface $logger) } /** - * @return Psr\Log\LoggerInterface implementation + * @return LoggerInterface implementation */ public function getLogger() { @@ -1127,7 +1127,7 @@ protected function createDefaultCache() /** * Set the Http Client object - * @param GuzzleHttp\ClientInterface $http + * @param ClientInterface $http */ public function setHttpClient(ClientInterface $http) { @@ -1135,7 +1135,7 @@ public function setHttpClient(ClientInterface $http) } /** - * @return GuzzleHttp\ClientInterface implementation + * @return ClientInterface implementation */ public function getHttpClient() { From fa3641c1a6e3e8832d70e70e93b42327c4efab78 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 27 Oct 2020 14:53:18 -0700 Subject: [PATCH 169/301] chore: fix docblock namespaced references (#1980) --- src/AccessToken/Revoke.php | 2 +- src/AccessToken/Verify.php | 6 +++--- src/Client.php | 10 +++++----- src/Http/Batch.php | 2 +- src/Http/MediaFileUpload.php | 8 ++++---- src/Http/REST.php | 18 +++++++++--------- src/Model.php | 2 +- src/Service.php | 4 ++-- src/Service/Exception.php | 2 +- src/Service/Resource.php | 6 +++--- src/Task/Composer.php | 2 +- src/Task/Runner.php | 4 ++-- 12 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/AccessToken/Revoke.php b/src/AccessToken/Revoke.php index d2d272454..45b60548e 100644 --- a/src/AccessToken/Revoke.php +++ b/src/AccessToken/Revoke.php @@ -31,7 +31,7 @@ class Revoke { /** - * @var GuzzleHttp\ClientInterface The http client + * @var ClientInterface The http client */ private $http; diff --git a/src/AccessToken/Verify.php b/src/AccessToken/Verify.php index c9ddf8541..96fabdefd 100644 --- a/src/AccessToken/Verify.php +++ b/src/AccessToken/Verify.php @@ -44,12 +44,12 @@ class Verify const OAUTH2_ISSUER_HTTPS = 'https://accounts.google.com/'; /** - * @var GuzzleHttp\ClientInterface The http client + * @var ClientInterface The http client */ private $http; /** - * @var Psr\Cache\CacheItemPoolInterface cache class + * @var CacheItemPoolInterface cache class */ private $cache; @@ -149,7 +149,7 @@ private function getCache() * Retrieve and cache a certificates file. * * @param $url string location - * @throws GoogleException + * @throws \Google\Exception * @return array certificates */ private function retrieveCertsFromLocation($url) diff --git a/src/Client.php b/src/Client.php index efc1d1240..00fda3fa1 100644 --- a/src/Client.php +++ b/src/Client.php @@ -937,7 +937,7 @@ public function getConfig($name, $default = null) * alias for setAuthConfig * * @param string $file the configuration file - * @throws Google\Exception + * @throws \Google\Exception * @deprecated */ public function setAuthConfigFile($file) @@ -951,7 +951,7 @@ public function setAuthConfigFile($file) * the "Download JSON" button on in the Google Developer * Console. * @param string|array $config the configuration json - * @throws Google\Exception + * @throws \Google\Exception */ public function setAuthConfig($config) { @@ -1067,7 +1067,7 @@ public function setCache(CacheItemPoolInterface $cache) } /** - * @return CacheItemPoolInterface Cache implementation + * @return CacheItemPoolInterface */ public function getCache() { @@ -1096,7 +1096,7 @@ public function setLogger(LoggerInterface $logger) } /** - * @return LoggerInterface implementation + * @return LoggerInterface */ public function getLogger() { @@ -1135,7 +1135,7 @@ public function setHttpClient(ClientInterface $http) } /** - * @return ClientInterface implementation + * @return ClientInterface */ public function getHttpClient() { diff --git a/src/Http/Batch.php b/src/Http/Batch.php index 391ea457a..a4607586e 100644 --- a/src/Http/Batch.php +++ b/src/Http/Batch.php @@ -93,7 +93,7 @@ public function execute() EOF; - /** @var Google\Http\Request $req */ + /** @var RequestInterface $req */ foreach ($this->requests as $key => $request) { $firstLine = sprintf( '%s %s HTTP/%s', diff --git a/src/Http/MediaFileUpload.php b/src/Http/MediaFileUpload.php index c98169d18..82a051abf 100644 --- a/src/Http/MediaFileUpload.php +++ b/src/Http/MediaFileUpload.php @@ -56,10 +56,10 @@ class MediaFileUpload /** @var int $progress */ private $progress; - /** @var Google\Client */ + /** @var Client */ private $client; - /** @var Psr\Http\Message\RequestInterface */ + /** @var RequestInterface */ private $request; /** @var string */ @@ -160,7 +160,7 @@ public function getHttpResultCode() * Sends a PUT-Request to google drive and parses the response, * setting the appropiate variables from the response() * - * @param Google\Http\Request $httpRequest the Reuqest which will be send + * @param RequestInterface $request the Request which will be send * * @return false|mixed false when the upload is unfinished or the decoded http response * @@ -212,7 +212,7 @@ public function resume($resumeUri) } /** - * @return Psr\Http\Message\RequestInterface $request + * @return RequestInterface * @visible for testing */ private function process() diff --git a/src/Http/REST.php b/src/Http/REST.php index 014932809..691982271 100644 --- a/src/Http/REST.php +++ b/src/Http/REST.php @@ -36,13 +36,13 @@ class REST * Executes a Psr\Http\Message\RequestInterface and (if applicable) automatically retries * when errors occur. * - * @param Google\Client $client - * @param Psr\Http\Message\RequestInterface $req + * @param Client $client + * @param RequestInterface $req * @param string $expectedClass * @param array $config * @param array $retryMap * @return array decoded result - * @throws Google\Service\Exception on server side error (ie: not authenticated, + * @throws \Google\Service\Exception on server side error (ie: not authenticated, * invalid or malformed post body, invalid url) */ public static function execute( @@ -69,11 +69,11 @@ public static function execute( /** * Executes a Psr\Http\Message\RequestInterface * - * @param Google\Client $client - * @param Psr\Http\Message\RequestInterface $request + * @param Client $client + * @param RequestInterface $request * @param string $expectedClass * @return array decoded result - * @throws Google\Service\Exception on server side error (ie: not authenticated, + * @throws \Google\Service\Exception on server side error (ie: not authenticated, * invalid or malformed post body, invalid url) */ public static function doExecute(ClientInterface $client, RequestInterface $request, $expectedClass = null) @@ -106,9 +106,9 @@ public static function doExecute(ClientInterface $client, RequestInterface $requ /** * Decode an HTTP Response. * @static - * @throws Google\Service\Exception - * @param Psr\Http\Message\RequestInterface $response The http response to be decoded. - * @param Psr\Http\Message\ResponseInterface $response + * @throws \Google\Service\Exception + * @param RequestInterface $response The http response to be decoded. + * @param ResponseInterface $response * @param string $expectedClass * @return mixed|null */ diff --git a/src/Model.php b/src/Model.php index 667899fe4..18f8917b0 100644 --- a/src/Model.php +++ b/src/Model.php @@ -240,7 +240,7 @@ protected function isAssociativeArray($array) /** * Verify if $obj is an array. - * @throws Google\Exception Thrown if $obj isn't an array. + * @throws \Google\Exception Thrown if $obj isn't an array. * @param array $obj Items that should be validated. * @param string $method Method expecting an array as an argument. */ diff --git a/src/Service.php b/src/Service.php index 0257abd43..7d3052499 100644 --- a/src/Service.php +++ b/src/Service.php @@ -47,7 +47,7 @@ public function __construct($clientOrConfig = []) /** * Return the associated Google\Client class. - * @return Google\Client + * @return \Google\Client */ public function getClient() { @@ -57,7 +57,7 @@ public function getClient() /** * Create a new HTTP Batch handler for this service * - * @return Google\Http\Batch + * @return Batch */ public function createBatch() { diff --git a/src/Service/Exception.php b/src/Service/Exception.php index 12464d4af..3270ad7f3 100644 --- a/src/Service/Exception.php +++ b/src/Service/Exception.php @@ -32,7 +32,7 @@ class Exception extends GoogleException * * @param string $message * @param int $code - * @param Exception|null $previous + * @param \Exception|null $previous * @param [{string, string}] errors List of errors returned in an HTTP * response. Defaults to []. */ diff --git a/src/Service/Resource.php b/src/Service/Resource.php index 8d1ea8bc0..be0014532 100644 --- a/src/Service/Resource.php +++ b/src/Service/Resource.php @@ -48,7 +48,7 @@ class Resource /** @var string $rootUrl */ private $rootUrl; - /** @var Google\Client $client */ + /** @var \Google\Client $client */ private $client; /** @var string $serviceName */ @@ -80,8 +80,8 @@ public function __construct($service, $serviceName, $resourceName, $resource) * @param $name * @param $arguments * @param $expectedClass - optional, the expected class name - * @return Google\Http\Request|expectedClass - * @throws Google\Exception + * @return Request|$expectedClass + * @throws \Google\Exception */ public function call($name, $arguments, $expectedClass = null) { diff --git a/src/Task/Composer.php b/src/Task/Composer.php index 32ca2de75..8fcf13b5b 100644 --- a/src/Task/Composer.php +++ b/src/Task/Composer.php @@ -26,7 +26,7 @@ class Composer { /** * @param Event $event Composer event passed in for any script method - * @param FilesystemInterface $filesystem Optional. Used for testing. + * @param Filesystem $filesystem Optional. Used for testing. */ public static function cleanup( Event $event, diff --git a/src/Task/Runner.php b/src/Task/Runner.php index ad1c56ab1..34fe37835 100644 --- a/src/Task/Runner.php +++ b/src/Task/Runner.php @@ -91,7 +91,7 @@ class Runner * @param string $name The name of the current task (used for logging) * @param callable $action The task to run and possibly retry * @param array $arguments The task arguments - * @throws Google\Task\Exception when misconfigured + * @throws \Google\Task\Exception when misconfigured */ public function __construct( $config, @@ -172,7 +172,7 @@ public function canAttempt() * Runs the task and (if applicable) automatically retries when errors occur. * * @return mixed - * @throws Google\Task\Retryable on failure when no retries are available. + * @throws \Google\Service\Exception on failure when no retries are available. */ public function run() { From c8f6d09f50f859fa9457104bb0fb72c893804ede Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 27 Oct 2020 16:20:13 -0700 Subject: [PATCH 170/301] chore: prepare v2.8.1 (#1981) --- src/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index 00fda3fa1..57087134e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -49,7 +49,7 @@ */ class Client { - const LIBVER = "2.8.0"; + const LIBVER = "2.8.1"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; From 7a92d4d825ee0520428586198c16885eb3a4681c Mon Sep 17 00:00:00 2001 From: Illia Hai Date: Wed, 11 Nov 2020 07:24:05 +0200 Subject: [PATCH 171/301] fix: avoid autoload in aliases.php (#1991) --- src/aliases.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aliases.php b/src/aliases.php index c5f34755c..6949209cc 100644 --- a/src/aliases.php +++ b/src/aliases.php @@ -1,6 +1,6 @@ Date: Wed, 11 Nov 2020 06:41:16 -0800 Subject: [PATCH 172/301] ci: fix github actions asset release for composer 2 (#1993) --- .github/workflows/asset-release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/asset-release.yml b/.github/workflows/asset-release.yml index b4efbcbf7..6124ac21f 100644 --- a/.github/workflows/asset-release.yml +++ b/.github/workflows/asset-release.yml @@ -39,7 +39,7 @@ jobs: with: timeout_minutes: 10 max_attempts: 3 - command: composer remove --dev cache/filesystem-adapter && composer install --no-dev --prefer-dist + command: composer remove --no-update --dev cache/filesystem-adapter && composer install --no-dev --prefer-dist - name: Create Archive run: | From 24f3ec0c79567a08c31f8271b61d79b050b25b0a Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 11 Nov 2020 09:07:53 -0800 Subject: [PATCH 173/301] chore: prepare v2.8.2 (#1992) --- src/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index 57087134e..5f5b6acc7 100644 --- a/src/Client.php +++ b/src/Client.php @@ -49,7 +49,7 @@ */ class Client { - const LIBVER = "2.8.1"; + const LIBVER = "2.8.2"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; From 48bec521727fea207f545f33022aeed1fc605b92 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 17 Nov 2020 09:10:45 -0800 Subject: [PATCH 174/301] fix: add classes in aliases.php to fix IDE error (#1995) --- phpcs.xml.dist | 8 ++++++-- src/aliases.php | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 4e4306986..926146867 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -89,7 +89,9 @@ - + + src/aliases\.php + - - - - - diff --git a/tests/Google/Http/RESTTest.php b/tests/Google/Http/RESTTest.php index b090fdd77..9f28cc269 100644 --- a/tests/Google/Http/RESTTest.php +++ b/tests/Google/Http/RESTTest.php @@ -26,7 +26,7 @@ class Google_HTTP_RESTTest extends BaseTest */ private $rest; - public function setUp() + public function setUp(): void { $this->rest = new Google_Http_REST(); $this->request = new Request('GET', '/'); diff --git a/tests/Google/Service/AdSenseTest.php b/tests/Google/Service/AdSenseTest.php index 14dbb3b1e..42268c75f 100644 --- a/tests/Google/Service/AdSenseTest.php +++ b/tests/Google/Service/AdSenseTest.php @@ -18,7 +18,7 @@ class Google_Service_AdSenseTest extends BaseTest { public $adsense; - public function setUp() + public function setUp(): void { $this->checkToken(); $this->adsense = new Google_Service_AdSense($this->getClient()); diff --git a/tests/Google/Service/ResourceTest.php b/tests/Google/Service/ResourceTest.php index 74cdc711d..8da5a7aa6 100644 --- a/tests/Google/Service/ResourceTest.php +++ b/tests/Google/Service/ResourceTest.php @@ -55,7 +55,7 @@ class Google_Service_ResourceTest extends BaseTest private $client; private $service; - public function setUp() + public function setUp(): void { $this->client = $this->prophesize("Google_Client"); diff --git a/tests/Google/Service/TasksTest.php b/tests/Google/Service/TasksTest.php index 0cae03732..87361bcd0 100644 --- a/tests/Google/Service/TasksTest.php +++ b/tests/Google/Service/TasksTest.php @@ -20,7 +20,7 @@ class Google_Service_TasksTest extends BaseTest /** @var Google_TasksService */ public $taskService; - public function setUp() + public function setUp(): void { $this->checkToken(); $this->taskService = new Google_Service_Tasks($this->getClient()); diff --git a/tests/Google/Service/YouTubeTest.php b/tests/Google/Service/YouTubeTest.php index b7745f8ae..2a9381a61 100644 --- a/tests/Google/Service/YouTubeTest.php +++ b/tests/Google/Service/YouTubeTest.php @@ -19,7 +19,7 @@ class Google_Service_YouTubeTest extends BaseTest { /** @var Google_Service_YouTube */ public $youtube; - public function setUp() + public function setUp(): void { $this->checkToken(); $this->youtube = new Google_Service_YouTube($this->getClient()); diff --git a/tests/Google/Task/RunnerTest.php b/tests/Google/Task/RunnerTest.php index 1dd93a0ec..3d421328b 100644 --- a/tests/Google/Task/RunnerTest.php +++ b/tests/Google/Task/RunnerTest.php @@ -32,7 +32,7 @@ class Google_Task_RunnerTest extends BaseTest private $retryMap; private $retryConfig; - protected function setUp() + protected function setUp(): void { $this->client = new Google_Client(); } @@ -288,7 +288,8 @@ public function testCurlTimeouts($config, $minTime) */ public function testBadTaskConfig($config, $message) { - $this->setExpectedException('Google_Task_Exception', $message); + $this->expectException('Google_Task_Exception'); + $this->expectExceptionMessage($message); $this->setRetryConfig($config); new Google_Task_Runner( From 791f2b26a73ef6954e41e110bfc6053daa277163 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 22 Dec 2020 13:18:03 -0800 Subject: [PATCH 177/301] chore: add prefer-lowest actions to unit test matrix (#2021) --- .github/sync-repo-settings.yaml | 2 ++ .github/workflows/tests.yml | 22 +++++++++++++++++----- composer.json | 6 +++--- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/.github/sync-repo-settings.yaml b/.github/sync-repo-settings.yaml index 74cb44ff2..6e411b4c9 100644 --- a/.github/sync-repo-settings.yaml +++ b/.github/sync-repo-settings.yaml @@ -6,12 +6,14 @@ branchProtectionRules: isAdminEnforced: true requiredStatusCheckContexts: - 'PHP 5.6 Unit Test' + - 'PHP 5.6 --prefer-lowest Unit Test' - 'PHP 7.0 Unit Test' - 'PHP 7.1 Unit Test' - 'PHP 7.2 Unit Test' - 'PHP 7.3 Unit Test' - 'PHP 7.4 Unit Test' - 'PHP 8.0 Unit Test' + - 'PHP 8.0 --prefer-lowest Unit Test' - 'PHP Style Check' - 'cla/google' requiredApprovingReviewCount: 1 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 6dc9a1233..3ff647cbb 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,15 +1,24 @@ name: Test Suite -on: [push, pull_request] +on: + push: + branches: + - master + pull_request: jobs: test: - runs-on: ${{ matrix.operating-system }} + runs-on: ubuntu-latest strategy: fail-fast: false matrix: - operating-system: [ ubuntu-latest ] php: [ "5.6", "7.0", "7.1", "7.2", "7.3", "7.4", "8.0" ] - name: PHP ${{ matrix.php }} Unit Test + composer-flags: [""] + include: + - php: "5.6" + composer-flags: "--prefer-lowest" + - php: "8.0" + composer-flags: "--prefer-lowest" + name: PHP ${{ matrix.php }} ${{ matrix.composer-flags }} Unit Test steps: - uses: actions/checkout@v2 - name: Setup PHP @@ -21,7 +30,10 @@ jobs: with: timeout_minutes: 10 max_attempts: 3 - command: composer install + command: composer update ${{ matrix.composer-flags }} + - if: ${{ matrix.php == '8.0' || matrix.composer-flags == '--prefer-lowest' }} + name: Update guzzlehttp/ringphp dependency + run: composer update guzzlehttp/ringphp - if: ${{ matrix.php == '5.6' || matrix.php == '7.0' || matrix.php == '7.1' }} name: Run PHPUnit Patches run: sh .github/apply-phpunit-patches.sh diff --git a/composer.json b/composer.json index 6455f5ef4..016fe6bcb 100644 --- a/composer.json +++ b/composer.json @@ -11,12 +11,12 @@ "google/apiclient-services": "~0.13", "firebase/php-jwt": "~2.0||~3.0||~4.0||~5.0", "monolog/monolog": "^1.17|^2.0", - "phpseclib/phpseclib": "~0.3.10||~2.0", - "guzzlehttp/guzzle": "~5.3.1||~6.0||~7.0", + "phpseclib/phpseclib": "~2.0", + "guzzlehttp/guzzle": "~5.3.3||~6.0||~7.0", "guzzlehttp/psr7": "^1.2" }, "require-dev": { - "phpunit/phpunit": "^5.0||^8.5", + "phpunit/phpunit": "^5.7||^8.5.13", "squizlabs/php_codesniffer": "~2.3", "symfony/dom-crawler": "~2.1", "symfony/css-selector": "~2.1", From 120c695f6a2a564a3e2d83a4846d2d627637fca1 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 28 Dec 2020 11:01:22 -0700 Subject: [PATCH 178/301] fix: ensure names match sync repo settings (#2022) --- .github/workflows/tests.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3ff647cbb..d758fa9c4 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -15,10 +15,10 @@ jobs: composer-flags: [""] include: - php: "5.6" - composer-flags: "--prefer-lowest" + composer-flags: "--prefer-lowest " - php: "8.0" - composer-flags: "--prefer-lowest" - name: PHP ${{ matrix.php }} ${{ matrix.composer-flags }} Unit Test + composer-flags: "--prefer-lowest " + name: PHP ${{ matrix.php }} ${{ matrix.composer-flags }}Unit Test steps: - uses: actions/checkout@v2 - name: Setup PHP From 8ed1dc8709caa0ecf9ad0a4b517b83b97d38c3b5 Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Mon, 28 Dec 2020 13:14:55 -0500 Subject: [PATCH 179/301] chore: fix asset release action (#2015) --- .github/workflows/asset-release.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/asset-release.yml b/.github/workflows/asset-release.yml index 02a224c8b..dd79db3d2 100644 --- a/.github/workflows/asset-release.yml +++ b/.github/workflows/asset-release.yml @@ -14,8 +14,9 @@ jobs: name: Upload Release Assets steps: - - uses: olegtarasov/get-tag@v2 - id: tagName + - id: getTag + name: Get Tag + run: echo ::set-output name=tag::${GITHUB_REF#refs/*/} - uses: octokit/request-action@v2.x id: getLatestRelease From e0753f9fb61e514b821108fcde665a10e1cce51c Mon Sep 17 00:00:00 2001 From: Kyle Date: Mon, 28 Dec 2020 19:40:46 +0100 Subject: [PATCH 180/301] feat: support phpseclib3 (#2019) --- composer.json | 2 +- src/AccessToken/Verify.php | 45 ++++++++++++++++++++----- tests/Google/AccessToken/VerifyTest.php | 4 +++ 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/composer.json b/composer.json index 016fe6bcb..c29adc2c3 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "google/apiclient-services": "~0.13", "firebase/php-jwt": "~2.0||~3.0||~4.0||~5.0", "monolog/monolog": "^1.17|^2.0", - "phpseclib/phpseclib": "~2.0", + "phpseclib/phpseclib": "~2.0||^3.0.2", "guzzlehttp/guzzle": "~5.3.3||~6.0||~7.0", "guzzlehttp/psr7": "^1.2" }, diff --git a/src/AccessToken/Verify.php b/src/AccessToken/Verify.php index 96fabdefd..fa997f211 100644 --- a/src/AccessToken/Verify.php +++ b/src/AccessToken/Verify.php @@ -22,6 +22,8 @@ use Firebase\JWT\SignatureInvalidException; use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; +use phpseclib3\Crypt\PublicKeyLoader; +use phpseclib3\Crypt\RSA\PublicKey; use Psr\Cache\CacheItemPoolInterface; use Google\Auth\Cache\MemoryCacheItemPool; use Google\Exception as GoogleException; @@ -97,18 +99,10 @@ public function verifyIdToken($idToken, $audience = null) // Check signature $certs = $this->getFederatedSignOnCerts(); foreach ($certs as $cert) { - $bigIntClass = $this->getBigIntClass(); - $rsaClass = $this->getRsaClass(); - $modulus = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['n']), 256); - $exponent = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['e']), 256); - - $rsa = new $rsaClass(); - $rsa->loadKey(array('n' => $modulus, 'e' => $exponent)); - try { $payload = $this->jwt->decode( $idToken, - $rsa->getPublicKey(), + $this->getPublicKey($cert), array('RS256') ); @@ -229,8 +223,33 @@ private function getJwtService() return new $jwtClass; } + private function getPublicKey($cert) + { + $bigIntClass = $this->getBigIntClass(); + $modulus = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['n']), 256); + $exponent = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['e']), 256); + $component = array('n' => $modulus, 'e' => $exponent); + + if (class_exists('phpseclib3\Crypt\RSA\PublicKey')) { + /** @var PublicKey $loader */ + $loader = PublicKeyLoader::load($component); + + return $loader->toString('PKCS8'); + } + + $rsaClass = $this->getRsaClass(); + $rsa = new $rsaClass(); + $rsa->loadKey($component); + + return $rsa->getPublicKey(); + } + private function getRsaClass() { + if (class_exists('phpseclib3\Crypt\RSA')) { + return 'phpseclib3\Crypt\RSA'; + } + if (class_exists('phpseclib\Crypt\RSA')) { return 'phpseclib\Crypt\RSA'; } @@ -240,6 +259,10 @@ private function getRsaClass() private function getBigIntClass() { + if (class_exists('phpseclib3\Math\BigInteger')) { + return 'phpseclib3\Math\BigInteger'; + } + if (class_exists('phpseclib\Math\BigInteger')) { return 'phpseclib\Math\BigInteger'; } @@ -249,6 +272,10 @@ private function getBigIntClass() private function getOpenSslConstant() { + if (class_exists('phpseclib3\Crypt\AES')) { + return 'phpseclib3\Crypt\AES::ENGINE_OPENSSL'; + } + if (class_exists('phpseclib\Crypt\RSA')) { return 'phpseclib\Crypt\RSA::MODE_OPENSSL'; } diff --git a/tests/Google/AccessToken/VerifyTest.php b/tests/Google/AccessToken/VerifyTest.php index bce9501b4..dbcaa6f33 100644 --- a/tests/Google/AccessToken/VerifyTest.php +++ b/tests/Google/AccessToken/VerifyTest.php @@ -139,6 +139,10 @@ private function getJwtService() private function getOpenSslConstant() { + if (class_exists('phpseclib3\Crypt\AES')) { + return 'phpseclib3\Crypt\AES::ENGINE_OPENSSL'; + } + if (class_exists('phpseclib\Crypt\RSA')) { return 'phpseclib\Crypt\RSA::MODE_OPENSSL'; } From 09028dc17e2ee95f1ffc382bdef2e1c54a551d87 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Tue, 19 Jan 2021 07:54:28 -0700 Subject: [PATCH 181/301] fix: exclude PageSpeed Insights API errors from retries. (#2010) --- src/Task/Runner.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Task/Runner.php b/src/Task/Runner.php index 34fe37835..c081dc591 100644 --- a/src/Task/Runner.php +++ b/src/Task/Runner.php @@ -81,7 +81,8 @@ class Runner 7 => self::TASK_RETRY_ALWAYS, // CURLE_COULDNT_CONNECT 28 => self::TASK_RETRY_ALWAYS, // CURLE_OPERATION_TIMEOUTED 35 => self::TASK_RETRY_ALWAYS, // CURLE_SSL_CONNECT_ERROR - 52 => self::TASK_RETRY_ALWAYS // CURLE_GOT_NOTHING + 52 => self::TASK_RETRY_ALWAYS, // CURLE_GOT_NOTHING + 'lighthouseError' => self::TASK_RETRY_NEVER ]; /** From 2fa15d9db4ce653dbb128bd6b08eff9e6c0702ae Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Tue, 19 Jan 2021 10:19:03 -0500 Subject: [PATCH 182/301] chore: prepare v2.9.0 (#2029) Co-authored-by: Brent Shaffer --- src/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index 269b0140a..e53e88e73 100644 --- a/src/Client.php +++ b/src/Client.php @@ -49,7 +49,7 @@ */ class Client { - const LIBVER = "2.8.3"; + const LIBVER = "2.9.0"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; From 7a09507beff1048cc9a29417f78a98284f06214f Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Tue, 19 Jan 2021 12:31:44 -0500 Subject: [PATCH 183/301] chore: fix release and documentation actions (#2030) --- .github/actions/docs/entrypoint.sh | 7 +++++-- .github/workflows/asset-release.yml | 11 ++++------- .github/workflows/docs.yml | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/actions/docs/entrypoint.sh b/.github/actions/docs/entrypoint.sh index 4d9c79fb4..203f98e62 100755 --- a/.github/actions/docs/entrypoint.sh +++ b/.github/actions/docs/entrypoint.sh @@ -1,12 +1,15 @@ #!/bin/sh -l apt-get update -apt-get install -y git +apt-get install -y git wget git reset --hard HEAD # Create the directories mkdir .docs mkdir .cache +wget https://github.com/jdpedrie/Sami/releases/download/v4.3.0/sami.phar + # Run the docs generation command -php vendor/bin/sami.php update .github/actions/docs/sami.php +php sami.phar update .github/actions/docs/sami.php +chmod -R 0777 . diff --git a/.github/workflows/asset-release.yml b/.github/workflows/asset-release.yml index dd79db3d2..6f86fdecf 100644 --- a/.github/workflows/asset-release.yml +++ b/.github/workflows/asset-release.yml @@ -18,12 +18,9 @@ jobs: name: Get Tag run: echo ::set-output name=tag::${GITHUB_REF#refs/*/} - - uses: octokit/request-action@v2.x - id: getLatestRelease - with: - route: GET /repos/:repository/releases/tags/:tag - repository: ${{ github.repository }} - tag: ${{ steps.tagName.outputs.tag }} + - name: Get release + id: get_release + uses: bruceadams/get-release@v1.2.2 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -60,7 +57,7 @@ jobs: env: GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} with: - upload_url: ${{ fromJson(steps.getLatestRelease.outputs.data).upload_url }} + upload_url: ${{ steps.get_release.outputs.upload_url }} asset_path: ./google-api-php-client-${{ steps.tagName.outputs.tag }}-PHP${{ matrix.php }}.zip asset_name: google-api-php-client-${{ steps.tagName.outputs.tag }}-PHP${{ matrix.php }}.zip asset_content_type: application/zip diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 8992af044..63244a80a 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -19,7 +19,7 @@ jobs: with: timeout_minutes: 10 max_attempts: 3 - command: composer config repositories.sami vcs https://${{ secrets.GITHUB_TOKEN }}@github.com/jdpedrie/sami.git && composer require sami/sami:dev-master && git reset --hard HEAD + command: composer install - name: Generate and Push Documentation uses: docker://php:7.3-cli env: From 2fb6e702aca5d68203fa737f89f6f774022494c6 Mon Sep 17 00:00:00 2001 From: John Pedrie Date: Tue, 19 Jan 2021 12:48:59 -0500 Subject: [PATCH 184/301] chore: prepare v2.9.1 (#2031) --- src/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index e53e88e73..d0cffa44b 100644 --- a/src/Client.php +++ b/src/Client.php @@ -49,7 +49,7 @@ */ class Client { - const LIBVER = "2.9.0"; + const LIBVER = "2.9.1"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; From fa4b989470cd69035fe3f32f9f6a006e867f8721 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 21 Jan 2021 09:27:36 -0700 Subject: [PATCH 185/301] chore: remove recommendation of v1-master branch (#2032) --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 784cb4da9..1b4db41f0 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,6 @@ The Google API Client Library enables you to work with Google APIs such as Gmail These client libraries are officially supported by Google. However, the libraries are considered complete and are in maintenance mode. This means that we will address critical bugs and security issues but will not add any new features. -**NOTE** The actively maintained (v2) version of this client requires PHP 5.6 or above. If you require support for PHP 5.2 or 5.3, use the v1 branch. - ## Google Cloud Platform For Google Cloud Platform APIs such as Datastore, Cloud Storage or Pub/Sub, we recommend using [GoogleCloudPlatform/google-cloud-php](https://github.com/googleapis/google-cloud-php) which is under active development. From ed5ea0cd8f30a9c98c2678448a7a8ccc95784099 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 28 Jan 2021 08:51:24 -0800 Subject: [PATCH 186/301] fix: ensure Google_Task_Retryable is an interface (#2034) --- src/aliases.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aliases.php b/src/aliases.php index baa0b49ab..7bf883730 100644 --- a/src/aliases.php +++ b/src/aliases.php @@ -59,7 +59,7 @@ class Google_Service extends \Google\Service {} class Google_Service_Exception extends \Google\Service\Exception {} class Google_Service_Resource extends \Google\Service\Resource {} class Google_Task_Exception extends \Google\Task\Exception {} - class Google_Task_Retryable extends \Google\Task\Retryable {} + interface Google_Task_Retryable extends \Google\Task\Retryable {} class Google_Task_Runner extends \Google\Task\Runner {} class Google_Utils_UriTemplate extends \Google\Utils\UriTemplate {} } From 16c78c5f1a65e88e6efcc314ece9312031018d64 Mon Sep 17 00:00:00 2001 From: Nana YAMANE Date: Wed, 10 Mar 2021 06:12:31 +0900 Subject: [PATCH 187/301] docs: update readme (how to use a specific JSON key) (#2057) --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 1b4db41f0..f4f23d3ef 100644 --- a/README.md +++ b/README.md @@ -217,6 +217,19 @@ calls return unexpected 401 or 403 errors. $client->setSubject($user_to_impersonate); ``` +#### How to use a specific JSON key + +If you want to a specific JSON key instead of using `GOOGLE_APPLICATION_CREDENTIALS` environment variable, you can do this: + +```php +$jsonKey = [ + 'type' => 'service_account', + // ... +]; +$client = new Google\Client(); +$client->setAuthConfig($jsonKey); +``` + ### Making Requests ### The classes used to call the API in [google-api-php-client-services](https://github.com/googleapis/google-api-php-client-services) are autogenerated. They map directly to the JSON requests and responses found in the [APIs Explorer](https://developers.google.com/apis-explorer/#p/). From c925552c84ca5cf02e36b83e72b5371ec3bea391 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 17 Mar 2021 09:47:13 -0700 Subject: [PATCH 188/301] chore: update README.md (#2061) --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f4f23d3ef..70812ed67 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,12 @@ These client libraries are officially supported by Google. However, the librari ## Google Cloud Platform -For Google Cloud Platform APIs such as Datastore, Cloud Storage or Pub/Sub, we recommend using [GoogleCloudPlatform/google-cloud-php](https://github.com/googleapis/google-cloud-php) which is under active development. +For Google Cloud Platform APIs such as [Datastore][cloud-datastore], [Cloud Storage][cloud-storage], [Pub/Sub][cloud-pubsub], and [Compute Engine][cloud-compute], we recommend using the Google Cloud client libraries. For a complete list of supported Google Cloud client libraries, see [googleapis/google-cloud-php](https://github.com/googleapis/google-cloud-php). + +[cloud-datastore]: https://github.com/googleapis/google-cloud-php-datastore +[cloud-pubsub]: https://github.com/googleapis/google-cloud-php-pubsub +[cloud-storage]: https://github.com/googleapis/google-cloud-php-storage +[cloud-compute]: https://github.com/googleapis/google-cloud-php-compute ## Requirements ## * [PHP 5.6.0 or higher](https://www.php.net/) From db46cd4cd642302fae4af77c1b954a8737526c25 Mon Sep 17 00:00:00 2001 From: David Supplee Date: Tue, 18 May 2021 09:45:16 -0700 Subject: [PATCH 189/301] chore: require patched version of composer (#2076) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c29adc2c3..2eae20354 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "cache/filesystem-adapter": "^0.3.2|^1.1", "phpcompatibility/php-compatibility": "^9.2", "dealerdirect/phpcodesniffer-composer-installer": "^0.7", - "composer/composer": "^1.10" + "composer/composer": "^1.10.22" }, "suggest": { "cache/filesystem-adapter": "For caching certs and tokens (using Google\\Client::setCache)" From e07b3a1c92d08b0a55e604cf61472a9affaa5d0d Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 9 Jun 2021 17:03:32 -0500 Subject: [PATCH 190/301] fix: ensure composer cleanup works for upcoming service namespaces (#2084) --- src/Task/Composer.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Task/Composer.php b/src/Task/Composer.php index 8fcf13b5b..892573b9c 100644 --- a/src/Task/Composer.php +++ b/src/Task/Composer.php @@ -37,10 +37,18 @@ public static function cleanup( $servicesToKeep = isset($extra['google/apiclient-services']) ? $extra['google/apiclient-services'] : []; if ($servicesToKeep) { + $vendorDir = $composer->getConfig()->get('vendor-dir'); $serviceDir = sprintf( '%s/google/apiclient-services/src/Google/Service', - $composer->getConfig()->get('vendor-dir') + $vendorDir ); + if (!is_dir($serviceDir)) { + // path for google/apiclient-services >= 0.200.0 + $serviceDir = sprintf( + '%s/google/apiclient-services/src', + $vendorDir + ); + } self::verifyServicesToKeep($serviceDir, $servicesToKeep); $finder = self::getServicesToRemove($serviceDir, $servicesToKeep); $filesystem = $filesystem ?: new Filesystem(); From 1003c933d2c934a9d08db7be7f82abd82c70ef01 Mon Sep 17 00:00:00 2001 From: "google-cloud-policy-bot[bot]" <80869356+google-cloud-policy-bot[bot]@users.noreply.github.com> Date: Wed, 9 Jun 2021 22:10:09 +0000 Subject: [PATCH 191/301] chore: add SECURITY.md (#2074) add a security policy --- SECURITY.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 SECURITY.md diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 000000000..8b58ae9c0 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,7 @@ +# Security Policy + +To report a security issue, please use [g.co/vulnz](https://g.co/vulnz). + +The Google Security Team will respond within 5 working days of your report on g.co/vulnz. + +We use g.co/vulnz for our intake, and do coordination and disclosure here using GitHub Security Advisory to privately discuss and fix the issue. From 33b28ac4594bd04fffdee4b58d14af2dbae6caaa Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 9 Jun 2021 17:11:07 -0500 Subject: [PATCH 192/301] chore(docs): bump version in readme (#2062) --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 70812ed67..631c4c5d1 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ composer installed. Once composer is installed, execute the following command in your project root to install this library: ```sh -composer require google/apiclient:"^2.7" +composer require google/apiclient:"^2.9" ``` Finally, be sure to include the autoloader: @@ -61,7 +61,7 @@ you want to keep in `composer.json`: ```json { "require": { - "google/apiclient": "^2.7" + "google/apiclient": "^2.9" }, "scripts": { "post-update-cmd": "Google\\Task\\Composer::cleanup" From e9ef4c26a044b8d39a46bcf296be795fe24a1849 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 9 Jun 2021 17:15:08 -0500 Subject: [PATCH 193/301] chore: recommend 'pre-autoload-dump' for cleanup command so it works with 'optimize-autoloader' (#2063) --- README.md | 2 +- tests/Google/Task/ComposerTest.php | 110 +++++++++++++++++++---------- 2 files changed, 72 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 631c4c5d1..1e892d2ee 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ you want to keep in `composer.json`: "google/apiclient": "^2.9" }, "scripts": { - "post-update-cmd": "Google\\Task\\Composer::cleanup" + "pre-autoload-dump": "Google\\Task\\Composer::cleanup" }, "extra": { "google/apiclient-services": [ diff --git a/tests/Google/Task/ComposerTest.php b/tests/Google/Task/ComposerTest.php index faf9f32dc..c2b81ca48 100644 --- a/tests/Google/Task/ComposerTest.php +++ b/tests/Google/Task/ComposerTest.php @@ -17,6 +17,25 @@ class Google_Task_ComposerTest extends BaseTest { + private static $composerBaseConfig = [ + 'repositories' => [ + [ + 'type' => 'path', + 'url' => __DIR__ . '/../../..', + 'options' => [ + 'symlink' => false + ] + ] + ], + 'require' => [ + 'google/apiclient' => '*' + ], + 'scripts' => [ + 'pre-autoload-dump' => 'Google\Task\Composer::cleanup' + ], + 'minimum-stability' => 'dev', + ]; + /** * @expectedException InvalidArgumentException * @expectedExceptionMessage Google service "Foo" does not exist @@ -147,26 +166,7 @@ private function createMockEvent( public function testE2E() { - $composer = [ - 'repositories' => [ - [ - 'type' => 'path', - 'url' => __DIR__ . '/../../..', - 'options' => [ - 'symlink' => false - ] - ] - ], - 'require' => [ - 'google/apiclient' => '*' - ], - 'scripts' => [ - 'post-update-cmd' => 'Google\Task\Composer::cleanup' - ], - 'minimum-stability' => 'dev', - ]; - - $composerJson = json_encode($composer + [ + $dir = $this->runComposerInstall(self::$composerBaseConfig + [ 'extra' => [ 'google/apiclient-services' => [ 'Drive', @@ -175,12 +175,7 @@ public function testE2E() ] ]); - $tmpDir = sys_get_temp_dir() . '/test-' . rand(); - mkdir($tmpDir); - file_put_contents($tmpDir . '/composer.json', $composerJson); - passthru('composer install -d ' . $tmpDir); - - $serviceDir = $tmpDir . '/vendor/google/apiclient-services/src/Google/Service'; + $serviceDir = $dir . '/vendor/google/apiclient-services/src/Google/Service'; $this->assertFileExists($serviceDir . '/Drive.php'); $this->assertFileExists($serviceDir . '/Drive'); $this->assertFileExists($serviceDir . '/YouTube.php'); @@ -188,7 +183,11 @@ public function testE2E() $this->assertFileNotExists($serviceDir . '/YouTubeReporting.php'); $this->assertFileNotExists($serviceDir . '/YouTubeReporting'); - $composerJson = json_encode($composer + [ + // Remove the "apiclient-services" directory, which is required to + // update the cleanup command. + passthru('rm -r ' . $dir . '/vendor/google/apiclient-services'); + + $this->runComposerInstall(self::$composerBaseConfig + [ 'extra' => [ 'google/apiclient-services' => [ 'Drive', @@ -196,11 +195,7 @@ public function testE2E() 'YouTubeReporting', ] ] - ]); - - file_put_contents($tmpDir . '/composer.json', $composerJson); - passthru('rm -r ' . $tmpDir . '/vendor/google/apiclient-services'); - passthru('composer update -d ' . $tmpDir); + ], $dir); $this->assertFileExists($serviceDir . '/Drive.php'); $this->assertFileExists($serviceDir . '/Drive'); @@ -208,20 +203,22 @@ public function testE2E() $this->assertFileExists($serviceDir . '/YouTube'); $this->assertFileExists($serviceDir . '/YouTubeReporting.php'); $this->assertFileExists($serviceDir . '/YouTubeReporting'); + } - // Test BC Task name - $composer['scripts']['post-update-cmd'] = 'Google_Task_Composer::cleanup'; - $composerJson = json_encode($composer + [ + public function testE2EBCTaskName() + { + $composerConfig = self::$composerBaseConfig + [ 'extra' => [ 'google/apiclient-services' => [ 'Drive', ] ] - ]); + ]; + // Test BC Task name + $composerConfig['scripts']['pre-autoload-dump'] = 'Google_Task_Composer::cleanup'; - file_put_contents($tmpDir . '/composer.json', $composerJson); - passthru('rm -r ' . $tmpDir . '/vendor/google/apiclient-services'); - passthru('composer update -d ' . $tmpDir); + $dir = $this->runComposerInstall($composerConfig); + $serviceDir = $dir . '/vendor/google/apiclient-services/src/Google/Service'; $this->assertFileExists($serviceDir . '/Drive.php'); $this->assertFileExists($serviceDir . '/Drive'); @@ -230,4 +227,39 @@ public function testE2E() $this->assertFileNotExists($serviceDir . '/YouTubeReporting.php'); $this->assertFileNotExists($serviceDir . '/YouTubeReporting'); } + + public function testE2EOptimized() + { + $dir = $this->runComposerInstall(self::$composerBaseConfig + [ + 'config' => [ + 'optimize-autoloader' => true, + ], + 'extra' => [ + 'google/apiclient-services' => [ + 'Drive' + ] + ] + ]); + + $classmap = require_once $dir . '/vendor/composer/autoload_classmap.php'; + + // Verify removed services do not show up in the classmap + $this->assertArrayHasKey('Google_Service_Drive', $classmap); + $this->assertArrayNotHasKey('Google_Service_YouTube', $classmap); + } + + private function runComposerInstall(array $composerConfig, $dir = null) + { + $composerJson = json_encode($composerConfig); + + if (is_null($dir)) { + $dir = sys_get_temp_dir() . '/test-' . rand(); + mkdir($dir); + } + + file_put_contents($dir . '/composer.json', $composerJson); + passthru('composer install -d ' . $dir); + + return $dir; + } } From 62f57e7682fa649f955010f9124e4892ced0717d Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 10 Jun 2021 13:56:25 -0500 Subject: [PATCH 194/301] chore: update examples for latest apiary (#2087) --- examples/batch.php | 14 +++++++------- examples/service-account.php | 4 ++-- examples/simple-query.php | 8 ++++---- tests/examples/largeFileDownloadTest.php | 3 +++ tests/examples/largeFileUploadTest.php | 3 +++ tests/examples/simpleFileUploadTest.php | 3 +++ 6 files changed, 22 insertions(+), 13 deletions(-) diff --git a/examples/batch.php b/examples/batch.php index 2285e5dd3..bb2ba4e02 100644 --- a/examples/batch.php +++ b/examples/batch.php @@ -59,22 +59,22 @@ keys will be reflected in the returned array. ************************************************/ -// NOTE: Some services use `$service->createBatch();` instead of -// `new Google\Http\Batch($client);` -$batch = new Google\Http\Batch($client); +// NOTE: Some services use `new Google\Http\Batch($client);` instead +$batch = $service->createBatch(); +$query = 'Henry David Thoreau'; $optParams = array('filter' => 'free-ebooks'); -$optParams['q'] = 'Henry David Thoreau'; -$req1 = $service->volumes->listVolumes($optParams); +$req1 = $service->volumes->listVolumes($query, $optParams); $batch->add($req1, "thoreau"); -$optParams['q'] = 'George Bernard Shaw'; -$req2 = $service->volumes->listVolumes($optParams); +$query = 'George Bernard Shaw'; +$req2 = $service->volumes->listVolumes($query, $optParams); $batch->add($req2, "shaw"); /************************************************ Executing the batch will send all requests off at once. ************************************************/ + $results = $batch->execute(); ?> diff --git a/examples/service-account.php b/examples/service-account.php index c86300116..e8b948918 100644 --- a/examples/service-account.php +++ b/examples/service-account.php @@ -59,11 +59,11 @@ We're just going to make the same call as in the simple query as an example. ************************************************/ +$query = 'Henry David Thoreau'; $optParams = array( - 'q' => 'Henry David Thoreau', 'filter' => 'free-ebooks', ); -$results = $service->volumes->listVolumes($optParams); +$results = $service->volumes->listVolumes($query, $optParams); ?>

      Results Of Call:

      diff --git a/examples/simple-query.php b/examples/simple-query.php index bf49581b1..7ff68c9ed 100644 --- a/examples/simple-query.php +++ b/examples/simple-query.php @@ -47,21 +47,21 @@ (the query), and an array of named optional parameters. ************************************************/ +$query = 'Henry David Thoreau'; $optParams = array( - 'q' => 'Henry David Thoreau', 'filter' => 'free-ebooks', ); -$results = $service->volumes->listVolumes($optParams); +$results = $service->volumes->listVolumes($query, $optParams); /************************************************ This is an example of deferring a call. ***********************************************/ $client->setDefer(true); +$query = 'Henry David Thoreau'; $optParams = array( - 'q' => 'Henry David Thoreau', 'filter' => 'free-ebooks', ); -$request = $service->volumes->listVolumes($optParams); +$request = $service->volumes->listVolumes($query, $optParams); $resultsDeferred = $client->execute($request); /************************************************ diff --git a/tests/examples/largeFileDownloadTest.php b/tests/examples/largeFileDownloadTest.php index 538b3ff70..e16e9d5d4 100644 --- a/tests/examples/largeFileDownloadTest.php +++ b/tests/examples/largeFileDownloadTest.php @@ -20,6 +20,9 @@ class examples_largeFileDownloadTest extends BaseTest { + /** + * @runInSeparateProcess + */ public function testSimpleFileDownloadNoToken() { $this->checkServiceAccountCredentials(); diff --git a/tests/examples/largeFileUploadTest.php b/tests/examples/largeFileUploadTest.php index ca9848879..a927309f8 100644 --- a/tests/examples/largeFileUploadTest.php +++ b/tests/examples/largeFileUploadTest.php @@ -21,6 +21,9 @@ class examples_largeFileUploadTest extends BaseTest { + /** + * @runInSeparateProcess + */ public function testLargeFileUpload() { $this->checkServiceAccountCredentials(); diff --git a/tests/examples/simpleFileUploadTest.php b/tests/examples/simpleFileUploadTest.php index 54dce650a..8b0298ba2 100644 --- a/tests/examples/simpleFileUploadTest.php +++ b/tests/examples/simpleFileUploadTest.php @@ -21,6 +21,9 @@ class examples_simpleFileUploadTest extends BaseTest { + /** + * @runInSeparateProcess + */ public function testSimpleFileUploadNoToken() { $this->checkServiceAccountCredentials(); From a1f05ce3b5733dd45fa4d45e41b76ecdbd229fee Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 18 Jun 2021 13:58:28 -0500 Subject: [PATCH 195/301] chore: fix tests in preparation of google/apiclient-services:v0.200.0 release (#2086) --- README.md | 38 +++--- UPGRADING.md | 47 +++++++ composer.json | 2 +- docs/media.md | 6 +- docs/oauth-server.md | 4 +- docs/oauth-web.md | 12 +- docs/parameters.md | 2 +- docs/start.md | 2 +- examples/batch.php | 2 +- examples/large-file-download.php | 2 +- examples/large-file-upload.php | 4 +- examples/multi-api.php | 4 +- examples/service-account.php | 2 +- examples/simple-file-upload.php | 6 +- examples/simple-query.php | 4 +- phpunit.xml.dist | 2 +- tests/BaseTest.php | 12 +- tests/Google/AccessToken/RevokeTest.php | 22 ++-- tests/Google/AccessToken/VerifyTest.php | 20 ++- tests/Google/CacheTest.php | 24 ++-- tests/Google/ClientTest.php | 129 +++++++++++-------- tests/Google/Http/BatchTest.php | 99 ++++---------- tests/Google/Http/MediaFileUploadTest.php | 49 +++---- tests/Google/Http/RESTTest.php | 30 ++--- tests/Google/ModelTest.php | 68 +++++----- tests/Google/Service/AdSenseTest.php | 10 +- tests/Google/Service/PagespeedonlineTest.php | 29 ----- tests/Google/Service/ResourceTest.php | 91 +++++++------ tests/Google/Service/TasksTest.php | 15 ++- tests/Google/Service/YouTubeTest.php | 29 +++-- tests/Google/ServiceTest.php | 32 +++-- tests/Google/Task/ComposerTest.php | 59 +++++---- tests/Google/Task/RunnerTest.php | 104 ++++++++------- tests/Google/Utils/UriTemplateTest.php | 21 +-- tests/examples/batchTest.php | 6 +- tests/examples/idTokenTest.php | 6 +- tests/examples/indexTest.php | 6 +- tests/examples/largeFileDownloadTest.php | 6 +- tests/examples/largeFileUploadTest.php | 6 +- tests/examples/multiApiTest.php | 6 +- tests/examples/serviceAccountTest.php | 6 +- tests/examples/simpleFileUploadTest.php | 6 +- tests/examples/simpleQueryTest.php | 6 +- 43 files changed, 568 insertions(+), 468 deletions(-) delete mode 100644 tests/Google/Service/PagespeedonlineTest.php diff --git a/README.md b/README.md index 1e892d2ee..7346110da 100644 --- a/README.md +++ b/README.md @@ -138,12 +138,12 @@ $client = new Google\Client(); $client->setApplicationName("Client_Library_Examples"); $client->setDeveloperKey("YOUR_APP_KEY"); -$service = new Google_Service_Books($client); -$optParams = array( +$service = new Google\Service\Books($client); +$query = 'Henry David Thoreau'; +$optParams = [ 'filter' => 'free-ebooks', - 'q' => 'Henry David Thoreau' -); -$results = $service->volumes->listVolumes($optParams); +]; +$results = $service->volumes->listVolumes($query, $optParams); foreach ($results->getItems() as $item) { echo $item['volumeInfo']['title'], "
      \n"; @@ -166,7 +166,7 @@ foreach ($results->getItems() as $item) { 1. Set the scopes required for the API you are going to call ```php - $client->addScope(Google_Service_Drive::DRIVE); + $client->addScope(Google\Service\Drive::DRIVE); ``` 1. Set your application's redirect URI @@ -213,7 +213,7 @@ calls return unexpected 401 or 403 errors. 1. Set the scopes required for the API you are going to call ```php - $client->addScope(Google_Service_Drive::DRIVE); + $client->addScope(Google\Service\Drive::DRIVE); ``` 1. If you have delegated domain-wide access to the service account and you want to impersonate a user account, specify the email address of the user account using the method setSubject: @@ -264,10 +264,10 @@ Using this library, the same call would look something like this: ```php // create the datastore service class -$datastore = new Google_Service_Datastore($client); +$datastore = new Google\Service\Datastore($client); // build the query - this maps directly to the JSON -$query = new Google_Service_Datastore_Query([ +$query = new Google\Service\Datastore\Query([ 'kind' => [ [ 'name' => 'Book', @@ -283,7 +283,7 @@ $query = new Google_Service_Datastore_Query([ ]); // build the request and response -$request = new Google_Service_Datastore_RunQueryRequest(['query' => $query]); +$request = new Google\Service\Datastore\RunQueryRequest(['query' => $query]); $response = $datastore->projects->runQuery('YOUR_DATASET_ID', $request); ``` @@ -291,20 +291,20 @@ However, as each property of the JSON API has a corresponding generated class, t ```php // create the datastore service class -$datastore = new Google_Service_Datastore($client); +$datastore = new Google\Service\Datastore($client); // build the query -$request = new Google_Service_Datastore_RunQueryRequest(); -$query = new Google_Service_Datastore_Query(); +$request = new Google\Service\Datastore_RunQueryRequest(); +$query = new Google\Service\Datastore\Query(); // - set the order -$order = new Google_Service_Datastore_PropertyOrder(); +$order = new Google\Service\Datastore_PropertyOrder(); $order->setDirection('descending'); -$property = new Google_Service_Datastore_PropertyReference(); +$property = new Google\Service\Datastore\PropertyReference(); $property->setName('title'); $order->setProperty($property); $query->setOrder([$order]); // - set the kinds -$kind = new Google_Service_Datastore_KindExpression(); +$kind = new Google\Service\Datastore\KindExpression(); $kind->setName('Book'); $query->setKinds([$kind]); // - set the limit @@ -335,7 +335,7 @@ $client = new Google\Client(); * Application Default Credentials. */ $client->useApplicationDefaultCredentials(); -$client->addScope(Google_Service_Plus::PLUS_ME); +$client->addScope(Google\Service\Plus::PLUS_ME); // returns a Guzzle HTTP Client $httpClient = $client->authorize(); @@ -438,9 +438,9 @@ If there is a specific bug with the library, please [file an issue](https://gith If X is a feature of the library, file away! If X is an example of using a specific service, the best place to go is to the teams for those specific APIs - our preference is to link to their examples rather than add them to the library, as they can then pin to specific versions of the library. If you have any examples for other APIs, let us know and we will happily add a link to the README above! -### Why does Google_..._Service have weird names? ### +### Why do some Google\Service classes have weird names? ### -The _Service classes are generally automatically generated from the API discovery documents: https://developers.google.com/discovery/. Sometimes new features are added to APIs with unusual names, which can cause some unexpected or non-standard style naming in the PHP classes. +The _Google\Service_ classes are generally automatically generated from the API discovery documents: https://developers.google.com/discovery/. Sometimes new features are added to APIs with unusual names, which can cause some unexpected or non-standard style naming in the PHP classes. ### How do I deal with non-JSON response types? ### diff --git a/UPGRADING.md b/UPGRADING.md index f07a24436..ef939e943 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -1,6 +1,53 @@ Google API Client Upgrade Guide =============================== +2.x to 2.10.0 +------------- + +### Namespaces + +The Google API Client for PHP now uses namespaces for all classes. Code using +the legacy classnames will continue to work, but it is advised to upgrade to the +underspaced names, as the legacy classnames will be deprecated some time in the +future. + +**Before** + +```php +$client = new Google_Client(); +$service = new Google_Service_Books($client); +``` + +**After** +```php +$client = new Google\Client(); +$service = new Google\Service\Books($client); +``` + +### Service class constructors + +Service class constructors now accept an optional `Google\Client|array` parameter +as their first argument, rather than requiring an instance of `Google\Client`. + +**Before** + +```php +$client = new Google_Client(); +$client->setApplicationName("Client_Library_Examples"); +$client->setDeveloperKey("YOUR_APP_KEY"); + +$service = new Google_Service_Books($client); +``` + +**After** + +```php +$service = new Google\Service\Books([ + 'application_name' => "Client_Library_Examples", + 'developer_key' => "YOUR_APP_KEY", +]); +``` + 1.0 to 2.0 ---------- diff --git a/composer.json b/composer.json index 2eae20354..cce3c93a1 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "require": { "php": "^5.6|^7.0|^8.0", "google/auth": "^1.10", - "google/apiclient-services": "~0.13", + "google/apiclient-services": "^0.200.0", "firebase/php-jwt": "~2.0||~3.0||~4.0||~5.0", "monolog/monolog": "^1.17|^2.0", "phpseclib/phpseclib": "~2.0||^3.0.2", diff --git a/docs/media.md b/docs/media.md index 66148c387..6e35bb2cf 100644 --- a/docs/media.md +++ b/docs/media.md @@ -7,7 +7,7 @@ The PHP client library allows for uploading large files for use with APIs such a In the simple upload case, the data is passed as the body of the request made to the server. This limits the ability to specify metadata, but is very easy to use. ```php -$file = new Google_Service_Drive_DriveFile(); +$file = new Google\Service\Drive\DriveFile(); $result = $service->files->insert($file, array( 'data' => file_get_contents("path/to/file"), 'mimeType' => 'application/octet-stream', @@ -20,7 +20,7 @@ $result = $service->files->insert($file, array( With multipart file uploads, the uploaded file is sent as one part of a multipart form post. This allows metadata about the file object to be sent as part of the post as well. This is triggered by specifying the _multipart_ uploadType. ```php -$file = new Google_Service_Drive_DriveFile(); +$file = new Google\Service\Drive\DriveFile(); $file->setTitle("Hello World!"); $result = $service->files->insert($file, array( 'data' => file_get_contents("path/to/file"), @@ -34,7 +34,7 @@ $result = $service->files->insert($file, array( It is also possible to split the upload across multiple requests. This is convenient for larger files, and allows resumption of the upload if there is a problem. Resumable uploads can be sent with separate metadata. ```php -$file = new Google_Service_Drive_DriveFile(); +$file = new Google\Service\Drive\DriveFile(); $file->title = "Big File"; $chunkSizeBytes = 1 * 1024 * 1024; diff --git a/docs/oauth-server.md b/docs/oauth-server.md index ad57b0c78..eb0707a18 100644 --- a/docs/oauth-server.md +++ b/docs/oauth-server.md @@ -87,7 +87,7 @@ Use the authorized `Google\Client` object to call Google APIs by completing the 1. Build a service object for the API that you want to call, providing the authorized `Google\Client` object. For example, to call the Cloud SQL Administration API: ```php - $sqladmin = new Google_Service_SQLAdmin($client); + $sqladmin = new Google\Service\SQLAdmin($client); ``` 2. Make requests to the API service using the [interface provided by the service object](https://github.com/googleapis/google-api-php-client/blob/master/docs/start.md#build-the-service-object). For example, to list the instances of Cloud SQL databases in the examinable-example-123 project: @@ -133,7 +133,7 @@ putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json'); $client = new Google\Client(); $client->useApplicationDefaultCredentials(); -$sqladmin = new Google_Service_SQLAdmin($client); +$sqladmin = new Google\Service\SQLAdmin($client); $response = $sqladmin->instances ->listInstances('examinable-example-123')->getItems(); echo json_encode($response) . "\n"; diff --git a/docs/oauth-web.md b/docs/oauth-web.md index dfc16f911..904745ed5 100644 --- a/docs/oauth-web.md +++ b/docs/oauth-web.md @@ -82,7 +82,7 @@ For example, this code requests read-only, offline access to a user's Google Dri ```php $client = new Google\Client(); $client->setAuthConfig('client_secret.json'); -$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY); +$client->addScope(Google\Service\Drive::DRIVE_METADATA_READONLY); $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php'); $client->setAccessType('offline'); // offline access $client->setIncludeGrantedScopes(true); // incremental auth @@ -118,7 +118,7 @@ $client->setRedirectUri('http://localhost:8080/oauth2callback.php'); Scopes enable your application to only request access to the resources that it needs while also enabling users to control the amount of access that they grant to your application. Thus, there is an inverse relationship between the number of scopes requested and the likelihood of obtaining user consent. To set this value in PHP, call the `addScope` function: ```php -$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY); +$client->addScope(Google\Service\Drive::DRIVE_METADATA_READONLY); ``` The [OAuth 2.0 API Scopes](https://developers.google.com/identity/protocols/googlescopes) document provides a full list of scopes that you might use to access Google APIs. @@ -284,7 +284,7 @@ Use the access token to call Google APIs by completing the following steps: 2. Build a service object for the API that you want to call. You build a a service object by providing an authorized `Google\Client` object to the constructor for the API you want to call. For example, to call the Drive API: ```php - $drive = new Google_Service_Drive($client); + $drive = new Google\Service\Drive($client); ``` 3. Make requests to the API service using the [interface provided by the service object](start.md). For example, to list the files in the authenticated user's Google Drive: @@ -331,11 +331,11 @@ session_start(); $client = new Google\Client(); $client->setAuthConfig('client_secrets.json'); -$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY); +$client->addScope(Google\Service\Drive::DRIVE_METADATA_READONLY); if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { $client->setAccessToken($_SESSION['access_token']); - $drive = new Google_Service_Drive($client); + $drive = new Google\Service\Drive($client); $files = $drive->files->listFiles(array())->getItems(); echo json_encode($files); } else { @@ -355,7 +355,7 @@ session_start(); $client = new Google\Client(); $client->setAuthConfigFile('client_secrets.json'); $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php'); -$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY); +$client->addScope(Google\Service\Drive::DRIVE_METADATA_READONLY); if (! isset($_GET['code'])) { $auth_url = $client->createAuthUrl(); diff --git a/docs/parameters.md b/docs/parameters.md index 029ebe4ec..183a0c298 100644 --- a/docs/parameters.md +++ b/docs/parameters.md @@ -1,6 +1,6 @@ # Standard Parameters -Many API methods include support for certain optional parameters. In addition to these there are several standard parameters that can be applied to any API call. These are defined in the `Google_Service_Resource` class. +Many API methods include support for certain optional parameters. In addition to these there are several standard parameters that can be applied to any API call. These are defined in the `Google\Service\Resource` class. ## Parameters diff --git a/docs/start.md b/docs/start.md index d22b49d0c..fd3c53f72 100644 --- a/docs/start.md +++ b/docs/start.md @@ -64,7 +64,7 @@ $client->setDeveloperKey("MY_SIMPLE_API_KEY"); Services are called through queries to service specific objects. These are created by constructing the service object, and passing an instance of `Google\Client` to it. `Google\Client` contains the IO, authentication and other classes required by the service to function, and the service informs the client which scopes it uses to provide a default when authenticating a user. ```php -$service = new Google_Service_Books($client); +$service = new Google\Service\Books($client); ``` ### Calling an API diff --git a/examples/batch.php b/examples/batch.php index bb2ba4e02..9df665059 100644 --- a/examples/batch.php +++ b/examples/batch.php @@ -42,7 +42,7 @@ } $client->setDeveloperKey($apiKey); -$service = new Google_Service_Books($client); +$service = new Google\Service\Books($client); /************************************************ To actually make the batch call we need to diff --git a/examples/large-file-download.php b/examples/large-file-download.php index 1ab30e735..f5978ed1a 100644 --- a/examples/large-file-download.php +++ b/examples/large-file-download.php @@ -38,7 +38,7 @@ $client->setAuthConfig($oauth_credentials); $client->setRedirectUri($redirect_uri); $client->addScope("https://www.googleapis.com/auth/drive"); -$service = new Google_Service_Drive($client); +$service = new Google\Service\Drive($client); /************************************************ * If we have a code back from the OAuth 2.0 flow, diff --git a/examples/large-file-upload.php b/examples/large-file-upload.php index ba4a96589..00eb93d1d 100644 --- a/examples/large-file-upload.php +++ b/examples/large-file-upload.php @@ -38,7 +38,7 @@ $client->setAuthConfig($oauth_credentials); $client->setRedirectUri($redirect_uri); $client->addScope("https://www.googleapis.com/auth/drive"); -$service = new Google_Service_Drive($client); +$service = new Google\Service\Drive($client); // add "?logout" to the URL to remove a token from the session if (isset($_REQUEST['logout'])) { @@ -89,7 +89,7 @@ fclose($fh); } - $file = new Google_Service_Drive_DriveFile(); + $file = new Google\Service\Drive\DriveFile(); $file->name = "Big File"; $chunkSizeBytes = 1 * 1024 * 1024; diff --git a/examples/multi-api.php b/examples/multi-api.php index 573e6bc8f..74b860f73 100644 --- a/examples/multi-api.php +++ b/examples/multi-api.php @@ -78,8 +78,8 @@ We are going to create both YouTube and Drive services, and query both. ************************************************/ -$yt_service = new Google_Service_YouTube($client); -$dr_service = new Google_Service_Drive($client); +$yt_service = new Google\Service\YouTube($client); +$dr_service = new Google\Service\Drive($client); /************************************************ If we're signed in, retrieve channels from YouTube diff --git a/examples/service-account.php b/examples/service-account.php index e8b948918..d00272719 100644 --- a/examples/service-account.php +++ b/examples/service-account.php @@ -53,7 +53,7 @@ $client->setApplicationName("Client_Library_Examples"); $client->setScopes(['https://www.googleapis.com/auth/books']); -$service = new Google_Service_Books($client); +$service = new Google\Service\Books($client); /************************************************ We're just going to make the same call as in the diff --git a/examples/simple-file-upload.php b/examples/simple-file-upload.php index 6e666a9cb..dc18fab2d 100644 --- a/examples/simple-file-upload.php +++ b/examples/simple-file-upload.php @@ -38,7 +38,7 @@ $client->setAuthConfig($oauth_credentials); $client->setRedirectUri($redirect_uri); $client->addScope("https://www.googleapis.com/auth/drive"); -$service = new Google_Service_Drive($client); +$service = new Google\Service\Drive($client); // add "?logout" to the URL to remove a token from the session if (isset($_REQUEST['logout'])) { @@ -88,7 +88,7 @@ } // This is uploading a file directly, with no metadata associated. - $file = new Google_Service_Drive_DriveFile(); + $file = new Google\Service\Drive\DriveFile(); $result = $service->files->create( $file, array( @@ -99,7 +99,7 @@ ); // Now lets try and send the metadata as well using multipart! - $file = new Google_Service_Drive_DriveFile(); + $file = new Google\Service\Drive\DriveFile(); $file->setName("Hello World!"); $result2 = $service->files->create( $file, diff --git a/examples/simple-query.php b/examples/simple-query.php index 7ff68c9ed..9b967029e 100644 --- a/examples/simple-query.php +++ b/examples/simple-query.php @@ -36,7 +36,7 @@ } $client->setDeveloperKey($apiKey); -$service = new Google_Service_Books($client); +$service = new Google\Service\Books($client); /************************************************ We make a call to our service, which will @@ -70,7 +70,7 @@ array. Some calls will return a single item which we can immediately use. The individual responses - are typed as Google_Service_Books_Volume, but + are typed as Google\Service\Books_Volume, but can be treated as an array. ************************************************/ ?> diff --git a/phpunit.xml.dist b/phpunit.xml.dist index da9664ea9..2acef56c2 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -13,7 +13,7 @@ - ./src/Google + ./src diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 4e51e8bb4..c7377395f 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -15,6 +15,10 @@ * limitations under the License. */ +namespace Google\Tests; + +use Google\Client; +use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\ClientInterface; use Symfony\Component\DomCrawler\Crawler; use League\Flysystem\Adapter\Local; @@ -62,14 +66,12 @@ private function createClient() $options = ['defaults' => $options]; } - $httpClient = new GuzzleHttp\Client($options); + $httpClient = new GuzzleClient($options); - $client = new Google_Client(); + $client = new Client(); $client->setApplicationName('google-api-php-client-tests'); $client->setHttpClient($httpClient); $client->setScopes([ - "https://www.googleapis.com/auth/plus.me", - "https://www.googleapis.com/auth/urlshortener", "https://www.googleapis.com/auth/tasks", "https://www.googleapis.com/auth/adsense", "https://www.googleapis.com/auth/youtube", @@ -115,7 +117,7 @@ public function checkToken() return true; } - public function tryToGetAnAccessToken(Google_Client $client) + public function tryToGetAnAccessToken(Client $client) { $this->checkClientCredentials(); diff --git a/tests/Google/AccessToken/RevokeTest.php b/tests/Google/AccessToken/RevokeTest.php index b2972ee74..1db83fa41 100644 --- a/tests/Google/AccessToken/RevokeTest.php +++ b/tests/Google/AccessToken/RevokeTest.php @@ -1,7 +1,5 @@ reveal()); + $revoke = new Revoke($http->reveal()); $this->assertTrue($revoke->revokeToken($t)); $this->assertEquals($accessToken, $token); // Test with refresh token. - $revoke = new Google_AccessToken_Revoke($http->reveal()); + $revoke = new Revoke($http->reveal()); $t = [ 'access_token' => $accessToken, 'refresh_token' => $refreshToken, @@ -94,7 +98,7 @@ public function testRevokeAccessGuzzle5() $this->assertEquals($refreshToken, $token); // Test with token string. - $revoke = new Google_AccessToken_Revoke($http->reveal()); + $revoke = new Revoke($http->reveal()); $t = $accessToken; $this->assertTrue($revoke->revokeToken($t)); $this->assertEquals($accessToken, $token); @@ -130,12 +134,12 @@ public function testRevokeAccessGuzzle6Or7() ]; // Test with access token. - $revoke = new Google_AccessToken_Revoke($http->reveal()); + $revoke = new Revoke($http->reveal()); $this->assertTrue($revoke->revokeToken($t)); $this->assertEquals($accessToken, $token); // Test with refresh token. - $revoke = new Google_AccessToken_Revoke($http->reveal()); + $revoke = new Revoke($http->reveal()); $t = [ 'access_token' => $accessToken, 'refresh_token' => $refreshToken, @@ -147,7 +151,7 @@ public function testRevokeAccessGuzzle6Or7() $this->assertEquals($refreshToken, $token); // Test with token string. - $revoke = new Google_AccessToken_Revoke($http->reveal()); + $revoke = new Revoke($http->reveal()); $t = $accessToken; $this->assertTrue($revoke->revokeToken($t)); $this->assertEquals($accessToken, $token); diff --git a/tests/Google/AccessToken/VerifyTest.php b/tests/Google/AccessToken/VerifyTest.php index dbcaa6f33..f9c75fb5c 100644 --- a/tests/Google/AccessToken/VerifyTest.php +++ b/tests/Google/AccessToken/VerifyTest.php @@ -19,7 +19,13 @@ * under the License. */ -class Google_AccessToken_VerifyTest extends BaseTest +namespace Google\Tests\AccessToken; + +use Google\AccessToken\Verify; +use Google\Tests\BaseTest; +use ReflectionMethod; + +class VerifyTest extends BaseTest { /** * This test needs to run before the other verify tests, @@ -28,7 +34,7 @@ class Google_AccessToken_VerifyTest extends BaseTest public function testPhpsecConstants() { $client = $this->getClient(); - $verify = new Google_AccessToken_Verify($client->getHttpClient()); + $verify = new Verify($client->getHttpClient()); // set these to values that will be changed if (defined('MATH_BIGINTEGER_OPENSSL_ENABLED') || defined('CRYPT_RSA_MODE')) { @@ -68,7 +74,7 @@ public function testValidateIdToken() $this->assertCount(3, $segments); // Extract the client ID in this case as it wont be set on the test client. $data = json_decode($jwt->urlSafeB64Decode($segments[1])); - $verify = new Google_AccessToken_Verify($http); + $verify = new Verify($http); $payload = $verify->verifyIdToken($token['id_token'], $data->aud); $this->assertArrayHasKey('sub', $payload); $this->assertGreaterThan(0, strlen($payload['sub'])); @@ -79,7 +85,7 @@ public function testValidateIdToken() $client = $this->getClient(); $http = $client->getHttpClient(); $data = json_decode($jwt->urlSafeB64Decode($segments[1])); - $verify = new Google_AccessToken_Verify($http); + $verify = new Verify($http); $payload = $verify->verifyIdToken($token['id_token'], $data->aud); $this->assertArrayHasKey('sub', $payload); $this->assertGreaterThan(0, strlen($payload['sub'])); @@ -106,7 +112,7 @@ public function testLeewayIsUnchangedWhenPassingInJwt() $this->assertCount(3, $segments); // Extract the client ID in this case as it wont be set on the test client. $data = json_decode($jwt->urlSafeB64Decode($segments[1])); - $verify = new Google_AccessToken_Verify($client->getHttpClient(), null, $jwt); + $verify = new Verify($client->getHttpClient(), null, $jwt); $payload = $verify->verifyIdToken($token['id_token'], $data->aud); // verify the leeway is set as it was $this->assertEquals($leeway, $jwt::$leeway); @@ -115,12 +121,12 @@ public function testLeewayIsUnchangedWhenPassingInJwt() public function testRetrieveCertsFromLocation() { $client = $this->getClient(); - $verify = new Google_AccessToken_Verify($client->getHttpClient()); + $verify = new Verify($client->getHttpClient()); // make this method public for testing purposes $method = new ReflectionMethod($verify, 'retrieveCertsFromLocation'); $method->setAccessible(true); - $certs = $method->invoke($verify, Google_AccessToken_Verify::FEDERATED_SIGNON_CERT_URL); + $certs = $method->invoke($verify, Verify::FEDERATED_SIGNON_CERT_URL); $this->assertArrayHasKey('keys', $certs); $this->assertGreaterThan(1, count($certs['keys'])); diff --git a/tests/Google/CacheTest.php b/tests/Google/CacheTest.php index f15c05935..2c1af30e4 100644 --- a/tests/Google/CacheTest.php +++ b/tests/Google/CacheTest.php @@ -18,10 +18,14 @@ * under the License. */ -use GuzzleHttp\Client; +namespace Google\Tests; + +use Google\Client; use Google\Auth\Cache\MemoryCacheItemPool; +use Google\Service\Drive; +use DateTime; -class Google_CacheTest extends BaseTest +class CacheTest extends BaseTest { public function testInMemoryCache() { @@ -39,7 +43,7 @@ public function testInMemoryCache() } /* Make a service call */ - $service = new Google_Service_Drive($client); + $service = new Drive($client); $files = $service->files->listFiles(); $this->assertInstanceOf('Google_Service_Drive_FileList', $files); } @@ -49,7 +53,7 @@ public function testFileCache() $this->onlyPhp55AndAbove(); $this->checkServiceAccountCredentials(); - $client = new Google_Client(); + $client = new Client(); $client->useApplicationDefaultCredentials(); $client->setScopes(['https://www.googleapis.com/auth/drive.readonly']); // filecache with new cache dir @@ -73,14 +77,14 @@ public function testFileCache() } /* Make a service call */ - $service = new Google_Service_Drive($client); + $service = new Drive($client); $files = $service->files->listFiles(); - $this->assertInstanceOf('Google_Service_Drive_FileList', $files); + $this->assertInstanceOf(Drive\FileList::class, $files); - sleep(1); + sleep(2); // make sure the token expires - $client = new Google_Client(); + $client = new Client(); $client->useApplicationDefaultCredentials(); $client->setScopes(['https://www.googleapis.com/auth/drive.readonly']); $client->setCache($cache); @@ -90,9 +94,9 @@ public function testFileCache() }); /* Make another service call */ - $service = new Google_Service_Drive($client); + $service = new Drive($client); $files = $service->files->listFiles(); - $this->assertInstanceOf('Google_Service_Drive_FileList', $files); + $this->assertInstanceOf(Drive\FileList::class, $files); $this->assertNotEquals($token1, $token2); } diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index b53a0a397..a7b6bd9f4 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -18,17 +18,31 @@ * under the License. */ -use GuzzleHttp\Client; +namespace Google\Tests; + +use Google\Client; +use Google\Service\Drive; +use Google\AuthHandler\AuthHandlerFactory; +use Google\Auth\FetchAuthTokenCache; +use Google\Auth\GCECache; +use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; +use GuzzleHttp\Exception\ClientException; use Prophecy\Argument; use Psr\Http\Message\RequestInterface; - -class Google_ClientTest extends BaseTest +use Psr\Cache\CacheItemInterface; +use Psr\Cache\CacheItemPoolInterface; +use ReflectionClass; +use ReflectionMethod; +use InvalidArgumentException; +use Exception; + +class ClientTest extends BaseTest { public function testClientConstructor() { - $this->assertInstanceOf('Google_Client', $this->getClient()); + $this->assertInstanceOf(Client::class, $this->getClient()); } public function testSignAppKey() @@ -36,7 +50,7 @@ public function testSignAppKey() $client = $this->getClient(); $client->setDeveloperKey('devKey'); - $http = new Client(); + $http = new GuzzleClient(); $client->authorize($http); $this->checkAuthHandler($http, 'Simple'); @@ -93,7 +107,7 @@ private function checkCredentials($http, $fetcherClass, $sub = null) $property = $class->getProperty('fetcher'); $property->setAccessible(true); $cacheFetcher = $property->getValue($auth); - $this->assertInstanceOf('Google\Auth\FetchAuthTokenCache', $cacheFetcher); + $this->assertInstanceOf(FetchAuthTokenCache::class, $cacheFetcher); $class = new ReflectionClass(get_class($cacheFetcher)); $property = $class->getProperty('fetcher'); @@ -116,7 +130,7 @@ public function testSignAccessToken() { $client = $this->getClient(); - $http = new Client(); + $http = new GuzzleClient(); $client->setAccessToken([ 'access_token' => 'test_token', 'expires_in' => 3600, @@ -179,7 +193,7 @@ public function testCreateAuthUrl() public function testPrepareNoScopes() { - $client = new Google_Client(); + $client = new Client(); $scopes = $client->prepareScopes(); $this->assertNull($scopes); @@ -187,7 +201,7 @@ public function testPrepareNoScopes() public function testNoAuthIsNull() { - $client = new Google_Client(); + $client = new Client(); $this->assertNull($client->getAccessToken()); } @@ -196,7 +210,7 @@ public function testPrepareService() { $this->onlyGuzzle6Or7(); - $client = new Google_Client(); + $client = new Client(); $client->setScopes(array("scope1", "scope2")); $scopes = $client->prepareScopes(); $this->assertEquals("scope1 scope2", $scopes); @@ -248,13 +262,13 @@ public function testPrepareService() ->willReturn($response->reveal()); $client->setHttpClient($http->reveal()); - $dr_service = new Google_Service_Drive($client); - $this->assertInstanceOf('Google_Model', $dr_service->files->listFiles()); + $dr_service = new Drive($client); + $this->assertInstanceOf('Google\Model', $dr_service->files->listFiles()); } public function testDefaultLogger() { - $client = new Google_Client(); + $client = new Client(); $logger = $client->getLogger(); $this->assertInstanceOf('Monolog\Logger', $logger); $handler = $logger->popHandler(); @@ -264,7 +278,7 @@ public function testDefaultLogger() public function testDefaultLoggerAppEngine() { $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine'; - $client = new Google_Client(); + $client = new Client(); $logger = $client->getLogger(); $handler = $logger->popHandler(); unset($_SERVER['SERVER_SOFTWARE']); @@ -275,7 +289,7 @@ public function testDefaultLoggerAppEngine() public function testSettersGetters() { - $client = new Google_Client(); + $client = new Client(); $client->setClientId("client1"); $client->setClientSecret('client1secret'); $client->setState('1'); @@ -285,9 +299,9 @@ public function testSettersGetters() $client->setRedirectUri('localhost'); $client->setConfig('application_name', 'me'); - $cache = $this->prophesize('Psr\Cache\CacheItemPoolInterface'); + $cache = $this->prophesize(CacheItemPoolInterface::class); $client->setCache($cache->reveal()); - $this->assertInstanceOf('Psr\Cache\CacheItemPoolInterface', $client->getCache()); + $this->assertInstanceOf(CacheItemPoolInterface::class, $client->getCache()); try { $client->setAccessToken(null); @@ -303,7 +317,7 @@ public function testSettersGetters() public function testDefaultConfigOptions() { - $client = new Google_Client(); + $client = new Client(); if ($this->isGuzzle6() || $this->isGuzzle7()) { $this->assertArrayHasKey('http_errors', $client->getHttpClient()->getConfig()); $this->assertArrayNotHasKey('exceptions', $client->getHttpClient()->getConfig()); @@ -321,7 +335,7 @@ public function testAppEngineStreamHandlerConfig() $this->onlyGuzzle5(); $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine'; - $client = new Google_Client(); + $client = new Client(); // check Stream Handler is used $http = $client->getHttpClient(); @@ -345,7 +359,7 @@ public function testAppEngineVerifyConfig() $this->onlyGuzzle5(); $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine'; - $client = new Google_Client(); + $client = new Client(); $this->assertEquals( '/etc/ca-certificates.crt', @@ -358,7 +372,7 @@ public function testAppEngineVerifyConfig() public function testJsonConfig() { // Device config - $client = new Google_Client(); + $client = new Client(); $device = '{"installed":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret"'. ':"N0aHCBT1qX1VAcF5J1pJAn6S","token_uri":"https://oauth2.googleapis.com/token",'. @@ -372,7 +386,7 @@ public function testJsonConfig() $this->assertEquals($client->getRedirectUri(), $dObj['installed']['redirect_uris'][0]); // Web config - $client = new Google_Client(); + $client = new Client(); $web = '{"web":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret"' . ':"lpoubuib8bj-Fmke_YhhyHGgXc","token_uri":"https://oauth2.googleapis.com/token"' . ',"client_email":"123456789@developer.gserviceaccount.com","client_x509_cert_url":'. @@ -389,7 +403,7 @@ public function testJsonConfig() public function testIniConfig() { $config = parse_ini_file(__DIR__ . '/../config/test.ini'); - $client = new Google_Client($config); + $client = new Client($config); $this->assertEquals('My Test application', $client->getConfig('application_name')); $this->assertEquals( @@ -401,7 +415,7 @@ public function testIniConfig() public function testNoAuth() { /** @var $noAuth Google_Auth_Simple */ - $client = new Google_Client(); + $client = new Client(); $client->setDeveloperKey(null); // unset application credentials @@ -409,7 +423,7 @@ public function testNoAuth() $HOME = getenv('HOME'); putenv('GOOGLE_APPLICATION_CREDENTIALS='); putenv('HOME='.sys_get_temp_dir()); - $http = new Client(); + $http = new GuzzleClient(); $client->authorize($http); putenv("GOOGLE_APPLICATION_CREDENTIALS=$GOOGLE_APPLICATION_CREDENTIALS"); @@ -422,10 +436,10 @@ public function testApplicationDefaultCredentials() $this->checkServiceAccountCredentials(); $credentialsFile = getenv('GOOGLE_APPLICATION_CREDENTIALS'); - $client = new Google_Client(); + $client = new Client(); $client->setAuthConfig($credentialsFile); - $http = new Client(); + $http = new GuzzleClient(); $client->authorize($http); $this->checkAuthHandler($http, 'AuthToken'); @@ -438,11 +452,11 @@ public function testApplicationDefaultCredentialsWithSubject() $credentialsFile = getenv('GOOGLE_APPLICATION_CREDENTIALS'); $sub = 'sub123'; - $client = new Google_Client(); + $client = new Client(); $client->setAuthConfig($credentialsFile); $client->setSubject($sub); - $http = new Client(); + $http = new GuzzleClient(); $client->authorize($http); $this->checkAuthHandler($http, 'AuthToken'); @@ -481,7 +495,7 @@ public function testRefreshTokenSetsValues() $http = $this->prophesize('GuzzleHttp\ClientInterface'); if ($this->isGuzzle5()) { - $guzzle5Request = new GuzzleHttp\Message\Request('POST', '/', ['body' => $token]); + $guzzle5Request = new \GuzzleHttp\Message\Request('POST', '/', ['body' => $token]); $http->createRequest(Argument::any(), Argument::any(), Argument::any()) ->shouldBeCalledTimes(1) ->willReturn($guzzle5Request); @@ -535,7 +549,7 @@ public function testRefreshTokenIsSetOnRefresh() $http = $this->prophesize('GuzzleHttp\ClientInterface'); if ($this->isGuzzle5()) { - $guzzle5Request = new GuzzleHttp\Message\Request('POST', '/', ['body' => $token]); + $guzzle5Request = new \GuzzleHttp\Message\Request('POST', '/', ['body' => $token]); $http->createRequest(Argument::any(), Argument::any(), Argument::any()) ->willReturn($guzzle5Request); @@ -587,7 +601,7 @@ public function testRefreshTokenIsNotSetWhenNewRefreshTokenIsReturned() $http = $this->prophesize('GuzzleHttp\ClientInterface'); if ($this->isGuzzle5()) { - $guzzle5Request = new GuzzleHttp\Message\Request('POST', '/', ['body' => $token]); + $guzzle5Request = new \GuzzleHttp\Message\Request('POST', '/', ['body' => $token]); $http->createRequest(Argument::any(), Argument::any(), Argument::any()) ->willReturn($guzzle5Request); @@ -666,7 +680,7 @@ public function testBadSubjectThrowsException() $client->useApplicationDefaultCredentials(); $client->setSubject('bad-subject'); - $authHandler = Google_AuthHandler_AuthHandlerFactory::build(); + $authHandler = AuthHandlerFactory::build(); // make this method public for testing purposes $method = new ReflectionMethod($authHandler, 'createAuthHttp'); @@ -676,7 +690,7 @@ public function testBadSubjectThrowsException() try { $token = $client->fetchAccessTokenWithAssertion($authHttp); $this->fail('no exception thrown'); - } catch (GuzzleHttp\Exception\ClientException $e) { + } catch (ClientException $e) { $response = $e->getResponse(); $this->assertContains('Invalid impersonation', (string) $response->getBody()); } @@ -766,7 +780,7 @@ public function testDefaultTokenCallback() /** @runInSeparateProcess */ public function testOnGceCacheAndCacheOptions() { - if (!class_exists('Google\Auth\GCECache')) { + if (!class_exists(GCECache::class)) { $this->markTestSkipped('Requires google/auth >= 1.12'); } @@ -775,19 +789,19 @@ public function testOnGceCacheAndCacheOptions() $prefix = 'test_prefix_'; $cacheConfig = ['gce_prefix' => $prefix]; - $mockCacheItem = $this->prophesize('Psr\Cache\CacheItemInterface'); + $mockCacheItem = $this->prophesize(CacheItemInterface::class); $mockCacheItem->isHit() ->willReturn(true); $mockCacheItem->get() ->shouldBeCalledTimes(1) ->willReturn(true); - $mockCache = $this->prophesize('Psr\Cache\CacheItemPoolInterface'); - $mockCache->getItem($prefix . Google\Auth\GCECache::GCE_CACHE_KEY) + $mockCache = $this->prophesize(CacheItemPoolInterface::class); + $mockCache->getItem($prefix . GCECache::GCE_CACHE_KEY) ->shouldBeCalledTimes(1) ->willReturn($mockCacheItem->reveal()); - $client = new Google_Client(['cache_config' => $cacheConfig]); + $client = new Client(['cache_config' => $cacheConfig]); $client->setCache($mockCache->reveal()); $client->useApplicationDefaultCredentials(); $client->authorize(); @@ -798,7 +812,7 @@ public function testFetchAccessTokenWithAssertionCache() { $this->checkServiceAccountCredentials(); $cachedValue = ['access_token' => '2/abcdef1234567890']; - $mockCacheItem = $this->prophesize('Psr\Cache\CacheItemInterface'); + $mockCacheItem = $this->prophesize(CacheItemInterface::class); $mockCacheItem->isHit() ->shouldBeCalledTimes(1) ->willReturn(true); @@ -806,12 +820,12 @@ public function testFetchAccessTokenWithAssertionCache() ->shouldBeCalledTimes(1) ->willReturn($cachedValue); - $mockCache = $this->prophesize('Psr\Cache\CacheItemPoolInterface'); + $mockCache = $this->prophesize(CacheItemPoolInterface::class); $mockCache->getItem(Argument::any()) ->shouldBeCalledTimes(1) ->willReturn($mockCacheItem->reveal()); - $client = new Google_Client(); + $client = new Client(); $client->setCache($mockCache->reveal()); $client->useApplicationDefaultCredentials(); $token = $client->fetchAccessTokenWithAssertion(); @@ -821,8 +835,8 @@ public function testFetchAccessTokenWithAssertionCache() public function testCacheClientOption() { - $mockCache = $this->prophesize('Psr\Cache\CacheItemPoolInterface'); - $client = new Google_Client([ + $mockCache = $this->prophesize(CacheItemPoolInterface::class); + $client = new Client([ 'cache' => $mockCache->reveal() ]); $this->assertEquals($mockCache->reveal(), $client->getCache()); @@ -832,17 +846,20 @@ public function testExecuteWithFormat() { $this->onlyGuzzle6Or7(); - $client = new Google_Client([ + $client = new Client([ 'api_format_v2' => true ]); $guzzle = $this->prophesize('GuzzleHttp\Client'); - $guzzle->send(Argument::allOf( + $guzzle + ->send(Argument::allOf( Argument::type('Psr\Http\Message\RequestInterface'), Argument::that(function (RequestInterface $request) { return $request->getHeaderLine('X-GOOG-API-FORMAT-VERSION') === '2'; }) - ), [])->willReturn(new Response(200, [], null)); + ), []) + ->shouldBeCalled() + ->willReturn(new Response(200, [], null)); $client->setHttpClient($guzzle->reveal()); @@ -854,19 +871,19 @@ public function testExecuteSetsCorrectHeaders() { $this->onlyGuzzle6Or7(); - $client = new Google_Client(); + $client = new Client(); $guzzle = $this->prophesize('GuzzleHttp\Client'); $guzzle->send(Argument::that(function (RequestInterface $request) { $userAgent = sprintf( '%s%s', - Google_Client::USER_AGENT_SUFFIX, - Google_Client::LIBVER + Client::USER_AGENT_SUFFIX, + Client::LIBVER ); $xGoogApiClient = sprintf( 'gl-php/%s gdcl/%s', phpversion(), - Google_Client::LIBVER + Client::LIBVER ); if ($request->getHeaderLine('User-Agent') !== $userAgent) { @@ -900,31 +917,31 @@ public function testClientOptions() ]; $tmpCredFile = tempnam(sys_get_temp_dir(), 'creds') . '.json'; file_put_contents($tmpCredFile, json_encode($tmpCreds)); - $client = new Google_Client([ + $client = new Client([ 'credentials' => $tmpCredFile ]); $this->assertEquals('foo', $client->getClientId()); // Test credentials array - $client = new Google_Client([ + $client = new Client([ 'credentials' => $tmpCredFile ]); $this->assertEquals('foo', $client->getClientId()); // Test singular scope - $client = new Google_Client([ + $client = new Client([ 'scopes' => 'a-scope' ]); $this->assertEquals(['a-scope'], $client->getScopes()); // Test multiple scopes - $client = new Google_Client([ + $client = new Client([ 'scopes' => ['one-scope', 'two-scope'] ]); $this->assertEquals(['one-scope', 'two-scope'], $client->getScopes()); // Test quota project - $client = new Google_Client([ + $client = new Client([ 'quota_project' => 'some-quota-project' ]); $this->assertEquals('some-quota-project', $client->getConfig('quota_project')); diff --git a/tests/Google/Http/BatchTest.php b/tests/Google/Http/BatchTest.php index ed3f25f63..021cad2c1 100644 --- a/tests/Google/Http/BatchTest.php +++ b/tests/Google/Http/BatchTest.php @@ -18,104 +18,49 @@ * under the License. */ +namespace Google\Tests\Http; + +use Google\Tests\BaseTest; +use Google\Http\Batch; +use Google\Service\Books; +use Google\Service\Storage; +use Google\Service\Exception as ServiceException; use GuzzleHttp\Psr7; -class Google_Http_BatchTest extends BaseTest +class BatchTest extends BaseTest { - public function testBatchRequestWithAuth() - { - $this->checkToken(); - - $client = $this->getClient(); - $batch = new Google_Http_Batch($client); - $plus = new Google_Service_Plus($client); - - $client->setUseBatch(true); - $batch->add($plus->people->get('me'), 'key1'); - $batch->add($plus->people->get('me'), 'key2'); - $batch->add($plus->people->get('me'), 'key3'); - - $result = $batch->execute(); - $this->assertArrayHasKey('response-key1', $result); - $this->assertArrayHasKey('response-key2', $result); - $this->assertArrayHasKey('response-key3', $result); - - } - public function testBatchRequest() { $this->checkKey(); $client = $this->getClient(); - $plus = new Google_Service_Plus($client); - $batch = $plus->createBatch(); - - $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 testBatchRequestWithBooksApi() - { - $this->checkKey(); - $client = $this->getClient(); - $plus = new Google_Service_Plus($client); - $batch = $plus->createBatch(); - - $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(); - - $client = $this->getClient(); - $shortener = new Google_Service_Urlshortener($client); - $batch = $shortener->createBatch(); - - $url1 = new Google_Service_Urlshortener_Url; - $url2 = new Google_Service_Urlshortener_Url; - $url3 = new Google_Service_Urlshortener_Url; - $url1->setLongUrl('http://brentertainment.com/'); - $url2->setLongUrl('http://morehazards.com/'); - $url3->setLongUrl('http://github.com/bshaffer'); + $client->setUseBatch(true); + $books = new Books($client); + $batch = $books->createBatch(); - $batch->add($shortener->url->insert($url1), 'key1'); - $batch->add($shortener->url->insert($url2), 'key2'); - $batch->add($shortener->url->insert($url3), 'key3'); + $batch->add($books->volumes->listVolumes('Henry David Thoreau'), 'key1'); + $batch->add($books->volumes->listVolumes('Edgar Allen Poe'), 'key2'); $result = $batch->execute(); $this->assertArrayHasKey('response-key1', $result); $this->assertArrayHasKey('response-key2', $result); - $this->assertArrayHasKey('response-key3', $result); } public function testInvalidBatchRequest() { $this->checkKey(); $client = $this->getClient(); - $plus = new Google_Service_Plus($client); - - $batch = $plus->createBatch(); + $client->setUseBatch(true); + $books = new Books($client); + $batch = $books->createBatch(); - $batch->add($plus->people->get('123456789987654321'), 'key1'); - $batch->add($plus->people->get('+LarryPage'), 'key2'); + $batch->add($books->volumes->listVolumes(false), 'key1'); + $batch->add($books->volumes->listVolumes('Edgar Allen Poe'), 'key2'); $result = $batch->execute(); + $this->assertArrayHasKey('response-key1', $result); $this->assertArrayHasKey('response-key2', $result); $this->assertInstanceOf( - 'Google_Service_Exception', + ServiceException::class, $result['response-key1'] ); } @@ -123,7 +68,7 @@ public function testInvalidBatchRequest() public function testMediaFileBatch() { $client = $this->getClient(); - $storage = new Google_Service_Storage($client); + $storage = new Storage($client); $bucket = 'testbucket'; $stream = Psr7\stream_for("testbucket-text"); $params = [ @@ -132,7 +77,7 @@ public function testMediaFileBatch() ]; // Metadata object for new Google Cloud Storage object - $obj = new Google_Service_Storage_StorageObject(); + $obj = new Storage\StorageObject(); $obj->contentType = "text/plain"; // Batch Upload diff --git a/tests/Google/Http/MediaFileUploadTest.php b/tests/Google/Http/MediaFileUploadTest.php index 911424ad8..f04f6ea12 100644 --- a/tests/Google/Http/MediaFileUploadTest.php +++ b/tests/Google/Http/MediaFileUploadTest.php @@ -1,8 +1,5 @@ getClient(); $request = new Request('POST', 'http://www.example.com/'); - $media = new Google_Http_MediaFileUpload( + $media = new MediaFileUpload( $client, $request, 'image/png', @@ -46,15 +51,15 @@ public function testGetUploadType() $request = new Request('POST', 'http://www.example.com/'); // Test resumable upload - $media = new Google_Http_MediaFileUpload($client, $request, 'image/png', 'a', true); + $media = new MediaFileUpload($client, $request, 'image/png', 'a', true); $this->assertEquals('resumable', $media->getUploadType(null)); // Test data *only* uploads - $media = new Google_Http_MediaFileUpload($client, $request, 'image/png', 'a', false); + $media = new MediaFileUpload($client, $request, 'image/png', 'a', false); $this->assertEquals('media', $media->getUploadType(null)); // Test multipart uploads - $media = new Google_Http_MediaFileUpload($client, $request, 'image/png', 'a', false); + $media = new MediaFileUpload($client, $request, 'image/png', 'a', false); $this->assertEquals('multipart', $media->getUploadType(array('a' => 'b'))); } @@ -65,7 +70,7 @@ public function testProcess() // Test data *only* uploads. $request = new Request('POST', 'http://www.example.com/'); - $media = new Google_Http_MediaFileUpload($client, $request, 'image/png', $data, false); + $media = new MediaFileUpload($client, $request, 'image/png', $data, false); $request = $media->getRequest(); $this->assertEquals($data, (string) $request->getBody()); @@ -73,7 +78,7 @@ public function testProcess() $request = new Request('POST', 'http://www.example.com/'); $reqData = json_encode("hello"); $request = $request->withBody(Psr7\stream_for($reqData)); - $media = new Google_Http_MediaFileUpload($client, $request, 'image/png', $data, true); + $media = new MediaFileUpload($client, $request, 'image/png', $data, true); $request = $media->getRequest(); $this->assertEquals(json_decode($reqData), (string) $request->getBody()); @@ -81,7 +86,7 @@ public function testProcess() $request = new Request('POST', 'http://www.example.com/'); $reqData = json_encode("hello"); $request = $request->withBody(Psr7\stream_for($reqData)); - $media = new Google_Http_MediaFileUpload($client, $request, 'image/png', $data, false); + $media = new MediaFileUpload($client, $request, 'image/png', $data, false); $request = $media->getRequest(); $this->assertContains($reqData, (string) $request->getBody()); $this->assertContains(base64_encode($data), (string) $request->getBody()); @@ -93,8 +98,8 @@ public function testGetResumeUri() $client = $this->getClient(); $client->addScope("https://www.googleapis.com/auth/drive"); - $service = new Google_Service_Drive($client); - $file = new Google_Service_Drive_DriveFile(); + $service = new Drive($client); + $file = new Drive\DriveFile(); $file->name = 'TESTFILE-testGetResumeUri'; $chunkSizeBytes = 1 * 1024 * 1024; @@ -103,7 +108,7 @@ public function testGetResumeUri() $request = $service->files->create($file); // Create a media file upload to represent our upload process. - $media = new Google_Http_MediaFileUpload( + $media = new MediaFileUpload( $client, $request, 'text/plain', @@ -114,7 +119,7 @@ public function testGetResumeUri() // request the resumable url $uri = $media->getResumeUri(); - $this->assertInternalType('string', $uri); + $this->assertIsString($uri); // parse the URL $parts = parse_url(http://github.com/$uri); @@ -133,10 +138,10 @@ public function testNextChunk() $client = $this->getClient(); $client->addScope("https://www.googleapis.com/auth/drive"); - $service = new Google_Service_Drive($client); + $service = new Drive($client); $data = 'foo'; - $file = new Google_Service_Drive_DriveFile(); + $file = new Drive\DriveFile(); $file->name = $name = 'TESTFILE-testNextChunk'; // Call the API with the media upload, defer so it doesn't immediately return. @@ -144,7 +149,7 @@ public function testNextChunk() $request = $service->files->create($file); // Create a media file upload to represent our upload process. - $media = new Google_Http_MediaFileUpload( + $media = new MediaFileUpload( $client, $request, 'text/plain', @@ -155,7 +160,7 @@ public function testNextChunk() // upload the file $file = $media->nextChunk($data); - $this->assertInstanceOf('Google_Service_Drive_DriveFile', $file); + $this->assertInstanceOf(Drive\DriveFile::class, $file); $this->assertEquals($name, $file->name); // remove the file @@ -170,11 +175,11 @@ public function testNextChunkWithMoreRemaining() $client = $this->getClient(); $client->addScope("https://www.googleapis.com/auth/drive"); - $service = new Google_Service_Drive($client); + $service = new Drive($client); $chunkSizeBytes = 262144; // smallest chunk size allowed by APIs $data = str_repeat('.', $chunkSizeBytes+1); - $file = new Google_Service_Drive_DriveFile(); + $file = new Drive\DriveFile(); $file->name = $name = 'TESTFILE-testNextChunkWithMoreRemaining'; // Call the API with the media upload, defer so it doesn't immediately return. @@ -182,7 +187,7 @@ public function testNextChunkWithMoreRemaining() $request = $service->files->create($file); // Create a media file upload to represent our upload process. - $media = new Google_Http_MediaFileUpload( + $media = new MediaFileUpload( $client, $request, 'text/plain', diff --git a/tests/Google/Http/RESTTest.php b/tests/Google/Http/RESTTest.php index 9f28cc269..6df7d3bc7 100644 --- a/tests/Google/Http/RESTTest.php +++ b/tests/Google/Http/RESTTest.php @@ -15,20 +15,26 @@ * limitations under the License. */ +namespace Google\Tests\Http; + +use Google\Http\REST; +use Google\Service\Exception as ServiceException; +use Google\Tests\BaseTest; +use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; -class Google_HTTP_RESTTest extends BaseTest +class RESTTest extends BaseTest { /** - * @var Google_Http_REST $rest + * @var REST $rest */ private $rest; public function setUp(): void { - $this->rest = new Google_Http_REST(); + $this->rest = new REST(); $this->request = new Request('GET', '/'); } @@ -63,17 +69,17 @@ public function testDecodeMediaResponse() } - /** @expectedException Google_Service_Exception */ public function testDecode500ResponseThrowsException() { + $this->expectException(ServiceException::class); $response = new Response(500); $this->rest->decodeHttpResponse($response, $this->request); } - /** @expectedException Google_Service_Exception */ public function testExceptionResponse() { - $http = new GuzzleHttp\Client(); + $this->expectException(ServiceException::class); + $http = new GuzzleClient(); $request = new Request('GET', 'http://httpbin.org/status/500'); $response = $this->rest->doExecute($http, $request); @@ -87,11 +93,9 @@ public function testDecodeEmptyResponse() $this->assertEquals('{}', (string) $decoded->getBody()); } - /** - * @expectedException Google_Service_Exception - */ public function testBadErrorFormatting() { + $this->expectException(ServiceException::class); $stream = Psr7\stream_for( '{ "error": { @@ -104,11 +108,9 @@ public function testBadErrorFormatting() $this->rest->decodeHttpResponse($response, $this->request); } - /** - * @expectedException Google_Service_Exception - */ public function tesProperErrorFormatting() { + $this->expectException(ServiceException::class); $stream = Psr7\stream_for( '{ error: { @@ -129,11 +131,9 @@ public function tesProperErrorFormatting() $this->rest->decodeHttpResponse($response, $this->request); } - /** - * @expectedException Google_Service_Exception - */ public function testNotJson404Error() { + $this->expectException(ServiceException::class); $stream = Psr7\stream_for('Not Found'); $response = new Response(404, array(), $stream); $this->rest->decodeHttpResponse($response, $this->request); diff --git a/tests/Google/ModelTest.php b/tests/Google/ModelTest.php index d1480868b..240faabcc 100644 --- a/tests/Google/ModelTest.php +++ b/tests/Google/ModelTest.php @@ -18,7 +18,15 @@ * under the License. */ -class Google_ModelTest extends BaseTest +namespace Google\Tests; + +use Google\Model; +use Google\Service\AdExchangeBuyer; +use Google\Service\Calendar; +use Google\Service\Dfareporting; +use Google\Service\Drive; + +class ModelTest extends BaseTest { private $calendarData = '{ "kind": "calendar#event", @@ -56,12 +64,12 @@ class Google_ModelTest extends BaseTest public function testIntentionalNulls() { $data = json_decode($this->calendarData, true); - $event = new Google_Service_Calendar_Event($data); + $event = new Calendar\Event($data); $obj = json_decode(json_encode($event->toSimpleObject()), true); $this->assertArrayHasKey('date', $obj['start']); $this->assertArrayNotHasKey('dateTime', $obj['start']); - $date = new Google_Service_Calendar_EventDateTime(); - $date->setDate(Google_Model::NULL_VALUE); + $date = new Calendar\EventDateTime(); + $date->setDate(Model::NULL_VALUE); $event->setStart($date); $obj = json_decode(json_encode($event->toSimpleObject()), true); $this->assertNull($obj['start']['date']); @@ -71,8 +79,8 @@ public function testIntentionalNulls() public function testModelMutation() { $data = json_decode($this->calendarData, true); - $event = new Google_Service_Calendar_Event($data); - $date = new Google_Service_Calendar_EventDateTime(); + $event = new Calendar\Event($data); + $date = new Calendar\EventDateTime(); date_default_timezone_set('UTC'); $dateString = Date("c"); $summary = "hello"; @@ -85,14 +93,14 @@ public function testModelMutation() $this->assertEquals($dateString, $simpleEvent->end->date); $this->assertEquals($summary, $simpleEvent->summary); - $event2 = new Google_Service_Calendar_Event(); + $event2 = new Calendar\Event(); $this->assertNull($event2->getStart()); } public function testVariantTypes() { - $file = new Google_Service_Drive_DriveFile(); - $metadata = new Google_Service_Drive_DriveFileImageMediaMetadata(); + $file = new Drive\DriveFile(); + $metadata = new Drive\DriveFileImageMediaMetadata(); $metadata->setCameraMake('Pokémon Snap'); $file->setImageMediaMetadata($metadata); $data = json_decode(json_encode($file->toSimpleObject()), true); @@ -101,7 +109,7 @@ public function testVariantTypes() public function testOddMappingNames() { - $creative = new Google_Service_AdExchangeBuyer_Creative(); + $creative = new AdExchangeBuyer\Creative(); $creative->setAccountId('12345'); $creative->setBuyerCreativeId('12345'); $creative->setAdvertiserName('Hi'); @@ -118,13 +126,13 @@ public function testOddMappingNames() public function testJsonStructure() { - $model = new Google_Model(); + $model = new Model(); $model->publicA = "This is a string"; - $model2 = new Google_Model(); + $model2 = new Model(); $model2->publicC = 12345; $model2->publicD = null; $model->publicB = $model2; - $model3 = new Google_Model(); + $model3 = new Model(); $model3->publicE = 54321; $model3->publicF = null; $model->publicG = array($model3, "hello", false); @@ -146,14 +154,14 @@ public function testJsonStructure() public function testIssetPropertyOnModel() { - $model = new Google_Model(); + $model = new Model(); $model['foo'] = 'bar'; $this->assertTrue(isset($model->foo)); } public function testUnsetPropertyOnModel() { - $model = new Google_Model(); + $model = new Model(); $model['foo'] = 'bar'; unset($model->foo); $this->assertFalse(isset($model->foo)); @@ -176,7 +184,7 @@ public function testCollectionWithItemsFromConstructor() }', true ); - $collection = new Google_Service_Calendar_Events($data); + $collection = new Calendar\Events($data); $this->assertCount(4, $collection); $count = 0; foreach ($collection as $col) { @@ -197,12 +205,12 @@ public function testCollectionWithItemsFromSetter() }', true ); - $collection = new Google_Service_Calendar_Events($data); + $collection = new Calendar\Events($data); $collection->setItems([ - new Google_Service_Calendar_Event(['id' => 1]), - new Google_Service_Calendar_Event(['id' => 2]), - new Google_Service_Calendar_Event(['id' => 3]), - new Google_Service_Calendar_Event(['id' => 4]), + new Calendar\Event(['id' => 1]), + new Calendar\Event(['id' => 2]), + new Calendar\Event(['id' => 3]), + new Calendar\Event(['id' => 4]), ]); $this->assertCount(4, $collection); $count = 0; @@ -224,30 +232,30 @@ public function testMapDataType() }', true ); - $collection = new Google_Service_Calendar_Colors($data); + $collection = new Calendar\Colors($data); $this->assertCount(2, $collection->calendar); $this->assertTrue(isset($collection->calendar['regular'])); $this->assertTrue(isset($collection->calendar['inverted'])); - $this->assertInstanceOf('Google_Service_Calendar_ColorDefinition', $collection->calendar['regular']); + $this->assertInstanceOf(Calendar\ColorDefinition::class, $collection->calendar['regular']); $this->assertEquals('#FFF', $collection->calendar['regular']->getBackground()); $this->assertEquals('#FFF', $collection->calendar['inverted']->getForeground()); } public function testPassingInstanceInConstructor() { - $creator = new Google_Service_Calendar_EventCreator(); + $creator = new Calendar\EventCreator(); $creator->setDisplayName('Brent Shaffer'); $data = [ "creator" => $creator ]; - $event = new Google_Service_Calendar_Event($data); - $this->assertInstanceOf('Google_Service_Calendar_EventCreator', $event->getCreator()); + $event = new Calendar\Event($data); + $this->assertInstanceOf(Calendar\EventCreator::class, $event->getCreator()); $this->assertEquals('Brent Shaffer', $event->creator->getDisplayName()); } public function testPassingInstanceInConstructorForMap() { - $regular = new Google_Service_Calendar_ColorDefinition(); + $regular = new Calendar\ColorDefinition(); $regular->setBackground('#FFF'); $regular->setForeground('#000'); $data = [ @@ -256,11 +264,11 @@ public function testPassingInstanceInConstructorForMap() "inverted" => [ "background" => "#000", "foreground" => "#FFF" ], ] ]; - $collection = new Google_Service_Calendar_Colors($data); + $collection = new Calendar\Colors($data); $this->assertCount(2, $collection->calendar); $this->assertTrue(isset($collection->calendar['regular'])); $this->assertTrue(isset($collection->calendar['inverted'])); - $this->assertInstanceOf('Google_Service_Calendar_ColorDefinition', $collection->calendar['regular']); + $this->assertInstanceOf(Calendar\ColorDefinition::class, $collection->calendar['regular']); $this->assertEquals('#FFF', $collection->calendar['regular']->getBackground()); $this->assertEquals('#FFF', $collection->calendar['inverted']->getForeground()); } @@ -274,7 +282,7 @@ public function testKeyTypePropertyConflict() "duration" => 0, "durationType" => "unknown", ]; - $creativeAsset = new Google_Service_Dfareporting_CreativeAsset($data); + $creativeAsset = new Dfareporting\CreativeAsset($data); $this->assertEquals(0, $creativeAsset->getDuration()); $this->assertEquals('unknown', $creativeAsset->getDurationType()); } diff --git a/tests/Google/Service/AdSenseTest.php b/tests/Google/Service/AdSenseTest.php index 42268c75f..5fa69830f 100644 --- a/tests/Google/Service/AdSenseTest.php +++ b/tests/Google/Service/AdSenseTest.php @@ -15,13 +15,19 @@ * limitations under the License. */ -class Google_Service_AdSenseTest extends BaseTest +namespace Google\Tests\Service; + +use Google\Service\AdSense; +use Google\Tests\BaseTest; + +class AdSenseTest extends BaseTest { public $adsense; public function setUp(): void { + $this->markTestSkipped('Thesse tests need to be fixed'); $this->checkToken(); - $this->adsense = new Google_Service_AdSense($this->getClient()); + $this->adsense = new AdSense($this->getClient()); } public function testAccountsList() diff --git a/tests/Google/Service/PagespeedonlineTest.php b/tests/Google/Service/PagespeedonlineTest.php deleted file mode 100644 index 4ebc1837a..000000000 --- a/tests/Google/Service/PagespeedonlineTest.php +++ /dev/null @@ -1,29 +0,0 @@ -checkToken(); - $service = new Google_Service_Pagespeedonline($this->getClient()); - $psapi = $service->pagespeedapi; - $result = $psapi->runpagespeed('http://code.google.com/'); - $this->assertArrayHasKey('kind', $result); - $this->assertArrayHasKey('id', $result); - } -} diff --git a/tests/Google/Service/ResourceTest.php b/tests/Google/Service/ResourceTest.php index 8da5a7aa6..ca2b58f9c 100644 --- a/tests/Google/Service/ResourceTest.php +++ b/tests/Google/Service/ResourceTest.php @@ -18,17 +18,26 @@ * under the License. */ +namespace Google\Tests\Service; + +use Google\Client; +use Google\Tests\BaseTest; +use Google\Service as GoogleService; +use Google\Service\Exception as ServiceException; +use Google\Service\Resource as GoogleResource; +use Google\Exception as GoogleException; use GuzzleHttp\Message\Response as Guzzle5Response; use GuzzleHttp\Psr7; +use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Stream; use GuzzleHttp\Stream\Stream as Guzzle5Stream; use Prophecy\Argument; -class Test_Google_Service extends Google_Service +class TestService extends \Google\Service { - public function __construct(Google_Client $client) + public function __construct(Client $client) { parent::__construct($client); $this->rootUrl = "https://test.example.com/"; @@ -38,7 +47,7 @@ public function __construct(Google_Client $client) } } -class Test_MediaType_Stream extends Stream +class TestMediaTypeStream extends Stream { public $toStringCalled = false; @@ -50,31 +59,29 @@ public function __toString() } } -class Google_Service_ResourceTest extends BaseTest +class ResourceTest extends BaseTest { private $client; private $service; public function setUp(): void { - $this->client = $this->prophesize("Google_Client"); + $this->client = $this->prophesize(Client::class); $logger = $this->prophesize("Monolog\Logger"); $this->client->getLogger()->willReturn($logger->reveal()); $this->client->shouldDefer()->willReturn(true); - $this->client->getHttpClient()->willReturn(new GuzzleHttp\Client()); + $this->client->getHttpClient()->willReturn(new GuzzleClient()); - $this->service = new Test_Google_Service($this->client->reveal()); + $this->service = new TestService($this->client->reveal()); } - /** - * @expectedException Google_Exception - * @expectedExceptionMessage Unknown function: test->testResource->someothermethod() - */ public function testCallFailure() { - $resource = new Google_Service_Resource( + $this->expectException(GoogleException::class); + $this->expectExceptionMessage('Unknown function: test->testResource->someothermethod()'); + $resource = new GoogleResource( $this->service, "test", "testResource", @@ -93,7 +100,7 @@ public function testCallFailure() public function testCall() { - $resource = new Google_Service_Resource( + $resource = new GoogleResource( $this->service, "test", "testResource", @@ -115,7 +122,7 @@ public function testCall() public function testCallServiceDefinedRoot() { $this->service->rootUrl = "https://sample.example.com/"; - $resource = new Google_Service_Resource( + $resource = new GoogleResource( $this->service, "test", "testResource", @@ -135,15 +142,15 @@ public function testCallServiceDefinedRoot() } /** - * Some Google Service (Google_Service_Directory_Resource_Channels and - * Google_Service_Reports_Resource_Channels) use a different servicePath value + * Some Google Service (Google\Service\Directory\Resource\Channels and + * Google\Service\Reports\Resource\Channels) use a different servicePath value * that should override the default servicePath value, it's represented by a / * before the resource path. All other Services have no / before the path */ public function testCreateRequestUriForASelfDefinedServicePath() { $this->service->servicePath = '/admin/directory/v1/'; - $resource = new Google_Service_Resource( + $resource = new GoogleResource( $this->service, 'test', 'testResource', @@ -164,9 +171,9 @@ public function testCreateRequestUriForASelfDefinedServicePath() public function testCreateRequestUri() { $restPath = "plus/{u}"; - $service = new Google_Service($this->client->reveal()); + $service = new GoogleService($this->client->reveal()); $service->servicePath = "http://localhost/"; - $resource = new Google_Service_Resource($service, 'test', 'testResource', array()); + $resource = new GoogleResource($service, 'test', 'testResource', array()); // Test Path $params = array(); @@ -218,7 +225,7 @@ public function testNoExpectedClassForAltMediaWithHttpSuccess() $response = new Guzzle5Response(200, [], $body); $http->createRequest(Argument::any(), Argument::any(), Argument::any()) - ->willReturn(new GuzzleHttp\Message\Request('GET', '/?alt=media')); + ->willReturn(new \GuzzleHttp\Message\Request('GET', '/?alt=media')); $http->send(Argument::type('GuzzleHttp\Message\Request')) ->shouldBeCalledTimes(1) @@ -232,12 +239,12 @@ public function testNoExpectedClassForAltMediaWithHttpSuccess() ->willReturn($response); } - $client = new Google_Client(); + $client = new Client(); $client->setHttpClient($http->reveal()); - $service = new Test_Google_Service($client); + $service = new TestService($client); // set up mock objects - $resource = new Google_Service_Resource( + $resource = new GoogleResource( $service, "test", "testResource", @@ -271,7 +278,7 @@ public function testNoExpectedClassForAltMediaWithHttpFail() $response = new Guzzle5Response(400, [], $body); $http->createRequest(Argument::any(), Argument::any(), Argument::any()) - ->willReturn(new GuzzleHttp\Message\Request('GET', '/?alt=media')); + ->willReturn(new \GuzzleHttp\Message\Request('GET', '/?alt=media')); $http->send(Argument::type('GuzzleHttp\Message\Request')) ->shouldBeCalledTimes(1) @@ -285,12 +292,12 @@ public function testNoExpectedClassForAltMediaWithHttpFail() ->willReturn($response); } - $client = new Google_Client(); + $client = new Client(); $client->setHttpClient($http->reveal()); - $service = new Test_Google_Service($client); + $service = new TestService($client); // set up mock objects - $resource = new Google_Service_Resource( + $resource = new GoogleResource( $service, "test", "testResource", @@ -309,7 +316,7 @@ public function testNoExpectedClassForAltMediaWithHttpFail() $expectedClass = 'ThisShouldBeIgnored'; $decoded = $resource->call('testMethod', $arguments, $expectedClass); $this->fail('should have thrown exception'); - } catch (Google_Service_Exception $e) { + } catch (ServiceException $e) { // Alt Media on error should return a safe error $this->assertEquals('thisisnotvalidjson', $e->getMessage()); } @@ -328,7 +335,7 @@ public function testErrorResponseWithVeryLongBody() $response = new Guzzle5Response(400, [], $body); $http->createRequest(Argument::any(), Argument::any(), Argument::any()) - ->willReturn(new GuzzleHttp\Message\Request('GET', '/?alt=media')); + ->willReturn(new \GuzzleHttp\Message\Request('GET', '/?alt=media')); $http->send(Argument::type('GuzzleHttp\Message\Request')) ->shouldBeCalledTimes(1) @@ -342,12 +349,12 @@ public function testErrorResponseWithVeryLongBody() ->willReturn($response); } - $client = new Google_Client(); + $client = new Client(); $client->setHttpClient($http->reveal()); - $service = new Test_Google_Service($client); + $service = new TestService($client); // set up mock objects - $resource = new Google_Service_Resource( + $resource = new GoogleResource( $service, "test", "testResource", @@ -366,7 +373,7 @@ public function testErrorResponseWithVeryLongBody() $expectedClass = 'ThisShouldBeIgnored'; $decoded = $resource->call('testMethod', $arguments, $expectedClass); $this->fail('should have thrown exception'); - } catch (Google_Service_Exception $e) { + } catch (ServiceException $e) { // empty message - alt=media means no message $this->assertEquals('this will be pulled into memory', $e->getMessage()); } @@ -380,7 +387,7 @@ public function testSuccessResponseWithVeryLongBody() $arguments = [['alt' => 'media']]; $request = new Request('GET', '/?alt=media'); $resource = fopen('php://temp', 'r+'); - $stream = new Test_MediaType_Stream($resource); + $stream = new TestMediaTypeStream($resource); $response = new Response(200, [], $stream); $http = $this->prophesize("GuzzleHttp\Client"); @@ -388,12 +395,12 @@ public function testSuccessResponseWithVeryLongBody() ->shouldBeCalledTimes(1) ->willReturn($response); - $client = new Google_Client(); + $client = new Client(); $client->setHttpClient($http->reveal()); - $service = new Test_Google_Service($client); + $service = new TestService($client); // set up mock objects - $resource = new Google_Service_Resource( + $resource = new GoogleResource( $service, "test", "testResource", @@ -433,7 +440,7 @@ public function testExceptionMessage() $response = new Guzzle5Response(400, [], $body); $http->createRequest(Argument::any(), Argument::any(), Argument::any()) - ->willReturn(new GuzzleHttp\Message\Request('GET', '/?alt=media')); + ->willReturn(new \GuzzleHttp\Message\Request('GET', '/?alt=media')); $http->send(Argument::type('GuzzleHttp\Message\Request')) ->shouldBeCalledTimes(1) @@ -447,12 +454,12 @@ public function testExceptionMessage() ->willReturn($response); } - $client = new Google_Client(); + $client = new Client(); $client->setHttpClient($http->reveal()); - $service = new Test_Google_Service($client); + $service = new TestService($client); // set up mock objects - $resource = new Google_Service_Resource( + $resource = new GoogleResource( $service, "test", "testResource", @@ -471,7 +478,7 @@ public function testExceptionMessage() $decoded = $resource->call('testMethod', array(array())); $this->fail('should have thrown exception'); - } catch (Google_Service_Exception $e) { + } catch (ServiceException $e) { $this->assertEquals($errors, $e->getErrors()); } } diff --git a/tests/Google/Service/TasksTest.php b/tests/Google/Service/TasksTest.php index 87361bcd0..862509418 100644 --- a/tests/Google/Service/TasksTest.php +++ b/tests/Google/Service/TasksTest.php @@ -15,15 +15,20 @@ * limitations under the License. */ -class Google_Service_TasksTest extends BaseTest +namespace Google\Tests\Service; + +use Google\Service\Tasks; +use Google\Tests\BaseTest; + +class TasksTest extends BaseTest { - /** @var Google_TasksService */ + /** @var Tasks */ public $taskService; public function setUp(): void { $this->checkToken(); - $this->taskService = new Google_Service_Tasks($this->getClient()); + $this->taskService = new Tasks($this->getClient()); } public function testInsertTask() @@ -67,7 +72,7 @@ public function testListTask() private function createTaskList($name) { - $list = new Google_Service_Tasks_TaskList(); + $list = new Tasks\TaskList(); $list->title = $name; return $this->taskService->tasklists->insert($list); } @@ -75,7 +80,7 @@ private function createTaskList($name) private function createTask($title, $listId) { $tasks = $this->taskService->tasks; - $task = new Google_Service_Tasks_Task(); + $task = new Tasks\Task(); $task->title = $title; return $tasks->insert($listId, $task); } diff --git a/tests/Google/Service/YouTubeTest.php b/tests/Google/Service/YouTubeTest.php index 2a9381a61..d40fd0a4c 100644 --- a/tests/Google/Service/YouTubeTest.php +++ b/tests/Google/Service/YouTubeTest.php @@ -15,14 +15,19 @@ * limitations under the License. */ -class Google_Service_YouTubeTest extends BaseTest +namespace Google\Tests\Service; + +use Google\Service\YouTube; +use Google\Tests\BaseTest; + +class YouTubeTest extends BaseTest { - /** @var Google_Service_YouTube */ + /** @var YouTube */ public $youtube; public function setUp(): void { $this->checkToken(); - $this->youtube = new Google_Service_YouTube($this->getClient()); + $this->youtube = new YouTube($this->getClient()); } public function testMissingFieldsAreNull() @@ -31,7 +36,7 @@ public function testMissingFieldsAreNull() $opts = array("mine" => true); $channels = $this->youtube->channels->listChannels($parts, $opts); - $newChannel = new Google_Service_YouTube_Channel(); + $newChannel = new YouTube\Channel(); $newChannel->setId( $channels[0]->getId()); $newChannel->setBrandingSettings($channels[0]->getBrandingSettings()); @@ -41,35 +46,35 @@ public function testMissingFieldsAreNull() $this->assertObjectHasAttribute('etag', $simpleOriginal); $this->assertObjectNotHasAttribute('etag', $simpleNew); - $owner_details = new Google_Service_YouTube_ChannelContentOwnerDetails(); + $owner_details = new YouTube\ChannelContentOwnerDetails(); $owner_details->setTimeLinked("123456789"); - $o_channel = new Google_Service_YouTube_Channel(); + $o_channel = new YouTube\Channel(); $o_channel->setContentOwnerDetails($owner_details); $simpleManual = $o_channel->toSimpleObject(); $this->assertObjectHasAttribute('timeLinked', $simpleManual->contentOwnerDetails); $this->assertObjectNotHasAttribute('contentOwner', $simpleManual->contentOwnerDetails); - $owner_details = new Google_Service_YouTube_ChannelContentOwnerDetails(); + $owner_details = new YouTube\ChannelContentOwnerDetails(); $owner_details->timeLinked = "123456789"; - $o_channel = new Google_Service_YouTube_Channel(); + $o_channel = new YouTube\Channel(); $o_channel->setContentOwnerDetails($owner_details); $simpleManual = $o_channel->toSimpleObject(); $this->assertObjectHasAttribute('timeLinked', $simpleManual->contentOwnerDetails); $this->assertObjectNotHasAttribute('contentOwner', $simpleManual->contentOwnerDetails); - $owner_details = new Google_Service_YouTube_ChannelContentOwnerDetails(); + $owner_details = new YouTube\ChannelContentOwnerDetails(); $owner_details['timeLinked'] = "123456789"; - $o_channel = new Google_Service_YouTube_Channel(); + $o_channel = new YouTube\Channel(); $o_channel->setContentOwnerDetails($owner_details); $simpleManual = $o_channel->toSimpleObject(); $this->assertObjectHasAttribute('timeLinked', $simpleManual->contentOwnerDetails); $this->assertObjectNotHasAttribute('contentOwner', $simpleManual->contentOwnerDetails); - $ping = new Google_Service_YouTube_ChannelConversionPing(); + $ping = new YouTube\ChannelConversionPing(); $ping->setContext("hello"); - $pings = new Google_Service_YouTube_ChannelConversionPings(); + $pings = new YouTube\ChannelConversionPings(); $pings->setPings(array($ping)); $simplePings = $pings->toSimpleObject(); $this->assertObjectHasAttribute('context', $simplePings->pings[0]); diff --git a/tests/Google/ServiceTest.php b/tests/Google/ServiceTest.php index 72b9a10d2..cf4bd20c0 100644 --- a/tests/Google/ServiceTest.php +++ b/tests/Google/ServiceTest.php @@ -18,10 +18,18 @@ * under the License. */ +namespace Google\Tests; + +use Google\Client; +use Google\Model; +use Google\Service; +use Google\Http\Batch; use PHPUnit\Framework\TestCase; use Prophecy\Argument; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; -class TestModel extends Google_Model +class TestModel extends Model { public function mapTypes($array) { @@ -34,20 +42,22 @@ public function isAssociativeArray($array) } } -class TestService extends Google_Service +class TestService extends Service { public $batchPath = 'batch/test'; } -class Google_ServiceTest extends TestCase +class ServiceTest extends TestCase { + private static $errorMessage; + public function testCreateBatch() { - $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); - $client = $this->prophesize('Google_Client'); + $response = $this->prophesize(ResponseInterface::class); + $client = $this->prophesize(Client::class); $client->execute(Argument::allOf( - Argument::type('Psr\Http\Message\RequestInterface'), + Argument::type(RequestInterface::class), Argument::that(function ($request) { $this->assertEquals('/batch/test', $request->getRequestTarget()); return $request; @@ -58,7 +68,7 @@ public function testCreateBatch() $model = new TestService($client->reveal()); $batch = $model->createBatch(); - $this->assertInstanceOf('Google_Http_Batch', $batch); + $this->assertInstanceOf(Batch::class, $batch); $batch->execute(); } @@ -110,7 +120,7 @@ public function testConfigConstructor() public function testNoConstructor() { $service = new TestService(); - $this->assertInstanceOf('Google_Client', $service->getClient()); + $this->assertInstanceOf(Client::class, $service->getClient()); } public function testInvalidConstructorPhp7Plus() @@ -121,7 +131,7 @@ public function testInvalidConstructorPhp7Plus() try { $service = new TestService('foo'); - } catch (TypeError $e) {} + } catch (\TypeError $e) {} $this->assertInstanceOf('TypeError', $e); $this->assertEquals( @@ -130,8 +140,6 @@ public function testInvalidConstructorPhp7Plus() ); } - private static $errorMessage; - /** @runInSeparateProcess */ public function testInvalidConstructorPhp5() { @@ -139,7 +147,7 @@ public function testInvalidConstructorPhp5() $this->markTestSkipped('PHP 5 only'); } - set_error_handler('Google_ServiceTest::handlePhp5Error'); + set_error_handler('Google\Tests\ServiceTest::handlePhp5Error'); $service = new TestService('foo'); diff --git a/tests/Google/Task/ComposerTest.php b/tests/Google/Task/ComposerTest.php index c2b81ca48..f4ff96a02 100644 --- a/tests/Google/Task/ComposerTest.php +++ b/tests/Google/Task/ComposerTest.php @@ -15,7 +15,14 @@ * limitations under the License. */ -class Google_Task_ComposerTest extends BaseTest +namespace Google\Tests\Task; + +use Google\Tests\BaseTest; +use Google\Task\Composer; +use Symfony\Component\Filesystem\Filesystem; +use InvalidArgumentException; + +class ComposerTest extends BaseTest { private static $composerBaseConfig = [ 'repositories' => [ @@ -36,47 +43,43 @@ class Google_Task_ComposerTest extends BaseTest 'minimum-stability' => 'dev', ]; - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage Google service "Foo" does not exist - */ public function testInvalidServiceName() { - Google_Task_Composer::cleanup($this->createMockEvent(['Foo'])); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Google service "Foo" does not exist'); + + Composer::cleanup($this->createMockEvent(['Foo'])); } - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage Invalid Google service name "../YouTube" - */ public function testRelatePathServiceName() { - Google_Task_Composer::cleanup($this->createMockEvent(['../YouTube'])); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid Google service name "../YouTube"'); + + Composer::cleanup($this->createMockEvent(['../YouTube'])); } - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage Google service "" does not exist - */ public function testEmptyServiceName() { - Google_Task_Composer::cleanup($this->createMockEvent([''])); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Google service "" does not exist'); + + Composer::cleanup($this->createMockEvent([''])); } - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage Invalid Google service name "YouTube*" - */ public function testWildcardServiceName() { - Google_Task_Composer::cleanup($this->createMockEvent(['YouTube*'])); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid Google service name "YouTube*"'); + + Composer::cleanup($this->createMockEvent(['YouTube*'])); } public function testRemoveServices() { $vendorDir = sys_get_temp_dir() . '/rand-' . rand(); $serviceDir = sprintf( - '%s/google/apiclient-services/src/Google/Service/', + '%s/google/apiclient-services/src/', $vendorDir ); $dirs = [ @@ -100,7 +103,7 @@ public function testRemoveServices() touch($serviceDir . $file); } $print = 'Removing 2 google services'; - Google_Task_Composer::cleanup( + Composer::cleanup( $this->createMockEvent(['ServiceToKeep'], $vendorDir, $print), $this->createMockFilesystem([ 'ServiceToDelete2', @@ -113,7 +116,7 @@ public function testRemoveServices() private function createMockFilesystem(array $files, $serviceDir) { - $mockFilesystem = $this->prophesize('Symfony\Component\Filesystem\Filesystem'); + $mockFilesystem = $this->prophesize(Filesystem::class); foreach ($files as $filename) { $file = new \SplFileInfo($serviceDir . $filename); $mockFilesystem->remove($file->getRealPath()) @@ -175,7 +178,7 @@ public function testE2E() ] ]); - $serviceDir = $dir . '/vendor/google/apiclient-services/src/Google/Service'; + $serviceDir = $dir . '/vendor/google/apiclient-services/src'; $this->assertFileExists($serviceDir . '/Drive.php'); $this->assertFileExists($serviceDir . '/Drive'); $this->assertFileExists($serviceDir . '/YouTube.php'); @@ -218,7 +221,7 @@ public function testE2EBCTaskName() $composerConfig['scripts']['pre-autoload-dump'] = 'Google_Task_Composer::cleanup'; $dir = $this->runComposerInstall($composerConfig); - $serviceDir = $dir . '/vendor/google/apiclient-services/src/Google/Service'; + $serviceDir = $dir . '/vendor/google/apiclient-services/src'; $this->assertFileExists($serviceDir . '/Drive.php'); $this->assertFileExists($serviceDir . '/Drive'); @@ -244,8 +247,8 @@ public function testE2EOptimized() $classmap = require_once $dir . '/vendor/composer/autoload_classmap.php'; // Verify removed services do not show up in the classmap - $this->assertArrayHasKey('Google_Service_Drive', $classmap); - $this->assertArrayNotHasKey('Google_Service_YouTube', $classmap); + $this->assertArrayHasKey('Google\Service\Drive', $classmap); + $this->assertArrayNotHasKey('Google\Service\YouTube', $classmap); } private function runComposerInstall(array $composerConfig, $dir = null) diff --git a/tests/Google/Task/RunnerTest.php b/tests/Google/Task/RunnerTest.php index 3d421328b..c138484c2 100644 --- a/tests/Google/Task/RunnerTest.php +++ b/tests/Google/Task/RunnerTest.php @@ -15,14 +15,24 @@ * limitations under the License. */ +namespace Google\Tests\Task; + +use Google\Client; +use Google\Task\Runner; +use Google\Tests\BaseTest; +use Google\Http\Request as GoogleRequest; +use Google\Http\REST; +use Google\Service\Exception as ServiceException; +use Google\Task\Exception as TaskException; use GuzzleHttp\Message\Response as Guzzle5Response; use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; use GuzzleHttp\Stream\Stream as Guzzle5Stream; use Prophecy\Argument; +use Exception; -class Google_Task_RunnerTest extends BaseTest +class RunnerTest extends BaseTest { private $client; @@ -34,36 +44,37 @@ class Google_Task_RunnerTest extends BaseTest protected function setUp(): void { - $this->client = new Google_Client(); + $this->client = new Client(); } /** * @dataProvider defaultRestErrorProvider - * @expectedException Google_Service_Exception */ public function testRestRetryOffByDefault($errorCode, $errorBody = '{}') { + $this->expectException(ServiceException::class); $this->setNextResponse($errorCode, $errorBody)->makeRequest(); } /** * @dataProvider defaultRestErrorProvider - * @expectedException Google_Service_Exception */ public function testOneRestRetryWithError($errorCode, $errorBody = '{}') { + $this->expectException(ServiceException::class); $this->setRetryConfig(array('retries' => 1)); $this->setNextResponses(2, $errorCode, $errorBody)->makeRequest(); } /** * @dataProvider defaultRestErrorProvider - * @expectedException Google_Service_Exception */ public function testMultipleRestRetriesWithErrors( $errorCode, $errorBody = '{}' ) { + $this->expectException(ServiceException::class); + $this->setRetryConfig(array('retries' => 5)); $this->setNextResponses(6, $errorCode, $errorBody)->makeRequest(); } @@ -98,12 +109,13 @@ public function testMultipleRestRetriesWithSuccess( /** * @dataProvider defaultRestErrorProvider - * @expectedException Google_Service_Exception */ public function testCustomRestRetryMapReplacesDefaults( $errorCode, $errorBody = '{}' ) { + $this->expectException(ServiceException::class); + $this->setRetryMap(array()); $this->setRetryConfig(array('retries' => 5)); @@ -113,7 +125,7 @@ public function testCustomRestRetryMapReplacesDefaults( public function testCustomRestRetryMapAddsNewHandlers() { $this->setRetryMap( - array('403' => Google_Task_Runner::TASK_RETRY_ALWAYS) + array('403' => Runner::TASK_RETRY_ALWAYS) ); $this->setRetryConfig(array('retries' => 5)); @@ -125,11 +137,12 @@ public function testCustomRestRetryMapAddsNewHandlers() } /** - * @expectedException Google_Service_Exception * @dataProvider customLimitsProvider */ public function testCustomRestRetryMapWithCustomLimits($limit) { + $this->expectException(ServiceException::class); + $this->setRetryMap( array('403' => $limit) ); @@ -157,20 +170,22 @@ public function testRestTimeouts($config, $minTime) /** * @requires extension curl * @dataProvider defaultCurlErrorProvider - * @expectedException Google_Service_Exception */ public function testCurlRetryOffByDefault($errorCode, $errorMessage = '') { + $this->expectException(ServiceException::class); + $this->setNextResponseThrows($errorMessage, $errorCode)->makeRequest(); } /** * @requires extension curl * @dataProvider defaultCurlErrorProvider - * @expectedException Google_Service_Exception */ public function testOneCurlRetryWithError($errorCode, $errorMessage = '') { + $this->expectException(ServiceException::class); + $this->setRetryConfig(array('retries' => 1)); $this->setNextResponsesThrow(2, $errorMessage, $errorCode)->makeRequest(); } @@ -178,12 +193,13 @@ public function testOneCurlRetryWithError($errorCode, $errorMessage = '') /** * @requires extension curl * @dataProvider defaultCurlErrorProvider - * @expectedException Google_Service_Exception */ public function testMultipleCurlRetriesWithErrors( $errorCode, $errorMessage = '' ) { + $this->expectException(ServiceException::class); + $this->setRetryConfig(array('retries' => 5)); $this->setNextResponsesThrow(6, $errorMessage, $errorCode)->makeRequest(); } @@ -221,12 +237,13 @@ public function testMultipleCurlRetriesWithSuccess( /** * @requires extension curl * @dataProvider defaultCurlErrorProvider - * @expectedException Google_Service_Exception */ public function testCustomCurlRetryMapReplacesDefaults( $errorCode, $errorMessage = '' ) { + $this->expectException(ServiceException::class); + $this->setRetryMap(array()); $this->setRetryConfig(array('retries' => 5)); @@ -239,7 +256,7 @@ public function testCustomCurlRetryMapReplacesDefaults( public function testCustomCurlRetryMapAddsNewHandlers() { $this->setRetryMap( - array(CURLE_COULDNT_RESOLVE_PROXY => Google_Task_Runner::TASK_RETRY_ALWAYS) + array(CURLE_COULDNT_RESOLVE_PROXY => Runner::TASK_RETRY_ALWAYS) ); $this->setRetryConfig(array('retries' => 5)); @@ -252,11 +269,12 @@ public function testCustomCurlRetryMapAddsNewHandlers() /** * @requires extension curl - * @expectedException Google_Service_Exception * @dataProvider customLimitsProvider */ public function testCustomCurlRetryMapWithCustomLimits($limit) { + $this->expectException(ServiceException::class); + $this->setRetryMap( array(CURLE_COULDNT_RESOLVE_PROXY => $limit) ); @@ -288,11 +306,11 @@ public function testCurlTimeouts($config, $minTime) */ public function testBadTaskConfig($config, $message) { - $this->expectException('Google_Task_Exception'); + $this->expectException(TaskException::class); $this->expectExceptionMessage($message); $this->setRetryConfig($config); - new Google_Task_Runner( + new Runner( $this->retryConfig, '', array($this, 'testBadTaskConfig') @@ -300,48 +318,45 @@ public function testBadTaskConfig($config, $message) } /** - * @expectedException Google_Task_Exception * @expectedExceptionMessage must be a valid callable */ public function testBadTaskCallback() { + $this->expectException(TaskException::class); $config = []; - new Google_Task_Runner($config, '', 5); + new Runner($config, '', 5); } - /** - * @expectedException Google_Service_Exception - */ public function testTaskRetryOffByDefault() { - $this->setNextTaskAllowedRetries(Google_Task_Runner::TASK_RETRY_ALWAYS) + $this->expectException(ServiceException::class); + + $this->setNextTaskAllowedRetries(Runner::TASK_RETRY_ALWAYS) ->runTask(); } - /** - * @expectedException Google_Service_Exception - */ public function testOneTaskRetryWithError() { + $this->expectException(ServiceException::class); + $this->setRetryConfig(array('retries' => 1)); - $this->setNextTasksAllowedRetries(2, Google_Task_Runner::TASK_RETRY_ALWAYS) + $this->setNextTasksAllowedRetries(2, Runner::TASK_RETRY_ALWAYS) ->runTask(); } - /** - * @expectedException Google_Service_Exception - */ public function testMultipleTaskRetriesWithErrors() { + $this->expectException(ServiceException::class); + $this->setRetryConfig(array('retries' => 5)); - $this->setNextTasksAllowedRetries(6, Google_Task_Runner::TASK_RETRY_ALWAYS) + $this->setNextTasksAllowedRetries(6, Runner::TASK_RETRY_ALWAYS) ->runTask(); } public function testOneTaskRetryWithSuccess() { $this->setRetryConfig(array('retries' => 1)); - $result = $this->setNextTaskAllowedRetries(Google_Task_Runner::TASK_RETRY_ALWAYS) + $result = $this->setNextTaskAllowedRetries(Runner::TASK_RETRY_ALWAYS) ->setNextTaskReturnValue('success') ->runTask(); @@ -351,7 +366,7 @@ public function testOneTaskRetryWithSuccess() public function testMultipleTaskRetriesWithSuccess() { $this->setRetryConfig(array('retries' => 5)); - $result = $this->setNextTasksAllowedRetries(2, Google_Task_Runner::TASK_RETRY_ALWAYS) + $result = $this->setNextTasksAllowedRetries(2, Runner::TASK_RETRY_ALWAYS) ->setNextTaskReturnValue('success') ->runTask(); @@ -359,11 +374,12 @@ public function testMultipleTaskRetriesWithSuccess() } /** - * @expectedException Google_Service_Exception * @dataProvider customLimitsProvider */ public function testTaskRetryWithCustomLimits($limit) { + $this->expectException(ServiceException::class); + $this->setRetryConfig(array('retries' => 5)); $this->setNextTasksAllowedRetries($limit + 1, $limit) ->runTask(); @@ -388,9 +404,9 @@ public function testTaskTimeouts($config, $minTime) public function testTaskWithManualRetries() { $this->setRetryConfig(array('retries' => 2)); - $this->setNextTasksAllowedRetries(2, Google_Task_Runner::TASK_RETRY_ALWAYS); + $this->setNextTasksAllowedRetries(2, Runner::TASK_RETRY_ALWAYS); - $task = new Google_Task_Runner( + $task = new Runner( $this->retryConfig, '', array($this, 'runNextTask') @@ -435,8 +451,8 @@ public function timeoutProvider() public function customLimitsProvider() { return array( - array(Google_Task_Runner::TASK_RETRY_NEVER), - array(Google_Task_Runner::TASK_RETRY_ONCE), + array(Runner::TASK_RETRY_NEVER), + array(Runner::TASK_RETRY_ONCE), ); } @@ -612,7 +628,7 @@ private function setNextResponsesThrow($count, $message, $code) */ private function setNextResponseThrows($message, $code) { - $this->mockedCalls[$this->mockedCallsCount++] = new Google_Service_Exception( + $this->mockedCalls[$this->mockedCallsCount++] = new ServiceException( $message, $code, null, @@ -635,7 +651,7 @@ private function makeRequest() if ($this->isGuzzle5()) { $http->createRequest(Argument::any(), Argument::any(), Argument::any()) ->shouldBeCalledTimes($this->mockedCallsCount) - ->willReturn(new GuzzleHttp\Message\Request('GET', '/test')); + ->willReturn(new \GuzzleHttp\Message\Request('GET', '/test')); $http->send(Argument::type('GuzzleHttp\Message\Request')) ->shouldBeCalledTimes($this->mockedCallsCount) @@ -646,15 +662,15 @@ private function makeRequest() ->will([$this, 'getNextMockedCall']); } - return Google_Http_REST::execute($http->reveal(), $request, '', $this->retryConfig, $this->retryMap); + return REST::execute($http->reveal(), $request, '', $this->retryConfig, $this->retryMap); } /** * Gets the next mocked response. * - * @param Google_Http_Request $request The mocked request + * @param GoogleRequest $request The mocked request * - * @return Google_Http_Request + * @return GoogleRequest */ public function getNextMockedCall($request) { @@ -725,7 +741,7 @@ private function setNextTasksAllowedRetries($count, $allowedRetries) */ private function runTask() { - $task = new Google_Task_Runner( + $task = new Runner( $this->retryConfig, '', array($this, 'runNextTask') @@ -735,7 +751,7 @@ private function runTask() $task->setRetryMap($this->retryMap); } - $exception = $this->prophesize('Google_Service_Exception'); + $exception = $this->prophesize(ServiceException::class); $exceptionCount = 0; $exceptionCalls = array(); diff --git a/tests/Google/Utils/UriTemplateTest.php b/tests/Google/Utils/UriTemplateTest.php index ba5dc3f3b..8a98ea8c9 100644 --- a/tests/Google/Utils/UriTemplateTest.php +++ b/tests/Google/Utils/UriTemplateTest.php @@ -18,14 +18,19 @@ * under the License. */ -class Google_Utils_UriTemplateTest extends BaseTest +namespace Google\Tests\Utils; + +use Google\Tests\BaseTest; +use Google\Utils\UriTemplate; + +class UriTemplateTest extends BaseTest { public function testLevelOne() { $var = "value"; $hello = "Hello World!"; - $urit = new Google_Utils_UriTemplate(); + $urit = new UriTemplate(); $this->assertEquals( "value", $urit->parse("{var}", array("var" => $var)) @@ -42,7 +47,7 @@ public function testLevelTwo() $hello = "Hello World!"; $path = "/foo/bar"; - $urit = new Google_Utils_UriTemplate(); + $urit = new UriTemplate(); $this->assertEquals( "value", $urit->parse("{+var}", array("var" => $var)) @@ -78,7 +83,7 @@ public function testLevelThree() $x = "1024"; $y = "768"; - $urit = new Google_Utils_UriTemplate(); + $urit = new UriTemplate(); $this->assertEquals( "map?1024,768", $urit->parse("map?{x,y}", array("x" => $x, "y" => $y)) @@ -219,7 +224,7 @@ public function testLevelFour() ); - $urit = new Google_Utils_UriTemplate(); + $urit = new UriTemplate(); foreach ($tests as $input => $output) { $this->assertEquals($output, $urit->parse($input, $values), $input . " failed"); @@ -230,7 +235,7 @@ public function testMultipleAnnotations() { $var = "value"; $hello = "Hello World!"; - $urit = new Google_Utils_UriTemplate(); + $urit = new UriTemplate(); $this->assertEquals( "http://www.google.com/Hello%20World!?var=value", $urit->parse( @@ -264,9 +269,9 @@ public function testMultipleAnnotations() */ public function testAgainstStandardTests() { - $location = "../../uritemplate-test/*.json"; + $location = __DIR__ . "/../../uritemplate-test/*.json"; - $urit = new Google_Utils_UriTemplate(); + $urit = new UriTemplate(); foreach (glob($location) as $file) { $test = json_decode(file_get_contents($file), true); foreach ($test as $title => $testsets) { diff --git a/tests/examples/batchTest.php b/tests/examples/batchTest.php index 425d9de2e..3628a11cb 100644 --- a/tests/examples/batchTest.php +++ b/tests/examples/batchTest.php @@ -19,7 +19,11 @@ * under the License. */ -class examples_batchTest extends BaseTest +namespace Google\Tests\Examples; + +use Google\Tests\BaseTest; + +class batchTest extends BaseTest { public function testBatch() { diff --git a/tests/examples/idTokenTest.php b/tests/examples/idTokenTest.php index acc9fd1d8..3f1b38834 100644 --- a/tests/examples/idTokenTest.php +++ b/tests/examples/idTokenTest.php @@ -19,7 +19,11 @@ * under the License. */ -class examples_idTokenTest extends BaseTest +namespace Google\Tests\Examples; + +use Google\Tests\BaseTest; + +class idTokenTest extends BaseTest { public function testIdToken() { diff --git a/tests/examples/indexTest.php b/tests/examples/indexTest.php index a62bbe2cb..0ef4f4d4e 100644 --- a/tests/examples/indexTest.php +++ b/tests/examples/indexTest.php @@ -19,7 +19,11 @@ * under the License. */ -class examples_indexTest extends BaseTest +namespace Google\Tests\Examples; + +use Google\Tests\BaseTest; + +class indexTest extends BaseTest { public function testIndex() { diff --git a/tests/examples/largeFileDownloadTest.php b/tests/examples/largeFileDownloadTest.php index e16e9d5d4..71286d9ac 100644 --- a/tests/examples/largeFileDownloadTest.php +++ b/tests/examples/largeFileDownloadTest.php @@ -18,7 +18,11 @@ * under the License. */ -class examples_largeFileDownloadTest extends BaseTest +namespace Google\Tests\Examples; + +use Google\Tests\BaseTest; + +class largeFileDownloadTest extends BaseTest { /** * @runInSeparateProcess diff --git a/tests/examples/largeFileUploadTest.php b/tests/examples/largeFileUploadTest.php index a927309f8..31e8f80d4 100644 --- a/tests/examples/largeFileUploadTest.php +++ b/tests/examples/largeFileUploadTest.php @@ -19,7 +19,11 @@ * under the License. */ -class examples_largeFileUploadTest extends BaseTest +namespace Google\Tests\Examples; + +use Google\Tests\BaseTest; + +class largeFileUploadTest extends BaseTest { /** * @runInSeparateProcess diff --git a/tests/examples/multiApiTest.php b/tests/examples/multiApiTest.php index 51e0a9b2b..569bb21d9 100644 --- a/tests/examples/multiApiTest.php +++ b/tests/examples/multiApiTest.php @@ -19,7 +19,11 @@ * under the License. */ -class examples_multiApiTest extends BaseTest +namespace Google\Tests\Examples; + +use Google\Tests\BaseTest; + +class multiApiTest extends BaseTest { public function testMultiApi() { diff --git a/tests/examples/serviceAccountTest.php b/tests/examples/serviceAccountTest.php index 81521c149..60aeb0212 100644 --- a/tests/examples/serviceAccountTest.php +++ b/tests/examples/serviceAccountTest.php @@ -19,7 +19,11 @@ * under the License. */ -class examples_serviceAccountTest extends BaseTest +namespace Google\Tests\Examples; + +use Google\Tests\BaseTest; + +class serviceAccountTest extends BaseTest { public function testServiceAccount() { diff --git a/tests/examples/simpleFileUploadTest.php b/tests/examples/simpleFileUploadTest.php index 8b0298ba2..192977ab7 100644 --- a/tests/examples/simpleFileUploadTest.php +++ b/tests/examples/simpleFileUploadTest.php @@ -19,7 +19,11 @@ * under the License. */ -class examples_simpleFileUploadTest extends BaseTest +namespace Google\Tests\Examples; + +use Google\Tests\BaseTest; + +class simpleFileUploadTest extends BaseTest { /** * @runInSeparateProcess diff --git a/tests/examples/simpleQueryTest.php b/tests/examples/simpleQueryTest.php index 274332aca..cbb49fd9d 100644 --- a/tests/examples/simpleQueryTest.php +++ b/tests/examples/simpleQueryTest.php @@ -19,7 +19,11 @@ * under the License. */ -class examples_simpleQueryTest extends BaseTest +namespace Google\Tests\Examples; + +use Google\Tests\BaseTest; + +class simpleQueryTest extends BaseTest { public function testSimpleQuery() { From b86b0c974bc998499e080f3d3178aaab06d07ee3 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 23 Jun 2021 13:16:21 -0500 Subject: [PATCH 196/301] chore: prepare v2.10.0 (#2090) --- README.md | 4 ++-- src/Client.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7346110da..235e05f2b 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ composer installed. Once composer is installed, execute the following command in your project root to install this library: ```sh -composer require google/apiclient:"^2.9" +composer require google/apiclient:^2.10 ``` Finally, be sure to include the autoloader: @@ -61,7 +61,7 @@ you want to keep in `composer.json`: ```json { "require": { - "google/apiclient": "^2.9" + "google/apiclient": "^2.10" }, "scripts": { "pre-autoload-dump": "Google\\Task\\Composer::cleanup" diff --git a/src/Client.php b/src/Client.php index d0cffa44b..174c5b333 100644 --- a/src/Client.php +++ b/src/Client.php @@ -49,7 +49,7 @@ */ class Client { - const LIBVER = "2.9.1"; + const LIBVER = "2.10.0"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; From f37538ecf176477876b26b4e3339a13f61b760d1 Mon Sep 17 00:00:00 2001 From: Thibault Vlacich Date: Thu, 24 Jun 2021 16:48:40 +0200 Subject: [PATCH 197/301] fix: composer requirement (#2093) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index cce3c93a1..b9d61ce64 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "require": { "php": "^5.6|^7.0|^8.0", "google/auth": "^1.10", - "google/apiclient-services": "^0.200.0", + "google/apiclient-services": "~0.200", "firebase/php-jwt": "~2.0||~3.0||~4.0||~5.0", "monolog/monolog": "^1.17|^2.0", "phpseclib/phpseclib": "~2.0||^3.0.2", From 11871e94006ce7a419bb6124d51b6f9ace3f679b Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 25 Jun 2021 09:25:44 -0500 Subject: [PATCH 198/301] chore: prepare v2.10.1 --- src/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index 174c5b333..7750efa31 100644 --- a/src/Client.php +++ b/src/Client.php @@ -49,7 +49,7 @@ */ class Client { - const LIBVER = "2.10.0"; + const LIBVER = "2.10.1"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; From 3a981757c0e6f89931dbe6a895f4e0f7610f50b5 Mon Sep 17 00:00:00 2001 From: Adam Mackiewicz Date: Fri, 27 Aug 2021 23:57:39 +0700 Subject: [PATCH 199/301] docs: fix phpdoc for some method return types (#2113) --- .gitignore | 1 + src/Client.php | 2 +- src/Http/REST.php | 2 +- src/Service/Resource.php | 2 +- 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 9b429eae6..812c3a3be 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ vendor examples/testfile-small.txt examples/testfile.txt tests/.apiKey +.idea/ diff --git a/src/Client.php b/src/Client.php index 7750efa31..7924ece36 100644 --- a/src/Client.php +++ b/src/Client.php @@ -856,7 +856,7 @@ public function prepareScopes() * @param $request RequestInterface|\Google\Http\Batch * @param string $expectedClass * @throws \Google\Exception - * @return object of the type of the expected class or Psr\Http\Message\ResponseInterface. + * @return mixed|$expectedClass|ResponseInterface */ public function execute(RequestInterface $request, $expectedClass = null) { diff --git a/src/Http/REST.php b/src/Http/REST.php index 691982271..4329e4d1b 100644 --- a/src/Http/REST.php +++ b/src/Http/REST.php @@ -41,7 +41,7 @@ class REST * @param string $expectedClass * @param array $config * @param array $retryMap - * @return array decoded result + * @return mixed decoded result * @throws \Google\Service\Exception on server side error (ie: not authenticated, * invalid or malformed post body, invalid url) */ diff --git a/src/Service/Resource.php b/src/Service/Resource.php index be0014532..09ebaa089 100644 --- a/src/Service/Resource.php +++ b/src/Service/Resource.php @@ -80,7 +80,7 @@ public function __construct($service, $serviceName, $resourceName, $resource) * @param $name * @param $arguments * @param $expectedClass - optional, the expected class name - * @return Request|$expectedClass + * @return mixed|$expectedClass|ResponseInterface|RequestInterface * @throws \Google\Exception */ public function call($name, $arguments, $expectedClass = null) From 0285bc1a8f12f3aa22e9de65f59c357d00973ff4 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 20 Sep 2021 17:13:36 +0200 Subject: [PATCH 200/301] chore: improve test suite (#2115) --- .github/apply-phpunit-patches.sh | 17 --------- .github/workflows/tests.yml | 16 ++++++-- .gitignore | 1 + composer.json | 6 ++- phpunit.xml.dist | 2 +- src/Collection.php | 14 ++++++- tests/BaseTest.php | 23 ++++++++++-- tests/Google/Http/BatchTest.php | 6 +-- tests/Google/Http/MediaFileUploadTest.php | 4 +- tests/Google/Http/RESTTest.php | 2 +- tests/Google/Service/AdSenseTest.php | 2 +- tests/Google/Service/ResourceTest.php | 2 +- tests/Google/Service/TasksTest.php | 2 +- tests/Google/Service/YouTubeTest.php | 2 +- tests/Google/ServiceTest.php | 46 ++++++++++++++++------- tests/Google/Task/ComposerTest.php | 12 +++--- tests/Google/Task/RunnerTest.php | 2 +- tests/Google/Utils/UriTemplateTest.php | 7 +++- 18 files changed, 105 insertions(+), 61 deletions(-) delete mode 100644 .github/apply-phpunit-patches.sh diff --git a/.github/apply-phpunit-patches.sh b/.github/apply-phpunit-patches.sh deleted file mode 100644 index 2584fd243..000000000 --- a/.github/apply-phpunit-patches.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/sh - -# Script used from php-webdriver/php-webdriver - -# All commands below must no fail -set -e - -# Be in the root dir -cd "$(dirname "$0")/../" - -find tests/ -type f -print0 | xargs -0 sed -i 's/function setUp(): void/function setUp()/g'; - -# Drop the listener from the config file -sed -i '//,+2d' phpunit.xml.dist; - -# Return back to original dir -cd - > /dev/null diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d758fa9c4..6334cdaaa 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -18,6 +18,10 @@ jobs: composer-flags: "--prefer-lowest " - php: "8.0" composer-flags: "--prefer-lowest " + - php: "8.1" + composer-flags: "--ignore-platform-reqs " + - php: "8.1" + composer-flags: "--ignore-platform-reqs --prefer-lowest " name: PHP ${{ matrix.php }} ${{ matrix.composer-flags }}Unit Test steps: - uses: actions/checkout@v2 @@ -31,13 +35,17 @@ jobs: timeout_minutes: 10 max_attempts: 3 command: composer update ${{ matrix.composer-flags }} - - if: ${{ matrix.php == '8.0' || matrix.composer-flags == '--prefer-lowest' }} + - if: ${{ matrix.php == '8.0' || ( matrix.composer-flags == '--prefer-lowest' && matrix.php != '8.1' ) }} name: Update guzzlehttp/ringphp dependency run: composer update guzzlehttp/ringphp - - if: ${{ matrix.php == '5.6' || matrix.php == '7.0' || matrix.php == '7.1' }} - name: Run PHPUnit Patches - run: sh .github/apply-phpunit-patches.sh + - if: ${{ matrix.php == '8.1' }} + name: Update guzzlehttp/ringphp dependency + run: composer update guzzlehttp/ringphp --ignore-platform-reqs + - if: ${{ matrix.php == '8.1' }} + name: Update phpunit/phpunit dependency + run: composer update phpunit/phpunit phpspec/prophecy-phpunit --with-dependencies --ignore-platform-reqs - name: Run Script + continue-on-error: ${{ matrix.php == '8.1' }} run: vendor/bin/phpunit style: diff --git a/.gitignore b/.gitignore index 812c3a3be..fc8518be0 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ vendor examples/testfile-small.txt examples/testfile.txt tests/.apiKey +.phpunit.result.cache .idea/ diff --git a/composer.json b/composer.json index b9d61ce64..cc20a8b30 100644 --- a/composer.json +++ b/composer.json @@ -16,14 +16,16 @@ "guzzlehttp/psr7": "^1.2" }, "require-dev": { - "phpunit/phpunit": "^5.7||^8.5.13", "squizlabs/php_codesniffer": "~2.3", "symfony/dom-crawler": "~2.1", "symfony/css-selector": "~2.1", "cache/filesystem-adapter": "^0.3.2|^1.1", "phpcompatibility/php-compatibility": "^9.2", "dealerdirect/phpcodesniffer-composer-installer": "^0.7", - "composer/composer": "^1.10.22" + "composer/composer": "^1.10.22", + "yoast/phpunit-polyfills": "^1.0", + "phpspec/prophecy-phpunit": "^1.1||^2.0", + "phpunit/phpunit": "^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0" }, "suggest": { "cache/filesystem-adapter": "For caching certs and tokens (using Google\\Client::setCache)" diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 2acef56c2..0063e91f1 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,6 +1,6 @@ diff --git a/src/Collection.php b/src/Collection.php index 3d61bea80..f3154e67d 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -11,6 +11,7 @@ class Collection extends Model implements \Iterator, \Countable { protected $collection_key = 'items'; + #[ReturnTypeWillChange] public function rewind() { if (isset($this->{$this->collection_key}) @@ -19,6 +20,7 @@ public function rewind() } } + #[ReturnTypeWillChange] public function current() { $this->coerceType($this->key()); @@ -27,6 +29,7 @@ public function current() } } + #[ReturnTypeWillChange] public function key() { if (isset($this->{$this->collection_key}) @@ -35,17 +38,20 @@ public function key() } } + #[ReturnTypeWillChange] public function next() { return next($this->{$this->collection_key}); } + #[ReturnTypeWillChange] public function valid() { $key = $this->key(); return $key !== null && $key !== false; } + #[ReturnTypeWillChange] public function count() { if (!isset($this->{$this->collection_key})) { @@ -54,6 +60,7 @@ public function count() return count($this->{$this->collection_key}); } + #[ReturnTypeWillChange] public function offsetExists($offset) { if (!is_numeric($offset)) { @@ -62,6 +69,7 @@ public function offsetExists($offset) return isset($this->{$this->collection_key}[$offset]); } + #[ReturnTypeWillChange] public function offsetGet($offset) { if (!is_numeric($offset)) { @@ -71,18 +79,20 @@ public function offsetGet($offset) return $this->{$this->collection_key}[$offset]; } + #[ReturnTypeWillChange] public function offsetSet($offset, $value) { if (!is_numeric($offset)) { - return parent::offsetSet($offset, $value); + parent::offsetSet($offset, $value); } $this->{$this->collection_key}[$offset] = $value; } + #[ReturnTypeWillChange] public function offsetUnset($offset) { if (!is_numeric($offset)) { - return parent::offsetUnset($offset); + parent::offsetUnset($offset); } unset($this->{$this->collection_key}[$offset]); } diff --git a/tests/BaseTest.php b/tests/BaseTest.php index c7377395f..0eaa34aba 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -24,13 +24,26 @@ use League\Flysystem\Adapter\Local; use League\Flysystem\Filesystem; use Cache\Adapter\Filesystem\FilesystemCachePool; -use PHPUnit\Framework\TestCase; +use Yoast\PHPUnitPolyfills\TestCases\TestCase; + +if (trait_exists('\Prophecy\PhpUnit\ProphecyTrait')) { + trait BaseTestTrait + { + use \Prophecy\PhpUnit\ProphecyTrait; + } +} else { + trait BaseTestTrait + { + } +} class BaseTest extends TestCase { private $key; private $client; + use BaseTestTrait; + public function getClient() { if (!$this->client) { @@ -71,12 +84,14 @@ private function createClient() $client = new Client(); $client->setApplicationName('google-api-php-client-tests'); $client->setHttpClient($httpClient); - $client->setScopes([ + $client->setScopes( + [ "https://www.googleapis.com/auth/tasks", "https://www.googleapis.com/auth/adsense", "https://www.googleapis.com/auth/youtube", "https://www.googleapis.com/auth/drive", - ]); + ] + ); if ($this->key) { $client->setDeveloperKey($this->key); @@ -178,7 +193,7 @@ protected function checkKey() $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" + "Test requires api key\nYou can create one in your developer console" ); file_put_contents($apiKeyFile, $apiKey); } diff --git a/tests/Google/Http/BatchTest.php b/tests/Google/Http/BatchTest.php index 021cad2c1..14cc9c2d9 100644 --- a/tests/Google/Http/BatchTest.php +++ b/tests/Google/Http/BatchTest.php @@ -86,8 +86,8 @@ public function testMediaFileBatch() /** @var \GuzzleHttp\Psr7\Request $request */ $request = $storage->objects->insert($bucket, $obj, $params); - $this->assertContains('multipart/related', $request->getHeaderLine('content-type')); - $this->assertContains('/upload/', $request->getUri()->getPath()); - $this->assertContains('uploadType=multipart', $request->getUri()->getQuery()); + $this->assertStringContainsString('multipart/related', $request->getHeaderLine('content-type')); + $this->assertStringContainsString('/upload/', $request->getUri()->getPath()); + $this->assertStringContainsString('uploadType=multipart', $request->getUri()->getQuery()); } } diff --git a/tests/Google/Http/MediaFileUploadTest.php b/tests/Google/Http/MediaFileUploadTest.php index f04f6ea12..16c8777c0 100644 --- a/tests/Google/Http/MediaFileUploadTest.php +++ b/tests/Google/Http/MediaFileUploadTest.php @@ -88,8 +88,8 @@ public function testProcess() $request = $request->withBody(Psr7\stream_for($reqData)); $media = new MediaFileUpload($client, $request, 'image/png', $data, false); $request = $media->getRequest(); - $this->assertContains($reqData, (string) $request->getBody()); - $this->assertContains(base64_encode($data), (string) $request->getBody()); + $this->assertStringContainsString($reqData, (string) $request->getBody()); + $this->assertStringContainsString(base64_encode($data), (string) $request->getBody()); } public function testGetResumeUri() diff --git a/tests/Google/Http/RESTTest.php b/tests/Google/Http/RESTTest.php index 6df7d3bc7..bf10bf936 100644 --- a/tests/Google/Http/RESTTest.php +++ b/tests/Google/Http/RESTTest.php @@ -32,7 +32,7 @@ class RESTTest extends BaseTest */ private $rest; - public function setUp(): void + public function set_up() { $this->rest = new REST(); $this->request = new Request('GET', '/'); diff --git a/tests/Google/Service/AdSenseTest.php b/tests/Google/Service/AdSenseTest.php index 5fa69830f..658c46a3c 100644 --- a/tests/Google/Service/AdSenseTest.php +++ b/tests/Google/Service/AdSenseTest.php @@ -23,7 +23,7 @@ class AdSenseTest extends BaseTest { public $adsense; - public function setUp(): void + public function set_up() { $this->markTestSkipped('Thesse tests need to be fixed'); $this->checkToken(); diff --git a/tests/Google/Service/ResourceTest.php b/tests/Google/Service/ResourceTest.php index ca2b58f9c..4ee768f6b 100644 --- a/tests/Google/Service/ResourceTest.php +++ b/tests/Google/Service/ResourceTest.php @@ -64,7 +64,7 @@ class ResourceTest extends BaseTest private $client; private $service; - public function setUp(): void + public function set_up() { $this->client = $this->prophesize(Client::class); diff --git a/tests/Google/Service/TasksTest.php b/tests/Google/Service/TasksTest.php index 862509418..d34eb55f1 100644 --- a/tests/Google/Service/TasksTest.php +++ b/tests/Google/Service/TasksTest.php @@ -25,7 +25,7 @@ class TasksTest extends BaseTest /** @var Tasks */ public $taskService; - public function setUp(): void + public function set_up() { $this->checkToken(); $this->taskService = new Tasks($this->getClient()); diff --git a/tests/Google/Service/YouTubeTest.php b/tests/Google/Service/YouTubeTest.php index d40fd0a4c..89d5520df 100644 --- a/tests/Google/Service/YouTubeTest.php +++ b/tests/Google/Service/YouTubeTest.php @@ -24,7 +24,7 @@ class YouTubeTest extends BaseTest { /** @var YouTube */ public $youtube; - public function setUp(): void + public function set_up() { $this->checkToken(); $this->youtube = new YouTube($this->getClient()); diff --git a/tests/Google/ServiceTest.php b/tests/Google/ServiceTest.php index cf4bd20c0..28a51325c 100644 --- a/tests/Google/ServiceTest.php +++ b/tests/Google/ServiceTest.php @@ -24,7 +24,7 @@ use Google\Model; use Google\Service; use Google\Http\Batch; -use PHPUnit\Framework\TestCase; +use Yoast\PHPUnitPolyfills\TestCases\TestCase; use Prophecy\Argument; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -47,22 +47,40 @@ class TestService extends Service public $batchPath = 'batch/test'; } +if (trait_exists('\Prophecy\PhpUnit\ProphecyTrait')) { + trait ServiceTestTrait + { + use \Prophecy\PhpUnit\ProphecyTrait; + } +} else { + trait ServiceTestTrait + { + } +} + class ServiceTest extends TestCase { private static $errorMessage; + use ServiceTestTrait; + public function testCreateBatch() { $response = $this->prophesize(ResponseInterface::class); $client = $this->prophesize(Client::class); - $client->execute(Argument::allOf( - Argument::type(RequestInterface::class), - Argument::that(function ($request) { - $this->assertEquals('/batch/test', $request->getRequestTarget()); - return $request; - }) - ), Argument::any())->willReturn($response->reveal()); + $client->execute( + Argument::allOf( + Argument::type(RequestInterface::class), + Argument::that( + function ($request) { + $this->assertEquals('/batch/test', $request->getRequestTarget()); + return $request; + } + ) + ), + Argument::any() + )->willReturn($response->reveal()); $client->getConfig('base_path')->willReturn(''); @@ -131,12 +149,14 @@ public function testInvalidConstructorPhp7Plus() try { $service = new TestService('foo'); - } catch (\TypeError $e) {} + } catch (\TypeError $e) { + + } $this->assertInstanceOf('TypeError', $e); $this->assertEquals( - 'constructor must be array or instance of Google\Client', - $e->getMessage() + 'constructor must be array or instance of Google\Client', + $e->getMessage() ); } @@ -152,8 +172,8 @@ public function testInvalidConstructorPhp5() $service = new TestService('foo'); $this->assertEquals( - 'constructor must be array or instance of Google\Client', - self::$errorMessage + 'constructor must be array or instance of Google\Client', + self::$errorMessage ); } diff --git a/tests/Google/Task/ComposerTest.php b/tests/Google/Task/ComposerTest.php index f4ff96a02..2976eabd8 100644 --- a/tests/Google/Task/ComposerTest.php +++ b/tests/Google/Task/ComposerTest.php @@ -183,8 +183,8 @@ public function testE2E() $this->assertFileExists($serviceDir . '/Drive'); $this->assertFileExists($serviceDir . '/YouTube.php'); $this->assertFileExists($serviceDir . '/YouTube'); - $this->assertFileNotExists($serviceDir . '/YouTubeReporting.php'); - $this->assertFileNotExists($serviceDir . '/YouTubeReporting'); + $this->assertFileDoesNotExist($serviceDir . '/YouTubeReporting.php'); + $this->assertFileDoesNotExist($serviceDir . '/YouTubeReporting'); // Remove the "apiclient-services" directory, which is required to // update the cleanup command. @@ -225,10 +225,10 @@ public function testE2EBCTaskName() $this->assertFileExists($serviceDir . '/Drive.php'); $this->assertFileExists($serviceDir . '/Drive'); - $this->assertFileNotExists($serviceDir . '/YouTube.php'); - $this->assertFileNotExists($serviceDir . '/YouTube'); - $this->assertFileNotExists($serviceDir . '/YouTubeReporting.php'); - $this->assertFileNotExists($serviceDir . '/YouTubeReporting'); + $this->assertFileDoesNotExist($serviceDir . '/YouTube.php'); + $this->assertFileDoesNotExist($serviceDir . '/YouTube'); + $this->assertFileDoesNotExist($serviceDir . '/YouTubeReporting.php'); + $this->assertFileDoesNotExist($serviceDir . '/YouTubeReporting'); } public function testE2EOptimized() diff --git a/tests/Google/Task/RunnerTest.php b/tests/Google/Task/RunnerTest.php index c138484c2..b11a3e367 100644 --- a/tests/Google/Task/RunnerTest.php +++ b/tests/Google/Task/RunnerTest.php @@ -42,7 +42,7 @@ class RunnerTest extends BaseTest private $retryMap; private $retryConfig; - protected function setUp(): void + protected function set_up() { $this->client = new Client(); } diff --git a/tests/Google/Utils/UriTemplateTest.php b/tests/Google/Utils/UriTemplateTest.php index 8a98ea8c9..09ecb6b0b 100644 --- a/tests/Google/Utils/UriTemplateTest.php +++ b/tests/Google/Utils/UriTemplateTest.php @@ -270,9 +270,14 @@ public function testMultipleAnnotations() public function testAgainstStandardTests() { $location = __DIR__ . "/../../uritemplate-test/*.json"; + $files = glob($location); + + if (!$files) { + $this->markTestSkipped('No JSON files provided'); + } $urit = new UriTemplate(); - foreach (glob($location) as $file) { + foreach ($files as $file) { $test = json_decode(file_get_contents($file), true); foreach ($test as $title => $testsets) { foreach ($testsets['testcases'] as $cases) { From be74b9aa8857021b0094bed1e491f42bf09d6fbb Mon Sep 17 00:00:00 2001 From: n00dl3 Date: Mon, 20 Sep 2021 17:29:19 +0200 Subject: [PATCH 201/301] feat: allow guzzlehttp/psr7 v2 (#2128) --- composer.json | 2 +- src/AccessToken/Revoke.php | 2 +- src/Http/Batch.php | 2 +- src/Http/MediaFileUpload.php | 4 +-- tests/Google/Http/BatchTest.php | 2 +- tests/Google/Http/MediaFileUploadTest.php | 4 +-- tests/Google/Http/RESTTest.php | 12 ++++----- tests/Google/Service/ResourceTest.php | 30 +++++++---------------- tests/Google/Task/RunnerTest.php | 2 +- 9 files changed, 24 insertions(+), 36 deletions(-) diff --git a/composer.json b/composer.json index cc20a8b30..ef4ac07a9 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "monolog/monolog": "^1.17|^2.0", "phpseclib/phpseclib": "~2.0||^3.0.2", "guzzlehttp/guzzle": "~5.3.3||~6.0||~7.0", - "guzzlehttp/psr7": "^1.2" + "guzzlehttp/psr7": "^1.7||^2.0.0" }, "require-dev": { "squizlabs/php_codesniffer": "~2.3", diff --git a/src/AccessToken/Revoke.php b/src/AccessToken/Revoke.php index 45b60548e..d86cc6e32 100644 --- a/src/AccessToken/Revoke.php +++ b/src/AccessToken/Revoke.php @@ -61,7 +61,7 @@ public function revokeToken($token) } } - $body = Psr7\stream_for(http_build_query(array('token' => $token))); + $body = Psr7\Utils::streamFor(http_build_query(array('token' => $token))); $request = new Request( 'POST', Client::OAUTH2_REVOKE_URI, diff --git a/src/Http/Batch.php b/src/Http/Batch.php index a4607586e..a657bacd4 100644 --- a/src/Http/Batch.php +++ b/src/Http/Batch.php @@ -174,7 +174,7 @@ public function parseResponse(ResponseInterface $response, $classes = array()) $response = new Response( $status, $partHeaders, - Psr7\stream_for($partBody) + Psr7\Utils::streamFor($partBody) ); // Need content id. diff --git a/src/Http/MediaFileUpload.php b/src/Http/MediaFileUpload.php index 82a051abf..15529bca7 100644 --- a/src/Http/MediaFileUpload.php +++ b/src/Http/MediaFileUpload.php @@ -141,7 +141,7 @@ public function nextChunk($chunk = false) 'PUT', $resumeUri, $headers, - Psr7\stream_for($chunk) + Psr7\Utils::streamFor($chunk) ); return $this->makePutRequest($request); @@ -255,7 +255,7 @@ private function process() $postBody = $related; } - $request = $request->withBody(Psr7\stream_for($postBody)); + $request = $request->withBody(Psr7\Utils::streamFor($postBody)); if (isset($contentType) && $contentType) { $request = $request->withHeader('content-type', $contentType); diff --git a/tests/Google/Http/BatchTest.php b/tests/Google/Http/BatchTest.php index 14cc9c2d9..49046d4c1 100644 --- a/tests/Google/Http/BatchTest.php +++ b/tests/Google/Http/BatchTest.php @@ -70,7 +70,7 @@ public function testMediaFileBatch() $client = $this->getClient(); $storage = new Storage($client); $bucket = 'testbucket'; - $stream = Psr7\stream_for("testbucket-text"); + $stream = Psr7\Utils::streamFor("testbucket-text"); $params = [ 'data' => $stream, 'mimeType' => 'text/plain', diff --git a/tests/Google/Http/MediaFileUploadTest.php b/tests/Google/Http/MediaFileUploadTest.php index 16c8777c0..d8d3a2a08 100644 --- a/tests/Google/Http/MediaFileUploadTest.php +++ b/tests/Google/Http/MediaFileUploadTest.php @@ -77,7 +77,7 @@ public function testProcess() // Test resumable (meta data) - we want to send the metadata, not the app data. $request = new Request('POST', 'http://www.example.com/'); $reqData = json_encode("hello"); - $request = $request->withBody(Psr7\stream_for($reqData)); + $request = $request->withBody(Psr7\Utils::streamFor($reqData)); $media = new MediaFileUpload($client, $request, 'image/png', $data, true); $request = $media->getRequest(); $this->assertEquals(json_decode($reqData), (string) $request->getBody()); @@ -85,7 +85,7 @@ public function testProcess() // Test multipart - we are sending encoded meta data and post data $request = new Request('POST', 'http://www.example.com/'); $reqData = json_encode("hello"); - $request = $request->withBody(Psr7\stream_for($reqData)); + $request = $request->withBody(Psr7\Utils::streamFor($reqData)); $media = new MediaFileUpload($client, $request, 'image/png', $data, false); $request = $media->getRequest(); $this->assertStringContainsString($reqData, (string) $request->getBody()); diff --git a/tests/Google/Http/RESTTest.php b/tests/Google/Http/RESTTest.php index bf10bf936..fb15ac238 100644 --- a/tests/Google/Http/RESTTest.php +++ b/tests/Google/Http/RESTTest.php @@ -47,7 +47,7 @@ public function testDecodeResponse() foreach (array(200, 201) as $code) { $headers = array('foo', 'bar'); - $stream = Psr7\stream_for('{"a": 1}'); + $stream = Psr7\Utils::streamFor('{"a": 1}'); $response = new Response($code, $headers, $stream); $decoded = $this->rest->decodeHttpResponse($response, $this->request); @@ -61,7 +61,7 @@ public function testDecodeMediaResponse() $request = new Request('GET', 'http://www.example.com/?alt=media'); $headers = array(); - $stream = Psr7\stream_for('thisisnotvalidjson'); + $stream = Psr7\Utils::streamFor('thisisnotvalidjson'); $response = new Response(200, $headers, $stream); $decoded = $this->rest->decodeHttpResponse($response, $request); @@ -87,7 +87,7 @@ public function testExceptionResponse() public function testDecodeEmptyResponse() { - $stream = Psr7\stream_for('{}'); + $stream = Psr7\Utils::streamFor('{}'); $response = new Response(200, array(), $stream); $decoded = $this->rest->decodeHttpResponse($response, $this->request); $this->assertEquals('{}', (string) $decoded->getBody()); @@ -96,7 +96,7 @@ public function testDecodeEmptyResponse() public function testBadErrorFormatting() { $this->expectException(ServiceException::class); - $stream = Psr7\stream_for( + $stream = Psr7\Utils::streamFor( '{ "error": { "code": 500, @@ -111,7 +111,7 @@ public function testBadErrorFormatting() public function tesProperErrorFormatting() { $this->expectException(ServiceException::class); - $stream = Psr7\stream_for( + $stream = Psr7\Utils::streamFor( '{ error: { errors: [ @@ -134,7 +134,7 @@ public function tesProperErrorFormatting() public function testNotJson404Error() { $this->expectException(ServiceException::class); - $stream = Psr7\stream_for('Not Found'); + $stream = Psr7\Utils::streamFor('Not Found'); $response = new Response(404, array(), $stream); $this->rest->decodeHttpResponse($response, $this->request); } diff --git a/tests/Google/Service/ResourceTest.php b/tests/Google/Service/ResourceTest.php index 4ee768f6b..312db72a3 100644 --- a/tests/Google/Service/ResourceTest.php +++ b/tests/Google/Service/ResourceTest.php @@ -47,18 +47,6 @@ public function __construct(Client $client) } } -class TestMediaTypeStream extends Stream -{ - public $toStringCalled = false; - - public function __toString() - { - $this->toStringCalled = true; - - return parent::__toString(); - } -} - class ResourceTest extends BaseTest { private $client; @@ -231,7 +219,7 @@ public function testNoExpectedClassForAltMediaWithHttpSuccess() ->shouldBeCalledTimes(1) ->willReturn($response); } else { - $body = Psr7\stream_for('thisisnotvalidjson'); + $body = Psr7\Utils::streamFor('thisisnotvalidjson'); $response = new Response(200, [], $body); $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) @@ -284,7 +272,7 @@ public function testNoExpectedClassForAltMediaWithHttpFail() ->shouldBeCalledTimes(1) ->willReturn($response); } else { - $body = Psr7\stream_for('thisisnotvalidjson'); + $body = Psr7\Utils::streamFor('thisisnotvalidjson'); $response = new Response(400, [], $body); $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) @@ -341,7 +329,7 @@ public function testErrorResponseWithVeryLongBody() ->shouldBeCalledTimes(1) ->willReturn($response); } else { - $body = Psr7\stream_for('this will be pulled into memory'); + $body = Psr7\Utils::streamFor('this will be pulled into memory'); $response = new Response(400, [], $body); $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) @@ -385,10 +373,10 @@ public function testSuccessResponseWithVeryLongBody() // set the "alt" parameter to "media" $arguments = [['alt' => 'media']]; - $request = new Request('GET', '/?alt=media'); - $resource = fopen('php://temp', 'r+'); - $stream = new TestMediaTypeStream($resource); - $response = new Response(200, [], $stream); + $stream = $this->prophesize(Stream::class); + $stream->__toString() + ->shouldNotBeCalled(); + $response = new Response(200, [], $stream->reveal()); $http = $this->prophesize("GuzzleHttp\Client"); $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) @@ -419,7 +407,7 @@ public function testSuccessResponseWithVeryLongBody() $response = $resource->call('testMethod', $arguments, $expectedClass); $this->assertEquals(200, $response->getStatusCode()); - $this->assertFalse($stream->toStringCalled); + // $this->assertFalse($stream->toStringCalled); } public function testExceptionMessage() @@ -446,7 +434,7 @@ public function testExceptionMessage() ->shouldBeCalledTimes(1) ->willReturn($response); } else { - $body = Psr7\stream_for($content); + $body = Psr7\Utils::streamFor($content); $response = new Response(400, [], $body); $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) diff --git a/tests/Google/Task/RunnerTest.php b/tests/Google/Task/RunnerTest.php index b11a3e367..85354e4ba 100644 --- a/tests/Google/Task/RunnerTest.php +++ b/tests/Google/Task/RunnerTest.php @@ -684,7 +684,7 @@ public function getNextMockedCall($request) $stream = Guzzle5Stream::factory($current['body']); $response = new Guzzle5Response($current['code'], $current['headers'], $stream); } else { - $stream = Psr7\stream_for($current['body']); + $stream = Psr7\Utils::streamFor($current['body']); $response = new Response($current['code'], $current['headers'], $stream); } From 3cbf59fc96237d6098d5886e820dd25ed9a9bca4 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 20 Sep 2021 14:39:28 -0600 Subject: [PATCH 202/301] chore: fix php 8.1 warnings and tests (#2131) --- .github/workflows/tests.yml | 7 ++----- src/Collection.php | 16 ++++++---------- src/Model.php | 4 ++++ 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 6334cdaaa..398d5dec8 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -11,7 +11,7 @@ jobs: strategy: fail-fast: false matrix: - php: [ "5.6", "7.0", "7.1", "7.2", "7.3", "7.4", "8.0" ] + php: [ "5.6", "7.0", "7.1", "7.2", "7.3", "7.4", "8.0", "8.1" ] composer-flags: [""] include: - php: "5.6" @@ -19,9 +19,7 @@ jobs: - php: "8.0" composer-flags: "--prefer-lowest " - php: "8.1" - composer-flags: "--ignore-platform-reqs " - - php: "8.1" - composer-flags: "--ignore-platform-reqs --prefer-lowest " + composer-flags: "--prefer-lowest " name: PHP ${{ matrix.php }} ${{ matrix.composer-flags }}Unit Test steps: - uses: actions/checkout@v2 @@ -45,7 +43,6 @@ jobs: name: Update phpunit/phpunit dependency run: composer update phpunit/phpunit phpspec/prophecy-phpunit --with-dependencies --ignore-platform-reqs - name: Run Script - continue-on-error: ${{ matrix.php == '8.1' }} run: vendor/bin/phpunit style: diff --git a/src/Collection.php b/src/Collection.php index f3154e67d..0417dbc9a 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -11,7 +11,7 @@ class Collection extends Model implements \Iterator, \Countable { protected $collection_key = 'items'; - #[ReturnTypeWillChange] + #[\ReturnTypeWillChange] public function rewind() { if (isset($this->{$this->collection_key}) @@ -20,7 +20,7 @@ public function rewind() } } - #[ReturnTypeWillChange] + #[\ReturnTypeWillChange] public function current() { $this->coerceType($this->key()); @@ -29,7 +29,7 @@ public function current() } } - #[ReturnTypeWillChange] + #[\ReturnTypeWillChange] public function key() { if (isset($this->{$this->collection_key}) @@ -38,20 +38,20 @@ public function key() } } - #[ReturnTypeWillChange] + #[\ReturnTypeWillChange] public function next() { return next($this->{$this->collection_key}); } - #[ReturnTypeWillChange] + #[\ReturnTypeWillChange] public function valid() { $key = $this->key(); return $key !== null && $key !== false; } - #[ReturnTypeWillChange] + #[\ReturnTypeWillChange] public function count() { if (!isset($this->{$this->collection_key})) { @@ -60,7 +60,6 @@ public function count() return count($this->{$this->collection_key}); } - #[ReturnTypeWillChange] public function offsetExists($offset) { if (!is_numeric($offset)) { @@ -69,7 +68,6 @@ public function offsetExists($offset) return isset($this->{$this->collection_key}[$offset]); } - #[ReturnTypeWillChange] public function offsetGet($offset) { if (!is_numeric($offset)) { @@ -79,7 +77,6 @@ public function offsetGet($offset) return $this->{$this->collection_key}[$offset]; } - #[ReturnTypeWillChange] public function offsetSet($offset, $value) { if (!is_numeric($offset)) { @@ -88,7 +85,6 @@ public function offsetSet($offset, $value) $this->{$this->collection_key}[$offset] = $value; } - #[ReturnTypeWillChange] public function offsetUnset($offset) { if (!is_numeric($offset)) { diff --git a/src/Model.php b/src/Model.php index 18f8917b0..25ea40381 100644 --- a/src/Model.php +++ b/src/Model.php @@ -253,11 +253,13 @@ public function assertIsArray($obj, $method) } } + #[\ReturnTypeWillChange] public function offsetExists($offset) { return isset($this->$offset) || isset($this->modelData[$offset]); } + #[\ReturnTypeWillChange] public function offsetGet($offset) { return isset($this->$offset) ? @@ -265,6 +267,7 @@ public function offsetGet($offset) $this->__get($offset); } + #[\ReturnTypeWillChange] public function offsetSet($offset, $value) { if (property_exists($this, $offset)) { @@ -275,6 +278,7 @@ public function offsetSet($offset, $value) } } + #[\ReturnTypeWillChange] public function offsetUnset($offset) { unset($this->modelData[$offset]); From b757168e435752ab8172e20087e0db971ed8df4c Mon Sep 17 00:00:00 2001 From: Mohammad Sadegh Maleki Date: Tue, 21 Sep 2021 00:46:15 +0400 Subject: [PATCH 203/301] chore: consistent monolog/monolog requirement (#2111) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index ef4ac07a9..046090389 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ "google/auth": "^1.10", "google/apiclient-services": "~0.200", "firebase/php-jwt": "~2.0||~3.0||~4.0||~5.0", - "monolog/monolog": "^1.17|^2.0", + "monolog/monolog": "^1.17||^2.0", "phpseclib/phpseclib": "~2.0||^3.0.2", "guzzlehttp/guzzle": "~5.3.3||~6.0||~7.0", "guzzlehttp/psr7": "^1.7||^2.0.0" From 7db9eb40c8ba887e81c0fe84f2888a967396cdfb Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 20 Sep 2021 15:15:55 -0600 Subject: [PATCH 204/301] chore: prepare v2.11.0 (#2132) --- README.md | 4 ++-- src/Client.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 235e05f2b..97dec0ddd 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ composer installed. Once composer is installed, execute the following command in your project root to install this library: ```sh -composer require google/apiclient:^2.10 +composer require google/apiclient:^2.11 ``` Finally, be sure to include the autoloader: @@ -61,7 +61,7 @@ you want to keep in `composer.json`: ```json { "require": { - "google/apiclient": "^2.10" + "google/apiclient": "^2.11" }, "scripts": { "pre-autoload-dump": "Google\\Task\\Composer::cleanup" diff --git a/src/Client.php b/src/Client.php index 7924ece36..c0e3ebcad 100644 --- a/src/Client.php +++ b/src/Client.php @@ -49,7 +49,7 @@ */ class Client { - const LIBVER = "2.10.1"; + const LIBVER = "2.11.0"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; From c9995b71e2f43f0430e353470973a1541907745c Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 30 Nov 2021 15:41:24 -0500 Subject: [PATCH 205/301] feat: allow credentials object (#2153) --- src/Client.php | 57 +++++++++++++++++++++++++++---------- tests/Google/ClientTest.php | 50 ++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 15 deletions(-) diff --git a/src/Client.php b/src/Client.php index c0e3ebcad..8aa6a52dd 100644 --- a/src/Client.php +++ b/src/Client.php @@ -86,6 +86,11 @@ class Client */ private $logger; + /** + * @var CredentialsLoader $credentials + */ + private $credentials; + /** * @var boolean $deferExecution */ @@ -114,8 +119,9 @@ public function __construct(array $config = array()) 'client_id' => '', 'client_secret' => '', - // Path to JSON credentials or an array representing those credentials - // @see Google\Client::setAuthConfig + // Can be a path to JSON credentials or an array representing those + // credentials (@see Google\Client::setAuthConfig), or an instance of + // Google\Auth\CredentialsLoader. 'credentials' => null, // @see Google\Client::setScopes 'scopes' => null, @@ -175,7 +181,11 @@ public function __construct(array $config = array()) ); if (!is_null($this->config['credentials'])) { - $this->setAuthConfig($this->config['credentials']); + if ($this->config['credentials'] instanceof CredentialsLoader) { + $this->credentials = $this->config['credentials']; + } else { + $this->setAuthConfig($this->config['credentials']); + } unset($this->config['credentials']); } @@ -405,25 +415,33 @@ public function createAuthUrl($scope = null) */ public function authorize(ClientInterface $http = null) { - $credentials = null; - $token = null; - $scopes = null; $http = $http ?: $this->getHttpClient(); $authHandler = $this->getAuthHandler(); // These conditionals represent the decision tree for authentication - // 1. Check for Application Default Credentials - // 2. Check for API Key + // 1. Check if a Google\Auth\CredentialsLoader instance has been supplied via the "credentials" option + // 2. Check for Application Default Credentials // 3a. Check for an Access Token // 3b. If access token exists but is expired, try to refresh it + // 4. Check for API Key + if ($this->credentials) { + return $authHandler->attachCredentials( + $http, + $this->credentials, + $this->config['token_callback'] + ); + } + if ($this->isUsingApplicationDefaultCredentials()) { $credentials = $this->createApplicationDefaultCredentials(); - $http = $authHandler->attachCredentialsCache( + return $authHandler->attachCredentialsCache( $http, $credentials, $this->config['token_callback'] ); - } elseif ($token = $this->getAccessToken()) { + } + + if ($token = $this->getAccessToken()) { $scopes = $this->prepareScopes(); // add refresh subscriber to request a new token if (isset($token['refresh_token']) && $this->isAccessTokenExpired()) { @@ -431,16 +449,18 @@ public function authorize(ClientInterface $http = null) $scopes, $token['refresh_token'] ); - $http = $authHandler->attachCredentials( + return $authHandler->attachCredentials( $http, $credentials, $this->config['token_callback'] ); - } else { - $http = $authHandler->attachToken($http, $token, (array) $scopes); } - } elseif ($key = $this->config['developer_key']) { - $http = $authHandler->attachKey($http, $key); + + return $authHandler->attachToken($http, $token, (array) $scopes); + } + + if ($key = $this->config['developer_key']) { + return $authHandler->attachKey($http, $key); } return $http; @@ -1235,6 +1255,13 @@ private function createApplicationDefaultCredentials() $credentials->setSub($sub); } + if ($credentials instanceof ServiceAccountCredentials && $this->isUsingJwtWithScope()) { + // tell the credentials to sign scopes into Self-Signed JWTs instead of + // calling the OAuth2 token endpoint + // @see https://google.aip.dev/auth/4111#scope-vs-audience + $credentials->useJwtAccessWithScope(); + } + // If we are not using FetchAuthTokenCache yet, create it now if (!$credentials instanceof FetchAuthTokenCache) { $credentials = new FetchAuthTokenCache( diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index a7b6bd9f4..1286e7436 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -952,4 +952,54 @@ public function testClientOptions() $credentials = $method->invoke($client); $this->assertEquals('some-quota-project', $credentials->getQuotaProject()); } + + public function testCredentialsOptionWithCredentialsLoader() + { + $this->onlyGuzzle6Or7(); + + $request = null; + $credentials = $this->prophesize('Google\Auth\CredentialsLoader'); + $credentials->getCacheKey() + ->willReturn('cache-key'); + + // Ensure the access token provided by our credentials loader is used + $credentials->fetchAuthToken(Argument::any()) + ->shouldBeCalledOnce() + ->willReturn(['access_token' => 'abc']); + + $client = new Client(['credentials' => $credentials->reveal()]); + + $handler = $this->prophesize('GuzzleHttp\HandlerStack'); + $handler->remove('google_auth') + ->shouldBeCalledOnce(); + $handler->push(Argument::any(), 'google_auth') + ->shouldBeCalledOnce() + ->will(function($args) use (&$request) { + $middleware = $args[0]; + $callable = $middleware(function ($req, $res) use (&$request) { + $request = $req; // test later + }); + $callable(new Request('GET', '/fake-uri'), ['auth' => 'google_auth']); + }); + + $httpClient = $this->prophesize('GuzzleHttp\ClientInterface'); + $httpClient->getConfig() + ->shouldBeCalledOnce() + ->willReturn(['handler' => $handler->reveal()]); + $httpClient->getConfig('base_uri') + ->shouldBeCalledOnce(); + $httpClient->getConfig('verify') + ->shouldBeCalledOnce(); + $httpClient->getConfig('proxy') + ->shouldBeCalledOnce(); + $httpClient->send(Argument::any(), Argument::any()) + ->shouldNotBeCalled(); + + $http = $client->authorize($httpClient->reveal()); + + $this->assertNotNull($request); + $authHeader = $request->getHeaderLine('authorization'); + $this->assertNotNull($authHeader); + $this->assertEquals('Bearer abc', $authHeader); + } } From 5205105b9973766455d0eb83445f68c9efda16d1 Mon Sep 17 00:00:00 2001 From: Ilhan Yumer Date: Tue, 30 Nov 2021 22:57:20 +0200 Subject: [PATCH 206/301] docs: README code fence highlighting (#2150) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 97dec0ddd..0f9e0cde0 100644 --- a/README.md +++ b/README.md @@ -446,7 +446,7 @@ The _Google\Service_ classes are generally automatically generated from the API Some services return XML or similar by default, rather than JSON, which is what the library supports. You can request a JSON response by adding an 'alt' argument to optional params that is normally the last argument to a method call: -``` +```php $opt_params = array( 'alt' => "json" ); From f235719d369805d99114677a82cff320ecc7c322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edson=20Hor=C3=A1cio=20Junior?= Date: Tue, 30 Nov 2021 19:18:32 -0300 Subject: [PATCH 207/301] docs: add paragraph regarding null properties in the response (#2130) --- docs/start.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/start.md b/docs/start.md index fd3c53f72..9c97991b7 100644 --- a/docs/start.md +++ b/docs/start.md @@ -86,6 +86,8 @@ foreach ($results as $item) { } ``` +**Properties are hydrated according to the value in the Response. If a property is not present in the Response it will be set to null. Some fields won't be in the Response if you didn't ask for them in the Request using the `fields` property. Therefore, watchout for null properties, maybe they have a value already but are null because they're not present in the Response.** + ## Google App Engine support -This library works well with Google App Engine applications. The Memcache class is automatically used for caching, and the file IO is implemented with the use of the Streams API. \ No newline at end of file +This library works well with Google App Engine applications. The Memcache class is automatically used for caching, and the file IO is implemented with the use of the Streams API. From c72e04523bd60f460c1db0a951f7f72454c58425 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 1 Dec 2021 12:55:09 -0500 Subject: [PATCH 208/301] chore: prepare v2.12 (#2158) --- README.md | 4 ++-- src/Client.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0f9e0cde0..698775ebc 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ composer installed. Once composer is installed, execute the following command in your project root to install this library: ```sh -composer require google/apiclient:^2.11 +composer require google/apiclient:^2.12 ``` Finally, be sure to include the autoloader: @@ -61,7 +61,7 @@ you want to keep in `composer.json`: ```json { "require": { - "google/apiclient": "^2.11" + "google/apiclient": "^2.12" }, "scripts": { "pre-autoload-dump": "Google\\Task\\Composer::cleanup" diff --git a/src/Client.php b/src/Client.php index 8aa6a52dd..873671c7a 100644 --- a/src/Client.php +++ b/src/Client.php @@ -49,7 +49,7 @@ */ class Client { - const LIBVER = "2.11.0"; + const LIBVER = "2.12.0"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; From 74a218e15aa16e2ddbc95cb9a3b0e36dfa478feb Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 1 Dec 2021 13:03:17 -0500 Subject: [PATCH 209/301] chore: switch to main (#2159) --- .github/workflows/docs.yml | 2 +- README.md | 2 +- composer.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 63244a80a..abf518785 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -2,7 +2,7 @@ name: Generate Documentation on: push: branches: - - master + - main tags: - "*" diff --git a/README.md b/README.md index 698775ebc..b7f9dae8a 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ # Google APIs Client Library for PHP #
      -
      Reference Docs
      https://googleapis.github.io/google-api-php-client/master/
      +
      Reference Docs
      https://googleapis.github.io/google-api-php-client/main/
      License
      Apache 2.0
      diff --git a/composer.json b/composer.json index 046090389..8b712c5bd 100644 --- a/composer.json +++ b/composer.json @@ -43,7 +43,7 @@ }, "extra": { "branch-alias": { - "dev-master": "2.x-dev" + "dev-main": "2.x-dev" } } } From 8865cab2efbd63b40f88552886865f31bd7f3edd Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 1 Dec 2021 13:14:35 -0500 Subject: [PATCH 210/301] chore: fix ubuntu version for docs generation (#2160) --- .github/workflows/docs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index abf518785..09199fd23 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -9,7 +9,7 @@ on: jobs: docs: name: "Generate Project Documentation" - runs-on: ubuntu-16.04 + runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v2 From 392c5c740811d707dc2563c11055d89c5dd0523b Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 1 Dec 2021 13:50:02 -0500 Subject: [PATCH 211/301] chore: fix main branch doc generation (#2161) --- .github/actions/docs/sami.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/actions/docs/sami.php b/.github/actions/docs/sami.php index b607837df..b3d3da8f7 100644 --- a/.github/actions/docs/sami.php +++ b/.github/actions/docs/sami.php @@ -18,7 +18,7 @@ ->addFromTags(function($tag) { return 0 === strpos($tag, 'v2.') && false === strpos($tag, 'RC'); }) - ->add('master', 'master branch'); + ->add('main', 'main branch'); return new Sami($iterator, [ 'title' => 'Google APIs Client Library for PHP API Reference', From 1530583a711f4414407112c4068464bcbace1c71 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 1 Dec 2021 22:34:25 -0500 Subject: [PATCH 212/301] fix: remove error block of code (#2163) --- README.md | 4 ++-- src/Client.php | 9 +-------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index b7f9dae8a..b3149928d 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ composer installed. Once composer is installed, execute the following command in your project root to install this library: ```sh -composer require google/apiclient:^2.12 +composer require google/apiclient:^2.12.1 ``` Finally, be sure to include the autoloader: @@ -61,7 +61,7 @@ you want to keep in `composer.json`: ```json { "require": { - "google/apiclient": "^2.12" + "google/apiclient": "^2.12.1" }, "scripts": { "pre-autoload-dump": "Google\\Task\\Composer::cleanup" diff --git a/src/Client.php b/src/Client.php index 873671c7a..70e8e82d6 100644 --- a/src/Client.php +++ b/src/Client.php @@ -49,7 +49,7 @@ */ class Client { - const LIBVER = "2.12.0"; + const LIBVER = "2.12.1"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; @@ -1255,13 +1255,6 @@ private function createApplicationDefaultCredentials() $credentials->setSub($sub); } - if ($credentials instanceof ServiceAccountCredentials && $this->isUsingJwtWithScope()) { - // tell the credentials to sign scopes into Self-Signed JWTs instead of - // calling the OAuth2 token endpoint - // @see https://google.aip.dev/auth/4111#scope-vs-audience - $credentials->useJwtAccessWithScope(); - } - // If we are not using FetchAuthTokenCache yet, create it now if (!$credentials instanceof FetchAuthTokenCache) { $credentials = new FetchAuthTokenCache( From c15fb919429e0a5acffc6c01b34dcb752c0d7c52 Mon Sep 17 00:00:00 2001 From: Robert Currall Date: Mon, 7 Feb 2022 09:06:50 -0500 Subject: [PATCH 213/301] chore: add explicit return types to address Symfony deprecations (#2200) --- src/Collection.php | 10 ++++++++++ src/Model.php | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/src/Collection.php b/src/Collection.php index 0417dbc9a..1d653c80d 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -11,6 +11,7 @@ class Collection extends Model implements \Iterator, \Countable { protected $collection_key = 'items'; + /** @return void */ #[\ReturnTypeWillChange] public function rewind() { @@ -20,6 +21,7 @@ public function rewind() } } + /** @return mixed */ #[\ReturnTypeWillChange] public function current() { @@ -29,6 +31,7 @@ public function current() } } + /** @return mixed */ #[\ReturnTypeWillChange] public function key() { @@ -38,12 +41,14 @@ public function key() } } + /** @return void */ #[\ReturnTypeWillChange] public function next() { return next($this->{$this->collection_key}); } + /** @return bool */ #[\ReturnTypeWillChange] public function valid() { @@ -51,6 +56,7 @@ public function valid() return $key !== null && $key !== false; } + /** @return int */ #[\ReturnTypeWillChange] public function count() { @@ -60,6 +66,7 @@ public function count() return count($this->{$this->collection_key}); } + /** @return bool */ public function offsetExists($offset) { if (!is_numeric($offset)) { @@ -68,6 +75,7 @@ public function offsetExists($offset) return isset($this->{$this->collection_key}[$offset]); } + /** @return mixed */ public function offsetGet($offset) { if (!is_numeric($offset)) { @@ -77,6 +85,7 @@ public function offsetGet($offset) return $this->{$this->collection_key}[$offset]; } + /** @return void */ public function offsetSet($offset, $value) { if (!is_numeric($offset)) { @@ -85,6 +94,7 @@ public function offsetSet($offset, $value) $this->{$this->collection_key}[$offset] = $value; } + /** @return void */ public function offsetUnset($offset) { if (!is_numeric($offset)) { diff --git a/src/Model.php b/src/Model.php index 25ea40381..590984ef0 100644 --- a/src/Model.php +++ b/src/Model.php @@ -253,12 +253,14 @@ public function assertIsArray($obj, $method) } } + /** @return bool */ #[\ReturnTypeWillChange] public function offsetExists($offset) { return isset($this->$offset) || isset($this->modelData[$offset]); } + /** @return mixed */ #[\ReturnTypeWillChange] public function offsetGet($offset) { @@ -267,6 +269,7 @@ public function offsetGet($offset) $this->__get($offset); } + /** @return void */ #[\ReturnTypeWillChange] public function offsetSet($offset, $value) { @@ -278,6 +281,7 @@ public function offsetSet($offset, $value) } } + /** @return void */ #[\ReturnTypeWillChange] public function offsetUnset($offset) { From 0d1a2567fd8fad18af35443eea4263be037461d2 Mon Sep 17 00:00:00 2001 From: Sebastian Fuss Date: Mon, 7 Feb 2022 15:08:03 +0100 Subject: [PATCH 214/301] chore: update README.md (#2197) --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b3149928d..ac5d4ef48 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ This example will remove all services other than "Drive" and "YouTube" when `composer update` or a fresh `composer install` is run. **IMPORTANT**: If you add any services back in `composer.json`, you will need to -remove the `vendor/google/apiclient-services` directory explicity for the +remove the `vendor/google/apiclient-services` directory explicitly for the change you made to have effect: ```sh @@ -454,7 +454,7 @@ $opt_params = array( ### How do I set a field to null? ### -The library strips out nulls from the objects sent to the Google APIs as its the default value of all of the uninitialized properties. To work around this, set the field you want to null to `Google\Model::NULL_VALUE`. This is a placeholder that will be replaced with a true null when sent over the wire. +The library strips out nulls from the objects sent to the Google APIs as it is the default value of all of the uninitialized properties. To work around this, set the field you want to null to `Google\Model::NULL_VALUE`. This is a placeholder that will be replaced with a true null when sent over the wire. ## Code Quality ## From c0ae314e055219978e6cd419087523fefc5c759f Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 7 Feb 2022 06:09:56 -0800 Subject: [PATCH 215/301] chore: clean up workflows (#2190) --- .github/workflows/tests.yml | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 398d5dec8..318d026bf 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -18,8 +18,6 @@ jobs: composer-flags: "--prefer-lowest " - php: "8.0" composer-flags: "--prefer-lowest " - - php: "8.1" - composer-flags: "--prefer-lowest " name: PHP ${{ matrix.php }} ${{ matrix.composer-flags }}Unit Test steps: - uses: actions/checkout@v2 @@ -33,15 +31,9 @@ jobs: timeout_minutes: 10 max_attempts: 3 command: composer update ${{ matrix.composer-flags }} - - if: ${{ matrix.php == '8.0' || ( matrix.composer-flags == '--prefer-lowest' && matrix.php != '8.1' ) }} + - if: ${{ matrix.php == '8.0' && matrix.composer-flags == '--prefer-lowest ' }} name: Update guzzlehttp/ringphp dependency run: composer update guzzlehttp/ringphp - - if: ${{ matrix.php == '8.1' }} - name: Update guzzlehttp/ringphp dependency - run: composer update guzzlehttp/ringphp --ignore-platform-reqs - - if: ${{ matrix.php == '8.1' }} - name: Update phpunit/phpunit dependency - run: composer update phpunit/phpunit phpspec/prophecy-phpunit --with-dependencies --ignore-platform-reqs - name: Run Script run: vendor/bin/phpunit From 0735218971c34c37ccbf3f1e6c42f14e4e7492a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20L=C3=A9v=C3=AAque?= <5123888+Legenyes@users.noreply.github.com> Date: Tue, 5 Apr 2022 18:10:05 +0200 Subject: [PATCH 216/301] chore: add support for firebase/php-jwt v6.0 for CVE-2021-46743 (#2235) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8b712c5bd..ae6b32efc 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "php": "^5.6|^7.0|^8.0", "google/auth": "^1.10", "google/apiclient-services": "~0.200", - "firebase/php-jwt": "~2.0||~3.0||~4.0||~5.0", + "firebase/php-jwt": "~2.0||~3.0||~4.0||~5.0||~6.0", "monolog/monolog": "^1.17||^2.0", "phpseclib/phpseclib": "~2.0||^3.0.2", "guzzlehttp/guzzle": "~5.3.3||~6.0||~7.0", From f7640911534cb4682f1683392e4aab73012648ec Mon Sep 17 00:00:00 2001 From: Felipe Hertzer Date: Wed, 6 Apr 2022 02:17:18 +1000 Subject: [PATCH 217/301] fix: batch header concatenation (#2233) --- src/Http/Batch.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/Batch.php b/src/Http/Batch.php index a657bacd4..d27d5cd85 100644 --- a/src/Http/Batch.php +++ b/src/Http/Batch.php @@ -207,7 +207,7 @@ private function parseRawHeaders($rawHeaders) list($header, $value) = explode(': ', $headerLine, 2); $header = strtolower($header); if (isset($headers[$header])) { - $headers[$header] .= "\n" . $value; + $headers[$header] = array_merge((array)$headers[$header], (array)$value); } else { $headers[$header] = $value; } From a18b0e1ef5618523c607c01a41ec137c7f9af3b1 Mon Sep 17 00:00:00 2001 From: CyberFlame Date: Wed, 6 Apr 2022 04:19:05 +1200 Subject: [PATCH 218/301] chore: update psr7 for CVE-2022-24775 (#2234) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index ae6b32efc..c2579f7d6 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "monolog/monolog": "^1.17||^2.0", "phpseclib/phpseclib": "~2.0||^3.0.2", "guzzlehttp/guzzle": "~5.3.3||~6.0||~7.0", - "guzzlehttp/psr7": "^1.7||^2.0.0" + "guzzlehttp/psr7": "^1.8.4||^2.2.1" }, "require-dev": { "squizlabs/php_codesniffer": "~2.3", From e96471b6264ec8b0d22ceeaf12271f568aeb81b1 Mon Sep 17 00:00:00 2001 From: Alexander Dmitryuk Date: Wed, 6 Apr 2022 21:55:40 +0700 Subject: [PATCH 219/301] fix: check token expires_in attribute (#2218) --- src/Client.php | 4 ++++ tests/Google/ClientTest.php | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/Client.php b/src/Client.php index 70e8e82d6..c119e1ce3 100644 --- a/src/Client.php +++ b/src/Client.php @@ -569,6 +569,10 @@ public function isAccessTokenExpired() } } } + if (!isset($this->token['expires_in'])) { + // if the token does not have an "expires_in", then it's considered expired + return true; + } // If the token is set to expire in the next 30 seconds. return ($created + ($this->token['expires_in'] - 30)) < time(); diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 1286e7436..8968c14a7 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -315,6 +315,16 @@ public function testSettersGetters() $this->assertEquals($token, $client->getAccessToken()); } + public function testSetAccessTokenValidation() + { + $client = new Client(); + $client->setAccessToken([ + 'access_token' => 'token', + 'created' => time() + ]); + self::assertEquals(true, $client->isAccessTokenExpired()); + } + public function testDefaultConfigOptions() { $client = new Client(); From cb5250c68a3d08c4c1f1ac79170edd78f8214712 Mon Sep 17 00:00:00 2001 From: Alexander Dmitryuk Date: Mon, 11 Apr 2022 23:56:32 +0700 Subject: [PATCH 220/301] fix: missing import InvalidArgumentException (#2240) --- src/AccessToken/Verify.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/AccessToken/Verify.php b/src/AccessToken/Verify.php index fa997f211..6952bb81b 100644 --- a/src/AccessToken/Verify.php +++ b/src/AccessToken/Verify.php @@ -22,13 +22,12 @@ use Firebase\JWT\SignatureInvalidException; use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; +use InvalidArgumentException; use phpseclib3\Crypt\PublicKeyLoader; use phpseclib3\Crypt\RSA\PublicKey; use Psr\Cache\CacheItemPoolInterface; use Google\Auth\Cache\MemoryCacheItemPool; use Google\Exception as GoogleException; -use Stash\Driver\FileSystem; -use Stash\Pool; use DateTime; use DomainException; use Exception; From 506c488cb22c960022adf515bf0acc1d266e81db Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 18 Apr 2022 11:01:04 -0500 Subject: [PATCH 221/301] fix: bad firebase call (#2245) --- src/AccessToken/Verify.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/AccessToken/Verify.php b/src/AccessToken/Verify.php index 6952bb81b..cba784fbd 100644 --- a/src/AccessToken/Verify.php +++ b/src/AccessToken/Verify.php @@ -20,6 +20,7 @@ use Firebase\JWT\ExpiredException as ExpiredExceptionV3; use Firebase\JWT\SignatureInvalidException; +use Firebase\JWT\Key; use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; use InvalidArgumentException; @@ -99,11 +100,15 @@ public function verifyIdToken($idToken, $audience = null) $certs = $this->getFederatedSignOnCerts(); foreach ($certs as $cert) { try { - $payload = $this->jwt->decode( - $idToken, - $this->getPublicKey($cert), - array('RS256') - ); + $args = [$idToken]; + $publicKey = $this->getPublicKey($cert); + if (class_exists(Key::class)) { + $args[] = new Key($publicKey, 'RS256'); + } else { + $args[] = $publicKey; + $args[] = ['RS256']; + } + $payload = \call_user_func_array([$this->jwt, 'decode'], $args); if (property_exists($payload, 'aud')) { if ($audience && $payload->aud != $audience) { From 702eed9ae7022ba20dc7118c8161060cb50ee9f8 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 20 Apr 2022 10:44:03 -0600 Subject: [PATCH 222/301] chore: attempt to fix doc generation on tag (#2249) --- .github/actions/docs/entrypoint.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/actions/docs/entrypoint.sh b/.github/actions/docs/entrypoint.sh index 203f98e62..8498f777d 100755 --- a/.github/actions/docs/entrypoint.sh +++ b/.github/actions/docs/entrypoint.sh @@ -2,6 +2,7 @@ apt-get update apt-get install -y git wget +git checkout main git reset --hard HEAD # Create the directories From 51c617957671d09fe3cd47aab976af4902c5136c Mon Sep 17 00:00:00 2001 From: Anton Belyaev Date: Wed, 20 Apr 2022 20:15:04 +0300 Subject: [PATCH 223/301] chore: fix class reference in docblock (#2248) Co-authored-by: Brent Shaffer --- src/Client.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Client.php b/src/Client.php index c119e1ce3..2ad61be26 100644 --- a/src/Client.php +++ b/src/Client.php @@ -34,6 +34,7 @@ use GuzzleHttp\Ring\Client\StreamHandler; use Psr\Cache\CacheItemPoolInterface; use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; use Psr\Log\LoggerInterface; use Monolog\Logger; use Monolog\Handler\StreamHandler as MonologStreamHandler; From 1f1b62144ecb8247c838703c005f8bd278fca9b5 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 21 Apr 2022 08:26:25 -0600 Subject: [PATCH 224/301] chore: remove docs generation on tag (#2250) --- .github/actions/docs/entrypoint.sh | 1 - .github/workflows/docs.yml | 5 +---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/.github/actions/docs/entrypoint.sh b/.github/actions/docs/entrypoint.sh index 8498f777d..203f98e62 100755 --- a/.github/actions/docs/entrypoint.sh +++ b/.github/actions/docs/entrypoint.sh @@ -2,7 +2,6 @@ apt-get update apt-get install -y git wget -git checkout main git reset --hard HEAD # Create the directories diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 09199fd23..0c6125ad8 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -1,10 +1,7 @@ name: Generate Documentation on: push: - branches: - - main - tags: - - "*" + branches: [main] jobs: docs: From c6d5a7bd348ac5cb15053bb4552bdeb5f6e597c8 Mon Sep 17 00:00:00 2001 From: Elvis Morales Date: Thu, 21 Apr 2022 07:30:24 -0700 Subject: [PATCH 225/301] chore: update service accounts URL in README (#2210) --- docs/oauth-server.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/oauth-server.md b/docs/oauth-server.md index eb0707a18..a4c403eb0 100644 --- a/docs/oauth-server.md +++ b/docs/oauth-server.md @@ -28,7 +28,7 @@ If your application runs on Google App Engine, a service account is set up autom If your application doesn't run on Google App Engine or Google Compute Engine, you must obtain these credentials in the Google Developers Console. To generate service-account credentials, or to view the public credentials that you've already generated, do the following: -1. Open the [**Service accounts** section](https://console.developers.google.com/permissions/serviceaccounts?project=_) of the Developers Console's **Permissions** page. +1. Open the [**Service accounts** section](https://console.cloud.google.com/iam-admin/serviceaccounts) of the Developers Console's **IAM & Admin** page. 2. Click **Create service account**. 3. In the **Create service account** window, type a name for the service account and select **Furnish a new private key**. If you want to [grant G Suite domain-wide authority](https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority) to the service account, also select **Enable G Suite Domain-wide Delegation**. Then, click **Create**. From b567d092b2d5480fa08645bd84e3c999ba283e25 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 21 Apr 2022 11:30:15 -0600 Subject: [PATCH 226/301] chore: make whitespace consistent with our other libraries (#2251) --- .github/workflows/tests.yml | 3 +- composer.json | 3 +- examples/batch.php | 14 +- examples/idtoken.php | 30 +- examples/index.php | 24 +- examples/large-file-download.php | 128 +- examples/large-file-upload.php | 145 +- examples/multi-api.php | 62 +- examples/service-account.php | 20 +- examples/simple-file-upload.php | 94 +- examples/simple-query.php | 18 +- examples/templates/base.php | 106 +- phpcs.xml.dist | 22 +- src/AccessToken/Revoke.php | 84 +- src/AccessToken/Verify.php | 430 ++-- src/AuthHandler/AuthHandlerFactory.php | 48 +- src/AuthHandler/Guzzle5AuthHandler.php | 178 +- src/AuthHandler/Guzzle6AuthHandler.php | 192 +- src/AuthHandler/Guzzle7AuthHandler.php | 4 +- src/Client.php | 2461 ++++++++++----------- src/Collection.php | 168 +- src/Http/Batch.php | 381 ++-- src/Http/MediaFileUpload.php | 569 +++-- src/Http/REST.php | 274 +-- src/Model.php | 506 ++--- src/Service.php | 70 +- src/Service/Exception.php | 88 +- src/Service/Resource.php | 484 ++-- src/Task/Composer.php | 162 +- src/Task/Runner.php | 471 ++-- src/Utils/UriTemplate.php | 536 ++--- src/aliases.php | 40 +- tests/BaseTest.php | 422 ++-- tests/Google/AccessToken/VerifyTest.php | 238 +- tests/Google/CacheTest.php | 140 +- tests/Google/ClientTest.php | 1865 ++++++++-------- tests/Google/Http/BatchTest.php | 104 +- tests/Google/Http/MediaFileUploadTest.php | 346 +-- tests/Google/Http/RESTTest.php | 217 +- tests/Google/ModelTest.php | 424 ++-- tests/Google/Service/AdSenseTest.php | 816 +++---- tests/Google/Service/ResourceTest.php | 824 +++---- tests/Google/Service/TasksTest.php | 118 +- tests/Google/Service/YouTubeTest.php | 96 +- tests/Google/ServiceTest.php | 253 ++- tests/Google/Task/RunnerTest.php | 886 ++++---- tests/Google/Utils/UriTemplateTest.php | 507 +++-- tests/examples/batchTest.php | 18 +- tests/examples/idTokenTest.php | 24 +- tests/examples/indexTest.php | 14 +- tests/examples/largeFileDownloadTest.php | 48 +- tests/examples/largeFileUploadTest.php | 26 +- tests/examples/multiApiTest.php | 18 +- tests/examples/serviceAccountTest.php | 16 +- tests/examples/simpleFileUploadTest.php | 48 +- tests/examples/simpleQueryTest.php | 20 +- 56 files changed, 7650 insertions(+), 7653 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 318d026bf..58cd04d21 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -53,4 +53,5 @@ jobs: max_attempts: 3 command: composer install - name: Run Script - run: vendor/bin/phpcs src --standard=phpcs.xml.dist -np + run: vendor/bin/phpcs src tests examples --standard=phpcs.xml.dist -nps + diff --git a/composer.json b/composer.json index c2579f7d6..04d65f650 100644 --- a/composer.json +++ b/composer.json @@ -16,12 +16,11 @@ "guzzlehttp/psr7": "^1.8.4||^2.2.1" }, "require-dev": { - "squizlabs/php_codesniffer": "~2.3", + "squizlabs/php_codesniffer": "^3.0", "symfony/dom-crawler": "~2.1", "symfony/css-selector": "~2.1", "cache/filesystem-adapter": "^0.3.2|^1.1", "phpcompatibility/php-compatibility": "^9.2", - "dealerdirect/phpcodesniffer-composer-installer": "^0.7", "composer/composer": "^1.10.22", "yoast/phpunit-polyfills": "^1.0", "phpspec/prophecy-phpunit": "^1.1||^2.0", diff --git a/examples/batch.php b/examples/batch.php index 9df665059..a8377b584 100644 --- a/examples/batch.php +++ b/examples/batch.php @@ -37,8 +37,8 @@ // Warn if the API key isn't set. if (!$apiKey = getApiKey()) { - echo missingApiKeyWarning(); - return; + echo missingApiKeyWarning(); + return; } $client->setDeveloperKey($apiKey); @@ -79,15 +79,15 @@ ?>

      Results Of Call 1:

      - - + +

      Results Of Call 2:

      - - + +
      - +fetchAccessTokenWithAuthCode($_GET['code']); + $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); - // store in the session also - $_SESSION['id_token_token'] = $token; + // store in the session also + $_SESSION['id_token_token'] = $token; - // redirect back to the example - header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); - return; + // redirect back to the example + header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); + return; } /************************************************ @@ -75,9 +75,9 @@ !empty($_SESSION['id_token_token']) && isset($_SESSION['id_token_token']['id_token']) ) { - $client->setAccessToken($_SESSION['id_token_token']); + $client->setAccessToken($_SESSION['id_token_token']); } else { - $authUrl = $client->createAuthUrl(); + $authUrl = $client->createAuthUrl(); } /************************************************ @@ -89,16 +89,16 @@ and that can be cached. ************************************************/ if ($client->getAccessToken()) { - $token_data = $client->verifyIdToken(); + $token_data = $client->verifyIdToken(); } ?>
      - + - +

      Here is the data from your Id Token:

      @@ -106,4 +106,4 @@
      - + - - To view this example, run the following command from the root directory of this repository: + + To view this example, run the following command from the root directory of this repository: - php -S localhost:8080 -t examples/ + php -S localhost:8080 -t examples/ - And then browse to "localhost:8080" in your web browser - + And then browse to "localhost:8080" in your web browser + - - - - API Key set! - + + + + API Key set! + - +
      You have not entered your API key
      " method="POST"> @@ -40,4 +40,4 @@
    • An example of using multiple APIs.
    - +fetchAccessTokenWithAuthCode($_GET['code']); - $client->setAccessToken($token); + $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); + $client->setAccessToken($token); - // store in the session also - $_SESSION['upload_token'] = $token; + // store in the session also + $_SESSION['upload_token'] = $token; - // redirect back to the example - header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); + // redirect back to the example + header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); } // set the access token as part of the client if (!empty($_SESSION['upload_token'])) { - $client->setAccessToken($_SESSION['upload_token']); - if ($client->isAccessTokenExpired()) { - unset($_SESSION['upload_token']); - } + $client->setAccessToken($_SESSION['upload_token']); + if ($client->isAccessTokenExpired()) { + unset($_SESSION['upload_token']); + } } else { - $authUrl = $client->createAuthUrl(); + $authUrl = $client->createAuthUrl(); } /************************************************ @@ -73,77 +73,77 @@ * file. ************************************************/ if ($client->getAccessToken()) { - // Check for "Big File" and include the file ID and size - $files = $service->files->listFiles([ - 'q' => "name='Big File'", - 'fields' => 'files(id,size)' - ]); - - if (count($files) == 0) { - echo " + // Check for "Big File" and include the file ID and size + $files = $service->files->listFiles([ + 'q' => "name='Big File'", + 'fields' => 'files(id,size)' + ]); + + if (count($files) == 0) { + echo "

    Before you can use this sample, you need to upload a large file to Drive.

    "; - return; - } - - // If this is a POST, download the file - if ($_SERVER['REQUEST_METHOD'] == 'POST') { - // Determine the file's size and ID - $fileId = $files[0]->id; - $fileSize = intval($files[0]->size); - - // Get the authorized Guzzle HTTP client - $http = $client->authorize(); - - // Open a file for writing - $fp = fopen('Big File (downloaded)', 'w'); - - // Download in 1 MB chunks - $chunkSizeBytes = 1 * 1024 * 1024; - $chunkStart = 0; - - // Iterate over each chunk and write it to our file - while ($chunkStart < $fileSize) { - $chunkEnd = $chunkStart + $chunkSizeBytes; - $response = $http->request( - 'GET', - sprintf('/drive/v3/files/%s', $fileId), - [ - 'query' => ['alt' => 'media'], - 'headers' => [ - 'Range' => sprintf('bytes=%s-%s', $chunkStart, $chunkEnd) - ] - ] - ); - $chunkStart = $chunkEnd + 1; - fwrite($fp, $response->getBody()->getContents()); + return; } - // close the file pointer - fclose($fp); - // redirect back to this example - header('Location: ' . filter_var($redirect_uri . '?downloaded', FILTER_SANITIZE_URL)); - } + // If this is a POST, download the file + if ($_SERVER['REQUEST_METHOD'] == 'POST') { + // Determine the file's size and ID + $fileId = $files[0]->id; + $fileSize = intval($files[0]->size); + + // Get the authorized Guzzle HTTP client + $http = $client->authorize(); + + // Open a file for writing + $fp = fopen('Big File (downloaded)', 'w'); + + // Download in 1 MB chunks + $chunkSizeBytes = 1 * 1024 * 1024; + $chunkStart = 0; + + // Iterate over each chunk and write it to our file + while ($chunkStart < $fileSize) { + $chunkEnd = $chunkStart + $chunkSizeBytes; + $response = $http->request( + 'GET', + sprintf('/drive/v3/files/%s', $fileId), + [ + 'query' => ['alt' => 'media'], + 'headers' => [ + 'Range' => sprintf('bytes=%s-%s', $chunkStart, $chunkEnd) + ] + ] + ); + $chunkStart = $chunkEnd + 1; + fwrite($fp, $response->getBody()->getContents()); + } + // close the file pointer + fclose($fp); + + // redirect back to this example + header('Location: ' . filter_var($redirect_uri . '?downloaded', FILTER_SANITIZE_URL)); + } } ?>
    - + - +

    Your call was successful! Check your filesystem for the file:

    Big File (downloaded)

    - +
    - +fetchAccessTokenWithAuthCode($_GET['code']); - $client->setAccessToken($token); + $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); + $client->setAccessToken($token); - // store in the session also - $_SESSION['upload_token'] = $token; + // store in the session also + $_SESSION['upload_token'] = $token; - // redirect back to the example - header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); + // redirect back to the example + header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); } // set the access token as part of the client if (!empty($_SESSION['upload_token'])) { - $client->setAccessToken($_SESSION['upload_token']); - if ($client->isAccessTokenExpired()) { - unset($_SESSION['upload_token']); - } + $client->setAccessToken($_SESSION['upload_token']); + if ($client->isAccessTokenExpired()) { + unset($_SESSION['upload_token']); + } } else { - $authUrl = $client->createAuthUrl(); + $authUrl = $client->createAuthUrl(); } /************************************************ @@ -78,69 +78,70 @@ * file. ************************************************/ if ($_SERVER['REQUEST_METHOD'] == 'POST' && $client->getAccessToken()) { - /************************************************ - * We'll setup an empty 20MB file to upload. - ************************************************/ - DEFINE("TESTFILE", 'testfile.txt'); - if (!file_exists(TESTFILE)) { - $fh = fopen(TESTFILE, 'w'); - fseek($fh, 1024*1024*20); - fwrite($fh, "!", 1); - fclose($fh); - } - - $file = new Google\Service\Drive\DriveFile(); - $file->name = "Big File"; - $chunkSizeBytes = 1 * 1024 * 1024; - - // Call the API with the media upload, defer so it doesn't immediately return. - $client->setDefer(true); - $request = $service->files->create($file); - - // Create a media file upload to represent our upload process. - $media = new Google\Http\MediaFileUpload( - $client, - $request, - 'text/plain', - null, - true, - $chunkSizeBytes - ); - $media->setFileSize(filesize(TESTFILE)); - - // Upload the various chunks. $status will be false until the process is - // complete. - $status = false; - $handle = fopen(TESTFILE, "rb"); - while (!$status && !feof($handle)) { - // read until you get $chunkSizeBytes from TESTFILE - // fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file - // An example of a read buffered file is when reading from a URL - $chunk = readVideoChunk($handle, $chunkSizeBytes); - $status = $media->nextChunk($chunk); - } - - // The final value of $status will be the data from the API for the object - // that has been uploaded. - $result = false; - if ($status != false) { - $result = $status; - } - - fclose($handle); + /************************************************ + * We'll setup an empty 20MB file to upload. + ************************************************/ + DEFINE("TESTFILE", 'testfile.txt'); + if (!file_exists(TESTFILE)) { + $fh = fopen(TESTFILE, 'w'); + fseek($fh, 1024*1024*20); + fwrite($fh, "!", 1); + fclose($fh); + } + + $file = new Google\Service\Drive\DriveFile(); + $file->name = "Big File"; + $chunkSizeBytes = 1 * 1024 * 1024; + + // Call the API with the media upload, defer so it doesn't immediately return. + $client->setDefer(true); + $request = $service->files->create($file); + + // Create a media file upload to represent our upload process. + $media = new Google\Http\MediaFileUpload( + $client, + $request, + 'text/plain', + null, + true, + $chunkSizeBytes + ); + $media->setFileSize(filesize(TESTFILE)); + + // Upload the various chunks. $status will be false until the process is + // complete. + $status = false; + $handle = fopen(TESTFILE, "rb"); + while (!$status && !feof($handle)) { + // read until you get $chunkSizeBytes from TESTFILE + // fread will never return more than 8192 bytes if the stream is read + // buffered and it does not represent a plain file + // An example of a read buffered file is when reading from a URL + $chunk = readVideoChunk($handle, $chunkSizeBytes); + $status = $media->nextChunk($chunk); + } + + // The final value of $status will be the data from the API for the object + // that has been uploaded. + $result = false; + if ($status != false) { + $result = $status; + } + + fclose($handle); } -function readVideoChunk ($handle, $chunkSize) +function readVideoChunk($handle, $chunkSize) { $byteCount = 0; $giantChunk = ""; while (!feof($handle)) { - // fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file + // fread will never return more than 8192 bytes if the stream is read + // buffered and it does not represent a plain file $chunk = fread($handle, 8192); $byteCount += strlen($chunk); $giantChunk .= $chunk; - if ($byteCount >= $chunkSize) - { + if ($byteCount >= $chunkSize) { return $giantChunk; } } @@ -149,21 +150,21 @@ function readVideoChunk ($handle, $chunkSize) ?>
    - + - +

    Your call was successful! Check your drive for this file:

    name ?>

    Now try downloading a large file from Drive.

    - +
    - +fetchAccessTokenWithAuthCode($_GET['code']); - $client->setAccessToken($token); + $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); + $client->setAccessToken($token); - // store in the session also - $_SESSION['multi-api-token'] = $token; + // store in the session also + $_SESSION['multi-api-token'] = $token; - // redirect back to the example - header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); + // redirect back to the example + header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); } // set the access token as part of the client if (!empty($_SESSION['multi-api-token'])) { - $client->setAccessToken($_SESSION['multi-api-token']); - if ($client->isAccessTokenExpired()) { - unset($_SESSION['multi-api-token']); - } + $client->setAccessToken($_SESSION['multi-api-token']); + if ($client->isAccessTokenExpired()) { + unset($_SESSION['multi-api-token']); + } } else { - $authUrl = $client->createAuthUrl(); + $authUrl = $client->createAuthUrl(); } /************************************************ @@ -86,35 +86,35 @@ and a list of files from Drive. ************************************************/ if ($client->getAccessToken()) { - $_SESSION['multi-api-token'] = $client->getAccessToken(); + $_SESSION['multi-api-token'] = $client->getAccessToken(); - $dr_results = $dr_service->files->listFiles(array('pageSize' => 10)); + $dr_results = $dr_service->files->listFiles(array('pageSize' => 10)); - $yt_channels = $yt_service->channels->listChannels('contentDetails', array("mine" => true)); - $likePlaylist = $yt_channels[0]->contentDetails->relatedPlaylists->likes; - $yt_results = $yt_service->playlistItems->listPlaylistItems( - "snippet", - array("playlistId" => $likePlaylist) - ); + $yt_channels = $yt_service->channels->listChannels('contentDetails', array("mine" => true)); + $likePlaylist = $yt_channels[0]->contentDetails->relatedPlaylists->likes; + $yt_results = $yt_service->playlistItems->listPlaylistItems( + "snippet", + array("playlistId" => $likePlaylist) + ); } ?>
    - + - +

    Results Of Drive List:

    - - name ?>
    - + + name ?>
    +

    Results Of YouTube Likes:

    - -
    - + +
    +
    - +setAuthConfig($credentials_file); + // set the location manually + $client->setAuthConfig($credentials_file); } elseif (getenv('GOOGLE_APPLICATION_CREDENTIALS')) { - // use the application default credentials - $client->useApplicationDefaultCredentials(); + // use the application default credentials + $client->useApplicationDefaultCredentials(); } else { - echo missingServiceAccountDetailsWarning(); - return; + echo missingServiceAccountDetailsWarning(); + return; } $client->setApplicationName("Client_Library_Examples"); @@ -61,15 +61,15 @@ ************************************************/ $query = 'Henry David Thoreau'; $optParams = array( - 'filter' => 'free-ebooks', + 'filter' => 'free-ebooks', ); $results = $service->volumes->listVolumes($query, $optParams); ?>

    Results Of Call:

    - - + +
    - +fetchAccessTokenWithAuthCode($_GET['code']); - $client->setAccessToken($token); + $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); + $client->setAccessToken($token); - // store in the session also - $_SESSION['upload_token'] = $token; + // store in the session also + $_SESSION['upload_token'] = $token; - // redirect back to the example - header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); + // redirect back to the example + header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); } // set the access token as part of the client if (!empty($_SESSION['upload_token'])) { - $client->setAccessToken($_SESSION['upload_token']); - if ($client->isAccessTokenExpired()) { - unset($_SESSION['upload_token']); - } + $client->setAccessToken($_SESSION['upload_token']); + if ($client->isAccessTokenExpired()) { + unset($_SESSION['upload_token']); + } } else { - $authUrl = $client->createAuthUrl(); + $authUrl = $client->createAuthUrl(); } /************************************************ @@ -78,46 +78,46 @@ * file. For larger files, see fileupload.php. ************************************************/ if ($_SERVER['REQUEST_METHOD'] == 'POST' && $client->getAccessToken()) { - // We'll setup an empty 1MB file to upload. - DEFINE("TESTFILE", 'testfile-small.txt'); - if (!file_exists(TESTFILE)) { - $fh = fopen(TESTFILE, 'w'); - fseek($fh, 1024 * 1024); - fwrite($fh, "!", 1); - fclose($fh); - } + // We'll setup an empty 1MB file to upload. + DEFINE("TESTFILE", 'testfile-small.txt'); + if (!file_exists(TESTFILE)) { + $fh = fopen(TESTFILE, 'w'); + fseek($fh, 1024 * 1024); + fwrite($fh, "!", 1); + fclose($fh); + } - // This is uploading a file directly, with no metadata associated. - $file = new Google\Service\Drive\DriveFile(); - $result = $service->files->create( - $file, - array( - 'data' => file_get_contents(TESTFILE), - 'mimeType' => 'application/octet-stream', - 'uploadType' => 'media' - ) - ); + // This is uploading a file directly, with no metadata associated. + $file = new Google\Service\Drive\DriveFile(); + $result = $service->files->create( + $file, + array( + 'data' => file_get_contents(TESTFILE), + 'mimeType' => 'application/octet-stream', + 'uploadType' => 'media' + ) + ); - // Now lets try and send the metadata as well using multipart! - $file = new Google\Service\Drive\DriveFile(); - $file->setName("Hello World!"); - $result2 = $service->files->create( - $file, - array( - 'data' => file_get_contents(TESTFILE), - 'mimeType' => 'application/octet-stream', - 'uploadType' => 'multipart' - ) - ); + // Now lets try and send the metadata as well using multipart! + $file = new Google\Service\Drive\DriveFile(); + $file->setName("Hello World!"); + $result2 = $service->files->create( + $file, + array( + 'data' => file_get_contents(TESTFILE), + 'mimeType' => 'application/octet-stream', + 'uploadType' => 'multipart' + ) + ); } ?>
    - + - +

    Your call was successful! Check your drive for the following files:

    - +
    - +setDeveloperKey($apiKey); @@ -49,7 +49,7 @@ ************************************************/ $query = 'Henry David Thoreau'; $optParams = array( - 'filter' => 'free-ebooks', + 'filter' => 'free-ebooks', ); $results = $service->volumes->listVolumes($query, $optParams); @@ -59,7 +59,7 @@ $client->setDefer(true); $query = 'Henry David Thoreau'; $optParams = array( - 'filter' => 'free-ebooks', + 'filter' => 'free-ebooks', ); $request = $service->volumes->listVolumes($query, $optParams); $resultsDeferred = $client->execute($request); @@ -76,15 +76,15 @@ ?>

    Results Of Call:

    - - + +

    Results Of Deferred Call:

    - - + +
    - + + $ret = " " . $title . " \n"; - if ($_SERVER['PHP_SELF'] != "/index.php") { - $ret .= "

    Back

    "; - } - $ret .= "

    " . $title . "

    "; + if ($_SERVER['PHP_SELF'] != "/index.php") { + $ret .= "

    Back

    "; + } + $ret .= "

    " . $title . "

    "; - // Start the session (for storing access tokens and things) - if (!headers_sent()) { - session_start(); - } + // Start the session (for storing access tokens and things) + if (!headers_sent()) { + session_start(); + } - return $ret; + return $ret; } function pageFooter($file = null) { - $ret = ""; - if ($file) { - $ret .= "

    Code:

    "; - $ret .= "
    ";
    -    $ret .= htmlspecialchars(file_get_contents($file));
    -    $ret .= "
    "; - } - $ret .= ""; - - return $ret; + $ret = ""; + if ($file) { + $ret .= "

    Code:

    "; + $ret .= "
    ";
    +        $ret .= htmlspecialchars(file_get_contents($file));
    +        $ret .= "
    "; + } + $ret .= ""; + + return $ret; } function missingApiKeyWarning() { - $ret = " + $ret = "

    Warning: You need to set a Simple API Access key from the Google API console

    "; - return $ret; + return $ret; } function missingClientSecretsWarning() { - $ret = " + $ret = "

    Warning: You need to set Client ID, Client Secret and Redirect URI from the Google API console

    "; - return $ret; + return $ret; } function missingServiceAccountDetailsWarning() { - $ret = " + $ret = "

    Warning: You need download your Service Account Credentials JSON from the Google API console. @@ -81,12 +81,12 @@ function missingServiceAccountDetailsWarning() as the path to this file, but in the context of this example we will do this for you.

    "; - return $ret; + return $ret; } function missingOAuth2CredentialsWarning() { - $ret = " + $ret = "

    Warning: You need to set the location of your OAuth2 Client Credentials from the Google API console. @@ -96,72 +96,72 @@ function missingOAuth2CredentialsWarning() rename them 'oauth-credentials.json'.

    "; - return $ret; + return $ret; } function invalidCsrfTokenWarning() { - $ret = " + $ret = "

    The CSRF token is invalid, your session probably expired. Please refresh the page.

    "; - return $ret; + return $ret; } function checkServiceAccountCredentialsFile() { - // service account creds - $application_creds = __DIR__ . '/../../service-account-credentials.json'; + // service account creds + $application_creds = __DIR__ . '/../../service-account-credentials.json'; - return file_exists($application_creds) ? $application_creds : false; + return file_exists($application_creds) ? $application_creds : false; } function getCsrfToken() { - if (!isset($_SESSION['csrf_token'])) { - $_SESSION['csrf_token'] = bin2hex(openssl_random_pseudo_bytes(32)); - } + if (!isset($_SESSION['csrf_token'])) { + $_SESSION['csrf_token'] = bin2hex(openssl_random_pseudo_bytes(32)); + } - return $_SESSION['csrf_token']; + return $_SESSION['csrf_token']; } function validateCsrfToken() { - return isset($_REQUEST['csrf_token']) + return isset($_REQUEST['csrf_token']) && isset($_SESSION['csrf_token']) && $_REQUEST['csrf_token'] === $_SESSION['csrf_token']; } function getOAuthCredentialsFile() { - // oauth2 creds - $oauth_creds = __DIR__ . '/../../oauth-credentials.json'; + // oauth2 creds + $oauth_creds = __DIR__ . '/../../oauth-credentials.json'; - if (file_exists($oauth_creds)) { - return $oauth_creds; - } + if (file_exists($oauth_creds)) { + return $oauth_creds; + } - return false; + return false; } function setClientCredentialsFile($apiKey) { - $file = __DIR__ . '/../../tests/.apiKey'; - file_put_contents($file, $apiKey); + $file = __DIR__ . '/../../tests/.apiKey'; + file_put_contents($file, $apiKey); } function getApiKey() { - $file = __DIR__ . '/../../tests/.apiKey'; - if (file_exists($file)) { - return file_get_contents($file); - } + $file = __DIR__ . '/../../tests/.apiKey'; + if (file_exists($file)) { + return file_get_contents($file); + } } function setApiKey($apiKey) { - $file = __DIR__ . '/../../tests/.apiKey'; - file_put_contents($file, $apiKey); + $file = __DIR__ . '/../../tests/.apiKey'; + file_put_contents($file, $apiKey); } diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 3f58a6ba6..c18eadccd 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -21,8 +21,8 @@ - - Service/*.php + + *Test.php @@ -67,10 +67,10 @@ - + - + @@ -128,9 +128,15 @@ - + + + 0 + + + 0 + - - - - - + diff --git a/src/AccessToken/Revoke.php b/src/AccessToken/Revoke.php index d86cc6e32..bcccc9b47 100644 --- a/src/AccessToken/Revoke.php +++ b/src/AccessToken/Revoke.php @@ -30,52 +30,52 @@ */ class Revoke { - /** - * @var ClientInterface The http client - */ - private $http; + /** + * @var ClientInterface The http client + */ + private $http; - /** - * Instantiates the class, but does not initiate the login flow, leaving it - * to the discretion of the caller. - */ - public function __construct(ClientInterface $http = null) - { - $this->http = $http; - } - - /** - * Revoke an OAuth2 access token or refresh token. This method will revoke the current access - * token, if a token isn't provided. - * - * @param string|array $token The token (access token or a refresh token) that should be revoked. - * @return boolean Returns True if the revocation was successful, otherwise False. - */ - public function revokeToken($token) - { - if (is_array($token)) { - if (isset($token['refresh_token'])) { - $token = $token['refresh_token']; - } else { - $token = $token['access_token']; - } + /** + * Instantiates the class, but does not initiate the login flow, leaving it + * to the discretion of the caller. + */ + public function __construct(ClientInterface $http = null) + { + $this->http = $http; } - $body = Psr7\Utils::streamFor(http_build_query(array('token' => $token))); - $request = new Request( - 'POST', - Client::OAUTH2_REVOKE_URI, - [ - 'Cache-Control' => 'no-store', - 'Content-Type' => 'application/x-www-form-urlencoded', - ], - $body - ); + /** + * Revoke an OAuth2 access token or refresh token. This method will revoke the current access + * token, if a token isn't provided. + * + * @param string|array $token The token (access token or a refresh token) that should be revoked. + * @return boolean Returns True if the revocation was successful, otherwise False. + */ + public function revokeToken($token) + { + if (is_array($token)) { + if (isset($token['refresh_token'])) { + $token = $token['refresh_token']; + } else { + $token = $token['access_token']; + } + } + + $body = Psr7\Utils::streamFor(http_build_query(array('token' => $token))); + $request = new Request( + 'POST', + Client::OAUTH2_REVOKE_URI, + [ + 'Cache-Control' => 'no-store', + 'Content-Type' => 'application/x-www-form-urlencoded', + ], + $body + ); - $httpHandler = HttpHandlerFactory::build($this->http); + $httpHandler = HttpHandlerFactory::build($this->http); - $response = $httpHandler($request); + $response = $httpHandler($request); - return $response->getStatusCode() == 200; - } + return $response->getStatusCode() == 200; + } } diff --git a/src/AccessToken/Verify.php b/src/AccessToken/Verify.php index cba784fbd..6542ab23e 100644 --- a/src/AccessToken/Verify.php +++ b/src/AccessToken/Verify.php @@ -41,257 +41,257 @@ */ class Verify { - const FEDERATED_SIGNON_CERT_URL = 'https://www.googleapis.com/oauth2/v3/certs'; - const OAUTH2_ISSUER = 'accounts.google.com'; - const OAUTH2_ISSUER_HTTPS = 'https://accounts.google.com/'; - - /** - * @var ClientInterface The http client - */ - private $http; - - /** - * @var CacheItemPoolInterface cache class - */ - private $cache; - - /** - * Instantiates the class, but does not initiate the login flow, leaving it - * to the discretion of the caller. - */ - public function __construct( - ClientInterface $http = null, - CacheItemPoolInterface $cache = null, - $jwt = null - ) { - if (null === $http) { - $http = new Client(); - } - - if (null === $cache) { - $cache = new MemoryCacheItemPool; - } + const FEDERATED_SIGNON_CERT_URL = 'https://www.googleapis.com/oauth2/v3/certs'; + const OAUTH2_ISSUER = 'accounts.google.com'; + const OAUTH2_ISSUER_HTTPS = 'https://accounts.google.com/'; + + /** + * @var ClientInterface The http client + */ + private $http; + + /** + * @var CacheItemPoolInterface cache class + */ + private $cache; + + /** + * Instantiates the class, but does not initiate the login flow, leaving it + * to the discretion of the caller. + */ + public function __construct( + ClientInterface $http = null, + CacheItemPoolInterface $cache = null, + $jwt = null + ) { + if (null === $http) { + $http = new Client(); + } - $this->http = $http; - $this->cache = $cache; - $this->jwt = $jwt ?: $this->getJwtService(); - } + if (null === $cache) { + $cache = new MemoryCacheItemPool; + } - /** - * Verifies an id token and returns the authenticated apiLoginTicket. - * Throws an exception if the id token is not valid. - * The audience parameter can be used to control which id tokens are - * accepted. By default, the id token must have been issued to this OAuth2 client. - * - * @param string $idToken the ID token in JWT format - * @param string $audience Optional. The audience to verify against JWt "aud" - * @return array the token payload, if successful - */ - public function verifyIdToken($idToken, $audience = null) - { - if (empty($idToken)) { - throw new LogicException('id_token cannot be null'); + $this->http = $http; + $this->cache = $cache; + $this->jwt = $jwt ?: $this->getJwtService(); } - // set phpseclib constants if applicable - $this->setPhpsecConstants(); - - // Check signature - $certs = $this->getFederatedSignOnCerts(); - foreach ($certs as $cert) { - try { - $args = [$idToken]; - $publicKey = $this->getPublicKey($cert); - if (class_exists(Key::class)) { - $args[] = new Key($publicKey, 'RS256'); - } else { - $args[] = $publicKey; - $args[] = ['RS256']; - } - $payload = \call_user_func_array([$this->jwt, 'decode'], $args); - - if (property_exists($payload, 'aud')) { - if ($audience && $payload->aud != $audience) { - return false; - } + /** + * Verifies an id token and returns the authenticated apiLoginTicket. + * Throws an exception if the id token is not valid. + * The audience parameter can be used to control which id tokens are + * accepted. By default, the id token must have been issued to this OAuth2 client. + * + * @param string $idToken the ID token in JWT format + * @param string $audience Optional. The audience to verify against JWt "aud" + * @return array the token payload, if successful + */ + public function verifyIdToken($idToken, $audience = null) + { + if (empty($idToken)) { + throw new LogicException('id_token cannot be null'); } - // support HTTP and HTTPS issuers - // @see https://developers.google.com/identity/sign-in/web/backend-auth - $issuers = array(self::OAUTH2_ISSUER, self::OAUTH2_ISSUER_HTTPS); - if (!isset($payload->iss) || !in_array($payload->iss, $issuers)) { - return false; + // set phpseclib constants if applicable + $this->setPhpsecConstants(); + + // Check signature + $certs = $this->getFederatedSignOnCerts(); + foreach ($certs as $cert) { + try { + $args = [$idToken]; + $publicKey = $this->getPublicKey($cert); + if (class_exists(Key::class)) { + $args[] = new Key($publicKey, 'RS256'); + } else { + $args[] = $publicKey; + $args[] = ['RS256']; + } + $payload = \call_user_func_array([$this->jwt, 'decode'], $args); + + if (property_exists($payload, 'aud')) { + if ($audience && $payload->aud != $audience) { + return false; + } + } + + // support HTTP and HTTPS issuers + // @see https://developers.google.com/identity/sign-in/web/backend-auth + $issuers = array(self::OAUTH2_ISSUER, self::OAUTH2_ISSUER_HTTPS); + if (!isset($payload->iss) || !in_array($payload->iss, $issuers)) { + return false; + } + + return (array) $payload; + } catch (ExpiredException $e) { + return false; + } catch (ExpiredExceptionV3 $e) { + return false; + } catch (SignatureInvalidException $e) { + // continue + } catch (DomainException $e) { + // continue + } } - return (array) $payload; - } catch (ExpiredException $e) { return false; - } catch (ExpiredExceptionV3 $e) { - return false; - } catch (SignatureInvalidException $e) { - // continue - } catch (DomainException $e) { - // continue - } } - return false; - } + private function getCache() + { + return $this->cache; + } + + /** + * Retrieve and cache a certificates file. + * + * @param $url string location + * @throws \Google\Exception + * @return array certificates + */ + private function retrieveCertsFromLocation($url) + { + // If we're retrieving a local file, just grab it. + if (0 !== strpos($url, 'http')) { + if (!$file = file_get_contents($url)) { + throw new GoogleException( + "Failed to retrieve verification certificates: '" . + $url . "'." + ); + } + + return json_decode($file, true); + } - private function getCache() - { - return $this->cache; - } + $response = $this->http->get($url); - /** - * Retrieve and cache a certificates file. - * - * @param $url string location - * @throws \Google\Exception - * @return array certificates - */ - private function retrieveCertsFromLocation($url) - { - // If we're retrieving a local file, just grab it. - if (0 !== strpos($url, 'http')) { - if (!$file = file_get_contents($url)) { + if ($response->getStatusCode() == 200) { + return json_decode((string) $response->getBody(), true); + } throw new GoogleException( - "Failed to retrieve verification certificates: '" . - $url . "'." + sprintf( + 'Failed to retrieve verification certificates: "%s".', + $response->getBody()->getContents() + ), + $response->getStatusCode() ); - } - - return json_decode($file, true); } - $response = $this->http->get($url); + // Gets federated sign-on certificates to use for verifying identity tokens. + // Returns certs as array structure, where keys are key ids, and values + // are PEM encoded certificates. + private function getFederatedSignOnCerts() + { + $certs = null; + if ($cache = $this->getCache()) { + $cacheItem = $cache->getItem('federated_signon_certs_v3'); + $certs = $cacheItem->get(); + } + - if ($response->getStatusCode() == 200) { - return json_decode((string) $response->getBody(), true); - } - throw new GoogleException( - sprintf( - 'Failed to retrieve verification certificates: "%s".', - $response->getBody()->getContents() - ), - $response->getStatusCode() - ); - } - - // Gets federated sign-on certificates to use for verifying identity tokens. - // Returns certs as array structure, where keys are key ids, and values - // are PEM encoded certificates. - private function getFederatedSignOnCerts() - { - $certs = null; - if ($cache = $this->getCache()) { - $cacheItem = $cache->getItem('federated_signon_certs_v3'); - $certs = $cacheItem->get(); - } + if (!$certs) { + $certs = $this->retrieveCertsFromLocation( + self::FEDERATED_SIGNON_CERT_URL + ); + if ($cache) { + $cacheItem->expiresAt(new DateTime('+1 hour')); + $cacheItem->set($certs); + $cache->save($cacheItem); + } + } - if (!$certs) { - $certs = $this->retrieveCertsFromLocation( - self::FEDERATED_SIGNON_CERT_URL - ); + if (!isset($certs['keys'])) { + throw new InvalidArgumentException( + 'federated sign-on certs expects "keys" to be set' + ); + } - if ($cache) { - $cacheItem->expiresAt(new DateTime('+1 hour')); - $cacheItem->set($certs); - $cache->save($cacheItem); - } + return $certs['keys']; } - if (!isset($certs['keys'])) { - throw new InvalidArgumentException( - 'federated sign-on certs expects "keys" to be set' - ); - } + private function getJwtService() + { + $jwtClass = 'JWT'; + if (class_exists('\Firebase\JWT\JWT')) { + $jwtClass = 'Firebase\JWT\JWT'; + } - return $certs['keys']; - } + if (property_exists($jwtClass, 'leeway') && $jwtClass::$leeway < 1) { + // Ensures JWT leeway is at least 1 + // @see https://github.com/google/google-api-php-client/issues/827 + $jwtClass::$leeway = 1; + } - private function getJwtService() - { - $jwtClass = 'JWT'; - if (class_exists('\Firebase\JWT\JWT')) { - $jwtClass = 'Firebase\JWT\JWT'; + return new $jwtClass; } - if (property_exists($jwtClass, 'leeway') && $jwtClass::$leeway < 1) { - // Ensures JWT leeway is at least 1 - // @see https://github.com/google/google-api-php-client/issues/827 - $jwtClass::$leeway = 1; - } + private function getPublicKey($cert) + { + $bigIntClass = $this->getBigIntClass(); + $modulus = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['n']), 256); + $exponent = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['e']), 256); + $component = array('n' => $modulus, 'e' => $exponent); - return new $jwtClass; - } + if (class_exists('phpseclib3\Crypt\RSA\PublicKey')) { + /** @var PublicKey $loader */ + $loader = PublicKeyLoader::load($component); - private function getPublicKey($cert) - { - $bigIntClass = $this->getBigIntClass(); - $modulus = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['n']), 256); - $exponent = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['e']), 256); - $component = array('n' => $modulus, 'e' => $exponent); + return $loader->toString('PKCS8'); + } - if (class_exists('phpseclib3\Crypt\RSA\PublicKey')) { - /** @var PublicKey $loader */ - $loader = PublicKeyLoader::load($component); + $rsaClass = $this->getRsaClass(); + $rsa = new $rsaClass(); + $rsa->loadKey($component); - return $loader->toString('PKCS8'); + return $rsa->getPublicKey(); } - $rsaClass = $this->getRsaClass(); - $rsa = new $rsaClass(); - $rsa->loadKey($component); - - return $rsa->getPublicKey(); - } + private function getRsaClass() + { + if (class_exists('phpseclib3\Crypt\RSA')) { + return 'phpseclib3\Crypt\RSA'; + } - private function getRsaClass() - { - if (class_exists('phpseclib3\Crypt\RSA')) { - return 'phpseclib3\Crypt\RSA'; - } + if (class_exists('phpseclib\Crypt\RSA')) { + return 'phpseclib\Crypt\RSA'; + } - if (class_exists('phpseclib\Crypt\RSA')) { - return 'phpseclib\Crypt\RSA'; + return 'Crypt_RSA'; } - return 'Crypt_RSA'; - } + private function getBigIntClass() + { + if (class_exists('phpseclib3\Math\BigInteger')) { + return 'phpseclib3\Math\BigInteger'; + } - private function getBigIntClass() - { - if (class_exists('phpseclib3\Math\BigInteger')) { - return 'phpseclib3\Math\BigInteger'; - } + if (class_exists('phpseclib\Math\BigInteger')) { + return 'phpseclib\Math\BigInteger'; + } - if (class_exists('phpseclib\Math\BigInteger')) { - return 'phpseclib\Math\BigInteger'; + return 'Math_BigInteger'; } - return 'Math_BigInteger'; - } + private function getOpenSslConstant() + { + if (class_exists('phpseclib3\Crypt\AES')) { + return 'phpseclib3\Crypt\AES::ENGINE_OPENSSL'; + } - private function getOpenSslConstant() - { - if (class_exists('phpseclib3\Crypt\AES')) { - return 'phpseclib3\Crypt\AES::ENGINE_OPENSSL'; - } + if (class_exists('phpseclib\Crypt\RSA')) { + return 'phpseclib\Crypt\RSA::MODE_OPENSSL'; + } - if (class_exists('phpseclib\Crypt\RSA')) { - return 'phpseclib\Crypt\RSA::MODE_OPENSSL'; - } + if (class_exists('Crypt_RSA')) { + return 'CRYPT_RSA_MODE_OPENSSL'; + } - if (class_exists('Crypt_RSA')) { - return 'CRYPT_RSA_MODE_OPENSSL'; + throw new Exception('Cannot find RSA class'); } - throw new Exception('Cannot find RSA class'); - } - - /** + /** * phpseclib calls "phpinfo" by default, which requires special * whitelisting in the AppEngine VM environment. This function * sets constants to bypass the need for phpseclib to check phpinfo @@ -299,15 +299,15 @@ private function getOpenSslConstant() * @see phpseclib/Math/BigInteger * @see https://github.com/GoogleCloudPlatform/getting-started-php/issues/85 */ - private function setPhpsecConstants() - { - if (filter_var(getenv('GAE_VM'), FILTER_VALIDATE_BOOLEAN)) { - if (!defined('MATH_BIGINTEGER_OPENSSL_ENABLED')) { - define('MATH_BIGINTEGER_OPENSSL_ENABLED', true); - } - if (!defined('CRYPT_RSA_MODE')) { - define('CRYPT_RSA_MODE', constant($this->getOpenSslConstant())); - } + private function setPhpsecConstants() + { + if (filter_var(getenv('GAE_VM'), FILTER_VALIDATE_BOOLEAN)) { + if (!defined('MATH_BIGINTEGER_OPENSSL_ENABLED')) { + define('MATH_BIGINTEGER_OPENSSL_ENABLED', true); + } + if (!defined('CRYPT_RSA_MODE')) { + define('CRYPT_RSA_MODE', constant($this->getOpenSslConstant())); + } + } } - } } diff --git a/src/AuthHandler/AuthHandlerFactory.php b/src/AuthHandler/AuthHandlerFactory.php index dced77a17..65510440f 100644 --- a/src/AuthHandler/AuthHandlerFactory.php +++ b/src/AuthHandler/AuthHandlerFactory.php @@ -23,30 +23,30 @@ class AuthHandlerFactory { - /** - * Builds out a default http handler for the installed version of guzzle. - * - * @return Guzzle5AuthHandler|Guzzle6AuthHandler|Guzzle7AuthHandler - * @throws Exception - */ - public static function build($cache = null, array $cacheConfig = []) - { - $guzzleVersion = null; - if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) { - $guzzleVersion = ClientInterface::MAJOR_VERSION; - } elseif (defined('\GuzzleHttp\ClientInterface::VERSION')) { - $guzzleVersion = (int) substr(ClientInterface::VERSION, 0, 1); - } + /** + * Builds out a default http handler for the installed version of guzzle. + * + * @return Guzzle5AuthHandler|Guzzle6AuthHandler|Guzzle7AuthHandler + * @throws Exception + */ + public static function build($cache = null, array $cacheConfig = []) + { + $guzzleVersion = null; + if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) { + $guzzleVersion = ClientInterface::MAJOR_VERSION; + } elseif (defined('\GuzzleHttp\ClientInterface::VERSION')) { + $guzzleVersion = (int) substr(ClientInterface::VERSION, 0, 1); + } - switch ($guzzleVersion) { - case 5: - return new Guzzle5AuthHandler($cache, $cacheConfig); - case 6: - return new Guzzle6AuthHandler($cache, $cacheConfig); - case 7: - return new Guzzle7AuthHandler($cache, $cacheConfig); - default: - throw new Exception('Version not supported'); + switch ($guzzleVersion) { + case 5: + return new Guzzle5AuthHandler($cache, $cacheConfig); + case 6: + return new Guzzle6AuthHandler($cache, $cacheConfig); + case 7: + return new Guzzle7AuthHandler($cache, $cacheConfig); + default: + throw new Exception('Version not supported'); + } } - } } diff --git a/src/AuthHandler/Guzzle5AuthHandler.php b/src/AuthHandler/Guzzle5AuthHandler.php index bf7440df1..f2767036f 100644 --- a/src/AuthHandler/Guzzle5AuthHandler.php +++ b/src/AuthHandler/Guzzle5AuthHandler.php @@ -13,98 +13,96 @@ use Psr\Cache\CacheItemPoolInterface; /** -* -*/ + * This supports Guzzle 5 + */ class Guzzle5AuthHandler { - protected $cache; - protected $cacheConfig; - - public function __construct(CacheItemPoolInterface $cache = null, array $cacheConfig = []) - { - $this->cache = $cache; - $this->cacheConfig = $cacheConfig; - } - - public function attachCredentials( - ClientInterface $http, - CredentialsLoader $credentials, - callable $tokenCallback = null - ) { - // use the provided cache - if ($this->cache) { - $credentials = new FetchAuthTokenCache( - $credentials, - $this->cacheConfig, - $this->cache - ); + protected $cache; + protected $cacheConfig; + + public function __construct(CacheItemPoolInterface $cache = null, array $cacheConfig = []) + { + $this->cache = $cache; + $this->cacheConfig = $cacheConfig; + } + + public function attachCredentials( + ClientInterface $http, + CredentialsLoader $credentials, + callable $tokenCallback = null + ) { + // use the provided cache + if ($this->cache) { + $credentials = new FetchAuthTokenCache( + $credentials, + $this->cacheConfig, + $this->cache + ); + } + + return $this->attachCredentialsCache($http, $credentials, $tokenCallback); + } + + public function attachCredentialsCache( + ClientInterface $http, + FetchAuthTokenCache $credentials, + callable $tokenCallback = null + ) { + // if we end up needing to make an HTTP request to retrieve credentials, we + // can use our existing one, but we need to throw exceptions so the error + // bubbles up. + $authHttp = $this->createAuthHttp($http); + $authHttpHandler = HttpHandlerFactory::build($authHttp); + $subscriber = new AuthTokenSubscriber( + $credentials, + $authHttpHandler, + $tokenCallback + ); + + $http->setDefaultOption('auth', 'google_auth'); + $http->getEmitter()->attach($subscriber); + + return $http; } - return $this->attachCredentialsCache($http, $credentials, $tokenCallback); - } - - public function attachCredentialsCache( - ClientInterface $http, - FetchAuthTokenCache $credentials, - callable $tokenCallback = null - ) { - // if we end up needing to make an HTTP request to retrieve credentials, we - // can use our existing one, but we need to throw exceptions so the error - // bubbles up. - $authHttp = $this->createAuthHttp($http); - $authHttpHandler = HttpHandlerFactory::build($authHttp); - $subscriber = new AuthTokenSubscriber( - $credentials, - $authHttpHandler, - $tokenCallback - ); - - $http->setDefaultOption('auth', 'google_auth'); - $http->getEmitter()->attach($subscriber); - - return $http; - } - - public function attachToken(ClientInterface $http, array $token, array $scopes) - { - $tokenFunc = function ($scopes) use ($token) { - return $token['access_token']; - }; - - $subscriber = new ScopedAccessTokenSubscriber( - $tokenFunc, - $scopes, - $this->cacheConfig, - $this->cache - ); - - $http->setDefaultOption('auth', 'scoped'); - $http->getEmitter()->attach($subscriber); - - return $http; - } - - public function attachKey(ClientInterface $http, $key) - { - $subscriber = new SimpleSubscriber(['key' => $key]); - - $http->setDefaultOption('auth', 'simple'); - $http->getEmitter()->attach($subscriber); - - return $http; - } - - private function createAuthHttp(ClientInterface $http) - { - return new Client( - [ - 'base_url' => $http->getBaseUrl(), - 'defaults' => [ - 'exceptions' => true, - 'verify' => $http->getDefaultOption('verify'), - 'proxy' => $http->getDefaultOption('proxy'), - ] - ] - ); - } + public function attachToken(ClientInterface $http, array $token, array $scopes) + { + $tokenFunc = function ($scopes) use ($token) { + return $token['access_token']; + }; + + $subscriber = new ScopedAccessTokenSubscriber( + $tokenFunc, + $scopes, + $this->cacheConfig, + $this->cache + ); + + $http->setDefaultOption('auth', 'scoped'); + $http->getEmitter()->attach($subscriber); + + return $http; + } + + public function attachKey(ClientInterface $http, $key) + { + $subscriber = new SimpleSubscriber(['key' => $key]); + + $http->setDefaultOption('auth', 'simple'); + $http->getEmitter()->attach($subscriber); + + return $http; + } + + private function createAuthHttp(ClientInterface $http) + { + return new Client([ + 'base_url' => $http->getBaseUrl(), + 'defaults' => [ + 'exceptions' => true, + 'verify' => $http->getDefaultOption('verify'), + 'proxy' => $http->getDefaultOption('proxy'), + ] + ]); + } } diff --git a/src/AuthHandler/Guzzle6AuthHandler.php b/src/AuthHandler/Guzzle6AuthHandler.php index 35de17ce7..560070724 100644 --- a/src/AuthHandler/Guzzle6AuthHandler.php +++ b/src/AuthHandler/Guzzle6AuthHandler.php @@ -13,105 +13,103 @@ use Psr\Cache\CacheItemPoolInterface; /** -* This supports Guzzle 6 -*/ + * This supports Guzzle 6 + */ class Guzzle6AuthHandler { - protected $cache; - protected $cacheConfig; - - public function __construct(CacheItemPoolInterface $cache = null, array $cacheConfig = []) - { - $this->cache = $cache; - $this->cacheConfig = $cacheConfig; - } - - public function attachCredentials( - ClientInterface $http, - CredentialsLoader $credentials, - callable $tokenCallback = null - ) { - // use the provided cache - if ($this->cache) { - $credentials = new FetchAuthTokenCache( - $credentials, - $this->cacheConfig, - $this->cache - ); + protected $cache; + protected $cacheConfig; + + public function __construct(CacheItemPoolInterface $cache = null, array $cacheConfig = []) + { + $this->cache = $cache; + $this->cacheConfig = $cacheConfig; + } + + public function attachCredentials( + ClientInterface $http, + CredentialsLoader $credentials, + callable $tokenCallback = null + ) { + // use the provided cache + if ($this->cache) { + $credentials = new FetchAuthTokenCache( + $credentials, + $this->cacheConfig, + $this->cache + ); + } + + return $this->attachCredentialsCache($http, $credentials, $tokenCallback); + } + + public function attachCredentialsCache( + ClientInterface $http, + FetchAuthTokenCache $credentials, + callable $tokenCallback = null + ) { + // if we end up needing to make an HTTP request to retrieve credentials, we + // can use our existing one, but we need to throw exceptions so the error + // bubbles up. + $authHttp = $this->createAuthHttp($http); + $authHttpHandler = HttpHandlerFactory::build($authHttp); + $middleware = new AuthTokenMiddleware( + $credentials, + $authHttpHandler, + $tokenCallback + ); + + $config = $http->getConfig(); + $config['handler']->remove('google_auth'); + $config['handler']->push($middleware, 'google_auth'); + $config['auth'] = 'google_auth'; + $http = new Client($config); + + return $http; + } + + public function attachToken(ClientInterface $http, array $token, array $scopes) + { + $tokenFunc = function ($scopes) use ($token) { + return $token['access_token']; + }; + + $middleware = new ScopedAccessTokenMiddleware( + $tokenFunc, + $scopes, + $this->cacheConfig, + $this->cache + ); + + $config = $http->getConfig(); + $config['handler']->remove('google_auth'); + $config['handler']->push($middleware, 'google_auth'); + $config['auth'] = 'scoped'; + $http = new Client($config); + + return $http; + } + + public function attachKey(ClientInterface $http, $key) + { + $middleware = new SimpleMiddleware(['key' => $key]); + + $config = $http->getConfig(); + $config['handler']->remove('google_auth'); + $config['handler']->push($middleware, 'google_auth'); + $config['auth'] = 'simple'; + $http = new Client($config); + + return $http; } - return $this->attachCredentialsCache($http, $credentials, $tokenCallback); - } - - public function attachCredentialsCache( - ClientInterface $http, - FetchAuthTokenCache $credentials, - callable $tokenCallback = null - ) { - // if we end up needing to make an HTTP request to retrieve credentials, we - // can use our existing one, but we need to throw exceptions so the error - // bubbles up. - $authHttp = $this->createAuthHttp($http); - $authHttpHandler = HttpHandlerFactory::build($authHttp); - $middleware = new AuthTokenMiddleware( - $credentials, - $authHttpHandler, - $tokenCallback - ); - - $config = $http->getConfig(); - $config['handler']->remove('google_auth'); - $config['handler']->push($middleware, 'google_auth'); - $config['auth'] = 'google_auth'; - $http = new Client($config); - - return $http; - } - - public function attachToken(ClientInterface $http, array $token, array $scopes) - { - $tokenFunc = function ($scopes) use ($token) { - return $token['access_token']; - }; - - $middleware = new ScopedAccessTokenMiddleware( - $tokenFunc, - $scopes, - $this->cacheConfig, - $this->cache - ); - - $config = $http->getConfig(); - $config['handler']->remove('google_auth'); - $config['handler']->push($middleware, 'google_auth'); - $config['auth'] = 'scoped'; - $http = new Client($config); - - return $http; - } - - public function attachKey(ClientInterface $http, $key) - { - $middleware = new SimpleMiddleware(['key' => $key]); - - $config = $http->getConfig(); - $config['handler']->remove('google_auth'); - $config['handler']->push($middleware, 'google_auth'); - $config['auth'] = 'simple'; - $http = new Client($config); - - return $http; - } - - private function createAuthHttp(ClientInterface $http) - { - return new Client( - [ - 'base_uri' => $http->getConfig('base_uri'), - 'http_errors' => true, - 'verify' => $http->getConfig('verify'), - 'proxy' => $http->getConfig('proxy'), - ] - ); - } + private function createAuthHttp(ClientInterface $http) + { + return new Client([ + 'base_uri' => $http->getConfig('base_uri'), + 'http_errors' => true, + 'verify' => $http->getConfig('verify'), + 'proxy' => $http->getConfig('proxy'), + ]); + } } diff --git a/src/AuthHandler/Guzzle7AuthHandler.php b/src/AuthHandler/Guzzle7AuthHandler.php index 6804f75cb..310f8b12c 100644 --- a/src/AuthHandler/Guzzle7AuthHandler.php +++ b/src/AuthHandler/Guzzle7AuthHandler.php @@ -18,8 +18,8 @@ namespace Google\AuthHandler; /** -* This supports Guzzle 7 -*/ + * This supports Guzzle 7 + */ class Guzzle7AuthHandler extends Guzzle6AuthHandler { } diff --git a/src/Client.php b/src/Client.php index 2ad61be26..0e95260e9 100644 --- a/src/Client.php +++ b/src/Client.php @@ -50,1250 +50,1243 @@ */ class Client { - const LIBVER = "2.12.1"; - const USER_AGENT_SUFFIX = "google-api-php-client/"; - const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; - const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; - const OAUTH2_AUTH_URL = 'https://accounts.google.com/o/oauth2/auth'; - const API_BASE_PATH = 'https://www.googleapis.com/'; - - /** - * @var OAuth2 $auth - */ - private $auth; - - /** - * @var ClientInterface $http - */ - private $http; - - /** - * @var CacheItemPoolInterface $cache - */ - private $cache; - - /** - * @var array access token - */ - private $token; - - /** - * @var array $config - */ - private $config; - - /** - * @var LoggerInterface $logger - */ - private $logger; - - /** - * @var CredentialsLoader $credentials - */ - private $credentials; - - /** - * @var boolean $deferExecution - */ - private $deferExecution = false; - - /** @var array $scopes */ - // Scopes requested by the client - protected $requestedScopes = []; - - /** - * Construct the Google Client. - * - * @param array $config - */ - public function __construct(array $config = array()) - { - $this->config = array_merge( - [ - 'application_name' => '', - - // Don't change these unless you're working against a special development - // or testing environment. - 'base_path' => self::API_BASE_PATH, - - // https://developers.google.com/console - 'client_id' => '', - 'client_secret' => '', - - // Can be a path to JSON credentials or an array representing those - // credentials (@see Google\Client::setAuthConfig), or an instance of - // Google\Auth\CredentialsLoader. - 'credentials' => null, - // @see Google\Client::setScopes - 'scopes' => null, - // Sets X-Goog-User-Project, which specifies a user project to bill - // for access charges associated with the request - 'quota_project' => null, - - 'redirect_uri' => null, - 'state' => null, - - // Simple API access key, also from the API console. Ensure you get - // a Server key, and not a Browser key. - 'developer_key' => '', - - // For use with Google Cloud Platform - // fetch the ApplicationDefaultCredentials, if applicable - // @see https://developers.google.com/identity/protocols/application-default-credentials - 'use_application_default_credentials' => false, - 'signing_key' => null, - 'signing_algorithm' => null, - 'subject' => null, - - // Other OAuth2 parameters. - 'hd' => '', - 'prompt' => '', - 'openid.realm' => '', - 'include_granted_scopes' => null, - 'login_hint' => '', - 'request_visible_actions' => '', - 'access_type' => 'online', - 'approval_prompt' => 'auto', - - // Task Runner retry configuration - // @see Google\Task\Runner - 'retry' => array(), - 'retry_map' => null, - - // Cache class implementing Psr\Cache\CacheItemPoolInterface. - // Defaults to Google\Auth\Cache\MemoryCacheItemPool. - 'cache' => null, - // cache config for downstream auth caching - 'cache_config' => [], - - // function to be called when an access token is fetched - // follows the signature function ($cacheKey, $accessToken) - 'token_callback' => null, - - // Service class used in Google\Client::verifyIdToken. - // Explicitly pass this in to avoid setting JWT::$leeway - 'jwt' => null, - - // Setting api_format_v2 will return more detailed error messages - // from certain APIs. - 'api_format_v2' => false - ], - $config - ); - - if (!is_null($this->config['credentials'])) { - if ($this->config['credentials'] instanceof CredentialsLoader) { - $this->credentials = $this->config['credentials']; - } else { - $this->setAuthConfig($this->config['credentials']); - } - unset($this->config['credentials']); - } - - if (!is_null($this->config['scopes'])) { - $this->setScopes($this->config['scopes']); - unset($this->config['scopes']); - } - - // Set a default token callback to update the in-memory access token - if (is_null($this->config['token_callback'])) { - $this->config['token_callback'] = function ($cacheKey, $newAccessToken) { - $this->setAccessToken( - [ - 'access_token' => $newAccessToken, - 'expires_in' => 3600, // Google default - 'created' => time(), - ] - ); - }; - } - - if (!is_null($this->config['cache'])) { - $this->setCache($this->config['cache']); - unset($this->config['cache']); - } - } - - /** - * Get a string containing the version of the library. - * - * @return string - */ - public function getLibraryVersion() - { - return self::LIBVER; - } - - /** - * For backwards compatibility - * alias for fetchAccessTokenWithAuthCode - * - * @param $code string code from accounts.google.com - * @return array access token - * @deprecated - */ - public function authenticate($code) - { - return $this->fetchAccessTokenWithAuthCode($code); - } - - /** - * Attempt to exchange a code for an valid authentication token. - * Helper wrapped around the OAuth 2.0 implementation. - * - * @param $code string code from accounts.google.com - * @return array access token - */ - public function fetchAccessTokenWithAuthCode($code) - { - if (strlen($code) == 0) { - throw new InvalidArgumentException("Invalid code"); - } - - $auth = $this->getOAuth2Service(); - $auth->setCode($code); - $auth->setRedirectUri($this->getRedirectUri()); - - $httpHandler = HttpHandlerFactory::build($this->getHttpClient()); - $creds = $auth->fetchAuthToken($httpHandler); - if ($creds && isset($creds['access_token'])) { - $creds['created'] = time(); - $this->setAccessToken($creds); - } - - return $creds; - } - - /** - * For backwards compatibility - * alias for fetchAccessTokenWithAssertion - * - * @return array access token - * @deprecated - */ - public function refreshTokenWithAssertion() - { - return $this->fetchAccessTokenWithAssertion(); - } - - /** - * Fetches a fresh access token with a given assertion token. - * @param ClientInterface $authHttp optional. - * @return array access token - */ - public function fetchAccessTokenWithAssertion(ClientInterface $authHttp = null) - { - if (!$this->isUsingApplicationDefaultCredentials()) { - throw new DomainException( - 'set the JSON service account credentials using' - . ' Google\Client::setAuthConfig or set the path to your JSON file' - . ' with the "GOOGLE_APPLICATION_CREDENTIALS" environment variable' - . ' and call Google\Client::useApplicationDefaultCredentials to' - . ' refresh a token with assertion.' - ); - } - - $this->getLogger()->log( - 'info', - 'OAuth2 access token refresh with Signed JWT assertion grants.' - ); - - $credentials = $this->createApplicationDefaultCredentials(); - - $httpHandler = HttpHandlerFactory::build($authHttp); - $creds = $credentials->fetchAuthToken($httpHandler); - if ($creds && isset($creds['access_token'])) { - $creds['created'] = time(); - $this->setAccessToken($creds); - } - - return $creds; - } - - /** - * For backwards compatibility - * alias for fetchAccessTokenWithRefreshToken - * - * @param string $refreshToken - * @return array access token - */ - public function refreshToken($refreshToken) - { - return $this->fetchAccessTokenWithRefreshToken($refreshToken); - } - - /** - * Fetches a fresh OAuth 2.0 access token with the given refresh token. - * @param string $refreshToken - * @return array access token - */ - public function fetchAccessTokenWithRefreshToken($refreshToken = null) - { - if (null === $refreshToken) { - if (!isset($this->token['refresh_token'])) { - throw new LogicException( - 'refresh token must be passed in or set as part of setAccessToken' + const LIBVER = "2.12.1"; + const USER_AGENT_SUFFIX = "google-api-php-client/"; + const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; + const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; + const OAUTH2_AUTH_URL = 'https://accounts.google.com/o/oauth2/auth'; + const API_BASE_PATH = 'https://www.googleapis.com/'; + + /** + * @var OAuth2 $auth + */ + private $auth; + + /** + * @var ClientInterface $http + */ + private $http; + + /** + * @var CacheItemPoolInterface $cache + */ + private $cache; + + /** + * @var array access token + */ + private $token; + + /** + * @var array $config + */ + private $config; + + /** + * @var LoggerInterface $logger + */ + private $logger; + + /** + * @var CredentialsLoader $credentials + */ + private $credentials; + + /** + * @var boolean $deferExecution + */ + private $deferExecution = false; + + /** @var array $scopes */ + // Scopes requested by the client + protected $requestedScopes = []; + + /** + * Construct the Google Client. + * + * @param array $config + */ + public function __construct(array $config = array()) + { + $this->config = array_merge([ + 'application_name' => '', + + // Don't change these unless you're working against a special development + // or testing environment. + 'base_path' => self::API_BASE_PATH, + + // https://developers.google.com/console + 'client_id' => '', + 'client_secret' => '', + + // Can be a path to JSON credentials or an array representing those + // credentials (@see Google\Client::setAuthConfig), or an instance of + // Google\Auth\CredentialsLoader. + 'credentials' => null, + // @see Google\Client::setScopes + 'scopes' => null, + // Sets X-Goog-User-Project, which specifies a user project to bill + // for access charges associated with the request + 'quota_project' => null, + + 'redirect_uri' => null, + 'state' => null, + + // Simple API access key, also from the API console. Ensure you get + // a Server key, and not a Browser key. + 'developer_key' => '', + + // For use with Google Cloud Platform + // fetch the ApplicationDefaultCredentials, if applicable + // @see https://developers.google.com/identity/protocols/application-default-credentials + 'use_application_default_credentials' => false, + 'signing_key' => null, + 'signing_algorithm' => null, + 'subject' => null, + + // Other OAuth2 parameters. + 'hd' => '', + 'prompt' => '', + 'openid.realm' => '', + 'include_granted_scopes' => null, + 'login_hint' => '', + 'request_visible_actions' => '', + 'access_type' => 'online', + 'approval_prompt' => 'auto', + + // Task Runner retry configuration + // @see Google\Task\Runner + 'retry' => array(), + 'retry_map' => null, + + // Cache class implementing Psr\Cache\CacheItemPoolInterface. + // Defaults to Google\Auth\Cache\MemoryCacheItemPool. + 'cache' => null, + // cache config for downstream auth caching + 'cache_config' => [], + + // function to be called when an access token is fetched + // follows the signature function ($cacheKey, $accessToken) + 'token_callback' => null, + + // Service class used in Google\Client::verifyIdToken. + // Explicitly pass this in to avoid setting JWT::$leeway + 'jwt' => null, + + // Setting api_format_v2 will return more detailed error messages + // from certain APIs. + 'api_format_v2' => false + ], $config); + + if (!is_null($this->config['credentials'])) { + if ($this->config['credentials'] instanceof CredentialsLoader) { + $this->credentials = $this->config['credentials']; + } else { + $this->setAuthConfig($this->config['credentials']); + } + unset($this->config['credentials']); + } + + if (!is_null($this->config['scopes'])) { + $this->setScopes($this->config['scopes']); + unset($this->config['scopes']); + } + + // Set a default token callback to update the in-memory access token + if (is_null($this->config['token_callback'])) { + $this->config['token_callback'] = function ($cacheKey, $newAccessToken) { + $this->setAccessToken( + [ + 'access_token' => $newAccessToken, + 'expires_in' => 3600, // Google default + 'created' => time(), + ] + ); + }; + } + + if (!is_null($this->config['cache'])) { + $this->setCache($this->config['cache']); + unset($this->config['cache']); + } + } + + /** + * Get a string containing the version of the library. + * + * @return string + */ + public function getLibraryVersion() + { + return self::LIBVER; + } + + /** + * For backwards compatibility + * alias for fetchAccessTokenWithAuthCode + * + * @param $code string code from accounts.google.com + * @return array access token + * @deprecated + */ + public function authenticate($code) + { + return $this->fetchAccessTokenWithAuthCode($code); + } + + /** + * Attempt to exchange a code for an valid authentication token. + * Helper wrapped around the OAuth 2.0 implementation. + * + * @param $code string code from accounts.google.com + * @return array access token + */ + public function fetchAccessTokenWithAuthCode($code) + { + if (strlen($code) == 0) { + throw new InvalidArgumentException("Invalid code"); + } + + $auth = $this->getOAuth2Service(); + $auth->setCode($code); + $auth->setRedirectUri($this->getRedirectUri()); + + $httpHandler = HttpHandlerFactory::build($this->getHttpClient()); + $creds = $auth->fetchAuthToken($httpHandler); + if ($creds && isset($creds['access_token'])) { + $creds['created'] = time(); + $this->setAccessToken($creds); + } + + return $creds; + } + + /** + * For backwards compatibility + * alias for fetchAccessTokenWithAssertion + * + * @return array access token + * @deprecated + */ + public function refreshTokenWithAssertion() + { + return $this->fetchAccessTokenWithAssertion(); + } + + /** + * Fetches a fresh access token with a given assertion token. + * @param ClientInterface $authHttp optional. + * @return array access token + */ + public function fetchAccessTokenWithAssertion(ClientInterface $authHttp = null) + { + if (!$this->isUsingApplicationDefaultCredentials()) { + throw new DomainException( + 'set the JSON service account credentials using' + . ' Google\Client::setAuthConfig or set the path to your JSON file' + . ' with the "GOOGLE_APPLICATION_CREDENTIALS" environment variable' + . ' and call Google\Client::useApplicationDefaultCredentials to' + . ' refresh a token with assertion.' + ); + } + + $this->getLogger()->log( + 'info', + 'OAuth2 access token refresh with Signed JWT assertion grants.' ); - } - $refreshToken = $this->token['refresh_token']; - } - $this->getLogger()->info('OAuth2 access token refresh'); - $auth = $this->getOAuth2Service(); - $auth->setRefreshToken($refreshToken); - - $httpHandler = HttpHandlerFactory::build($this->getHttpClient()); - $creds = $auth->fetchAuthToken($httpHandler); - if ($creds && isset($creds['access_token'])) { - $creds['created'] = time(); - if (!isset($creds['refresh_token'])) { - $creds['refresh_token'] = $refreshToken; - } - $this->setAccessToken($creds); - } - - return $creds; - } - - /** - * Create a URL to obtain user authorization. - * The authorization endpoint allows the user to first - * authenticate, and then grant/deny the access request. - * @param string|array $scope The scope is expressed as an array or list of space-delimited strings. - * @return string - */ - public function createAuthUrl($scope = null) - { - if (empty($scope)) { - $scope = $this->prepareScopes(); - } - if (is_array($scope)) { - $scope = implode(' ', $scope); - } - - // only accept one of prompt or approval_prompt - $approvalPrompt = $this->config['prompt'] - ? null - : $this->config['approval_prompt']; - - // include_granted_scopes should be string "true", string "false", or null - $includeGrantedScopes = $this->config['include_granted_scopes'] === null - ? null - : var_export($this->config['include_granted_scopes'], true); - - $params = array_filter( - [ - 'access_type' => $this->config['access_type'], - 'approval_prompt' => $approvalPrompt, - 'hd' => $this->config['hd'], - 'include_granted_scopes' => $includeGrantedScopes, - 'login_hint' => $this->config['login_hint'], - 'openid.realm' => $this->config['openid.realm'], - 'prompt' => $this->config['prompt'], - 'response_type' => 'code', - 'scope' => $scope, - 'state' => $this->config['state'], - ] - ); - - // If the list of scopes contains plus.login, add request_visible_actions - // to auth URL. - $rva = $this->config['request_visible_actions']; - if (strlen($rva) > 0 && false !== strpos($scope, 'plus.login')) { - $params['request_visible_actions'] = $rva; - } - - $auth = $this->getOAuth2Service(); - - return (string) $auth->buildFullAuthorizationUri($params); - } - - /** - * Adds auth listeners to the HTTP client based on the credentials - * set in the Google API Client object - * - * @param ClientInterface $http the http client object. - * @return ClientInterface the http client object - */ - public function authorize(ClientInterface $http = null) - { - $http = $http ?: $this->getHttpClient(); - $authHandler = $this->getAuthHandler(); - - // These conditionals represent the decision tree for authentication - // 1. Check if a Google\Auth\CredentialsLoader instance has been supplied via the "credentials" option - // 2. Check for Application Default Credentials - // 3a. Check for an Access Token - // 3b. If access token exists but is expired, try to refresh it - // 4. Check for API Key - if ($this->credentials) { - return $authHandler->attachCredentials( - $http, - $this->credentials, - $this->config['token_callback'] - ); - } - - if ($this->isUsingApplicationDefaultCredentials()) { - $credentials = $this->createApplicationDefaultCredentials(); - return $authHandler->attachCredentialsCache( - $http, - $credentials, - $this->config['token_callback'] - ); - } - - if ($token = $this->getAccessToken()) { - $scopes = $this->prepareScopes(); - // add refresh subscriber to request a new token - if (isset($token['refresh_token']) && $this->isAccessTokenExpired()) { - $credentials = $this->createUserRefreshCredentials( - $scopes, - $token['refresh_token'] + + $credentials = $this->createApplicationDefaultCredentials(); + + $httpHandler = HttpHandlerFactory::build($authHttp); + $creds = $credentials->fetchAuthToken($httpHandler); + if ($creds && isset($creds['access_token'])) { + $creds['created'] = time(); + $this->setAccessToken($creds); + } + + return $creds; + } + + /** + * For backwards compatibility + * alias for fetchAccessTokenWithRefreshToken + * + * @param string $refreshToken + * @return array access token + */ + public function refreshToken($refreshToken) + { + return $this->fetchAccessTokenWithRefreshToken($refreshToken); + } + + /** + * Fetches a fresh OAuth 2.0 access token with the given refresh token. + * @param string $refreshToken + * @return array access token + */ + public function fetchAccessTokenWithRefreshToken($refreshToken = null) + { + if (null === $refreshToken) { + if (!isset($this->token['refresh_token'])) { + throw new LogicException( + 'refresh token must be passed in or set as part of setAccessToken' + ); + } + $refreshToken = $this->token['refresh_token']; + } + $this->getLogger()->info('OAuth2 access token refresh'); + $auth = $this->getOAuth2Service(); + $auth->setRefreshToken($refreshToken); + + $httpHandler = HttpHandlerFactory::build($this->getHttpClient()); + $creds = $auth->fetchAuthToken($httpHandler); + if ($creds && isset($creds['access_token'])) { + $creds['created'] = time(); + if (!isset($creds['refresh_token'])) { + $creds['refresh_token'] = $refreshToken; + } + $this->setAccessToken($creds); + } + + return $creds; + } + + /** + * Create a URL to obtain user authorization. + * The authorization endpoint allows the user to first + * authenticate, and then grant/deny the access request. + * @param string|array $scope The scope is expressed as an array or list of space-delimited strings. + * @return string + */ + public function createAuthUrl($scope = null) + { + if (empty($scope)) { + $scope = $this->prepareScopes(); + } + if (is_array($scope)) { + $scope = implode(' ', $scope); + } + + // only accept one of prompt or approval_prompt + $approvalPrompt = $this->config['prompt'] + ? null + : $this->config['approval_prompt']; + + // include_granted_scopes should be string "true", string "false", or null + $includeGrantedScopes = $this->config['include_granted_scopes'] === null + ? null + : var_export($this->config['include_granted_scopes'], true); + + $params = array_filter([ + 'access_type' => $this->config['access_type'], + 'approval_prompt' => $approvalPrompt, + 'hd' => $this->config['hd'], + 'include_granted_scopes' => $includeGrantedScopes, + 'login_hint' => $this->config['login_hint'], + 'openid.realm' => $this->config['openid.realm'], + 'prompt' => $this->config['prompt'], + 'response_type' => 'code', + 'scope' => $scope, + 'state' => $this->config['state'], + ]); + + // If the list of scopes contains plus.login, add request_visible_actions + // to auth URL. + $rva = $this->config['request_visible_actions']; + if (strlen($rva) > 0 && false !== strpos($scope, 'plus.login')) { + $params['request_visible_actions'] = $rva; + } + + $auth = $this->getOAuth2Service(); + + return (string) $auth->buildFullAuthorizationUri($params); + } + + /** + * Adds auth listeners to the HTTP client based on the credentials + * set in the Google API Client object + * + * @param ClientInterface $http the http client object. + * @return ClientInterface the http client object + */ + public function authorize(ClientInterface $http = null) + { + $http = $http ?: $this->getHttpClient(); + $authHandler = $this->getAuthHandler(); + + // These conditionals represent the decision tree for authentication + // 1. Check if a Google\Auth\CredentialsLoader instance has been supplied via the "credentials" option + // 2. Check for Application Default Credentials + // 3a. Check for an Access Token + // 3b. If access token exists but is expired, try to refresh it + // 4. Check for API Key + if ($this->credentials) { + return $authHandler->attachCredentials( + $http, + $this->credentials, + $this->config['token_callback'] + ); + } + + if ($this->isUsingApplicationDefaultCredentials()) { + $credentials = $this->createApplicationDefaultCredentials(); + return $authHandler->attachCredentialsCache( + $http, + $credentials, + $this->config['token_callback'] + ); + } + + if ($token = $this->getAccessToken()) { + $scopes = $this->prepareScopes(); + // add refresh subscriber to request a new token + if (isset($token['refresh_token']) && $this->isAccessTokenExpired()) { + $credentials = $this->createUserRefreshCredentials( + $scopes, + $token['refresh_token'] + ); + return $authHandler->attachCredentials( + $http, + $credentials, + $this->config['token_callback'] + ); + } + + return $authHandler->attachToken($http, $token, (array) $scopes); + } + + if ($key = $this->config['developer_key']) { + return $authHandler->attachKey($http, $key); + } + + return $http; + } + + /** + * Set the configuration to use application default credentials for + * authentication + * + * @see https://developers.google.com/identity/protocols/application-default-credentials + * @param boolean $useAppCreds + */ + public function useApplicationDefaultCredentials($useAppCreds = true) + { + $this->config['use_application_default_credentials'] = $useAppCreds; + } + + /** + * To prevent useApplicationDefaultCredentials from inappropriately being + * called in a conditional + * + * @see https://developers.google.com/identity/protocols/application-default-credentials + */ + public function isUsingApplicationDefaultCredentials() + { + return $this->config['use_application_default_credentials']; + } + + /** + * Set the access token used for requests. + * + * Note that at the time requests are sent, tokens are cached. A token will be + * cached for each combination of service and authentication scopes. If a + * cache pool is not provided, creating a new instance of the client will + * allow modification of access tokens. If a persistent cache pool is + * provided, in order to change the access token, you must clear the cached + * token by calling `$client->getCache()->clear()`. (Use caution in this case, + * as calling `clear()` will remove all cache items, including any items not + * related to Google API PHP Client.) + * + * @param string|array $token + * @throws InvalidArgumentException + */ + public function setAccessToken($token) + { + if (is_string($token)) { + if ($json = json_decode($token, true)) { + $token = $json; + } else { + // assume $token is just the token string + $token = array( + 'access_token' => $token, + ); + } + } + if ($token == null) { + throw new InvalidArgumentException('invalid json token'); + } + if (!isset($token['access_token'])) { + throw new InvalidArgumentException("Invalid token format"); + } + $this->token = $token; + } + + public function getAccessToken() + { + return $this->token; + } + + /** + * @return string|null + */ + public function getRefreshToken() + { + if (isset($this->token['refresh_token'])) { + return $this->token['refresh_token']; + } + + return null; + } + + /** + * Returns if the access_token is expired. + * @return bool Returns True if the access_token is expired. + */ + public function isAccessTokenExpired() + { + if (!$this->token) { + return true; + } + + $created = 0; + if (isset($this->token['created'])) { + $created = $this->token['created']; + } elseif (isset($this->token['id_token'])) { + // check the ID token for "iat" + // signature verification is not required here, as we are just + // using this for convenience to save a round trip request + // to the Google API server + $idToken = $this->token['id_token']; + if (substr_count($idToken, '.') == 2) { + $parts = explode('.', $idToken); + $payload = json_decode(base64_decode($parts[1]), true); + if ($payload && isset($payload['iat'])) { + $created = $payload['iat']; + } + } + } + if (!isset($this->token['expires_in'])) { + // if the token does not have an "expires_in", then it's considered expired + return true; + } + + // If the token is set to expire in the next 30 seconds. + return ($created + ($this->token['expires_in'] - 30)) < time(); + } + + /** + * @deprecated See UPGRADING.md for more information + */ + public function getAuth() + { + throw new BadMethodCallException( + 'This function no longer exists. See UPGRADING.md for more information' ); - return $authHandler->attachCredentials( - $http, - $credentials, - $this->config['token_callback'] + } + + /** + * @deprecated See UPGRADING.md for more information + */ + public function setAuth($auth) + { + throw new BadMethodCallException( + 'This function no longer exists. See UPGRADING.md for more information' ); - } - - return $authHandler->attachToken($http, $token, (array) $scopes); - } - - if ($key = $this->config['developer_key']) { - return $authHandler->attachKey($http, $key); - } - - return $http; - } - - /** - * Set the configuration to use application default credentials for - * authentication - * - * @see https://developers.google.com/identity/protocols/application-default-credentials - * @param boolean $useAppCreds - */ - public function useApplicationDefaultCredentials($useAppCreds = true) - { - $this->config['use_application_default_credentials'] = $useAppCreds; - } - - /** - * To prevent useApplicationDefaultCredentials from inappropriately being - * called in a conditional - * - * @see https://developers.google.com/identity/protocols/application-default-credentials - */ - public function isUsingApplicationDefaultCredentials() - { - return $this->config['use_application_default_credentials']; - } - - /** - * Set the access token used for requests. - * - * Note that at the time requests are sent, tokens are cached. A token will be - * cached for each combination of service and authentication scopes. If a - * cache pool is not provided, creating a new instance of the client will - * allow modification of access tokens. If a persistent cache pool is - * provided, in order to change the access token, you must clear the cached - * token by calling `$client->getCache()->clear()`. (Use caution in this case, - * as calling `clear()` will remove all cache items, including any items not - * related to Google API PHP Client.) - * - * @param string|array $token - * @throws InvalidArgumentException - */ - public function setAccessToken($token) - { - if (is_string($token)) { - if ($json = json_decode($token, true)) { - $token = $json; - } else { - // assume $token is just the token string - $token = array( - 'access_token' => $token, + } + + /** + * Set the OAuth 2.0 Client ID. + * @param string $clientId + */ + public function setClientId($clientId) + { + $this->config['client_id'] = $clientId; + } + + public function getClientId() + { + return $this->config['client_id']; + } + + /** + * Set the OAuth 2.0 Client Secret. + * @param string $clientSecret + */ + public function setClientSecret($clientSecret) + { + $this->config['client_secret'] = $clientSecret; + } + + public function getClientSecret() + { + return $this->config['client_secret']; + } + + /** + * Set the OAuth 2.0 Redirect URI. + * @param string $redirectUri + */ + public function setRedirectUri($redirectUri) + { + $this->config['redirect_uri'] = $redirectUri; + } + + public function getRedirectUri() + { + return $this->config['redirect_uri']; + } + + /** + * Set OAuth 2.0 "state" parameter to achieve per-request customization. + * @see http://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-3.1.2.2 + * @param string $state + */ + public function setState($state) + { + $this->config['state'] = $state; + } + + /** + * @param string $accessType Possible values for access_type include: + * {@code "offline"} to request offline access from the user. + * {@code "online"} to request online access from the user. + */ + public function setAccessType($accessType) + { + $this->config['access_type'] = $accessType; + } + + /** + * @param string $approvalPrompt Possible values for approval_prompt include: + * {@code "force"} to force the approval UI to appear. + * {@code "auto"} to request auto-approval when possible. (This is the default value) + */ + public function setApprovalPrompt($approvalPrompt) + { + $this->config['approval_prompt'] = $approvalPrompt; + } + + /** + * Set the login hint, email address or sub id. + * @param string $loginHint + */ + public function setLoginHint($loginHint) + { + $this->config['login_hint'] = $loginHint; + } + + /** + * Set the application name, this is included in the User-Agent HTTP header. + * @param string $applicationName + */ + public function setApplicationName($applicationName) + { + $this->config['application_name'] = $applicationName; + } + + /** + * If 'plus.login' is included in the list of requested scopes, you can use + * this method to define types of app activities that your app will write. + * You can find a list of available types here: + * @link https://developers.google.com/+/api/moment-types + * + * @param array $requestVisibleActions Array of app activity types + */ + public function setRequestVisibleActions($requestVisibleActions) + { + if (is_array($requestVisibleActions)) { + $requestVisibleActions = implode(" ", $requestVisibleActions); + } + $this->config['request_visible_actions'] = $requestVisibleActions; + } + + /** + * Set the developer key to use, these are obtained through the API Console. + * @see http://code.google.com/apis/console-help/#generatingdevkeys + * @param string $developerKey + */ + public function setDeveloperKey($developerKey) + { + $this->config['developer_key'] = $developerKey; + } + + /** + * Set the hd (hosted domain) parameter streamlines the login process for + * Google Apps hosted accounts. By including the domain of the user, you + * restrict sign-in to accounts at that domain. + * @param $hd string - the domain to use. + */ + public function setHostedDomain($hd) + { + $this->config['hd'] = $hd; + } + + /** + * Set the prompt hint. Valid values are none, consent and select_account. + * If no value is specified and the user has not previously authorized + * access, then the user is shown a consent screen. + * @param $prompt string + * {@code "none"} Do not display any authentication or consent screens. Must not be specified with other values. + * {@code "consent"} Prompt the user for consent. + * {@code "select_account"} Prompt the user to select an account. + */ + public function setPrompt($prompt) + { + $this->config['prompt'] = $prompt; + } + + /** + * openid.realm is a parameter from the OpenID 2.0 protocol, not from OAuth + * 2.0. It is used in OpenID 2.0 requests to signify the URL-space for which + * an authentication request is valid. + * @param $realm string - the URL-space to use. + */ + public function setOpenidRealm($realm) + { + $this->config['openid.realm'] = $realm; + } + + /** + * If this is provided with the value true, and the authorization request is + * granted, the authorization will include any previous authorizations + * granted to this user/application combination for other scopes. + * @param $include boolean - the URL-space to use. + */ + public function setIncludeGrantedScopes($include) + { + $this->config['include_granted_scopes'] = $include; + } + + /** + * sets function to be called when an access token is fetched + * @param callable $tokenCallback - function ($cacheKey, $accessToken) + */ + public function setTokenCallback(callable $tokenCallback) + { + $this->config['token_callback'] = $tokenCallback; + } + + /** + * Revoke an OAuth2 access token or refresh token. This method will revoke the current access + * token, if a token isn't provided. + * + * @param string|array|null $token The token (access token or a refresh token) that should be revoked. + * @return boolean Returns True if the revocation was successful, otherwise False. + */ + public function revokeToken($token = null) + { + $tokenRevoker = new Revoke($this->getHttpClient()); + + return $tokenRevoker->revokeToken($token ?: $this->getAccessToken()); + } + + /** + * Verify an id_token. This method will verify the current id_token, if one + * isn't provided. + * + * @throws LogicException If no token was provided and no token was set using `setAccessToken`. + * @throws UnexpectedValueException If the token is not a valid JWT. + * @param string|null $idToken The token (id_token) that should be verified. + * @return array|false Returns the token payload as an array if the verification was + * successful, false otherwise. + */ + public function verifyIdToken($idToken = null) + { + $tokenVerifier = new Verify( + $this->getHttpClient(), + $this->getCache(), + $this->config['jwt'] ); - } - } - if ($token == null) { - throw new InvalidArgumentException('invalid json token'); - } - if (!isset($token['access_token'])) { - throw new InvalidArgumentException("Invalid token format"); - } - $this->token = $token; - } - - public function getAccessToken() - { - return $this->token; - } - - /** - * @return string|null - */ - public function getRefreshToken() - { - if (isset($this->token['refresh_token'])) { - return $this->token['refresh_token']; - } - - return null; - } - - /** - * Returns if the access_token is expired. - * @return bool Returns True if the access_token is expired. - */ - public function isAccessTokenExpired() - { - if (!$this->token) { - return true; - } - - $created = 0; - if (isset($this->token['created'])) { - $created = $this->token['created']; - } elseif (isset($this->token['id_token'])) { - // check the ID token for "iat" - // signature verification is not required here, as we are just - // using this for convenience to save a round trip request - // to the Google API server - $idToken = $this->token['id_token']; - if (substr_count($idToken, '.') == 2) { - $parts = explode('.', $idToken); - $payload = json_decode(base64_decode($parts[1]), true); - if ($payload && isset($payload['iat'])) { - $created = $payload['iat']; + + if (null === $idToken) { + $token = $this->getAccessToken(); + if (!isset($token['id_token'])) { + throw new LogicException( + 'id_token must be passed in or set as part of setAccessToken' + ); + } + $idToken = $token['id_token']; } - } - } - if (!isset($this->token['expires_in'])) { - // if the token does not have an "expires_in", then it's considered expired - return true; - } - - // If the token is set to expire in the next 30 seconds. - return ($created + ($this->token['expires_in'] - 30)) < time(); - } - - /** - * @deprecated See UPGRADING.md for more information - */ - public function getAuth() - { - throw new BadMethodCallException( - 'This function no longer exists. See UPGRADING.md for more information' - ); - } - - /** - * @deprecated See UPGRADING.md for more information - */ - public function setAuth($auth) - { - throw new BadMethodCallException( - 'This function no longer exists. See UPGRADING.md for more information' - ); - } - - /** - * Set the OAuth 2.0 Client ID. - * @param string $clientId - */ - public function setClientId($clientId) - { - $this->config['client_id'] = $clientId; - } - - public function getClientId() - { - return $this->config['client_id']; - } - - /** - * Set the OAuth 2.0 Client Secret. - * @param string $clientSecret - */ - public function setClientSecret($clientSecret) - { - $this->config['client_secret'] = $clientSecret; - } - - public function getClientSecret() - { - return $this->config['client_secret']; - } - - /** - * Set the OAuth 2.0 Redirect URI. - * @param string $redirectUri - */ - public function setRedirectUri($redirectUri) - { - $this->config['redirect_uri'] = $redirectUri; - } - - public function getRedirectUri() - { - return $this->config['redirect_uri']; - } - - /** - * Set OAuth 2.0 "state" parameter to achieve per-request customization. - * @see http://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-3.1.2.2 - * @param string $state - */ - public function setState($state) - { - $this->config['state'] = $state; - } - - /** - * @param string $accessType Possible values for access_type include: - * {@code "offline"} to request offline access from the user. - * {@code "online"} to request online access from the user. - */ - public function setAccessType($accessType) - { - $this->config['access_type'] = $accessType; - } - - /** - * @param string $approvalPrompt Possible values for approval_prompt include: - * {@code "force"} to force the approval UI to appear. - * {@code "auto"} to request auto-approval when possible. (This is the default value) - */ - public function setApprovalPrompt($approvalPrompt) - { - $this->config['approval_prompt'] = $approvalPrompt; - } - - /** - * Set the login hint, email address or sub id. - * @param string $loginHint - */ - public function setLoginHint($loginHint) - { - $this->config['login_hint'] = $loginHint; - } - - /** - * Set the application name, this is included in the User-Agent HTTP header. - * @param string $applicationName - */ - public function setApplicationName($applicationName) - { - $this->config['application_name'] = $applicationName; - } - - /** - * If 'plus.login' is included in the list of requested scopes, you can use - * this method to define types of app activities that your app will write. - * You can find a list of available types here: - * @link https://developers.google.com/+/api/moment-types - * - * @param array $requestVisibleActions Array of app activity types - */ - public function setRequestVisibleActions($requestVisibleActions) - { - if (is_array($requestVisibleActions)) { - $requestVisibleActions = implode(" ", $requestVisibleActions); - } - $this->config['request_visible_actions'] = $requestVisibleActions; - } - - /** - * Set the developer key to use, these are obtained through the API Console. - * @see http://code.google.com/apis/console-help/#generatingdevkeys - * @param string $developerKey - */ - public function setDeveloperKey($developerKey) - { - $this->config['developer_key'] = $developerKey; - } - - /** - * Set the hd (hosted domain) parameter streamlines the login process for - * Google Apps hosted accounts. By including the domain of the user, you - * restrict sign-in to accounts at that domain. - * @param $hd string - the domain to use. - */ - public function setHostedDomain($hd) - { - $this->config['hd'] = $hd; - } - - /** - * Set the prompt hint. Valid values are none, consent and select_account. - * If no value is specified and the user has not previously authorized - * access, then the user is shown a consent screen. - * @param $prompt string - * {@code "none"} Do not display any authentication or consent screens. Must not be specified with other values. - * {@code "consent"} Prompt the user for consent. - * {@code "select_account"} Prompt the user to select an account. - */ - public function setPrompt($prompt) - { - $this->config['prompt'] = $prompt; - } - - /** - * openid.realm is a parameter from the OpenID 2.0 protocol, not from OAuth - * 2.0. It is used in OpenID 2.0 requests to signify the URL-space for which - * an authentication request is valid. - * @param $realm string - the URL-space to use. - */ - public function setOpenidRealm($realm) - { - $this->config['openid.realm'] = $realm; - } - - /** - * If this is provided with the value true, and the authorization request is - * granted, the authorization will include any previous authorizations - * granted to this user/application combination for other scopes. - * @param $include boolean - the URL-space to use. - */ - public function setIncludeGrantedScopes($include) - { - $this->config['include_granted_scopes'] = $include; - } - - /** - * sets function to be called when an access token is fetched - * @param callable $tokenCallback - function ($cacheKey, $accessToken) - */ - public function setTokenCallback(callable $tokenCallback) - { - $this->config['token_callback'] = $tokenCallback; - } - - /** - * Revoke an OAuth2 access token or refresh token. This method will revoke the current access - * token, if a token isn't provided. - * - * @param string|array|null $token The token (access token or a refresh token) that should be revoked. - * @return boolean Returns True if the revocation was successful, otherwise False. - */ - public function revokeToken($token = null) - { - $tokenRevoker = new Revoke($this->getHttpClient()); - - return $tokenRevoker->revokeToken($token ?: $this->getAccessToken()); - } - - /** - * Verify an id_token. This method will verify the current id_token, if one - * isn't provided. - * - * @throws LogicException If no token was provided and no token was set using `setAccessToken`. - * @throws UnexpectedValueException If the token is not a valid JWT. - * @param string|null $idToken The token (id_token) that should be verified. - * @return array|false Returns the token payload as an array if the verification was - * successful, false otherwise. - */ - public function verifyIdToken($idToken = null) - { - $tokenVerifier = new Verify( - $this->getHttpClient(), - $this->getCache(), - $this->config['jwt'] - ); - - if (null === $idToken) { - $token = $this->getAccessToken(); - if (!isset($token['id_token'])) { - throw new LogicException( - 'id_token must be passed in or set as part of setAccessToken' + + return $tokenVerifier->verifyIdToken( + $idToken, + $this->getClientId() ); - } - $idToken = $token['id_token']; - } - - return $tokenVerifier->verifyIdToken( - $idToken, - $this->getClientId() - ); - } - - /** - * Set the scopes to be requested. Must be called before createAuthUrl(). - * Will remove any previously configured scopes. - * @param string|array $scope_or_scopes, ie: - * array( - * 'https://www.googleapis.com/auth/plus.login', - * 'https://www.googleapis.com/auth/moderator' - * ); - */ - public function setScopes($scope_or_scopes) - { - $this->requestedScopes = array(); - $this->addScope($scope_or_scopes); - } - - /** - * This functions adds a scope to be requested as part of the OAuth2.0 flow. - * Will append any scopes not previously requested to the scope parameter. - * A single string will be treated as a scope to request. An array of strings - * will each be appended. - * @param $scope_or_scopes string|array e.g. "profile" - */ - public function addScope($scope_or_scopes) - { - if (is_string($scope_or_scopes) && !in_array($scope_or_scopes, $this->requestedScopes)) { - $this->requestedScopes[] = $scope_or_scopes; - } else if (is_array($scope_or_scopes)) { - foreach ($scope_or_scopes as $scope) { - $this->addScope($scope); - } - } - } - - /** - * Returns the list of scopes requested by the client - * @return array the list of scopes - * - */ - public function getScopes() - { - return $this->requestedScopes; - } - - /** - * @return string|null - * @visible For Testing - */ - public function prepareScopes() - { - if (empty($this->requestedScopes)) { - return null; - } - - return implode(' ', $this->requestedScopes); - } - - /** - * Helper method to execute deferred HTTP requests. - * - * @param $request RequestInterface|\Google\Http\Batch - * @param string $expectedClass - * @throws \Google\Exception - * @return mixed|$expectedClass|ResponseInterface - */ - public function execute(RequestInterface $request, $expectedClass = null) - { - $request = $request - ->withHeader( - 'User-Agent', - sprintf( - '%s %s%s', - $this->config['application_name'], - self::USER_AGENT_SUFFIX, - $this->getLibraryVersion() - ) - ) - ->withHeader( - 'x-goog-api-client', - sprintf( - 'gl-php/%s gdcl/%s', - phpversion(), - $this->getLibraryVersion() + } + + /** + * Set the scopes to be requested. Must be called before createAuthUrl(). + * Will remove any previously configured scopes. + * @param string|array $scope_or_scopes, ie: + * array( + * 'https://www.googleapis.com/auth/plus.login', + * 'https://www.googleapis.com/auth/moderator' + * ); + */ + public function setScopes($scope_or_scopes) + { + $this->requestedScopes = array(); + $this->addScope($scope_or_scopes); + } + + /** + * This functions adds a scope to be requested as part of the OAuth2.0 flow. + * Will append any scopes not previously requested to the scope parameter. + * A single string will be treated as a scope to request. An array of strings + * will each be appended. + * @param $scope_or_scopes string|array e.g. "profile" + */ + public function addScope($scope_or_scopes) + { + if (is_string($scope_or_scopes) && !in_array($scope_or_scopes, $this->requestedScopes)) { + $this->requestedScopes[] = $scope_or_scopes; + } elseif (is_array($scope_or_scopes)) { + foreach ($scope_or_scopes as $scope) { + $this->addScope($scope); + } + } + } + + /** + * Returns the list of scopes requested by the client + * @return array the list of scopes + * + */ + public function getScopes() + { + return $this->requestedScopes; + } + + /** + * @return string|null + * @visible For Testing + */ + public function prepareScopes() + { + if (empty($this->requestedScopes)) { + return null; + } + + return implode(' ', $this->requestedScopes); + } + + /** + * Helper method to execute deferred HTTP requests. + * + * @param $request RequestInterface|\Google\Http\Batch + * @param string $expectedClass + * @throws \Google\Exception + * @return mixed|$expectedClass|ResponseInterface + */ + public function execute(RequestInterface $request, $expectedClass = null) + { + $request = $request + ->withHeader( + 'User-Agent', + sprintf( + '%s %s%s', + $this->config['application_name'], + self::USER_AGENT_SUFFIX, + $this->getLibraryVersion() + ) ) + ->withHeader( + 'x-goog-api-client', + sprintf( + 'gl-php/%s gdcl/%s', + phpversion(), + $this->getLibraryVersion() + ) + ); + + if ($this->config['api_format_v2']) { + $request = $request->withHeader( + 'X-GOOG-API-FORMAT-VERSION', + 2 + ); + } + + // call the authorize method + // this is where most of the grunt work is done + $http = $this->authorize(); + + return REST::execute( + $http, + $request, + $expectedClass, + $this->config['retry'], + $this->config['retry_map'] ); + } + + /** + * Declare whether batch calls should be used. This may increase throughput + * by making multiple requests in one connection. + * + * @param boolean $useBatch True if the batch support should + * be enabled. Defaults to False. + */ + public function setUseBatch($useBatch) + { + // This is actually an alias for setDefer. + $this->setDefer($useBatch); + } + + /** + * Are we running in Google AppEngine? + * return bool + */ + public function isAppEngine() + { + return (isset($_SERVER['SERVER_SOFTWARE']) && + strpos($_SERVER['SERVER_SOFTWARE'], 'Google App Engine') !== false); + } + + public function setConfig($name, $value) + { + $this->config[$name] = $value; + } + + public function getConfig($name, $default = null) + { + return isset($this->config[$name]) ? $this->config[$name] : $default; + } + + /** + * For backwards compatibility + * alias for setAuthConfig + * + * @param string $file the configuration file + * @throws \Google\Exception + * @deprecated + */ + public function setAuthConfigFile($file) + { + $this->setAuthConfig($file); + } + + /** + * Set the auth config from new or deprecated JSON config. + * This structure should match the file downloaded from + * the "Download JSON" button on in the Google Developer + * Console. + * @param string|array $config the configuration json + * @throws \Google\Exception + */ + public function setAuthConfig($config) + { + if (is_string($config)) { + if (!file_exists($config)) { + throw new InvalidArgumentException(sprintf('file "%s" does not exist', $config)); + } + + $json = file_get_contents($config); + + if (!$config = json_decode($json, true)) { + throw new LogicException('invalid json for auth config'); + } + } + + $key = isset($config['installed']) ? 'installed' : 'web'; + if (isset($config['type']) && $config['type'] == 'service_account') { + // application default credentials + $this->useApplicationDefaultCredentials(); + + // set the information from the config + $this->setClientId($config['client_id']); + $this->config['client_email'] = $config['client_email']; + $this->config['signing_key'] = $config['private_key']; + $this->config['signing_algorithm'] = 'HS256'; + } elseif (isset($config[$key])) { + // old-style + $this->setClientId($config[$key]['client_id']); + $this->setClientSecret($config[$key]['client_secret']); + if (isset($config[$key]['redirect_uris'])) { + $this->setRedirectUri($config[$key]['redirect_uris'][0]); + } + } else { + // new-style + $this->setClientId($config['client_id']); + $this->setClientSecret($config['client_secret']); + if (isset($config['redirect_uris'])) { + $this->setRedirectUri($config['redirect_uris'][0]); + } + } + } + + /** + * Use when the service account has been delegated domain wide access. + * + * @param string $subject an email address account to impersonate + */ + public function setSubject($subject) + { + $this->config['subject'] = $subject; + } + + /** + * Declare whether making API calls should make the call immediately, or + * return a request which can be called with ->execute(); + * + * @param boolean $defer True if calls should not be executed right away. + */ + public function setDefer($defer) + { + $this->deferExecution = $defer; + } - if ($this->config['api_format_v2']) { - $request = $request->withHeader( - 'X-GOOG-API-FORMAT-VERSION', - 2 + /** + * Whether or not to return raw requests + * @return boolean + */ + public function shouldDefer() + { + return $this->deferExecution; + } + + /** + * @return OAuth2 implementation + */ + public function getOAuth2Service() + { + if (!isset($this->auth)) { + $this->auth = $this->createOAuth2Service(); + } + + return $this->auth; + } + + /** + * create a default google auth object + */ + protected function createOAuth2Service() + { + $auth = new OAuth2([ + 'clientId' => $this->getClientId(), + 'clientSecret' => $this->getClientSecret(), + 'authorizationUri' => self::OAUTH2_AUTH_URL, + 'tokenCredentialUri' => self::OAUTH2_TOKEN_URI, + 'redirectUri' => $this->getRedirectUri(), + 'issuer' => $this->config['client_id'], + 'signingKey' => $this->config['signing_key'], + 'signingAlgorithm' => $this->config['signing_algorithm'], + ]); + + return $auth; + } + + /** + * Set the Cache object + * @param CacheItemPoolInterface $cache + */ + public function setCache(CacheItemPoolInterface $cache) + { + $this->cache = $cache; + } + + /** + * @return CacheItemPoolInterface + */ + public function getCache() + { + if (!$this->cache) { + $this->cache = $this->createDefaultCache(); + } + + return $this->cache; + } + + /** + * @param array $cacheConfig + */ + public function setCacheConfig(array $cacheConfig) + { + $this->config['cache_config'] = $cacheConfig; + } + + /** + * Set the Logger object + * @param LoggerInterface $logger + */ + public function setLogger(LoggerInterface $logger) + { + $this->logger = $logger; + } + + /** + * @return LoggerInterface + */ + public function getLogger() + { + if (!isset($this->logger)) { + $this->logger = $this->createDefaultLogger(); + } + + return $this->logger; + } + + protected function createDefaultLogger() + { + $logger = new Logger('google-api-php-client'); + if ($this->isAppEngine()) { + $handler = new MonologSyslogHandler('app', LOG_USER, Logger::NOTICE); + } else { + $handler = new MonologStreamHandler('php://stderr', Logger::NOTICE); + } + $logger->pushHandler($handler); + + return $logger; + } + + protected function createDefaultCache() + { + return new MemoryCacheItemPool; + } + + /** + * Set the Http Client object + * @param ClientInterface $http + */ + public function setHttpClient(ClientInterface $http) + { + $this->http = $http; + } + + /** + * @return ClientInterface + */ + public function getHttpClient() + { + if (null === $this->http) { + $this->http = $this->createDefaultHttpClient(); + } + + return $this->http; + } + + /** + * Set the API format version. + * + * `true` will use V2, which may return more useful error messages. + * + * @param bool $value + */ + public function setApiFormatV2($value) + { + $this->config['api_format_v2'] = (bool) $value; + } + + protected function createDefaultHttpClient() + { + $guzzleVersion = null; + if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) { + $guzzleVersion = ClientInterface::MAJOR_VERSION; + } elseif (defined('\GuzzleHttp\ClientInterface::VERSION')) { + $guzzleVersion = (int)substr(ClientInterface::VERSION, 0, 1); + } + + if (5 === $guzzleVersion) { + $options = [ + 'base_url' => $this->config['base_path'], + 'defaults' => ['exceptions' => false], + ]; + if ($this->isAppEngine()) { + // set StreamHandler on AppEngine by default + $options['handler'] = new StreamHandler(); + $options['defaults']['verify'] = '/etc/ca-certificates.crt'; + } + } elseif (6 === $guzzleVersion || 7 === $guzzleVersion) { + // guzzle 6 or 7 + $options = [ + 'base_uri' => $this->config['base_path'], + 'http_errors' => false, + ]; + } else { + throw new LogicException('Could not find supported version of Guzzle.'); + } + + return new GuzzleClient($options); + } + + /** + * @return FetchAuthTokenCache + */ + private function createApplicationDefaultCredentials() + { + $scopes = $this->prepareScopes(); + $sub = $this->config['subject']; + $signingKey = $this->config['signing_key']; + + // create credentials using values supplied in setAuthConfig + if ($signingKey) { + $serviceAccountCredentials = array( + 'client_id' => $this->config['client_id'], + 'client_email' => $this->config['client_email'], + 'private_key' => $signingKey, + 'type' => 'service_account', + 'quota_project_id' => $this->config['quota_project'], + ); + $credentials = CredentialsLoader::makeCredentials( + $scopes, + $serviceAccountCredentials + ); + } else { + // When $sub is provided, we cannot pass cache classes to ::getCredentials + // because FetchAuthTokenCache::setSub does not exist. + // The result is when $sub is provided, calls to ::onGce are not cached. + $credentials = ApplicationDefaultCredentials::getCredentials( + $scopes, + null, + $sub ? null : $this->config['cache_config'], + $sub ? null : $this->getCache(), + $this->config['quota_project'] + ); + } + + // for service account domain-wide authority (impersonating a user) + // @see https://developers.google.com/identity/protocols/OAuth2ServiceAccount + if ($sub) { + if (!$credentials instanceof ServiceAccountCredentials) { + throw new DomainException('domain-wide authority requires service account credentials'); + } + + $credentials->setSub($sub); + } + + // If we are not using FetchAuthTokenCache yet, create it now + if (!$credentials instanceof FetchAuthTokenCache) { + $credentials = new FetchAuthTokenCache( + $credentials, + $this->config['cache_config'], + $this->getCache() + ); + } + return $credentials; + } + + protected function getAuthHandler() + { + // Be very careful using the cache, as the underlying auth library's cache + // implementation is naive, and the cache keys do not account for user + // sessions. + // + // @see https://github.com/google/google-api-php-client/issues/821 + return AuthHandlerFactory::build( + $this->getCache(), + $this->config['cache_config'] ); } - // call the authorize method - // this is where most of the grunt work is done - $http = $this->authorize(); - - return REST::execute( - $http, - $request, - $expectedClass, - $this->config['retry'], - $this->config['retry_map'] - ); - } - - /** - * Declare whether batch calls should be used. This may increase throughput - * by making multiple requests in one connection. - * - * @param boolean $useBatch True if the batch support should - * be enabled. Defaults to False. - */ - public function setUseBatch($useBatch) - { - // This is actually an alias for setDefer. - $this->setDefer($useBatch); - } - - /** - * Are we running in Google AppEngine? - * return bool - */ - public function isAppEngine() - { - return (isset($_SERVER['SERVER_SOFTWARE']) && - strpos($_SERVER['SERVER_SOFTWARE'], 'Google App Engine') !== false); - } - - public function setConfig($name, $value) - { - $this->config[$name] = $value; - } - - public function getConfig($name, $default = null) - { - return isset($this->config[$name]) ? $this->config[$name] : $default; - } - - /** - * For backwards compatibility - * alias for setAuthConfig - * - * @param string $file the configuration file - * @throws \Google\Exception - * @deprecated - */ - public function setAuthConfigFile($file) - { - $this->setAuthConfig($file); - } - - /** - * Set the auth config from new or deprecated JSON config. - * This structure should match the file downloaded from - * the "Download JSON" button on in the Google Developer - * Console. - * @param string|array $config the configuration json - * @throws \Google\Exception - */ - public function setAuthConfig($config) - { - if (is_string($config)) { - if (!file_exists($config)) { - throw new InvalidArgumentException(sprintf('file "%s" does not exist', $config)); - } - - $json = file_get_contents($config); - - if (!$config = json_decode($json, true)) { - throw new LogicException('invalid json for auth config'); - } - } - - $key = isset($config['installed']) ? 'installed' : 'web'; - if (isset($config['type']) && $config['type'] == 'service_account') { - // application default credentials - $this->useApplicationDefaultCredentials(); - - // set the information from the config - $this->setClientId($config['client_id']); - $this->config['client_email'] = $config['client_email']; - $this->config['signing_key'] = $config['private_key']; - $this->config['signing_algorithm'] = 'HS256'; - } elseif (isset($config[$key])) { - // old-style - $this->setClientId($config[$key]['client_id']); - $this->setClientSecret($config[$key]['client_secret']); - if (isset($config[$key]['redirect_uris'])) { - $this->setRedirectUri($config[$key]['redirect_uris'][0]); - } - } else { - // new-style - $this->setClientId($config['client_id']); - $this->setClientSecret($config['client_secret']); - if (isset($config['redirect_uris'])) { - $this->setRedirectUri($config['redirect_uris'][0]); - } - } - } - - /** - * Use when the service account has been delegated domain wide access. - * - * @param string $subject an email address account to impersonate - */ - public function setSubject($subject) - { - $this->config['subject'] = $subject; - } - - /** - * Declare whether making API calls should make the call immediately, or - * return a request which can be called with ->execute(); - * - * @param boolean $defer True if calls should not be executed right away. - */ - public function setDefer($defer) - { - $this->deferExecution = $defer; - } - - /** - * Whether or not to return raw requests - * @return boolean - */ - public function shouldDefer() - { - return $this->deferExecution; - } - - /** - * @return OAuth2 implementation - */ - public function getOAuth2Service() - { - if (!isset($this->auth)) { - $this->auth = $this->createOAuth2Service(); - } - - return $this->auth; - } - - /** - * create a default google auth object - */ - protected function createOAuth2Service() - { - $auth = new OAuth2( - [ - 'clientId' => $this->getClientId(), - 'clientSecret' => $this->getClientSecret(), - 'authorizationUri' => self::OAUTH2_AUTH_URL, - 'tokenCredentialUri' => self::OAUTH2_TOKEN_URI, - 'redirectUri' => $this->getRedirectUri(), - 'issuer' => $this->config['client_id'], - 'signingKey' => $this->config['signing_key'], - 'signingAlgorithm' => $this->config['signing_algorithm'], - ] - ); - - return $auth; - } - - /** - * Set the Cache object - * @param CacheItemPoolInterface $cache - */ - public function setCache(CacheItemPoolInterface $cache) - { - $this->cache = $cache; - } - - /** - * @return CacheItemPoolInterface - */ - public function getCache() - { - if (!$this->cache) { - $this->cache = $this->createDefaultCache(); - } - - return $this->cache; - } - - /** - * @param array $cacheConfig - */ - public function setCacheConfig(array $cacheConfig) - { - $this->config['cache_config'] = $cacheConfig; - } - - /** - * Set the Logger object - * @param LoggerInterface $logger - */ - public function setLogger(LoggerInterface $logger) - { - $this->logger = $logger; - } - - /** - * @return LoggerInterface - */ - public function getLogger() - { - if (!isset($this->logger)) { - $this->logger = $this->createDefaultLogger(); - } - - return $this->logger; - } - - protected function createDefaultLogger() - { - $logger = new Logger('google-api-php-client'); - if ($this->isAppEngine()) { - $handler = new MonologSyslogHandler('app', LOG_USER, Logger::NOTICE); - } else { - $handler = new MonologStreamHandler('php://stderr', Logger::NOTICE); - } - $logger->pushHandler($handler); - - return $logger; - } - - protected function createDefaultCache() - { - return new MemoryCacheItemPool; - } - - /** - * Set the Http Client object - * @param ClientInterface $http - */ - public function setHttpClient(ClientInterface $http) - { - $this->http = $http; - } - - /** - * @return ClientInterface - */ - public function getHttpClient() - { - if (null === $this->http) { - $this->http = $this->createDefaultHttpClient(); - } - - return $this->http; - } - - /** - * Set the API format version. - * - * `true` will use V2, which may return more useful error messages. - * - * @param bool $value - */ - public function setApiFormatV2($value) - { - $this->config['api_format_v2'] = (bool) $value; - } - - protected function createDefaultHttpClient() - { - $guzzleVersion = null; - if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) { - $guzzleVersion = ClientInterface::MAJOR_VERSION; - } elseif (defined('\GuzzleHttp\ClientInterface::VERSION')) { - $guzzleVersion = (int)substr(ClientInterface::VERSION, 0, 1); - } - - if (5 === $guzzleVersion) { - $options = [ - 'base_url' => $this->config['base_path'], - 'defaults' => ['exceptions' => false], - ]; - if ($this->isAppEngine()) { - // set StreamHandler on AppEngine by default - $options['handler'] = new StreamHandler(); - $options['defaults']['verify'] = '/etc/ca-certificates.crt'; - } - } elseif (6 === $guzzleVersion || 7 === $guzzleVersion) { - // guzzle 6 or 7 - $options = [ - 'base_uri' => $this->config['base_path'], - 'http_errors' => false, - ]; - } else { - throw new LogicException('Could not find supported version of Guzzle.'); - } - - return new GuzzleClient($options); - } - - /** - * @return FetchAuthTokenCache - */ - private function createApplicationDefaultCredentials() - { - $scopes = $this->prepareScopes(); - $sub = $this->config['subject']; - $signingKey = $this->config['signing_key']; - - // create credentials using values supplied in setAuthConfig - if ($signingKey) { - $serviceAccountCredentials = array( - 'client_id' => $this->config['client_id'], - 'client_email' => $this->config['client_email'], - 'private_key' => $signingKey, - 'type' => 'service_account', - 'quota_project_id' => $this->config['quota_project'], - ); - $credentials = CredentialsLoader::makeCredentials( - $scopes, - $serviceAccountCredentials - ); - } else { - // When $sub is provided, we cannot pass cache classes to ::getCredentials - // because FetchAuthTokenCache::setSub does not exist. - // The result is when $sub is provided, calls to ::onGce are not cached. - $credentials = ApplicationDefaultCredentials::getCredentials( - $scopes, - null, - $sub ? null : $this->config['cache_config'], - $sub ? null : $this->getCache(), - $this->config['quota_project'] - ); - } - - // for service account domain-wide authority (impersonating a user) - // @see https://developers.google.com/identity/protocols/OAuth2ServiceAccount - if ($sub) { - if (!$credentials instanceof ServiceAccountCredentials) { - throw new DomainException('domain-wide authority requires service account credentials'); - } - - $credentials->setSub($sub); - } - - // If we are not using FetchAuthTokenCache yet, create it now - if (!$credentials instanceof FetchAuthTokenCache) { - $credentials = new FetchAuthTokenCache( - $credentials, - $this->config['cache_config'], - $this->getCache() - ); - } - return $credentials; - } - - protected function getAuthHandler() - { - // Be very careful using the cache, as the underlying auth library's cache - // implementation is naive, and the cache keys do not account for user - // sessions. - // - // @see https://github.com/google/google-api-php-client/issues/821 - return AuthHandlerFactory::build( - $this->getCache(), - $this->config['cache_config'] - ); - } - - private function createUserRefreshCredentials($scope, $refreshToken) - { - $creds = array_filter( - array( - 'client_id' => $this->getClientId(), - 'client_secret' => $this->getClientSecret(), - 'refresh_token' => $refreshToken, - ) - ); - - return new UserRefreshCredentials($scope, $creds); - } + private function createUserRefreshCredentials($scope, $refreshToken) + { + $creds = array_filter( + array( + 'client_id' => $this->getClientId(), + 'client_secret' => $this->getClientSecret(), + 'refresh_token' => $refreshToken, + ) + ); + + return new UserRefreshCredentials($scope, $creds); + } } diff --git a/src/Collection.php b/src/Collection.php index 1d653c80d..39ebccaa7 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -9,106 +9,110 @@ */ class Collection extends Model implements \Iterator, \Countable { - protected $collection_key = 'items'; + protected $collection_key = 'items'; - /** @return void */ - #[\ReturnTypeWillChange] - public function rewind() - { - if (isset($this->{$this->collection_key}) - && is_array($this->{$this->collection_key})) { - reset($this->{$this->collection_key}); + /** @return void */ + #[\ReturnTypeWillChange] + public function rewind() + { + if ( + isset($this->{$this->collection_key}) + && is_array($this->{$this->collection_key}) + ) { + reset($this->{$this->collection_key}); + } } - } - /** @return mixed */ - #[\ReturnTypeWillChange] - public function current() - { - $this->coerceType($this->key()); - if (is_array($this->{$this->collection_key})) { - return current($this->{$this->collection_key}); + /** @return mixed */ + #[\ReturnTypeWillChange] + public function current() + { + $this->coerceType($this->key()); + if (is_array($this->{$this->collection_key})) { + return current($this->{$this->collection_key}); + } } - } - /** @return mixed */ - #[\ReturnTypeWillChange] - public function key() - { - if (isset($this->{$this->collection_key}) - && is_array($this->{$this->collection_key})) { - return key($this->{$this->collection_key}); + /** @return mixed */ + #[\ReturnTypeWillChange] + public function key() + { + if ( + isset($this->{$this->collection_key}) + && is_array($this->{$this->collection_key}) + ) { + return key($this->{$this->collection_key}); + } } - } - /** @return void */ - #[\ReturnTypeWillChange] - public function next() - { - return next($this->{$this->collection_key}); - } + /** @return void */ + #[\ReturnTypeWillChange] + public function next() + { + return next($this->{$this->collection_key}); + } - /** @return bool */ - #[\ReturnTypeWillChange] - public function valid() - { - $key = $this->key(); - return $key !== null && $key !== false; - } + /** @return bool */ + #[\ReturnTypeWillChange] + public function valid() + { + $key = $this->key(); + return $key !== null && $key !== false; + } - /** @return int */ - #[\ReturnTypeWillChange] - public function count() - { - if (!isset($this->{$this->collection_key})) { - return 0; + /** @return int */ + #[\ReturnTypeWillChange] + public function count() + { + if (!isset($this->{$this->collection_key})) { + return 0; + } + return count($this->{$this->collection_key}); } - return count($this->{$this->collection_key}); - } - /** @return bool */ - public function offsetExists($offset) - { - if (!is_numeric($offset)) { - return parent::offsetExists($offset); + /** @return bool */ + public function offsetExists($offset) + { + if (!is_numeric($offset)) { + return parent::offsetExists($offset); + } + return isset($this->{$this->collection_key}[$offset]); } - return isset($this->{$this->collection_key}[$offset]); - } - /** @return mixed */ - public function offsetGet($offset) - { - if (!is_numeric($offset)) { - return parent::offsetGet($offset); + /** @return mixed */ + public function offsetGet($offset) + { + if (!is_numeric($offset)) { + return parent::offsetGet($offset); + } + $this->coerceType($offset); + return $this->{$this->collection_key}[$offset]; } - $this->coerceType($offset); - return $this->{$this->collection_key}[$offset]; - } - /** @return void */ - public function offsetSet($offset, $value) - { - if (!is_numeric($offset)) { - parent::offsetSet($offset, $value); + /** @return void */ + public function offsetSet($offset, $value) + { + if (!is_numeric($offset)) { + parent::offsetSet($offset, $value); + } + $this->{$this->collection_key}[$offset] = $value; } - $this->{$this->collection_key}[$offset] = $value; - } - /** @return void */ - public function offsetUnset($offset) - { - if (!is_numeric($offset)) { - parent::offsetUnset($offset); + /** @return void */ + public function offsetUnset($offset) + { + if (!is_numeric($offset)) { + parent::offsetUnset($offset); + } + unset($this->{$this->collection_key}[$offset]); } - unset($this->{$this->collection_key}[$offset]); - } - private function coerceType($offset) - { - $keyType = $this->keyType($this->collection_key); - if ($keyType && !is_object($this->{$this->collection_key}[$offset])) { - $this->{$this->collection_key}[$offset] = - new $keyType($this->{$this->collection_key}[$offset]); + private function coerceType($offset) + { + $keyType = $this->keyType($this->collection_key); + if ($keyType && !is_object($this->{$this->collection_key}[$offset])) { + $this->{$this->collection_key}[$offset] = + new $keyType($this->{$this->collection_key}[$offset]); + } } - } } diff --git a/src/Http/Batch.php b/src/Http/Batch.php index d27d5cd85..eb797582e 100644 --- a/src/Http/Batch.php +++ b/src/Http/Batch.php @@ -35,52 +35,52 @@ */ class Batch { - const BATCH_PATH = 'batch'; - - private static $CONNECTION_ESTABLISHED_HEADERS = array( - "HTTP/1.0 200 Connection established\r\n\r\n", - "HTTP/1.1 200 Connection established\r\n\r\n", - ); - - /** @var string Multipart Boundary. */ - private $boundary; - - /** @var array service requests to be executed. */ - private $requests = array(); - - /** @var Client */ - private $client; - - private $rootUrl; - - private $batchPath; - - public function __construct( - Client $client, - $boundary = false, - $rootUrl = null, - $batchPath = null - ) { - $this->client = $client; - $this->boundary = $boundary ?: mt_rand(); - $this->rootUrl = rtrim($rootUrl ?: $this->client->getConfig('base_path'), '/'); - $this->batchPath = $batchPath ?: self::BATCH_PATH; - } - - public function add(RequestInterface $request, $key = false) - { - if (false == $key) { - $key = mt_rand(); + const BATCH_PATH = 'batch'; + + private static $CONNECTION_ESTABLISHED_HEADERS = array( + "HTTP/1.0 200 Connection established\r\n\r\n", + "HTTP/1.1 200 Connection established\r\n\r\n", + ); + + /** @var string Multipart Boundary. */ + private $boundary; + + /** @var array service requests to be executed. */ + private $requests = array(); + + /** @var Client */ + private $client; + + private $rootUrl; + + private $batchPath; + + public function __construct( + Client $client, + $boundary = false, + $rootUrl = null, + $batchPath = null + ) { + $this->client = $client; + $this->boundary = $boundary ?: mt_rand(); + $this->rootUrl = rtrim($rootUrl ?: $this->client->getConfig('base_path'), '/'); + $this->batchPath = $batchPath ?: self::BATCH_PATH; } - $this->requests[$key] = $request; - } + public function add(RequestInterface $request, $key = false) + { + if (false == $key) { + $key = mt_rand(); + } + + $this->requests[$key] = $request; + } - public function execute() - { - $body = ''; - $classes = array(); - $batchHttpTemplate = <<requests as $key => $request) { - $firstLine = sprintf( - '%s %s HTTP/%s', - $request->getMethod(), - $request->getRequestTarget(), - $request->getProtocolVersion() - ); - - $content = (string) $request->getBody(); - - $headers = ''; - foreach ($request->getHeaders() as $name => $values) { - $headers .= sprintf("%s:%s\r\n", $name, implode(', ', $values)); - } - - $body .= sprintf( - $batchHttpTemplate, - $this->boundary, - $key, - $firstLine, - $headers, - $content ? "\n".$content : '' - ); - - $classes['response-' . $key] = $request->getHeaderLine('X-Php-Expected-Class'); - } + /** @var RequestInterface $req */ + foreach ($this->requests as $key => $request) { + $firstLine = sprintf( + '%s %s HTTP/%s', + $request->getMethod(), + $request->getRequestTarget(), + $request->getProtocolVersion() + ); + + $content = (string) $request->getBody(); + + $headers = ''; + foreach ($request->getHeaders() as $name => $values) { + $headers .= sprintf("%s:%s\r\n", $name, implode(', ', $values)); + } + + $body .= sprintf( + $batchHttpTemplate, + $this->boundary, + $key, + $firstLine, + $headers, + $content ? "\n".$content : '' + ); + + $classes['response-' . $key] = $request->getHeaderLine('X-Php-Expected-Class'); + } - $body .= "--{$this->boundary}--"; - $body = trim($body); - $url = $this->rootUrl . '/' . $this->batchPath; - $headers = array( - 'Content-Type' => sprintf('multipart/mixed; boundary=%s', $this->boundary), - 'Content-Length' => strlen($body), - ); + $body .= "--{$this->boundary}--"; + $body = trim($body); + $url = $this->rootUrl . '/' . $this->batchPath; + $headers = array( + 'Content-Type' => sprintf('multipart/mixed; boundary=%s', $this->boundary), + 'Content-Length' => strlen($body), + ); - $request = new Request( - 'POST', - $url, - $headers, - $body - ); + $request = new Request( + 'POST', + $url, + $headers, + $body + ); - $response = $this->client->execute($request); - - return $this->parseResponse($response, $classes); - } - - public function parseResponse(ResponseInterface $response, $classes = array()) - { - $contentType = $response->getHeaderLine('content-type'); - $contentType = explode(';', $contentType); - $boundary = false; - foreach ($contentType as $part) { - $part = explode('=', $part, 2); - if (isset($part[0]) && 'boundary' == trim($part[0])) { - $boundary = $part[1]; - } + $response = $this->client->execute($request); + + return $this->parseResponse($response, $classes); } - $body = (string) $response->getBody(); - if (!empty($body)) { - $body = str_replace("--$boundary--", "--$boundary", $body); - $parts = explode("--$boundary", $body); - $responses = array(); - $requests = array_values($this->requests); - - foreach ($parts as $i => $part) { - $part = trim($part); - if (!empty($part)) { - list($rawHeaders, $part) = explode("\r\n\r\n", $part, 2); - $headers = $this->parseRawHeaders($rawHeaders); - - $status = substr($part, 0, strpos($part, "\n")); - $status = explode(" ", $status); - $status = $status[1]; - - list($partHeaders, $partBody) = $this->parseHttpResponse($part, false); - $response = new Response( - $status, - $partHeaders, - Psr7\Utils::streamFor($partBody) - ); - - // Need content id. - $key = $headers['content-id']; - - try { - $response = REST::decodeHttpResponse($response, $requests[$i-1]); - } catch (GoogleServiceException $e) { - // Store the exception as the response, so successful responses - // can be processed. - $response = $e; - } - - $responses[$key] = $response; + public function parseResponse(ResponseInterface $response, $classes = array()) + { + $contentType = $response->getHeaderLine('content-type'); + $contentType = explode(';', $contentType); + $boundary = false; + foreach ($contentType as $part) { + $part = explode('=', $part, 2); + if (isset($part[0]) && 'boundary' == trim($part[0])) { + $boundary = $part[1]; + } } - } - return $responses; + $body = (string) $response->getBody(); + if (!empty($body)) { + $body = str_replace("--$boundary--", "--$boundary", $body); + $parts = explode("--$boundary", $body); + $responses = array(); + $requests = array_values($this->requests); + + foreach ($parts as $i => $part) { + $part = trim($part); + if (!empty($part)) { + list($rawHeaders, $part) = explode("\r\n\r\n", $part, 2); + $headers = $this->parseRawHeaders($rawHeaders); + + $status = substr($part, 0, strpos($part, "\n")); + $status = explode(" ", $status); + $status = $status[1]; + + list($partHeaders, $partBody) = $this->parseHttpResponse($part, false); + $response = new Response( + $status, + $partHeaders, + Psr7\Utils::streamFor($partBody) + ); + + // Need content id. + $key = $headers['content-id']; + + try { + $response = REST::decodeHttpResponse($response, $requests[$i-1]); + } catch (GoogleServiceException $e) { + // Store the exception as the response, so successful responses + // can be processed. + $response = $e; + } + + $responses[$key] = $response; + } + } + + return $responses; + } + + return null; } - return null; - } - - private function parseRawHeaders($rawHeaders) - { - $headers = array(); - $responseHeaderLines = explode("\r\n", $rawHeaders); - foreach ($responseHeaderLines as $headerLine) { - if ($headerLine && strpos($headerLine, ':') !== false) { - list($header, $value) = explode(': ', $headerLine, 2); - $header = strtolower($header); - if (isset($headers[$header])) { - $headers[$header] = array_merge((array)$headers[$header], (array)$value); - } else { - $headers[$header] = $value; + private function parseRawHeaders($rawHeaders) + { + $headers = array(); + $responseHeaderLines = explode("\r\n", $rawHeaders); + foreach ($responseHeaderLines as $headerLine) { + if ($headerLine && strpos($headerLine, ':') !== false) { + list($header, $value) = explode(': ', $headerLine, 2); + $header = strtolower($header); + if (isset($headers[$header])) { + $headers[$header] = array_merge((array)$headers[$header], (array)$value); + } else { + $headers[$header] = $value; + } + } } - } - } - return $headers; - } - - /** - * Used by the IO lib and also the batch processing. - * - * @param $respData - * @param $headerSize - * @return array - */ - private function parseHttpResponse($respData, $headerSize) - { - // check proxy header - foreach (self::$CONNECTION_ESTABLISHED_HEADERS as $established_header) { - if (stripos($respData, $established_header) !== false) { - // existed, remove it - $respData = str_ireplace($established_header, '', $respData); - // Subtract the proxy header size unless the cURL bug prior to 7.30.0 - // is present which prevented the proxy header size from being taken into - // account. - // @TODO look into this - // if (!$this->needsQuirk()) { - // $headerSize -= strlen($established_header); - // } - break; - } + return $headers; } - if ($headerSize) { - $responseBody = substr($respData, $headerSize); - $responseHeaders = substr($respData, 0, $headerSize); - } else { - $responseSegments = explode("\r\n\r\n", $respData, 2); - $responseHeaders = $responseSegments[0]; - $responseBody = isset($responseSegments[1]) ? $responseSegments[1] : - null; - } + /** + * Used by the IO lib and also the batch processing. + * + * @param $respData + * @param $headerSize + * @return array + */ + private function parseHttpResponse($respData, $headerSize) + { + // check proxy header + foreach (self::$CONNECTION_ESTABLISHED_HEADERS as $established_header) { + if (stripos($respData, $established_header) !== false) { + // existed, remove it + $respData = str_ireplace($established_header, '', $respData); + // Subtract the proxy header size unless the cURL bug prior to 7.30.0 + // is present which prevented the proxy header size from being taken into + // account. + // @TODO look into this + // if (!$this->needsQuirk()) { + // $headerSize -= strlen($established_header); + // } + break; + } + } + + if ($headerSize) { + $responseBody = substr($respData, $headerSize); + $responseHeaders = substr($respData, 0, $headerSize); + } else { + $responseSegments = explode("\r\n\r\n", $respData, 2); + $responseHeaders = $responseSegments[0]; + $responseBody = isset($responseSegments[1]) ? $responseSegments[1] : null; + } - $responseHeaders = $this->parseRawHeaders($responseHeaders); + $responseHeaders = $this->parseRawHeaders($responseHeaders); - return array($responseHeaders, $responseBody); - } + return array($responseHeaders, $responseBody); + } } diff --git a/src/Http/MediaFileUpload.php b/src/Http/MediaFileUpload.php index 15529bca7..edb73ab93 100644 --- a/src/Http/MediaFileUpload.php +++ b/src/Http/MediaFileUpload.php @@ -31,328 +31,327 @@ */ class MediaFileUpload { - const UPLOAD_MEDIA_TYPE = 'media'; - const UPLOAD_MULTIPART_TYPE = 'multipart'; - const UPLOAD_RESUMABLE_TYPE = 'resumable'; + const UPLOAD_MEDIA_TYPE = 'media'; + const UPLOAD_MULTIPART_TYPE = 'multipart'; + const UPLOAD_RESUMABLE_TYPE = 'resumable'; - /** @var string $mimeType */ - private $mimeType; + /** @var string $mimeType */ + private $mimeType; - /** @var string $data */ - private $data; + /** @var string $data */ + private $data; - /** @var bool $resumable */ - private $resumable; + /** @var bool $resumable */ + private $resumable; - /** @var int $chunkSize */ - private $chunkSize; + /** @var int $chunkSize */ + private $chunkSize; - /** @var int $size */ - private $size; + /** @var int $size */ + private $size; - /** @var string $resumeUri */ - private $resumeUri; + /** @var string $resumeUri */ + private $resumeUri; - /** @var int $progress */ - private $progress; + /** @var int $progress */ + private $progress; - /** @var Client */ - private $client; + /** @var Client */ + private $client; - /** @var RequestInterface */ - private $request; + /** @var RequestInterface */ + private $request; - /** @var string */ - private $boundary; + /** @var string */ + private $boundary; - /** + /** * Result code from last HTTP call * @var int */ - private $httpResultCode; - - /** - * @param Client $client - * @param RequestInterface $request - * @param string $mimeType - * @param string $data The bytes you want to upload. - * @param bool $resumable - * @param bool $chunkSize File will be uploaded in chunks of this many bytes. - * only used if resumable=True - */ - public function __construct( - Client $client, - RequestInterface $request, - $mimeType, - $data, - $resumable = false, - $chunkSize = false - ) { - $this->client = $client; - $this->request = $request; - $this->mimeType = $mimeType; - $this->data = $data; - $this->resumable = $resumable; - $this->chunkSize = $chunkSize; - $this->progress = 0; - - $this->process(); - } - - /** - * Set the size of the file that is being uploaded. - * @param $size - int file size in bytes - */ - public function setFileSize($size) - { - $this->size = $size; - } - - /** - * Return the progress on the upload - * @return int progress in bytes uploaded. - */ - public function getProgress() - { - return $this->progress; - } - - /** - * Send the next part of the file to upload. - * @param string|bool $chunk Optional. The next set of bytes to send. If false will - * use $data passed at construct time. - */ - public function nextChunk($chunk = false) - { - $resumeUri = $this->getResumeUri(); - - if (false == $chunk) { - $chunk = substr($this->data, $this->progress, $this->chunkSize); + private $httpResultCode; + + /** + * @param Client $client + * @param RequestInterface $request + * @param string $mimeType + * @param string $data The bytes you want to upload. + * @param bool $resumable + * @param bool $chunkSize File will be uploaded in chunks of this many bytes. + * only used if resumable=True + */ + public function __construct( + Client $client, + RequestInterface $request, + $mimeType, + $data, + $resumable = false, + $chunkSize = false + ) { + $this->client = $client; + $this->request = $request; + $this->mimeType = $mimeType; + $this->data = $data; + $this->resumable = $resumable; + $this->chunkSize = $chunkSize; + $this->progress = 0; + + $this->process(); } - $lastBytePos = $this->progress + strlen($chunk) - 1; - $headers = array( - 'content-range' => "bytes $this->progress-$lastBytePos/$this->size", - 'content-length' => strlen($chunk), - 'expect' => '', - ); - - $request = new Request( - 'PUT', - $resumeUri, - $headers, - Psr7\Utils::streamFor($chunk) - ); - - return $this->makePutRequest($request); - } - - /** - * Return the HTTP result code from the last call made. - * @return int code - */ - public function getHttpResultCode() - { - return $this->httpResultCode; - } - - /** - * Sends a PUT-Request to google drive and parses the response, - * setting the appropiate variables from the response() - * - * @param RequestInterface $request the Request which will be send - * - * @return false|mixed false when the upload is unfinished or the decoded http response - * - */ - private function makePutRequest(RequestInterface $request) - { - $response = $this->client->execute($request); - $this->httpResultCode = $response->getStatusCode(); - - if (308 == $this->httpResultCode) { - // Track the amount uploaded. - $range = $response->getHeaderLine('range'); - if ($range) { - $range_array = explode('-', $range); - $this->progress = $range_array[1] + 1; - } - - // Allow for changing upload URLs. - $location = $response->getHeaderLine('location'); - if ($location) { - $this->resumeUri = $location; - } - - // No problems, but upload not complete. - return false; + /** + * Set the size of the file that is being uploaded. + * @param $size - int file size in bytes + */ + public function setFileSize($size) + { + $this->size = $size; } - return REST::decodeHttpResponse($response, $this->request); - } - - /** - * Resume a previously unfinished upload - * @param $resumeUri the resume-URI of the unfinished, resumable upload. - */ - public function resume($resumeUri) - { - $this->resumeUri = $resumeUri; - $headers = array( - 'content-range' => "bytes */$this->size", - 'content-length' => 0, - ); - $httpRequest = new Request( - 'PUT', - $this->resumeUri, - $headers - ); - - return $this->makePutRequest($httpRequest); - } - - /** - * @return RequestInterface - * @visible for testing - */ - private function process() - { - $this->transformToUploadUrl(); - $request = $this->request; - - $postBody = ''; - $contentType = false; - - $meta = (string) $request->getBody(); - $meta = is_string($meta) ? json_decode($meta, true) : $meta; - - $uploadType = $this->getUploadType($meta); - $request = $request->withUri( - Uri::withQueryValue($request->getUri(), 'uploadType', $uploadType) - ); - - $mimeType = $this->mimeType ?: $request->getHeaderLine('content-type'); - - if (self::UPLOAD_RESUMABLE_TYPE == $uploadType) { - $contentType = $mimeType; - $postBody = is_string($meta) ? $meta : json_encode($meta); - } else if (self::UPLOAD_MEDIA_TYPE == $uploadType) { - $contentType = $mimeType; - $postBody = $this->data; - } else if (self::UPLOAD_MULTIPART_TYPE == $uploadType) { - // This is a multipart/related upload. - $boundary = $this->boundary ?: mt_rand(); - $boundary = str_replace('"', '', $boundary); - $contentType = 'multipart/related; boundary=' . $boundary; - $related = "--$boundary\r\n"; - $related .= "Content-Type: application/json; charset=UTF-8\r\n"; - $related .= "\r\n" . json_encode($meta) . "\r\n"; - $related .= "--$boundary\r\n"; - $related .= "Content-Type: $mimeType\r\n"; - $related .= "Content-Transfer-Encoding: base64\r\n"; - $related .= "\r\n" . base64_encode($this->data) . "\r\n"; - $related .= "--$boundary--"; - $postBody = $related; + /** + * Return the progress on the upload + * @return int progress in bytes uploaded. + */ + public function getProgress() + { + return $this->progress; } - $request = $request->withBody(Psr7\Utils::streamFor($postBody)); - - if (isset($contentType) && $contentType) { - $request = $request->withHeader('content-type', $contentType); + /** + * Send the next part of the file to upload. + * @param string|bool $chunk Optional. The next set of bytes to send. If false will + * use $data passed at construct time. + */ + public function nextChunk($chunk = false) + { + $resumeUri = $this->getResumeUri(); + + if (false == $chunk) { + $chunk = substr($this->data, $this->progress, $this->chunkSize); + } + + $lastBytePos = $this->progress + strlen($chunk) - 1; + $headers = array( + 'content-range' => "bytes $this->progress-$lastBytePos/$this->size", + 'content-length' => strlen($chunk), + 'expect' => '', + ); + + $request = new Request( + 'PUT', + $resumeUri, + $headers, + Psr7\Utils::streamFor($chunk) + ); + + return $this->makePutRequest($request); } - return $this->request = $request; - } - - /** - * Valid upload types: - * - resumable (UPLOAD_RESUMABLE_TYPE) - * - media (UPLOAD_MEDIA_TYPE) - * - multipart (UPLOAD_MULTIPART_TYPE) - * @param $meta - * @return string - * @visible for testing - */ - public function getUploadType($meta) - { - if ($this->resumable) { - return self::UPLOAD_RESUMABLE_TYPE; + /** + * Return the HTTP result code from the last call made. + * @return int code + */ + public function getHttpResultCode() + { + return $this->httpResultCode; } - if (false == $meta && $this->data) { - return self::UPLOAD_MEDIA_TYPE; + /** + * Sends a PUT-Request to google drive and parses the response, + * setting the appropiate variables from the response() + * + * @param RequestInterface $request the Request which will be send + * + * @return false|mixed false when the upload is unfinished or the decoded http response + * + */ + private function makePutRequest(RequestInterface $request) + { + $response = $this->client->execute($request); + $this->httpResultCode = $response->getStatusCode(); + + if (308 == $this->httpResultCode) { + // Track the amount uploaded. + $range = $response->getHeaderLine('range'); + if ($range) { + $range_array = explode('-', $range); + $this->progress = $range_array[1] + 1; + } + + // Allow for changing upload URLs. + $location = $response->getHeaderLine('location'); + if ($location) { + $this->resumeUri = $location; + } + + // No problems, but upload not complete. + return false; + } + + return REST::decodeHttpResponse($response, $this->request); } - return self::UPLOAD_MULTIPART_TYPE; - } + /** + * Resume a previously unfinished upload + * @param $resumeUri the resume-URI of the unfinished, resumable upload. + */ + public function resume($resumeUri) + { + $this->resumeUri = $resumeUri; + $headers = array( + 'content-range' => "bytes */$this->size", + 'content-length' => 0, + ); + $httpRequest = new Request( + 'PUT', + $this->resumeUri, + $headers + ); + return $this->makePutRequest($httpRequest); + } - public function getResumeUri() - { - if (null === $this->resumeUri) { - $this->resumeUri = $this->fetchResumeUri(); + /** + * @return RequestInterface + * @visible for testing + */ + private function process() + { + $this->transformToUploadUrl(); + $request = $this->request; + + $postBody = ''; + $contentType = false; + + $meta = (string) $request->getBody(); + $meta = is_string($meta) ? json_decode($meta, true) : $meta; + + $uploadType = $this->getUploadType($meta); + $request = $request->withUri( + Uri::withQueryValue($request->getUri(), 'uploadType', $uploadType) + ); + + $mimeType = $this->mimeType ?: $request->getHeaderLine('content-type'); + + if (self::UPLOAD_RESUMABLE_TYPE == $uploadType) { + $contentType = $mimeType; + $postBody = is_string($meta) ? $meta : json_encode($meta); + } else if (self::UPLOAD_MEDIA_TYPE == $uploadType) { + $contentType = $mimeType; + $postBody = $this->data; + } else if (self::UPLOAD_MULTIPART_TYPE == $uploadType) { + // This is a multipart/related upload. + $boundary = $this->boundary ?: mt_rand(); + $boundary = str_replace('"', '', $boundary); + $contentType = 'multipart/related; boundary=' . $boundary; + $related = "--$boundary\r\n"; + $related .= "Content-Type: application/json; charset=UTF-8\r\n"; + $related .= "\r\n" . json_encode($meta) . "\r\n"; + $related .= "--$boundary\r\n"; + $related .= "Content-Type: $mimeType\r\n"; + $related .= "Content-Transfer-Encoding: base64\r\n"; + $related .= "\r\n" . base64_encode($this->data) . "\r\n"; + $related .= "--$boundary--"; + $postBody = $related; + } + + $request = $request->withBody(Psr7\Utils::streamFor($postBody)); + + if (isset($contentType) && $contentType) { + $request = $request->withHeader('content-type', $contentType); + } + + return $this->request = $request; } - return $this->resumeUri; - } - - private function fetchResumeUri() - { - $body = $this->request->getBody(); - if ($body) { - $headers = array( - 'content-type' => 'application/json; charset=UTF-8', - 'content-length' => $body->getSize(), - 'x-upload-content-type' => $this->mimeType, - 'x-upload-content-length' => $this->size, - 'expect' => '', - ); - foreach ($headers as $key => $value) { - $this->request = $this->request->withHeader($key, $value); - } + /** + * Valid upload types: + * - resumable (UPLOAD_RESUMABLE_TYPE) + * - media (UPLOAD_MEDIA_TYPE) + * - multipart (UPLOAD_MULTIPART_TYPE) + * @param $meta + * @return string + * @visible for testing + */ + public function getUploadType($meta) + { + if ($this->resumable) { + return self::UPLOAD_RESUMABLE_TYPE; + } + + if (false == $meta && $this->data) { + return self::UPLOAD_MEDIA_TYPE; + } + + return self::UPLOAD_MULTIPART_TYPE; } - $response = $this->client->execute($this->request, false); - $location = $response->getHeaderLine('location'); - $code = $response->getStatusCode(); + public function getResumeUri() + { + if (null === $this->resumeUri) { + $this->resumeUri = $this->fetchResumeUri(); + } - if (200 == $code && true == $location) { - return $location; + return $this->resumeUri; } - $message = $code; - $body = json_decode((string) $this->request->getBody(), true); - if (isset($body['error']['errors'])) { - $message .= ': '; - foreach ($body['error']['errors'] as $error) { - $message .= "{$error['domain']}, {$error['message']};"; - } - $message = rtrim($message, ';'); + private function fetchResumeUri() + { + $body = $this->request->getBody(); + if ($body) { + $headers = array( + 'content-type' => 'application/json; charset=UTF-8', + 'content-length' => $body->getSize(), + 'x-upload-content-type' => $this->mimeType, + 'x-upload-content-length' => $this->size, + 'expect' => '', + ); + foreach ($headers as $key => $value) { + $this->request = $this->request->withHeader($key, $value); + } + } + + $response = $this->client->execute($this->request, false); + $location = $response->getHeaderLine('location'); + $code = $response->getStatusCode(); + + if (200 == $code && true == $location) { + return $location; + } + + $message = $code; + $body = json_decode((string) $this->request->getBody(), true); + if (isset($body['error']['errors'])) { + $message .= ': '; + foreach ($body['error']['errors'] as $error) { + $message .= "{$error['domain']}, {$error['message']};"; + } + $message = rtrim($message, ';'); + } + + $error = "Failed to start the resumable upload (HTTP {$message})"; + $this->client->getLogger()->error($error); + + throw new GoogleException($error); } - $error = "Failed to start the resumable upload (HTTP {$message})"; - $this->client->getLogger()->error($error); + private function transformToUploadUrl() + { + $parts = parse_url(http://github.com/(string) $this->request->getUri()); + if (!isset($parts['path'])) { + $parts['path'] = ''; + } + $parts['path'] = '/upload' . $parts['path']; + $uri = Uri::fromParts($parts); + $this->request = $this->request->withUri($uri); + } - throw new GoogleException($error); - } + public function setChunkSize($chunkSize) + { + $this->chunkSize = $chunkSize; + } - private function transformToUploadUrl() - { - $parts = parse_url(http://github.com/(string) $this->request->getUri()); - if (!isset($parts['path'])) { - $parts['path'] = ''; + public function getRequest() + { + return $this->request; } - $parts['path'] = '/upload' . $parts['path']; - $uri = Uri::fromParts($parts); - $this->request = $this->request->withUri($uri); - } - - public function setChunkSize($chunkSize) - { - $this->chunkSize = $chunkSize; - } - - public function getRequest() - { - return $this->request; - } } diff --git a/src/Http/REST.php b/src/Http/REST.php index 4329e4d1b..908481689 100644 --- a/src/Http/REST.php +++ b/src/Http/REST.php @@ -32,161 +32,161 @@ */ class REST { - /** - * Executes a Psr\Http\Message\RequestInterface and (if applicable) automatically retries - * when errors occur. - * - * @param Client $client - * @param RequestInterface $req - * @param string $expectedClass - * @param array $config - * @param array $retryMap - * @return mixed decoded result - * @throws \Google\Service\Exception on server side error (ie: not authenticated, - * invalid or malformed post body, invalid url) - */ - public static function execute( - ClientInterface $client, - RequestInterface $request, - $expectedClass = null, - $config = array(), - $retryMap = null - ) { - $runner = new Runner( - $config, - sprintf('%s %s', $request->getMethod(), (string) $request->getUri()), - array(get_class(), 'doExecute'), - array($client, $request, $expectedClass) - ); - - if (null !== $retryMap) { - $runner->setRetryMap($retryMap); - } - - return $runner->run(); - } - - /** - * Executes a Psr\Http\Message\RequestInterface - * - * @param Client $client - * @param RequestInterface $request - * @param string $expectedClass - * @return array decoded result - * @throws \Google\Service\Exception on server side error (ie: not authenticated, - * invalid or malformed post body, invalid url) - */ - public static function doExecute(ClientInterface $client, RequestInterface $request, $expectedClass = null) - { - try { - $httpHandler = HttpHandlerFactory::build($client); - $response = $httpHandler($request); - } catch (RequestException $e) { - // if Guzzle throws an exception, catch it and handle the response - if (!$e->hasResponse()) { - throw $e; - } - - $response = $e->getResponse(); - // specific checking for Guzzle 5: convert to PSR7 response - if ($response instanceof \GuzzleHttp\Message\ResponseInterface) { - $response = new Response( - $response->getStatusCode(), - $response->getHeaders() ?: [], - $response->getBody(), - $response->getProtocolVersion(), - $response->getReasonPhrase() + /** + * Executes a Psr\Http\Message\RequestInterface and (if applicable) automatically retries + * when errors occur. + * + * @param Client $client + * @param RequestInterface $req + * @param string $expectedClass + * @param array $config + * @param array $retryMap + * @return mixed decoded result + * @throws \Google\Service\Exception on server side error (ie: not authenticated, + * invalid or malformed post body, invalid url) + */ + public static function execute( + ClientInterface $client, + RequestInterface $request, + $expectedClass = null, + $config = array(), + $retryMap = null + ) { + $runner = new Runner( + $config, + sprintf('%s %s', $request->getMethod(), (string) $request->getUri()), + array(get_class(), 'doExecute'), + array($client, $request, $expectedClass) ); - } - } - return self::decodeHttpResponse($response, $request, $expectedClass); - } - - /** - * Decode an HTTP Response. - * @static - * @throws \Google\Service\Exception - * @param RequestInterface $response The http response to be decoded. - * @param ResponseInterface $response - * @param string $expectedClass - * @return mixed|null - */ - public static function decodeHttpResponse( - ResponseInterface $response, - RequestInterface $request = null, - $expectedClass = null - ) { - $code = $response->getStatusCode(); - - // retry strategy - if (intVal($code) >= 400) { - // if we errored out, it should be safe to grab the response body - $body = (string) $response->getBody(); - - // Check if we received errors, and add those to the Exception for convenience - throw new GoogleServiceException($body, $code, null, self::getResponseErrors($body)); - } + if (null !== $retryMap) { + $runner->setRetryMap($retryMap); + } - // Ensure we only pull the entire body into memory if the request is not - // of media type - $body = self::decodeBody($response, $request); + return $runner->run(); + } - if ($expectedClass = self::determineExpectedClass($expectedClass, $request)) { - $json = json_decode($body, true); + /** + * Executes a Psr\Http\Message\RequestInterface + * + * @param Client $client + * @param RequestInterface $request + * @param string $expectedClass + * @return array decoded result + * @throws \Google\Service\Exception on server side error (ie: not authenticated, + * invalid or malformed post body, invalid url) + */ + public static function doExecute(ClientInterface $client, RequestInterface $request, $expectedClass = null) + { + try { + $httpHandler = HttpHandlerFactory::build($client); + $response = $httpHandler($request); + } catch (RequestException $e) { + // if Guzzle throws an exception, catch it and handle the response + if (!$e->hasResponse()) { + throw $e; + } + + $response = $e->getResponse(); + // specific checking for Guzzle 5: convert to PSR7 response + if ($response instanceof \GuzzleHttp\Message\ResponseInterface) { + $response = new Response( + $response->getStatusCode(), + $response->getHeaders() ?: [], + $response->getBody(), + $response->getProtocolVersion(), + $response->getReasonPhrase() + ); + } + } + + return self::decodeHttpResponse($response, $request, $expectedClass); + } - return new $expectedClass($json); + /** + * Decode an HTTP Response. + * @static + * @throws \Google\Service\Exception + * @param RequestInterface $response The http response to be decoded. + * @param ResponseInterface $response + * @param string $expectedClass + * @return mixed|null + */ + public static function decodeHttpResponse( + ResponseInterface $response, + RequestInterface $request = null, + $expectedClass = null + ) { + $code = $response->getStatusCode(); + + // retry strategy + if (intVal($code) >= 400) { + // if we errored out, it should be safe to grab the response body + $body = (string) $response->getBody(); + + // Check if we received errors, and add those to the Exception for convenience + throw new GoogleServiceException($body, $code, null, self::getResponseErrors($body)); + } + + // Ensure we only pull the entire body into memory if the request is not + // of media type + $body = self::decodeBody($response, $request); + + if ($expectedClass = self::determineExpectedClass($expectedClass, $request)) { + $json = json_decode($body, true); + + return new $expectedClass($json); + } + + return $response; } - return $response; - } + private static function decodeBody(ResponseInterface $response, RequestInterface $request = null) + { + if (self::isAltMedia($request)) { + // don't decode the body, it's probably a really long string + return ''; + } - private static function decodeBody(ResponseInterface $response, RequestInterface $request = null) - { - if (self::isAltMedia($request)) { - // don't decode the body, it's probably a really long string - return ''; + return (string) $response->getBody(); } - return (string) $response->getBody(); - } + private static function determineExpectedClass($expectedClass, RequestInterface $request = null) + { + // "false" is used to explicitly prevent an expected class from being returned + if (false === $expectedClass) { + return null; + } - private static function determineExpectedClass($expectedClass, RequestInterface $request = null) - { - // "false" is used to explicitly prevent an expected class from being returned - if (false === $expectedClass) { - return null; - } + // if we don't have a request, we just use what's passed in + if (null === $request) { + return $expectedClass; + } - // if we don't have a request, we just use what's passed in - if (null === $request) { - return $expectedClass; + // return what we have in the request header if one was not supplied + return $expectedClass ?: $request->getHeaderLine('X-Php-Expected-Class'); } - // return what we have in the request header if one was not supplied - return $expectedClass ?: $request->getHeaderLine('X-Php-Expected-Class'); - } + private static function getResponseErrors($body) + { + $json = json_decode($body, true); - private static function getResponseErrors($body) - { - $json = json_decode($body, true); + if (isset($json['error']['errors'])) { + return $json['error']['errors']; + } - if (isset($json['error']['errors'])) { - return $json['error']['errors']; + return null; } - return null; - } + private static function isAltMedia(RequestInterface $request = null) + { + if ($request && $qs = $request->getUri()->getQuery()) { + parse_str($qs, $query); + if (isset($query['alt']) && $query['alt'] == 'media') { + return true; + } + } - private static function isAltMedia(RequestInterface $request = null) - { - if ($request && $qs = $request->getUri()->getQuery()) { - parse_str($qs, $query); - if (isset($query['alt']) && $query['alt'] == 'media') { - return true; - } + return false; } - - return false; - } } diff --git a/src/Model.php b/src/Model.php index 590984ef0..4d0e8dbca 100644 --- a/src/Model.php +++ b/src/Model.php @@ -30,303 +30,303 @@ */ class Model implements \ArrayAccess { - /** - * If you need to specify a NULL JSON value, use Google\Model::NULL_VALUE - * instead - it will be replaced when converting to JSON with a real null. - */ - const NULL_VALUE = "{}gapi-php-null"; - protected $internal_gapi_mappings = array(); - protected $modelData = array(); - protected $processed = array(); + /** + * If you need to specify a NULL JSON value, use Google\Model::NULL_VALUE + * instead - it will be replaced when converting to JSON with a real null. + */ + const NULL_VALUE = "{}gapi-php-null"; + protected $internal_gapi_mappings = array(); + protected $modelData = array(); + protected $processed = array(); - /** - * Polymorphic - accepts a variable number of arguments dependent - * on the type of the model subclass. - */ - final public function __construct() - { - if (func_num_args() == 1 && is_array(func_get_arg(0))) { - // Initialize the model with the array's contents. - $array = func_get_arg(0); - $this->mapTypes($array); + /** + * Polymorphic - accepts a variable number of arguments dependent + * on the type of the model subclass. + */ + final public function __construct() + { + if (func_num_args() == 1 && is_array(func_get_arg(0))) { + // Initialize the model with the array's contents. + $array = func_get_arg(0); + $this->mapTypes($array); + } + $this->gapiInit(); } - $this->gapiInit(); - } - /** - * Getter that handles passthrough access to the data array, and lazy object creation. - * @param string $key Property name. - * @return mixed The value if any, or null. - */ - public function __get($key) - { - $keyType = $this->keyType($key); - $keyDataType = $this->dataType($key); - if ($keyType && !isset($this->processed[$key])) { - if (isset($this->modelData[$key])) { - $val = $this->modelData[$key]; - } elseif ($keyDataType == 'array' || $keyDataType == 'map') { - $val = array(); - } else { - $val = null; - } + /** + * Getter that handles passthrough access to the data array, and lazy object creation. + * @param string $key Property name. + * @return mixed The value if any, or null. + */ + public function __get($key) + { + $keyType = $this->keyType($key); + $keyDataType = $this->dataType($key); + if ($keyType && !isset($this->processed[$key])) { + if (isset($this->modelData[$key])) { + $val = $this->modelData[$key]; + } elseif ($keyDataType == 'array' || $keyDataType == 'map') { + $val = array(); + } else { + $val = null; + } - if ($this->isAssociativeArray($val)) { - if ($keyDataType && 'map' == $keyDataType) { - foreach ($val as $arrayKey => $arrayItem) { - $this->modelData[$key][$arrayKey] = - new $keyType($arrayItem); - } - } else { - $this->modelData[$key] = new $keyType($val); - } - } else if (is_array($val)) { - $arrayObject = array(); - foreach ($val as $arrayIndex => $arrayItem) { - $arrayObject[$arrayIndex] = new $keyType($arrayItem); + if ($this->isAssociativeArray($val)) { + if ($keyDataType && 'map' == $keyDataType) { + foreach ($val as $arrayKey => $arrayItem) { + $this->modelData[$key][$arrayKey] = + new $keyType($arrayItem); + } + } else { + $this->modelData[$key] = new $keyType($val); + } + } else if (is_array($val)) { + $arrayObject = array(); + foreach ($val as $arrayIndex => $arrayItem) { + $arrayObject[$arrayIndex] = new $keyType($arrayItem); + } + $this->modelData[$key] = $arrayObject; + } + $this->processed[$key] = true; } - $this->modelData[$key] = $arrayObject; - } - $this->processed[$key] = true; - } - return isset($this->modelData[$key]) ? $this->modelData[$key] : null; - } + return isset($this->modelData[$key]) ? $this->modelData[$key] : null; + } - /** - * Initialize this object's properties from an array. - * - * @param array $array Used to seed this object's properties. - * @return void - */ - protected function mapTypes($array) - { - // Hard initialise simple types, lazy load more complex ones. - foreach ($array as $key => $val) { - if ($keyType = $this->keyType($key)) { - $dataType = $this->dataType($key); - if ($dataType == 'array' || $dataType == 'map') { - $this->$key = array(); - foreach ($val as $itemKey => $itemVal) { - if ($itemVal instanceof $keyType) { - $this->{$key}[$itemKey] = $itemVal; - } else { - $this->{$key}[$itemKey] = new $keyType($itemVal); + /** + * Initialize this object's properties from an array. + * + * @param array $array Used to seed this object's properties. + * @return void + */ + protected function mapTypes($array) + { + // Hard initialise simple types, lazy load more complex ones. + foreach ($array as $key => $val) { + if ($keyType = $this->keyType($key)) { + $dataType = $this->dataType($key); + if ($dataType == 'array' || $dataType == 'map') { + $this->$key = array(); + foreach ($val as $itemKey => $itemVal) { + if ($itemVal instanceof $keyType) { + $this->{$key}[$itemKey] = $itemVal; + } else { + $this->{$key}[$itemKey] = new $keyType($itemVal); + } + } + } elseif ($val instanceof $keyType) { + $this->$key = $val; + } else { + $this->$key = new $keyType($val); + } + unset($array[$key]); + } elseif (property_exists($this, $key)) { + $this->$key = $val; + unset($array[$key]); + } elseif (property_exists($this, $camelKey = $this->camelCase($key))) { + // This checks if property exists as camelCase, leaving it in array as snake_case + // in case of backwards compatibility issues. + $this->$camelKey = $val; } - } - } elseif ($val instanceof $keyType) { - $this->$key = $val; - } else { - $this->$key = new $keyType($val); } - unset($array[$key]); - } elseif (property_exists($this, $key)) { - $this->$key = $val; - unset($array[$key]); - } elseif (property_exists($this, $camelKey = $this->camelCase($key))) { - // This checks if property exists as camelCase, leaving it in array as snake_case - // in case of backwards compatibility issues. - $this->$camelKey = $val; - } + $this->modelData = $array; } - $this->modelData = $array; - } - - /** - * Blank initialiser to be used in subclasses to do post-construction initialisation - this - * avoids the need for subclasses to have to implement the variadics handling in their - * constructors. - */ - protected function gapiInit() - { - return; - } - - /** - * Create a simplified object suitable for straightforward - * conversion to JSON. This is relatively expensive - * due to the usage of reflection, but shouldn't be called - * a whole lot, and is the most straightforward way to filter. - */ - public function toSimpleObject() - { - $object = new stdClass(); - // Process all other data. - foreach ($this->modelData as $key => $val) { - $result = $this->getSimpleValue($val); - if ($result !== null) { - $object->$key = $this->nullPlaceholderCheck($result); - } + /** + * Blank initialiser to be used in subclasses to do post-construction initialisation - this + * avoids the need for subclasses to have to implement the variadics handling in their + * constructors. + */ + protected function gapiInit() + { + return; } - // Process all public properties. - $reflect = new ReflectionObject($this); - $props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC); - foreach ($props as $member) { - $name = $member->getName(); - $result = $this->getSimpleValue($this->$name); - if ($result !== null) { - $name = $this->getMappedName($name); - $object->$name = $this->nullPlaceholderCheck($result); - } - } + /** + * Create a simplified object suitable for straightforward + * conversion to JSON. This is relatively expensive + * due to the usage of reflection, but shouldn't be called + * a whole lot, and is the most straightforward way to filter. + */ + public function toSimpleObject() + { + $object = new stdClass(); - return $object; - } + // Process all other data. + foreach ($this->modelData as $key => $val) { + $result = $this->getSimpleValue($val); + if ($result !== null) { + $object->$key = $this->nullPlaceholderCheck($result); + } + } - /** - * Handle different types of values, primarily - * other objects and map and array data types. - */ - private function getSimpleValue($value) - { - if ($value instanceof Model) { - return $value->toSimpleObject(); - } else if (is_array($value)) { - $return = array(); - foreach ($value as $key => $a_value) { - $a_value = $this->getSimpleValue($a_value); - if ($a_value !== null) { - $key = $this->getMappedName($key); - $return[$key] = $this->nullPlaceholderCheck($a_value); + // Process all public properties. + $reflect = new ReflectionObject($this); + $props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC); + foreach ($props as $member) { + $name = $member->getName(); + $result = $this->getSimpleValue($this->$name); + if ($result !== null) { + $name = $this->getMappedName($name); + $object->$name = $this->nullPlaceholderCheck($result); + } } - } - return $return; + + return $object; } - return $value; - } - /** - * Check whether the value is the null placeholder and return true null. - */ - private function nullPlaceholderCheck($value) - { - if ($value === self::NULL_VALUE) { - return null; + /** + * Handle different types of values, primarily + * other objects and map and array data types. + */ + private function getSimpleValue($value) + { + if ($value instanceof Model) { + return $value->toSimpleObject(); + } else if (is_array($value)) { + $return = array(); + foreach ($value as $key => $a_value) { + $a_value = $this->getSimpleValue($a_value); + if ($a_value !== null) { + $key = $this->getMappedName($key); + $return[$key] = $this->nullPlaceholderCheck($a_value); + } + } + return $return; + } + return $value; } - return $value; - } - /** - * If there is an internal name mapping, use that. - */ - private function getMappedName($key) - { - if (isset($this->internal_gapi_mappings, $this->internal_gapi_mappings[$key])) { - $key = $this->internal_gapi_mappings[$key]; + /** + * Check whether the value is the null placeholder and return true null. + */ + private function nullPlaceholderCheck($value) + { + if ($value === self::NULL_VALUE) { + return null; + } + return $value; } - return $key; - } - /** - * Returns true only if the array is associative. - * @param array $array - * @return bool True if the array is associative. - */ - protected function isAssociativeArray($array) - { - if (!is_array($array)) { - return false; + /** + * If there is an internal name mapping, use that. + */ + private function getMappedName($key) + { + if (isset($this->internal_gapi_mappings, $this->internal_gapi_mappings[$key])) { + $key = $this->internal_gapi_mappings[$key]; + } + return $key; } - $keys = array_keys($array); - foreach ($keys as $key) { - if (is_string($key)) { - return true; - } + + /** + * Returns true only if the array is associative. + * @param array $array + * @return bool True if the array is associative. + */ + protected function isAssociativeArray($array) + { + if (!is_array($array)) { + return false; + } + $keys = array_keys($array); + foreach ($keys as $key) { + if (is_string($key)) { + return true; + } + } + return false; } - return false; - } - /** - * Verify if $obj is an array. - * @throws \Google\Exception Thrown if $obj isn't an array. - * @param array $obj Items that should be validated. - * @param string $method Method expecting an array as an argument. - */ - public function assertIsArray($obj, $method) - { - if ($obj && !is_array($obj)) { - throw new GoogleException( - "Incorrect parameter type passed to $method(). Expected an array." - ); + /** + * Verify if $obj is an array. + * @throws \Google\Exception Thrown if $obj isn't an array. + * @param array $obj Items that should be validated. + * @param string $method Method expecting an array as an argument. + */ + public function assertIsArray($obj, $method) + { + if ($obj && !is_array($obj)) { + throw new GoogleException( + "Incorrect parameter type passed to $method(). Expected an array." + ); + } } - } - /** @return bool */ - #[\ReturnTypeWillChange] - public function offsetExists($offset) - { - return isset($this->$offset) || isset($this->modelData[$offset]); - } + /** @return bool */ + #[\ReturnTypeWillChange] + public function offsetExists($offset) + { + return isset($this->$offset) || isset($this->modelData[$offset]); + } - /** @return mixed */ - #[\ReturnTypeWillChange] - public function offsetGet($offset) - { - return isset($this->$offset) ? + /** @return mixed */ + #[\ReturnTypeWillChange] + public function offsetGet($offset) + { + return isset($this->$offset) ? $this->$offset : $this->__get($offset); - } + } - /** @return void */ - #[\ReturnTypeWillChange] - public function offsetSet($offset, $value) - { - if (property_exists($this, $offset)) { - $this->$offset = $value; - } else { - $this->modelData[$offset] = $value; - $this->processed[$offset] = true; + /** @return void */ + #[\ReturnTypeWillChange] + public function offsetSet($offset, $value) + { + if (property_exists($this, $offset)) { + $this->$offset = $value; + } else { + $this->modelData[$offset] = $value; + $this->processed[$offset] = true; + } } - } - /** @return void */ - #[\ReturnTypeWillChange] - public function offsetUnset($offset) - { - unset($this->modelData[$offset]); - } + /** @return void */ + #[\ReturnTypeWillChange] + public function offsetUnset($offset) + { + unset($this->modelData[$offset]); + } - protected function keyType($key) - { - $keyType = $key . "Type"; + protected function keyType($key) + { + $keyType = $key . "Type"; - // ensure keyType is a valid class - if (property_exists($this, $keyType) && class_exists($this->$keyType)) { - return $this->$keyType; + // ensure keyType is a valid class + if (property_exists($this, $keyType) && class_exists($this->$keyType)) { + return $this->$keyType; + } } - } - protected function dataType($key) - { - $dataType = $key . "DataType"; + protected function dataType($key) + { + $dataType = $key . "DataType"; - if (property_exists($this, $dataType)) { - return $this->$dataType; + if (property_exists($this, $dataType)) { + return $this->$dataType; + } } - } - public function __isset($key) - { - return isset($this->modelData[$key]); - } + public function __isset($key) + { + return isset($this->modelData[$key]); + } - public function __unset($key) - { - unset($this->modelData[$key]); - } + public function __unset($key) + { + unset($this->modelData[$key]); + } - /** + /** * Convert a string to camelCase * @param string $value * @return string */ - private function camelCase($value) - { - $value = ucwords(str_replace(array('-', '_'), ' ', $value)); - $value = str_replace(' ', '', $value); - $value[0] = strtolower($value[0]); - return $value; - } + private function camelCase($value) + { + $value = ucwords(str_replace(array('-', '_'), ' ', $value)); + $value = str_replace(' ', '', $value); + $value[0] = strtolower($value[0]); + return $value; + } } diff --git a/src/Service.php b/src/Service.php index 7d3052499..923e7caef 100644 --- a/src/Service.php +++ b/src/Service.php @@ -22,50 +22,50 @@ class Service { - public $batchPath; - public $rootUrl; - public $version; - public $servicePath; - public $availableScopes; - public $resource; - private $client; + public $batchPath; + public $rootUrl; + public $version; + public $servicePath; + public $availableScopes; + public $resource; + private $client; - public function __construct($clientOrConfig = []) - { - if ($clientOrConfig instanceof Client) { - $this->client = $clientOrConfig; - } elseif (is_array($clientOrConfig)) { - $this->client = new Client($clientOrConfig ?: []); - } else { - $errorMessage = 'constructor must be array or instance of Google\Client'; - if (class_exists('TypeError')) { - throw new TypeError($errorMessage); - } - trigger_error($errorMessage, E_USER_ERROR); + public function __construct($clientOrConfig = []) + { + if ($clientOrConfig instanceof Client) { + $this->client = $clientOrConfig; + } elseif (is_array($clientOrConfig)) { + $this->client = new Client($clientOrConfig ?: []); + } else { + $errorMessage = 'constructor must be array or instance of Google\Client'; + if (class_exists('TypeError')) { + throw new TypeError($errorMessage); + } + trigger_error($errorMessage, E_USER_ERROR); + } } - } - /** + /** * Return the associated Google\Client class. * @return \Google\Client */ - public function getClient() - { - return $this->client; - } + public function getClient() + { + return $this->client; + } - /** + /** * Create a new HTTP Batch handler for this service * * @return Batch */ - public function createBatch() - { - return new Batch( - $this->client, - false, - $this->rootUrl, - $this->batchPath - ); - } + public function createBatch() + { + return new Batch( + $this->client, + false, + $this->rootUrl, + $this->batchPath + ); + } } diff --git a/src/Service/Exception.php b/src/Service/Exception.php index 3270ad7f3..bffdeaca9 100644 --- a/src/Service/Exception.php +++ b/src/Service/Exception.php @@ -21,51 +21,51 @@ class Exception extends GoogleException { - /** - * Optional list of errors returned in a JSON body of an HTTP error response. - */ - protected $errors = array(); + /** + * Optional list of errors returned in a JSON body of an HTTP error response. + */ + protected $errors = array(); - /** - * Override default constructor to add the ability to set $errors and a retry - * map. - * - * @param string $message - * @param int $code - * @param \Exception|null $previous - * @param [{string, string}] errors List of errors returned in an HTTP - * response. Defaults to []. - */ - public function __construct( - $message, - $code = 0, - Exception $previous = null, - $errors = array() - ) { - if (version_compare(PHP_VERSION, '5.3.0') >= 0) { - parent::__construct($message, $code, $previous); - } else { - parent::__construct($message, $code); - } + /** + * Override default constructor to add the ability to set $errors and a retry + * map. + * + * @param string $message + * @param int $code + * @param \Exception|null $previous + * @param [{string, string}] errors List of errors returned in an HTTP + * response. Defaults to []. + */ + public function __construct( + $message, + $code = 0, + Exception $previous = null, + $errors = array() + ) { + if (version_compare(PHP_VERSION, '5.3.0') >= 0) { + parent::__construct($message, $code, $previous); + } else { + parent::__construct($message, $code); + } - $this->errors = $errors; - } + $this->errors = $errors; + } - /** - * An example of the possible errors returned. - * - * { - * "domain": "global", - * "reason": "authError", - * "message": "Invalid Credentials", - * "locationType": "header", - * "location": "Authorization", - * } - * - * @return [{string, string}] List of errors return in an HTTP response or []. - */ - public function getErrors() - { - return $this->errors; - } + /** + * An example of the possible errors returned. + * + * { + * "domain": "global", + * "reason": "authError", + * "message": "Invalid Credentials", + * "locationType": "header", + * "location": "Authorization", + * } + * + * @return [{string, string}] List of errors return in an HTTP response or []. + */ + public function getErrors() + { + return $this->errors; + } } diff --git a/src/Service/Resource.php b/src/Service/Resource.php index 09ebaa089..c255a2f5a 100644 --- a/src/Service/Resource.php +++ b/src/Service/Resource.php @@ -31,278 +31,276 @@ */ class Resource { - // Valid query parameters that work, but don't appear in discovery. - private $stackParameters = array( - 'alt' => array('type' => 'string', 'location' => 'query'), - 'fields' => array('type' => 'string', 'location' => 'query'), - 'trace' => array('type' => 'string', 'location' => 'query'), - 'userIp' => array('type' => 'string', 'location' => 'query'), - 'quotaUser' => array('type' => 'string', 'location' => 'query'), - 'data' => array('type' => 'string', 'location' => 'body'), - 'mimeType' => array('type' => 'string', 'location' => 'header'), - 'uploadType' => array('type' => 'string', 'location' => 'query'), - 'mediaUpload' => array('type' => 'complex', 'location' => 'query'), - 'prettyPrint' => array('type' => 'string', 'location' => 'query'), - ); - - /** @var string $rootUrl */ - private $rootUrl; - - /** @var \Google\Client $client */ - private $client; - - /** @var string $serviceName */ - private $serviceName; - - /** @var string $servicePath */ - private $servicePath; - - /** @var string $resourceName */ - private $resourceName; - - /** @var array $methods */ - private $methods; - - public function __construct($service, $serviceName, $resourceName, $resource) - { - $this->rootUrl = $service->rootUrl; - $this->client = $service->getClient(); - $this->servicePath = $service->servicePath; - $this->serviceName = $serviceName; - $this->resourceName = $resourceName; - $this->methods = is_array($resource) && isset($resource['methods']) ? + // Valid query parameters that work, but don't appear in discovery. + private $stackParameters = array( + 'alt' => array('type' => 'string', 'location' => 'query'), + 'fields' => array('type' => 'string', 'location' => 'query'), + 'trace' => array('type' => 'string', 'location' => 'query'), + 'userIp' => array('type' => 'string', 'location' => 'query'), + 'quotaUser' => array('type' => 'string', 'location' => 'query'), + 'data' => array('type' => 'string', 'location' => 'body'), + 'mimeType' => array('type' => 'string', 'location' => 'header'), + 'uploadType' => array('type' => 'string', 'location' => 'query'), + 'mediaUpload' => array('type' => 'complex', 'location' => 'query'), + 'prettyPrint' => array('type' => 'string', 'location' => 'query'), + ); + + /** @var string $rootUrl */ + private $rootUrl; + + /** @var \Google\Client $client */ + private $client; + + /** @var string $serviceName */ + private $serviceName; + + /** @var string $servicePath */ + private $servicePath; + + /** @var string $resourceName */ + private $resourceName; + + /** @var array $methods */ + private $methods; + + public function __construct($service, $serviceName, $resourceName, $resource) + { + $this->rootUrl = $service->rootUrl; + $this->client = $service->getClient(); + $this->servicePath = $service->servicePath; + $this->serviceName = $serviceName; + $this->resourceName = $resourceName; + $this->methods = is_array($resource) && isset($resource['methods']) ? $resource['methods'] : array($resourceName => $resource); - } - - /** - * TODO: This function needs simplifying. - * @param $name - * @param $arguments - * @param $expectedClass - optional, the expected class name - * @return mixed|$expectedClass|ResponseInterface|RequestInterface - * @throws \Google\Exception - */ - public function call($name, $arguments, $expectedClass = null) - { - if (! isset($this->methods[$name])) { - $this->client->getLogger()->error( - 'Service method unknown', - array( - 'service' => $this->serviceName, - 'resource' => $this->resourceName, - 'method' => $name - ) - ); - - throw new GoogleException( - "Unknown function: " . - "{$this->serviceName}->{$this->resourceName}->{$name}()" - ); - } - $method = $this->methods[$name]; - $parameters = $arguments[0]; - - // postBody is a special case since it's not defined in the discovery - // document as parameter, but we abuse the param entry for storing it. - $postBody = null; - if (isset($parameters['postBody'])) { - if ($parameters['postBody'] instanceof Model) { - // In the cases the post body is an existing object, we want - // to use the smart method to create a simple object for - // for JSONification. - $parameters['postBody'] = $parameters['postBody']->toSimpleObject(); - } else if (is_object($parameters['postBody'])) { - // If the post body is another kind of object, we will try and - // wrangle it into a sensible format. - $parameters['postBody'] = - $this->convertToArrayAndStripNulls($parameters['postBody']); - } - $postBody = (array) $parameters['postBody']; - unset($parameters['postBody']); } - // TODO: optParams here probably should have been - // handled already - this may well be redundant code. - if (isset($parameters['optParams'])) { - $optParams = $parameters['optParams']; - unset($parameters['optParams']); - $parameters = array_merge($parameters, $optParams); - } + /** + * TODO: This function needs simplifying. + * @param $name + * @param $arguments + * @param $expectedClass - optional, the expected class name + * @return mixed|$expectedClass|ResponseInterface|RequestInterface + * @throws \Google\Exception + */ + public function call($name, $arguments, $expectedClass = null) + { + if (! isset($this->methods[$name])) { + $this->client->getLogger()->error( + 'Service method unknown', + array( + 'service' => $this->serviceName, + 'resource' => $this->resourceName, + 'method' => $name + ) + ); + + throw new GoogleException( + "Unknown function: " . + "{$this->serviceName}->{$this->resourceName}->{$name}()" + ); + } + $method = $this->methods[$name]; + $parameters = $arguments[0]; + + // postBody is a special case since it's not defined in the discovery + // document as parameter, but we abuse the param entry for storing it. + $postBody = null; + if (isset($parameters['postBody'])) { + if ($parameters['postBody'] instanceof Model) { + // In the cases the post body is an existing object, we want + // to use the smart method to create a simple object for + // for JSONification. + $parameters['postBody'] = $parameters['postBody']->toSimpleObject(); + } else if (is_object($parameters['postBody'])) { + // If the post body is another kind of object, we will try and + // wrangle it into a sensible format. + $parameters['postBody'] = + $this->convertToArrayAndStripNulls($parameters['postBody']); + } + $postBody = (array) $parameters['postBody']; + unset($parameters['postBody']); + } - if (!isset($method['parameters'])) { - $method['parameters'] = array(); - } + // TODO: optParams here probably should have been + // handled already - this may well be redundant code. + if (isset($parameters['optParams'])) { + $optParams = $parameters['optParams']; + unset($parameters['optParams']); + $parameters = array_merge($parameters, $optParams); + } - $method['parameters'] = array_merge( - $this->stackParameters, - $method['parameters'] - ); + if (!isset($method['parameters'])) { + $method['parameters'] = array(); + } - foreach ($parameters as $key => $val) { - if ($key != 'postBody' && ! isset($method['parameters'][$key])) { - $this->client->getLogger()->error( - 'Service parameter unknown', - array( - 'service' => $this->serviceName, - 'resource' => $this->resourceName, - 'method' => $name, - 'parameter' => $key - ) + $method['parameters'] = array_merge( + $this->stackParameters, + $method['parameters'] ); - throw new GoogleException("($name) unknown parameter: '$key'"); - } - } - foreach ($method['parameters'] as $paramName => $paramSpec) { - if (isset($paramSpec['required']) && - $paramSpec['required'] && - ! isset($parameters[$paramName]) - ) { - $this->client->getLogger()->error( - 'Service parameter missing', + foreach ($parameters as $key => $val) { + if ($key != 'postBody' && !isset($method['parameters'][$key])) { + $this->client->getLogger()->error( + 'Service parameter unknown', + array( + 'service' => $this->serviceName, + 'resource' => $this->resourceName, + 'method' => $name, + 'parameter' => $key + ) + ); + throw new GoogleException("($name) unknown parameter: '$key'"); + } + } + + foreach ($method['parameters'] as $paramName => $paramSpec) { + if ( + isset($paramSpec['required']) && + $paramSpec['required'] && + ! isset($parameters[$paramName]) + ) { + $this->client->getLogger()->error( + 'Service parameter missing', + array( + 'service' => $this->serviceName, + 'resource' => $this->resourceName, + 'method' => $name, + 'parameter' => $paramName + ) + ); + throw new GoogleException("($name) missing required param: '$paramName'"); + } + if (isset($parameters[$paramName])) { + $value = $parameters[$paramName]; + $parameters[$paramName] = $paramSpec; + $parameters[$paramName]['value'] = $value; + unset($parameters[$paramName]['required']); + } else { + // Ensure we don't pass nulls. + unset($parameters[$paramName]); + } + } + + $this->client->getLogger()->info( + 'Service Call', array( 'service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, - 'parameter' => $paramName + 'arguments' => $parameters, ) ); - throw new GoogleException("($name) missing required param: '$paramName'"); - } - if (isset($parameters[$paramName])) { - $value = $parameters[$paramName]; - $parameters[$paramName] = $paramSpec; - $parameters[$paramName]['value'] = $value; - unset($parameters[$paramName]['required']); - } else { - // Ensure we don't pass nulls. - unset($parameters[$paramName]); - } - } - - $this->client->getLogger()->info( - 'Service Call', - array( - 'service' => $this->serviceName, - 'resource' => $this->resourceName, - 'method' => $name, - 'arguments' => $parameters, - ) - ); - // build the service uri - $url = $this->createRequestUri( - $method['path'], - $parameters - ); + // build the service uri + $url = $this->createRequestUri($method['path'], $parameters); + + // NOTE: because we're creating the request by hand, + // and because the service has a rootUrl property + // the "base_uri" of the Http Client is not accounted for + $request = new Request( + $method['httpMethod'], + $url, + ['content-type' => 'application/json'], + $postBody ? json_encode($postBody) : '' + ); - // NOTE: because we're creating the request by hand, - // and because the service has a rootUrl property - // the "base_uri" of the Http Client is not accounted for - $request = new Request( - $method['httpMethod'], - $url, - ['content-type' => 'application/json'], - $postBody ? json_encode($postBody) : '' - ); + // support uploads + if (isset($parameters['data'])) { + $mimeType = isset($parameters['mimeType']) + ? $parameters['mimeType']['value'] + : 'application/octet-stream'; + $data = $parameters['data']['value']; + $upload = new MediaFileUpload($this->client, $request, $mimeType, $data); - // support uploads - if (isset($parameters['data'])) { - $mimeType = isset($parameters['mimeType']) - ? $parameters['mimeType']['value'] - : 'application/octet-stream'; - $data = $parameters['data']['value']; - $upload = new MediaFileUpload($this->client, $request, $mimeType, $data); + // pull down the modified request + $request = $upload->getRequest(); + } - // pull down the modified request - $request = $upload->getRequest(); - } + // if this is a media type, we will return the raw response + // rather than using an expected class + if (isset($parameters['alt']) && $parameters['alt']['value'] == 'media') { + $expectedClass = null; + } - // if this is a media type, we will return the raw response - // rather than using an expected class - if (isset($parameters['alt']) && $parameters['alt']['value'] == 'media') { - $expectedClass = null; - } + // if the client is marked for deferring, rather than + // execute the request, return the response + if ($this->client->shouldDefer()) { + // @TODO find a better way to do this + $request = $request + ->withHeader('X-Php-Expected-Class', $expectedClass); - // if the client is marked for deferring, rather than - // execute the request, return the response - if ($this->client->shouldDefer()) { - // @TODO find a better way to do this - $request = $request - ->withHeader('X-Php-Expected-Class', $expectedClass); + return $request; + } - return $request; + return $this->client->execute($request, $expectedClass); } - return $this->client->execute($request, $expectedClass); - } - - protected function convertToArrayAndStripNulls($o) - { - $o = (array) $o; - foreach ($o as $k => $v) { - if ($v === null) { - unset($o[$k]); - } elseif (is_object($v) || is_array($v)) { - $o[$k] = $this->convertToArrayAndStripNulls($o[$k]); - } - } - return $o; - } - - /** - * Parse/expand request parameters and create a fully qualified - * request uri. - * @static - * @param string $restPath - * @param array $params - * @return string $requestUrl - */ - public function createRequestUri($restPath, $params) - { - // Override the default servicePath address if the $restPath use a / - if ('/' == substr($restPath, 0, 1)) { - $requestUrl = substr($restPath, 1); - } else { - $requestUrl = $this->servicePath . $restPath; + protected function convertToArrayAndStripNulls($o) + { + $o = (array) $o; + foreach ($o as $k => $v) { + if ($v === null) { + unset($o[$k]); + } elseif (is_object($v) || is_array($v)) { + $o[$k] = $this->convertToArrayAndStripNulls($o[$k]); + } + } + return $o; } - // code for leading slash - if ($this->rootUrl) { - if ('/' !== substr($this->rootUrl, -1) && '/' !== substr($requestUrl, 0, 1)) { - $requestUrl = '/' . $requestUrl; - } - $requestUrl = $this->rootUrl . $requestUrl; - } - $uriTemplateVars = array(); - $queryVars = array(); - foreach ($params as $paramName => $paramSpec) { - if ($paramSpec['type'] == 'boolean') { - $paramSpec['value'] = $paramSpec['value'] ? 'true' : 'false'; - } - if ($paramSpec['location'] == 'path') { - $uriTemplateVars[$paramName] = $paramSpec['value']; - } else if ($paramSpec['location'] == 'query') { - if (is_array($paramSpec['value'])) { - foreach ($paramSpec['value'] as $value) { - $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($value)); - } + /** + * Parse/expand request parameters and create a fully qualified + * request uri. + * @static + * @param string $restPath + * @param array $params + * @return string $requestUrl + */ + public function createRequestUri($restPath, $params) + { + // Override the default servicePath address if the $restPath use a / + if ('/' == substr($restPath, 0, 1)) { + $requestUrl = substr($restPath, 1); } else { - $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($paramSpec['value'])); + $requestUrl = $this->servicePath . $restPath; } - } - } - if (count($uriTemplateVars)) { - $uriTemplateParser = new UriTemplate(); - $requestUrl = $uriTemplateParser->parse($requestUrl, $uriTemplateVars); - } + // code for leading slash + if ($this->rootUrl) { + if ('/' !== substr($this->rootUrl, -1) && '/' !== substr($requestUrl, 0, 1)) { + $requestUrl = '/' . $requestUrl; + } + $requestUrl = $this->rootUrl . $requestUrl; + } + $uriTemplateVars = array(); + $queryVars = array(); + foreach ($params as $paramName => $paramSpec) { + if ($paramSpec['type'] == 'boolean') { + $paramSpec['value'] = $paramSpec['value'] ? 'true' : 'false'; + } + if ($paramSpec['location'] == 'path') { + $uriTemplateVars[$paramName] = $paramSpec['value']; + } else if ($paramSpec['location'] == 'query') { + if (is_array($paramSpec['value'])) { + foreach ($paramSpec['value'] as $value) { + $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($value)); + } + } else { + $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($paramSpec['value'])); + } + } + } - if (count($queryVars)) { - $requestUrl .= '?' . implode('&', $queryVars); - } + if (count($uriTemplateVars)) { + $uriTemplateParser = new UriTemplate(); + $requestUrl = $uriTemplateParser->parse($requestUrl, $uriTemplateVars); + } - return $requestUrl; - } + if (count($queryVars)) { + $requestUrl .= '?' . implode('&', $queryVars); + } + + return $requestUrl; + } } diff --git a/src/Task/Composer.php b/src/Task/Composer.php index 892573b9c..1c3a1b5ee 100644 --- a/src/Task/Composer.php +++ b/src/Task/Composer.php @@ -24,92 +24,90 @@ class Composer { - /** - * @param Event $event Composer event passed in for any script method - * @param Filesystem $filesystem Optional. Used for testing. - */ - public static function cleanup( - Event $event, - Filesystem $filesystem = null - ) { - $composer = $event->getComposer(); - $extra = $composer->getPackage()->getExtra(); - $servicesToKeep = isset($extra['google/apiclient-services']) ? - $extra['google/apiclient-services'] : []; - if ($servicesToKeep) { - $vendorDir = $composer->getConfig()->get('vendor-dir'); - $serviceDir = sprintf( - '%s/google/apiclient-services/src/Google/Service', - $vendorDir - ); - if (!is_dir($serviceDir)) { - // path for google/apiclient-services >= 0.200.0 - $serviceDir = sprintf( - '%s/google/apiclient-services/src', - $vendorDir - ); - } - self::verifyServicesToKeep($serviceDir, $servicesToKeep); - $finder = self::getServicesToRemove($serviceDir, $servicesToKeep); - $filesystem = $filesystem ?: new Filesystem(); - if (0 !== $count = count($finder)) { - $event->getIO()->write( - sprintf( - 'Removing %s google services', - $count - ) - ); - foreach ($finder as $file) { - $realpath = $file->getRealPath(); - $filesystem->remove($realpath); - $filesystem->remove($realpath . '.php'); + /** + * @param Event $event Composer event passed in for any script method + * @param Filesystem $filesystem Optional. Used for testing. + */ + public static function cleanup( + Event $event, + Filesystem $filesystem = null + ) { + $composer = $event->getComposer(); + $extra = $composer->getPackage()->getExtra(); + $servicesToKeep = isset($extra['google/apiclient-services']) + ? $extra['google/apiclient-services'] + : []; + if ($servicesToKeep) { + $vendorDir = $composer->getConfig()->get('vendor-dir'); + $serviceDir = sprintf( + '%s/google/apiclient-services/src/Google/Service', + $vendorDir + ); + if (!is_dir($serviceDir)) { + // path for google/apiclient-services >= 0.200.0 + $serviceDir = sprintf( + '%s/google/apiclient-services/src', + $vendorDir + ); + } + self::verifyServicesToKeep($serviceDir, $servicesToKeep); + $finder = self::getServicesToRemove($serviceDir, $servicesToKeep); + $filesystem = $filesystem ?: new Filesystem(); + if (0 !== $count = count($finder)) { + $event->getIO()->write( + sprintf('Removing %s google services', $count) + ); + foreach ($finder as $file) { + $realpath = $file->getRealPath(); + $filesystem->remove($realpath); + $filesystem->remove($realpath . '.php'); + } + } } - } } - } - /** - * @throws InvalidArgumentException when the service doesn't exist - */ - private static function verifyServicesToKeep( - $serviceDir, - array $servicesToKeep - ) { - $finder = (new Finder()) - ->directories() - ->depth('== 0'); + /** + * @throws InvalidArgumentException when the service doesn't exist + */ + private static function verifyServicesToKeep( + $serviceDir, + array $servicesToKeep + ) { + $finder = (new Finder()) + ->directories() + ->depth('== 0'); - foreach ($servicesToKeep as $service) { - if (!preg_match('/^[a-zA-Z0-9]*$/', $service)) { - throw new InvalidArgumentException( - sprintf( - 'Invalid Google service name "%s"', - $service - ) - ); - } - try { - $finder->in($serviceDir . '/' . $service); - } catch (InvalidArgumentException $e) { - throw new InvalidArgumentException( - sprintf( - 'Google service "%s" does not exist or was removed previously', - $service - ) - ); - } + foreach ($servicesToKeep as $service) { + if (!preg_match('/^[a-zA-Z0-9]*$/', $service)) { + throw new InvalidArgumentException( + sprintf( + 'Invalid Google service name "%s"', + $service + ) + ); + } + try { + $finder->in($serviceDir . '/' . $service); + } catch (InvalidArgumentException $e) { + throw new InvalidArgumentException( + sprintf( + 'Google service "%s" does not exist or was removed previously', + $service + ) + ); + } + } } - } - private static function getServicesToRemove( - $serviceDir, - array $servicesToKeep - ) { - // find all files in the current directory - return (new Finder()) - ->directories() - ->depth('== 0') - ->in($serviceDir) - ->exclude($servicesToKeep); - } + private static function getServicesToRemove( + $serviceDir, + array $servicesToKeep + ) { + // find all files in the current directory + return (new Finder()) + ->directories() + ->depth('== 0') + ->in($serviceDir) + ->exclude($servicesToKeep); + } } diff --git a/src/Task/Runner.php b/src/Task/Runner.php index c081dc591..becc78b08 100644 --- a/src/Task/Runner.php +++ b/src/Task/Runner.php @@ -27,261 +27,266 @@ */ class Runner { - const TASK_RETRY_NEVER = 0; - const TASK_RETRY_ONCE = 1; - const TASK_RETRY_ALWAYS = -1; - - /** - * @var integer $maxDelay The max time (in seconds) to wait before a retry. - */ - private $maxDelay = 60; - /** - * @var integer $delay The previous delay from which the next is calculated. - */ - private $delay = 1; - - /** - * @var integer $factor The base number for the exponential back off. - */ - private $factor = 2; - /** - * @var float $jitter A random number between -$jitter and $jitter will be - * added to $factor on each iteration to allow for a better distribution of - * retries. - */ - private $jitter = 0.5; - - /** - * @var integer $attempts The number of attempts that have been tried so far. - */ - private $attempts = 0; - /** - * @var integer $maxAttempts The max number of attempts allowed. - */ - private $maxAttempts = 1; - - /** - * @var callable $action The task to run and possibly retry. - */ - private $action; - /** - * @var array $arguments The task arguments. - */ - private $arguments; - - /** - * @var array $retryMap Map of errors with retry counts. - */ - protected $retryMap = [ - '500' => self::TASK_RETRY_ALWAYS, - '503' => self::TASK_RETRY_ALWAYS, - 'rateLimitExceeded' => self::TASK_RETRY_ALWAYS, - 'userRateLimitExceeded' => self::TASK_RETRY_ALWAYS, - 6 => self::TASK_RETRY_ALWAYS, // CURLE_COULDNT_RESOLVE_HOST - 7 => self::TASK_RETRY_ALWAYS, // CURLE_COULDNT_CONNECT - 28 => self::TASK_RETRY_ALWAYS, // CURLE_OPERATION_TIMEOUTED - 35 => self::TASK_RETRY_ALWAYS, // CURLE_SSL_CONNECT_ERROR - 52 => self::TASK_RETRY_ALWAYS, // CURLE_GOT_NOTHING - 'lighthouseError' => self::TASK_RETRY_NEVER - ]; - - /** - * Creates a new task runner with exponential backoff support. - * - * @param array $config The task runner config - * @param string $name The name of the current task (used for logging) - * @param callable $action The task to run and possibly retry - * @param array $arguments The task arguments - * @throws \Google\Task\Exception when misconfigured - */ - public function __construct( - $config, - $name, - $action, - array $arguments = array() - ) { - if (isset($config['initial_delay'])) { - if ($config['initial_delay'] < 0) { - throw new GoogleTaskException( - 'Task configuration `initial_delay` must not be negative.' - ); - } - - $this->delay = $config['initial_delay']; - } + const TASK_RETRY_NEVER = 0; + const TASK_RETRY_ONCE = 1; + const TASK_RETRY_ALWAYS = -1; + + /** + * @var integer $maxDelay The max time (in seconds) to wait before a retry. + */ + private $maxDelay = 60; + + /** + * @var integer $delay The previous delay from which the next is calculated. + */ + private $delay = 1; + + /** + * @var integer $factor The base number for the exponential back off. + */ + private $factor = 2; + + /** + * @var float $jitter A random number between -$jitter and $jitter will be + * added to $factor on each iteration to allow for a better distribution of + * retries. + */ + private $jitter = 0.5; + + /** + * @var integer $attempts The number of attempts that have been tried so far. + */ + private $attempts = 0; + + /** + * @var integer $maxAttempts The max number of attempts allowed. + */ + private $maxAttempts = 1; + + /** + * @var callable $action The task to run and possibly retry. + */ + private $action; + + /** + * @var array $arguments The task arguments. + */ + private $arguments; + + /** + * @var array $retryMap Map of errors with retry counts. + */ + protected $retryMap = [ + '500' => self::TASK_RETRY_ALWAYS, + '503' => self::TASK_RETRY_ALWAYS, + 'rateLimitExceeded' => self::TASK_RETRY_ALWAYS, + 'userRateLimitExceeded' => self::TASK_RETRY_ALWAYS, + 6 => self::TASK_RETRY_ALWAYS, // CURLE_COULDNT_RESOLVE_HOST + 7 => self::TASK_RETRY_ALWAYS, // CURLE_COULDNT_CONNECT + 28 => self::TASK_RETRY_ALWAYS, // CURLE_OPERATION_TIMEOUTED + 35 => self::TASK_RETRY_ALWAYS, // CURLE_SSL_CONNECT_ERROR + 52 => self::TASK_RETRY_ALWAYS, // CURLE_GOT_NOTHING + 'lighthouseError' => self::TASK_RETRY_NEVER + ]; + + /** + * Creates a new task runner with exponential backoff support. + * + * @param array $config The task runner config + * @param string $name The name of the current task (used for logging) + * @param callable $action The task to run and possibly retry + * @param array $arguments The task arguments + * @throws \Google\Task\Exception when misconfigured + */ + public function __construct( + $config, + $name, + $action, + array $arguments = array() + ) { + if (isset($config['initial_delay'])) { + if ($config['initial_delay'] < 0) { + throw new GoogleTaskException( + 'Task configuration `initial_delay` must not be negative.' + ); + } + + $this->delay = $config['initial_delay']; + } - if (isset($config['max_delay'])) { - if ($config['max_delay'] <= 0) { - throw new GoogleTaskException( - 'Task configuration `max_delay` must be greater than 0.' - ); - } + if (isset($config['max_delay'])) { + if ($config['max_delay'] <= 0) { + throw new GoogleTaskException( + 'Task configuration `max_delay` must be greater than 0.' + ); + } - $this->maxDelay = $config['max_delay']; - } + $this->maxDelay = $config['max_delay']; + } - if (isset($config['factor'])) { - if ($config['factor'] <= 0) { - throw new GoogleTaskException( - 'Task configuration `factor` must be greater than 0.' - ); - } + if (isset($config['factor'])) { + if ($config['factor'] <= 0) { + throw new GoogleTaskException( + 'Task configuration `factor` must be greater than 0.' + ); + } - $this->factor = $config['factor']; - } + $this->factor = $config['factor']; + } - if (isset($config['jitter'])) { - if ($config['jitter'] <= 0) { - throw new GoogleTaskException( - 'Task configuration `jitter` must be greater than 0.' - ); - } + if (isset($config['jitter'])) { + if ($config['jitter'] <= 0) { + throw new GoogleTaskException( + 'Task configuration `jitter` must be greater than 0.' + ); + } + + $this->jitter = $config['jitter']; + } + + if (isset($config['retries'])) { + if ($config['retries'] < 0) { + throw new GoogleTaskException( + 'Task configuration `retries` must not be negative.' + ); + } + $this->maxAttempts += $config['retries']; + } + + if (!is_callable($action)) { + throw new GoogleTaskException( + 'Task argument `$action` must be a valid callable.' + ); + } - $this->jitter = $config['jitter']; + $this->action = $action; + $this->arguments = $arguments; } - if (isset($config['retries'])) { - if ($config['retries'] < 0) { - throw new GoogleTaskException( - 'Task configuration `retries` must not be negative.' - ); - } - $this->maxAttempts += $config['retries']; + /** + * Checks if a retry can be attempted. + * + * @return boolean + */ + public function canAttempt() + { + return $this->attempts < $this->maxAttempts; } - if (!is_callable($action)) { - throw new GoogleTaskException( - 'Task argument `$action` must be a valid callable.' - ); + /** + * Runs the task and (if applicable) automatically retries when errors occur. + * + * @return mixed + * @throws \Google\Service\Exception on failure when no retries are available. + */ + public function run() + { + while ($this->attempt()) { + try { + return call_user_func_array($this->action, $this->arguments); + } catch (GoogleServiceException $exception) { + $allowedRetries = $this->allowedRetries( + $exception->getCode(), + $exception->getErrors() + ); + + if (!$this->canAttempt() || !$allowedRetries) { + throw $exception; + } + + if ($allowedRetries > 0) { + $this->maxAttempts = min( + $this->maxAttempts, + $this->attempts + $allowedRetries + ); + } + } + } } - $this->action = $action; - $this->arguments = $arguments; - } - - /** - * Checks if a retry can be attempted. - * - * @return boolean - */ - public function canAttempt() - { - return $this->attempts < $this->maxAttempts; - } - - /** - * Runs the task and (if applicable) automatically retries when errors occur. - * - * @return mixed - * @throws \Google\Service\Exception on failure when no retries are available. - */ - public function run() - { - while ($this->attempt()) { - try { - return call_user_func_array($this->action, $this->arguments); - } catch (GoogleServiceException $exception) { - $allowedRetries = $this->allowedRetries( - $exception->getCode(), - $exception->getErrors() - ); - - if (!$this->canAttempt() || !$allowedRetries) { - throw $exception; + /** + * Runs a task once, if possible. This is useful for bypassing the `run()` + * loop. + * + * NOTE: If this is not the first attempt, this function will sleep in + * accordance to the backoff configurations before running the task. + * + * @return boolean + */ + public function attempt() + { + if (!$this->canAttempt()) { + return false; } - if ($allowedRetries > 0) { - $this->maxAttempts = min( - $this->maxAttempts, - $this->attempts + $allowedRetries - ); + if ($this->attempts > 0) { + $this->backOff(); } - } - } - } - - /** - * Runs a task once, if possible. This is useful for bypassing the `run()` - * loop. - * - * NOTE: If this is not the first attempt, this function will sleep in - * accordance to the backoff configurations before running the task. - * - * @return boolean - */ - public function attempt() - { - if (!$this->canAttempt()) { - return false; + + $this->attempts++; + + return true; } - if ($this->attempts > 0) { - $this->backOff(); + /** + * Sleeps in accordance to the backoff configurations. + */ + private function backOff() + { + $delay = $this->getDelay(); + + usleep($delay * 1000000); } - $this->attempts++; - return true; - } - - /** - * Sleeps in accordance to the backoff configurations. - */ - private function backOff() - { - $delay = $this->getDelay(); - - usleep($delay * 1000000); - } - - /** - * Gets the delay (in seconds) for the current backoff period. - * - * @return float - */ - private function getDelay() - { - $jitter = $this->getJitter(); - $factor = $this->attempts > 1 ? $this->factor + $jitter : 1 + abs($jitter); - - return $this->delay = min($this->maxDelay, $this->delay * $factor); - } - - /** - * Gets the current jitter (random number between -$this->jitter and - * $this->jitter). - * - * @return float - */ - private function getJitter() - { - return $this->jitter * 2 * mt_rand() / mt_getrandmax() - $this->jitter; - } - - /** - * Gets the number of times the associated task can be retried. - * - * NOTE: -1 is returned if the task can be retried indefinitely - * - * @return integer - */ - public function allowedRetries($code, $errors = array()) - { - if (isset($this->retryMap[$code])) { - return $this->retryMap[$code]; + /** + * Gets the delay (in seconds) for the current backoff period. + * + * @return float + */ + private function getDelay() + { + $jitter = $this->getJitter(); + $factor = $this->attempts > 1 ? $this->factor + $jitter : 1 + abs($jitter); + + return $this->delay = min($this->maxDelay, $this->delay * $factor); } - if ( - !empty($errors) && - isset($errors[0]['reason'], $this->retryMap[$errors[0]['reason']]) - ) { - return $this->retryMap[$errors[0]['reason']]; + /** + * Gets the current jitter (random number between -$this->jitter and + * $this->jitter). + * + * @return float + */ + private function getJitter() + { + return $this->jitter * 2 * mt_rand() / mt_getrandmax() - $this->jitter; } - return 0; - } + /** + * Gets the number of times the associated task can be retried. + * + * NOTE: -1 is returned if the task can be retried indefinitely + * + * @return integer + */ + public function allowedRetries($code, $errors = array()) + { + if (isset($this->retryMap[$code])) { + return $this->retryMap[$code]; + } + + if ( + !empty($errors) && + isset($errors[0]['reason'], $this->retryMap[$errors[0]['reason']]) + ) { + return $this->retryMap[$errors[0]['reason']]; + } - public function setRetryMap($retryMap) - { - $this->retryMap = $retryMap; - } + return 0; + } + + public function setRetryMap($retryMap) + { + $this->retryMap = $retryMap; + } } diff --git a/src/Utils/UriTemplate.php b/src/Utils/UriTemplate.php index 1f0c6b31d..c74814d25 100644 --- a/src/Utils/UriTemplate.php +++ b/src/Utils/UriTemplate.php @@ -23,17 +23,17 @@ */ class UriTemplate { - const TYPE_MAP = "1"; - const TYPE_LIST = "2"; - const TYPE_SCALAR = "4"; + const TYPE_MAP = "1"; + const TYPE_LIST = "2"; + const TYPE_SCALAR = "4"; - /** - * @var $operators array - * These are valid at the start of a template block to - * modify the way in which the variables inside are - * processed. - */ - private $operators = array( + /** + * @var $operators array + * These are valid at the start of a template block to + * modify the way in which the variables inside are + * processed. + */ + private $operators = array( "+" => "reserved", "/" => "segments", "." => "dotprefix", @@ -41,295 +41,295 @@ class UriTemplate ";" => "semicolon", "?" => "form", "&" => "continuation" - ); + ); - /** - * @var reserved array - * These are the characters which should not be URL encoded in reserved - * strings. - */ - private $reserved = array( + /** + * @var reserved array + * These are the characters which should not be URL encoded in reserved + * strings. + */ + private $reserved = array( "=", ",", "!", "@", "|", ":", "/", "?", "#", "[", "]",'$', "&", "'", "(", ")", "*", "+", ";" - ); - private $reservedEncoded = array( + ); + private $reservedEncoded = array( "%3D", "%2C", "%21", "%40", "%7C", "%3A", "%2F", "%3F", "%23", "%5B", "%5D", "%24", "%26", "%27", "%28", "%29", "%2A", "%2B", "%3B" - ); + ); - public function parse($string, array $parameters) - { - return $this->resolveNextSection($string, $parameters); - } - - /** - * This function finds the first matching {...} block and - * executes the replacement. It then calls itself to find - * subsequent blocks, if any. - */ - private function resolveNextSection($string, $parameters) - { - $start = strpos($string, "{"); - if ($start === false) { - return $string; + public function parse($string, array $parameters) + { + return $this->resolveNextSection($string, $parameters); } - $end = strpos($string, "}"); - if ($end === false) { - return $string; + + /** + * This function finds the first matching {...} block and + * executes the replacement. It then calls itself to find + * subsequent blocks, if any. + */ + private function resolveNextSection($string, $parameters) + { + $start = strpos($string, "{"); + if ($start === false) { + return $string; + } + $end = strpos($string, "}"); + if ($end === false) { + return $string; + } + $string = $this->replace($string, $start, $end, $parameters); + return $this->resolveNextSection($string, $parameters); } - $string = $this->replace($string, $start, $end, $parameters); - return $this->resolveNextSection($string, $parameters); - } - private function replace($string, $start, $end, $parameters) - { - // We know a data block will have {} round it, so we can strip that. - $data = substr($string, $start + 1, $end - $start - 1); + private function replace($string, $start, $end, $parameters) + { + // We know a data block will have {} round it, so we can strip that. + $data = substr($string, $start + 1, $end - $start - 1); - // If the first character is one of the reserved operators, it effects - // the processing of the stream. - if (isset($this->operators[$data[0]])) { - $op = $this->operators[$data[0]]; - $data = substr($data, 1); - $prefix = ""; - $prefix_on_missing = false; + // If the first character is one of the reserved operators, it effects + // the processing of the stream. + if (isset($this->operators[$data[0]])) { + $op = $this->operators[$data[0]]; + $data = substr($data, 1); + $prefix = ""; + $prefix_on_missing = false; - switch ($op) { - case "reserved": - // Reserved means certain characters should not be URL encoded - $data = $this->replaceVars($data, $parameters, ",", null, true); - break; - case "fragment": - // Comma separated with fragment prefix. Bare values only. - $prefix = "#"; - $prefix_on_missing = true; - $data = $this->replaceVars($data, $parameters, ",", null, true); - break; - case "segments": - // Slash separated data. Bare values only. - $prefix = "/"; - $data =$this->replaceVars($data, $parameters, "/"); - break; - case "dotprefix": - // Dot separated data. Bare values only. - $prefix = "."; - $prefix_on_missing = true; - $data = $this->replaceVars($data, $parameters, "."); - break; - case "semicolon": - // Semicolon prefixed and separated. Uses the key name - $prefix = ";"; - $data = $this->replaceVars($data, $parameters, ";", "=", false, true, false); - break; - case "form": - // Standard URL format. Uses the key name - $prefix = "?"; - $data = $this->replaceVars($data, $parameters, "&", "="); - break; - case "continuation": - // Standard URL, but with leading ampersand. Uses key name. - $prefix = "&"; - $data = $this->replaceVars($data, $parameters, "&", "="); - break; - } + switch ($op) { + case "reserved": + // Reserved means certain characters should not be URL encoded + $data = $this->replaceVars($data, $parameters, ",", null, true); + break; + case "fragment": + // Comma separated with fragment prefix. Bare values only. + $prefix = "#"; + $prefix_on_missing = true; + $data = $this->replaceVars($data, $parameters, ",", null, true); + break; + case "segments": + // Slash separated data. Bare values only. + $prefix = "/"; + $data =$this->replaceVars($data, $parameters, "/"); + break; + case "dotprefix": + // Dot separated data. Bare values only. + $prefix = "."; + $prefix_on_missing = true; + $data = $this->replaceVars($data, $parameters, "."); + break; + case "semicolon": + // Semicolon prefixed and separated. Uses the key name + $prefix = ";"; + $data = $this->replaceVars($data, $parameters, ";", "=", false, true, false); + break; + case "form": + // Standard URL format. Uses the key name + $prefix = "?"; + $data = $this->replaceVars($data, $parameters, "&", "="); + break; + case "continuation": + // Standard URL, but with leading ampersand. Uses key name. + $prefix = "&"; + $data = $this->replaceVars($data, $parameters, "&", "="); + break; + } - // Add the initial prefix character if data is valid. - if ($data || ($data !== false && $prefix_on_missing)) { - $data = $prefix . $data; - } + // Add the initial prefix character if data is valid. + if ($data || ($data !== false && $prefix_on_missing)) { + $data = $prefix . $data; + } - } else { - // If no operator we replace with the defaults. - $data = $this->replaceVars($data, $parameters); + } else { + // If no operator we replace with the defaults. + $data = $this->replaceVars($data, $parameters); + } + // This is chops out the {...} and replaces with the new section. + return substr($string, 0, $start) . $data . substr($string, $end + 1); } - // This is chops out the {...} and replaces with the new section. - return substr($string, 0, $start) . $data . substr($string, $end + 1); - } - private function replaceVars( - $section, - $parameters, - $sep = ",", - $combine = null, - $reserved = false, - $tag_empty = false, - $combine_on_empty = true - ) { - if (strpos($section, ",") === false) { - // If we only have a single value, we can immediately process. - return $this->combine( - $section, - $parameters, - $sep, - $combine, - $reserved, - $tag_empty, - $combine_on_empty - ); - } else { - // If we have multiple values, we need to split and loop over them. - // Each is treated individually, then glued together with the - // separator character. - $vars = explode(",", $section); - return $this->combineList( - $vars, - $sep, - $parameters, - $combine, - $reserved, - false, // Never emit empty strings in multi-param replacements - $combine_on_empty - ); + private function replaceVars( + $section, + $parameters, + $sep = ",", + $combine = null, + $reserved = false, + $tag_empty = false, + $combine_on_empty = true + ) { + if (strpos($section, ",") === false) { + // If we only have a single value, we can immediately process. + return $this->combine( + $section, + $parameters, + $sep, + $combine, + $reserved, + $tag_empty, + $combine_on_empty + ); + } else { + // If we have multiple values, we need to split and loop over them. + // Each is treated individually, then glued together with the + // separator character. + $vars = explode(",", $section); + return $this->combineList( + $vars, + $sep, + $parameters, + $combine, + $reserved, + false, // Never emit empty strings in multi-param replacements + $combine_on_empty + ); + } } - } - public function combine( - $key, - $parameters, - $sep, - $combine, - $reserved, - $tag_empty, - $combine_on_empty - ) { - $length = false; - $explode = false; - $skip_final_combine = false; - $value = false; + public function combine( + $key, + $parameters, + $sep, + $combine, + $reserved, + $tag_empty, + $combine_on_empty + ) { + $length = false; + $explode = false; + $skip_final_combine = false; + $value = false; - // Check for length restriction. - if (strpos($key, ":") !== false) { - list($key, $length) = explode(":", $key); - } + // Check for length restriction. + if (strpos($key, ":") !== false) { + list($key, $length) = explode(":", $key); + } - // Check for explode parameter. - if ($key[strlen($key) - 1] == "*") { - $explode = true; - $key = substr($key, 0, -1); - $skip_final_combine = true; - } + // Check for explode parameter. + if ($key[strlen($key) - 1] == "*") { + $explode = true; + $key = substr($key, 0, -1); + $skip_final_combine = true; + } - // Define the list separator. - $list_sep = $explode ? $sep : ","; + // Define the list separator. + $list_sep = $explode ? $sep : ","; - if (isset($parameters[$key])) { - $data_type = $this->getDataType($parameters[$key]); - switch ($data_type) { - case self::TYPE_SCALAR: - $value = $this->getValue($parameters[$key], $length); - break; - case self::TYPE_LIST: - $values = array(); - foreach ($parameters[$key] as $pkey => $pvalue) { - $pvalue = $this->getValue($pvalue, $length); - if ($combine && $explode) { - $values[$pkey] = $key . $combine . $pvalue; - } else { - $values[$pkey] = $pvalue; - } - } - $value = implode($list_sep, $values); - if ($value == '') { - return ''; - } - break; - case self::TYPE_MAP: - $values = array(); - foreach ($parameters[$key] as $pkey => $pvalue) { - $pvalue = $this->getValue($pvalue, $length); - if ($explode) { - $pkey = $this->getValue($pkey, $length); - $values[] = $pkey . "=" . $pvalue; // Explode triggers = combine. - } else { - $values[] = $pkey; - $values[] = $pvalue; + if (isset($parameters[$key])) { + $data_type = $this->getDataType($parameters[$key]); + switch ($data_type) { + case self::TYPE_SCALAR: + $value = $this->getValue($parameters[$key], $length); + break; + case self::TYPE_LIST: + $values = array(); + foreach ($parameters[$key] as $pkey => $pvalue) { + $pvalue = $this->getValue($pvalue, $length); + if ($combine && $explode) { + $values[$pkey] = $key . $combine . $pvalue; + } else { + $values[$pkey] = $pvalue; + } + } + $value = implode($list_sep, $values); + if ($value == '') { + return ''; + } + break; + case self::TYPE_MAP: + $values = array(); + foreach ($parameters[$key] as $pkey => $pvalue) { + $pvalue = $this->getValue($pvalue, $length); + if ($explode) { + $pkey = $this->getValue($pkey, $length); + $values[] = $pkey . "=" . $pvalue; // Explode triggers = combine. + } else { + $values[] = $pkey; + $values[] = $pvalue; + } + } + $value = implode($list_sep, $values); + if ($value == '') { + return false; + } + break; } - } - $value = implode($list_sep, $values); - if ($value == '') { + } else if ($tag_empty) { + // If we are just indicating empty values with their key name, return that. + return $key; + } else { + // Otherwise we can skip this variable due to not being defined. return false; - } - break; - } - } else if ($tag_empty) { - // If we are just indicating empty values with their key name, return that. - return $key; - } else { - // Otherwise we can skip this variable due to not being defined. - return false; - } + } - if ($reserved) { - $value = str_replace($this->reservedEncoded, $this->reserved, $value); - } + if ($reserved) { + $value = str_replace($this->reservedEncoded, $this->reserved, $value); + } - // If we do not need to include the key name, we just return the raw - // value. - if (!$combine || $skip_final_combine) { - return $value; - } + // If we do not need to include the key name, we just return the raw + // value. + if (!$combine || $skip_final_combine) { + return $value; + } - // Else we combine the key name: foo=bar, if value is not the empty string. - return $key . ($value != '' || $combine_on_empty ? $combine . $value : ''); - } + // Else we combine the key name: foo=bar, if value is not the empty string. + return $key . ($value != '' || $combine_on_empty ? $combine . $value : ''); + } - /** - * Return the type of a passed in value - */ - private function getDataType($data) - { - if (is_array($data)) { - reset($data); - if (key($data) !== 0) { - return self::TYPE_MAP; - } - return self::TYPE_LIST; + /** + * Return the type of a passed in value + */ + private function getDataType($data) + { + if (is_array($data)) { + reset($data); + if (key($data) !== 0) { + return self::TYPE_MAP; + } + return self::TYPE_LIST; + } + return self::TYPE_SCALAR; } - return self::TYPE_SCALAR; - } - /** - * Utility function that merges multiple combine calls - * for multi-key templates. - */ - private function combineList( - $vars, - $sep, - $parameters, - $combine, - $reserved, - $tag_empty, - $combine_on_empty - ) { - $ret = array(); - foreach ($vars as $var) { - $response = $this->combine( - $var, - $parameters, - $sep, - $combine, - $reserved, - $tag_empty, - $combine_on_empty - ); - if ($response === false) { - continue; - } - $ret[] = $response; + /** + * Utility function that merges multiple combine calls + * for multi-key templates. + */ + private function combineList( + $vars, + $sep, + $parameters, + $combine, + $reserved, + $tag_empty, + $combine_on_empty + ) { + $ret = array(); + foreach ($vars as $var) { + $response = $this->combine( + $var, + $parameters, + $sep, + $combine, + $reserved, + $tag_empty, + $combine_on_empty + ); + if ($response === false) { + continue; + } + $ret[] = $response; + } + return implode($sep, $ret); } - return implode($sep, $ret); - } - /** - * Utility function to encode and trim values - */ - private function getValue($value, $length) - { - if ($length) { - $value = substr($value, 0, $length); + /** + * Utility function to encode and trim values + */ + private function getValue($value, $length) + { + if ($length) { + $value = substr($value, 0, $length); + } + $value = rawurlencode($value); + return $value; } - $value = rawurlencode($value); - return $value; - } } diff --git a/src/aliases.php b/src/aliases.php index 7bf883730..04154743c 100644 --- a/src/aliases.php +++ b/src/aliases.php @@ -42,24 +42,24 @@ class Google_Task_Composer extends \Google\Task\Composer } if (\false) { - class Google_AccessToken_Revoke extends \Google\AccessToken\Revoke {} - class Google_AccessToken_Verify extends \Google\AccessToken\Verify {} - class Google_AuthHandler_AuthHandlerFactory extends \Google\AuthHandler\AuthHandlerFactory {} - class Google_AuthHandler_Guzzle5AuthHandler extends \Google\AuthHandler\Guzzle5AuthHandler {} - class Google_AuthHandler_Guzzle6AuthHandler extends \Google\AuthHandler\Guzzle6AuthHandler {} - class Google_AuthHandler_Guzzle7AuthHandler extends \Google\AuthHandler\Guzzle7AuthHandler {} - class Google_Client extends \Google\Client {} - class Google_Collection extends \Google\Collection {} - class Google_Exception extends \Google\Exception {} - class Google_Http_Batch extends \Google\Http\Batch {} - class Google_Http_MediaFileUpload extends \Google\Http\MediaFileUpload {} - class Google_Http_REST extends \Google\Http\REST {} - class Google_Model extends \Google\Model {} - class Google_Service extends \Google\Service {} - class Google_Service_Exception extends \Google\Service\Exception {} - class Google_Service_Resource extends \Google\Service\Resource {} - class Google_Task_Exception extends \Google\Task\Exception {} - interface Google_Task_Retryable extends \Google\Task\Retryable {} - class Google_Task_Runner extends \Google\Task\Runner {} - class Google_Utils_UriTemplate extends \Google\Utils\UriTemplate {} + class Google_AccessToken_Revoke extends \Google\AccessToken\Revoke {} + class Google_AccessToken_Verify extends \Google\AccessToken\Verify {} + class Google_AuthHandler_AuthHandlerFactory extends \Google\AuthHandler\AuthHandlerFactory {} + class Google_AuthHandler_Guzzle5AuthHandler extends \Google\AuthHandler\Guzzle5AuthHandler {} + class Google_AuthHandler_Guzzle6AuthHandler extends \Google\AuthHandler\Guzzle6AuthHandler {} + class Google_AuthHandler_Guzzle7AuthHandler extends \Google\AuthHandler\Guzzle7AuthHandler {} + class Google_Client extends \Google\Client {} + class Google_Collection extends \Google\Collection {} + class Google_Exception extends \Google\Exception {} + class Google_Http_Batch extends \Google\Http\Batch {} + class Google_Http_MediaFileUpload extends \Google\Http\MediaFileUpload {} + class Google_Http_REST extends \Google\Http\REST {} + class Google_Model extends \Google\Model {} + class Google_Service extends \Google\Service {} + class Google_Service_Exception extends \Google\Service\Exception {} + class Google_Service_Resource extends \Google\Service\Resource {} + class Google_Task_Exception extends \Google\Task\Exception {} + interface Google_Task_Retryable extends \Google\Task\Retryable {} + class Google_Task_Runner extends \Google\Task\Runner {} + class Google_Utils_UriTemplate extends \Google\Utils\UriTemplate {} } diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 0eaa34aba..84457cb3b 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -27,268 +27,268 @@ use Yoast\PHPUnitPolyfills\TestCases\TestCase; if (trait_exists('\Prophecy\PhpUnit\ProphecyTrait')) { - trait BaseTestTrait + trait BaseTestTrait { - use \Prophecy\PhpUnit\ProphecyTrait; - } + use \Prophecy\PhpUnit\ProphecyTrait; + } } else { - trait BaseTestTrait + trait BaseTestTrait { - } + } } class BaseTest extends TestCase { - private $key; - private $client; - - use BaseTestTrait; - - public function getClient() - { - if (!$this->client) { - $this->client = $this->createClient(); - } + private $key; + private $client; - return $this->client; - } + use BaseTestTrait; - public function getCache($path = null) - { - $path = $path ?: sys_get_temp_dir().'/google-api-php-client-tests/'; - $filesystemAdapter = new Local($path); - $filesystem = new Filesystem($filesystemAdapter); - - return new FilesystemCachePool($filesystem); - } - - private function createClient() - { - $options = [ - 'auth' => 'google_auth', - 'exceptions' => false, - ]; + public function getClient() + { + if (!$this->client) { + $this->client = $this->createClient(); + } - if ($proxy = getenv('HTTP_PROXY')) { - $options['proxy'] = $proxy; - $options['verify'] = false; + return $this->client; } - // adjust constructor depending on guzzle version - if ($this->isGuzzle5()) { - $options = ['defaults' => $options]; + public function getCache($path = null) + { + $path = $path ?: sys_get_temp_dir().'/google-api-php-client-tests/'; + $filesystemAdapter = new Local($path); + $filesystem = new Filesystem($filesystemAdapter); + + return new FilesystemCachePool($filesystem); } - $httpClient = new GuzzleClient($options); - - $client = new Client(); - $client->setApplicationName('google-api-php-client-tests'); - $client->setHttpClient($httpClient); - $client->setScopes( - [ - "https://www.googleapis.com/auth/tasks", - "https://www.googleapis.com/auth/adsense", - "https://www.googleapis.com/auth/youtube", - "https://www.googleapis.com/auth/drive", - ] - ); - - if ($this->key) { - $client->setDeveloperKey($this->key); + private function createClient() + { + $options = [ + 'auth' => 'google_auth', + 'exceptions' => false, + ]; + + if ($proxy = getenv('HTTP_PROXY')) { + $options['proxy'] = $proxy; + $options['verify'] = false; + } + + // adjust constructor depending on guzzle version + if ($this->isGuzzle5()) { + $options = ['defaults' => $options]; + } + + $httpClient = new GuzzleClient($options); + + $client = new Client(); + $client->setApplicationName('google-api-php-client-tests'); + $client->setHttpClient($httpClient); + $client->setScopes( + [ + "https://www.googleapis.com/auth/tasks", + "https://www.googleapis.com/auth/adsense", + "https://www.googleapis.com/auth/youtube", + "https://www.googleapis.com/auth/drive", + ] + ); + + if ($this->key) { + $client->setDeveloperKey($this->key); + } + + list($clientId, $clientSecret) = $this->getClientIdAndSecret(); + $client->setClientId($clientId); + $client->setClientSecret($clientSecret); + if (version_compare(PHP_VERSION, '5.5', '>=')) { + $client->setCache($this->getCache()); + } + + return $client; } - list($clientId, $clientSecret) = $this->getClientIdAndSecret(); - $client->setClientId($clientId); - $client->setClientSecret($clientSecret); - if (version_compare(PHP_VERSION, '5.5', '>=')) { - $client->setCache($this->getCache()); + public function checkToken() + { + $client = $this->getClient(); + $cache = $client->getCache(); + $cacheItem = $cache->getItem('access_token'); + + if (!$token = $cacheItem->get()) { + if (!$token = $this->tryToGetAnAccessToken($client)) { + return $this->markTestSkipped("Test requires access token"); + } + $cacheItem->set($token); + $cache->save($cacheItem); + } + + $client->setAccessToken($token); + + if ($client->isAccessTokenExpired()) { + // as long as we have client credentials, even if its expired + // our access token will automatically be refreshed + $this->checkClientCredentials(); + } + + return true; } - return $client; - } - - public function checkToken() - { - $client = $this->getClient(); - $cache = $client->getCache(); - $cacheItem = $cache->getItem('access_token'); - - if (!$token = $cacheItem->get()) { - if (!$token = $this->tryToGetAnAccessToken($client)) { - return $this->markTestSkipped("Test requires access token"); - } - $cacheItem->set($token); - $cache->save($cacheItem); + public function tryToGetAnAccessToken(Client $client) + { + $this->checkClientCredentials(); + + $client->setRedirectUri("urn:ietf:wg:oauth:2.0:oob"); + $client->setConfig('access_type', 'offline'); + $authUrl = $client->createAuthUrl(); + echo "\nGo to: $authUrl\n"; + echo "\nPlease enter the auth code:\n"; + ob_flush(); + `open '$authUrl'`; + $authCode = trim(fgets(STDIN)); + + if ($accessToken = $client->fetchAccessTokenWithAuthCode($authCode)) { + if (isset($accessToken['access_token'])) { + return $accessToken; + } + } + + return false; } - $client->setAccessToken($token); + private function getClientIdAndSecret() + { + $clientId = getenv('GOOGLE_CLIENT_ID') ?: null; + $clientSecret = getenv('GOOGLE_CLIENT_SECRET') ?: null; - if ($client->isAccessTokenExpired()) { - // as long as we have client credentials, even if its expired - // our access token will automatically be refreshed - $this->checkClientCredentials(); + return array($clientId, $clientSecret); } - return true; - } - - public function tryToGetAnAccessToken(Client $client) - { - $this->checkClientCredentials(); - - $client->setRedirectUri("urn:ietf:wg:oauth:2.0:oob"); - $client->setConfig('access_type', 'offline'); - $authUrl = $client->createAuthUrl(); - echo "\nGo to: $authUrl\n"; - echo "\nPlease enter the auth code:\n"; - ob_flush(); - `open '$authUrl'`; - $authCode = trim(fgets(STDIN)); - - if ($accessToken = $client->fetchAccessTokenWithAuthCode($authCode)) { - if (isset($accessToken['access_token'])) { - return $accessToken; - } + protected function checkClientCredentials() + { + list($clientId, $clientSecret) = $this->getClientIdAndSecret(); + if (!($clientId && $clientSecret)) { + $this->markTestSkipped("Test requires GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET to be set"); + } } - return false; - } - - private function getClientIdAndSecret() - { - $clientId = getenv('GOOGLE_CLIENT_ID') ?: null; - $clientSecret = getenv('GOOGLE_CLIENT_SECRET') ?: null; - - return array($clientId, $clientSecret); - } + protected function checkServiceAccountCredentials() + { + if (!$f = getenv('GOOGLE_APPLICATION_CREDENTIALS')) { + $skip = "This test requires the GOOGLE_APPLICATION_CREDENTIALS environment variable to be set\n" + . "see https://developers.google.com/accounts/docs/application-default-credentials"; + $this->markTestSkipped($skip); - protected function checkClientCredentials() - { - list($clientId, $clientSecret) = $this->getClientIdAndSecret(); - if (!($clientId && $clientSecret)) { - $this->markTestSkipped("Test requires GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET to be set"); - } - } + return false; + } - protected function checkServiceAccountCredentials() - { - if (!$f = getenv('GOOGLE_APPLICATION_CREDENTIALS')) { - $skip = "This test requires the GOOGLE_APPLICATION_CREDENTIALS environment variable to be set\n" - . "see https://developers.google.com/accounts/docs/application-default-credentials"; - $this->markTestSkipped($skip); + if (!file_exists($f)) { + $this->markTestSkipped('invalid path for GOOGLE_APPLICATION_CREDENTIALS'); + } - return false; + return true; } - if (!file_exists($f)) { - $this->markTestSkipped('invalid path for GOOGLE_APPLICATION_CREDENTIALS'); + protected function checkKey() + { + 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; } - return true; - } - - protected function checkKey() - { - 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) - { - // trick app into thinking we are a web server - $_SERVER['HTTP_USER_AGENT'] = 'google-api-php-client-tests'; - $_SERVER['HTTP_HOST'] = 'localhost'; - $_SERVER['REQUEST_METHOD'] = 'GET'; - - // include the file and return an HTML crawler - $file = __DIR__ . '/../examples/' . $example; - if (is_file($file)) { - ob_start(); - include $file; - $html = ob_get_clean(); - - return new Crawler($html); + protected function loadExample($example) + { + // trick app into thinking we are a web server + $_SERVER['HTTP_USER_AGENT'] = 'google-api-php-client-tests'; + $_SERVER['HTTP_HOST'] = 'localhost'; + $_SERVER['REQUEST_METHOD'] = 'GET'; + + // include the file and return an HTML crawler + $file = __DIR__ . '/../examples/' . $example; + if (is_file($file)) { + ob_start(); + include $file; + $html = ob_get_clean(); + + return new Crawler($html); + } + + return false; } - return false; - } + protected function isGuzzle7() + { + if (!defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) { + return false; + } - protected function isGuzzle7() - { - if (!defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) { - return false; + return (7 === ClientInterface::MAJOR_VERSION); } - return (7 === ClientInterface::MAJOR_VERSION); - } + protected function isGuzzle6() + { + if (!defined('\GuzzleHttp\ClientInterface::VERSION')) { + return false; + } + $version = ClientInterface::VERSION; - protected function isGuzzle6() - { - if (!defined('\GuzzleHttp\ClientInterface::VERSION')) { - return false; + return ('6' === $version[0]); } - $version = ClientInterface::VERSION; - return ('6' === $version[0]); - } - - protected function isGuzzle5() - { - if (!defined('\GuzzleHttp\ClientInterface::VERSION')) { - return false; - } + protected function isGuzzle5() + { + if (!defined('\GuzzleHttp\ClientInterface::VERSION')) { + return false; + } - $version = ClientInterface::VERSION; + $version = ClientInterface::VERSION; - return ('5' === $version[0]); - } + return ('5' === $version[0]); + } - public function onlyGuzzle6() - { - if (!$this->isGuzzle6()) { - $this->markTestSkipped('Guzzle 6 only'); + public function onlyGuzzle6() + { + if (!$this->isGuzzle6()) { + $this->markTestSkipped('Guzzle 6 only'); + } } - } - public function onlyPhp55AndAbove() - { - if (version_compare(PHP_VERSION, '5.5', '<')) { - $this->markTestSkipped('PHP 5.5 and above only'); + public function onlyPhp55AndAbove() + { + if (version_compare(PHP_VERSION, '5.5', '<')) { + $this->markTestSkipped('PHP 5.5 and above only'); + } } - } - public function onlyGuzzle5() - { - if (!$this->isGuzzle5()) { - $this->markTestSkipped('Guzzle 5 only'); + public function onlyGuzzle5() + { + if (!$this->isGuzzle5()) { + $this->markTestSkipped('Guzzle 5 only'); + } } - } - public function onlyGuzzle6Or7() - { - if (!$this->isGuzzle6() && !$this->isGuzzle7()) { - $this->markTestSkipped('Guzzle 6 or 7 only'); + public function onlyGuzzle6Or7() + { + if (!$this->isGuzzle6() && !$this->isGuzzle7()) { + $this->markTestSkipped('Guzzle 6 or 7 only'); + } } - } - protected function getGuzzle5ResponseMock() - { - $response = $this->prophesize('GuzzleHttp\Message\ResponseInterface'); - $response->getStatusCode() - ->willReturn(200); + protected function getGuzzle5ResponseMock() + { + $response = $this->prophesize('GuzzleHttp\Message\ResponseInterface'); + $response->getStatusCode() + ->willReturn(200); - $response->getHeaders()->willReturn([]); - $response->getBody()->willReturn(''); - $response->getProtocolVersion()->willReturn(''); - $response->getReasonPhrase()->willReturn(''); + $response->getHeaders()->willReturn([]); + $response->getBody()->willReturn(''); + $response->getProtocolVersion()->willReturn(''); + $response->getReasonPhrase()->willReturn(''); - return $response; - } + return $response; + } } diff --git a/tests/Google/AccessToken/VerifyTest.php b/tests/Google/AccessToken/VerifyTest.php index f9c75fb5c..25a7224b2 100644 --- a/tests/Google/AccessToken/VerifyTest.php +++ b/tests/Google/AccessToken/VerifyTest.php @@ -27,134 +27,134 @@ class VerifyTest extends BaseTest { - /** - * This test needs to run before the other verify tests, - * to ensure the constants are not defined. - */ - public function testPhpsecConstants() - { - $client = $this->getClient(); - $verify = new Verify($client->getHttpClient()); - - // set these to values that will be changed - if (defined('MATH_BIGINTEGER_OPENSSL_ENABLED') || defined('CRYPT_RSA_MODE')) { - $this->markTestSkipped('Cannot run test - constants already defined'); + /** + * This test needs to run before the other verify tests, + * to ensure the constants are not defined. + */ + public function testPhpsecConstants() + { + $client = $this->getClient(); + $verify = new Verify($client->getHttpClient()); + + // set these to values that will be changed + if (defined('MATH_BIGINTEGER_OPENSSL_ENABLED') || defined('CRYPT_RSA_MODE')) { + $this->markTestSkipped('Cannot run test - constants already defined'); + } + + // Pretend we are on App Engine VMs + putenv('GAE_VM=1'); + + $verify->verifyIdToken('a.b.c'); + + putenv('GAE_VM=0'); + + $openSslEnable = constant('MATH_BIGINTEGER_OPENSSL_ENABLED'); + $rsaMode = constant('CRYPT_RSA_MODE'); + $this->assertTrue($openSslEnable); + $this->assertEquals(constant($this->getOpenSslConstant()), $rsaMode); } - // Pretend we are on App Engine VMs - putenv('GAE_VM=1'); - - $verify->verifyIdToken('a.b.c'); - - putenv('GAE_VM=0'); - - $openSslEnable = constant('MATH_BIGINTEGER_OPENSSL_ENABLED'); - $rsaMode = constant('CRYPT_RSA_MODE'); - $this->assertTrue($openSslEnable); - $this->assertEquals(constant($this->getOpenSslConstant()), $rsaMode); - } - - /** - * Most of the logic for ID token validation is in AuthTest - - * this is just a general check to ensure we verify a valid - * id token if one exists. - */ - public function testValidateIdToken() - { - $this->checkToken(); - - $jwt = $this->getJwtService(); - $client = $this->getClient(); - $http = $client->getHttpClient(); - $token = $client->getAccessToken(); - if ($client->isAccessTokenExpired()) { - $token = $client->fetchAccessTokenWithRefreshToken(); + /** + * Most of the logic for ID token validation is in AuthTest - + * this is just a general check to ensure we verify a valid + * id token if one exists. + */ + public function testValidateIdToken() + { + $this->checkToken(); + + $jwt = $this->getJwtService(); + $client = $this->getClient(); + $http = $client->getHttpClient(); + $token = $client->getAccessToken(); + if ($client->isAccessTokenExpired()) { + $token = $client->fetchAccessTokenWithRefreshToken(); + } + $segments = explode('.', $token['id_token']); + $this->assertCount(3, $segments); + // Extract the client ID in this case as it wont be set on the test client. + $data = json_decode($jwt->urlSafeB64Decode($segments[1])); + $verify = new Verify($http); + $payload = $verify->verifyIdToken($token['id_token'], $data->aud); + $this->assertArrayHasKey('sub', $payload); + $this->assertGreaterThan(0, strlen($payload['sub'])); + + // TODO: Need to be smart about testing/disabling the + // caching for this test to make sense. Not sure how to do that + // at the moment. + $client = $this->getClient(); + $http = $client->getHttpClient(); + $data = json_decode($jwt->urlSafeB64Decode($segments[1])); + $verify = new Verify($http); + $payload = $verify->verifyIdToken($token['id_token'], $data->aud); + $this->assertArrayHasKey('sub', $payload); + $this->assertGreaterThan(0, strlen($payload['sub'])); } - $segments = explode('.', $token['id_token']); - $this->assertCount(3, $segments); - // Extract the client ID in this case as it wont be set on the test client. - $data = json_decode($jwt->urlSafeB64Decode($segments[1])); - $verify = new Verify($http); - $payload = $verify->verifyIdToken($token['id_token'], $data->aud); - $this->assertArrayHasKey('sub', $payload); - $this->assertGreaterThan(0, strlen($payload['sub'])); - - // TODO: Need to be smart about testing/disabling the - // caching for this test to make sense. Not sure how to do that - // at the moment. - $client = $this->getClient(); - $http = $client->getHttpClient(); - $data = json_decode($jwt->urlSafeB64Decode($segments[1])); - $verify = new Verify($http); - $payload = $verify->verifyIdToken($token['id_token'], $data->aud); - $this->assertArrayHasKey('sub', $payload); - $this->assertGreaterThan(0, strlen($payload['sub'])); - } - - /** - * Most of the logic for ID token validation is in AuthTest - - * this is just a general check to ensure we verify a valid - * id token if one exists. - */ - public function testLeewayIsUnchangedWhenPassingInJwt() - { - $this->checkToken(); - - $jwt = $this->getJwtService(); - // set arbitrary leeway so we can check this later - $jwt::$leeway = $leeway = 1.5; - $client = $this->getClient(); - $token = $client->getAccessToken(); - if ($client->isAccessTokenExpired()) { - $token = $client->fetchAccessTokenWithRefreshToken(); - } - $segments = explode('.', $token['id_token']); - $this->assertCount(3, $segments); - // Extract the client ID in this case as it wont be set on the test client. - $data = json_decode($jwt->urlSafeB64Decode($segments[1])); - $verify = new Verify($client->getHttpClient(), null, $jwt); - $payload = $verify->verifyIdToken($token['id_token'], $data->aud); - // verify the leeway is set as it was - $this->assertEquals($leeway, $jwt::$leeway); - } - - public function testRetrieveCertsFromLocation() - { - $client = $this->getClient(); - $verify = new Verify($client->getHttpClient()); - - // make this method public for testing purposes - $method = new ReflectionMethod($verify, 'retrieveCertsFromLocation'); - $method->setAccessible(true); - $certs = $method->invoke($verify, Verify::FEDERATED_SIGNON_CERT_URL); - - $this->assertArrayHasKey('keys', $certs); - $this->assertGreaterThan(1, count($certs['keys'])); - $this->assertArrayHasKey('alg', $certs['keys'][0]); - $this->assertEquals('RS256', $certs['keys'][0]['alg']); - } - - private function getJwtService() - { - if (class_exists('\Firebase\JWT\JWT')) { - return new \Firebase\JWT\JWT; + + /** + * Most of the logic for ID token validation is in AuthTest - + * this is just a general check to ensure we verify a valid + * id token if one exists. + */ + public function testLeewayIsUnchangedWhenPassingInJwt() + { + $this->checkToken(); + + $jwt = $this->getJwtService(); + // set arbitrary leeway so we can check this later + $jwt::$leeway = $leeway = 1.5; + $client = $this->getClient(); + $token = $client->getAccessToken(); + if ($client->isAccessTokenExpired()) { + $token = $client->fetchAccessTokenWithRefreshToken(); + } + $segments = explode('.', $token['id_token']); + $this->assertCount(3, $segments); + // Extract the client ID in this case as it wont be set on the test client. + $data = json_decode($jwt->urlSafeB64Decode($segments[1])); + $verify = new Verify($client->getHttpClient(), null, $jwt); + $payload = $verify->verifyIdToken($token['id_token'], $data->aud); + // verify the leeway is set as it was + $this->assertEquals($leeway, $jwt::$leeway); } - return new \JWT; - } + public function testRetrieveCertsFromLocation() + { + $client = $this->getClient(); + $verify = new Verify($client->getHttpClient()); + + // make this method public for testing purposes + $method = new ReflectionMethod($verify, 'retrieveCertsFromLocation'); + $method->setAccessible(true); + $certs = $method->invoke($verify, Verify::FEDERATED_SIGNON_CERT_URL); - private function getOpenSslConstant() - { - if (class_exists('phpseclib3\Crypt\AES')) { - return 'phpseclib3\Crypt\AES::ENGINE_OPENSSL'; + $this->assertArrayHasKey('keys', $certs); + $this->assertGreaterThan(1, count($certs['keys'])); + $this->assertArrayHasKey('alg', $certs['keys'][0]); + $this->assertEquals('RS256', $certs['keys'][0]['alg']); } - if (class_exists('phpseclib\Crypt\RSA')) { - return 'phpseclib\Crypt\RSA::MODE_OPENSSL'; + private function getJwtService() + { + if (class_exists('\Firebase\JWT\JWT')) { + return new \Firebase\JWT\JWT; + } + + return new \JWT; } - if (class_exists('Crypt_RSA')) { - return 'CRYPT_RSA_MODE_OPENSSL'; + private function getOpenSslConstant() + { + if (class_exists('phpseclib3\Crypt\AES')) { + return 'phpseclib3\Crypt\AES::ENGINE_OPENSSL'; + } + + if (class_exists('phpseclib\Crypt\RSA')) { + return 'phpseclib\Crypt\RSA::MODE_OPENSSL'; + } + + if (class_exists('Crypt_RSA')) { + return 'CRYPT_RSA_MODE_OPENSSL'; + } } - } } diff --git a/tests/Google/CacheTest.php b/tests/Google/CacheTest.php index 2c1af30e4..e6743a45c 100644 --- a/tests/Google/CacheTest.php +++ b/tests/Google/CacheTest.php @@ -27,77 +27,77 @@ class CacheTest extends BaseTest { - public function testInMemoryCache() - { - $this->checkServiceAccountCredentials(); - - $client = $this->getClient(); - $client->useApplicationDefaultCredentials(); - $client->setAccessType('offline'); - $client->setScopes(['https://www.googleapis.com/auth/drive.readonly']); - $client->setCache(new MemoryCacheItemPool); - - /* Refresh token when expired */ - if ($client->isAccessTokenExpired()) { - $client->refreshTokenWithAssertion(); + public function testInMemoryCache() + { + $this->checkServiceAccountCredentials(); + + $client = $this->getClient(); + $client->useApplicationDefaultCredentials(); + $client->setAccessType('offline'); + $client->setScopes(['https://www.googleapis.com/auth/drive.readonly']); + $client->setCache(new MemoryCacheItemPool); + + /* Refresh token when expired */ + if ($client->isAccessTokenExpired()) { + $client->refreshTokenWithAssertion(); + } + + /* Make a service call */ + $service = new Drive($client); + $files = $service->files->listFiles(); + $this->assertInstanceOf('Google_Service_Drive_FileList', $files); } - /* Make a service call */ - $service = new Drive($client); - $files = $service->files->listFiles(); - $this->assertInstanceOf('Google_Service_Drive_FileList', $files); - } - - public function testFileCache() - { - $this->onlyPhp55AndAbove(); - $this->checkServiceAccountCredentials(); - - $client = new Client(); - $client->useApplicationDefaultCredentials(); - $client->setScopes(['https://www.googleapis.com/auth/drive.readonly']); - // filecache with new cache dir - $cache = $this->getCache(sys_get_temp_dir() . '/cloud-samples-tests-php-cache-test/'); - $client->setCache($cache); - - $token1 = null; - $client->setTokenCallback(function($cacheKey, $accessToken) use ($cache, &$token1) { - $token1 = $accessToken; - $cacheItem = $cache->getItem($cacheKey); - // expire the item - $cacheItem->expiresAt(new DateTime('now -1 second')); - $cache->save($cacheItem); - - $cacheItem2 = $cache->getItem($cacheKey); - }); - - /* Refresh token when expired */ - if ($client->isAccessTokenExpired()) { - $client->refreshTokenWithAssertion(); + public function testFileCache() + { + $this->onlyPhp55AndAbove(); + $this->checkServiceAccountCredentials(); + + $client = new Client(); + $client->useApplicationDefaultCredentials(); + $client->setScopes(['https://www.googleapis.com/auth/drive.readonly']); + // filecache with new cache dir + $cache = $this->getCache(sys_get_temp_dir() . '/cloud-samples-tests-php-cache-test/'); + $client->setCache($cache); + + $token1 = null; + $client->setTokenCallback(function ($cacheKey, $accessToken) use ($cache, &$token1) { + $token1 = $accessToken; + $cacheItem = $cache->getItem($cacheKey); + // expire the item + $cacheItem->expiresAt(new DateTime('now -1 second')); + $cache->save($cacheItem); + + $cacheItem2 = $cache->getItem($cacheKey); + }); + + /* Refresh token when expired */ + if ($client->isAccessTokenExpired()) { + $client->refreshTokenWithAssertion(); + } + + /* Make a service call */ + $service = new Drive($client); + $files = $service->files->listFiles(); + $this->assertInstanceOf(Drive\FileList::class, $files); + + sleep(2); + + // make sure the token expires + $client = new Client(); + $client->useApplicationDefaultCredentials(); + $client->setScopes(['https://www.googleapis.com/auth/drive.readonly']); + $client->setCache($cache); + $token2 = null; + $client->setTokenCallback(function ($cacheKey, $accessToken) use (&$token2) { + $token2 = $accessToken; + }); + + /* Make another service call */ + $service = new Drive($client); + $files = $service->files->listFiles(); + $this->assertInstanceOf(Drive\FileList::class, $files); + + $this->assertNotEquals($token1, $token2); } - - /* Make a service call */ - $service = new Drive($client); - $files = $service->files->listFiles(); - $this->assertInstanceOf(Drive\FileList::class, $files); - - sleep(2); - - // make sure the token expires - $client = new Client(); - $client->useApplicationDefaultCredentials(); - $client->setScopes(['https://www.googleapis.com/auth/drive.readonly']); - $client->setCache($cache); - $token2 = null; - $client->setTokenCallback(function($cacheKey, $accessToken) use (&$token2) { - $token2 = $accessToken; - }); - - /* Make another service call */ - $service = new Drive($client); - $files = $service->files->listFiles(); - $this->assertInstanceOf(Drive\FileList::class, $files); - - $this->assertNotEquals($token1, $token2); - } } diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 8968c14a7..3e604b510 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -40,976 +40,979 @@ class ClientTest extends BaseTest { - public function testClientConstructor() - { - $this->assertInstanceOf(Client::class, $this->getClient()); - } - - public function testSignAppKey() - { - $client = $this->getClient(); - $client->setDeveloperKey('devKey'); - - $http = new GuzzleClient(); - $client->authorize($http); - - $this->checkAuthHandler($http, 'Simple'); - } - - private function checkAuthHandler($http, $className) - { - if ($this->isGuzzle6() || $this->isGuzzle7()) { - $stack = $http->getConfig('handler'); - $class = new ReflectionClass(get_class($stack)); - $property = $class->getProperty('stack'); - $property->setAccessible(true); - $middlewares = $property->getValue($stack); - $middleware = array_pop($middlewares); - - if (null === $className) { - // only the default middlewares have been added - $this->assertCount(3, $middlewares); - } else { - $authClass = sprintf('Google\Auth\Middleware\%sMiddleware', $className); - $this->assertInstanceOf($authClass, $middleware[0]); - } - } else { - $listeners = $http->getEmitter()->listeners('before'); - - if (null === $className) { - $this->assertCount(0, $listeners); - } else { - $authClass = sprintf('Google\Auth\Subscriber\%sSubscriber', $className); - $this->assertCount(1, $listeners); - $this->assertCount(2, $listeners[0]); - $this->assertInstanceOf($authClass, $listeners[0][0]); - } - } - } - - private function checkCredentials($http, $fetcherClass, $sub = null) - { - if ($this->isGuzzle6() || $this->isGuzzle7()) { - $stack = $http->getConfig('handler'); - $class = new ReflectionClass(get_class($stack)); - $property = $class->getProperty('stack'); - $property->setAccessible(true); - $middlewares = $property->getValue($stack); // Works - $middleware = array_pop($middlewares); - $auth = $middleware[0]; - } else { - // access the protected $fetcher property - $listeners = $http->getEmitter()->listeners('before'); - $auth = $listeners[0][0]; - } - - $class = new ReflectionClass(get_class($auth)); - $property = $class->getProperty('fetcher'); - $property->setAccessible(true); - $cacheFetcher = $property->getValue($auth); - $this->assertInstanceOf(FetchAuthTokenCache::class, $cacheFetcher); - - $class = new ReflectionClass(get_class($cacheFetcher)); - $property = $class->getProperty('fetcher'); - $property->setAccessible(true); - $fetcher = $property->getValue($cacheFetcher); - $this->assertInstanceOf($fetcherClass, $fetcher); - - if ($sub) { - // access the protected $auth property - $class = new ReflectionClass(get_class($fetcher)); - $property = $class->getProperty('auth'); - $property->setAccessible(true); - $auth = $property->getValue($fetcher); - - $this->assertEquals($sub, $auth->getSub()); - } - } - - public function testSignAccessToken() - { - $client = $this->getClient(); - - $http = new GuzzleClient(); - $client->setAccessToken([ - 'access_token' => 'test_token', - 'expires_in' => 3600, - 'created' => time(), - ]); - $client->setScopes('test_scope'); - $client->authorize($http); - - $this->checkAuthHandler($http, 'ScopedAccessToken'); - } - - public function testCreateAuthUrl() - { - $client = $this->getClient(); - - $client->setClientId('clientId1'); - $client->setClientSecret('clientSecret1'); - $client->setRedirectUri('http://localhost/'); - $client->setDeveloperKey('devKey'); - $client->setState('xyz'); - $client->setAccessType('offline'); - $client->setApprovalPrompt('force'); - $client->setRequestVisibleActions('http://foo/'); - $client->setLoginHint('bob@example.org'); - - $authUrl = $client->createAuthUrl("http://googleapis.com/scope/foo"); - $expected = "https://accounts.google.com/o/oauth2/auth" - . "?response_type=code" - . "&access_type=offline" - . "&client_id=clientId1" - . "&redirect_uri=http%3A%2F%2Flocalhost" - . "&state=xyz" - . "&scope=http%3A%2F%2Fgoogleapis.com%2Fscope%2Ffoo" - . "&approval_prompt=force" - . "&login_hint=bob%40example.org"; - - $this->assertEquals($expected, $authUrl); - - // Again with a blank login hint (should remove all traces from authUrl) - $client->setLoginHint(''); - $client->setHostedDomain('example.com'); - $client->setOpenIdRealm('example.com'); - $client->setPrompt('select_account'); - $client->setIncludeGrantedScopes(true); - $authUrl = $client->createAuthUrl("http://googleapis.com/scope/foo"); - $expected = "https://accounts.google.com/o/oauth2/auth" - . "?response_type=code" - . "&access_type=offline" - . "&client_id=clientId1" - . "&redirect_uri=http%3A%2F%2Flocalhost" - . "&state=xyz" - . "&scope=http%3A%2F%2Fgoogleapis.com%2Fscope%2Ffoo" - . "&hd=example.com" - . "&include_granted_scopes=true" - . "&openid.realm=example.com" - . "&prompt=select_account"; - - $this->assertEquals($expected, $authUrl); - } - - public function testPrepareNoScopes() - { - $client = new Client(); - - $scopes = $client->prepareScopes(); - $this->assertNull($scopes); - } - - public function testNoAuthIsNull() - { - $client = new Client(); - - $this->assertNull($client->getAccessToken()); - } - - public function testPrepareService() - { - $this->onlyGuzzle6Or7(); - - $client = new Client(); - $client->setScopes(array("scope1", "scope2")); - $scopes = $client->prepareScopes(); - $this->assertEquals("scope1 scope2", $scopes); - - $client->setScopes(array("", "scope2")); - $scopes = $client->prepareScopes(); - $this->assertEquals(" scope2", $scopes); - - $client->setScopes("scope2"); - $client->addScope("scope3"); - $client->addScope(array("scope4", "scope5")); - $scopes = $client->prepareScopes(); - $this->assertEquals("scope2 scope3 scope4 scope5", $scopes); - - $client->setClientId('test1'); - $client->setRedirectUri('http://localhost/'); - $client->setState('xyz'); - $client->setScopes(array("http://test.com/", "scope2")); - $scopes = $client->prepareScopes(); - $this->assertEquals("http://test.com scope2", $scopes); - $this->assertEquals( - '' - . 'https://accounts.google.com/o/oauth2/auth' - . '?response_type=code' - . '&access_type=online' - . '&client_id=test1' - . '&redirect_uri=http%3A%2F%2Flocalhost%2F' - . '&state=xyz' - . '&scope=http%3A%2F%2Ftest.com%20scope2' - . '&approval_prompt=auto', - - $client->createAuthUrl() - ); - - $stream = $this->prophesize('GuzzleHttp\Psr7\Stream'); - $stream->__toString()->willReturn(''); - - $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); - $response->getBody() + public function testClientConstructor() + { + $this->assertInstanceOf(Client::class, $this->getClient()); + } + + public function testSignAppKey() + { + $client = $this->getClient(); + $client->setDeveloperKey('devKey'); + + $http = new GuzzleClient(); + $client->authorize($http); + + $this->checkAuthHandler($http, 'Simple'); + } + + private function checkAuthHandler($http, $className) + { + if ($this->isGuzzle6() || $this->isGuzzle7()) { + $stack = $http->getConfig('handler'); + $class = new ReflectionClass(get_class($stack)); + $property = $class->getProperty('stack'); + $property->setAccessible(true); + $middlewares = $property->getValue($stack); + $middleware = array_pop($middlewares); + + if (null === $className) { + // only the default middlewares have been added + $this->assertCount(3, $middlewares); + } else { + $authClass = sprintf('Google\Auth\Middleware\%sMiddleware', $className); + $this->assertInstanceOf($authClass, $middleware[0]); + } + } else { + $listeners = $http->getEmitter()->listeners('before'); + + if (null === $className) { + $this->assertCount(0, $listeners); + } else { + $authClass = sprintf('Google\Auth\Subscriber\%sSubscriber', $className); + $this->assertCount(1, $listeners); + $this->assertCount(2, $listeners[0]); + $this->assertInstanceOf($authClass, $listeners[0][0]); + } + } + } + + private function checkCredentials($http, $fetcherClass, $sub = null) + { + if ($this->isGuzzle6() || $this->isGuzzle7()) { + $stack = $http->getConfig('handler'); + $class = new ReflectionClass(get_class($stack)); + $property = $class->getProperty('stack'); + $property->setAccessible(true); + $middlewares = $property->getValue($stack); // Works + $middleware = array_pop($middlewares); + $auth = $middleware[0]; + } else { + // access the protected $fetcher property + $listeners = $http->getEmitter()->listeners('before'); + $auth = $listeners[0][0]; + } + + $class = new ReflectionClass(get_class($auth)); + $property = $class->getProperty('fetcher'); + $property->setAccessible(true); + $cacheFetcher = $property->getValue($auth); + $this->assertInstanceOf(FetchAuthTokenCache::class, $cacheFetcher); + + $class = new ReflectionClass(get_class($cacheFetcher)); + $property = $class->getProperty('fetcher'); + $property->setAccessible(true); + $fetcher = $property->getValue($cacheFetcher); + $this->assertInstanceOf($fetcherClass, $fetcher); + + if ($sub) { + // access the protected $auth property + $class = new ReflectionClass(get_class($fetcher)); + $property = $class->getProperty('auth'); + $property->setAccessible(true); + $auth = $property->getValue($fetcher); + + $this->assertEquals($sub, $auth->getSub()); + } + } + + public function testSignAccessToken() + { + $client = $this->getClient(); + + $http = new GuzzleClient(); + $client->setAccessToken([ + 'access_token' => 'test_token', + 'expires_in' => 3600, + 'created' => time(), + ]); + $client->setScopes('test_scope'); + $client->authorize($http); + + $this->checkAuthHandler($http, 'ScopedAccessToken'); + } + + public function testCreateAuthUrl() + { + $client = $this->getClient(); + + $client->setClientId('clientId1'); + $client->setClientSecret('clientSecret1'); + $client->setRedirectUri('http://localhost/'); + $client->setDeveloperKey('devKey'); + $client->setState('xyz'); + $client->setAccessType('offline'); + $client->setApprovalPrompt('force'); + $client->setRequestVisibleActions('http://foo/'); + $client->setLoginHint('bob@example.org'); + + $authUrl = $client->createAuthUrl("http://googleapis.com/scope/foo"); + $expected = "https://accounts.google.com/o/oauth2/auth" + . "?response_type=code" + . "&access_type=offline" + . "&client_id=clientId1" + . "&redirect_uri=http%3A%2F%2Flocalhost" + . "&state=xyz" + . "&scope=http%3A%2F%2Fgoogleapis.com%2Fscope%2Ffoo" + . "&approval_prompt=force" + . "&login_hint=bob%40example.org"; + + $this->assertEquals($expected, $authUrl); + + // Again with a blank login hint (should remove all traces from authUrl) + $client->setLoginHint(''); + $client->setHostedDomain('example.com'); + $client->setOpenIdRealm('example.com'); + $client->setPrompt('select_account'); + $client->setIncludeGrantedScopes(true); + $authUrl = $client->createAuthUrl("http://googleapis.com/scope/foo"); + $expected = "https://accounts.google.com/o/oauth2/auth" + . "?response_type=code" + . "&access_type=offline" + . "&client_id=clientId1" + . "&redirect_uri=http%3A%2F%2Flocalhost" + . "&state=xyz" + . "&scope=http%3A%2F%2Fgoogleapis.com%2Fscope%2Ffoo" + . "&hd=example.com" + . "&include_granted_scopes=true" + . "&openid.realm=example.com" + . "&prompt=select_account"; + + $this->assertEquals($expected, $authUrl); + } + + public function testPrepareNoScopes() + { + $client = new Client(); + + $scopes = $client->prepareScopes(); + $this->assertNull($scopes); + } + + public function testNoAuthIsNull() + { + $client = new Client(); + + $this->assertNull($client->getAccessToken()); + } + + public function testPrepareService() + { + $this->onlyGuzzle6Or7(); + + $client = new Client(); + $client->setScopes(array("scope1", "scope2")); + $scopes = $client->prepareScopes(); + $this->assertEquals("scope1 scope2", $scopes); + + $client->setScopes(array("", "scope2")); + $scopes = $client->prepareScopes(); + $this->assertEquals(" scope2", $scopes); + + $client->setScopes("scope2"); + $client->addScope("scope3"); + $client->addScope(array("scope4", "scope5")); + $scopes = $client->prepareScopes(); + $this->assertEquals("scope2 scope3 scope4 scope5", $scopes); + + $client->setClientId('test1'); + $client->setRedirectUri('http://localhost/'); + $client->setState('xyz'); + $client->setScopes(array("http://test.com/", "scope2")); + $scopes = $client->prepareScopes(); + $this->assertEquals("http://test.com scope2", $scopes); + $this->assertEquals( + '' + . 'https://accounts.google.com/o/oauth2/auth' + . '?response_type=code' + . '&access_type=online' + . '&client_id=test1' + . '&redirect_uri=http%3A%2F%2Flocalhost%2F' + . '&state=xyz' + . '&scope=http%3A%2F%2Ftest.com%20scope2' + . '&approval_prompt=auto', + $client->createAuthUrl() + ); + + $stream = $this->prophesize('GuzzleHttp\Psr7\Stream'); + $stream->__toString()->willReturn(''); + + $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); + $response->getBody() ->shouldBeCalledTimes(1) ->willReturn($stream->reveal()); - $response->getStatusCode()->willReturn(200); + $response->getStatusCode()->willReturn(200); - $http = $this->prophesize('GuzzleHttp\ClientInterface'); + $http = $this->prophesize('GuzzleHttp\ClientInterface'); - $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) ->shouldBeCalledTimes(1) ->willReturn($response->reveal()); - $client->setHttpClient($http->reveal()); - $dr_service = new Drive($client); - $this->assertInstanceOf('Google\Model', $dr_service->files->listFiles()); - } - - public function testDefaultLogger() - { - $client = new Client(); - $logger = $client->getLogger(); - $this->assertInstanceOf('Monolog\Logger', $logger); - $handler = $logger->popHandler(); - $this->assertInstanceOf('Monolog\Handler\StreamHandler', $handler); - } - - public function testDefaultLoggerAppEngine() - { - $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine'; - $client = new Client(); - $logger = $client->getLogger(); - $handler = $logger->popHandler(); - unset($_SERVER['SERVER_SOFTWARE']); - - $this->assertInstanceOf('Monolog\Logger', $logger); - $this->assertInstanceOf('Monolog\Handler\SyslogHandler', $handler); - } - - public function testSettersGetters() - { - $client = new Client(); - $client->setClientId("client1"); - $client->setClientSecret('client1secret'); - $client->setState('1'); - $client->setApprovalPrompt('force'); - $client->setAccessType('offline'); - - $client->setRedirectUri('localhost'); - $client->setConfig('application_name', 'me'); - - $cache = $this->prophesize(CacheItemPoolInterface::class); - $client->setCache($cache->reveal()); - $this->assertInstanceOf(CacheItemPoolInterface::class, $client->getCache()); - - try { - $client->setAccessToken(null); - $this->fail('Should have thrown an Exception.'); - } catch (InvalidArgumentException $e) { - $this->assertEquals('invalid json token', $e->getMessage()); - } - - $token = array('access_token' => 'token'); - $client->setAccessToken($token); - $this->assertEquals($token, $client->getAccessToken()); - } - - public function testSetAccessTokenValidation() - { - $client = new Client(); - $client->setAccessToken([ - 'access_token' => 'token', - 'created' => time() - ]); - self::assertEquals(true, $client->isAccessTokenExpired()); - } - - public function testDefaultConfigOptions() - { - $client = new Client(); - if ($this->isGuzzle6() || $this->isGuzzle7()) { - $this->assertArrayHasKey('http_errors', $client->getHttpClient()->getConfig()); - $this->assertArrayNotHasKey('exceptions', $client->getHttpClient()->getConfig()); - $this->assertFalse($client->getHttpClient()->getConfig()['http_errors']); - } - if ($this->isGuzzle5()) { - $this->assertArrayHasKey('exceptions', $client->getHttpClient()->getDefaultOption()); - $this->assertArrayNotHasKey('http_errors', $client->getHttpClient()->getDefaultOption()); - $this->assertFalse($client->getHttpClient()->getDefaultOption()['exceptions']); - } - } - - public function testAppEngineStreamHandlerConfig() - { - $this->onlyGuzzle5(); - - $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine'; - $client = new Client(); - - // check Stream Handler is used - $http = $client->getHttpClient(); - $class = new ReflectionClass(get_class($http)); - $property = $class->getProperty('fsm'); - $property->setAccessible(true); - $fsm = $property->getValue($http); - - $class = new ReflectionClass(get_class($fsm)); - $property = $class->getProperty('handler'); - $property->setAccessible(true); - $handler = $property->getValue($fsm); - - $this->assertInstanceOf('GuzzleHttp\Ring\Client\StreamHandler', $handler); - - unset($_SERVER['SERVER_SOFTWARE']); - } - - public function testAppEngineVerifyConfig() - { - $this->onlyGuzzle5(); - - $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine'; - $client = new Client(); - - $this->assertEquals( - '/etc/ca-certificates.crt', - $client->getHttpClient()->getDefaultOption('verify') - ); - - unset($_SERVER['SERVER_SOFTWARE']); - } - - public function testJsonConfig() - { - // Device config - $client = new Client(); - $device = - '{"installed":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret"'. - ':"N0aHCBT1qX1VAcF5J1pJAn6S","token_uri":"https://oauth2.googleapis.com/token",'. - '"client_email":"","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","oob"],"client_x509_cert_url"'. - ':"","client_id":"123456789.apps.googleusercontent.com","auth_provider_x509_cert_url":'. - '"https://www.googleapis.com/oauth2/v1/certs"}}'; - $dObj = json_decode($device, true); - $client->setAuthConfig($dObj); - $this->assertEquals($client->getClientId(), $dObj['installed']['client_id']); - $this->assertEquals($client->getClientSecret(), $dObj['installed']['client_secret']); - $this->assertEquals($client->getRedirectUri(), $dObj['installed']['redirect_uris'][0]); - - // Web config - $client = new Client(); - $web = '{"web":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret"' . - ':"lpoubuib8bj-Fmke_YhhyHGgXc","token_uri":"https://oauth2.googleapis.com/token"' . - ',"client_email":"123456789@developer.gserviceaccount.com","client_x509_cert_url":'. - '"https://www.googleapis.com/robot/v1/metadata/x509/123456789@developer.gserviceaccount.com"'. - ',"client_id":"123456789.apps.googleusercontent.com","auth_provider_x509_cert_url":'. - '"https://www.googleapis.com/oauth2/v1/certs"}}'; - $wObj = json_decode($web, true); - $client->setAuthConfig($wObj); - $this->assertEquals($client->getClientId(), $wObj['web']['client_id']); - $this->assertEquals($client->getClientSecret(), $wObj['web']['client_secret']); - $this->assertEquals($client->getRedirectUri(), ''); - } - - public function testIniConfig() - { - $config = parse_ini_file(__DIR__ . '/../config/test.ini'); - $client = new Client($config); - - $this->assertEquals('My Test application', $client->getConfig('application_name')); - $this->assertEquals( - 'gjfiwnGinpena3', - $client->getClientSecret() - ); - } - - public function testNoAuth() - { - /** @var $noAuth Google_Auth_Simple */ - $client = new Client(); - $client->setDeveloperKey(null); - - // unset application credentials - $GOOGLE_APPLICATION_CREDENTIALS = getenv('GOOGLE_APPLICATION_CREDENTIALS'); - $HOME = getenv('HOME'); - putenv('GOOGLE_APPLICATION_CREDENTIALS='); - putenv('HOME='.sys_get_temp_dir()); - $http = new GuzzleClient(); - $client->authorize($http); - - putenv("GOOGLE_APPLICATION_CREDENTIALS=$GOOGLE_APPLICATION_CREDENTIALS"); - putenv("HOME=$HOME"); - $this->checkAuthHandler($http, null); - } - - public function testApplicationDefaultCredentials() - { - $this->checkServiceAccountCredentials(); - $credentialsFile = getenv('GOOGLE_APPLICATION_CREDENTIALS'); - - $client = new Client(); - $client->setAuthConfig($credentialsFile); - - $http = new GuzzleClient(); - $client->authorize($http); - - $this->checkAuthHandler($http, 'AuthToken'); - $this->checkCredentials($http, 'Google\Auth\Credentials\ServiceAccountCredentials'); - } - - public function testApplicationDefaultCredentialsWithSubject() - { - $this->checkServiceAccountCredentials(); - $credentialsFile = getenv('GOOGLE_APPLICATION_CREDENTIALS'); - - $sub = 'sub123'; - $client = new Client(); - $client->setAuthConfig($credentialsFile); - $client->setSubject($sub); - - $http = new GuzzleClient(); - $client->authorize($http); - - $this->checkAuthHandler($http, 'AuthToken'); - $this->checkCredentials($http, 'Google\Auth\Credentials\ServiceAccountCredentials', $sub); - } - - /** - * Test that the ID token is properly refreshed. - */ - public function testRefreshTokenSetsValues() - { - $token = json_encode([ - 'access_token' => 'xyz', - 'id_token' => 'ID_TOKEN', - ]); - $postBody = $this->prophesize('GuzzleHttp\Psr7\Stream'); - $postBody->__toString() - ->shouldBeCalledTimes(1) - ->willReturn($token); + $client->setHttpClient($http->reveal()); + $dr_service = new Drive($client); + $this->assertInstanceOf('Google\Model', $dr_service->files->listFiles()); + } - if ($this->isGuzzle5()) { - $response = $this->getGuzzle5ResponseMock(); - $response->getStatusCode() - ->shouldBeCalledTimes(1) - ->willReturn(200); - } else { - $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); + public function testDefaultLogger() + { + $client = new Client(); + $logger = $client->getLogger(); + $this->assertInstanceOf('Monolog\Logger', $logger); + $handler = $logger->popHandler(); + $this->assertInstanceOf('Monolog\Handler\StreamHandler', $handler); } - $response->getBody() - ->shouldBeCalledTimes(1) - ->willReturn($postBody->reveal()); + public function testDefaultLoggerAppEngine() + { + $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine'; + $client = new Client(); + $logger = $client->getLogger(); + $handler = $logger->popHandler(); + unset($_SERVER['SERVER_SOFTWARE']); - $response->hasHeader('Content-Type')->willReturn(false); + $this->assertInstanceOf('Monolog\Logger', $logger); + $this->assertInstanceOf('Monolog\Handler\SyslogHandler', $handler); + } - $http = $this->prophesize('GuzzleHttp\ClientInterface'); + public function testSettersGetters() + { + $client = new Client(); + $client->setClientId("client1"); + $client->setClientSecret('client1secret'); + $client->setState('1'); + $client->setApprovalPrompt('force'); + $client->setAccessType('offline'); + + $client->setRedirectUri('localhost'); + $client->setConfig('application_name', 'me'); + + $cache = $this->prophesize(CacheItemPoolInterface::class); + $client->setCache($cache->reveal()); + $this->assertInstanceOf(CacheItemPoolInterface::class, $client->getCache()); + + try { + $client->setAccessToken(null); + $this->fail('Should have thrown an Exception.'); + } catch (InvalidArgumentException $e) { + $this->assertEquals('invalid json token', $e->getMessage()); + } + + $token = array('access_token' => 'token'); + $client->setAccessToken($token); + $this->assertEquals($token, $client->getAccessToken()); + } - if ($this->isGuzzle5()) { - $guzzle5Request = new \GuzzleHttp\Message\Request('POST', '/', ['body' => $token]); - $http->createRequest(Argument::any(), Argument::any(), Argument::any()) - ->shouldBeCalledTimes(1) - ->willReturn($guzzle5Request); + public function testSetAccessTokenValidation() + { + $client = new Client(); + $client->setAccessToken([ + 'access_token' => 'token', + 'created' => time() + ]); + self::assertEquals(true, $client->isAccessTokenExpired()); + } - $http->send(Argument::type('GuzzleHttp\Message\Request')) - ->shouldBeCalledTimes(1) - ->willReturn($response->reveal()); - } else { - $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) - ->shouldBeCalledTimes(1) - ->willReturn($response->reveal()); + public function testDefaultConfigOptions() + { + $client = new Client(); + if ($this->isGuzzle6() || $this->isGuzzle7()) { + $this->assertArrayHasKey('http_errors', $client->getHttpClient()->getConfig()); + $this->assertArrayNotHasKey('exceptions', $client->getHttpClient()->getConfig()); + $this->assertFalse($client->getHttpClient()->getConfig()['http_errors']); + } + if ($this->isGuzzle5()) { + $this->assertArrayHasKey('exceptions', $client->getHttpClient()->getDefaultOption()); + $this->assertArrayNotHasKey('http_errors', $client->getHttpClient()->getDefaultOption()); + $this->assertFalse($client->getHttpClient()->getDefaultOption()['exceptions']); + } } - $client = $this->getClient(); - $client->setHttpClient($http->reveal()); - $client->fetchAccessTokenWithRefreshToken("REFRESH_TOKEN"); - $token = $client->getAccessToken(); - $this->assertEquals("ID_TOKEN", $token['id_token']); - } + public function testAppEngineStreamHandlerConfig() + { + $this->onlyGuzzle5(); - /** - * Test that the Refresh Token is set when refreshed. - */ - public function testRefreshTokenIsSetOnRefresh() - { - $refreshToken = 'REFRESH_TOKEN'; - $token = json_encode(array( - 'access_token' => 'xyz', - 'id_token' => 'ID_TOKEN', - )); - $postBody = $this->prophesize('Psr\Http\Message\StreamInterface'); - $postBody->__toString() - ->shouldBeCalledTimes(1) - ->willReturn($token); + $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine'; + $client = new Client(); + + // check Stream Handler is used + $http = $client->getHttpClient(); + $class = new ReflectionClass(get_class($http)); + $property = $class->getProperty('fsm'); + $property->setAccessible(true); + $fsm = $property->getValue($http); - if ($this->isGuzzle5()) { - $response = $this->getGuzzle5ResponseMock(); - $response->getStatusCode() - ->shouldBeCalledTimes(1) - ->willReturn(200); - } else { - $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); + $class = new ReflectionClass(get_class($fsm)); + $property = $class->getProperty('handler'); + $property->setAccessible(true); + $handler = $property->getValue($fsm); + + $this->assertInstanceOf('GuzzleHttp\Ring\Client\StreamHandler', $handler); + + unset($_SERVER['SERVER_SOFTWARE']); } - $response->getBody() - ->shouldBeCalledTimes(1) - ->willReturn($postBody->reveal()); + public function testAppEngineVerifyConfig() + { + $this->onlyGuzzle5(); - $response->hasHeader('Content-Type')->willReturn(false); + $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine'; + $client = new Client(); - $http = $this->prophesize('GuzzleHttp\ClientInterface'); + $this->assertEquals( + '/etc/ca-certificates.crt', + $client->getHttpClient()->getDefaultOption('verify') + ); - if ($this->isGuzzle5()) { - $guzzle5Request = new \GuzzleHttp\Message\Request('POST', '/', ['body' => $token]); - $http->createRequest(Argument::any(), Argument::any(), Argument::any()) - ->willReturn($guzzle5Request); + unset($_SERVER['SERVER_SOFTWARE']); + } - $http->send(Argument::type('GuzzleHttp\Message\Request')) - ->shouldBeCalledTimes(1) - ->willReturn($response->reveal()); - } else { - $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) - ->shouldBeCalledTimes(1) - ->willReturn($response->reveal()); + public function testJsonConfig() + { + // Device config + $client = new Client(); + $device = + '{"installed":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret"'. + ':"N0aHCBT1qX1VAcF5J1pJAn6S","token_uri":"https://oauth2.googleapis.com/token",'. + '"client_email":"","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","oob"],"client_x509_cert_url"'. + ':"","client_id":"123456789.apps.googleusercontent.com","auth_provider_x509_cert_url":'. + '"https://www.googleapis.com/oauth2/v1/certs"}}'; + $dObj = json_decode($device, true); + $client->setAuthConfig($dObj); + $this->assertEquals($client->getClientId(), $dObj['installed']['client_id']); + $this->assertEquals($client->getClientSecret(), $dObj['installed']['client_secret']); + $this->assertEquals($client->getRedirectUri(), $dObj['installed']['redirect_uris'][0]); + + // Web config + $client = new Client(); + $web = '{"web":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret"' . + ':"lpoubuib8bj-Fmke_YhhyHGgXc","token_uri":"https://oauth2.googleapis.com/token"' . + ',"client_email":"123456789@developer.gserviceaccount.com","client_x509_cert_url":'. + '"https://www.googleapis.com/robot/v1/metadata/x509/123456789@developer.gserviceaccount.com"'. + ',"client_id":"123456789.apps.googleusercontent.com","auth_provider_x509_cert_url":'. + '"https://www.googleapis.com/oauth2/v1/certs"}}'; + $wObj = json_decode($web, true); + $client->setAuthConfig($wObj); + $this->assertEquals($client->getClientId(), $wObj['web']['client_id']); + $this->assertEquals($client->getClientSecret(), $wObj['web']['client_secret']); + $this->assertEquals($client->getRedirectUri(), ''); } - $client = $this->getClient(); - $client->setHttpClient($http->reveal()); - $client->fetchAccessTokenWithRefreshToken($refreshToken); - $token = $client->getAccessToken(); - $this->assertEquals($refreshToken, $token['refresh_token']); - } + public function testIniConfig() + { + $config = parse_ini_file(__DIR__ . '/../config/test.ini'); + $client = new Client($config); - /** - * Test that the Refresh Token is not set when a new refresh token is returned. - */ - public function testRefreshTokenIsNotSetWhenNewRefreshTokenIsReturned() - { - $refreshToken = 'REFRESH_TOKEN'; - $token = json_encode(array( - 'access_token' => 'xyz', - 'id_token' => 'ID_TOKEN', - 'refresh_token' => 'NEW_REFRESH_TOKEN' - )); - - $postBody = $this->prophesize('GuzzleHttp\Psr7\Stream'); - $postBody->__toString() - ->wilLReturn($token); - - if ($this->isGuzzle5()) { - $response = $this->getGuzzle5ResponseMock(); - $response->getStatusCode() - ->willReturn(200); - } else { - $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); - } - - $response->getBody() - ->willReturn($postBody->reveal()); - - $response->hasHeader('Content-Type')->willReturn(false); - - $http = $this->prophesize('GuzzleHttp\ClientInterface'); - - if ($this->isGuzzle5()) { - $guzzle5Request = new \GuzzleHttp\Message\Request('POST', '/', ['body' => $token]); - $http->createRequest(Argument::any(), Argument::any(), Argument::any()) - ->willReturn($guzzle5Request); - - $http->send(Argument::type('GuzzleHttp\Message\Request')) - ->willReturn($response->reveal()); - } else { - $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) - ->shouldBeCalledTimes(1) - ->willReturn($response->reveal()); - } - - $client = $this->getClient(); - $client->setHttpClient($http->reveal()); - $client->fetchAccessTokenWithRefreshToken($refreshToken); - $token = $client->getAccessToken(); - $this->assertEquals('NEW_REFRESH_TOKEN', $token['refresh_token']); - } - - /** - * Test fetching an access token with assertion credentials - * using "useApplicationDefaultCredentials" - */ - public function testFetchAccessTokenWithAssertionFromEnv() - { - $this->checkServiceAccountCredentials(); + $this->assertEquals('My Test application', $client->getConfig('application_name')); + $this->assertEquals( + 'gjfiwnGinpena3', + $client->getClientSecret() + ); + } - $client = $this->getClient(); - $client->useApplicationDefaultCredentials(); - $token = $client->fetchAccessTokenWithAssertion(); + public function testNoAuth() + { + /** @var $noAuth Google_Auth_Simple */ + $client = new Client(); + $client->setDeveloperKey(null); + + // unset application credentials + $GOOGLE_APPLICATION_CREDENTIALS = getenv('GOOGLE_APPLICATION_CREDENTIALS'); + $HOME = getenv('HOME'); + putenv('GOOGLE_APPLICATION_CREDENTIALS='); + putenv('HOME='.sys_get_temp_dir()); + $http = new GuzzleClient(); + $client->authorize($http); + + putenv("GOOGLE_APPLICATION_CREDENTIALS=$GOOGLE_APPLICATION_CREDENTIALS"); + putenv("HOME=$HOME"); + $this->checkAuthHandler($http, null); + } - $this->assertNotNull($token); - $this->assertArrayHasKey('access_token', $token); - } + public function testApplicationDefaultCredentials() + { + $this->checkServiceAccountCredentials(); + $credentialsFile = getenv('GOOGLE_APPLICATION_CREDENTIALS'); - /** - * Test fetching an access token with assertion credentials - * using "setAuthConfig" - */ - public function testFetchAccessTokenWithAssertionFromFile() - { - $this->checkServiceAccountCredentials(); + $client = new Client(); + $client->setAuthConfig($credentialsFile); - $client = $this->getClient(); - $client->setAuthConfig(getenv('GOOGLE_APPLICATION_CREDENTIALS')); - $token = $client->fetchAccessTokenWithAssertion(); + $http = new GuzzleClient(); + $client->authorize($http); - $this->assertNotNull($token); - $this->assertArrayHasKey('access_token', $token); - } + $this->checkAuthHandler($http, 'AuthToken'); + $this->checkCredentials($http, 'Google\Auth\Credentials\ServiceAccountCredentials'); + } - /** - * Test fetching an access token with assertion credentials - * populates the "created" field - */ - public function testFetchAccessTokenWithAssertionAddsCreated() - { - $this->checkServiceAccountCredentials(); + public function testApplicationDefaultCredentialsWithSubject() + { + $this->checkServiceAccountCredentials(); + $credentialsFile = getenv('GOOGLE_APPLICATION_CREDENTIALS'); - $client = $this->getClient(); - $client->useApplicationDefaultCredentials(); - $token = $client->fetchAccessTokenWithAssertion(); + $sub = 'sub123'; + $client = new Client(); + $client->setAuthConfig($credentialsFile); + $client->setSubject($sub); - $this->assertNotNull($token); - $this->assertArrayHasKey('created', $token); - } + $http = new GuzzleClient(); + $client->authorize($http); - /** - * Test fetching an access token with assertion credentials - * using "setAuthConfig" and "setSubject" but with user credentials - */ - public function testBadSubjectThrowsException() - { - $this->checkServiceAccountCredentials(); - - $client = $this->getClient(); - $client->useApplicationDefaultCredentials(); - $client->setSubject('bad-subject'); - - $authHandler = AuthHandlerFactory::build(); - - // make this method public for testing purposes - $method = new ReflectionMethod($authHandler, 'createAuthHttp'); - $method->setAccessible(true); - $authHttp = $method->invoke($authHandler, $client->getHttpClient()); - - try { - $token = $client->fetchAccessTokenWithAssertion($authHttp); - $this->fail('no exception thrown'); - } catch (ClientException $e) { - $response = $e->getResponse(); - $this->assertContains('Invalid impersonation', (string) $response->getBody()); - } - } - - public function testTokenCallback() - { - $this->onlyPhp55AndAbove(); - $this->checkToken(); - - $client = $this->getClient(); - $accessToken = $client->getAccessToken(); - - if (!isset($accessToken['refresh_token'])) { - $this->markTestSkipped('Refresh Token required'); - } - - // make the auth library think the token is expired - $accessToken['expires_in'] = 0; - $cache = $client->getCache(); - $path = sys_get_temp_dir().'/google-api-php-client-tests-'.time(); - $client->setCache($this->getCache($path)); - $client->setAccessToken($accessToken); - - // create the callback function - $phpunit = $this; - $called = false; - $callback = function ($key, $value) use ($client, $cache, $phpunit, &$called) { - // assert the expected keys and values - $phpunit->assertNotNull($key); - $phpunit->assertNotNull($value); - $called = true; - - // go back to the previous cache - $client->setCache($cache); - }; - - // set the token callback to the client - $client->setTokenCallback($callback); - - // make a silly request to obtain a new token (it's ok if it fails) - $http = $client->authorize(); - try { - $http->get('https://www.googleapis.com/books/v1/volumes?q=Voltaire'); - } catch (Exception $e) {} - $newToken = $client->getAccessToken(); - - // go back to the previous cache - // (in case callback wasn't called) - $client->setCache($cache); - - $this->assertTrue($called); - } - - public function testDefaultTokenCallback() - { - $this->onlyPhp55AndAbove(); - $this->checkToken(); - - $client = $this->getClient(); - $accessToken = $client->getAccessToken(); - - if (!isset($accessToken['refresh_token'])) { - $this->markTestSkipped('Refresh Token required'); - } - - // make the auth library think the token is expired - $accessToken['expires_in'] = 0; - $client->setAccessToken($accessToken); - - // make a silly request to obtain a new token (it's ok if it fails) - $http = $client->authorize(); - try { - $http->get('https://www.googleapis.com/books/v1/volumes?q=Voltaire'); - } catch (Exception $e) {} - - // Assert the in-memory token has been updated - $newToken = $client->getAccessToken(); - $this->assertNotEquals( - $accessToken['access_token'], - $newToken['access_token'] - ); - - $this->assertFalse($client->isAccessTokenExpired()); - } - - /** @runInSeparateProcess */ - public function testOnGceCacheAndCacheOptions() - { - if (!class_exists(GCECache::class)) { - $this->markTestSkipped('Requires google/auth >= 1.12'); - } - - putenv('HOME='); - putenv('GOOGLE_APPLICATION_CREDENTIALS='); - $prefix = 'test_prefix_'; - $cacheConfig = ['gce_prefix' => $prefix]; - - $mockCacheItem = $this->prophesize(CacheItemInterface::class); - $mockCacheItem->isHit() - ->willReturn(true); - $mockCacheItem->get() - ->shouldBeCalledTimes(1) - ->willReturn(true); + $this->checkAuthHandler($http, 'AuthToken'); + $this->checkCredentials($http, 'Google\Auth\Credentials\ServiceAccountCredentials', $sub); + } - $mockCache = $this->prophesize(CacheItemPoolInterface::class); - $mockCache->getItem($prefix . GCECache::GCE_CACHE_KEY) - ->shouldBeCalledTimes(1) - ->willReturn($mockCacheItem->reveal()); - - $client = new Client(['cache_config' => $cacheConfig]); - $client->setCache($mockCache->reveal()); - $client->useApplicationDefaultCredentials(); - $client->authorize(); - } - - /** @runInSeparateProcess */ - public function testFetchAccessTokenWithAssertionCache() - { - $this->checkServiceAccountCredentials(); - $cachedValue = ['access_token' => '2/abcdef1234567890']; - $mockCacheItem = $this->prophesize(CacheItemInterface::class); - $mockCacheItem->isHit() - ->shouldBeCalledTimes(1) - ->willReturn(true); - $mockCacheItem->get() - ->shouldBeCalledTimes(1) - ->willReturn($cachedValue); + /** + * Test that the ID token is properly refreshed. + */ + public function testRefreshTokenSetsValues() + { + $token = json_encode([ + 'access_token' => 'xyz', + 'id_token' => 'ID_TOKEN', + ]); + $postBody = $this->prophesize('GuzzleHttp\Psr7\Stream'); + $postBody->__toString() + ->shouldBeCalledTimes(1) + ->willReturn($token); + + if ($this->isGuzzle5()) { + $response = $this->getGuzzle5ResponseMock(); + $response->getStatusCode() + ->shouldBeCalledTimes(1) + ->willReturn(200); + } else { + $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); + } + + $response->getBody() + ->shouldBeCalledTimes(1) + ->willReturn($postBody->reveal()); + + $response->hasHeader('Content-Type')->willReturn(false); + + $http = $this->prophesize('GuzzleHttp\ClientInterface'); + + if ($this->isGuzzle5()) { + $guzzle5Request = new \GuzzleHttp\Message\Request('POST', '/', ['body' => $token]); + $http->createRequest(Argument::any(), Argument::any(), Argument::any()) + ->shouldBeCalledTimes(1) + ->willReturn($guzzle5Request); + + $http->send(Argument::type('GuzzleHttp\Message\Request')) + ->shouldBeCalledTimes(1) + ->willReturn($response->reveal()); + } else { + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response->reveal()); + } + + $client = $this->getClient(); + $client->setHttpClient($http->reveal()); + $client->fetchAccessTokenWithRefreshToken("REFRESH_TOKEN"); + $token = $client->getAccessToken(); + $this->assertEquals("ID_TOKEN", $token['id_token']); + } - $mockCache = $this->prophesize(CacheItemPoolInterface::class); - $mockCache->getItem(Argument::any()) - ->shouldBeCalledTimes(1) - ->willReturn($mockCacheItem->reveal()); - - $client = new Client(); - $client->setCache($mockCache->reveal()); - $client->useApplicationDefaultCredentials(); - $token = $client->fetchAccessTokenWithAssertion(); - $this->assertArrayHasKey('access_token', $token); - $this->assertEquals($cachedValue['access_token'], $token['access_token']); - } - - public function testCacheClientOption() - { - $mockCache = $this->prophesize(CacheItemPoolInterface::class); - $client = new Client([ - 'cache' => $mockCache->reveal() - ]); - $this->assertEquals($mockCache->reveal(), $client->getCache()); - } - - public function testExecuteWithFormat() - { - $this->onlyGuzzle6Or7(); - - $client = new Client([ - 'api_format_v2' => true - ]); - - $guzzle = $this->prophesize('GuzzleHttp\Client'); - $guzzle - ->send(Argument::allOf( - Argument::type('Psr\Http\Message\RequestInterface'), - Argument::that(function (RequestInterface $request) { - return $request->getHeaderLine('X-GOOG-API-FORMAT-VERSION') === '2'; - }) - ), []) - ->shouldBeCalled() - ->willReturn(new Response(200, [], null)); - - $client->setHttpClient($guzzle->reveal()); - - $request = new Request('POST', 'http://foo.bar/'); - $client->execute($request); - } - - public function testExecuteSetsCorrectHeaders() - { - $this->onlyGuzzle6Or7(); - - $client = new Client(); - - $guzzle = $this->prophesize('GuzzleHttp\Client'); - $guzzle->send(Argument::that(function (RequestInterface $request) { - $userAgent = sprintf( - '%s%s', - Client::USER_AGENT_SUFFIX, - Client::LIBVER - ); - $xGoogApiClient = sprintf( - 'gl-php/%s gdcl/%s', - phpversion(), - Client::LIBVER - ); - - if ($request->getHeaderLine('User-Agent') !== $userAgent) { - return false; - } - - if ($request->getHeaderLine('x-goog-api-client') !== $xGoogApiClient) { - return false; - } - - return true; - }), [])->shouldBeCalledTimes(1)->willReturn(new Response(200, [], null)); - - $client->setHttpClient($guzzle->reveal()); - - $request = new Request('POST', 'http://foo.bar/'); - $client->execute($request); - } - - /** + /** + * Test that the Refresh Token is set when refreshed. + */ + public function testRefreshTokenIsSetOnRefresh() + { + $refreshToken = 'REFRESH_TOKEN'; + $token = json_encode(array( + 'access_token' => 'xyz', + 'id_token' => 'ID_TOKEN', + )); + $postBody = $this->prophesize('Psr\Http\Message\StreamInterface'); + $postBody->__toString() + ->shouldBeCalledTimes(1) + ->willReturn($token); + + if ($this->isGuzzle5()) { + $response = $this->getGuzzle5ResponseMock(); + $response->getStatusCode() + ->shouldBeCalledTimes(1) + ->willReturn(200); + } else { + $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); + } + + $response->getBody() + ->shouldBeCalledTimes(1) + ->willReturn($postBody->reveal()); + + $response->hasHeader('Content-Type')->willReturn(false); + + $http = $this->prophesize('GuzzleHttp\ClientInterface'); + + if ($this->isGuzzle5()) { + $guzzle5Request = new \GuzzleHttp\Message\Request('POST', '/', ['body' => $token]); + $http->createRequest(Argument::any(), Argument::any(), Argument::any()) + ->willReturn($guzzle5Request); + + $http->send(Argument::type('GuzzleHttp\Message\Request')) + ->shouldBeCalledTimes(1) + ->willReturn($response->reveal()); + } else { + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response->reveal()); + } + + $client = $this->getClient(); + $client->setHttpClient($http->reveal()); + $client->fetchAccessTokenWithRefreshToken($refreshToken); + $token = $client->getAccessToken(); + $this->assertEquals($refreshToken, $token['refresh_token']); + } + + /** + * Test that the Refresh Token is not set when a new refresh token is returned. + */ + public function testRefreshTokenIsNotSetWhenNewRefreshTokenIsReturned() + { + $refreshToken = 'REFRESH_TOKEN'; + $token = json_encode(array( + 'access_token' => 'xyz', + 'id_token' => 'ID_TOKEN', + 'refresh_token' => 'NEW_REFRESH_TOKEN' + )); + + $postBody = $this->prophesize('GuzzleHttp\Psr7\Stream'); + $postBody->__toString() + ->wilLReturn($token); + + if ($this->isGuzzle5()) { + $response = $this->getGuzzle5ResponseMock(); + $response->getStatusCode() + ->willReturn(200); + } else { + $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); + } + + $response->getBody() + ->willReturn($postBody->reveal()); + + $response->hasHeader('Content-Type')->willReturn(false); + + $http = $this->prophesize('GuzzleHttp\ClientInterface'); + + if ($this->isGuzzle5()) { + $guzzle5Request = new \GuzzleHttp\Message\Request('POST', '/', ['body' => $token]); + $http->createRequest(Argument::any(), Argument::any(), Argument::any()) + ->willReturn($guzzle5Request); + + $http->send(Argument::type('GuzzleHttp\Message\Request')) + ->willReturn($response->reveal()); + } else { + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response->reveal()); + } + + $client = $this->getClient(); + $client->setHttpClient($http->reveal()); + $client->fetchAccessTokenWithRefreshToken($refreshToken); + $token = $client->getAccessToken(); + $this->assertEquals('NEW_REFRESH_TOKEN', $token['refresh_token']); + } + + /** + * Test fetching an access token with assertion credentials + * using "useApplicationDefaultCredentials" + */ + public function testFetchAccessTokenWithAssertionFromEnv() + { + $this->checkServiceAccountCredentials(); + + $client = $this->getClient(); + $client->useApplicationDefaultCredentials(); + $token = $client->fetchAccessTokenWithAssertion(); + + $this->assertNotNull($token); + $this->assertArrayHasKey('access_token', $token); + } + + /** + * Test fetching an access token with assertion credentials + * using "setAuthConfig" + */ + public function testFetchAccessTokenWithAssertionFromFile() + { + $this->checkServiceAccountCredentials(); + + $client = $this->getClient(); + $client->setAuthConfig(getenv('GOOGLE_APPLICATION_CREDENTIALS')); + $token = $client->fetchAccessTokenWithAssertion(); + + $this->assertNotNull($token); + $this->assertArrayHasKey('access_token', $token); + } + + /** + * Test fetching an access token with assertion credentials + * populates the "created" field + */ + public function testFetchAccessTokenWithAssertionAddsCreated() + { + $this->checkServiceAccountCredentials(); + + $client = $this->getClient(); + $client->useApplicationDefaultCredentials(); + $token = $client->fetchAccessTokenWithAssertion(); + + $this->assertNotNull($token); + $this->assertArrayHasKey('created', $token); + } + + /** + * Test fetching an access token with assertion credentials + * using "setAuthConfig" and "setSubject" but with user credentials + */ + public function testBadSubjectThrowsException() + { + $this->checkServiceAccountCredentials(); + + $client = $this->getClient(); + $client->useApplicationDefaultCredentials(); + $client->setSubject('bad-subject'); + + $authHandler = AuthHandlerFactory::build(); + + // make this method public for testing purposes + $method = new ReflectionMethod($authHandler, 'createAuthHttp'); + $method->setAccessible(true); + $authHttp = $method->invoke($authHandler, $client->getHttpClient()); + + try { + $token = $client->fetchAccessTokenWithAssertion($authHttp); + $this->fail('no exception thrown'); + } catch (ClientException $e) { + $response = $e->getResponse(); + $this->assertContains('Invalid impersonation', (string) $response->getBody()); + } + } + + public function testTokenCallback() + { + $this->onlyPhp55AndAbove(); + $this->checkToken(); + + $client = $this->getClient(); + $accessToken = $client->getAccessToken(); + + if (!isset($accessToken['refresh_token'])) { + $this->markTestSkipped('Refresh Token required'); + } + + // make the auth library think the token is expired + $accessToken['expires_in'] = 0; + $cache = $client->getCache(); + $path = sys_get_temp_dir().'/google-api-php-client-tests-'.time(); + $client->setCache($this->getCache($path)); + $client->setAccessToken($accessToken); + + // create the callback function + $phpunit = $this; + $called = false; + $callback = function ($key, $value) use ($client, $cache, $phpunit, &$called) { + // assert the expected keys and values + $phpunit->assertNotNull($key); + $phpunit->assertNotNull($value); + $called = true; + + // go back to the previous cache + $client->setCache($cache); + }; + + // set the token callback to the client + $client->setTokenCallback($callback); + + // make a silly request to obtain a new token (it's ok if it fails) + $http = $client->authorize(); + try { + $http->get('https://www.googleapis.com/books/v1/volumes?q=Voltaire'); + } catch (Exception $e) { + + } + $newToken = $client->getAccessToken(); + + // go back to the previous cache + // (in case callback wasn't called) + $client->setCache($cache); + + $this->assertTrue($called); + } + + public function testDefaultTokenCallback() + { + $this->onlyPhp55AndAbove(); + $this->checkToken(); + + $client = $this->getClient(); + $accessToken = $client->getAccessToken(); + + if (!isset($accessToken['refresh_token'])) { + $this->markTestSkipped('Refresh Token required'); + } + + // make the auth library think the token is expired + $accessToken['expires_in'] = 0; + $client->setAccessToken($accessToken); + + // make a silly request to obtain a new token (it's ok if it fails) + $http = $client->authorize(); + try { + $http->get('https://www.googleapis.com/books/v1/volumes?q=Voltaire'); + } catch (Exception $e) { + + } + + // Assert the in-memory token has been updated + $newToken = $client->getAccessToken(); + $this->assertNotEquals( + $accessToken['access_token'], + $newToken['access_token'] + ); + + $this->assertFalse($client->isAccessTokenExpired()); + } + + /** @runInSeparateProcess */ + public function testOnGceCacheAndCacheOptions() + { + if (!class_exists(GCECache::class)) { + $this->markTestSkipped('Requires google/auth >= 1.12'); + } + + putenv('HOME='); + putenv('GOOGLE_APPLICATION_CREDENTIALS='); + $prefix = 'test_prefix_'; + $cacheConfig = ['gce_prefix' => $prefix]; + + $mockCacheItem = $this->prophesize(CacheItemInterface::class); + $mockCacheItem->isHit() + ->willReturn(true); + $mockCacheItem->get() + ->shouldBeCalledTimes(1) + ->willReturn(true); + + $mockCache = $this->prophesize(CacheItemPoolInterface::class); + $mockCache->getItem($prefix . GCECache::GCE_CACHE_KEY) + ->shouldBeCalledTimes(1) + ->willReturn($mockCacheItem->reveal()); + + $client = new Client(['cache_config' => $cacheConfig]); + $client->setCache($mockCache->reveal()); + $client->useApplicationDefaultCredentials(); + $client->authorize(); + } + + /** @runInSeparateProcess */ + public function testFetchAccessTokenWithAssertionCache() + { + $this->checkServiceAccountCredentials(); + $cachedValue = ['access_token' => '2/abcdef1234567890']; + $mockCacheItem = $this->prophesize(CacheItemInterface::class); + $mockCacheItem->isHit() + ->shouldBeCalledTimes(1) + ->willReturn(true); + $mockCacheItem->get() + ->shouldBeCalledTimes(1) + ->willReturn($cachedValue); + + $mockCache = $this->prophesize(CacheItemPoolInterface::class); + $mockCache->getItem(Argument::any()) + ->shouldBeCalledTimes(1) + ->willReturn($mockCacheItem->reveal()); + + $client = new Client(); + $client->setCache($mockCache->reveal()); + $client->useApplicationDefaultCredentials(); + $token = $client->fetchAccessTokenWithAssertion(); + $this->assertArrayHasKey('access_token', $token); + $this->assertEquals($cachedValue['access_token'], $token['access_token']); + } + + public function testCacheClientOption() + { + $mockCache = $this->prophesize(CacheItemPoolInterface::class); + $client = new Client([ + 'cache' => $mockCache->reveal() + ]); + $this->assertEquals($mockCache->reveal(), $client->getCache()); + } + + public function testExecuteWithFormat() + { + $this->onlyGuzzle6Or7(); + + $client = new Client([ + 'api_format_v2' => true + ]); + + $guzzle = $this->prophesize('GuzzleHttp\Client'); + $guzzle + ->send(Argument::allOf( + Argument::type('Psr\Http\Message\RequestInterface'), + Argument::that(function (RequestInterface $request) { + return $request->getHeaderLine('X-GOOG-API-FORMAT-VERSION') === '2'; + }) + ), []) + ->shouldBeCalled() + ->willReturn(new Response(200, [], null)); + + $client->setHttpClient($guzzle->reveal()); + + $request = new Request('POST', 'http://foo.bar/'); + $client->execute($request); + } + + public function testExecuteSetsCorrectHeaders() + { + $this->onlyGuzzle6Or7(); + + $client = new Client(); + + $guzzle = $this->prophesize('GuzzleHttp\Client'); + $guzzle->send(Argument::that(function (RequestInterface $request) { + $userAgent = sprintf( + '%s%s', + Client::USER_AGENT_SUFFIX, + Client::LIBVER + ); + $xGoogApiClient = sprintf( + 'gl-php/%s gdcl/%s', + phpversion(), + Client::LIBVER + ); + + if ($request->getHeaderLine('User-Agent') !== $userAgent) { + return false; + } + + if ($request->getHeaderLine('x-goog-api-client') !== $xGoogApiClient) { + return false; + } + + return true; + }), [])->shouldBeCalledTimes(1)->willReturn(new Response(200, [], null)); + + $client->setHttpClient($guzzle->reveal()); + + $request = new Request('POST', 'http://foo.bar/'); + $client->execute($request); + } + + /** * @runInSeparateProcess */ - public function testClientOptions() - { - // Test credential file - $tmpCreds = [ - 'type' => 'service_account', - 'client_id' => 'foo', - 'client_email' => '', - 'private_key' => '' - ]; - $tmpCredFile = tempnam(sys_get_temp_dir(), 'creds') . '.json'; - file_put_contents($tmpCredFile, json_encode($tmpCreds)); - $client = new Client([ - 'credentials' => $tmpCredFile - ]); - $this->assertEquals('foo', $client->getClientId()); - - // Test credentials array - $client = new Client([ - 'credentials' => $tmpCredFile - ]); - $this->assertEquals('foo', $client->getClientId()); - - // Test singular scope - $client = new Client([ - 'scopes' => 'a-scope' - ]); - $this->assertEquals(['a-scope'], $client->getScopes()); - - // Test multiple scopes - $client = new Client([ - 'scopes' => ['one-scope', 'two-scope'] - ]); - $this->assertEquals(['one-scope', 'two-scope'], $client->getScopes()); - - // Test quota project - $client = new Client([ - 'quota_project' => 'some-quota-project' - ]); - $this->assertEquals('some-quota-project', $client->getConfig('quota_project')); - // Test quota project in google/auth dependency - putenv('GOOGLE_APPLICATION_CREDENTIALS='.$tmpCredFile); - $method = new ReflectionMethod($client, 'createApplicationDefaultCredentials'); - $method->setAccessible(true); - $credentials = $method->invoke($client); - $this->assertEquals('some-quota-project', $credentials->getQuotaProject()); - } - - public function testCredentialsOptionWithCredentialsLoader() - { - $this->onlyGuzzle6Or7(); - - $request = null; - $credentials = $this->prophesize('Google\Auth\CredentialsLoader'); - $credentials->getCacheKey() - ->willReturn('cache-key'); - - // Ensure the access token provided by our credentials loader is used - $credentials->fetchAuthToken(Argument::any()) - ->shouldBeCalledOnce() - ->willReturn(['access_token' => 'abc']); - - $client = new Client(['credentials' => $credentials->reveal()]); - - $handler = $this->prophesize('GuzzleHttp\HandlerStack'); - $handler->remove('google_auth') - ->shouldBeCalledOnce(); - $handler->push(Argument::any(), 'google_auth') - ->shouldBeCalledOnce() - ->will(function($args) use (&$request) { - $middleware = $args[0]; - $callable = $middleware(function ($req, $res) use (&$request) { - $request = $req; // test later - }); - $callable(new Request('GET', '/fake-uri'), ['auth' => 'google_auth']); - }); - - $httpClient = $this->prophesize('GuzzleHttp\ClientInterface'); - $httpClient->getConfig() - ->shouldBeCalledOnce() - ->willReturn(['handler' => $handler->reveal()]); - $httpClient->getConfig('base_uri') - ->shouldBeCalledOnce(); - $httpClient->getConfig('verify') - ->shouldBeCalledOnce(); - $httpClient->getConfig('proxy') - ->shouldBeCalledOnce(); - $httpClient->send(Argument::any(), Argument::any()) - ->shouldNotBeCalled(); - - $http = $client->authorize($httpClient->reveal()); - - $this->assertNotNull($request); - $authHeader = $request->getHeaderLine('authorization'); - $this->assertNotNull($authHeader); - $this->assertEquals('Bearer abc', $authHeader); - } + public function testClientOptions() + { + // Test credential file + $tmpCreds = [ + 'type' => 'service_account', + 'client_id' => 'foo', + 'client_email' => '', + 'private_key' => '' + ]; + $tmpCredFile = tempnam(sys_get_temp_dir(), 'creds') . '.json'; + file_put_contents($tmpCredFile, json_encode($tmpCreds)); + $client = new Client([ + 'credentials' => $tmpCredFile + ]); + $this->assertEquals('foo', $client->getClientId()); + + // Test credentials array + $client = new Client([ + 'credentials' => $tmpCredFile + ]); + $this->assertEquals('foo', $client->getClientId()); + + // Test singular scope + $client = new Client([ + 'scopes' => 'a-scope' + ]); + $this->assertEquals(['a-scope'], $client->getScopes()); + + // Test multiple scopes + $client = new Client([ + 'scopes' => ['one-scope', 'two-scope'] + ]); + $this->assertEquals(['one-scope', 'two-scope'], $client->getScopes()); + + // Test quota project + $client = new Client([ + 'quota_project' => 'some-quota-project' + ]); + $this->assertEquals('some-quota-project', $client->getConfig('quota_project')); + // Test quota project in google/auth dependency + putenv('GOOGLE_APPLICATION_CREDENTIALS='.$tmpCredFile); + $method = new ReflectionMethod($client, 'createApplicationDefaultCredentials'); + $method->setAccessible(true); + $credentials = $method->invoke($client); + $this->assertEquals('some-quota-project', $credentials->getQuotaProject()); + } + + public function testCredentialsOptionWithCredentialsLoader() + { + $this->onlyGuzzle6Or7(); + + $request = null; + $credentials = $this->prophesize('Google\Auth\CredentialsLoader'); + $credentials->getCacheKey() + ->willReturn('cache-key'); + + // Ensure the access token provided by our credentials loader is used + $credentials->fetchAuthToken(Argument::any()) + ->shouldBeCalledOnce() + ->willReturn(['access_token' => 'abc']); + + $client = new Client(['credentials' => $credentials->reveal()]); + + $handler = $this->prophesize('GuzzleHttp\HandlerStack'); + $handler->remove('google_auth') + ->shouldBeCalledOnce(); + $handler->push(Argument::any(), 'google_auth') + ->shouldBeCalledOnce() + ->will(function ($args) use (&$request) { + $middleware = $args[0]; + $callable = $middleware(function ($req, $res) use (&$request) { + $request = $req; // test later + }); + $callable(new Request('GET', '/fake-uri'), ['auth' => 'google_auth']); + }); + + $httpClient = $this->prophesize('GuzzleHttp\ClientInterface'); + $httpClient->getConfig() + ->shouldBeCalledOnce() + ->willReturn(['handler' => $handler->reveal()]); + $httpClient->getConfig('base_uri') + ->shouldBeCalledOnce(); + $httpClient->getConfig('verify') + ->shouldBeCalledOnce(); + $httpClient->getConfig('proxy') + ->shouldBeCalledOnce(); + $httpClient->send(Argument::any(), Argument::any()) + ->shouldNotBeCalled(); + + $http = $client->authorize($httpClient->reveal()); + + $this->assertNotNull($request); + $authHeader = $request->getHeaderLine('authorization'); + $this->assertNotNull($authHeader); + $this->assertEquals('Bearer abc', $authHeader); + } } diff --git a/tests/Google/Http/BatchTest.php b/tests/Google/Http/BatchTest.php index 49046d4c1..e60421a06 100644 --- a/tests/Google/Http/BatchTest.php +++ b/tests/Google/Http/BatchTest.php @@ -29,65 +29,65 @@ class BatchTest extends BaseTest { - public function testBatchRequest() - { - $this->checkKey(); - $client = $this->getClient(); - $client->setUseBatch(true); - $books = new Books($client); - $batch = $books->createBatch(); + public function testBatchRequest() + { + $this->checkKey(); + $client = $this->getClient(); + $client->setUseBatch(true); + $books = new Books($client); + $batch = $books->createBatch(); - $batch->add($books->volumes->listVolumes('Henry David Thoreau'), 'key1'); - $batch->add($books->volumes->listVolumes('Edgar Allen Poe'), 'key2'); + $batch->add($books->volumes->listVolumes('Henry David Thoreau'), 'key1'); + $batch->add($books->volumes->listVolumes('Edgar Allen Poe'), 'key2'); - $result = $batch->execute(); - $this->assertArrayHasKey('response-key1', $result); - $this->assertArrayHasKey('response-key2', $result); - } + $result = $batch->execute(); + $this->assertArrayHasKey('response-key1', $result); + $this->assertArrayHasKey('response-key2', $result); + } - public function testInvalidBatchRequest() - { - $this->checkKey(); - $client = $this->getClient(); - $client->setUseBatch(true); - $books = new Books($client); - $batch = $books->createBatch(); + public function testInvalidBatchRequest() + { + $this->checkKey(); + $client = $this->getClient(); + $client->setUseBatch(true); + $books = new Books($client); + $batch = $books->createBatch(); - $batch->add($books->volumes->listVolumes(false), 'key1'); - $batch->add($books->volumes->listVolumes('Edgar Allen Poe'), 'key2'); + $batch->add($books->volumes->listVolumes(false), 'key1'); + $batch->add($books->volumes->listVolumes('Edgar Allen Poe'), 'key2'); - $result = $batch->execute(); - $this->assertArrayHasKey('response-key1', $result); - $this->assertArrayHasKey('response-key2', $result); - $this->assertInstanceOf( - ServiceException::class, - $result['response-key1'] - ); - } + $result = $batch->execute(); + $this->assertArrayHasKey('response-key1', $result); + $this->assertArrayHasKey('response-key2', $result); + $this->assertInstanceOf( + ServiceException::class, + $result['response-key1'] + ); + } - public function testMediaFileBatch() - { - $client = $this->getClient(); - $storage = new Storage($client); - $bucket = 'testbucket'; - $stream = Psr7\Utils::streamFor("testbucket-text"); - $params = [ - 'data' => $stream, - 'mimeType' => 'text/plain', - ]; + public function testMediaFileBatch() + { + $client = $this->getClient(); + $storage = new Storage($client); + $bucket = 'testbucket'; + $stream = Psr7\Utils::streamFor("testbucket-text"); + $params = [ + 'data' => $stream, + 'mimeType' => 'text/plain', + ]; - // Metadata object for new Google Cloud Storage object - $obj = new Storage\StorageObject(); - $obj->contentType = "text/plain"; + // Metadata object for new Google Cloud Storage object + $obj = new Storage\StorageObject(); + $obj->contentType = "text/plain"; - // Batch Upload - $client->setUseBatch(true); - $obj->name = "batch"; - /** @var \GuzzleHttp\Psr7\Request $request */ - $request = $storage->objects->insert($bucket, $obj, $params); + // Batch Upload + $client->setUseBatch(true); + $obj->name = "batch"; + /** @var \GuzzleHttp\Psr7\Request $request */ + $request = $storage->objects->insert($bucket, $obj, $params); - $this->assertStringContainsString('multipart/related', $request->getHeaderLine('content-type')); - $this->assertStringContainsString('/upload/', $request->getUri()->getPath()); - $this->assertStringContainsString('uploadType=multipart', $request->getUri()->getQuery()); - } + $this->assertStringContainsString('multipart/related', $request->getHeaderLine('content-type')); + $this->assertStringContainsString('/upload/', $request->getUri()->getPath()); + $this->assertStringContainsString('uploadType=multipart', $request->getUri()->getQuery()); + } } diff --git a/tests/Google/Http/MediaFileUploadTest.php b/tests/Google/Http/MediaFileUploadTest.php index d8d3a2a08..bfb824169 100644 --- a/tests/Google/Http/MediaFileUploadTest.php +++ b/tests/Google/Http/MediaFileUploadTest.php @@ -29,177 +29,177 @@ class MediaFileUploadTest extends BaseTest { - public function testMediaFile() - { - $client = $this->getClient(); - $request = new Request('POST', 'http://www.example.com/'); - $media = new MediaFileUpload( - $client, - $request, - 'image/png', - base64_decode('data:image/png;base64,a') - ); - $request = $media->getRequest(); - - $this->assertEquals(0, $media->getProgress()); - $this->assertGreaterThan(0, strlen($request->getBody())); - } - - public function testGetUploadType() - { - $client = $this->getClient(); - $request = new Request('POST', 'http://www.example.com/'); - - // Test resumable upload - $media = new MediaFileUpload($client, $request, 'image/png', 'a', true); - $this->assertEquals('resumable', $media->getUploadType(null)); - - // Test data *only* uploads - $media = new MediaFileUpload($client, $request, 'image/png', 'a', false); - $this->assertEquals('media', $media->getUploadType(null)); - - // Test multipart uploads - $media = new MediaFileUpload($client, $request, 'image/png', 'a', false); - $this->assertEquals('multipart', $media->getUploadType(array('a' => 'b'))); - } - - public function testProcess() - { - $client = $this->getClient(); - $data = 'foo'; - - // Test data *only* uploads. - $request = new Request('POST', 'http://www.example.com/'); - $media = new MediaFileUpload($client, $request, 'image/png', $data, false); - $request = $media->getRequest(); - $this->assertEquals($data, (string) $request->getBody()); - - // Test resumable (meta data) - we want to send the metadata, not the app data. - $request = new Request('POST', 'http://www.example.com/'); - $reqData = json_encode("hello"); - $request = $request->withBody(Psr7\Utils::streamFor($reqData)); - $media = new MediaFileUpload($client, $request, 'image/png', $data, true); - $request = $media->getRequest(); - $this->assertEquals(json_decode($reqData), (string) $request->getBody()); - - // Test multipart - we are sending encoded meta data and post data - $request = new Request('POST', 'http://www.example.com/'); - $reqData = json_encode("hello"); - $request = $request->withBody(Psr7\Utils::streamFor($reqData)); - $media = new MediaFileUpload($client, $request, 'image/png', $data, false); - $request = $media->getRequest(); - $this->assertStringContainsString($reqData, (string) $request->getBody()); - $this->assertStringContainsString(base64_encode($data), (string) $request->getBody()); - } - - public function testGetResumeUri() - { - $this->checkToken(); - - $client = $this->getClient(); - $client->addScope("https://www.googleapis.com/auth/drive"); - $service = new Drive($client); - $file = new Drive\DriveFile(); - $file->name = 'TESTFILE-testGetResumeUri'; - $chunkSizeBytes = 1 * 1024 * 1024; - - // Call the API with the media upload, defer so it doesn't immediately return. - $client->setDefer(true); - $request = $service->files->create($file); - - // Create a media file upload to represent our upload process. - $media = new MediaFileUpload( - $client, - $request, - 'text/plain', - null, - true, - $chunkSizeBytes - ); - - // request the resumable url - $uri = $media->getResumeUri(); - $this->assertIsString($uri); - - // parse the URL - $parts = parse_url(http://github.com/$uri); - $this->assertArrayHasKey('query', $parts); - - // parse the querystring - parse_str($parts['query'], $query); - $this->assertArrayHasKey('uploadType', $query); - $this->assertArrayHasKey('upload_id', $query); - $this->assertEquals('resumable', $query['uploadType']); - } - - public function testNextChunk() - { - $this->checkToken(); - - $client = $this->getClient(); - $client->addScope("https://www.googleapis.com/auth/drive"); - $service = new Drive($client); - - $data = 'foo'; - $file = new Drive\DriveFile(); - $file->name = $name = 'TESTFILE-testNextChunk'; - - // Call the API with the media upload, defer so it doesn't immediately return. - $client->setDefer(true); - $request = $service->files->create($file); - - // Create a media file upload to represent our upload process. - $media = new MediaFileUpload( - $client, - $request, - 'text/plain', - null, - true - ); - $media->setFileSize(strlen($data)); - - // upload the file - $file = $media->nextChunk($data); - $this->assertInstanceOf(Drive\DriveFile::class, $file); - $this->assertEquals($name, $file->name); - - // remove the file - $client->setDefer(false); - $response = $service->files->delete($file->id); - $this->assertEquals(204, $response->getStatusCode()); - } - - public function testNextChunkWithMoreRemaining() - { - $this->checkToken(); - - $client = $this->getClient(); - $client->addScope("https://www.googleapis.com/auth/drive"); - $service = new Drive($client); - - $chunkSizeBytes = 262144; // smallest chunk size allowed by APIs - $data = str_repeat('.', $chunkSizeBytes+1); - $file = new Drive\DriveFile(); - $file->name = $name = 'TESTFILE-testNextChunkWithMoreRemaining'; - - // Call the API with the media upload, defer so it doesn't immediately return. - $client->setDefer(true); - $request = $service->files->create($file); - - // Create a media file upload to represent our upload process. - $media = new MediaFileUpload( - $client, - $request, - 'text/plain', - $data, - true, - $chunkSizeBytes - ); - $media->setFileSize(strlen($data)); - - // upload the file - $file = $media->nextChunk(); - // false means we aren't done uploading, which is exactly what we expect! - $this->assertFalse($file); - } + public function testMediaFile() + { + $client = $this->getClient(); + $request = new Request('POST', 'http://www.example.com/'); + $media = new MediaFileUpload( + $client, + $request, + 'image/png', + base64_decode('data:image/png;base64,a') + ); + $request = $media->getRequest(); + + $this->assertEquals(0, $media->getProgress()); + $this->assertGreaterThan(0, strlen($request->getBody())); + } + + public function testGetUploadType() + { + $client = $this->getClient(); + $request = new Request('POST', 'http://www.example.com/'); + + // Test resumable upload + $media = new MediaFileUpload($client, $request, 'image/png', 'a', true); + $this->assertEquals('resumable', $media->getUploadType(null)); + + // Test data *only* uploads + $media = new MediaFileUpload($client, $request, 'image/png', 'a', false); + $this->assertEquals('media', $media->getUploadType(null)); + + // Test multipart uploads + $media = new MediaFileUpload($client, $request, 'image/png', 'a', false); + $this->assertEquals('multipart', $media->getUploadType(array('a' => 'b'))); + } + + public function testProcess() + { + $client = $this->getClient(); + $data = 'foo'; + + // Test data *only* uploads. + $request = new Request('POST', 'http://www.example.com/'); + $media = new MediaFileUpload($client, $request, 'image/png', $data, false); + $request = $media->getRequest(); + $this->assertEquals($data, (string) $request->getBody()); + + // Test resumable (meta data) - we want to send the metadata, not the app data. + $request = new Request('POST', 'http://www.example.com/'); + $reqData = json_encode("hello"); + $request = $request->withBody(Psr7\Utils::streamFor($reqData)); + $media = new MediaFileUpload($client, $request, 'image/png', $data, true); + $request = $media->getRequest(); + $this->assertEquals(json_decode($reqData), (string) $request->getBody()); + + // Test multipart - we are sending encoded meta data and post data + $request = new Request('POST', 'http://www.example.com/'); + $reqData = json_encode("hello"); + $request = $request->withBody(Psr7\Utils::streamFor($reqData)); + $media = new MediaFileUpload($client, $request, 'image/png', $data, false); + $request = $media->getRequest(); + $this->assertStringContainsString($reqData, (string) $request->getBody()); + $this->assertStringContainsString(base64_encode($data), (string) $request->getBody()); + } + + public function testGetResumeUri() + { + $this->checkToken(); + + $client = $this->getClient(); + $client->addScope("https://www.googleapis.com/auth/drive"); + $service = new Drive($client); + $file = new Drive\DriveFile(); + $file->name = 'TESTFILE-testGetResumeUri'; + $chunkSizeBytes = 1 * 1024 * 1024; + + // Call the API with the media upload, defer so it doesn't immediately return. + $client->setDefer(true); + $request = $service->files->create($file); + + // Create a media file upload to represent our upload process. + $media = new MediaFileUpload( + $client, + $request, + 'text/plain', + null, + true, + $chunkSizeBytes + ); + + // request the resumable url + $uri = $media->getResumeUri(); + $this->assertIsString($uri); + + // parse the URL + $parts = parse_url(http://github.com/$uri); + $this->assertArrayHasKey('query', $parts); + + // parse the querystring + parse_str($parts['query'], $query); + $this->assertArrayHasKey('uploadType', $query); + $this->assertArrayHasKey('upload_id', $query); + $this->assertEquals('resumable', $query['uploadType']); + } + + public function testNextChunk() + { + $this->checkToken(); + + $client = $this->getClient(); + $client->addScope("https://www.googleapis.com/auth/drive"); + $service = new Drive($client); + + $data = 'foo'; + $file = new Drive\DriveFile(); + $file->name = $name = 'TESTFILE-testNextChunk'; + + // Call the API with the media upload, defer so it doesn't immediately return. + $client->setDefer(true); + $request = $service->files->create($file); + + // Create a media file upload to represent our upload process. + $media = new MediaFileUpload( + $client, + $request, + 'text/plain', + null, + true + ); + $media->setFileSize(strlen($data)); + + // upload the file + $file = $media->nextChunk($data); + $this->assertInstanceOf(Drive\DriveFile::class, $file); + $this->assertEquals($name, $file->name); + + // remove the file + $client->setDefer(false); + $response = $service->files->delete($file->id); + $this->assertEquals(204, $response->getStatusCode()); + } + + public function testNextChunkWithMoreRemaining() + { + $this->checkToken(); + + $client = $this->getClient(); + $client->addScope("https://www.googleapis.com/auth/drive"); + $service = new Drive($client); + + $chunkSizeBytes = 262144; // smallest chunk size allowed by APIs + $data = str_repeat('.', $chunkSizeBytes+1); + $file = new Drive\DriveFile(); + $file->name = $name = 'TESTFILE-testNextChunkWithMoreRemaining'; + + // Call the API with the media upload, defer so it doesn't immediately return. + $client->setDefer(true); + $request = $service->files->create($file); + + // Create a media file upload to represent our upload process. + $media = new MediaFileUpload( + $client, + $request, + 'text/plain', + $data, + true, + $chunkSizeBytes + ); + $media->setFileSize(strlen($data)); + + // upload the file + $file = $media->nextChunk(); + // false means we aren't done uploading, which is exactly what we expect! + $this->assertFalse($file); + } } diff --git a/tests/Google/Http/RESTTest.php b/tests/Google/Http/RESTTest.php index fb15ac238..2c8bb2136 100644 --- a/tests/Google/Http/RESTTest.php +++ b/tests/Google/Http/RESTTest.php @@ -27,115 +27,116 @@ class RESTTest extends BaseTest { - /** + /** * @var REST $rest */ - private $rest; - - public function set_up() - { - $this->rest = new REST(); - $this->request = new Request('GET', '/'); - } - - public function testDecodeResponse() - { - $client = $this->getClient(); - $response = new Response(204); - $decoded = $this->rest->decodeHttpResponse($response, $this->request); - $this->assertEquals($response, $decoded); - - foreach (array(200, 201) as $code) { - $headers = array('foo', 'bar'); - $stream = Psr7\Utils::streamFor('{"a": 1}'); - $response = new Response($code, $headers, $stream); - - $decoded = $this->rest->decodeHttpResponse($response, $this->request); - $this->assertEquals('{"a": 1}', (string) $decoded->getBody()); + private $rest; + + public function set_up() + { + $this->rest = new REST(); + $this->request = new Request('GET', '/'); + } + + public function testDecodeResponse() + { + $client = $this->getClient(); + $response = new Response(204); + $decoded = $this->rest->decodeHttpResponse($response, $this->request); + $this->assertEquals($response, $decoded); + + foreach (array(200, 201) as $code) { + $headers = array('foo', 'bar'); + $stream = Psr7\Utils::streamFor('{"a": 1}'); + $response = new Response($code, $headers, $stream); + + $decoded = $this->rest->decodeHttpResponse($response, $this->request); + $this->assertEquals('{"a": 1}', (string) $decoded->getBody()); + } + } + + public function testDecodeMediaResponse() + { + $client = $this->getClient(); + + $request = new Request('GET', 'http://www.example.com/?alt=media'); + $headers = array(); + $stream = Psr7\Utils::streamFor('thisisnotvalidjson'); + $response = new Response(200, $headers, $stream); + + $decoded = $this->rest->decodeHttpResponse($response, $request); + $this->assertEquals('thisisnotvalidjson', (string) $decoded->getBody()); + } + + + public function testDecode500ResponseThrowsException() + { + $this->expectException(ServiceException::class); + $response = new Response(500); + $this->rest->decodeHttpResponse($response, $this->request); + } + + public function testExceptionResponse() + { + $this->expectException(ServiceException::class); + $http = new GuzzleClient(); + + $request = new Request('GET', 'http://httpbin.org/status/500'); + $response = $this->rest->doExecute($http, $request); + } + + public function testDecodeEmptyResponse() + { + $stream = Psr7\Utils::streamFor('{}'); + $response = new Response(200, array(), $stream); + $decoded = $this->rest->decodeHttpResponse($response, $this->request); + $this->assertEquals('{}', (string) $decoded->getBody()); + } + + public function testBadErrorFormatting() + { + $this->expectException(ServiceException::class); + $stream = Psr7\Utils::streamFor( + '{ + "error": { + "code": 500, + "message": null + } + }' + ); + $response = new Response(500, array(), $stream); + $this->rest->decodeHttpResponse($response, $this->request); + } + + public function tesProperErrorFormatting() + { + $this->expectException(ServiceException::class); + $stream = Psr7\Utils::streamFor( + '{ + error: { + errors: [ + { + "domain": "global", + "reason": "authError", + "message": "Invalid Credentials", + "locationType": "header", + "location": "Authorization", + } + ], + "code": 401, + "message": "Invalid Credentials" + } + }' + ); + $response = new Response(401, array(), $stream); + $this->rest->decodeHttpResponse($response, $this->request); + } + + public function testNotJson404Error() + { + $this->expectException(ServiceException::class); + $stream = Psr7\Utils::streamFor('Not Found'); + $response = new Response(404, array(), $stream); + $this->rest->decodeHttpResponse($response, $this->request); } - } - - public function testDecodeMediaResponse() - { - $client = $this->getClient(); - - $request = new Request('GET', 'http://www.example.com/?alt=media'); - $headers = array(); - $stream = Psr7\Utils::streamFor('thisisnotvalidjson'); - $response = new Response(200, $headers, $stream); - - $decoded = $this->rest->decodeHttpResponse($response, $request); - $this->assertEquals('thisisnotvalidjson', (string) $decoded->getBody()); - } - - - public function testDecode500ResponseThrowsException() - { - $this->expectException(ServiceException::class); - $response = new Response(500); - $this->rest->decodeHttpResponse($response, $this->request); - } - - public function testExceptionResponse() - { - $this->expectException(ServiceException::class); - $http = new GuzzleClient(); - - $request = new Request('GET', 'http://httpbin.org/status/500'); - $response = $this->rest->doExecute($http, $request); - } - - public function testDecodeEmptyResponse() - { - $stream = Psr7\Utils::streamFor('{}'); - $response = new Response(200, array(), $stream); - $decoded = $this->rest->decodeHttpResponse($response, $this->request); - $this->assertEquals('{}', (string) $decoded->getBody()); - } - - public function testBadErrorFormatting() - { - $this->expectException(ServiceException::class); - $stream = Psr7\Utils::streamFor( - '{ - "error": { - "code": 500, - "message": null - } - }' - ); - $response = new Response(500, array(), $stream); - $this->rest->decodeHttpResponse($response, $this->request); - } - - public function tesProperErrorFormatting() - { - $this->expectException(ServiceException::class); - $stream = Psr7\Utils::streamFor( - '{ - error: { - errors: [ - { - "domain": "global", - "reason": "authError", - "message": "Invalid Credentials", - "locationType": "header", - "location": "Authorization", - } - ], - "code": 401, - "message": "Invalid Credentials" - }' - ); - $response = new Response(401, array(), $stream); - $this->rest->decodeHttpResponse($response, $this->request); - } - - public function testNotJson404Error() - { - $this->expectException(ServiceException::class); - $stream = Psr7\Utils::streamFor('Not Found'); - $response = new Response(404, array(), $stream); - $this->rest->decodeHttpResponse($response, $this->request); - } } diff --git a/tests/Google/ModelTest.php b/tests/Google/ModelTest.php index 240faabcc..35abcd310 100644 --- a/tests/Google/ModelTest.php +++ b/tests/Google/ModelTest.php @@ -28,7 +28,7 @@ class ModelTest extends BaseTest { - private $calendarData = '{ + private $calendarData = '{ "kind": "calendar#event", "etag": "\"-kteSF26GsdKQ5bfmcd4H3_-u3g/MTE0NTUyNTAxOTk0MjAwMA\"", "id": "1234566", @@ -61,229 +61,229 @@ class ModelTest extends BaseTest } }'; - public function testIntentionalNulls() - { - $data = json_decode($this->calendarData, true); - $event = new Calendar\Event($data); - $obj = json_decode(json_encode($event->toSimpleObject()), true); - $this->assertArrayHasKey('date', $obj['start']); - $this->assertArrayNotHasKey('dateTime', $obj['start']); - $date = new Calendar\EventDateTime(); - $date->setDate(Model::NULL_VALUE); - $event->setStart($date); - $obj = json_decode(json_encode($event->toSimpleObject()), true); - $this->assertNull($obj['start']['date']); - $this->assertArrayHasKey('date', $obj['start']); - $this->assertArrayNotHasKey('dateTime', $obj['start']); - } - public function testModelMutation() - { - $data = json_decode($this->calendarData, true); - $event = new Calendar\Event($data); - $date = new Calendar\EventDateTime(); - date_default_timezone_set('UTC'); - $dateString = Date("c"); - $summary = "hello"; - $date->setDate($dateString); - $event->setStart($date); - $event->setEnd($date); - $event->setSummary($summary); - $simpleEvent = $event->toSimpleObject(); - $this->assertEquals($dateString, $simpleEvent->start->date); - $this->assertEquals($dateString, $simpleEvent->end->date); - $this->assertEquals($summary, $simpleEvent->summary); + public function testIntentionalNulls() + { + $data = json_decode($this->calendarData, true); + $event = new Calendar\Event($data); + $obj = json_decode(json_encode($event->toSimpleObject()), true); + $this->assertArrayHasKey('date', $obj['start']); + $this->assertArrayNotHasKey('dateTime', $obj['start']); + $date = new Calendar\EventDateTime(); + $date->setDate(Model::NULL_VALUE); + $event->setStart($date); + $obj = json_decode(json_encode($event->toSimpleObject()), true); + $this->assertNull($obj['start']['date']); + $this->assertArrayHasKey('date', $obj['start']); + $this->assertArrayNotHasKey('dateTime', $obj['start']); + } + public function testModelMutation() + { + $data = json_decode($this->calendarData, true); + $event = new Calendar\Event($data); + $date = new Calendar\EventDateTime(); + date_default_timezone_set('UTC'); + $dateString = Date("c"); + $summary = "hello"; + $date->setDate($dateString); + $event->setStart($date); + $event->setEnd($date); + $event->setSummary($summary); + $simpleEvent = $event->toSimpleObject(); + $this->assertEquals($dateString, $simpleEvent->start->date); + $this->assertEquals($dateString, $simpleEvent->end->date); + $this->assertEquals($summary, $simpleEvent->summary); - $event2 = new Calendar\Event(); - $this->assertNull($event2->getStart()); - } + $event2 = new Calendar\Event(); + $this->assertNull($event2->getStart()); + } - public function testVariantTypes() - { - $file = new Drive\DriveFile(); - $metadata = new Drive\DriveFileImageMediaMetadata(); - $metadata->setCameraMake('Pokémon Snap'); - $file->setImageMediaMetadata($metadata); - $data = json_decode(json_encode($file->toSimpleObject()), true); - $this->assertEquals('Pokémon Snap', $data['imageMediaMetadata']['cameraMake']); - } + public function testVariantTypes() + { + $file = new Drive\DriveFile(); + $metadata = new Drive\DriveFileImageMediaMetadata(); + $metadata->setCameraMake('Pokémon Snap'); + $file->setImageMediaMetadata($metadata); + $data = json_decode(json_encode($file->toSimpleObject()), true); + $this->assertEquals('Pokémon Snap', $data['imageMediaMetadata']['cameraMake']); + } - public function testOddMappingNames() - { - $creative = new AdExchangeBuyer\Creative(); - $creative->setAccountId('12345'); - $creative->setBuyerCreativeId('12345'); - $creative->setAdvertiserName('Hi'); - $creative->setHTMLSnippet("

    Foo!

    "); - $creative->setClickThroughUrl(array('http://somedomain.com/')); - $creative->setWidth(100); - $creative->setHeight(100); - $data = json_decode(json_encode($creative->toSimpleObject()), true); - $this->assertEquals($data['HTMLSnippet'], "

    Foo!

    "); - $this->assertEquals($data['width'], 100); - $this->assertEquals($data['height'], 100); - $this->assertEquals($data['accountId'], "12345"); - } + public function testOddMappingNames() + { + $creative = new AdExchangeBuyer\Creative(); + $creative->setAccountId('12345'); + $creative->setBuyerCreativeId('12345'); + $creative->setAdvertiserName('Hi'); + $creative->setHTMLSnippet("

    Foo!

    "); + $creative->setClickThroughUrl(array('http://somedomain.com/')); + $creative->setWidth(100); + $creative->setHeight(100); + $data = json_decode(json_encode($creative->toSimpleObject()), true); + $this->assertEquals($data['HTMLSnippet'], "

    Foo!

    "); + $this->assertEquals($data['width'], 100); + $this->assertEquals($data['height'], 100); + $this->assertEquals($data['accountId'], "12345"); + } - public function testJsonStructure() - { - $model = new Model(); - $model->publicA = "This is a string"; - $model2 = new Model(); - $model2->publicC = 12345; - $model2->publicD = null; - $model->publicB = $model2; - $model3 = new Model(); - $model3->publicE = 54321; - $model3->publicF = null; - $model->publicG = array($model3, "hello", false); - $model->publicH = false; - $model->publicI = 0; - $string = json_encode($model->toSimpleObject()); - $data = json_decode($string, true); - $this->assertEquals(12345, $data['publicB']['publicC']); - $this->assertEquals("This is a string", $data['publicA']); - $this->assertArrayNotHasKey("publicD", $data['publicB']); - $this->assertArrayHasKey("publicE", $data['publicG'][0]); - $this->assertArrayNotHasKey("publicF", $data['publicG'][0]); - $this->assertEquals("hello", $data['publicG'][1]); - $this->assertFalse($data['publicG'][2]); - $this->assertArrayNotHasKey("data", $data); - $this->assertFalse($data['publicH']); - $this->assertEquals(0, $data['publicI']); - } + public function testJsonStructure() + { + $model = new Model(); + $model->publicA = "This is a string"; + $model2 = new Model(); + $model2->publicC = 12345; + $model2->publicD = null; + $model->publicB = $model2; + $model3 = new Model(); + $model3->publicE = 54321; + $model3->publicF = null; + $model->publicG = array($model3, "hello", false); + $model->publicH = false; + $model->publicI = 0; + $string = json_encode($model->toSimpleObject()); + $data = json_decode($string, true); + $this->assertEquals(12345, $data['publicB']['publicC']); + $this->assertEquals("This is a string", $data['publicA']); + $this->assertArrayNotHasKey("publicD", $data['publicB']); + $this->assertArrayHasKey("publicE", $data['publicG'][0]); + $this->assertArrayNotHasKey("publicF", $data['publicG'][0]); + $this->assertEquals("hello", $data['publicG'][1]); + $this->assertFalse($data['publicG'][2]); + $this->assertArrayNotHasKey("data", $data); + $this->assertFalse($data['publicH']); + $this->assertEquals(0, $data['publicI']); + } - public function testIssetPropertyOnModel() - { - $model = new Model(); - $model['foo'] = 'bar'; - $this->assertTrue(isset($model->foo)); - } + public function testIssetPropertyOnModel() + { + $model = new Model(); + $model['foo'] = 'bar'; + $this->assertTrue(isset($model->foo)); + } - public function testUnsetPropertyOnModel() - { - $model = new Model(); - $model['foo'] = 'bar'; - unset($model->foo); - $this->assertFalse(isset($model->foo)); - } + public function testUnsetPropertyOnModel() + { + $model = new Model(); + $model['foo'] = 'bar'; + unset($model->foo); + $this->assertFalse(isset($model->foo)); + } - public function testCollectionWithItemsFromConstructor() - { - $data = json_decode( - '{ - "kind": "calendar#events", - "id": "1234566", - "etag": "abcdef", - "totalItems": 4, - "items": [ - {"id": 1}, - {"id": 2}, - {"id": 3}, - {"id": 4} - ] - }', - true - ); - $collection = new Calendar\Events($data); - $this->assertCount(4, $collection); - $count = 0; - foreach ($collection as $col) { - $count++; + public function testCollectionWithItemsFromConstructor() + { + $data = json_decode( + '{ + "kind": "calendar#events", + "id": "1234566", + "etag": "abcdef", + "totalItems": 4, + "items": [ + {"id": 1}, + {"id": 2}, + {"id": 3}, + {"id": 4} + ] + }', + true + ); + $collection = new Calendar\Events($data); + $this->assertCount(4, $collection); + $count = 0; + foreach ($collection as $col) { + $count++; + } + $this->assertEquals(4, $count); + $this->assertEquals(1, $collection[0]->id); } - $this->assertEquals(4, $count); - $this->assertEquals(1, $collection[0]->id); - } - public function testCollectionWithItemsFromSetter() - { - $data = json_decode( - '{ - "kind": "calendar#events", - "id": "1234566", - "etag": "abcdef", - "totalItems": 4 - }', - true - ); - $collection = new Calendar\Events($data); - $collection->setItems([ - new Calendar\Event(['id' => 1]), - new Calendar\Event(['id' => 2]), - new Calendar\Event(['id' => 3]), - new Calendar\Event(['id' => 4]), - ]); - $this->assertCount(4, $collection); - $count = 0; - foreach ($collection as $col) { - $count++; + public function testCollectionWithItemsFromSetter() + { + $data = json_decode( + '{ + "kind": "calendar#events", + "id": "1234566", + "etag": "abcdef", + "totalItems": 4 + }', + true + ); + $collection = new Calendar\Events($data); + $collection->setItems([ + new Calendar\Event(['id' => 1]), + new Calendar\Event(['id' => 2]), + new Calendar\Event(['id' => 3]), + new Calendar\Event(['id' => 4]), + ]); + $this->assertCount(4, $collection); + $count = 0; + foreach ($collection as $col) { + $count++; + } + $this->assertEquals(4, $count); + $this->assertEquals(1, $collection[0]->id); } - $this->assertEquals(4, $count); - $this->assertEquals(1, $collection[0]->id); - } - public function testMapDataType() - { - $data = json_decode( - '{ - "calendar": { - "regular": { "background": "#FFF", "foreground": "#000" }, - "inverted": { "background": "#000", "foreground": "#FFF" } - } - }', - true - ); - $collection = new Calendar\Colors($data); - $this->assertCount(2, $collection->calendar); - $this->assertTrue(isset($collection->calendar['regular'])); - $this->assertTrue(isset($collection->calendar['inverted'])); - $this->assertInstanceOf(Calendar\ColorDefinition::class, $collection->calendar['regular']); - $this->assertEquals('#FFF', $collection->calendar['regular']->getBackground()); - $this->assertEquals('#FFF', $collection->calendar['inverted']->getForeground()); - } + public function testMapDataType() + { + $data = json_decode( + '{ + "calendar": { + "regular": { "background": "#FFF", "foreground": "#000" }, + "inverted": { "background": "#000", "foreground": "#FFF" } + } + }', + true + ); + $collection = new Calendar\Colors($data); + $this->assertCount(2, $collection->calendar); + $this->assertTrue(isset($collection->calendar['regular'])); + $this->assertTrue(isset($collection->calendar['inverted'])); + $this->assertInstanceOf(Calendar\ColorDefinition::class, $collection->calendar['regular']); + $this->assertEquals('#FFF', $collection->calendar['regular']->getBackground()); + $this->assertEquals('#FFF', $collection->calendar['inverted']->getForeground()); + } - public function testPassingInstanceInConstructor() - { - $creator = new Calendar\EventCreator(); - $creator->setDisplayName('Brent Shaffer'); - $data = [ - "creator" => $creator - ]; - $event = new Calendar\Event($data); - $this->assertInstanceOf(Calendar\EventCreator::class, $event->getCreator()); - $this->assertEquals('Brent Shaffer', $event->creator->getDisplayName()); - } + public function testPassingInstanceInConstructor() + { + $creator = new Calendar\EventCreator(); + $creator->setDisplayName('Brent Shaffer'); + $data = [ + "creator" => $creator + ]; + $event = new Calendar\Event($data); + $this->assertInstanceOf(Calendar\EventCreator::class, $event->getCreator()); + $this->assertEquals('Brent Shaffer', $event->creator->getDisplayName()); + } - public function testPassingInstanceInConstructorForMap() - { - $regular = new Calendar\ColorDefinition(); - $regular->setBackground('#FFF'); - $regular->setForeground('#000'); - $data = [ - "calendar" => [ - "regular" => $regular, - "inverted" => [ "background" => "#000", "foreground" => "#FFF" ], - ] - ]; - $collection = new Calendar\Colors($data); - $this->assertCount(2, $collection->calendar); - $this->assertTrue(isset($collection->calendar['regular'])); - $this->assertTrue(isset($collection->calendar['inverted'])); - $this->assertInstanceOf(Calendar\ColorDefinition::class, $collection->calendar['regular']); - $this->assertEquals('#FFF', $collection->calendar['regular']->getBackground()); - $this->assertEquals('#FFF', $collection->calendar['inverted']->getForeground()); - } + public function testPassingInstanceInConstructorForMap() + { + $regular = new Calendar\ColorDefinition(); + $regular->setBackground('#FFF'); + $regular->setForeground('#000'); + $data = [ + "calendar" => [ + "regular" => $regular, + "inverted" => [ "background" => "#000", "foreground" => "#FFF" ], + ] + ]; + $collection = new Calendar\Colors($data); + $this->assertCount(2, $collection->calendar); + $this->assertTrue(isset($collection->calendar['regular'])); + $this->assertTrue(isset($collection->calendar['inverted'])); + $this->assertInstanceOf(Calendar\ColorDefinition::class, $collection->calendar['regular']); + $this->assertEquals('#FFF', $collection->calendar['regular']->getBackground()); + $this->assertEquals('#FFF', $collection->calendar['inverted']->getForeground()); + } - /** - * @see https://github.com/google/google-api-php-client/issues/1308 - */ - public function testKeyTypePropertyConflict() - { - $data = [ - "duration" => 0, - "durationType" => "unknown", - ]; - $creativeAsset = new Dfareporting\CreativeAsset($data); - $this->assertEquals(0, $creativeAsset->getDuration()); - $this->assertEquals('unknown', $creativeAsset->getDurationType()); - } + /** + * @see https://github.com/google/google-api-php-client/issues/1308 + */ + public function testKeyTypePropertyConflict() + { + $data = [ + "duration" => 0, + "durationType" => "unknown", + ]; + $creativeAsset = new Dfareporting\CreativeAsset($data); + $this->assertEquals(0, $creativeAsset->getDuration()); + $this->assertEquals('unknown', $creativeAsset->getDurationType()); + } } diff --git a/tests/Google/Service/AdSenseTest.php b/tests/Google/Service/AdSenseTest.php index 658c46a3c..3599f7abc 100644 --- a/tests/Google/Service/AdSenseTest.php +++ b/tests/Google/Service/AdSenseTest.php @@ -22,472 +22,472 @@ class AdSenseTest extends BaseTest { - public $adsense; - public function set_up() - { - $this->markTestSkipped('Thesse tests need to be fixed'); - $this->checkToken(); - $this->adsense = new AdSense($this->getClient()); - } - - public function testAccountsList() - { - $accounts = $this->adsense->accounts->listAccounts(); - $this->assertArrayHasKey('kind', $accounts); - $this->assertEquals($accounts['kind'], 'adsense#accounts'); - $account = $this->getRandomElementFromArray($accounts['items']); - $this->checkAccountElement($account); - } - - /** - * @depends testAccountsList - */ - public function testAccountsGet() - { - $accounts = $this->adsense->accounts->listAccounts(); - $account = $this->getRandomElementFromArray($accounts['items']); - $retrievedAccount = $this->adsense->accounts->get($account['id']); - $this->checkAccountElement($retrievedAccount); - } - - /** - * @depends testAccountsList - */ - public function testAccountsReportGenerate() - { - $startDate = '2011-01-01'; - $endDate = '2011-01-31'; - $optParams = $this->getReportOptParams(); - $accounts = $this->adsense->accounts->listAccounts(); - $accountId = $accounts['items'][0]['id']; - $report = $this->adsense->accounts_reports->generate( - $accountId, - $startDate, - $endDate, - $optParams - ); - $this->checkReport($report); - } - - /** - * @depends testAccountsList - */ - public function testAccountsAdClientsList() - { - $accounts = $this->adsense->accounts->listAccounts(); - $account = $this->getRandomElementFromArray($accounts['items']); - $adClients = - $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); - $this->checkAdClientsCollection($adClients); - } - - /** - * @depends testAccountsList - * @depends testAccountsAdClientsList - */ - public function testAccountsAdUnitsList() - { - $accounts = $this->adsense->accounts->listAccounts(); - foreach ($accounts['items'] as $account) { - $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); - foreach ($adClients['items'] as $adClient) { - $adUnits = $this->adsense->accounts_adunits->listAccountsAdunits( - $account['id'], - $adClient['id'] - ); - $this->checkAdUnitsCollection($adUnits); - break 2; - } + public $adsense; + public function set_up() + { + $this->markTestSkipped('Thesse tests need to be fixed'); + $this->checkToken(); + $this->adsense = new AdSense($this->getClient()); } - } - /** - * @depends testAccountsList - * @depends testAccountsAdClientsList - */ - public function testAccountsAdUnitsGet() - { - $accounts = $this->adsense->accounts->listAccounts(); - foreach ($accounts['items'] as $account) { - $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); - foreach ($adClients['items'] as $adClient) { - $adUnits = $this->adsense->accounts_adunits->listAccountsAdunits( - $account['id'], - $adClient['id'] - ); - if (array_key_exists('items', $adUnits)) { - $adUnit = $this->getRandomElementFromArray($adUnits['items']); - $this->checkAdUnitElement($adUnit); - break 2; - } - } + public function testAccountsList() + { + $accounts = $this->adsense->accounts->listAccounts(); + $this->assertArrayHasKey('kind', $accounts); + $this->assertEquals($accounts['kind'], 'adsense#accounts'); + $account = $this->getRandomElementFromArray($accounts['items']); + $this->checkAccountElement($account); } - } - /** - * @depends testAccountsList - * @depends testAccountsAdClientsList - */ - public function testAccountsCustomChannelsList() - { - $accounts = $this->adsense->accounts->listAccounts(); - foreach ($accounts['items'] as $account) { - $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); - foreach ($adClients['items'] as $adClient) { - $customChannels = $this->adsense->accounts_customchannels - ->listAccountsCustomchannels($account['id'], $adClient['id']); - $this->checkCustomChannelsCollection($customChannels); - break 2; - } + /** + * @depends testAccountsList + */ + public function testAccountsGet() + { + $accounts = $this->adsense->accounts->listAccounts(); + $account = $this->getRandomElementFromArray($accounts['items']); + $retrievedAccount = $this->adsense->accounts->get($account['id']); + $this->checkAccountElement($retrievedAccount); } - } - /** - * @depends testAccountsList - * @depends testAccountsAdClientsList - */ - public function testAccountsCustomChannelsGet() - { - $accounts = $this->adsense->accounts->listAccounts(); - foreach ($accounts['items'] as $account) { - $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); - foreach ($adClients['items'] as $adClient) { - $customChannels = - $this->adsense->accounts_customchannels->listAccountsCustomchannels( - $account['id'], - $adClient['id'] - ); - if (array_key_exists('items', $customChannels)) { - $customChannel = - $this->getRandomElementFromArray($customChannels['items']); - $this->checkCustomChannelElement($customChannel); - break 2; - } - } + /** + * @depends testAccountsList + */ + public function testAccountsReportGenerate() + { + $startDate = '2011-01-01'; + $endDate = '2011-01-31'; + $optParams = $this->getReportOptParams(); + $accounts = $this->adsense->accounts->listAccounts(); + $accountId = $accounts['items'][0]['id']; + $report = $this->adsense->accounts_reports->generate( + $accountId, + $startDate, + $endDate, + $optParams + ); + $this->checkReport($report); } - } - /** - * @depends testAccountsList - * @depends testAccountsAdClientsList - */ - public function testAccountsUrlChannelsList() - { - $accounts = $this->adsense->accounts->listAccounts(); - foreach ($accounts['items'] as $account) { - $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); - foreach ($adClients['items'] as $adClient) { - $urlChannels = - $this->adsense->accounts_urlchannels->listAccountsUrlchannels( - $account['id'], - $adClient['id'] - ); - $this->checkUrlChannelsCollection($urlChannels); - break 2; - } + /** + * @depends testAccountsList + */ + public function testAccountsAdClientsList() + { + $accounts = $this->adsense->accounts->listAccounts(); + $account = $this->getRandomElementFromArray($accounts['items']); + $adClients = + $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); + $this->checkAdClientsCollection($adClients); } - } - /** - * @depends testAccountsList - * @depends testAccountsAdClientsList - * @depends testAccountsAdUnitsList - */ - public function testAccountsAdUnitsCustomChannelsList() - { - $accounts = $this->adsense->accounts->listAccounts(); - foreach ($accounts['items'] as $account) { - $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); - foreach ($adClients['items'] as $adClient) { - $adUnits = - $this->adsense->accounts_adunits->listAccountsAdunits($account['id'], $adClient['id']); - if (array_key_exists('items', $adUnits)) { - foreach ($adUnits['items'] as $adUnit) { - $customChannels = - $this->adsense->accounts_adunits_customchannels->listAccountsAdunitsCustomchannels( + /** + * @depends testAccountsList + * @depends testAccountsAdClientsList + */ + public function testAccountsAdUnitsList() + { + $accounts = $this->adsense->accounts->listAccounts(); + foreach ($accounts['items'] as $account) { + $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); + foreach ($adClients['items'] as $adClient) { + $adUnits = $this->adsense->accounts_adunits->listAccountsAdunits( $account['id'], - $adClient['id'], - $adUnit['id'] + $adClient['id'] ); - $this->checkCustomChannelsCollection($customChannels); - // it's too expensive to go through each, if one is correct good - break 3; - } + $this->checkAdUnitsCollection($adUnits); + break 2; + } } - } } - } - /** - * @depends testAccountsList - * @depends testAccountsAdClientsList - * @depends testAccountsCustomChannelsList - */ - public function testAccountsCustomChannelsAdUnitsList() - { - $accounts = $this->adsense->accounts->listAccounts(); - foreach ($accounts['items'] as $account) { - $adClients = - $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); - foreach ($adClients['items'] as $adClient) { - $customChannels = - $this->adsense->accounts_customchannels->listAccountsCustomchannels( - $account['id'], - $adClient['id'] - ); - if (array_key_exists('items', $customChannels)) { - foreach ($customChannels['items'] as $customChannel) { - $adUnits = - $this->adsense->accounts_customchannels_adunits->listAccountsCustomchannelsAdunits( + /** + * @depends testAccountsList + * @depends testAccountsAdClientsList + */ + public function testAccountsAdUnitsGet() + { + $accounts = $this->adsense->accounts->listAccounts(); + foreach ($accounts['items'] as $account) { + $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); + foreach ($adClients['items'] as $adClient) { + $adUnits = $this->adsense->accounts_adunits->listAccountsAdunits( $account['id'], - $adClient['id'], - $customChannel['id'] + $adClient['id'] ); - $this->checkAdUnitsCollection($adUnits); - // it's too expensive to go through each, if one is correct good - break 3; - } + if (array_key_exists('items', $adUnits)) { + $adUnit = $this->getRandomElementFromArray($adUnits['items']); + $this->checkAdUnitElement($adUnit); + break 2; + } + } + } + } + + /** + * @depends testAccountsList + * @depends testAccountsAdClientsList + */ + public function testAccountsCustomChannelsList() + { + $accounts = $this->adsense->accounts->listAccounts(); + foreach ($accounts['items'] as $account) { + $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); + foreach ($adClients['items'] as $adClient) { + $customChannels = $this->adsense->accounts_customchannels + ->listAccountsCustomchannels($account['id'], $adClient['id']); + $this->checkCustomChannelsCollection($customChannels); + break 2; + } + } + } + + /** + * @depends testAccountsList + * @depends testAccountsAdClientsList + */ + public function testAccountsCustomChannelsGet() + { + $accounts = $this->adsense->accounts->listAccounts(); + foreach ($accounts['items'] as $account) { + $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); + foreach ($adClients['items'] as $adClient) { + $customChannels = + $this->adsense->accounts_customchannels->listAccountsCustomchannels( + $account['id'], + $adClient['id'] + ); + if (array_key_exists('items', $customChannels)) { + $customChannel = + $this->getRandomElementFromArray($customChannels['items']); + $this->checkCustomChannelElement($customChannel); + break 2; + } + } + } + } + + /** + * @depends testAccountsList + * @depends testAccountsAdClientsList + */ + public function testAccountsUrlChannelsList() + { + $accounts = $this->adsense->accounts->listAccounts(); + foreach ($accounts['items'] as $account) { + $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); + foreach ($adClients['items'] as $adClient) { + $urlChannels = + $this->adsense->accounts_urlchannels->listAccountsUrlchannels( + $account['id'], + $adClient['id'] + ); + $this->checkUrlChannelsCollection($urlChannels); + break 2; + } + } + } + + /** + * @depends testAccountsList + * @depends testAccountsAdClientsList + * @depends testAccountsAdUnitsList + */ + public function testAccountsAdUnitsCustomChannelsList() + { + $accounts = $this->adsense->accounts->listAccounts(); + foreach ($accounts['items'] as $account) { + $adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); + foreach ($adClients['items'] as $adClient) { + $adUnits = + $this->adsense->accounts_adunits->listAccountsAdunits($account['id'], $adClient['id']); + if (array_key_exists('items', $adUnits)) { + foreach ($adUnits['items'] as $adUnit) { + $customChannels = + $this->adsense->accounts_adunits_customchannels->listAccountsAdunitsCustomchannels( + $account['id'], + $adClient['id'], + $adUnit['id'] + ); + $this->checkCustomChannelsCollection($customChannels); + // it's too expensive to go through each, if one is correct good + break 3; + } + } + } + } + } + + /** + * @depends testAccountsList + * @depends testAccountsAdClientsList + * @depends testAccountsCustomChannelsList + */ + public function testAccountsCustomChannelsAdUnitsList() + { + $accounts = $this->adsense->accounts->listAccounts(); + foreach ($accounts['items'] as $account) { + $adClients = + $this->adsense->accounts_adclients->listAccountsAdclients($account['id']); + foreach ($adClients['items'] as $adClient) { + $customChannels = + $this->adsense->accounts_customchannels->listAccountsCustomchannels( + $account['id'], + $adClient['id'] + ); + if (array_key_exists('items', $customChannels)) { + foreach ($customChannels['items'] as $customChannel) { + $adUnits = + $this->adsense->accounts_customchannels_adunits->listAccountsCustomchannelsAdunits( + $account['id'], + $adClient['id'], + $customChannel['id'] + ); + $this->checkAdUnitsCollection($adUnits); + // it's too expensive to go through each, if one is correct good + break 3; + } + } + } } - } } - } - public function testAdClientsList() - { - $adClients = $this->adsense->adclients->listAdclients(); - $this->checkAdClientsCollection($adClients); - } + public function testAdClientsList() + { + $adClients = $this->adsense->adclients->listAdclients(); + $this->checkAdClientsCollection($adClients); + } - /** + /** * @depends testAdClientsList */ - public function testAdUnitsList() - { - $adClients = $this->adsense->adclients->listAdclients(); - foreach ($adClients['items'] as $adClient) { - $adUnits = $this->adsense->adunits->listAdunits($adClient['id']); - $this->checkAdUnitsCollection($adUnits); + public function testAdUnitsList() + { + $adClients = $this->adsense->adclients->listAdclients(); + foreach ($adClients['items'] as $adClient) { + $adUnits = $this->adsense->adunits->listAdunits($adClient['id']); + $this->checkAdUnitsCollection($adUnits); + } } - } - /** + /** * @depends testAdClientsList */ - public function testAdUnitsGet() - { - $adClients = $this->adsense->adclients->listAdclients(); - foreach ($adClients['items'] as $adClient) { - $adUnits = $this->adsense->adunits->listAdunits($adClient['id']); - if (array_key_exists('items', $adUnits)) { - $adUnit = $this->getRandomElementFromArray($adUnits['items']); - $this->checkAdUnitElement($adUnit); - break 1; - } + public function testAdUnitsGet() + { + $adClients = $this->adsense->adclients->listAdclients(); + foreach ($adClients['items'] as $adClient) { + $adUnits = $this->adsense->adunits->listAdunits($adClient['id']); + if (array_key_exists('items', $adUnits)) { + $adUnit = $this->getRandomElementFromArray($adUnits['items']); + $this->checkAdUnitElement($adUnit); + break 1; + } + } } - } - /** + /** * @depends testAdClientsList * @depends testAdUnitsList */ - public function testAdUnitsCustomChannelsList() - { - $adClients = $this->adsense->adclients->listAdclients(); - foreach ($adClients['items'] as $adClient) { - $adUnits = $this->adsense->adunits->listAdunits($adClient['id']); - if (array_key_exists('items', $adUnits)) { - foreach ($adUnits['items'] as $adUnit) { - $customChannels = - $this->adsense->adunits_customchannels->listAdunitsCustomchannels( - $adClient['id'], - $adUnit['id'] - ); - $this->checkCustomChannelsCollection($customChannels); - break 2; + public function testAdUnitsCustomChannelsList() + { + $adClients = $this->adsense->adclients->listAdclients(); + foreach ($adClients['items'] as $adClient) { + $adUnits = $this->adsense->adunits->listAdunits($adClient['id']); + if (array_key_exists('items', $adUnits)) { + foreach ($adUnits['items'] as $adUnit) { + $customChannels = + $this->adsense->adunits_customchannels->listAdunitsCustomchannels( + $adClient['id'], + $adUnit['id'] + ); + $this->checkCustomChannelsCollection($customChannels); + break 2; + } + } } - } } - } - /** + /** * @depends testAdClientsList */ - public function testCustomChannelsList() - { - $adClients = $this->adsense->adclients->listAdclients(); - foreach ($adClients['items'] as $adClient) { - $customChannels = - $this->adsense->customchannels->listCustomchannels($adClient['id']); - $this->checkCustomChannelsCollection($customChannels); + public function testCustomChannelsList() + { + $adClients = $this->adsense->adclients->listAdclients(); + foreach ($adClients['items'] as $adClient) { + $customChannels = + $this->adsense->customchannels->listCustomchannels($adClient['id']); + $this->checkCustomChannelsCollection($customChannels); + } } - } - /** + /** * @depends testAdClientsList */ - public function testCustomChannelsGet() - { - $adClients = $this->adsense->adclients->listAdclients(); - foreach ($adClients['items'] as $adClient) { - $customChannels = $this->adsense->customchannels->listCustomchannels($adClient['id']); - if (array_key_exists('items', $customChannels)) { - $customChannel = $this->getRandomElementFromArray($customChannels['items']); - $this->checkCustomChannelElement($customChannel); - break 1; - } + public function testCustomChannelsGet() + { + $adClients = $this->adsense->adclients->listAdclients(); + foreach ($adClients['items'] as $adClient) { + $customChannels = $this->adsense->customchannels->listCustomchannels($adClient['id']); + if (array_key_exists('items', $customChannels)) { + $customChannel = $this->getRandomElementFromArray($customChannels['items']); + $this->checkCustomChannelElement($customChannel); + break 1; + } + } } - } - /** + /** * @depends testAdClientsList * @depends testCustomChannelsList */ - public function testCustomChannelsAdUnitsList() - { - $adClients = $this->adsense->adclients->listAdclients(); - foreach ($adClients['items'] as $adClient) { - $customChannels = $this->adsense->customchannels->listCustomchannels($adClient['id']); - if (array_key_exists('items', $customChannels)) { - foreach ($customChannels['items'] as $customChannel) { - $adUnits = - $this->adsense->customchannels_adunits->listCustomchannelsAdunits( - $adClient['id'], - $customChannel['id'] - ); - $this->checkAdUnitsCollection($adUnits); - break 2; + public function testCustomChannelsAdUnitsList() + { + $adClients = $this->adsense->adclients->listAdclients(); + foreach ($adClients['items'] as $adClient) { + $customChannels = $this->adsense->customchannels->listCustomchannels($adClient['id']); + if (array_key_exists('items', $customChannels)) { + foreach ($customChannels['items'] as $customChannel) { + $adUnits = + $this->adsense->customchannels_adunits->listCustomchannelsAdunits( + $adClient['id'], + $customChannel['id'] + ); + $this->checkAdUnitsCollection($adUnits); + break 2; + } + } } - } } - } - /** + /** * @depends testAdClientsList */ - public function testUrlChannelsList() - { - $adClients = $this->adsense->adclients->listAdclients(); - foreach ($adClients['items'] as $adClient) { - $urlChannels = $this->adsense->urlchannels->listUrlchannels($adClient['id']); - $this->checkUrlChannelsCollection($urlChannels); + public function testUrlChannelsList() + { + $adClients = $this->adsense->adclients->listAdclients(); + foreach ($adClients['items'] as $adClient) { + $urlChannels = $this->adsense->urlchannels->listUrlchannels($adClient['id']); + $this->checkUrlChannelsCollection($urlChannels); + } } - } - public function testReportsGenerate() - { - if (!$this->checkToken()) { - return; + public function testReportsGenerate() + { + if (!$this->checkToken()) { + return; + } + $startDate = '2011-01-01'; + $endDate = '2011-01-31'; + $optParams = $this->getReportOptParams(); + $report = $this->adsense->reports->generate($startDate, $endDate, $optParams); + $this->checkReport($report); } - $startDate = '2011-01-01'; - $endDate = '2011-01-31'; - $optParams = $this->getReportOptParams(); - $report = $this->adsense->reports->generate($startDate, $endDate, $optParams); - $this->checkReport($report); - } - - private function checkAccountElement($account) - { - $this->assertArrayHasKey('kind', $account); - $this->assertArrayHasKey('id', $account); - $this->assertArrayHasKey('name', $account); - } - - private function checkAdClientsCollection($adClients) - { - $this->assertArrayHasKey('kind', $adClients); - $this->assertEquals($adClients['kind'], 'adsense#adClients'); - foreach ($adClients['items'] as $adClient) { - $this->assertArrayHasKey('id', $adClient); - $this->assertArrayHasKey('kind', $adClient); - $this->assertArrayHasKey('productCode', $adClient); - $this->assertArrayHasKey('supportsReporting', $adClient); + + private function checkAccountElement($account) + { + $this->assertArrayHasKey('kind', $account); + $this->assertArrayHasKey('id', $account); + $this->assertArrayHasKey('name', $account); } - } - - private function checkAdUnitsCollection($adUnits) - { - $this->assertArrayHasKey('kind', $adUnits); - $this->assertEquals($adUnits['kind'], 'adsense#adUnits'); - if (array_key_exists('items', $adUnits)) { - foreach ($adUnits['items'] as $adUnit) { - $this->checkAdUnitElement($adUnit); - } + + private function checkAdClientsCollection($adClients) + { + $this->assertArrayHasKey('kind', $adClients); + $this->assertEquals($adClients['kind'], 'adsense#adClients'); + foreach ($adClients['items'] as $adClient) { + $this->assertArrayHasKey('id', $adClient); + $this->assertArrayHasKey('kind', $adClient); + $this->assertArrayHasKey('productCode', $adClient); + $this->assertArrayHasKey('supportsReporting', $adClient); + } } - } - - private function checkAdUnitElement($adUnit) - { - $this->assertArrayHasKey('code', $adUnit); - $this->assertArrayHasKey('id', $adUnit); - $this->assertArrayHasKey('kind', $adUnit); - $this->assertArrayHasKey('name', $adUnit); - $this->assertArrayHasKey('status', $adUnit); - } - - private function checkCustomChannelsCollection($customChannels) - { - $this->assertArrayHasKey('kind', $customChannels); - $this->assertEquals($customChannels['kind'], 'adsense#customChannels'); - if (array_key_exists('items', $customChannels)) { - foreach ($customChannels['items'] as $customChannel) { - $this->checkCustomChannelElement($customChannel); - } + + private function checkAdUnitsCollection($adUnits) + { + $this->assertArrayHasKey('kind', $adUnits); + $this->assertEquals($adUnits['kind'], 'adsense#adUnits'); + if (array_key_exists('items', $adUnits)) { + foreach ($adUnits['items'] as $adUnit) { + $this->checkAdUnitElement($adUnit); + } + } } - } - - private function checkCustomChannelElement($customChannel) - { - $this->assertArrayHasKey('kind', $customChannel); - $this->assertArrayHasKey('id', $customChannel); - $this->assertArrayHasKey('code', $customChannel); - $this->assertArrayHasKey('name', $customChannel); - } - - private function checkUrlChannelsCollection($urlChannels) - { - $this->assertArrayHasKey('kind', $urlChannels); - $this->assertEquals($urlChannels['kind'], 'adsense#urlChannels'); - if (array_key_exists('items', $urlChannels)) { - foreach ($urlChannels['items'] as $urlChannel) { - $this->assertArrayHasKey('kind', $urlChannel); - $this->assertArrayHasKey('id', $urlChannel); - $this->assertArrayHasKey('urlPattern', $urlChannel); - } + + private function checkAdUnitElement($adUnit) + { + $this->assertArrayHasKey('code', $adUnit); + $this->assertArrayHasKey('id', $adUnit); + $this->assertArrayHasKey('kind', $adUnit); + $this->assertArrayHasKey('name', $adUnit); + $this->assertArrayHasKey('status', $adUnit); + } + + private function checkCustomChannelsCollection($customChannels) + { + $this->assertArrayHasKey('kind', $customChannels); + $this->assertEquals($customChannels['kind'], 'adsense#customChannels'); + if (array_key_exists('items', $customChannels)) { + foreach ($customChannels['items'] as $customChannel) { + $this->checkCustomChannelElement($customChannel); + } + } } - } - - private function getReportOptParams() - { - return array( - 'metric' => array('PAGE_VIEWS', 'AD_REQUESTS'), - 'dimension' => array ('DATE', 'AD_CLIENT_ID'), - 'sort' => array('DATE'), - 'filter' => array('COUNTRY_NAME==United States'), - ); - } - - private function checkReport($report) - { - $this->assertArrayHasKey('kind', $report); - $this->assertEquals($report['kind'], 'adsense#report'); - $this->assertArrayHasKey('totalMatchedRows', $report); - $this->assertGreaterThan(0, count($report->headers)); - foreach ($report['headers'] as $header) { - $this->assertArrayHasKey('name', $header); - $this->assertArrayHasKey('type', $header); + + private function checkCustomChannelElement($customChannel) + { + $this->assertArrayHasKey('kind', $customChannel); + $this->assertArrayHasKey('id', $customChannel); + $this->assertArrayHasKey('code', $customChannel); + $this->assertArrayHasKey('name', $customChannel); + } + + private function checkUrlChannelsCollection($urlChannels) + { + $this->assertArrayHasKey('kind', $urlChannels); + $this->assertEquals($urlChannels['kind'], 'adsense#urlChannels'); + if (array_key_exists('items', $urlChannels)) { + foreach ($urlChannels['items'] as $urlChannel) { + $this->assertArrayHasKey('kind', $urlChannel); + $this->assertArrayHasKey('id', $urlChannel); + $this->assertArrayHasKey('urlPattern', $urlChannel); + } + } } - if (array_key_exists('items', $report)) { - foreach ($report['items'] as $row) { - $this->assertCount(4, $row); - } + + private function getReportOptParams() + { + return array( + 'metric' => array('PAGE_VIEWS', 'AD_REQUESTS'), + 'dimension' => array ('DATE', 'AD_CLIENT_ID'), + 'sort' => array('DATE'), + 'filter' => array('COUNTRY_NAME==United States'), + ); + } + + private function checkReport($report) + { + $this->assertArrayHasKey('kind', $report); + $this->assertEquals($report['kind'], 'adsense#report'); + $this->assertArrayHasKey('totalMatchedRows', $report); + $this->assertGreaterThan(0, count($report->headers)); + foreach ($report['headers'] as $header) { + $this->assertArrayHasKey('name', $header); + $this->assertArrayHasKey('type', $header); + } + if (array_key_exists('items', $report)) { + foreach ($report['items'] as $row) { + $this->assertCount(4, $row); + } + } + $this->assertArrayHasKey('totals', $report); + $this->assertArrayHasKey('averages', $report); + } + + private function getRandomElementFromArray($array) + { + $elementKey = array_rand($array); + return $array[$elementKey]; } - $this->assertArrayHasKey('totals', $report); - $this->assertArrayHasKey('averages', $report); - } - - private function getRandomElementFromArray($array) - { - $elementKey = array_rand($array); - return $array[$elementKey]; - } } diff --git a/tests/Google/Service/ResourceTest.php b/tests/Google/Service/ResourceTest.php index 312db72a3..51e7e25cd 100644 --- a/tests/Google/Service/ResourceTest.php +++ b/tests/Google/Service/ResourceTest.php @@ -37,437 +37,437 @@ class TestService extends \Google\Service { - public function __construct(Client $client) - { - parent::__construct($client); - $this->rootUrl = "https://test.example.com/"; - $this->servicePath = ""; - $this->version = "v1beta1"; - $this->serviceName = "test"; - } + public function __construct(Client $client) + { + parent::__construct($client); + $this->rootUrl = "https://test.example.com/"; + $this->servicePath = ""; + $this->version = "v1beta1"; + $this->serviceName = "test"; + } } class ResourceTest extends BaseTest { - private $client; - private $service; - - public function set_up() - { - $this->client = $this->prophesize(Client::class); - - $logger = $this->prophesize("Monolog\Logger"); - - $this->client->getLogger()->willReturn($logger->reveal()); - $this->client->shouldDefer()->willReturn(true); - $this->client->getHttpClient()->willReturn(new GuzzleClient()); - - $this->service = new TestService($this->client->reveal()); - } - - public function testCallFailure() - { - $this->expectException(GoogleException::class); - $this->expectExceptionMessage('Unknown function: test->testResource->someothermethod()'); - $resource = new GoogleResource( - $this->service, - "test", - "testResource", - array("methods" => - array( - "testMethod" => array( - "parameters" => array(), - "path" => "method/path", - "httpMethod" => "POST", - ) - ) - ) - ); - $resource->call("someothermethod", array()); - } - - public function testCall() - { - $resource = new GoogleResource( - $this->service, - "test", - "testResource", - array("methods" => - array( - "testMethod" => array( - "parameters" => array(), - "path" => "method/path", - "httpMethod" => "POST", - ) - ) - ) - ); - $request = $resource->call("testMethod", array(array())); - $this->assertEquals("https://test.example.com/method/path", (string) $request->getUri()); - $this->assertEquals("POST", $request->getMethod()); - } - - public function testCallServiceDefinedRoot() - { - $this->service->rootUrl = "https://sample.example.com/"; - $resource = new GoogleResource( - $this->service, - "test", - "testResource", - array("methods" => - array( - "testMethod" => array( - "parameters" => array(), - "path" => "method/path", - "httpMethod" => "POST", - ) - ) - ) - ); - $request = $resource->call("testMethod", array(array())); - $this->assertEquals("https://sample.example.com/method/path", (string) $request->getUri()); - $this->assertEquals("POST", $request->getMethod()); - } - - /** + private $client; + private $service; + + public function set_up() + { + $this->client = $this->prophesize(Client::class); + + $logger = $this->prophesize("Monolog\Logger"); + + $this->client->getLogger()->willReturn($logger->reveal()); + $this->client->shouldDefer()->willReturn(true); + $this->client->getHttpClient()->willReturn(new GuzzleClient()); + + $this->service = new TestService($this->client->reveal()); + } + + public function testCallFailure() + { + $this->expectException(GoogleException::class); + $this->expectExceptionMessage('Unknown function: test->testResource->someothermethod()'); + $resource = new GoogleResource( + $this->service, + "test", + "testResource", + array( + "methods" => array( + "testMethod" => array( + "parameters" => array(), + "path" => "method/path", + "httpMethod" => "POST", + ) + ) + ) + ); + $resource->call("someothermethod", array()); + } + + public function testCall() + { + $resource = new GoogleResource( + $this->service, + "test", + "testResource", + array( + "methods" => array( + "testMethod" => array( + "parameters" => array(), + "path" => "method/path", + "httpMethod" => "POST", + ) + ) + ) + ); + $request = $resource->call("testMethod", array(array())); + $this->assertEquals("https://test.example.com/method/path", (string) $request->getUri()); + $this->assertEquals("POST", $request->getMethod()); + } + + public function testCallServiceDefinedRoot() + { + $this->service->rootUrl = "https://sample.example.com/"; + $resource = new GoogleResource( + $this->service, + "test", + "testResource", + array( + "methods" => array( + "testMethod" => array( + "parameters" => array(), + "path" => "method/path", + "httpMethod" => "POST", + ) + ) + ) + ); + $request = $resource->call("testMethod", array(array())); + $this->assertEquals("https://sample.example.com/method/path", (string) $request->getUri()); + $this->assertEquals("POST", $request->getMethod()); + } + + /** * Some Google Service (Google\Service\Directory\Resource\Channels and * Google\Service\Reports\Resource\Channels) use a different servicePath value * that should override the default servicePath value, it's represented by a / * before the resource path. All other Services have no / before the path */ - public function testCreateRequestUriForASelfDefinedServicePath() - { - $this->service->servicePath = '/admin/directory/v1/'; - $resource = new GoogleResource( - $this->service, - 'test', - 'testResource', - array("methods" => - array( - 'testMethod' => array( - 'parameters' => array(), - 'path' => '/admin/directory_v1/watch/stop', - 'httpMethod' => 'POST', - ) - ) - ) - ); - $request = $resource->call('testMethod', array(array())); - $this->assertEquals('https://test.example.com/admin/directory_v1/watch/stop', (string) $request->getUri()); - } - - public function testCreateRequestUri() - { - $restPath = "plus/{u}"; - $service = new GoogleService($this->client->reveal()); - $service->servicePath = "http://localhost/"; - $resource = new GoogleResource($service, 'test', 'testResource', array()); - - // Test Path - $params = array(); - $params['u']['type'] = 'string'; - $params['u']['location'] = 'path'; - $params['u']['value'] = 'me'; - $value = $resource->createRequestUri($restPath, $params); - $this->assertEquals("http://localhost/plus/me", $value); - - // Test Query - $params = array(); - $params['u']['type'] = 'string'; - $params['u']['location'] = 'query'; - $params['u']['value'] = 'me'; - $value = $resource->createRequestUri('plus', $params); - $this->assertEquals("http://localhost/plus?u=me", $value); - - // Test Booleans - $params = array(); - $params['u']['type'] = 'boolean'; - $params['u']['location'] = 'path'; - $params['u']['value'] = '1'; - $value = $resource->createRequestUri($restPath, $params); - $this->assertEquals("http://localhost/plus/true", $value); - - $params['u']['location'] = 'query'; - $value = $resource->createRequestUri('plus', $params); - $this->assertEquals("http://localhost/plus?u=true", $value); - - // Test encoding - $params = array(); - $params['u']['type'] = 'string'; - $params['u']['location'] = 'query'; - $params['u']['value'] = '@me/'; - $value = $resource->createRequestUri('plus', $params); - $this->assertEquals("http://localhost/plus?u=%40me%2F", $value); - } - - public function testNoExpectedClassForAltMediaWithHttpSuccess() - { - // set the "alt" parameter to "media" - $arguments = [['alt' => 'media']]; - $request = new Request('GET', '/?alt=media'); - - $http = $this->prophesize("GuzzleHttp\Client"); - - if ($this->isGuzzle5()) { - $body = Guzzle5Stream::factory('thisisnotvalidjson'); - $response = new Guzzle5Response(200, [], $body); - - $http->createRequest(Argument::any(), Argument::any(), Argument::any()) - ->willReturn(new \GuzzleHttp\Message\Request('GET', '/?alt=media')); - - $http->send(Argument::type('GuzzleHttp\Message\Request')) - ->shouldBeCalledTimes(1) - ->willReturn($response); - } else { - $body = Psr7\Utils::streamFor('thisisnotvalidjson'); - $response = new Response(200, [], $body); - - $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) - ->shouldBeCalledTimes(1) - ->willReturn($response); + public function testCreateRequestUriForASelfDefinedServicePath() + { + $this->service->servicePath = '/admin/directory/v1/'; + $resource = new GoogleResource( + $this->service, + 'test', + 'testResource', + array( + "methods" => array( + 'testMethod' => array( + 'parameters' => array(), + 'path' => '/admin/directory_v1/watch/stop', + 'httpMethod' => 'POST', + ) + ) + ) + ); + $request = $resource->call('testMethod', array(array())); + $this->assertEquals('https://test.example.com/admin/directory_v1/watch/stop', (string) $request->getUri()); } - $client = new Client(); - $client->setHttpClient($http->reveal()); - $service = new TestService($client); - - // set up mock objects - $resource = new GoogleResource( - $service, - "test", - "testResource", - array("methods" => - array( - "testMethod" => array( - "parameters" => array(), - "path" => "method/path", - "httpMethod" => "POST", - ) - ) - ) - ); - - $expectedClass = 'ThisShouldBeIgnored'; - $response = $resource->call('testMethod', $arguments, $expectedClass); - $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response); - $this->assertEquals('thisisnotvalidjson', (string) $response->getBody()); - } - - public function testNoExpectedClassForAltMediaWithHttpFail() - { - // set the "alt" parameter to "media" - $arguments = [['alt' => 'media']]; - $request = new Request('GET', '/?alt=media'); - - $http = $this->prophesize("GuzzleHttp\Client"); - - if ($this->isGuzzle5()) { - $body = Guzzle5Stream::factory('thisisnotvalidjson'); - $response = new Guzzle5Response(400, [], $body); - - $http->createRequest(Argument::any(), Argument::any(), Argument::any()) - ->willReturn(new \GuzzleHttp\Message\Request('GET', '/?alt=media')); - - $http->send(Argument::type('GuzzleHttp\Message\Request')) - ->shouldBeCalledTimes(1) - ->willReturn($response); - } else { - $body = Psr7\Utils::streamFor('thisisnotvalidjson'); - $response = new Response(400, [], $body); - - $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) - ->shouldBeCalledTimes(1) - ->willReturn($response); + public function testCreateRequestUri() + { + $restPath = "plus/{u}"; + $service = new GoogleService($this->client->reveal()); + $service->servicePath = "http://localhost/"; + $resource = new GoogleResource($service, 'test', 'testResource', array()); + + // Test Path + $params = array(); + $params['u']['type'] = 'string'; + $params['u']['location'] = 'path'; + $params['u']['value'] = 'me'; + $value = $resource->createRequestUri($restPath, $params); + $this->assertEquals("http://localhost/plus/me", $value); + + // Test Query + $params = array(); + $params['u']['type'] = 'string'; + $params['u']['location'] = 'query'; + $params['u']['value'] = 'me'; + $value = $resource->createRequestUri('plus', $params); + $this->assertEquals("http://localhost/plus?u=me", $value); + + // Test Booleans + $params = array(); + $params['u']['type'] = 'boolean'; + $params['u']['location'] = 'path'; + $params['u']['value'] = '1'; + $value = $resource->createRequestUri($restPath, $params); + $this->assertEquals("http://localhost/plus/true", $value); + + $params['u']['location'] = 'query'; + $value = $resource->createRequestUri('plus', $params); + $this->assertEquals("http://localhost/plus?u=true", $value); + + // Test encoding + $params = array(); + $params['u']['type'] = 'string'; + $params['u']['location'] = 'query'; + $params['u']['value'] = '@me/'; + $value = $resource->createRequestUri('plus', $params); + $this->assertEquals("http://localhost/plus?u=%40me%2F", $value); } - $client = new Client(); - $client->setHttpClient($http->reveal()); - $service = new TestService($client); - - // set up mock objects - $resource = new GoogleResource( - $service, - "test", - "testResource", - array("methods" => - array( - "testMethod" => array( - "parameters" => array(), - "path" => "method/path", - "httpMethod" => "POST", - ) - ) - ) - ); - - try { - $expectedClass = 'ThisShouldBeIgnored'; - $decoded = $resource->call('testMethod', $arguments, $expectedClass); - $this->fail('should have thrown exception'); - } catch (ServiceException $e) { - // Alt Media on error should return a safe error - $this->assertEquals('thisisnotvalidjson', $e->getMessage()); + public function testNoExpectedClassForAltMediaWithHttpSuccess() + { + // set the "alt" parameter to "media" + $arguments = [['alt' => 'media']]; + $request = new Request('GET', '/?alt=media'); + + $http = $this->prophesize("GuzzleHttp\Client"); + + if ($this->isGuzzle5()) { + $body = Guzzle5Stream::factory('thisisnotvalidjson'); + $response = new Guzzle5Response(200, [], $body); + + $http->createRequest(Argument::any(), Argument::any(), Argument::any()) + ->willReturn(new \GuzzleHttp\Message\Request('GET', '/?alt=media')); + + $http->send(Argument::type('GuzzleHttp\Message\Request')) + ->shouldBeCalledTimes(1) + ->willReturn($response); + } else { + $body = Psr7\Utils::streamFor('thisisnotvalidjson'); + $response = new Response(200, [], $body); + + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response); + } + + $client = new Client(); + $client->setHttpClient($http->reveal()); + $service = new TestService($client); + + // set up mock objects + $resource = new GoogleResource( + $service, + "test", + "testResource", + array( + "methods" => array( + "testMethod" => array( + "parameters" => array(), + "path" => "method/path", + "httpMethod" => "POST", + ) + ) + ) + ); + + $expectedClass = 'ThisShouldBeIgnored'; + $response = $resource->call('testMethod', $arguments, $expectedClass); + $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response); + $this->assertEquals('thisisnotvalidjson', (string) $response->getBody()); } - } - - public function testErrorResponseWithVeryLongBody() - { - // set the "alt" parameter to "media" - $arguments = [['alt' => 'media']]; - $request = new Request('GET', '/?alt=media'); - - $http = $this->prophesize("GuzzleHttp\Client"); - if ($this->isGuzzle5()) { - $body = Guzzle5Stream::factory('this will be pulled into memory'); - $response = new Guzzle5Response(400, [], $body); - - $http->createRequest(Argument::any(), Argument::any(), Argument::any()) - ->willReturn(new \GuzzleHttp\Message\Request('GET', '/?alt=media')); - - $http->send(Argument::type('GuzzleHttp\Message\Request')) - ->shouldBeCalledTimes(1) - ->willReturn($response); - } else { - $body = Psr7\Utils::streamFor('this will be pulled into memory'); - $response = new Response(400, [], $body); - - $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) - ->shouldBeCalledTimes(1) - ->willReturn($response); + public function testNoExpectedClassForAltMediaWithHttpFail() + { + // set the "alt" parameter to "media" + $arguments = [['alt' => 'media']]; + $request = new Request('GET', '/?alt=media'); + + $http = $this->prophesize("GuzzleHttp\Client"); + + if ($this->isGuzzle5()) { + $body = Guzzle5Stream::factory('thisisnotvalidjson'); + $response = new Guzzle5Response(400, [], $body); + + $http->createRequest(Argument::any(), Argument::any(), Argument::any()) + ->willReturn(new \GuzzleHttp\Message\Request('GET', '/?alt=media')); + + $http->send(Argument::type('GuzzleHttp\Message\Request')) + ->shouldBeCalledTimes(1) + ->willReturn($response); + } else { + $body = Psr7\Utils::streamFor('thisisnotvalidjson'); + $response = new Response(400, [], $body); + + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response); + } + + $client = new Client(); + $client->setHttpClient($http->reveal()); + $service = new TestService($client); + + // set up mock objects + $resource = new GoogleResource( + $service, + "test", + "testResource", + array( + "methods" => array( + "testMethod" => array( + "parameters" => array(), + "path" => "method/path", + "httpMethod" => "POST", + ) + ) + ) + ); + + try { + $expectedClass = 'ThisShouldBeIgnored'; + $decoded = $resource->call('testMethod', $arguments, $expectedClass); + $this->fail('should have thrown exception'); + } catch (ServiceException $e) { + // Alt Media on error should return a safe error + $this->assertEquals('thisisnotvalidjson', $e->getMessage()); + } } - $client = new Client(); - $client->setHttpClient($http->reveal()); - $service = new TestService($client); - - // set up mock objects - $resource = new GoogleResource( - $service, - "test", - "testResource", - array("methods" => - array( - "testMethod" => array( - "parameters" => array(), - "path" => "method/path", - "httpMethod" => "POST", - ) - ) - ) - ); - - try { - $expectedClass = 'ThisShouldBeIgnored'; - $decoded = $resource->call('testMethod', $arguments, $expectedClass); - $this->fail('should have thrown exception'); - } catch (ServiceException $e) { - // empty message - alt=media means no message - $this->assertEquals('this will be pulled into memory', $e->getMessage()); + public function testErrorResponseWithVeryLongBody() + { + // set the "alt" parameter to "media" + $arguments = [['alt' => 'media']]; + $request = new Request('GET', '/?alt=media'); + + $http = $this->prophesize("GuzzleHttp\Client"); + + if ($this->isGuzzle5()) { + $body = Guzzle5Stream::factory('this will be pulled into memory'); + $response = new Guzzle5Response(400, [], $body); + + $http->createRequest(Argument::any(), Argument::any(), Argument::any()) + ->willReturn(new \GuzzleHttp\Message\Request('GET', '/?alt=media')); + + $http->send(Argument::type('GuzzleHttp\Message\Request')) + ->shouldBeCalledTimes(1) + ->willReturn($response); + } else { + $body = Psr7\Utils::streamFor('this will be pulled into memory'); + $response = new Response(400, [], $body); + + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response); + } + + $client = new Client(); + $client->setHttpClient($http->reveal()); + $service = new TestService($client); + + // set up mock objects + $resource = new GoogleResource( + $service, + "test", + "testResource", + array( + "methods" => array( + "testMethod" => array( + "parameters" => array(), + "path" => "method/path", + "httpMethod" => "POST", + ) + ) + ) + ); + + try { + $expectedClass = 'ThisShouldBeIgnored'; + $decoded = $resource->call('testMethod', $arguments, $expectedClass); + $this->fail('should have thrown exception'); + } catch (ServiceException $e) { + // empty message - alt=media means no message + $this->assertEquals('this will be pulled into memory', $e->getMessage()); + } } - } - - public function testSuccessResponseWithVeryLongBody() - { - $this->onlyGuzzle6Or7(); - - // set the "alt" parameter to "media" - $arguments = [['alt' => 'media']]; - $stream = $this->prophesize(Stream::class); - $stream->__toString() - ->shouldNotBeCalled(); - $response = new Response(200, [], $stream->reveal()); - - $http = $this->prophesize("GuzzleHttp\Client"); - $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) - ->shouldBeCalledTimes(1) - ->willReturn($response); - - $client = new Client(); - $client->setHttpClient($http->reveal()); - $service = new TestService($client); - - // set up mock objects - $resource = new GoogleResource( - $service, - "test", - "testResource", - array("methods" => - array( - "testMethod" => array( - "parameters" => array(), - "path" => "method/path", - "httpMethod" => "POST", - ) - ) - ) - ); - - $expectedClass = 'ThisShouldBeIgnored'; - $response = $resource->call('testMethod', $arguments, $expectedClass); - - $this->assertEquals(200, $response->getStatusCode()); - // $this->assertFalse($stream->toStringCalled); - } - - public function testExceptionMessage() - { - // set the "alt" parameter to "media" - $request = new Request('GET', '/'); - $errors = [ ["domain" => "foo"] ]; - $content = json_encode([ - 'error' => [ - 'errors' => $errors - ] - ]); - - $http = $this->prophesize("GuzzleHttp\Client"); - - if ($this->isGuzzle5()) { - $body = Guzzle5Stream::factory($content); - $response = new Guzzle5Response(400, [], $body); - - $http->createRequest(Argument::any(), Argument::any(), Argument::any()) - ->willReturn(new \GuzzleHttp\Message\Request('GET', '/?alt=media')); - - $http->send(Argument::type('GuzzleHttp\Message\Request')) - ->shouldBeCalledTimes(1) - ->willReturn($response); - } else { - $body = Psr7\Utils::streamFor($content); - $response = new Response(400, [], $body); - - $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) - ->shouldBeCalledTimes(1) - ->willReturn($response); + + public function testSuccessResponseWithVeryLongBody() + { + $this->onlyGuzzle6Or7(); + + // set the "alt" parameter to "media" + $arguments = [['alt' => 'media']]; + $stream = $this->prophesize(Stream::class); + $stream->__toString() + ->shouldNotBeCalled(); + $response = new Response(200, [], $stream->reveal()); + + $http = $this->prophesize("GuzzleHttp\Client"); + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response); + + $client = new Client(); + $client->setHttpClient($http->reveal()); + $service = new TestService($client); + + // set up mock objects + $resource = new GoogleResource( + $service, + "test", + "testResource", + array( + "methods" => array( + "testMethod" => array( + "parameters" => array(), + "path" => "method/path", + "httpMethod" => "POST", + ) + ) + ) + ); + + $expectedClass = 'ThisShouldBeIgnored'; + $response = $resource->call('testMethod', $arguments, $expectedClass); + + $this->assertEquals(200, $response->getStatusCode()); + // $this->assertFalse($stream->toStringCalled); } - $client = new Client(); - $client->setHttpClient($http->reveal()); - $service = new TestService($client); - - // set up mock objects - $resource = new GoogleResource( - $service, - "test", - "testResource", - array("methods" => - array( - "testMethod" => array( - "parameters" => array(), - "path" => "method/path", - "httpMethod" => "POST", - ) - ) - ) - ); - - try { - - $decoded = $resource->call('testMethod', array(array())); - $this->fail('should have thrown exception'); - } catch (ServiceException $e) { - $this->assertEquals($errors, $e->getErrors()); + public function testExceptionMessage() + { + // set the "alt" parameter to "media" + $request = new Request('GET', '/'); + $errors = [ ["domain" => "foo"] ]; + $content = json_encode([ + 'error' => [ + 'errors' => $errors + ] + ]); + + $http = $this->prophesize("GuzzleHttp\Client"); + + if ($this->isGuzzle5()) { + $body = Guzzle5Stream::factory($content); + $response = new Guzzle5Response(400, [], $body); + + $http->createRequest(Argument::any(), Argument::any(), Argument::any()) + ->willReturn(new \GuzzleHttp\Message\Request('GET', '/?alt=media')); + + $http->send(Argument::type('GuzzleHttp\Message\Request')) + ->shouldBeCalledTimes(1) + ->willReturn($response); + } else { + $body = Psr7\Utils::streamFor($content); + $response = new Response(400, [], $body); + + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response); + } + + $client = new Client(); + $client->setHttpClient($http->reveal()); + $service = new TestService($client); + + // set up mock objects + $resource = new GoogleResource( + $service, + "test", + "testResource", + array( + "methods" => array( + "testMethod" => array( + "parameters" => array(), + "path" => "method/path", + "httpMethod" => "POST", + ) + ) + ) + ); + + try { + + $decoded = $resource->call('testMethod', array(array())); + $this->fail('should have thrown exception'); + } catch (ServiceException $e) { + $this->assertEquals($errors, $e->getErrors()); + } } - } } diff --git a/tests/Google/Service/TasksTest.php b/tests/Google/Service/TasksTest.php index d34eb55f1..341b0e7ff 100644 --- a/tests/Google/Service/TasksTest.php +++ b/tests/Google/Service/TasksTest.php @@ -22,74 +22,74 @@ class TasksTest extends BaseTest { - /** @var Tasks */ - public $taskService; + /** @var Tasks */ + public $taskService; - public function set_up() - { - $this->checkToken(); - $this->taskService = new Tasks($this->getClient()); - } + public function set_up() + { + $this->checkToken(); + $this->taskService = new Tasks($this->getClient()); + } - public function testInsertTask() - { - $list = $this->createTaskList('List: ' . __METHOD__); - $task = $this->createTask('Task: '.__METHOD__, $list->id); - $this->assertIsTask($task); - } + public function testInsertTask() + { + $list = $this->createTaskList('List: ' . __METHOD__); + $task = $this->createTask('Task: '.__METHOD__, $list->id); + $this->assertIsTask($task); + } - /** - * @depends testInsertTask - */ - public function testGetTask() - { - $tasks = $this->taskService->tasks; - $list = $this->createTaskList('List: ' . __METHOD__); - $task = $this->createTask('Task: '. __METHOD__, $list['id']); + /** + * @depends testInsertTask + */ + public function testGetTask() + { + $tasks = $this->taskService->tasks; + $list = $this->createTaskList('List: ' . __METHOD__); + $task = $this->createTask('Task: '. __METHOD__, $list['id']); - $task = $tasks->get($list['id'], $task['id']); - $this->assertIsTask($task); - } + $task = $tasks->get($list['id'], $task['id']); + $this->assertIsTask($task); + } - /** - * @depends testInsertTask - */ - public function testListTask() - { - $tasks = $this->taskService->tasks; - $list = $this->createTaskList('List: ' . __METHOD__); + /** + * @depends testInsertTask + */ + public function testListTask() + { + $tasks = $this->taskService->tasks; + $list = $this->createTaskList('List: ' . __METHOD__); - for ($i=0; $i<4; $i++) { - $this->createTask("Task: $i ".__METHOD__, $list['id']); - } + for ($i=0; $i<4; $i++) { + $this->createTask("Task: $i ".__METHOD__, $list['id']); + } - $tasksArray = $tasks->listTasks($list['id']); - $this->assertGreaterThan(1, count($tasksArray)); - foreach ($tasksArray['items'] as $task) { - $this->assertIsTask($task); + $tasksArray = $tasks->listTasks($list['id']); + $this->assertGreaterThan(1, count($tasksArray)); + foreach ($tasksArray['items'] as $task) { + $this->assertIsTask($task); + } } - } - private function createTaskList($name) - { - $list = new Tasks\TaskList(); - $list->title = $name; - return $this->taskService->tasklists->insert($list); - } + private function createTaskList($name) + { + $list = new Tasks\TaskList(); + $list->title = $name; + return $this->taskService->tasklists->insert($list); + } - private function createTask($title, $listId) - { - $tasks = $this->taskService->tasks; - $task = new Tasks\Task(); - $task->title = $title; - return $tasks->insert($listId, $task); - } + private function createTask($title, $listId) + { + $tasks = $this->taskService->tasks; + $task = new Tasks\Task(); + $task->title = $title; + return $tasks->insert($listId, $task); + } - private function assertIsTask($task) - { - $this->assertArrayHasKey('title', $task); - $this->assertArrayHasKey('kind', $task); - $this->assertArrayHasKey('id', $task); - $this->assertArrayHasKey('position', $task); - } + private function assertIsTask($task) + { + $this->assertArrayHasKey('title', $task); + $this->assertArrayHasKey('kind', $task); + $this->assertArrayHasKey('id', $task); + $this->assertArrayHasKey('position', $task); + } } diff --git a/tests/Google/Service/YouTubeTest.php b/tests/Google/Service/YouTubeTest.php index 89d5520df..8cc0b34f9 100644 --- a/tests/Google/Service/YouTubeTest.php +++ b/tests/Google/Service/YouTubeTest.php @@ -22,62 +22,62 @@ class YouTubeTest extends BaseTest { - /** @var YouTube */ - public $youtube; - public function set_up() - { - $this->checkToken(); - $this->youtube = new YouTube($this->getClient()); - } + /** @var YouTube */ + public $youtube; + public function set_up() + { + $this->checkToken(); + $this->youtube = new YouTube($this->getClient()); + } - public function testMissingFieldsAreNull() - { - $parts = "id,brandingSettings"; - $opts = array("mine" => true); - $channels = $this->youtube->channels->listChannels($parts, $opts); + public function testMissingFieldsAreNull() + { + $parts = "id,brandingSettings"; + $opts = array("mine" => true); + $channels = $this->youtube->channels->listChannels($parts, $opts); - $newChannel = new YouTube\Channel(); - $newChannel->setId( $channels[0]->getId()); - $newChannel->setBrandingSettings($channels[0]->getBrandingSettings()); + $newChannel = new YouTube\Channel(); + $newChannel->setId($channels[0]->getId()); + $newChannel->setBrandingSettings($channels[0]->getBrandingSettings()); - $simpleOriginal = $channels[0]->toSimpleObject(); - $simpleNew = $newChannel->toSimpleObject(); + $simpleOriginal = $channels[0]->toSimpleObject(); + $simpleNew = $newChannel->toSimpleObject(); - $this->assertObjectHasAttribute('etag', $simpleOriginal); - $this->assertObjectNotHasAttribute('etag', $simpleNew); + $this->assertObjectHasAttribute('etag', $simpleOriginal); + $this->assertObjectNotHasAttribute('etag', $simpleNew); - $owner_details = new YouTube\ChannelContentOwnerDetails(); - $owner_details->setTimeLinked("123456789"); - $o_channel = new YouTube\Channel(); - $o_channel->setContentOwnerDetails($owner_details); - $simpleManual = $o_channel->toSimpleObject(); - $this->assertObjectHasAttribute('timeLinked', $simpleManual->contentOwnerDetails); - $this->assertObjectNotHasAttribute('contentOwner', $simpleManual->contentOwnerDetails); + $owner_details = new YouTube\ChannelContentOwnerDetails(); + $owner_details->setTimeLinked("123456789"); + $o_channel = new YouTube\Channel(); + $o_channel->setContentOwnerDetails($owner_details); + $simpleManual = $o_channel->toSimpleObject(); + $this->assertObjectHasAttribute('timeLinked', $simpleManual->contentOwnerDetails); + $this->assertObjectNotHasAttribute('contentOwner', $simpleManual->contentOwnerDetails); - $owner_details = new YouTube\ChannelContentOwnerDetails(); - $owner_details->timeLinked = "123456789"; - $o_channel = new YouTube\Channel(); - $o_channel->setContentOwnerDetails($owner_details); - $simpleManual = $o_channel->toSimpleObject(); + $owner_details = new YouTube\ChannelContentOwnerDetails(); + $owner_details->timeLinked = "123456789"; + $o_channel = new YouTube\Channel(); + $o_channel->setContentOwnerDetails($owner_details); + $simpleManual = $o_channel->toSimpleObject(); - $this->assertObjectHasAttribute('timeLinked', $simpleManual->contentOwnerDetails); - $this->assertObjectNotHasAttribute('contentOwner', $simpleManual->contentOwnerDetails); + $this->assertObjectHasAttribute('timeLinked', $simpleManual->contentOwnerDetails); + $this->assertObjectNotHasAttribute('contentOwner', $simpleManual->contentOwnerDetails); - $owner_details = new YouTube\ChannelContentOwnerDetails(); - $owner_details['timeLinked'] = "123456789"; - $o_channel = new YouTube\Channel(); - $o_channel->setContentOwnerDetails($owner_details); - $simpleManual = $o_channel->toSimpleObject(); + $owner_details = new YouTube\ChannelContentOwnerDetails(); + $owner_details['timeLinked'] = "123456789"; + $o_channel = new YouTube\Channel(); + $o_channel->setContentOwnerDetails($owner_details); + $simpleManual = $o_channel->toSimpleObject(); - $this->assertObjectHasAttribute('timeLinked', $simpleManual->contentOwnerDetails); - $this->assertObjectNotHasAttribute('contentOwner', $simpleManual->contentOwnerDetails); + $this->assertObjectHasAttribute('timeLinked', $simpleManual->contentOwnerDetails); + $this->assertObjectNotHasAttribute('contentOwner', $simpleManual->contentOwnerDetails); - $ping = new YouTube\ChannelConversionPing(); - $ping->setContext("hello"); - $pings = new YouTube\ChannelConversionPings(); - $pings->setPings(array($ping)); - $simplePings = $pings->toSimpleObject(); - $this->assertObjectHasAttribute('context', $simplePings->pings[0]); - $this->assertObjectNotHasAttribute('conversionUrl', $simplePings->pings[0]); - } + $ping = new YouTube\ChannelConversionPing(); + $ping->setContext("hello"); + $pings = new YouTube\ChannelConversionPings(); + $pings->setPings(array($ping)); + $simplePings = $pings->toSimpleObject(); + $this->assertObjectHasAttribute('context', $simplePings->pings[0]); + $this->assertObjectNotHasAttribute('conversionUrl', $simplePings->pings[0]); + } } diff --git a/tests/Google/ServiceTest.php b/tests/Google/ServiceTest.php index 28a51325c..07da91a3f 100644 --- a/tests/Google/ServiceTest.php +++ b/tests/Google/ServiceTest.php @@ -31,156 +31,155 @@ class TestModel extends Model { - public function mapTypes($array) - { - return parent::mapTypes($array); - } - - public function isAssociativeArray($array) - { - return parent::isAssociativeArray($array); - } + public function mapTypes($array) + { + return parent::mapTypes($array); + } + + public function isAssociativeArray($array) + { + return parent::isAssociativeArray($array); + } } class TestService extends Service { - public $batchPath = 'batch/test'; + public $batchPath = 'batch/test'; } if (trait_exists('\Prophecy\PhpUnit\ProphecyTrait')) { - trait ServiceTestTrait + trait ServiceTestTrait { - use \Prophecy\PhpUnit\ProphecyTrait; - } + use \Prophecy\PhpUnit\ProphecyTrait; + } } else { - trait ServiceTestTrait + trait ServiceTestTrait { - } + } } class ServiceTest extends TestCase { - private static $errorMessage; - - use ServiceTestTrait; - - public function testCreateBatch() - { - $response = $this->prophesize(ResponseInterface::class); - $client = $this->prophesize(Client::class); - - $client->execute( - Argument::allOf( - Argument::type(RequestInterface::class), - Argument::that( - function ($request) { - $this->assertEquals('/batch/test', $request->getRequestTarget()); - return $request; - } + private static $errorMessage; + + use ServiceTestTrait; + + public function testCreateBatch() + { + $response = $this->prophesize(ResponseInterface::class); + $client = $this->prophesize(Client::class); + + $client->execute( + Argument::allOf( + Argument::type(RequestInterface::class), + Argument::that( + function ($request) { + $this->assertEquals('/batch/test', $request->getRequestTarget()); + return $request; + } + ) + ), + Argument::any() + )->willReturn($response->reveal()); + + $client->getConfig('base_path')->willReturn(''); + + $model = new TestService($client->reveal()); + $batch = $model->createBatch(); + $this->assertInstanceOf(Batch::class, $batch); + $batch->execute(); + } + + public function testModel() + { + $model = new TestModel(); + + $model->mapTypes( + array( + 'name' => 'asdf', + 'gender' => 'z', + ) + ); + $this->assertEquals('asdf', $model->name); + $this->assertEquals('z', $model->gender); + $model->mapTypes( + array( + '__infoType' => 'Google_Model', + '__infoDataType' => 'map', + 'info' => array ( + 'location' => 'mars', + 'timezone' => 'mst', + ), + 'name' => 'asdf', + 'gender' => 'z', ) - ), - Argument::any() - )->willReturn($response->reveal()); - - $client->getConfig('base_path')->willReturn(''); - - $model = new TestService($client->reveal()); - $batch = $model->createBatch(); - $this->assertInstanceOf(Batch::class, $batch); - $batch->execute(); - } - - public function testModel() - { - $model = new TestModel(); - - $model->mapTypes( - array( - 'name' => 'asdf', - 'gender' => 'z', - ) - ); - $this->assertEquals('asdf', $model->name); - $this->assertEquals('z', $model->gender); - $model->mapTypes( - array( - '__infoType' => 'Google_Model', - '__infoDataType' => 'map', - 'info' => array ( - 'location' => 'mars', - 'timezone' => 'mst', - ), - 'name' => 'asdf', - 'gender' => 'z', - ) - ); - $this->assertEquals('asdf', $model->name); - $this->assertEquals('z', $model->gender); - - $this->assertFalse($model->isAssociativeArray("")); - $this->assertFalse($model->isAssociativeArray(false)); - $this->assertFalse($model->isAssociativeArray(null)); - $this->assertFalse($model->isAssociativeArray(array())); - $this->assertFalse($model->isAssociativeArray(array(1, 2))); - $this->assertFalse($model->isAssociativeArray(array(1 => 2))); - - $this->assertTrue($model->isAssociativeArray(array('test' => 'a'))); - $this->assertTrue($model->isAssociativeArray(array("a", "b" => 2))); - } - - public function testConfigConstructor() - { - $clientId = 'test-client-id'; - $service = new TestService(['client_id' => $clientId]); - $this->assertEquals($clientId, $service->getClient()->getClientId()); - } - - public function testNoConstructor() - { - $service = new TestService(); - $this->assertInstanceOf(Client::class, $service->getClient()); - } - - public function testInvalidConstructorPhp7Plus() - { - if (!class_exists('TypeError')) { - $this->markTestSkipped('PHP 7+ only'); + ); + $this->assertEquals('asdf', $model->name); + $this->assertEquals('z', $model->gender); + + $this->assertFalse($model->isAssociativeArray("")); + $this->assertFalse($model->isAssociativeArray(false)); + $this->assertFalse($model->isAssociativeArray(null)); + $this->assertFalse($model->isAssociativeArray(array())); + $this->assertFalse($model->isAssociativeArray(array(1, 2))); + $this->assertFalse($model->isAssociativeArray(array(1 => 2))); + + $this->assertTrue($model->isAssociativeArray(array('test' => 'a'))); + $this->assertTrue($model->isAssociativeArray(array("a", "b" => 2))); } - try { - $service = new TestService('foo'); - } catch (\TypeError $e) { + public function testConfigConstructor() + { + $clientId = 'test-client-id'; + $service = new TestService(['client_id' => $clientId]); + $this->assertEquals($clientId, $service->getClient()->getClientId()); + } + public function testNoConstructor() + { + $service = new TestService(); + $this->assertInstanceOf(Client::class, $service->getClient()); } - $this->assertInstanceOf('TypeError', $e); - $this->assertEquals( - 'constructor must be array or instance of Google\Client', - $e->getMessage() - ); - } - - /** @runInSeparateProcess */ - public function testInvalidConstructorPhp5() - { - if (class_exists('TypeError')) { - $this->markTestSkipped('PHP 5 only'); + public function testInvalidConstructorPhp7Plus() + { + if (!class_exists('TypeError')) { + $this->markTestSkipped('PHP 7+ only'); + } + + try { + $service = new TestService('foo'); + } catch (\TypeError $e) { + } + + $this->assertInstanceOf('TypeError', $e); + $this->assertEquals( + 'constructor must be array or instance of Google\Client', + $e->getMessage() + ); } - set_error_handler('Google\Tests\ServiceTest::handlePhp5Error'); + /** @runInSeparateProcess */ + public function testInvalidConstructorPhp5() + { + if (class_exists('TypeError')) { + $this->markTestSkipped('PHP 5 only'); + } + + set_error_handler('Google\Tests\ServiceTest::handlePhp5Error'); - $service = new TestService('foo'); + $service = new TestService('foo'); - $this->assertEquals( - 'constructor must be array or instance of Google\Client', - self::$errorMessage - ); - } + $this->assertEquals( + 'constructor must be array or instance of Google\Client', + self::$errorMessage + ); + } - public static function handlePhp5Error($errno, $errstr, $errfile, $errline) - { - self::assertEquals(E_USER_ERROR, $errno); - self::$errorMessage = $errstr; - return true; - } + public static function handlePhp5Error($errno, $errstr, $errfile, $errline) + { + self::assertEquals(E_USER_ERROR, $errno); + self::$errorMessage = $errstr; + return true; + } } diff --git a/tests/Google/Task/RunnerTest.php b/tests/Google/Task/RunnerTest.php index 85354e4ba..dd3cc80e5 100644 --- a/tests/Google/Task/RunnerTest.php +++ b/tests/Google/Task/RunnerTest.php @@ -34,476 +34,476 @@ class RunnerTest extends BaseTest { - private $client; + private $client; - private $mockedCallsCount = 0; - private $currentMockedCall = 0; - private $mockedCalls = array(); - private $retryMap; - private $retryConfig; + private $mockedCallsCount = 0; + private $currentMockedCall = 0; + private $mockedCalls = array(); + private $retryMap; + private $retryConfig; - protected function set_up() - { - $this->client = new Client(); - } + protected function set_up() + { + $this->client = new Client(); + } - /** + /** * @dataProvider defaultRestErrorProvider */ - public function testRestRetryOffByDefault($errorCode, $errorBody = '{}') - { - $this->expectException(ServiceException::class); - $this->setNextResponse($errorCode, $errorBody)->makeRequest(); - } + public function testRestRetryOffByDefault($errorCode, $errorBody = '{}') + { + $this->expectException(ServiceException::class); + $this->setNextResponse($errorCode, $errorBody)->makeRequest(); + } - /** + /** * @dataProvider defaultRestErrorProvider */ - public function testOneRestRetryWithError($errorCode, $errorBody = '{}') - { - $this->expectException(ServiceException::class); - $this->setRetryConfig(array('retries' => 1)); - $this->setNextResponses(2, $errorCode, $errorBody)->makeRequest(); - } + public function testOneRestRetryWithError($errorCode, $errorBody = '{}') + { + $this->expectException(ServiceException::class); + $this->setRetryConfig(array('retries' => 1)); + $this->setNextResponses(2, $errorCode, $errorBody)->makeRequest(); + } - /** + /** * @dataProvider defaultRestErrorProvider */ - public function testMultipleRestRetriesWithErrors( - $errorCode, - $errorBody = '{}' - ) { - $this->expectException(ServiceException::class); + public function testMultipleRestRetriesWithErrors( + $errorCode, + $errorBody = '{}' + ) { + $this->expectException(ServiceException::class); - $this->setRetryConfig(array('retries' => 5)); - $this->setNextResponses(6, $errorCode, $errorBody)->makeRequest(); - } + $this->setRetryConfig(array('retries' => 5)); + $this->setNextResponses(6, $errorCode, $errorBody)->makeRequest(); + } - /** + /** * @dataProvider defaultRestErrorProvider */ - public function testOneRestRetryWithSuccess($errorCode, $errorBody = '{}') - { - $this->setRetryConfig(array('retries' => 1)); - $result = $this->setNextResponse($errorCode, $errorBody) + public function testOneRestRetryWithSuccess($errorCode, $errorBody = '{}') + { + $this->setRetryConfig(array('retries' => 1)); + $result = $this->setNextResponse($errorCode, $errorBody) ->setNextResponse(200, '{"success": true}') ->makeRequest(); - $this->assertEquals('{"success": true}', (string) $result->getBody()); - } + $this->assertEquals('{"success": true}', (string) $result->getBody()); + } - /** + /** * @dataProvider defaultRestErrorProvider */ - public function testMultipleRestRetriesWithSuccess( - $errorCode, - $errorBody = '{}' - ) { - $this->setRetryConfig(array('retries' => 5)); - $result = $this->setNextResponses(2, $errorCode, $errorBody) + public function testMultipleRestRetriesWithSuccess( + $errorCode, + $errorBody = '{}' + ) { + $this->setRetryConfig(array('retries' => 5)); + $result = $this->setNextResponses(2, $errorCode, $errorBody) ->setNextResponse(200, '{"success": true}') ->makeRequest(); - $this->assertEquals('{"success": true}', (string) $result->getBody()); - } + $this->assertEquals('{"success": true}', (string) $result->getBody()); + } - /** + /** * @dataProvider defaultRestErrorProvider */ - public function testCustomRestRetryMapReplacesDefaults( - $errorCode, - $errorBody = '{}' - ) { - $this->expectException(ServiceException::class); + public function testCustomRestRetryMapReplacesDefaults( + $errorCode, + $errorBody = '{}' + ) { + $this->expectException(ServiceException::class); - $this->setRetryMap(array()); + $this->setRetryMap(array()); - $this->setRetryConfig(array('retries' => 5)); - $this->setNextResponse($errorCode, $errorBody)->makeRequest(); - } + $this->setRetryConfig(array('retries' => 5)); + $this->setNextResponse($errorCode, $errorBody)->makeRequest(); + } - public function testCustomRestRetryMapAddsNewHandlers() - { - $this->setRetryMap( - array('403' => Runner::TASK_RETRY_ALWAYS) - ); + public function testCustomRestRetryMapAddsNewHandlers() + { + $this->setRetryMap( + array('403' => Runner::TASK_RETRY_ALWAYS) + ); - $this->setRetryConfig(array('retries' => 5)); - $result = $this->setNextResponses(2, 403) + $this->setRetryConfig(array('retries' => 5)); + $result = $this->setNextResponses(2, 403) ->setNextResponse(200, '{"success": true}') ->makeRequest(); - $this->assertEquals('{"success": true}', (string) $result->getBody()); - } + $this->assertEquals('{"success": true}', (string) $result->getBody()); + } - /** + /** * @dataProvider customLimitsProvider */ - public function testCustomRestRetryMapWithCustomLimits($limit) - { - $this->expectException(ServiceException::class); + public function testCustomRestRetryMapWithCustomLimits($limit) + { + $this->expectException(ServiceException::class); - $this->setRetryMap( - array('403' => $limit) - ); + $this->setRetryMap( + array('403' => $limit) + ); - $this->setRetryConfig(array('retries' => 5)); - $this->setNextResponses($limit + 1, 403)->makeRequest(); - } + $this->setRetryConfig(array('retries' => 5)); + $this->setNextResponses($limit + 1, 403)->makeRequest(); + } - /** + /** * @dataProvider timeoutProvider */ - public function testRestTimeouts($config, $minTime) - { - $this->setRetryConfig($config); - $this->setNextResponses($config['retries'], 500) + public function testRestTimeouts($config, $minTime) + { + $this->setRetryConfig($config); + $this->setNextResponses($config['retries'], 500) ->setNextResponse(200, '{"success": true}'); - $this->assertTaskTimeGreaterThanOrEqual( - $minTime, - array($this, 'makeRequest'), - $config['initial_delay'] / 10 - ); - } + $this->assertTaskTimeGreaterThanOrEqual( + $minTime, + array($this, 'makeRequest'), + $config['initial_delay'] / 10 + ); + } - /** + /** * @requires extension curl * @dataProvider defaultCurlErrorProvider */ - public function testCurlRetryOffByDefault($errorCode, $errorMessage = '') - { - $this->expectException(ServiceException::class); + public function testCurlRetryOffByDefault($errorCode, $errorMessage = '') + { + $this->expectException(ServiceException::class); - $this->setNextResponseThrows($errorMessage, $errorCode)->makeRequest(); - } + $this->setNextResponseThrows($errorMessage, $errorCode)->makeRequest(); + } - /** + /** * @requires extension curl * @dataProvider defaultCurlErrorProvider */ - public function testOneCurlRetryWithError($errorCode, $errorMessage = '') - { - $this->expectException(ServiceException::class); + public function testOneCurlRetryWithError($errorCode, $errorMessage = '') + { + $this->expectException(ServiceException::class); - $this->setRetryConfig(array('retries' => 1)); - $this->setNextResponsesThrow(2, $errorMessage, $errorCode)->makeRequest(); - } + $this->setRetryConfig(array('retries' => 1)); + $this->setNextResponsesThrow(2, $errorMessage, $errorCode)->makeRequest(); + } - /** + /** * @requires extension curl * @dataProvider defaultCurlErrorProvider */ - public function testMultipleCurlRetriesWithErrors( - $errorCode, - $errorMessage = '' - ) { - $this->expectException(ServiceException::class); + public function testMultipleCurlRetriesWithErrors( + $errorCode, + $errorMessage = '' + ) { + $this->expectException(ServiceException::class); - $this->setRetryConfig(array('retries' => 5)); - $this->setNextResponsesThrow(6, $errorMessage, $errorCode)->makeRequest(); - } + $this->setRetryConfig(array('retries' => 5)); + $this->setNextResponsesThrow(6, $errorMessage, $errorCode)->makeRequest(); + } - /** + /** * @requires extension curl * @dataProvider defaultCurlErrorProvider */ - public function testOneCurlRetryWithSuccess($errorCode, $errorMessage = '') - { - $this->setRetryConfig(array('retries' => 1)); - $result = $this->setNextResponseThrows($errorMessage, $errorCode) + public function testOneCurlRetryWithSuccess($errorCode, $errorMessage = '') + { + $this->setRetryConfig(array('retries' => 1)); + $result = $this->setNextResponseThrows($errorMessage, $errorCode) ->setNextResponse(200, '{"success": true}') ->makeRequest(); - $this->assertEquals('{"success": true}', (string) $result->getBody()); - } + $this->assertEquals('{"success": true}', (string) $result->getBody()); + } - /** + /** * @requires extension curl * @dataProvider defaultCurlErrorProvider */ - public function testMultipleCurlRetriesWithSuccess( - $errorCode, - $errorMessage = '' - ) { - $this->setRetryConfig(array('retries' => 5)); - $result = $this->setNextResponsesThrow(2, $errorMessage, $errorCode) + public function testMultipleCurlRetriesWithSuccess( + $errorCode, + $errorMessage = '' + ) { + $this->setRetryConfig(array('retries' => 5)); + $result = $this->setNextResponsesThrow(2, $errorMessage, $errorCode) ->setNextResponse(200, '{"success": true}') ->makeRequest(); - $this->assertEquals('{"success": true}', (string) $result->getBody()); - } + $this->assertEquals('{"success": true}', (string) $result->getBody()); + } - /** + /** * @requires extension curl * @dataProvider defaultCurlErrorProvider */ - public function testCustomCurlRetryMapReplacesDefaults( - $errorCode, - $errorMessage = '' - ) { - $this->expectException(ServiceException::class); + public function testCustomCurlRetryMapReplacesDefaults( + $errorCode, + $errorMessage = '' + ) { + $this->expectException(ServiceException::class); - $this->setRetryMap(array()); + $this->setRetryMap(array()); - $this->setRetryConfig(array('retries' => 5)); - $this->setNextResponseThrows($errorMessage, $errorCode)->makeRequest(); - } + $this->setRetryConfig(array('retries' => 5)); + $this->setNextResponseThrows($errorMessage, $errorCode)->makeRequest(); + } - /** + /** * @requires extension curl */ - public function testCustomCurlRetryMapAddsNewHandlers() - { - $this->setRetryMap( - array(CURLE_COULDNT_RESOLVE_PROXY => Runner::TASK_RETRY_ALWAYS) - ); + public function testCustomCurlRetryMapAddsNewHandlers() + { + $this->setRetryMap( + array(CURLE_COULDNT_RESOLVE_PROXY => Runner::TASK_RETRY_ALWAYS) + ); - $this->setRetryConfig(array('retries' => 5)); - $result = $this->setNextResponsesThrow(2, '', CURLE_COULDNT_RESOLVE_PROXY) + $this->setRetryConfig(array('retries' => 5)); + $result = $this->setNextResponsesThrow(2, '', CURLE_COULDNT_RESOLVE_PROXY) ->setNextResponse(200, '{"success": true}') ->makeRequest(); - $this->assertEquals('{"success": true}', (string) $result->getBody()); - } + $this->assertEquals('{"success": true}', (string) $result->getBody()); + } - /** + /** * @requires extension curl * @dataProvider customLimitsProvider */ - public function testCustomCurlRetryMapWithCustomLimits($limit) - { - $this->expectException(ServiceException::class); + public function testCustomCurlRetryMapWithCustomLimits($limit) + { + $this->expectException(ServiceException::class); - $this->setRetryMap( - array(CURLE_COULDNT_RESOLVE_PROXY => $limit) - ); + $this->setRetryMap( + array(CURLE_COULDNT_RESOLVE_PROXY => $limit) + ); - $this->setRetryConfig(array('retries' => 5)); - $this->setNextResponsesThrow($limit + 1, '', CURLE_COULDNT_RESOLVE_PROXY) + $this->setRetryConfig(array('retries' => 5)); + $this->setNextResponsesThrow($limit + 1, '', CURLE_COULDNT_RESOLVE_PROXY) ->makeRequest(); - } + } - /** + /** * @requires extension curl * @dataProvider timeoutProvider */ - public function testCurlTimeouts($config, $minTime) - { - $this->setRetryConfig($config); - $this->setNextResponsesThrow($config['retries'], '', CURLE_GOT_NOTHING) + public function testCurlTimeouts($config, $minTime) + { + $this->setRetryConfig($config); + $this->setNextResponsesThrow($config['retries'], '', CURLE_GOT_NOTHING) ->setNextResponse(200, '{"success": true}'); - $this->assertTaskTimeGreaterThanOrEqual( - $minTime, - array($this, 'makeRequest'), - $config['initial_delay'] / 10 - ); - } + $this->assertTaskTimeGreaterThanOrEqual( + $minTime, + array($this, 'makeRequest'), + $config['initial_delay'] / 10 + ); + } - /** + /** * @dataProvider badTaskConfigProvider */ - public function testBadTaskConfig($config, $message) - { - $this->expectException(TaskException::class); - $this->expectExceptionMessage($message); - $this->setRetryConfig($config); - - new Runner( - $this->retryConfig, - '', - array($this, 'testBadTaskConfig') - ); - } + public function testBadTaskConfig($config, $message) + { + $this->expectException(TaskException::class); + $this->expectExceptionMessage($message); + $this->setRetryConfig($config); + + new Runner( + $this->retryConfig, + '', + array($this, 'testBadTaskConfig') + ); + } - /** + /** * @expectedExceptionMessage must be a valid callable */ - public function testBadTaskCallback() - { - $this->expectException(TaskException::class); - $config = []; - new Runner($config, '', 5); - } + public function testBadTaskCallback() + { + $this->expectException(TaskException::class); + $config = []; + new Runner($config, '', 5); + } - public function testTaskRetryOffByDefault() - { - $this->expectException(ServiceException::class); + public function testTaskRetryOffByDefault() + { + $this->expectException(ServiceException::class); - $this->setNextTaskAllowedRetries(Runner::TASK_RETRY_ALWAYS) + $this->setNextTaskAllowedRetries(Runner::TASK_RETRY_ALWAYS) ->runTask(); - } + } - public function testOneTaskRetryWithError() - { - $this->expectException(ServiceException::class); + public function testOneTaskRetryWithError() + { + $this->expectException(ServiceException::class); - $this->setRetryConfig(array('retries' => 1)); - $this->setNextTasksAllowedRetries(2, Runner::TASK_RETRY_ALWAYS) + $this->setRetryConfig(array('retries' => 1)); + $this->setNextTasksAllowedRetries(2, Runner::TASK_RETRY_ALWAYS) ->runTask(); - } + } - public function testMultipleTaskRetriesWithErrors() - { - $this->expectException(ServiceException::class); + public function testMultipleTaskRetriesWithErrors() + { + $this->expectException(ServiceException::class); - $this->setRetryConfig(array('retries' => 5)); - $this->setNextTasksAllowedRetries(6, Runner::TASK_RETRY_ALWAYS) + $this->setRetryConfig(array('retries' => 5)); + $this->setNextTasksAllowedRetries(6, Runner::TASK_RETRY_ALWAYS) ->runTask(); - } + } - public function testOneTaskRetryWithSuccess() - { - $this->setRetryConfig(array('retries' => 1)); - $result = $this->setNextTaskAllowedRetries(Runner::TASK_RETRY_ALWAYS) + public function testOneTaskRetryWithSuccess() + { + $this->setRetryConfig(array('retries' => 1)); + $result = $this->setNextTaskAllowedRetries(Runner::TASK_RETRY_ALWAYS) ->setNextTaskReturnValue('success') ->runTask(); - $this->assertEquals('success', $result); - } + $this->assertEquals('success', $result); + } - public function testMultipleTaskRetriesWithSuccess() - { - $this->setRetryConfig(array('retries' => 5)); - $result = $this->setNextTasksAllowedRetries(2, Runner::TASK_RETRY_ALWAYS) + public function testMultipleTaskRetriesWithSuccess() + { + $this->setRetryConfig(array('retries' => 5)); + $result = $this->setNextTasksAllowedRetries(2, Runner::TASK_RETRY_ALWAYS) ->setNextTaskReturnValue('success') ->runTask(); - $this->assertEquals('success', $result); - } + $this->assertEquals('success', $result); + } - /** + /** * @dataProvider customLimitsProvider */ - public function testTaskRetryWithCustomLimits($limit) - { - $this->expectException(ServiceException::class); + public function testTaskRetryWithCustomLimits($limit) + { + $this->expectException(ServiceException::class); - $this->setRetryConfig(array('retries' => 5)); - $this->setNextTasksAllowedRetries($limit + 1, $limit) + $this->setRetryConfig(array('retries' => 5)); + $this->setNextTasksAllowedRetries($limit + 1, $limit) ->runTask(); - } + } - /** + /** * @dataProvider timeoutProvider */ - public function testTaskTimeouts($config, $minTime) - { - $this->setRetryConfig($config); - $this->setNextTasksAllowedRetries($config['retries'], $config['retries'] + 1) + public function testTaskTimeouts($config, $minTime) + { + $this->setRetryConfig($config); + $this->setNextTasksAllowedRetries($config['retries'], $config['retries'] + 1) ->setNextTaskReturnValue('success'); - $this->assertTaskTimeGreaterThanOrEqual( - $minTime, - array($this, 'runTask'), - $config['initial_delay'] / 10 - ); - } + $this->assertTaskTimeGreaterThanOrEqual( + $minTime, + array($this, 'runTask'), + $config['initial_delay'] / 10 + ); + } - public function testTaskWithManualRetries() - { - $this->setRetryConfig(array('retries' => 2)); - $this->setNextTasksAllowedRetries(2, Runner::TASK_RETRY_ALWAYS); + public function testTaskWithManualRetries() + { + $this->setRetryConfig(array('retries' => 2)); + $this->setNextTasksAllowedRetries(2, Runner::TASK_RETRY_ALWAYS); - $task = new Runner( - $this->retryConfig, - '', - array($this, 'runNextTask') - ); + $task = new Runner( + $this->retryConfig, + '', + array($this, 'runNextTask') + ); - $this->assertTrue($task->canAttempt()); - $this->assertTrue($task->attempt()); + $this->assertTrue($task->canAttempt()); + $this->assertTrue($task->attempt()); - $this->assertTrue($task->canAttempt()); - $this->assertTrue($task->attempt()); + $this->assertTrue($task->canAttempt()); + $this->assertTrue($task->attempt()); - $this->assertTrue($task->canAttempt()); - $this->assertTrue($task->attempt()); + $this->assertTrue($task->canAttempt()); + $this->assertTrue($task->attempt()); - $this->assertFalse($task->canAttempt()); - $this->assertFalse($task->attempt()); - } + $this->assertFalse($task->canAttempt()); + $this->assertFalse($task->attempt()); + } - /** + /** * Provider for backoff configurations and expected minimum runtimes. * * @return array */ - public function timeoutProvider() - { - $config = array('initial_delay' => .001, 'max_delay' => .01); + public function timeoutProvider() + { + $config = array('initial_delay' => .001, 'max_delay' => .01); - return array( + return array( array(array_merge($config, array('retries' => 1)), .001), array(array_merge($config, array('retries' => 2)), .0015), array(array_merge($config, array('retries' => 3)), .00225), array(array_merge($config, array('retries' => 4)), .00375), array(array_merge($config, array('retries' => 5)), .005625) - ); - } + ); + } - /** + /** * Provider for custom retry limits. * * @return array */ - public function customLimitsProvider() - { - return array( + public function customLimitsProvider() + { + return array( array(Runner::TASK_RETRY_NEVER), array(Runner::TASK_RETRY_ONCE), - ); - } + ); + } - /** + /** * Provider for invalid task configurations. * * @return array */ - public function badTaskConfigProvider() - { - return array( + public function badTaskConfigProvider() + { + return array( array(array('initial_delay' => -1), 'must not be negative'), array(array('max_delay' => 0), 'must be greater than 0'), array(array('factor' => 0), 'must be greater than 0'), array(array('jitter' => 0), 'must be greater than 0'), array(array('retries' => -1), 'must not be negative') - ); - } + ); + } - /** + /** * Provider for the default REST errors. * * @return array */ - public function defaultRestErrorProvider() - { - return array( + public function defaultRestErrorProvider() + { + return array( array(500), array(503), array(403, '{"error":{"errors":[{"reason":"rateLimitExceeded"}]}}'), array(403, '{"error":{"errors":[{"reason":"userRateLimitExceeded"}]}}'), - ); - } + ); + } - /** + /** * Provider for the default cURL errors. * * @return array */ - public function defaultCurlErrorProvider() - { - return array( + public function defaultCurlErrorProvider() + { + return array( array(6), // CURLE_COULDNT_RESOLVE_HOST array(7), // CURLE_COULDNT_CONNECT array(28), // CURLE_OPERATION_TIMEOUTED array(35), // CURLE_SSL_CONNECT_ERROR array(52), // CURLE_GOT_NOTHING - ); - } + ); + } - /** + /** * Assert the minimum amount of time required to run a task. * * NOTE: Intentionally crude for brevity. @@ -514,47 +514,47 @@ public function defaultCurlErrorProvider() * * @throws PHPUnit_Framework_ExpectationFailedException */ - public static function assertTaskTimeGreaterThanOrEqual( - $expected, - $callback, - $delta = 0.0 - ) { - $time = microtime(true); - - call_user_func($callback); - - self::assertThat( - microtime(true) - $time, - self::logicalOr( - self::greaterThan($expected), - self::equalTo($expected, $delta) - ) - ); - } - - /** + public static function assertTaskTimeGreaterThanOrEqual( + $expected, + $callback, + $delta = 0.0 + ) { + $time = microtime(true); + + call_user_func($callback); + + self::assertThat( + microtime(true) - $time, + self::logicalOr( + self::greaterThan($expected), + self::equalTo($expected, $delta) + ) + ); + } + + /** * Sets the task runner configurations. * * @param array $config The task runner configurations */ - private function setRetryConfig(array $config) - { - $config += array( + private function setRetryConfig(array $config) + { + $config += array( 'initial_delay' => .0001, 'max_delay' => .001, 'factor' => 2, 'jitter' => .5, 'retries' => 1 - ); - $this->retryConfig = $config; - } + ); + $this->retryConfig = $config; + } - private function setRetryMap(array $retryMap) - { - $this->retryMap = $retryMap; - } + private function setRetryMap(array $retryMap) + { + $this->retryMap = $retryMap; + } - /** + /** * Sets the next responses. * * @param integer $count The number of responses @@ -564,20 +564,20 @@ private function setRetryMap(array $retryMap) * * @return TaskTest */ - private function setNextResponses( - $count, - $code = '200', - $body = '{}', - array $headers = array() - ) { - while ($count-- > 0) { - $this->setNextResponse($code, $body, $headers); + private function setNextResponses( + $count, + $code = '200', + $body = '{}', + array $headers = array() + ) { + while ($count-- > 0) { + $this->setNextResponse($code, $body, $headers); + } + + return $this; } - return $this; - } - - /** + /** * Sets the next response. * * @param string $code The response code @@ -586,21 +586,21 @@ private function setNextResponses( * * @return TaskTest */ - private function setNextResponse( - $code = '200', - $body = '{}', - array $headers = array() - ) { - $this->mockedCalls[$this->mockedCallsCount++] = array( + private function setNextResponse( + $code = '200', + $body = '{}', + array $headers = array() + ) { + $this->mockedCalls[$this->mockedCallsCount++] = array( 'code' => (string) $code, 'headers' => $headers, 'body' => is_string($body) ? $body : json_encode($body) - ); + ); - return $this; - } + return $this; + } - /** + /** * Forces the next responses to throw an IO exception. * * @param integer $count The number of responses @@ -609,16 +609,16 @@ private function setNextResponse( * * @return TaskTest */ - private function setNextResponsesThrow($count, $message, $code) - { - while ($count-- > 0) { - $this->setNextResponseThrows($message, $code); - } + private function setNextResponsesThrow($count, $message, $code) + { + while ($count-- > 0) { + $this->setNextResponseThrows($message, $code); + } - return $this; - } + return $this; + } - /** + /** * Forces the next response to throw an IO exception. * * @param string $message The exception messages @@ -626,98 +626,98 @@ private function setNextResponsesThrow($count, $message, $code) * * @return TaskTest */ - private function setNextResponseThrows($message, $code) - { - $this->mockedCalls[$this->mockedCallsCount++] = new ServiceException( - $message, - $code, - null, - array() - ); + private function setNextResponseThrows($message, $code) + { + $this->mockedCalls[$this->mockedCallsCount++] = new ServiceException( + $message, + $code, + null, + array() + ); - return $this; - } + return $this; + } - /** + /** * Runs the defined request. * * @return array */ - private function makeRequest() - { - $request = new Request('GET', '/test'); - $http = $this->prophesize('GuzzleHttp\ClientInterface'); - - if ($this->isGuzzle5()) { - $http->createRequest(Argument::any(), Argument::any(), Argument::any()) - ->shouldBeCalledTimes($this->mockedCallsCount) - ->willReturn(new \GuzzleHttp\Message\Request('GET', '/test')); - - $http->send(Argument::type('GuzzleHttp\Message\Request')) - ->shouldBeCalledTimes($this->mockedCallsCount) - ->will([$this, 'getNextMockedCall']); - } else { - $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) - ->shouldBeCalledTimes($this->mockedCallsCount) - ->will([$this, 'getNextMockedCall']); + private function makeRequest() + { + $request = new Request('GET', '/test'); + $http = $this->prophesize('GuzzleHttp\ClientInterface'); + + if ($this->isGuzzle5()) { + $http->createRequest(Argument::any(), Argument::any(), Argument::any()) + ->shouldBeCalledTimes($this->mockedCallsCount) + ->willReturn(new \GuzzleHttp\Message\Request('GET', '/test')); + + $http->send(Argument::type('GuzzleHttp\Message\Request')) + ->shouldBeCalledTimes($this->mockedCallsCount) + ->will([$this, 'getNextMockedCall']); + } else { + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes($this->mockedCallsCount) + ->will([$this, 'getNextMockedCall']); + } + + return REST::execute($http->reveal(), $request, '', $this->retryConfig, $this->retryMap); } - return REST::execute($http->reveal(), $request, '', $this->retryConfig, $this->retryMap); - } - - /** + /** * Gets the next mocked response. * * @param GoogleRequest $request The mocked request * * @return GoogleRequest */ - public function getNextMockedCall($request) - { - $current = $this->mockedCalls[$this->currentMockedCall++]; + public function getNextMockedCall($request) + { + $current = $this->mockedCalls[$this->currentMockedCall++]; - if ($current instanceof Exception) { - throw $current; - } + if ($current instanceof Exception) { + throw $current; + } - if ($this->isGuzzle5()) { - $stream = Guzzle5Stream::factory($current['body']); - $response = new Guzzle5Response($current['code'], $current['headers'], $stream); - } else { - $stream = Psr7\Utils::streamFor($current['body']); - $response = new Response($current['code'], $current['headers'], $stream); - } + if ($this->isGuzzle5()) { + $stream = Guzzle5Stream::factory($current['body']); + $response = new Guzzle5Response($current['code'], $current['headers'], $stream); + } else { + $stream = Psr7\Utils::streamFor($current['body']); + $response = new Response($current['code'], $current['headers'], $stream); + } - return $response; - } + return $response; + } - /** + /** * Sets the next task return value. * * @param mixed $value The next return value * * @return TaskTest */ - private function setNextTaskReturnValue($value) - { - $this->mockedCalls[$this->mockedCallsCount++] = $value; - return $this; - } + private function setNextTaskReturnValue($value) + { + $this->mockedCalls[$this->mockedCallsCount++] = $value; + return $this; + } - /** + /** * Sets the next exception `allowedRetries()` return value. * * @param boolean $allowedRetries The next `allowedRetries()` return value. * * @return TaskTest */ - private function setNextTaskAllowedRetries($allowedRetries) - { - $this->mockedCalls[$this->mockedCallsCount++] = $allowedRetries; - return $this; - } + private function setNextTaskAllowedRetries($allowedRetries) + { + $this->mockedCalls[$this->mockedCallsCount++] = $allowedRetries; + return $this; + } - /** + /** * Sets multiple exception `allowedRetries()` return value. * * @param integer $count The number of `allowedRetries()` return values. @@ -725,62 +725,62 @@ private function setNextTaskAllowedRetries($allowedRetries) * * @return TaskTest */ - private function setNextTasksAllowedRetries($count, $allowedRetries) - { - while ($count-- > 0) { - $this->setNextTaskAllowedRetries($allowedRetries); - } + private function setNextTasksAllowedRetries($count, $allowedRetries) + { + while ($count-- > 0) { + $this->setNextTaskAllowedRetries($allowedRetries); + } - return $this; - } + return $this; + } - /** + /** * Runs the defined task. * * @return mixed */ - private function runTask() - { - $task = new Runner( - $this->retryConfig, - '', - array($this, 'runNextTask') - ); + private function runTask() + { + $task = new Runner( + $this->retryConfig, + '', + array($this, 'runNextTask') + ); - if (null !== $this->retryMap) { - $task->setRetryMap($this->retryMap); - } + if (null !== $this->retryMap) { + $task->setRetryMap($this->retryMap); + } - $exception = $this->prophesize(ServiceException::class); + $exception = $this->prophesize(ServiceException::class); - $exceptionCount = 0; - $exceptionCalls = array(); + $exceptionCount = 0; + $exceptionCalls = array(); - for ($i = 0; $i < $this->mockedCallsCount; $i++) { - if (is_int($this->mockedCalls[$i])) { - $exceptionCalls[$exceptionCount++] = $this->mockedCalls[$i]; - $this->mockedCalls[$i] = $exception->reveal(); - } - } + for ($i = 0; $i < $this->mockedCallsCount; $i++) { + if (is_int($this->mockedCalls[$i])) { + $exceptionCalls[$exceptionCount++] = $this->mockedCalls[$i]; + $this->mockedCalls[$i] = $exception->reveal(); + } + } - $task->setRetryMap($exceptionCalls); + $task->setRetryMap($exceptionCalls); - return $task->run(); - } + return $task->run(); + } - /** + /** * Gets the next task return value. * * @return mixed */ - public function runNextTask() - { - $current = $this->mockedCalls[$this->currentMockedCall++]; + public function runNextTask() + { + $current = $this->mockedCalls[$this->currentMockedCall++]; - if ($current instanceof Exception) { - throw $current; - } + if ($current instanceof Exception) { + throw $current; + } - return $current; - } + return $current; + } } diff --git a/tests/Google/Utils/UriTemplateTest.php b/tests/Google/Utils/UriTemplateTest.php index 09ecb6b0b..5e3e2d414 100644 --- a/tests/Google/Utils/UriTemplateTest.php +++ b/tests/Google/Utils/UriTemplateTest.php @@ -25,282 +25,281 @@ class UriTemplateTest extends BaseTest { - public function testLevelOne() - { - $var = "value"; - $hello = "Hello World!"; + public function testLevelOne() + { + $var = "value"; + $hello = "Hello World!"; - $urit = new UriTemplate(); - $this->assertEquals( - "value", - $urit->parse("{var}", array("var" => $var)) - ); - $this->assertEquals( - "Hello%20World%21", - $urit->parse("{hello}", array("hello" => $hello)) - ); - } - - public function testLevelTwo() - { - $var = "value"; - $hello = "Hello World!"; - $path = "/foo/bar"; + $urit = new UriTemplate(); + $this->assertEquals( + "value", + $urit->parse("{var}", array("var" => $var)) + ); + $this->assertEquals( + "Hello%20World%21", + $urit->parse("{hello}", array("hello" => $hello)) + ); + } - $urit = new UriTemplate(); - $this->assertEquals( - "value", - $urit->parse("{+var}", array("var" => $var)) - ); - $this->assertEquals( - "Hello%20World!", - $urit->parse("{+hello}", array("hello" => $hello)) - ); - $this->assertEquals( - "/foo/bar/here", - $urit->parse("{+path}/here", array("path" => $path)) - ); - $this->assertEquals( - "here?ref=/foo/bar", - $urit->parse("here?ref={+path}", array("path" => $path)) - ); - $this->assertEquals( - "X#value", - $urit->parse("X{#var}", array("var" => $var)) - ); - $this->assertEquals( - "X#Hello%20World!", - $urit->parse("X{#hello}", array("hello" => $hello)) - ); - } + public function testLevelTwo() + { + $var = "value"; + $hello = "Hello World!"; + $path = "/foo/bar"; - public function testLevelThree() - { - $var = "value"; - $hello = "Hello World!"; - $empty = ''; - $path = "/foo/bar"; - $x = "1024"; - $y = "768"; + $urit = new UriTemplate(); + $this->assertEquals( + "value", + $urit->parse("{+var}", array("var" => $var)) + ); + $this->assertEquals( + "Hello%20World!", + $urit->parse("{+hello}", array("hello" => $hello)) + ); + $this->assertEquals( + "/foo/bar/here", + $urit->parse("{+path}/here", array("path" => $path)) + ); + $this->assertEquals( + "here?ref=/foo/bar", + $urit->parse("here?ref={+path}", array("path" => $path)) + ); + $this->assertEquals( + "X#value", + $urit->parse("X{#var}", array("var" => $var)) + ); + $this->assertEquals( + "X#Hello%20World!", + $urit->parse("X{#hello}", array("hello" => $hello)) + ); + } - $urit = new UriTemplate(); - $this->assertEquals( - "map?1024,768", - $urit->parse("map?{x,y}", array("x" => $x, "y" => $y)) - ); - $this->assertEquals( - "1024,Hello%20World%21,768", - $urit->parse("{x,hello,y}", array("x" => $x, "y" => $y, "hello" => $hello)) - ); + public function testLevelThree() + { + $var = "value"; + $hello = "Hello World!"; + $empty = ''; + $path = "/foo/bar"; + $x = "1024"; + $y = "768"; - $this->assertEquals( - "1024,Hello%20World!,768", - $urit->parse("{+x,hello,y}", array("x" => $x, "y" => $y, "hello" => $hello)) - ); - $this->assertEquals( - "/foo/bar,1024/here", - $urit->parse("{+path,x}/here", array("x" => $x, "path" => $path)) - ); + $urit = new UriTemplate(); + $this->assertEquals( + "map?1024,768", + $urit->parse("map?{x,y}", array("x" => $x, "y" => $y)) + ); + $this->assertEquals( + "1024,Hello%20World%21,768", + $urit->parse("{x,hello,y}", array("x" => $x, "y" => $y, "hello" => $hello)) + ); - $this->assertEquals( - "#1024,Hello%20World!,768", - $urit->parse("{#x,hello,y}", array("x" => $x, "y" => $y, "hello" => $hello)) - ); - $this->assertEquals( - "#/foo/bar,1024/here", - $urit->parse("{#path,x}/here", array("x" => $x, "path" => $path)) - ); + $this->assertEquals( + "1024,Hello%20World!,768", + $urit->parse("{+x,hello,y}", array("x" => $x, "y" => $y, "hello" => $hello)) + ); + $this->assertEquals( + "/foo/bar,1024/here", + $urit->parse("{+path,x}/here", array("x" => $x, "path" => $path)) + ); - $this->assertEquals( - "X.value", - $urit->parse("X{.var}", array("var" => $var)) - ); - $this->assertEquals( - "X.1024.768", - $urit->parse("X{.x,y}", array("x" => $x, "y" => $y)) - ); + $this->assertEquals( + "#1024,Hello%20World!,768", + $urit->parse("{#x,hello,y}", array("x" => $x, "y" => $y, "hello" => $hello)) + ); + $this->assertEquals( + "#/foo/bar,1024/here", + $urit->parse("{#path,x}/here", array("x" => $x, "path" => $path)) + ); - $this->assertEquals( - "X.value", - $urit->parse("X{.var}", array("var" => $var)) - ); - $this->assertEquals( - "X.1024.768", - $urit->parse("X{.x,y}", array("x" => $x, "y" => $y)) - ); + $this->assertEquals( + "X.value", + $urit->parse("X{.var}", array("var" => $var)) + ); + $this->assertEquals( + "X.1024.768", + $urit->parse("X{.x,y}", array("x" => $x, "y" => $y)) + ); - $this->assertEquals( - "/value", - $urit->parse("{/var}", array("var" => $var)) - ); - $this->assertEquals( - "/value/1024/here", - $urit->parse("{/var,x}/here", array("x" => $x, "var" => $var)) - ); + $this->assertEquals( + "X.value", + $urit->parse("X{.var}", array("var" => $var)) + ); + $this->assertEquals( + "X.1024.768", + $urit->parse("X{.x,y}", array("x" => $x, "y" => $y)) + ); - $this->assertEquals( - ";x=1024;y=768", - $urit->parse("{;x,y}", array("x" => $x, "y" => $y)) - ); - $this->assertEquals( - ";x=1024;y=768;empty", - $urit->parse("{;x,y,empty}", array("x" => $x, "y" => $y, "empty" => $empty)) - ); + $this->assertEquals( + "/value", + $urit->parse("{/var}", array("var" => $var)) + ); + $this->assertEquals( + "/value/1024/here", + $urit->parse("{/var,x}/here", array("x" => $x, "var" => $var)) + ); - $this->assertEquals( - "?x=1024&y=768", - $urit->parse("{?x,y}", array("x" => $x, "y" => $y)) - ); - $this->assertEquals( - "?x=1024&y=768&empty=", - $urit->parse("{?x,y,empty}", array("x" => $x, "y" => $y, "empty" => $empty)) - ); + $this->assertEquals( + ";x=1024;y=768", + $urit->parse("{;x,y}", array("x" => $x, "y" => $y)) + ); + $this->assertEquals( + ";x=1024;y=768;empty", + $urit->parse("{;x,y,empty}", array("x" => $x, "y" => $y, "empty" => $empty)) + ); - $this->assertEquals( - "?fixed=yes&x=1024", - $urit->parse("?fixed=yes{&x}", array("x" => $x, "y" => $y)) - ); - $this->assertEquals( - "&x=1024&y=768&empty=", - $urit->parse("{&x,y,empty}", array("x" => $x, "y" => $y, "empty" => $empty)) - ); - } + $this->assertEquals( + "?x=1024&y=768", + $urit->parse("{?x,y}", array("x" => $x, "y" => $y)) + ); + $this->assertEquals( + "?x=1024&y=768&empty=", + $urit->parse("{?x,y,empty}", array("x" => $x, "y" => $y, "empty" => $empty)) + ); - public function testLevelFour() - { - $values = array( - 'var' => "value", - 'hello' => "Hello World!", - 'path' => "/foo/bar", - 'list' => array("red", "green", "blue"), - 'keys' => array("semi" => ";", "dot" => ".", "comma" => ","), - ); + $this->assertEquals( + "?fixed=yes&x=1024", + $urit->parse("?fixed=yes{&x}", array("x" => $x, "y" => $y)) + ); + $this->assertEquals( + "&x=1024&y=768&empty=", + $urit->parse("{&x,y,empty}", array("x" => $x, "y" => $y, "empty" => $empty)) + ); + } - $tests = array( - "{var:3}" => "val", - "{var:30}" => "value", - "{list}" => "red,green,blue", - "{list*}" => "red,green,blue", - "{keys}" => "semi,%3B,dot,.,comma,%2C", - "{keys*}" => "semi=%3B,dot=.,comma=%2C", - "{+path:6}/here" => "/foo/b/here", - "{+list}" => "red,green,blue", - "{+list*}" => "red,green,blue", - "{+keys}" => "semi,;,dot,.,comma,,", - "{+keys*}" => "semi=;,dot=.,comma=,", - "{#path:6}/here" => "#/foo/b/here", - "{#list}" => "#red,green,blue", - "{#list*}" => "#red,green,blue", - "{#keys}" => "#semi,;,dot,.,comma,,", - "{#keys*}" => "#semi=;,dot=.,comma=,", - "X{.var:3}" => "X.val", - "X{.list}" => "X.red,green,blue", - "X{.list*}" => "X.red.green.blue", - "X{.keys}" => "X.semi,%3B,dot,.,comma,%2C", - "X{.keys*}" => "X.semi=%3B.dot=..comma=%2C", - "{/var:1,var}" => "/v/value", - "{/list}" => "/red,green,blue", - "{/list*}" => "/red/green/blue", - "{/list*,path:4}" => "/red/green/blue/%2Ffoo", - "{/keys}" => "/semi,%3B,dot,.,comma,%2C", - "{/keys*}" => "/semi=%3B/dot=./comma=%2C", - "{;hello:5}" => ";hello=Hello", - "{;list}" => ";list=red,green,blue", - "{;list*}" => ";list=red;list=green;list=blue", - "{;keys}" => ";keys=semi,%3B,dot,.,comma,%2C", - "{;keys*}" => ";semi=%3B;dot=.;comma=%2C", - "{?var:3}" => "?var=val", - "{?list}" => "?list=red,green,blue", - "{?list*}" => "?list=red&list=green&list=blue", - "{?keys}" => "?keys=semi,%3B,dot,.,comma,%2C", - "{?keys*}" => "?semi=%3B&dot=.&comma=%2C", - "{&var:3}" => "&var=val", - "{&list}" => "&list=red,green,blue", - "{&list*}" => "&list=red&list=green&list=blue", - "{&keys}" => "&keys=semi,%3B,dot,.,comma,%2C", - "{&keys*}" => "&semi=%3B&dot=.&comma=%2C", - "find{?list*}" => "find?list=red&list=green&list=blue", - "www{.list*}" => "www.red.green.blue" + public function testLevelFour() + { + $values = array( + 'var' => "value", + 'hello' => "Hello World!", + 'path' => "/foo/bar", + 'list' => array("red", "green", "blue"), + 'keys' => array("semi" => ";", "dot" => ".", "comma" => ","), + ); - ); + $tests = array( + "{var:3}" => "val", + "{var:30}" => "value", + "{list}" => "red,green,blue", + "{list*}" => "red,green,blue", + "{keys}" => "semi,%3B,dot,.,comma,%2C", + "{keys*}" => "semi=%3B,dot=.,comma=%2C", + "{+path:6}/here" => "/foo/b/here", + "{+list}" => "red,green,blue", + "{+list*}" => "red,green,blue", + "{+keys}" => "semi,;,dot,.,comma,,", + "{+keys*}" => "semi=;,dot=.,comma=,", + "{#path:6}/here" => "#/foo/b/here", + "{#list}" => "#red,green,blue", + "{#list*}" => "#red,green,blue", + "{#keys}" => "#semi,;,dot,.,comma,,", + "{#keys*}" => "#semi=;,dot=.,comma=,", + "X{.var:3}" => "X.val", + "X{.list}" => "X.red,green,blue", + "X{.list*}" => "X.red.green.blue", + "X{.keys}" => "X.semi,%3B,dot,.,comma,%2C", + "X{.keys*}" => "X.semi=%3B.dot=..comma=%2C", + "{/var:1,var}" => "/v/value", + "{/list}" => "/red,green,blue", + "{/list*}" => "/red/green/blue", + "{/list*,path:4}" => "/red/green/blue/%2Ffoo", + "{/keys}" => "/semi,%3B,dot,.,comma,%2C", + "{/keys*}" => "/semi=%3B/dot=./comma=%2C", + "{;hello:5}" => ";hello=Hello", + "{;list}" => ";list=red,green,blue", + "{;list*}" => ";list=red;list=green;list=blue", + "{;keys}" => ";keys=semi,%3B,dot,.,comma,%2C", + "{;keys*}" => ";semi=%3B;dot=.;comma=%2C", + "{?var:3}" => "?var=val", + "{?list}" => "?list=red,green,blue", + "{?list*}" => "?list=red&list=green&list=blue", + "{?keys}" => "?keys=semi,%3B,dot,.,comma,%2C", + "{?keys*}" => "?semi=%3B&dot=.&comma=%2C", + "{&var:3}" => "&var=val", + "{&list}" => "&list=red,green,blue", + "{&list*}" => "&list=red&list=green&list=blue", + "{&keys}" => "&keys=semi,%3B,dot,.,comma,%2C", + "{&keys*}" => "&semi=%3B&dot=.&comma=%2C", + "find{?list*}" => "find?list=red&list=green&list=blue", + "www{.list*}" => "www.red.green.blue" + ); - $urit = new UriTemplate(); + $urit = new UriTemplate(); - foreach ($tests as $input => $output) { - $this->assertEquals($output, $urit->parse($input, $values), $input . " failed"); + foreach ($tests as $input => $output) { + $this->assertEquals($output, $urit->parse($input, $values), $input . " failed"); + } } - } - public function testMultipleAnnotations() - { - $var = "value"; - $hello = "Hello World!"; - $urit = new UriTemplate(); - $this->assertEquals( - "http://www.google.com/Hello%20World!?var=value", - $urit->parse( - "http://www.google.com/%7B+hello%7D%7B?var}", - array("var" => $var, "hello" => $hello) - ) - ); - $params = array( - "playerId" => "me", - "leaderboardId" => "CgkIhcG1jYEbEAIQAw", - "timeSpan" => "ALL_TIME", - "other" => "irrelevant" - ); - $this->assertEquals( - "players/me/leaderboards/CgkIhcG1jYEbEAIQAw/scores/ALL_TIME", - $urit->parse( - "players/{playerId}/leaderboards/{leaderboardId}/scores/{timeSpan}", - $params - ) - ); - } + public function testMultipleAnnotations() + { + $var = "value"; + $hello = "Hello World!"; + $urit = new UriTemplate(); + $this->assertEquals( + "http://www.google.com/Hello%20World!?var=value", + $urit->parse( + "http://www.google.com/%7B+hello%7D%7B?var}", + array("var" => $var, "hello" => $hello) + ) + ); + $params = array( + "playerId" => "me", + "leaderboardId" => "CgkIhcG1jYEbEAIQAw", + "timeSpan" => "ALL_TIME", + "other" => "irrelevant" + ); + $this->assertEquals( + "players/me/leaderboards/CgkIhcG1jYEbEAIQAw/scores/ALL_TIME", + $urit->parse( + "players/{playerId}/leaderboards/{leaderboardId}/scores/{timeSpan}", + $params + ) + ); + } - /** - * This test test against the JSON files defined in - * https://github.com/uri-templates/uritemplate-test - * - * We don't ship these tests with it, so they'll just silently - * skip unless provided - this is mainly for use when - * making specific URI template changes and wanting - * to do a full regression check. - */ - public function testAgainstStandardTests() - { - $location = __DIR__ . "/../../uritemplate-test/*.json"; - $files = glob($location); + /** + * This test test against the JSON files defined in + * https://github.com/uri-templates/uritemplate-test + * + * We don't ship these tests with it, so they'll just silently + * skip unless provided - this is mainly for use when + * making specific URI template changes and wanting + * to do a full regression check. + */ + public function testAgainstStandardTests() + { + $location = __DIR__ . "/../../uritemplate-test/*.json"; + $files = glob($location); - if (!$files) { - $this->markTestSkipped('No JSON files provided'); - } + if (!$files) { + $this->markTestSkipped('No JSON files provided'); + } - $urit = new UriTemplate(); - foreach ($files as $file) { - $test = json_decode(file_get_contents($file), true); - foreach ($test as $title => $testsets) { - foreach ($testsets['testcases'] as $cases) { - $input = $cases[0]; - $output = $cases[1]; - if ($output == false) { - continue; // skipping negative tests for now - } else if (is_array($output)) { - $response = $urit->parse($input, $testsets['variables']); - $this->assertContains( - $response, - $output, - $input . " failed from " . $title - ); - } else { - $this->assertEquals( - $output, - $urit->parse($input, $testsets['variables']), - $input . " failed." - ); - } + $urit = new UriTemplate(); + foreach ($files as $file) { + $test = json_decode(file_get_contents($file), true); + foreach ($test as $title => $testsets) { + foreach ($testsets['testcases'] as $cases) { + $input = $cases[0]; + $output = $cases[1]; + if ($output == false) { + continue; // skipping negative tests for now + } else if (is_array($output)) { + $response = $urit->parse($input, $testsets['variables']); + $this->assertContains( + $response, + $output, + $input . " failed from " . $title + ); + } else { + $this->assertEquals( + $output, + $urit->parse($input, $testsets['variables']), + $input . " failed." + ); + } + } + } } - } } - } } diff --git a/tests/examples/batchTest.php b/tests/examples/batchTest.php index 3628a11cb..bf2e5a53b 100644 --- a/tests/examples/batchTest.php +++ b/tests/examples/batchTest.php @@ -25,15 +25,15 @@ class batchTest extends BaseTest { - public function testBatch() - { - $this->checkKey(); + public function testBatch() + { + $this->checkKey(); - $crawler = $this->loadExample('batch.php'); + $crawler = $this->loadExample('batch.php'); - $nodes = $crawler->filter('br'); - $this->assertCount(20, $nodes); - $this->assertContains('Walden', $crawler->text()); - $this->assertContains('George Bernard Shaw', $crawler->text()); - } + $nodes = $crawler->filter('br'); + $this->assertCount(20, $nodes); + $this->assertContains('Walden', $crawler->text()); + $this->assertContains('George Bernard Shaw', $crawler->text()); + } } diff --git a/tests/examples/idTokenTest.php b/tests/examples/idTokenTest.php index 3f1b38834..b40531e8c 100644 --- a/tests/examples/idTokenTest.php +++ b/tests/examples/idTokenTest.php @@ -25,18 +25,18 @@ class idTokenTest extends BaseTest { - public function testIdToken() - { - $this->checkServiceAccountCredentials(); + public function testIdToken() + { + $this->checkServiceAccountCredentials(); - $crawler = $this->loadExample('idtoken.php'); + $crawler = $this->loadExample('idtoken.php'); - $nodes = $crawler->filter('h1'); - $this->assertCount(1, $nodes); - $this->assertEquals('Retrieving An Id Token', $nodes->first()->text()); + $nodes = $crawler->filter('h1'); + $this->assertCount(1, $nodes); + $this->assertEquals('Retrieving An Id Token', $nodes->first()->text()); - $nodes = $crawler->filter('a.login'); - $this->assertCount(1, $nodes); - $this->assertEquals('Connect Me!', $nodes->first()->text()); - } -} \ No newline at end of file + $nodes = $crawler->filter('a.login'); + $this->assertCount(1, $nodes); + $this->assertEquals('Connect Me!', $nodes->first()->text()); + } +} diff --git a/tests/examples/indexTest.php b/tests/examples/indexTest.php index 0ef4f4d4e..6b2e65a44 100644 --- a/tests/examples/indexTest.php +++ b/tests/examples/indexTest.php @@ -25,12 +25,12 @@ class indexTest extends BaseTest { - public function testIndex() - { - $crawler = $this->loadExample('index.php'); + public function testIndex() + { + $crawler = $this->loadExample('index.php'); - $nodes = $crawler->filter('li'); - $this->assertCount(8, $nodes); - $this->assertEquals('A query using simple API access', $nodes->first()->text()); - } + $nodes = $crawler->filter('li'); + $this->assertCount(8, $nodes); + $this->assertEquals('A query using simple API access', $nodes->first()->text()); + } } diff --git a/tests/examples/largeFileDownloadTest.php b/tests/examples/largeFileDownloadTest.php index 71286d9ac..49e3bba9a 100644 --- a/tests/examples/largeFileDownloadTest.php +++ b/tests/examples/largeFileDownloadTest.php @@ -24,36 +24,36 @@ class largeFileDownloadTest extends BaseTest { - /** + /** * @runInSeparateProcess */ - public function testSimpleFileDownloadNoToken() - { - $this->checkServiceAccountCredentials(); + public function testSimpleFileDownloadNoToken() + { + $this->checkServiceAccountCredentials(); - $crawler = $this->loadExample('large-file-download.php'); + $crawler = $this->loadExample('large-file-download.php'); - $nodes = $crawler->filter('h1'); - $this->assertCount(1, $nodes); - $this->assertEquals('File Download - Downloading a large file', $nodes->first()->text()); + $nodes = $crawler->filter('h1'); + $this->assertCount(1, $nodes); + $this->assertEquals('File Download - Downloading a large file', $nodes->first()->text()); - $nodes = $crawler->filter('a.login'); - $this->assertCount(1, $nodes); - $this->assertEquals('Connect Me!', $nodes->first()->text()); - } + $nodes = $crawler->filter('a.login'); + $this->assertCount(1, $nodes); + $this->assertEquals('Connect Me!', $nodes->first()->text()); + } - public function testSimpleFileDownloadWithToken() - { - $this->checkToken(); + public function testSimpleFileDownloadWithToken() + { + $this->checkToken(); - global $_SESSION; - $_SESSION['upload_token'] = $this->getClient()->getAccessToken(); + global $_SESSION; + $_SESSION['upload_token'] = $this->getClient()->getAccessToken(); - $crawler = $this->loadExample('large-file-download.php'); + $crawler = $this->loadExample('large-file-download.php'); - $buttonText = 'Click here to download a large (20MB) test file'; - $nodes = $crawler->filter('input'); - $this->assertCount(1, $nodes); - $this->assertEquals($buttonText, $nodes->first()->attr('value')); - } -} \ No newline at end of file + $buttonText = 'Click here to download a large (20MB) test file'; + $nodes = $crawler->filter('input'); + $this->assertCount(1, $nodes); + $this->assertEquals($buttonText, $nodes->first()->attr('value')); + } +} diff --git a/tests/examples/largeFileUploadTest.php b/tests/examples/largeFileUploadTest.php index 31e8f80d4..f1c8af4cb 100644 --- a/tests/examples/largeFileUploadTest.php +++ b/tests/examples/largeFileUploadTest.php @@ -25,21 +25,21 @@ class largeFileUploadTest extends BaseTest { - /** + /** * @runInSeparateProcess */ - public function testLargeFileUpload() - { - $this->checkServiceAccountCredentials(); + public function testLargeFileUpload() + { + $this->checkServiceAccountCredentials(); - $crawler = $this->loadExample('large-file-upload.php'); + $crawler = $this->loadExample('large-file-upload.php'); - $nodes = $crawler->filter('h1'); - $this->assertCount(1, $nodes); - $this->assertEquals('File Upload - Uploading a large file', $nodes->first()->text()); + $nodes = $crawler->filter('h1'); + $this->assertCount(1, $nodes); + $this->assertEquals('File Upload - Uploading a large file', $nodes->first()->text()); - $nodes = $crawler->filter('a.login'); - $this->assertCount(1, $nodes); - $this->assertEquals('Connect Me!', $nodes->first()->text()); - } -} \ No newline at end of file + $nodes = $crawler->filter('a.login'); + $this->assertCount(1, $nodes); + $this->assertEquals('Connect Me!', $nodes->first()->text()); + } +} diff --git a/tests/examples/multiApiTest.php b/tests/examples/multiApiTest.php index 569bb21d9..b572c31ee 100644 --- a/tests/examples/multiApiTest.php +++ b/tests/examples/multiApiTest.php @@ -25,14 +25,14 @@ class multiApiTest extends BaseTest { - public function testMultiApi() - { - $this->checkKey(); + public function testMultiApi() + { + $this->checkKey(); - $crawler = $this->loadExample('multi-api.php'); + $crawler = $this->loadExample('multi-api.php'); - $nodes = $crawler->filter('h1'); - $this->assertCount(1, $nodes); - $this->assertEquals('User Query - Multiple APIs', $nodes->first()->text()); - } -} \ No newline at end of file + $nodes = $crawler->filter('h1'); + $this->assertCount(1, $nodes); + $this->assertEquals('User Query - Multiple APIs', $nodes->first()->text()); + } +} diff --git a/tests/examples/serviceAccountTest.php b/tests/examples/serviceAccountTest.php index 60aeb0212..719057bf5 100644 --- a/tests/examples/serviceAccountTest.php +++ b/tests/examples/serviceAccountTest.php @@ -25,14 +25,14 @@ class serviceAccountTest extends BaseTest { - public function testServiceAccount() - { - $this->checkServiceAccountCredentials(); + public function testServiceAccount() + { + $this->checkServiceAccountCredentials(); - $crawler = $this->loadExample('service-account.php'); + $crawler = $this->loadExample('service-account.php'); - $nodes = $crawler->filter('br'); - $this->assertCount(10, $nodes); - $this->assertContains('Walden', $crawler->text()); - } + $nodes = $crawler->filter('br'); + $this->assertCount(10, $nodes); + $this->assertContains('Walden', $crawler->text()); + } } diff --git a/tests/examples/simpleFileUploadTest.php b/tests/examples/simpleFileUploadTest.php index 192977ab7..1942586bb 100644 --- a/tests/examples/simpleFileUploadTest.php +++ b/tests/examples/simpleFileUploadTest.php @@ -25,36 +25,36 @@ class simpleFileUploadTest extends BaseTest { - /** + /** * @runInSeparateProcess */ - public function testSimpleFileUploadNoToken() - { - $this->checkServiceAccountCredentials(); + public function testSimpleFileUploadNoToken() + { + $this->checkServiceAccountCredentials(); - $crawler = $this->loadExample('simple-file-upload.php'); + $crawler = $this->loadExample('simple-file-upload.php'); - $nodes = $crawler->filter('h1'); - $this->assertCount(1, $nodes); - $this->assertEquals('File Upload - Uploading a simple file', $nodes->first()->text()); + $nodes = $crawler->filter('h1'); + $this->assertCount(1, $nodes); + $this->assertEquals('File Upload - Uploading a simple file', $nodes->first()->text()); - $nodes = $crawler->filter('a.login'); - $this->assertCount(1, $nodes); - $this->assertEquals('Connect Me!', $nodes->first()->text()); - } + $nodes = $crawler->filter('a.login'); + $this->assertCount(1, $nodes); + $this->assertEquals('Connect Me!', $nodes->first()->text()); + } - public function testSimpleFileUploadWithToken() - { - $this->checkToken(); + public function testSimpleFileUploadWithToken() + { + $this->checkToken(); - global $_SESSION; - $_SESSION['upload_token'] = $this->getClient()->getAccessToken(); + global $_SESSION; + $_SESSION['upload_token'] = $this->getClient()->getAccessToken(); - $crawler = $this->loadExample('simple-file-upload.php'); + $crawler = $this->loadExample('simple-file-upload.php'); - $buttonText = 'Click here to upload two small (1MB) test files'; - $nodes = $crawler->filter('input'); - $this->assertCount(1, $nodes); - $this->assertEquals($buttonText, $nodes->first()->attr('value')); - } -} \ No newline at end of file + $buttonText = 'Click here to upload two small (1MB) test files'; + $nodes = $crawler->filter('input'); + $this->assertCount(1, $nodes); + $this->assertEquals($buttonText, $nodes->first()->attr('value')); + } +} diff --git a/tests/examples/simpleQueryTest.php b/tests/examples/simpleQueryTest.php index cbb49fd9d..8bc7eac7d 100644 --- a/tests/examples/simpleQueryTest.php +++ b/tests/examples/simpleQueryTest.php @@ -25,17 +25,17 @@ class simpleQueryTest extends BaseTest { - public function testSimpleQuery() - { - $this->checkKey(); + public function testSimpleQuery() + { + $this->checkKey(); - $crawler = $this->loadExample('simple-query.php'); + $crawler = $this->loadExample('simple-query.php'); - $nodes = $crawler->filter('br'); - $this->assertCount(20, $nodes); + $nodes = $crawler->filter('br'); + $this->assertCount(20, $nodes); - $nodes = $crawler->filter('h1'); - $this->assertCount(1, $nodes); - $this->assertEquals('Simple API Access', $nodes->first()->text()); - } + $nodes = $crawler->filter('h1'); + $this->assertCount(1, $nodes); + $this->assertEquals('Simple API Access', $nodes->first()->text()); + } } From 8be21867aaa245b691ca763aaf7f657edffb4d7a Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 21 Apr 2022 13:12:10 -0600 Subject: [PATCH 227/301] chore: add phpstan and fix resulting issues (#2252) --- .github/workflows/tests.yml | 14 ++++++++ phpstan.neon.dist | 8 +++++ src/AccessToken/Verify.php | 13 ++++++-- src/AuthHandler/AuthHandlerFactory.php | 1 + src/Client.php | 41 ++++++++++++----------- src/Collection.php | 5 ++- src/Http/Batch.php | 12 +++---- src/Http/MediaFileUpload.php | 45 ++++++++++++-------------- src/Http/REST.php | 11 ++++--- src/Service/Exception.php | 6 ++-- src/Service/Resource.php | 10 +++--- src/Task/Runner.php | 4 +-- src/Utils/UriTemplate.php | 4 +-- src/aliases.php | 1 + 14 files changed, 108 insertions(+), 67 deletions(-) create mode 100644 phpstan.neon.dist diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 58cd04d21..4a2b8fced 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -54,4 +54,18 @@ jobs: command: composer install - name: Run Script run: vendor/bin/phpcs src tests examples --standard=phpcs.xml.dist -nps + staticanalysis: + runs-on: ubuntu-latest + name: PHPStan Static Analysis + steps: + - uses: actions/checkout@v2 + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.0' + - name: Run Script + run: | + composer install + composer global require phpstan/phpstan + ~/.composer/vendor/bin/phpstan analyse diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 000000000..a70dd0e9a --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,8 @@ +parameters: + treatPhpDocTypesAsCertain: false + level: 5 + paths: + - src + excludePaths: + - src/AuthHandler/Guzzle5AuthHandler.php + diff --git a/src/AccessToken/Verify.php b/src/AccessToken/Verify.php index 6542ab23e..388a57318 100644 --- a/src/AccessToken/Verify.php +++ b/src/AccessToken/Verify.php @@ -55,6 +55,11 @@ class Verify */ private $cache; + /** + * @var \Firebase\JWT\JWT + */ + public $jwt; + /** * Instantiates the class, but does not initiate the login flow, leaving it * to the discretion of the caller. @@ -85,7 +90,7 @@ public function __construct( * * @param string $idToken the ID token in JWT format * @param string $audience Optional. The audience to verify against JWt "aud" - * @return array the token payload, if successful + * @return array|false the token payload, if successful */ public function verifyIdToken($idToken, $audience = null) { @@ -124,7 +129,7 @@ public function verifyIdToken($idToken, $audience = null) } return (array) $payload; - } catch (ExpiredException $e) { + } catch (ExpiredException $e) { // @phpstan-ignore-line return false; } catch (ExpiredExceptionV3 $e) { return false; @@ -146,7 +151,7 @@ private function getCache() /** * Retrieve and cache a certificates file. * - * @param $url string location + * @param string $url location * @throws \Google\Exception * @return array certificates */ @@ -164,6 +169,7 @@ private function retrieveCertsFromLocation($url) return json_decode($file, true); } + // @phpstan-ignore-next-line $response = $this->http->get($url); if ($response->getStatusCode() == 200) { @@ -224,6 +230,7 @@ private function getJwtService() $jwtClass::$leeway = 1; } + // @phpstan-ignore-next-line return new $jwtClass; } diff --git a/src/AuthHandler/AuthHandlerFactory.php b/src/AuthHandler/AuthHandlerFactory.php index 65510440f..6bb6328d3 100644 --- a/src/AuthHandler/AuthHandlerFactory.php +++ b/src/AuthHandler/AuthHandlerFactory.php @@ -35,6 +35,7 @@ public static function build($cache = null, array $cacheConfig = []) if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) { $guzzleVersion = ClientInterface::MAJOR_VERSION; } elseif (defined('\GuzzleHttp\ClientInterface::VERSION')) { + // @phpstan-ignore-next-line $guzzleVersion = (int) substr(ClientInterface::VERSION, 0, 1); } diff --git a/src/Client.php b/src/Client.php index 0e95260e9..bfe023049 100644 --- a/src/Client.php +++ b/src/Client.php @@ -43,6 +43,7 @@ use DomainException; use InvalidArgumentException; use LogicException; +use UnexpectedValueException; /** * The Google API Client @@ -58,7 +59,7 @@ class Client const API_BASE_PATH = 'https://www.googleapis.com/'; /** - * @var OAuth2 $auth + * @var ?OAuth2 $auth */ private $auth; @@ -68,7 +69,7 @@ class Client private $http; /** - * @var CacheItemPoolInterface $cache + * @var ?CacheItemPoolInterface $cache */ private $cache; @@ -83,12 +84,12 @@ class Client private $config; /** - * @var LoggerInterface $logger + * @var ?LoggerInterface $logger */ private $logger; /** - * @var CredentialsLoader $credentials + * @var ?CredentialsLoader $credentials */ private $credentials; @@ -225,7 +226,7 @@ public function getLibraryVersion() * For backwards compatibility * alias for fetchAccessTokenWithAuthCode * - * @param $code string code from accounts.google.com + * @param string $code string code from accounts.google.com * @return array access token * @deprecated */ @@ -238,7 +239,7 @@ public function authenticate($code) * Attempt to exchange a code for an valid authentication token. * Helper wrapped around the OAuth 2.0 implementation. * - * @param $code string code from accounts.google.com + * @param string $code code from accounts.google.com * @return array access token */ public function fetchAccessTokenWithAuthCode($code) @@ -714,7 +715,7 @@ public function setDeveloperKey($developerKey) * Set the hd (hosted domain) parameter streamlines the login process for * Google Apps hosted accounts. By including the domain of the user, you * restrict sign-in to accounts at that domain. - * @param $hd string - the domain to use. + * @param string $hd the domain to use. */ public function setHostedDomain($hd) { @@ -725,7 +726,7 @@ public function setHostedDomain($hd) * Set the prompt hint. Valid values are none, consent and select_account. * If no value is specified and the user has not previously authorized * access, then the user is shown a consent screen. - * @param $prompt string + * @param string $prompt * {@code "none"} Do not display any authentication or consent screens. Must not be specified with other values. * {@code "consent"} Prompt the user for consent. * {@code "select_account"} Prompt the user to select an account. @@ -739,7 +740,7 @@ public function setPrompt($prompt) * openid.realm is a parameter from the OpenID 2.0 protocol, not from OAuth * 2.0. It is used in OpenID 2.0 requests to signify the URL-space for which * an authentication request is valid. - * @param $realm string - the URL-space to use. + * @param string $realm the URL-space to use. */ public function setOpenidRealm($realm) { @@ -750,7 +751,7 @@ public function setOpenidRealm($realm) * If this is provided with the value true, and the authorization request is * granted, the authorization will include any previous authorizations * granted to this user/application combination for other scopes. - * @param $include boolean - the URL-space to use. + * @param bool $include the URL-space to use. */ public function setIncludeGrantedScopes($include) { @@ -834,7 +835,7 @@ public function setScopes($scope_or_scopes) * Will append any scopes not previously requested to the scope parameter. * A single string will be treated as a scope to request. An array of strings * will each be appended. - * @param $scope_or_scopes string|array e.g. "profile" + * @param string|string[] $scope_or_scopes e.g. "profile" */ public function addScope($scope_or_scopes) { @@ -873,10 +874,11 @@ public function prepareScopes() /** * Helper method to execute deferred HTTP requests. * - * @param $request RequestInterface|\Google\Http\Batch - * @param string $expectedClass + * @template T + * @param RequestInterface $request + * @param class-string $expectedClass * @throws \Google\Exception - * @return mixed|$expectedClass|ResponseInterface + * @return mixed|T|ResponseInterface */ public function execute(RequestInterface $request, $expectedClass = null) { @@ -902,7 +904,7 @@ public function execute(RequestInterface $request, $expectedClass = null) if ($this->config['api_format_v2']) { $request = $request->withHeader( 'X-GOOG-API-FORMAT-VERSION', - 2 + '2' ); } @@ -1182,6 +1184,7 @@ protected function createDefaultHttpClient() if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) { $guzzleVersion = ClientInterface::MAJOR_VERSION; } elseif (defined('\GuzzleHttp\ClientInterface::VERSION')) { + // @phpstan-ignore-next-line $guzzleVersion = (int)substr(ClientInterface::VERSION, 0, 1); } @@ -1191,9 +1194,11 @@ protected function createDefaultHttpClient() 'defaults' => ['exceptions' => false], ]; if ($this->isAppEngine()) { - // set StreamHandler on AppEngine by default - $options['handler'] = new StreamHandler(); - $options['defaults']['verify'] = '/etc/ca-certificates.crt'; + if (class_exists(StreamHandler::class)) { + // set StreamHandler on AppEngine by default + $options['handler'] = new StreamHandler(); + $options['defaults']['verify'] = '/etc/ca-certificates.crt'; + } } } elseif (6 === $guzzleVersion || 7 === $guzzleVersion) { // guzzle 6 or 7 diff --git a/src/Collection.php b/src/Collection.php index 39ebccaa7..c164c12a2 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -45,7 +45,7 @@ public function key() } } - /** @return void */ + /** @return mixed */ #[\ReturnTypeWillChange] public function next() { @@ -71,6 +71,7 @@ public function count() } /** @return bool */ + #[\ReturnTypeWillChange] public function offsetExists($offset) { if (!is_numeric($offset)) { @@ -90,6 +91,7 @@ public function offsetGet($offset) } /** @return void */ + #[\ReturnTypeWillChange] public function offsetSet($offset, $value) { if (!is_numeric($offset)) { @@ -99,6 +101,7 @@ public function offsetSet($offset, $value) } /** @return void */ + #[\ReturnTypeWillChange] public function offsetUnset($offset) { if (!is_numeric($offset)) { diff --git a/src/Http/Batch.php b/src/Http/Batch.php index eb797582e..954e84dfe 100644 --- a/src/Http/Batch.php +++ b/src/Http/Batch.php @@ -93,7 +93,7 @@ public function execute() EOF; - /** @var RequestInterface $req */ + /** @var RequestInterface $request */ foreach ($this->requests as $key => $request) { $firstLine = sprintf( '%s %s HTTP/%s', @@ -126,7 +126,7 @@ public function execute() $url = $this->rootUrl . '/' . $this->batchPath; $headers = array( 'Content-Type' => sprintf('multipart/mixed; boundary=%s', $this->boundary), - 'Content-Length' => strlen($body), + 'Content-Length' => (string) strlen($body), ); $request = new Request( @@ -170,9 +170,9 @@ public function parseResponse(ResponseInterface $response, $classes = array()) $status = explode(" ", $status); $status = $status[1]; - list($partHeaders, $partBody) = $this->parseHttpResponse($part, false); + list($partHeaders, $partBody) = $this->parseHttpResponse($part, 0); $response = new Response( - $status, + (int) $status, $partHeaders, Psr7\Utils::streamFor($partBody) ); @@ -219,8 +219,8 @@ private function parseRawHeaders($rawHeaders) /** * Used by the IO lib and also the batch processing. * - * @param $respData - * @param $headerSize + * @param string $respData + * @param int $headerSize * @return array */ private function parseHttpResponse($respData, $headerSize) diff --git a/src/Http/MediaFileUpload.php b/src/Http/MediaFileUpload.php index edb73ab93..de1a40895 100644 --- a/src/Http/MediaFileUpload.php +++ b/src/Http/MediaFileUpload.php @@ -63,7 +63,7 @@ class MediaFileUpload private $request; /** @var string */ - private $boundary; + private $boundary; // @phpstan-ignore-line /** * Result code from last HTTP call @@ -77,7 +77,7 @@ class MediaFileUpload * @param string $mimeType * @param string $data The bytes you want to upload. * @param bool $resumable - * @param bool $chunkSize File will be uploaded in chunks of this many bytes. + * @param int $chunkSize File will be uploaded in chunks of this many bytes. * only used if resumable=True */ public function __construct( @@ -86,7 +86,7 @@ public function __construct( $mimeType, $data, $resumable = false, - $chunkSize = false + $chunkSize = 0 ) { $this->client = $client; $this->request = $request; @@ -101,7 +101,7 @@ public function __construct( /** * Set the size of the file that is being uploaded. - * @param $size - int file size in bytes + * @param int $size - int file size in bytes */ public function setFileSize($size) { @@ -133,7 +133,7 @@ public function nextChunk($chunk = false) $lastBytePos = $this->progress + strlen($chunk) - 1; $headers = array( 'content-range' => "bytes $this->progress-$lastBytePos/$this->size", - 'content-length' => strlen($chunk), + 'content-length' => (string) strlen($chunk), 'expect' => '', ); @@ -175,7 +175,7 @@ private function makePutRequest(RequestInterface $request) $range = $response->getHeaderLine('range'); if ($range) { $range_array = explode('-', $range); - $this->progress = $range_array[1] + 1; + $this->progress = ((int) $range_array[1]) + 1; } // Allow for changing upload URLs. @@ -193,14 +193,14 @@ private function makePutRequest(RequestInterface $request) /** * Resume a previously unfinished upload - * @param $resumeUri the resume-URI of the unfinished, resumable upload. + * @param string $resumeUri the resume-URI of the unfinished, resumable upload. */ public function resume($resumeUri) { $this->resumeUri = $resumeUri; $headers = array( 'content-range' => "bytes */$this->size", - 'content-length' => 0, + 'content-length' => '0', ); $httpRequest = new Request( 'PUT', @@ -222,8 +222,7 @@ private function process() $postBody = ''; $contentType = false; - $meta = (string) $request->getBody(); - $meta = is_string($meta) ? json_decode($meta, true) : $meta; + $meta = json_decode((string) $request->getBody(), true); $uploadType = $this->getUploadType($meta); $request = $request->withUri( @@ -256,7 +255,7 @@ private function process() $request = $request->withBody(Psr7\Utils::streamFor($postBody)); - if (isset($contentType) && $contentType) { + if ($contentType) { $request = $request->withHeader('content-type', $contentType); } @@ -268,7 +267,7 @@ private function process() * - resumable (UPLOAD_RESUMABLE_TYPE) * - media (UPLOAD_MEDIA_TYPE) * - multipart (UPLOAD_MULTIPART_TYPE) - * @param $meta + * @param string|false $meta * @return string * @visible for testing */ @@ -297,20 +296,18 @@ public function getResumeUri() private function fetchResumeUri() { $body = $this->request->getBody(); - if ($body) { - $headers = array( - 'content-type' => 'application/json; charset=UTF-8', - 'content-length' => $body->getSize(), - 'x-upload-content-type' => $this->mimeType, - 'x-upload-content-length' => $this->size, - 'expect' => '', - ); - foreach ($headers as $key => $value) { - $this->request = $this->request->withHeader($key, $value); - } + $headers = array( + 'content-type' => 'application/json; charset=UTF-8', + 'content-length' => $body->getSize(), + 'x-upload-content-type' => $this->mimeType, + 'x-upload-content-length' => $this->size, + 'expect' => '', + ); + foreach ($headers as $key => $value) { + $this->request = $this->request->withHeader($key, $value); } - $response = $this->client->execute($this->request, false); + $response = $this->client->execute($this->request, null); $location = $response->getHeaderLine('location'); $code = $response->getStatusCode(); diff --git a/src/Http/REST.php b/src/Http/REST.php index 908481689..0bcead157 100644 --- a/src/Http/REST.php +++ b/src/Http/REST.php @@ -36,8 +36,8 @@ class REST * Executes a Psr\Http\Message\RequestInterface and (if applicable) automatically retries * when errors occur. * - * @param Client $client - * @param RequestInterface $req + * @param ClientInterface $client + * @param RequestInterface $request * @param string $expectedClass * @param array $config * @param array $retryMap @@ -69,7 +69,7 @@ public static function execute( /** * Executes a Psr\Http\Message\RequestInterface * - * @param Client $client + * @param ClientInterface $client * @param RequestInterface $request * @param string $expectedClass * @return array decoded result @@ -89,7 +89,10 @@ public static function doExecute(ClientInterface $client, RequestInterface $requ $response = $e->getResponse(); // specific checking for Guzzle 5: convert to PSR7 response - if ($response instanceof \GuzzleHttp\Message\ResponseInterface) { + if ( + interface_exists('\GuzzleHttp\Message\ResponseInterface') + && $response instanceof \GuzzleHttp\Message\ResponseInterface + ) { $response = new Response( $response->getStatusCode(), $response->getHeaders() ?: [], diff --git a/src/Service/Exception.php b/src/Service/Exception.php index bffdeaca9..caec08c07 100644 --- a/src/Service/Exception.php +++ b/src/Service/Exception.php @@ -32,8 +32,8 @@ class Exception extends GoogleException * * @param string $message * @param int $code - * @param \Exception|null $previous - * @param [{string, string}] errors List of errors returned in an HTTP + * @param Exception|null $previous + * @param array $errors List of errors returned in an HTTP * response. Defaults to []. */ public function __construct( @@ -62,7 +62,7 @@ public function __construct( * "location": "Authorization", * } * - * @return [{string, string}] List of errors return in an HTTP response or []. + * @return array List of errors return in an HTTP response or []. */ public function getErrors() { diff --git a/src/Service/Resource.php b/src/Service/Resource.php index c255a2f5a..3afe957ae 100644 --- a/src/Service/Resource.php +++ b/src/Service/Resource.php @@ -77,10 +77,12 @@ public function __construct($service, $serviceName, $resourceName, $resource) /** * TODO: This function needs simplifying. - * @param $name - * @param $arguments - * @param $expectedClass - optional, the expected class name - * @return mixed|$expectedClass|ResponseInterface|RequestInterface + * + * @template T + * @param string $name + * @param array $arguments + * @param class-string $expectedClass - optional, the expected class name + * @return mixed|T|ResponseInterface|RequestInterface * @throws \Google\Exception */ public function call($name, $arguments, $expectedClass = null) diff --git a/src/Task/Runner.php b/src/Task/Runner.php index becc78b08..8f74559dc 100644 --- a/src/Task/Runner.php +++ b/src/Task/Runner.php @@ -98,7 +98,7 @@ class Runner * @param array $arguments The task arguments * @throws \Google\Task\Exception when misconfigured */ - public function __construct( + public function __construct( // @phpstan-ignore-line $config, $name, $action, @@ -241,7 +241,7 @@ private function backOff() /** * Gets the delay (in seconds) for the current backoff period. * - * @return float + * @return int */ private function getDelay() { diff --git a/src/Utils/UriTemplate.php b/src/Utils/UriTemplate.php index c74814d25..da82d3fd2 100644 --- a/src/Utils/UriTemplate.php +++ b/src/Utils/UriTemplate.php @@ -28,7 +28,7 @@ class UriTemplate const TYPE_SCALAR = "4"; /** - * @var $operators array + * @var array $operators * These are valid at the start of a template block to * modify the way in which the variables inside are * processed. @@ -44,7 +44,7 @@ class UriTemplate ); /** - * @var reserved array + * @var array * These are the characters which should not be URL encoded in reserved * strings. */ diff --git a/src/aliases.php b/src/aliases.php index 04154743c..525d09164 100644 --- a/src/aliases.php +++ b/src/aliases.php @@ -41,6 +41,7 @@ class Google_Task_Composer extends \Google\Task\Composer { } +/** @phpstan-ignore-next-line */ if (\false) { class Google_AccessToken_Revoke extends \Google\AccessToken\Revoke {} class Google_AccessToken_Verify extends \Google\AccessToken\Verify {} From ea2d79cc006ed19dc7a148792b978bc16d1622d1 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 22 Apr 2022 13:24:34 -0600 Subject: [PATCH 228/301] chore: misc code style fixes (#2253) --- examples/batch.php | 2 +- examples/multi-api.php | 6 +- examples/service-account.php | 4 +- examples/simple-file-upload.php | 8 +- examples/simple-query.php | 8 +- phpcs.xml.dist | 3 + src/AccessToken/Revoke.php | 2 +- src/AccessToken/Verify.php | 26 ++-- src/AuthHandler/AuthHandlerFactory.php | 3 +- src/AuthHandler/Guzzle5AuthHandler.php | 2 +- src/AuthHandler/Guzzle6AuthHandler.php | 2 +- src/Client.php | 52 ++++--- src/Http/Batch.php | 23 ++- src/Http/MediaFileUpload.php | 17 ++- src/Http/REST.php | 9 +- src/Model.php | 20 +-- src/Service/Exception.php | 4 +- src/Service/Resource.php | 56 ++++---- src/Task/Composer.php | 2 +- src/Task/Runner.php | 7 +- src/Utils/UriTemplate.php | 47 +++---- src/aliases.php | 80 ++++++++--- tests/BaseTest.php | 2 +- tests/Google/ClientTest.php | 18 +-- tests/Google/Http/MediaFileUploadTest.php | 2 +- tests/Google/Http/RESTTest.php | 14 +- tests/Google/ModelTest.php | 4 +- tests/Google/Service/AdSenseTest.php | 12 +- tests/Google/Service/ResourceTest.php | 146 +++++++++---------- tests/Google/Service/YouTubeTest.php | 4 +- tests/Google/ServiceTest.php | 38 +++-- tests/Google/Task/RunnerTest.php | 162 +++++++++++----------- tests/Google/Utils/UriTemplateTest.php | 70 +++++----- 33 files changed, 444 insertions(+), 411 deletions(-) diff --git a/examples/batch.php b/examples/batch.php index a8377b584..d9b359c35 100644 --- a/examples/batch.php +++ b/examples/batch.php @@ -63,7 +63,7 @@ $batch = $service->createBatch(); $query = 'Henry David Thoreau'; -$optParams = array('filter' => 'free-ebooks'); +$optParams = ['filter' => 'free-ebooks']; $req1 = $service->volumes->listVolumes($query, $optParams); $batch->add($req1, "thoreau"); $query = 'George Bernard Shaw'; diff --git a/examples/multi-api.php b/examples/multi-api.php index a877f7ffc..4c15bdcf5 100644 --- a/examples/multi-api.php +++ b/examples/multi-api.php @@ -88,13 +88,13 @@ if ($client->getAccessToken()) { $_SESSION['multi-api-token'] = $client->getAccessToken(); - $dr_results = $dr_service->files->listFiles(array('pageSize' => 10)); + $dr_results = $dr_service->files->listFiles(['pageSize' => 10]); - $yt_channels = $yt_service->channels->listChannels('contentDetails', array("mine" => true)); + $yt_channels = $yt_service->channels->listChannels('contentDetails', ["mine" => true]); $likePlaylist = $yt_channels[0]->contentDetails->relatedPlaylists->likes; $yt_results = $yt_service->playlistItems->listPlaylistItems( "snippet", - array("playlistId" => $likePlaylist) + ["playlistId" => $likePlaylist] ); } ?> diff --git a/examples/service-account.php b/examples/service-account.php index 6cbe0bbf6..6134ae7d0 100644 --- a/examples/service-account.php +++ b/examples/service-account.php @@ -60,9 +60,9 @@ simple query as an example. ************************************************/ $query = 'Henry David Thoreau'; -$optParams = array( +$optParams = [ 'filter' => 'free-ebooks', -); +]; $results = $service->volumes->listVolumes($query, $optParams); ?> diff --git a/examples/simple-file-upload.php b/examples/simple-file-upload.php index cca5efd7e..a66027270 100644 --- a/examples/simple-file-upload.php +++ b/examples/simple-file-upload.php @@ -91,11 +91,11 @@ $file = new Google\Service\Drive\DriveFile(); $result = $service->files->create( $file, - array( + [ 'data' => file_get_contents(TESTFILE), 'mimeType' => 'application/octet-stream', 'uploadType' => 'media' - ) + ] ); // Now lets try and send the metadata as well using multipart! @@ -103,11 +103,11 @@ $file->setName("Hello World!"); $result2 = $service->files->create( $file, - array( + [ 'data' => file_get_contents(TESTFILE), 'mimeType' => 'application/octet-stream', 'uploadType' => 'multipart' - ) + ] ); } ?> diff --git a/examples/simple-query.php b/examples/simple-query.php index 4195e5b12..8112cc32c 100644 --- a/examples/simple-query.php +++ b/examples/simple-query.php @@ -48,9 +48,9 @@ parameters. ************************************************/ $query = 'Henry David Thoreau'; -$optParams = array( +$optParams = [ 'filter' => 'free-ebooks', -); +]; $results = $service->volumes->listVolumes($query, $optParams); /************************************************ @@ -58,9 +58,9 @@ ***********************************************/ $client->setDefer(true); $query = 'Henry David Thoreau'; -$optParams = array( +$optParams = [ 'filter' => 'free-ebooks', -); +]; $request = $service->volumes->listVolumes($query, $optParams); $resultsDeferred = $client->execute($request); diff --git a/phpcs.xml.dist b/phpcs.xml.dist index c18eadccd..c508de9c9 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -64,6 +64,9 @@ 0
    + + + diff --git a/src/AccessToken/Revoke.php b/src/AccessToken/Revoke.php index bcccc9b47..dd94fe0f2 100644 --- a/src/AccessToken/Revoke.php +++ b/src/AccessToken/Revoke.php @@ -61,7 +61,7 @@ public function revokeToken($token) } } - $body = Psr7\Utils::streamFor(http_build_query(array('token' => $token))); + $body = Psr7\Utils::streamFor(http_build_query(['token' => $token])); $request = new Request( 'POST', Client::OAUTH2_REVOKE_URI, diff --git a/src/AccessToken/Verify.php b/src/AccessToken/Verify.php index 388a57318..8b46ab1c8 100644 --- a/src/AccessToken/Verify.php +++ b/src/AccessToken/Verify.php @@ -18,22 +18,22 @@ namespace Google\AccessToken; +use DateTime; +use DomainException; +use Exception; +use ExpiredException; use Firebase\JWT\ExpiredException as ExpiredExceptionV3; -use Firebase\JWT\SignatureInvalidException; use Firebase\JWT\Key; +use Firebase\JWT\SignatureInvalidException; +use Google\Auth\Cache\MemoryCacheItemPool; +use Google\Exception as GoogleException; use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; use InvalidArgumentException; +use LogicException; use phpseclib3\Crypt\PublicKeyLoader; -use phpseclib3\Crypt\RSA\PublicKey; +use phpseclib3\Crypt\RSA\PublicKey; // Firebase v2 use Psr\Cache\CacheItemPoolInterface; -use Google\Auth\Cache\MemoryCacheItemPool; -use Google\Exception as GoogleException; -use DateTime; -use DomainException; -use Exception; -use ExpiredException; // Firebase v2 -use LogicException; /** * Wrapper around Google Access Tokens which provides convenience functions @@ -74,7 +74,7 @@ public function __construct( } if (null === $cache) { - $cache = new MemoryCacheItemPool; + $cache = new MemoryCacheItemPool(); } $this->http = $http; @@ -123,7 +123,7 @@ public function verifyIdToken($idToken, $audience = null) // support HTTP and HTTPS issuers // @see https://developers.google.com/identity/sign-in/web/backend-auth - $issuers = array(self::OAUTH2_ISSUER, self::OAUTH2_ISSUER_HTTPS); + $issuers = [self::OAUTH2_ISSUER, self::OAUTH2_ISSUER_HTTPS]; if (!isset($payload->iss) || !in_array($payload->iss, $issuers)) { return false; } @@ -231,7 +231,7 @@ private function getJwtService() } // @phpstan-ignore-next-line - return new $jwtClass; + return new $jwtClass(); } private function getPublicKey($cert) @@ -239,7 +239,7 @@ private function getPublicKey($cert) $bigIntClass = $this->getBigIntClass(); $modulus = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['n']), 256); $exponent = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['e']), 256); - $component = array('n' => $modulus, 'e' => $exponent); + $component = ['n' => $modulus, 'e' => $exponent]; if (class_exists('phpseclib3\Crypt\RSA\PublicKey')) { /** @var PublicKey $loader */ diff --git a/src/AuthHandler/AuthHandlerFactory.php b/src/AuthHandler/AuthHandlerFactory.php index 6bb6328d3..67f6fc145 100644 --- a/src/AuthHandler/AuthHandlerFactory.php +++ b/src/AuthHandler/AuthHandlerFactory.php @@ -17,9 +17,8 @@ namespace Google\AuthHandler; -use GuzzleHttp\Client; -use GuzzleHttp\ClientInterface; use Exception; +use GuzzleHttp\ClientInterface; class AuthHandlerFactory { diff --git a/src/AuthHandler/Guzzle5AuthHandler.php b/src/AuthHandler/Guzzle5AuthHandler.php index f2767036f..f8a76f0aa 100644 --- a/src/AuthHandler/Guzzle5AuthHandler.php +++ b/src/AuthHandler/Guzzle5AuthHandler.php @@ -3,8 +3,8 @@ namespace Google\AuthHandler; use Google\Auth\CredentialsLoader; -use Google\Auth\HttpHandler\HttpHandlerFactory; use Google\Auth\FetchAuthTokenCache; +use Google\Auth\HttpHandler\HttpHandlerFactory; use Google\Auth\Subscriber\AuthTokenSubscriber; use Google\Auth\Subscriber\ScopedAccessTokenSubscriber; use Google\Auth\Subscriber\SimpleSubscriber; diff --git a/src/AuthHandler/Guzzle6AuthHandler.php b/src/AuthHandler/Guzzle6AuthHandler.php index 560070724..13f9ee59d 100644 --- a/src/AuthHandler/Guzzle6AuthHandler.php +++ b/src/AuthHandler/Guzzle6AuthHandler.php @@ -3,8 +3,8 @@ namespace Google\AuthHandler; use Google\Auth\CredentialsLoader; -use Google\Auth\HttpHandler\HttpHandlerFactory; use Google\Auth\FetchAuthTokenCache; +use Google\Auth\HttpHandler\HttpHandlerFactory; use Google\Auth\Middleware\AuthTokenMiddleware; use Google\Auth\Middleware\ScopedAccessTokenMiddleware; use Google\Auth\Middleware\SimpleMiddleware; diff --git a/src/Client.php b/src/Client.php index bfe023049..20f70044f 100644 --- a/src/Client.php +++ b/src/Client.php @@ -17,32 +17,32 @@ namespace Google; +use BadMethodCallException; +use DomainException; use Google\AccessToken\Revoke; use Google\AccessToken\Verify; use Google\Auth\ApplicationDefaultCredentials; use Google\Auth\Cache\MemoryCacheItemPool; +use Google\Auth\Credentials\ServiceAccountCredentials; +use Google\Auth\Credentials\UserRefreshCredentials; use Google\Auth\CredentialsLoader; use Google\Auth\FetchAuthTokenCache; use Google\Auth\HttpHandler\HttpHandlerFactory; use Google\Auth\OAuth2; -use Google\Auth\Credentials\ServiceAccountCredentials; -use Google\Auth\Credentials\UserRefreshCredentials; use Google\AuthHandler\AuthHandlerFactory; use Google\Http\REST; use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\ClientInterface; use GuzzleHttp\Ring\Client\StreamHandler; +use InvalidArgumentException; +use LogicException; +use Monolog\Handler\StreamHandler as MonologStreamHandler; +use Monolog\Handler\SyslogHandler as MonologSyslogHandler; +use Monolog\Logger; use Psr\Cache\CacheItemPoolInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Log\LoggerInterface; -use Monolog\Logger; -use Monolog\Handler\StreamHandler as MonologStreamHandler; -use Monolog\Handler\SyslogHandler as MonologSyslogHandler; -use BadMethodCallException; -use DomainException; -use InvalidArgumentException; -use LogicException; use UnexpectedValueException; /** @@ -107,7 +107,7 @@ class Client * * @param array $config */ - public function __construct(array $config = array()) + public function __construct(array $config = []) { $this->config = array_merge([ 'application_name' => '', @@ -157,7 +157,7 @@ public function __construct(array $config = array()) // Task Runner retry configuration // @see Google\Task\Runner - 'retry' => array(), + 'retry' => [], 'retry_map' => null, // Cache class implementing Psr\Cache\CacheItemPoolInterface. @@ -185,7 +185,7 @@ public function __construct(array $config = array()) } else { $this->setAuthConfig($this->config['credentials']); } - unset($this->config['credentials']); + unset($this->config['credentials']); } if (!is_null($this->config['scopes'])) { @@ -446,11 +446,11 @@ public function authorize(ClientInterface $http = null) $scopes, $token['refresh_token'] ); - return $authHandler->attachCredentials( - $http, - $credentials, - $this->config['token_callback'] - ); + return $authHandler->attachCredentials( + $http, + $credentials, + $this->config['token_callback'] + ); } return $authHandler->attachToken($http, $token, (array) $scopes); @@ -508,9 +508,9 @@ public function setAccessToken($token) $token = $json; } else { // assume $token is just the token string - $token = array( + $token = [ 'access_token' => $token, - ); + ]; } } if ($token == null) { @@ -826,7 +826,7 @@ public function verifyIdToken($idToken = null) */ public function setScopes($scope_or_scopes) { - $this->requestedScopes = array(); + $this->requestedScopes = []; $this->addScope($scope_or_scopes); } @@ -1142,7 +1142,7 @@ protected function createDefaultLogger() protected function createDefaultCache() { - return new MemoryCacheItemPool; + return new MemoryCacheItemPool(); } /** @@ -1224,13 +1224,13 @@ private function createApplicationDefaultCredentials() // create credentials using values supplied in setAuthConfig if ($signingKey) { - $serviceAccountCredentials = array( + $serviceAccountCredentials = [ 'client_id' => $this->config['client_id'], 'client_email' => $this->config['client_email'], 'private_key' => $signingKey, 'type' => 'service_account', 'quota_project_id' => $this->config['quota_project'], - ); + ]; $credentials = CredentialsLoader::makeCredentials( $scopes, $serviceAccountCredentials @@ -1284,13 +1284,11 @@ protected function getAuthHandler() private function createUserRefreshCredentials($scope, $refreshToken) { - $creds = array_filter( - array( + $creds = array_filter([ 'client_id' => $this->getClientId(), 'client_secret' => $this->getClientSecret(), 'refresh_token' => $refreshToken, - ) - ); + ]); return new UserRefreshCredentials($scope, $creds); } diff --git a/src/Http/Batch.php b/src/Http/Batch.php index 954e84dfe..f37045c01 100644 --- a/src/Http/Batch.php +++ b/src/Http/Batch.php @@ -18,7 +18,6 @@ namespace Google\Http; use Google\Client; -use Google\Http\REST; use Google\Service\Exception as GoogleServiceException; use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\Request; @@ -37,16 +36,16 @@ class Batch { const BATCH_PATH = 'batch'; - private static $CONNECTION_ESTABLISHED_HEADERS = array( + private static $CONNECTION_ESTABLISHED_HEADERS = [ "HTTP/1.0 200 Connection established\r\n\r\n", "HTTP/1.1 200 Connection established\r\n\r\n", - ); + ]; /** @var string Multipart Boundary. */ private $boundary; /** @var array service requests to be executed. */ - private $requests = array(); + private $requests = []; /** @var Client */ private $client; @@ -79,7 +78,7 @@ public function add(RequestInterface $request, $key = false) public function execute() { $body = ''; - $classes = array(); + $classes = []; $batchHttpTemplate = <<getHeaderLine('X-Php-Expected-Class'); @@ -124,10 +123,10 @@ public function execute() $body .= "--{$this->boundary}--"; $body = trim($body); $url = $this->rootUrl . '/' . $this->batchPath; - $headers = array( + $headers = [ 'Content-Type' => sprintf('multipart/mixed; boundary=%s', $this->boundary), 'Content-Length' => (string) strlen($body), - ); + ]; $request = new Request( 'POST', @@ -141,7 +140,7 @@ public function execute() return $this->parseResponse($response, $classes); } - public function parseResponse(ResponseInterface $response, $classes = array()) + public function parseResponse(ResponseInterface $response, $classes = []) { $contentType = $response->getHeaderLine('content-type'); $contentType = explode(';', $contentType); @@ -157,7 +156,7 @@ public function parseResponse(ResponseInterface $response, $classes = array()) if (!empty($body)) { $body = str_replace("--$boundary--", "--$boundary", $body); $parts = explode("--$boundary", $body); - $responses = array(); + $responses = []; $requests = array_values($this->requests); foreach ($parts as $i => $part) { @@ -200,7 +199,7 @@ public function parseResponse(ResponseInterface $response, $classes = array()) private function parseRawHeaders($rawHeaders) { - $headers = array(); + $headers = []; $responseHeaderLines = explode("\r\n", $rawHeaders); foreach ($responseHeaderLines as $headerLine) { if ($headerLine && strpos($headerLine, ':') !== false) { @@ -252,6 +251,6 @@ private function parseHttpResponse($respData, $headerSize) $responseHeaders = $this->parseRawHeaders($responseHeaders); - return array($responseHeaders, $responseBody); + return [$responseHeaders, $responseBody]; } } diff --git a/src/Http/MediaFileUpload.php b/src/Http/MediaFileUpload.php index de1a40895..25c98938b 100644 --- a/src/Http/MediaFileUpload.php +++ b/src/Http/MediaFileUpload.php @@ -18,7 +18,6 @@ namespace Google\Http; use Google\Client; -use Google\Http\REST; use Google\Exception as GoogleException; use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\Request; @@ -131,11 +130,11 @@ public function nextChunk($chunk = false) } $lastBytePos = $this->progress + strlen($chunk) - 1; - $headers = array( + $headers = [ 'content-range' => "bytes $this->progress-$lastBytePos/$this->size", 'content-length' => (string) strlen($chunk), 'expect' => '', - ); + ]; $request = new Request( 'PUT', @@ -198,10 +197,10 @@ private function makePutRequest(RequestInterface $request) public function resume($resumeUri) { $this->resumeUri = $resumeUri; - $headers = array( + $headers = [ 'content-range' => "bytes */$this->size", 'content-length' => '0', - ); + ]; $httpRequest = new Request( 'PUT', $this->resumeUri, @@ -234,10 +233,10 @@ private function process() if (self::UPLOAD_RESUMABLE_TYPE == $uploadType) { $contentType = $mimeType; $postBody = is_string($meta) ? $meta : json_encode($meta); - } else if (self::UPLOAD_MEDIA_TYPE == $uploadType) { + } elseif (self::UPLOAD_MEDIA_TYPE == $uploadType) { $contentType = $mimeType; $postBody = $this->data; - } else if (self::UPLOAD_MULTIPART_TYPE == $uploadType) { + } elseif (self::UPLOAD_MULTIPART_TYPE == $uploadType) { // This is a multipart/related upload. $boundary = $this->boundary ?: mt_rand(); $boundary = str_replace('"', '', $boundary); @@ -296,13 +295,13 @@ public function getResumeUri() private function fetchResumeUri() { $body = $this->request->getBody(); - $headers = array( + $headers = [ 'content-type' => 'application/json; charset=UTF-8', 'content-length' => $body->getSize(), 'x-upload-content-type' => $this->mimeType, 'x-upload-content-length' => $this->size, 'expect' => '', - ); + ]; foreach ($headers as $key => $value) { $this->request = $this->request->withHeader($key, $value); } diff --git a/src/Http/REST.php b/src/Http/REST.php index 0bcead157..0e7c11e5d 100644 --- a/src/Http/REST.php +++ b/src/Http/REST.php @@ -18,9 +18,8 @@ namespace Google\Http; use Google\Auth\HttpHandler\HttpHandlerFactory; -use Google\Client; -use Google\Task\Runner; use Google\Service\Exception as GoogleServiceException; +use Google\Task\Runner; use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Psr7\Response; @@ -49,14 +48,14 @@ public static function execute( ClientInterface $client, RequestInterface $request, $expectedClass = null, - $config = array(), + $config = [], $retryMap = null ) { $runner = new Runner( $config, sprintf('%s %s', $request->getMethod(), (string) $request->getUri()), - array(get_class(), 'doExecute'), - array($client, $request, $expectedClass) + [get_class(), 'doExecute'], + [$client, $request, $expectedClass] ); if (null !== $retryMap) { diff --git a/src/Model.php b/src/Model.php index 4d0e8dbca..6dea45523 100644 --- a/src/Model.php +++ b/src/Model.php @@ -35,9 +35,9 @@ class Model implements \ArrayAccess * instead - it will be replaced when converting to JSON with a real null. */ const NULL_VALUE = "{}gapi-php-null"; - protected $internal_gapi_mappings = array(); - protected $modelData = array(); - protected $processed = array(); + protected $internal_gapi_mappings = []; + protected $modelData = []; + protected $processed = []; /** * Polymorphic - accepts a variable number of arguments dependent @@ -66,7 +66,7 @@ public function __get($key) if (isset($this->modelData[$key])) { $val = $this->modelData[$key]; } elseif ($keyDataType == 'array' || $keyDataType == 'map') { - $val = array(); + $val = []; } else { $val = null; } @@ -80,8 +80,8 @@ public function __get($key) } else { $this->modelData[$key] = new $keyType($val); } - } else if (is_array($val)) { - $arrayObject = array(); + } elseif (is_array($val)) { + $arrayObject = []; foreach ($val as $arrayIndex => $arrayItem) { $arrayObject[$arrayIndex] = new $keyType($arrayItem); } @@ -106,7 +106,7 @@ protected function mapTypes($array) if ($keyType = $this->keyType($key)) { $dataType = $this->dataType($key); if ($dataType == 'array' || $dataType == 'map') { - $this->$key = array(); + $this->$key = []; foreach ($val as $itemKey => $itemVal) { if ($itemVal instanceof $keyType) { $this->{$key}[$itemKey] = $itemVal; @@ -183,8 +183,8 @@ private function getSimpleValue($value) { if ($value instanceof Model) { return $value->toSimpleObject(); - } else if (is_array($value)) { - $return = array(); + } elseif (is_array($value)) { + $return = []; foreach ($value as $key => $a_value) { $a_value = $this->getSimpleValue($a_value); if ($a_value !== null) { @@ -324,7 +324,7 @@ public function __unset($key) */ private function camelCase($value) { - $value = ucwords(str_replace(array('-', '_'), ' ', $value)); + $value = ucwords(str_replace(['-', '_'], ' ', $value)); $value = str_replace(' ', '', $value); $value[0] = strtolower($value[0]); return $value; diff --git a/src/Service/Exception.php b/src/Service/Exception.php index caec08c07..3212611a6 100644 --- a/src/Service/Exception.php +++ b/src/Service/Exception.php @@ -24,7 +24,7 @@ class Exception extends GoogleException /** * Optional list of errors returned in a JSON body of an HTTP error response. */ - protected $errors = array(); + protected $errors = []; /** * Override default constructor to add the ability to set $errors and a retry @@ -40,7 +40,7 @@ public function __construct( $message, $code = 0, Exception $previous = null, - $errors = array() + $errors = [] ) { if (version_compare(PHP_VERSION, '5.3.0') >= 0) { parent::__construct($message, $code, $previous); diff --git a/src/Service/Resource.php b/src/Service/Resource.php index 3afe957ae..b8b08d3cd 100644 --- a/src/Service/Resource.php +++ b/src/Service/Resource.php @@ -17,9 +17,9 @@ namespace Google\Service; -use Google\Model; -use Google\Http\MediaFileUpload; use Google\Exception as GoogleException; +use Google\Http\MediaFileUpload; +use Google\Model; use Google\Utils\UriTemplate; use GuzzleHttp\Psr7\Request; @@ -32,18 +32,18 @@ class Resource { // Valid query parameters that work, but don't appear in discovery. - private $stackParameters = array( - 'alt' => array('type' => 'string', 'location' => 'query'), - 'fields' => array('type' => 'string', 'location' => 'query'), - 'trace' => array('type' => 'string', 'location' => 'query'), - 'userIp' => array('type' => 'string', 'location' => 'query'), - 'quotaUser' => array('type' => 'string', 'location' => 'query'), - 'data' => array('type' => 'string', 'location' => 'body'), - 'mimeType' => array('type' => 'string', 'location' => 'header'), - 'uploadType' => array('type' => 'string', 'location' => 'query'), - 'mediaUpload' => array('type' => 'complex', 'location' => 'query'), - 'prettyPrint' => array('type' => 'string', 'location' => 'query'), - ); + private $stackParameters = [ + 'alt' => ['type' => 'string', 'location' => 'query'], + 'fields' => ['type' => 'string', 'location' => 'query'], + 'trace' => ['type' => 'string', 'location' => 'query'], + 'userIp' => ['type' => 'string', 'location' => 'query'], + 'quotaUser' => ['type' => 'string', 'location' => 'query'], + 'data' => ['type' => 'string', 'location' => 'body'], + 'mimeType' => ['type' => 'string', 'location' => 'header'], + 'uploadType' => ['type' => 'string', 'location' => 'query'], + 'mediaUpload' => ['type' => 'complex', 'location' => 'query'], + 'prettyPrint' => ['type' => 'string', 'location' => 'query'], + ]; /** @var string $rootUrl */ private $rootUrl; @@ -72,7 +72,7 @@ public function __construct($service, $serviceName, $resourceName, $resource) $this->resourceName = $resourceName; $this->methods = is_array($resource) && isset($resource['methods']) ? $resource['methods'] : - array($resourceName => $resource); + [$resourceName => $resource]; } /** @@ -90,11 +90,11 @@ public function call($name, $arguments, $expectedClass = null) if (! isset($this->methods[$name])) { $this->client->getLogger()->error( 'Service method unknown', - array( + [ 'service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name - ) + ] ); throw new GoogleException( @@ -114,7 +114,7 @@ public function call($name, $arguments, $expectedClass = null) // to use the smart method to create a simple object for // for JSONification. $parameters['postBody'] = $parameters['postBody']->toSimpleObject(); - } else if (is_object($parameters['postBody'])) { + } elseif (is_object($parameters['postBody'])) { // If the post body is another kind of object, we will try and // wrangle it into a sensible format. $parameters['postBody'] = @@ -133,7 +133,7 @@ public function call($name, $arguments, $expectedClass = null) } if (!isset($method['parameters'])) { - $method['parameters'] = array(); + $method['parameters'] = []; } $method['parameters'] = array_merge( @@ -145,12 +145,12 @@ public function call($name, $arguments, $expectedClass = null) if ($key != 'postBody' && !isset($method['parameters'][$key])) { $this->client->getLogger()->error( 'Service parameter unknown', - array( + [ 'service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'parameter' => $key - ) + ] ); throw new GoogleException("($name) unknown parameter: '$key'"); } @@ -164,12 +164,12 @@ public function call($name, $arguments, $expectedClass = null) ) { $this->client->getLogger()->error( 'Service parameter missing', - array( + [ 'service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'parameter' => $paramName - ) + ] ); throw new GoogleException("($name) missing required param: '$paramName'"); } @@ -186,12 +186,12 @@ public function call($name, $arguments, $expectedClass = null) $this->client->getLogger()->info( 'Service Call', - array( + [ 'service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'arguments' => $parameters, - ) + ] ); // build the service uri @@ -275,15 +275,15 @@ public function createRequestUri($restPath, $params) } $requestUrl = $this->rootUrl . $requestUrl; } - $uriTemplateVars = array(); - $queryVars = array(); + $uriTemplateVars = []; + $queryVars = []; foreach ($params as $paramName => $paramSpec) { if ($paramSpec['type'] == 'boolean') { $paramSpec['value'] = $paramSpec['value'] ? 'true' : 'false'; } if ($paramSpec['location'] == 'path') { $uriTemplateVars[$paramName] = $paramSpec['value']; - } else if ($paramSpec['location'] == 'query') { + } elseif ($paramSpec['location'] == 'query') { if (is_array($paramSpec['value'])) { foreach ($paramSpec['value'] as $value) { $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($value)); diff --git a/src/Task/Composer.php b/src/Task/Composer.php index 1c3a1b5ee..04969f207 100644 --- a/src/Task/Composer.php +++ b/src/Task/Composer.php @@ -18,9 +18,9 @@ namespace Google\Task; use Composer\Script\Event; +use InvalidArgumentException; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Finder\Finder; -use InvalidArgumentException; class Composer { diff --git a/src/Task/Runner.php b/src/Task/Runner.php index 8f74559dc..8494f8c9e 100644 --- a/src/Task/Runner.php +++ b/src/Task/Runner.php @@ -98,11 +98,12 @@ class Runner * @param array $arguments The task arguments * @throws \Google\Task\Exception when misconfigured */ - public function __construct( // @phpstan-ignore-line + // @phpstan-ignore-next-line + public function __construct( $config, $name, $action, - array $arguments = array() + array $arguments = [] ) { if (isset($config['initial_delay'])) { if ($config['initial_delay'] < 0) { @@ -269,7 +270,7 @@ private function getJitter() * * @return integer */ - public function allowedRetries($code, $errors = array()) + public function allowedRetries($code, $errors = []) { if (isset($this->retryMap[$code])) { return $this->retryMap[$code]; diff --git a/src/Utils/UriTemplate.php b/src/Utils/UriTemplate.php index da82d3fd2..d4691e02c 100644 --- a/src/Utils/UriTemplate.php +++ b/src/Utils/UriTemplate.php @@ -33,30 +33,30 @@ class UriTemplate * modify the way in which the variables inside are * processed. */ - private $operators = array( - "+" => "reserved", - "/" => "segments", - "." => "dotprefix", - "#" => "fragment", - ";" => "semicolon", - "?" => "form", - "&" => "continuation" - ); + private $operators = [ + "+" => "reserved", + "/" => "segments", + "." => "dotprefix", + "#" => "fragment", + ";" => "semicolon", + "?" => "form", + "&" => "continuation" + ]; /** * @var array * These are the characters which should not be URL encoded in reserved * strings. */ - private $reserved = array( - "=", ",", "!", "@", "|", ":", "/", "?", "#", - "[", "]",'$', "&", "'", "(", ")", "*", "+", ";" - ); - private $reservedEncoded = array( - "%3D", "%2C", "%21", "%40", "%7C", "%3A", "%2F", "%3F", - "%23", "%5B", "%5D", "%24", "%26", "%27", "%28", "%29", - "%2A", "%2B", "%3B" - ); + private $reserved = [ + "=", ",", "!", "@", "|", ":", "/", "?", "#", + "[", "]", '$', "&", "'", "(", ")", "*", "+", ";" + ]; + private $reservedEncoded = [ + "%3D", "%2C", "%21", "%40", "%7C", "%3A", "%2F", "%3F", + "%23", "%5B", "%5D", "%24", "%26", "%27", "%28", "%29", + "%2A", "%2B", "%3B" + ]; public function parse($string, array $parameters) { @@ -138,7 +138,6 @@ private function replace($string, $start, $end, $parameters) if ($data || ($data !== false && $prefix_on_missing)) { $data = $prefix . $data; } - } else { // If no operator we replace with the defaults. $data = $this->replaceVars($data, $parameters); @@ -220,7 +219,7 @@ public function combine( $value = $this->getValue($parameters[$key], $length); break; case self::TYPE_LIST: - $values = array(); + $values = []; foreach ($parameters[$key] as $pkey => $pvalue) { $pvalue = $this->getValue($pvalue, $length); if ($combine && $explode) { @@ -235,7 +234,7 @@ public function combine( } break; case self::TYPE_MAP: - $values = array(); + $values = []; foreach ($parameters[$key] as $pkey => $pvalue) { $pvalue = $this->getValue($pvalue, $length); if ($explode) { @@ -252,7 +251,7 @@ public function combine( } break; } - } else if ($tag_empty) { + } elseif ($tag_empty) { // If we are just indicating empty values with their key name, return that. return $key; } else { @@ -302,7 +301,7 @@ private function combineList( $tag_empty, $combine_on_empty ) { - $ret = array(); + $ret = []; foreach ($vars as $var) { $response = $this->combine( $var, @@ -314,7 +313,7 @@ private function combineList( $combine_on_empty ); if ($response === false) { - continue; + continue; } $ret[] = $response; } diff --git a/src/aliases.php b/src/aliases.php index 525d09164..4419ba7e3 100644 --- a/src/aliases.php +++ b/src/aliases.php @@ -43,24 +43,64 @@ class Google_Task_Composer extends \Google\Task\Composer /** @phpstan-ignore-next-line */ if (\false) { - class Google_AccessToken_Revoke extends \Google\AccessToken\Revoke {} - class Google_AccessToken_Verify extends \Google\AccessToken\Verify {} - class Google_AuthHandler_AuthHandlerFactory extends \Google\AuthHandler\AuthHandlerFactory {} - class Google_AuthHandler_Guzzle5AuthHandler extends \Google\AuthHandler\Guzzle5AuthHandler {} - class Google_AuthHandler_Guzzle6AuthHandler extends \Google\AuthHandler\Guzzle6AuthHandler {} - class Google_AuthHandler_Guzzle7AuthHandler extends \Google\AuthHandler\Guzzle7AuthHandler {} - class Google_Client extends \Google\Client {} - class Google_Collection extends \Google\Collection {} - class Google_Exception extends \Google\Exception {} - class Google_Http_Batch extends \Google\Http\Batch {} - class Google_Http_MediaFileUpload extends \Google\Http\MediaFileUpload {} - class Google_Http_REST extends \Google\Http\REST {} - class Google_Model extends \Google\Model {} - class Google_Service extends \Google\Service {} - class Google_Service_Exception extends \Google\Service\Exception {} - class Google_Service_Resource extends \Google\Service\Resource {} - class Google_Task_Exception extends \Google\Task\Exception {} - interface Google_Task_Retryable extends \Google\Task\Retryable {} - class Google_Task_Runner extends \Google\Task\Runner {} - class Google_Utils_UriTemplate extends \Google\Utils\UriTemplate {} + class Google_AccessToken_Revoke extends \Google\AccessToken\Revoke + { + } + class Google_AccessToken_Verify extends \Google\AccessToken\Verify + { + } + class Google_AuthHandler_AuthHandlerFactory extends \Google\AuthHandler\AuthHandlerFactory + { + } + class Google_AuthHandler_Guzzle5AuthHandler extends \Google\AuthHandler\Guzzle5AuthHandler + { + } + class Google_AuthHandler_Guzzle6AuthHandler extends \Google\AuthHandler\Guzzle6AuthHandler + { + } + class Google_AuthHandler_Guzzle7AuthHandler extends \Google\AuthHandler\Guzzle7AuthHandler + { + } + class Google_Client extends \Google\Client + { + } + class Google_Collection extends \Google\Collection + { + } + class Google_Exception extends \Google\Exception + { + } + class Google_Http_Batch extends \Google\Http\Batch + { + } + class Google_Http_MediaFileUpload extends \Google\Http\MediaFileUpload + { + } + class Google_Http_REST extends \Google\Http\REST + { + } + class Google_Model extends \Google\Model + { + } + class Google_Service extends \Google\Service + { + } + class Google_Service_Exception extends \Google\Service\Exception + { + } + class Google_Service_Resource extends \Google\Service\Resource + { + } + class Google_Task_Exception extends \Google\Task\Exception + { + } + interface Google_Task_Retryable extends \Google\Task\Retryable + { + } + class Google_Task_Runner extends \Google\Task\Runner + { + } + class Google_Utils_UriTemplate extends \Google\Utils\UriTemplate + { + } } diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 84457cb3b..66df1bd4b 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -159,7 +159,7 @@ private function getClientIdAndSecret() $clientId = getenv('GOOGLE_CLIENT_ID') ?: null; $clientSecret = getenv('GOOGLE_CLIENT_SECRET') ?: null; - return array($clientId, $clientSecret); + return [$clientId, $clientSecret]; } protected function checkClientCredentials() diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 3e604b510..0f1dcc56c 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -211,24 +211,24 @@ public function testPrepareService() $this->onlyGuzzle6Or7(); $client = new Client(); - $client->setScopes(array("scope1", "scope2")); + $client->setScopes(["scope1", "scope2"]); $scopes = $client->prepareScopes(); $this->assertEquals("scope1 scope2", $scopes); - $client->setScopes(array("", "scope2")); + $client->setScopes(["", "scope2"]); $scopes = $client->prepareScopes(); $this->assertEquals(" scope2", $scopes); $client->setScopes("scope2"); $client->addScope("scope3"); - $client->addScope(array("scope4", "scope5")); + $client->addScope(["scope4", "scope5"]); $scopes = $client->prepareScopes(); $this->assertEquals("scope2 scope3 scope4 scope5", $scopes); $client->setClientId('test1'); $client->setRedirectUri('http://localhost/'); $client->setState('xyz'); - $client->setScopes(array("http://test.com/", "scope2")); + $client->setScopes(["http://test.com/", "scope2"]); $scopes = $client->prepareScopes(); $this->assertEquals("http://test.com scope2", $scopes); $this->assertEquals( @@ -309,7 +309,7 @@ public function testSettersGetters() $this->assertEquals('invalid json token', $e->getMessage()); } - $token = array('access_token' => 'token'); + $token = ['access_token' => 'token']; $client->setAccessToken($token); $this->assertEquals($token, $client->getAccessToken()); } @@ -531,10 +531,10 @@ public function testRefreshTokenSetsValues() public function testRefreshTokenIsSetOnRefresh() { $refreshToken = 'REFRESH_TOKEN'; - $token = json_encode(array( + $token = json_encode([ 'access_token' => 'xyz', 'id_token' => 'ID_TOKEN', - )); + ]); $postBody = $this->prophesize('Psr\Http\Message\StreamInterface'); $postBody->__toString() ->shouldBeCalledTimes(1) @@ -584,11 +584,11 @@ public function testRefreshTokenIsSetOnRefresh() public function testRefreshTokenIsNotSetWhenNewRefreshTokenIsReturned() { $refreshToken = 'REFRESH_TOKEN'; - $token = json_encode(array( + $token = json_encode([ 'access_token' => 'xyz', 'id_token' => 'ID_TOKEN', 'refresh_token' => 'NEW_REFRESH_TOKEN' - )); + ]); $postBody = $this->prophesize('GuzzleHttp\Psr7\Stream'); $postBody->__toString() diff --git a/tests/Google/Http/MediaFileUploadTest.php b/tests/Google/Http/MediaFileUploadTest.php index bfb824169..ae7bffedf 100644 --- a/tests/Google/Http/MediaFileUploadTest.php +++ b/tests/Google/Http/MediaFileUploadTest.php @@ -60,7 +60,7 @@ public function testGetUploadType() // Test multipart uploads $media = new MediaFileUpload($client, $request, 'image/png', 'a', false); - $this->assertEquals('multipart', $media->getUploadType(array('a' => 'b'))); + $this->assertEquals('multipart', $media->getUploadType(['a' => 'b'])); } public function testProcess() diff --git a/tests/Google/Http/RESTTest.php b/tests/Google/Http/RESTTest.php index 2c8bb2136..1d09c0261 100644 --- a/tests/Google/Http/RESTTest.php +++ b/tests/Google/Http/RESTTest.php @@ -45,8 +45,8 @@ public function testDecodeResponse() $decoded = $this->rest->decodeHttpResponse($response, $this->request); $this->assertEquals($response, $decoded); - foreach (array(200, 201) as $code) { - $headers = array('foo', 'bar'); + foreach ([200, 201] as $code) { + $headers = ['foo', 'bar']; $stream = Psr7\Utils::streamFor('{"a": 1}'); $response = new Response($code, $headers, $stream); @@ -60,7 +60,7 @@ public function testDecodeMediaResponse() $client = $this->getClient(); $request = new Request('GET', 'http://www.example.com/?alt=media'); - $headers = array(); + $headers = []; $stream = Psr7\Utils::streamFor('thisisnotvalidjson'); $response = new Response(200, $headers, $stream); @@ -88,7 +88,7 @@ public function testExceptionResponse() public function testDecodeEmptyResponse() { $stream = Psr7\Utils::streamFor('{}'); - $response = new Response(200, array(), $stream); + $response = new Response(200, [], $stream); $decoded = $this->rest->decodeHttpResponse($response, $this->request); $this->assertEquals('{}', (string) $decoded->getBody()); } @@ -104,7 +104,7 @@ public function testBadErrorFormatting() } }' ); - $response = new Response(500, array(), $stream); + $response = new Response(500, [], $stream); $this->rest->decodeHttpResponse($response, $this->request); } @@ -128,7 +128,7 @@ public function tesProperErrorFormatting() } }' ); - $response = new Response(401, array(), $stream); + $response = new Response(401, [], $stream); $this->rest->decodeHttpResponse($response, $this->request); } @@ -136,7 +136,7 @@ public function testNotJson404Error() { $this->expectException(ServiceException::class); $stream = Psr7\Utils::streamFor('Not Found'); - $response = new Response(404, array(), $stream); + $response = new Response(404, [], $stream); $this->rest->decodeHttpResponse($response, $this->request); } } diff --git a/tests/Google/ModelTest.php b/tests/Google/ModelTest.php index 35abcd310..77d35e09e 100644 --- a/tests/Google/ModelTest.php +++ b/tests/Google/ModelTest.php @@ -114,7 +114,7 @@ public function testOddMappingNames() $creative->setBuyerCreativeId('12345'); $creative->setAdvertiserName('Hi'); $creative->setHTMLSnippet("

    Foo!

    "); - $creative->setClickThroughUrl(array('http://somedomain.com/')); + $creative->setClickThroughUrl(['http://somedomain.com/']); $creative->setWidth(100); $creative->setHeight(100); $data = json_decode(json_encode($creative->toSimpleObject()), true); @@ -135,7 +135,7 @@ public function testJsonStructure() $model3 = new Model(); $model3->publicE = 54321; $model3->publicF = null; - $model->publicG = array($model3, "hello", false); + $model->publicG = [$model3, "hello", false]; $model->publicH = false; $model->publicI = 0; $string = json_encode($model->toSimpleObject()); diff --git a/tests/Google/Service/AdSenseTest.php b/tests/Google/Service/AdSenseTest.php index 3599f7abc..453908ced 100644 --- a/tests/Google/Service/AdSenseTest.php +++ b/tests/Google/Service/AdSenseTest.php @@ -458,12 +458,12 @@ private function checkUrlChannelsCollection($urlChannels) private function getReportOptParams() { - return array( - 'metric' => array('PAGE_VIEWS', 'AD_REQUESTS'), - 'dimension' => array ('DATE', 'AD_CLIENT_ID'), - 'sort' => array('DATE'), - 'filter' => array('COUNTRY_NAME==United States'), - ); + return [ + 'metric' => ['PAGE_VIEWS', 'AD_REQUESTS'], + 'dimension' => ['DATE', 'AD_CLIENT_ID'], + 'sort' => ['DATE'], + 'filter' => ['COUNTRY_NAME==United States'], + ]; } private function checkReport($report) diff --git a/tests/Google/Service/ResourceTest.php b/tests/Google/Service/ResourceTest.php index 51e7e25cd..870286608 100644 --- a/tests/Google/Service/ResourceTest.php +++ b/tests/Google/Service/ResourceTest.php @@ -73,17 +73,17 @@ public function testCallFailure() $this->service, "test", "testResource", - array( - "methods" => array( - "testMethod" => array( - "parameters" => array(), + [ + "methods" => [ + "testMethod" => [ + "parameters" => [], "path" => "method/path", "httpMethod" => "POST", - ) - ) - ) + ] + ] + ] ); - $resource->call("someothermethod", array()); + $resource->call("someothermethod", []); } public function testCall() @@ -92,17 +92,17 @@ public function testCall() $this->service, "test", "testResource", - array( - "methods" => array( - "testMethod" => array( - "parameters" => array(), + [ + "methods" => [ + "testMethod" => [ + "parameters" => [], "path" => "method/path", "httpMethod" => "POST", - ) - ) - ) + ] + ] + ] ); - $request = $resource->call("testMethod", array(array())); + $request = $resource->call("testMethod", [[]]); $this->assertEquals("https://test.example.com/method/path", (string) $request->getUri()); $this->assertEquals("POST", $request->getMethod()); } @@ -114,17 +114,17 @@ public function testCallServiceDefinedRoot() $this->service, "test", "testResource", - array( - "methods" => array( - "testMethod" => array( - "parameters" => array(), + [ + "methods" => [ + "testMethod" => [ + "parameters" => [], "path" => "method/path", "httpMethod" => "POST", - ) - ) - ) + ] + ] + ] ); - $request = $resource->call("testMethod", array(array())); + $request = $resource->call("testMethod", [[]]); $this->assertEquals("https://sample.example.com/method/path", (string) $request->getUri()); $this->assertEquals("POST", $request->getMethod()); } @@ -142,17 +142,17 @@ public function testCreateRequestUriForASelfDefinedServicePath() $this->service, 'test', 'testResource', - array( - "methods" => array( - 'testMethod' => array( - 'parameters' => array(), + [ + "methods" => [ + 'testMethod' => [ + 'parameters' => [], 'path' => '/admin/directory_v1/watch/stop', 'httpMethod' => 'POST', - ) - ) - ) + ] + ] + ] ); - $request = $resource->call('testMethod', array(array())); + $request = $resource->call('testMethod', [[]]); $this->assertEquals('https://test.example.com/admin/directory_v1/watch/stop', (string) $request->getUri()); } @@ -161,10 +161,10 @@ public function testCreateRequestUri() $restPath = "plus/{u}"; $service = new GoogleService($this->client->reveal()); $service->servicePath = "http://localhost/"; - $resource = new GoogleResource($service, 'test', 'testResource', array()); + $resource = new GoogleResource($service, 'test', 'testResource', []); // Test Path - $params = array(); + $params = []; $params['u']['type'] = 'string'; $params['u']['location'] = 'path'; $params['u']['value'] = 'me'; @@ -172,7 +172,7 @@ public function testCreateRequestUri() $this->assertEquals("http://localhost/plus/me", $value); // Test Query - $params = array(); + $params = []; $params['u']['type'] = 'string'; $params['u']['location'] = 'query'; $params['u']['value'] = 'me'; @@ -180,7 +180,7 @@ public function testCreateRequestUri() $this->assertEquals("http://localhost/plus?u=me", $value); // Test Booleans - $params = array(); + $params = []; $params['u']['type'] = 'boolean'; $params['u']['location'] = 'path'; $params['u']['value'] = '1'; @@ -192,7 +192,7 @@ public function testCreateRequestUri() $this->assertEquals("http://localhost/plus?u=true", $value); // Test encoding - $params = array(); + $params = []; $params['u']['type'] = 'string'; $params['u']['location'] = 'query'; $params['u']['value'] = '@me/'; @@ -236,15 +236,15 @@ public function testNoExpectedClassForAltMediaWithHttpSuccess() $service, "test", "testResource", - array( - "methods" => array( - "testMethod" => array( - "parameters" => array(), + [ + "methods" => [ + "testMethod" => [ + "parameters" => [], "path" => "method/path", "httpMethod" => "POST", - ) - ) - ) + ] + ] + ] ); $expectedClass = 'ThisShouldBeIgnored'; @@ -289,15 +289,15 @@ public function testNoExpectedClassForAltMediaWithHttpFail() $service, "test", "testResource", - array( - "methods" => array( - "testMethod" => array( - "parameters" => array(), + [ + "methods" => [ + "testMethod" => [ + "parameters" => [], "path" => "method/path", "httpMethod" => "POST", - ) - ) - ) + ] + ] + ] ); try { @@ -346,15 +346,15 @@ public function testErrorResponseWithVeryLongBody() $service, "test", "testResource", - array( - "methods" => array( - "testMethod" => array( - "parameters" => array(), + [ + "methods" => [ + "testMethod" => [ + "parameters" => [], "path" => "method/path", "httpMethod" => "POST", - ) - ) - ) + ] + ] + ] ); try { @@ -392,15 +392,15 @@ public function testSuccessResponseWithVeryLongBody() $service, "test", "testResource", - array( - "methods" => array( - "testMethod" => array( - "parameters" => array(), + [ + "methods" => [ + "testMethod" => [ + "parameters" => [], "path" => "method/path", "httpMethod" => "POST", - ) - ) - ) + ] + ] + ] ); $expectedClass = 'ThisShouldBeIgnored'; @@ -451,20 +451,20 @@ public function testExceptionMessage() $service, "test", "testResource", - array( - "methods" => array( - "testMethod" => array( - "parameters" => array(), + [ + "methods" => [ + "testMethod" => [ + "parameters" => [], "path" => "method/path", "httpMethod" => "POST", - ) - ) - ) + ] + ] + ] ); try { - $decoded = $resource->call('testMethod', array(array())); + $decoded = $resource->call('testMethod', [[]]); $this->fail('should have thrown exception'); } catch (ServiceException $e) { $this->assertEquals($errors, $e->getErrors()); diff --git a/tests/Google/Service/YouTubeTest.php b/tests/Google/Service/YouTubeTest.php index 8cc0b34f9..c6e4032ee 100644 --- a/tests/Google/Service/YouTubeTest.php +++ b/tests/Google/Service/YouTubeTest.php @@ -33,7 +33,7 @@ public function set_up() public function testMissingFieldsAreNull() { $parts = "id,brandingSettings"; - $opts = array("mine" => true); + $opts = ["mine" => true]; $channels = $this->youtube->channels->listChannels($parts, $opts); $newChannel = new YouTube\Channel(); @@ -75,7 +75,7 @@ public function testMissingFieldsAreNull() $ping = new YouTube\ChannelConversionPing(); $ping->setContext("hello"); $pings = new YouTube\ChannelConversionPings(); - $pings->setPings(array($ping)); + $pings->setPings([$ping]); $simplePings = $pings->toSimpleObject(); $this->assertObjectHasAttribute('context', $simplePings->pings[0]); $this->assertObjectNotHasAttribute('conversionUrl', $simplePings->pings[0]); diff --git a/tests/Google/ServiceTest.php b/tests/Google/ServiceTest.php index 07da91a3f..75d49bfbe 100644 --- a/tests/Google/ServiceTest.php +++ b/tests/Google/ServiceTest.php @@ -94,38 +94,34 @@ public function testModel() { $model = new TestModel(); - $model->mapTypes( - array( + $model->mapTypes([ 'name' => 'asdf', 'gender' => 'z', - ) - ); + ]); $this->assertEquals('asdf', $model->name); $this->assertEquals('z', $model->gender); - $model->mapTypes( - array( - '__infoType' => 'Google_Model', - '__infoDataType' => 'map', - 'info' => array ( - 'location' => 'mars', - 'timezone' => 'mst', - ), - 'name' => 'asdf', - 'gender' => 'z', - ) - ); + $model->mapTypes([ + '__infoType' => 'Google_Model', + '__infoDataType' => 'map', + 'info' => [ + 'location' => 'mars', + 'timezone' => 'mst', + ], + 'name' => 'asdf', + 'gender' => 'z', + ]); $this->assertEquals('asdf', $model->name); $this->assertEquals('z', $model->gender); $this->assertFalse($model->isAssociativeArray("")); $this->assertFalse($model->isAssociativeArray(false)); $this->assertFalse($model->isAssociativeArray(null)); - $this->assertFalse($model->isAssociativeArray(array())); - $this->assertFalse($model->isAssociativeArray(array(1, 2))); - $this->assertFalse($model->isAssociativeArray(array(1 => 2))); + $this->assertFalse($model->isAssociativeArray([])); + $this->assertFalse($model->isAssociativeArray([1, 2])); + $this->assertFalse($model->isAssociativeArray([1 => 2])); - $this->assertTrue($model->isAssociativeArray(array('test' => 'a'))); - $this->assertTrue($model->isAssociativeArray(array("a", "b" => 2))); + $this->assertTrue($model->isAssociativeArray(['test' => 'a'])); + $this->assertTrue($model->isAssociativeArray(["a", "b" => 2])); } public function testConfigConstructor() diff --git a/tests/Google/Task/RunnerTest.php b/tests/Google/Task/RunnerTest.php index dd3cc80e5..62d5a987d 100644 --- a/tests/Google/Task/RunnerTest.php +++ b/tests/Google/Task/RunnerTest.php @@ -38,7 +38,7 @@ class RunnerTest extends BaseTest private $mockedCallsCount = 0; private $currentMockedCall = 0; - private $mockedCalls = array(); + private $mockedCalls = []; private $retryMap; private $retryConfig; @@ -62,7 +62,7 @@ public function testRestRetryOffByDefault($errorCode, $errorBody = '{}') public function testOneRestRetryWithError($errorCode, $errorBody = '{}') { $this->expectException(ServiceException::class); - $this->setRetryConfig(array('retries' => 1)); + $this->setRetryConfig(['retries' => 1]); $this->setNextResponses(2, $errorCode, $errorBody)->makeRequest(); } @@ -75,7 +75,7 @@ public function testMultipleRestRetriesWithErrors( ) { $this->expectException(ServiceException::class); - $this->setRetryConfig(array('retries' => 5)); + $this->setRetryConfig(['retries' => 5]); $this->setNextResponses(6, $errorCode, $errorBody)->makeRequest(); } @@ -84,7 +84,7 @@ public function testMultipleRestRetriesWithErrors( */ public function testOneRestRetryWithSuccess($errorCode, $errorBody = '{}') { - $this->setRetryConfig(array('retries' => 1)); + $this->setRetryConfig(['retries' => 1]); $result = $this->setNextResponse($errorCode, $errorBody) ->setNextResponse(200, '{"success": true}') ->makeRequest(); @@ -99,7 +99,7 @@ public function testMultipleRestRetriesWithSuccess( $errorCode, $errorBody = '{}' ) { - $this->setRetryConfig(array('retries' => 5)); + $this->setRetryConfig(['retries' => 5]); $result = $this->setNextResponses(2, $errorCode, $errorBody) ->setNextResponse(200, '{"success": true}') ->makeRequest(); @@ -116,19 +116,19 @@ public function testCustomRestRetryMapReplacesDefaults( ) { $this->expectException(ServiceException::class); - $this->setRetryMap(array()); + $this->setRetryMap([]); - $this->setRetryConfig(array('retries' => 5)); + $this->setRetryConfig(['retries' => 5]); $this->setNextResponse($errorCode, $errorBody)->makeRequest(); } public function testCustomRestRetryMapAddsNewHandlers() { $this->setRetryMap( - array('403' => Runner::TASK_RETRY_ALWAYS) + ['403' => Runner::TASK_RETRY_ALWAYS] ); - $this->setRetryConfig(array('retries' => 5)); + $this->setRetryConfig(['retries' => 5]); $result = $this->setNextResponses(2, 403) ->setNextResponse(200, '{"success": true}') ->makeRequest(); @@ -144,10 +144,10 @@ public function testCustomRestRetryMapWithCustomLimits($limit) $this->expectException(ServiceException::class); $this->setRetryMap( - array('403' => $limit) + ['403' => $limit] ); - $this->setRetryConfig(array('retries' => 5)); + $this->setRetryConfig(['retries' => 5]); $this->setNextResponses($limit + 1, 403)->makeRequest(); } @@ -162,7 +162,7 @@ public function testRestTimeouts($config, $minTime) $this->assertTaskTimeGreaterThanOrEqual( $minTime, - array($this, 'makeRequest'), + [$this, 'makeRequest'], $config['initial_delay'] / 10 ); } @@ -186,7 +186,7 @@ public function testOneCurlRetryWithError($errorCode, $errorMessage = '') { $this->expectException(ServiceException::class); - $this->setRetryConfig(array('retries' => 1)); + $this->setRetryConfig(['retries' => 1]); $this->setNextResponsesThrow(2, $errorMessage, $errorCode)->makeRequest(); } @@ -200,7 +200,7 @@ public function testMultipleCurlRetriesWithErrors( ) { $this->expectException(ServiceException::class); - $this->setRetryConfig(array('retries' => 5)); + $this->setRetryConfig(['retries' => 5]); $this->setNextResponsesThrow(6, $errorMessage, $errorCode)->makeRequest(); } @@ -210,7 +210,7 @@ public function testMultipleCurlRetriesWithErrors( */ public function testOneCurlRetryWithSuccess($errorCode, $errorMessage = '') { - $this->setRetryConfig(array('retries' => 1)); + $this->setRetryConfig(['retries' => 1]); $result = $this->setNextResponseThrows($errorMessage, $errorCode) ->setNextResponse(200, '{"success": true}') ->makeRequest(); @@ -226,7 +226,7 @@ public function testMultipleCurlRetriesWithSuccess( $errorCode, $errorMessage = '' ) { - $this->setRetryConfig(array('retries' => 5)); + $this->setRetryConfig(['retries' => 5]); $result = $this->setNextResponsesThrow(2, $errorMessage, $errorCode) ->setNextResponse(200, '{"success": true}') ->makeRequest(); @@ -244,9 +244,9 @@ public function testCustomCurlRetryMapReplacesDefaults( ) { $this->expectException(ServiceException::class); - $this->setRetryMap(array()); + $this->setRetryMap([]); - $this->setRetryConfig(array('retries' => 5)); + $this->setRetryConfig(['retries' => 5]); $this->setNextResponseThrows($errorMessage, $errorCode)->makeRequest(); } @@ -256,10 +256,10 @@ public function testCustomCurlRetryMapReplacesDefaults( public function testCustomCurlRetryMapAddsNewHandlers() { $this->setRetryMap( - array(CURLE_COULDNT_RESOLVE_PROXY => Runner::TASK_RETRY_ALWAYS) + [CURLE_COULDNT_RESOLVE_PROXY => Runner::TASK_RETRY_ALWAYS] ); - $this->setRetryConfig(array('retries' => 5)); + $this->setRetryConfig(['retries' => 5]); $result = $this->setNextResponsesThrow(2, '', CURLE_COULDNT_RESOLVE_PROXY) ->setNextResponse(200, '{"success": true}') ->makeRequest(); @@ -276,10 +276,10 @@ public function testCustomCurlRetryMapWithCustomLimits($limit) $this->expectException(ServiceException::class); $this->setRetryMap( - array(CURLE_COULDNT_RESOLVE_PROXY => $limit) + [CURLE_COULDNT_RESOLVE_PROXY => $limit] ); - $this->setRetryConfig(array('retries' => 5)); + $this->setRetryConfig(['retries' => 5]); $this->setNextResponsesThrow($limit + 1, '', CURLE_COULDNT_RESOLVE_PROXY) ->makeRequest(); } @@ -296,7 +296,7 @@ public function testCurlTimeouts($config, $minTime) $this->assertTaskTimeGreaterThanOrEqual( $minTime, - array($this, 'makeRequest'), + [$this, 'makeRequest'], $config['initial_delay'] / 10 ); } @@ -313,7 +313,7 @@ public function testBadTaskConfig($config, $message) new Runner( $this->retryConfig, '', - array($this, 'testBadTaskConfig') + [$this, 'testBadTaskConfig'] ); } @@ -339,7 +339,7 @@ public function testOneTaskRetryWithError() { $this->expectException(ServiceException::class); - $this->setRetryConfig(array('retries' => 1)); + $this->setRetryConfig(['retries' => 1]); $this->setNextTasksAllowedRetries(2, Runner::TASK_RETRY_ALWAYS) ->runTask(); } @@ -348,14 +348,14 @@ public function testMultipleTaskRetriesWithErrors() { $this->expectException(ServiceException::class); - $this->setRetryConfig(array('retries' => 5)); + $this->setRetryConfig(['retries' => 5]); $this->setNextTasksAllowedRetries(6, Runner::TASK_RETRY_ALWAYS) ->runTask(); } public function testOneTaskRetryWithSuccess() { - $this->setRetryConfig(array('retries' => 1)); + $this->setRetryConfig(['retries' => 1]); $result = $this->setNextTaskAllowedRetries(Runner::TASK_RETRY_ALWAYS) ->setNextTaskReturnValue('success') ->runTask(); @@ -365,7 +365,7 @@ public function testOneTaskRetryWithSuccess() public function testMultipleTaskRetriesWithSuccess() { - $this->setRetryConfig(array('retries' => 5)); + $this->setRetryConfig(['retries' => 5]); $result = $this->setNextTasksAllowedRetries(2, Runner::TASK_RETRY_ALWAYS) ->setNextTaskReturnValue('success') ->runTask(); @@ -380,7 +380,7 @@ public function testTaskRetryWithCustomLimits($limit) { $this->expectException(ServiceException::class); - $this->setRetryConfig(array('retries' => 5)); + $this->setRetryConfig(['retries' => 5]); $this->setNextTasksAllowedRetries($limit + 1, $limit) ->runTask(); } @@ -396,20 +396,20 @@ public function testTaskTimeouts($config, $minTime) $this->assertTaskTimeGreaterThanOrEqual( $minTime, - array($this, 'runTask'), + [$this, 'runTask'], $config['initial_delay'] / 10 ); } public function testTaskWithManualRetries() { - $this->setRetryConfig(array('retries' => 2)); + $this->setRetryConfig(['retries' => 2]); $this->setNextTasksAllowedRetries(2, Runner::TASK_RETRY_ALWAYS); $task = new Runner( $this->retryConfig, '', - array($this, 'runNextTask') + [$this, 'runNextTask'] ); $this->assertTrue($task->canAttempt()); @@ -432,15 +432,15 @@ public function testTaskWithManualRetries() */ public function timeoutProvider() { - $config = array('initial_delay' => .001, 'max_delay' => .01); + $config = ['initial_delay' => .001, 'max_delay' => .01]; - return array( - array(array_merge($config, array('retries' => 1)), .001), - array(array_merge($config, array('retries' => 2)), .0015), - array(array_merge($config, array('retries' => 3)), .00225), - array(array_merge($config, array('retries' => 4)), .00375), - array(array_merge($config, array('retries' => 5)), .005625) - ); + return [ + [array_merge($config, ['retries' => 1]), .001], + [array_merge($config, ['retries' => 2]), .0015], + [array_merge($config, ['retries' => 3]), .00225], + [array_merge($config, ['retries' => 4]), .00375], + [array_merge($config, ['retries' => 5]), .005625] + ]; } /** @@ -450,10 +450,10 @@ public function timeoutProvider() */ public function customLimitsProvider() { - return array( - array(Runner::TASK_RETRY_NEVER), - array(Runner::TASK_RETRY_ONCE), - ); + return [ + [Runner::TASK_RETRY_NEVER], + [Runner::TASK_RETRY_ONCE], + ]; } /** @@ -463,13 +463,13 @@ public function customLimitsProvider() */ public function badTaskConfigProvider() { - return array( - array(array('initial_delay' => -1), 'must not be negative'), - array(array('max_delay' => 0), 'must be greater than 0'), - array(array('factor' => 0), 'must be greater than 0'), - array(array('jitter' => 0), 'must be greater than 0'), - array(array('retries' => -1), 'must not be negative') - ); + return [ + [['initial_delay' => -1], 'must not be negative'], + [['max_delay' => 0], 'must be greater than 0'], + [['factor' => 0], 'must be greater than 0'], + [['jitter' => 0], 'must be greater than 0'], + [['retries' => -1], 'must not be negative'] + ]; } /** @@ -479,12 +479,12 @@ public function badTaskConfigProvider() */ public function defaultRestErrorProvider() { - return array( - array(500), - array(503), - array(403, '{"error":{"errors":[{"reason":"rateLimitExceeded"}]}}'), - array(403, '{"error":{"errors":[{"reason":"userRateLimitExceeded"}]}}'), - ); + return [ + [500], + [503], + [403, '{"error":{"errors":[{"reason":"rateLimitExceeded"}]}}'], + [403, '{"error":{"errors":[{"reason":"userRateLimitExceeded"}]}}'], + ]; } /** @@ -494,13 +494,13 @@ public function defaultRestErrorProvider() */ public function defaultCurlErrorProvider() { - return array( - array(6), // CURLE_COULDNT_RESOLVE_HOST - array(7), // CURLE_COULDNT_CONNECT - array(28), // CURLE_OPERATION_TIMEOUTED - array(35), // CURLE_SSL_CONNECT_ERROR - array(52), // CURLE_GOT_NOTHING - ); + return [ + [6], // CURLE_COULDNT_RESOLVE_HOST + [7], // CURLE_COULDNT_CONNECT + [28], // CURLE_OPERATION_TIMEOUTED + [35], // CURLE_SSL_CONNECT_ERROR + [52], // CURLE_GOT_NOTHING + ]; } /** @@ -539,13 +539,13 @@ public static function assertTaskTimeGreaterThanOrEqual( */ private function setRetryConfig(array $config) { - $config += array( - 'initial_delay' => .0001, - 'max_delay' => .001, - 'factor' => 2, - 'jitter' => .5, - 'retries' => 1 - ); + $config += [ + 'initial_delay' => .0001, + 'max_delay' => .001, + 'factor' => 2, + 'jitter' => .5, + 'retries' => 1 + ]; $this->retryConfig = $config; } @@ -568,7 +568,7 @@ private function setNextResponses( $count, $code = '200', $body = '{}', - array $headers = array() + array $headers = [] ) { while ($count-- > 0) { $this->setNextResponse($code, $body, $headers); @@ -589,13 +589,13 @@ private function setNextResponses( private function setNextResponse( $code = '200', $body = '{}', - array $headers = array() + array $headers = [] ) { - $this->mockedCalls[$this->mockedCallsCount++] = array( - 'code' => (string) $code, - 'headers' => $headers, - 'body' => is_string($body) ? $body : json_encode($body) - ); + $this->mockedCalls[$this->mockedCallsCount++] = [ + 'code' => (string) $code, + 'headers' => $headers, + 'body' => is_string($body) ? $body : json_encode($body) + ]; return $this; } @@ -632,7 +632,7 @@ private function setNextResponseThrows($message, $code) $message, $code, null, - array() + [] ); return $this; @@ -744,7 +744,7 @@ private function runTask() $task = new Runner( $this->retryConfig, '', - array($this, 'runNextTask') + [$this, 'runNextTask'] ); if (null !== $this->retryMap) { @@ -754,7 +754,7 @@ private function runTask() $exception = $this->prophesize(ServiceException::class); $exceptionCount = 0; - $exceptionCalls = array(); + $exceptionCalls = []; for ($i = 0; $i < $this->mockedCallsCount; $i++) { if (is_int($this->mockedCalls[$i])) { diff --git a/tests/Google/Utils/UriTemplateTest.php b/tests/Google/Utils/UriTemplateTest.php index 5e3e2d414..8c366435d 100644 --- a/tests/Google/Utils/UriTemplateTest.php +++ b/tests/Google/Utils/UriTemplateTest.php @@ -33,11 +33,11 @@ public function testLevelOne() $urit = new UriTemplate(); $this->assertEquals( "value", - $urit->parse("{var}", array("var" => $var)) + $urit->parse("{var}", ["var" => $var]) ); $this->assertEquals( "Hello%20World%21", - $urit->parse("{hello}", array("hello" => $hello)) + $urit->parse("{hello}", ["hello" => $hello]) ); } @@ -50,27 +50,27 @@ public function testLevelTwo() $urit = new UriTemplate(); $this->assertEquals( "value", - $urit->parse("{+var}", array("var" => $var)) + $urit->parse("{+var}", ["var" => $var]) ); $this->assertEquals( "Hello%20World!", - $urit->parse("{+hello}", array("hello" => $hello)) + $urit->parse("{+hello}", ["hello" => $hello]) ); $this->assertEquals( "/foo/bar/here", - $urit->parse("{+path}/here", array("path" => $path)) + $urit->parse("{+path}/here", ["path" => $path]) ); $this->assertEquals( "here?ref=/foo/bar", - $urit->parse("here?ref={+path}", array("path" => $path)) + $urit->parse("here?ref={+path}", ["path" => $path]) ); $this->assertEquals( "X#value", - $urit->parse("X{#var}", array("var" => $var)) + $urit->parse("X{#var}", ["var" => $var]) ); $this->assertEquals( "X#Hello%20World!", - $urit->parse("X{#hello}", array("hello" => $hello)) + $urit->parse("X{#hello}", ["hello" => $hello]) ); } @@ -86,97 +86,97 @@ public function testLevelThree() $urit = new UriTemplate(); $this->assertEquals( "map?1024,768", - $urit->parse("map?{x,y}", array("x" => $x, "y" => $y)) + $urit->parse("map?{x,y}", ["x" => $x, "y" => $y]) ); $this->assertEquals( "1024,Hello%20World%21,768", - $urit->parse("{x,hello,y}", array("x" => $x, "y" => $y, "hello" => $hello)) + $urit->parse("{x,hello,y}", ["x" => $x, "y" => $y, "hello" => $hello]) ); $this->assertEquals( "1024,Hello%20World!,768", - $urit->parse("{+x,hello,y}", array("x" => $x, "y" => $y, "hello" => $hello)) + $urit->parse("{+x,hello,y}", ["x" => $x, "y" => $y, "hello" => $hello]) ); $this->assertEquals( "/foo/bar,1024/here", - $urit->parse("{+path,x}/here", array("x" => $x, "path" => $path)) + $urit->parse("{+path,x}/here", ["x" => $x, "path" => $path]) ); $this->assertEquals( "#1024,Hello%20World!,768", - $urit->parse("{#x,hello,y}", array("x" => $x, "y" => $y, "hello" => $hello)) + $urit->parse("{#x,hello,y}", ["x" => $x, "y" => $y, "hello" => $hello]) ); $this->assertEquals( "#/foo/bar,1024/here", - $urit->parse("{#path,x}/here", array("x" => $x, "path" => $path)) + $urit->parse("{#path,x}/here", ["x" => $x, "path" => $path]) ); $this->assertEquals( "X.value", - $urit->parse("X{.var}", array("var" => $var)) + $urit->parse("X{.var}", ["var" => $var]) ); $this->assertEquals( "X.1024.768", - $urit->parse("X{.x,y}", array("x" => $x, "y" => $y)) + $urit->parse("X{.x,y}", ["x" => $x, "y" => $y]) ); $this->assertEquals( "X.value", - $urit->parse("X{.var}", array("var" => $var)) + $urit->parse("X{.var}", ["var" => $var]) ); $this->assertEquals( "X.1024.768", - $urit->parse("X{.x,y}", array("x" => $x, "y" => $y)) + $urit->parse("X{.x,y}", ["x" => $x, "y" => $y]) ); $this->assertEquals( "/value", - $urit->parse("{/var}", array("var" => $var)) + $urit->parse("{/var}", ["var" => $var]) ); $this->assertEquals( "/value/1024/here", - $urit->parse("{/var,x}/here", array("x" => $x, "var" => $var)) + $urit->parse("{/var,x}/here", ["x" => $x, "var" => $var]) ); $this->assertEquals( ";x=1024;y=768", - $urit->parse("{;x,y}", array("x" => $x, "y" => $y)) + $urit->parse("{;x,y}", ["x" => $x, "y" => $y]) ); $this->assertEquals( ";x=1024;y=768;empty", - $urit->parse("{;x,y,empty}", array("x" => $x, "y" => $y, "empty" => $empty)) + $urit->parse("{;x,y,empty}", ["x" => $x, "y" => $y, "empty" => $empty]) ); $this->assertEquals( "?x=1024&y=768", - $urit->parse("{?x,y}", array("x" => $x, "y" => $y)) + $urit->parse("{?x,y}", ["x" => $x, "y" => $y]) ); $this->assertEquals( "?x=1024&y=768&empty=", - $urit->parse("{?x,y,empty}", array("x" => $x, "y" => $y, "empty" => $empty)) + $urit->parse("{?x,y,empty}", ["x" => $x, "y" => $y, "empty" => $empty]) ); $this->assertEquals( "?fixed=yes&x=1024", - $urit->parse("?fixed=yes{&x}", array("x" => $x, "y" => $y)) + $urit->parse("?fixed=yes{&x}", ["x" => $x, "y" => $y]) ); $this->assertEquals( "&x=1024&y=768&empty=", - $urit->parse("{&x,y,empty}", array("x" => $x, "y" => $y, "empty" => $empty)) + $urit->parse("{&x,y,empty}", ["x" => $x, "y" => $y, "empty" => $empty]) ); } public function testLevelFour() { - $values = array( + $values = [ 'var' => "value", 'hello' => "Hello World!", 'path' => "/foo/bar", - 'list' => array("red", "green", "blue"), - 'keys' => array("semi" => ";", "dot" => ".", "comma" => ","), - ); + 'list' => ["red", "green", "blue"], + 'keys' => ["semi" => ";", "dot" => ".", "comma" => ","], + ]; - $tests = array( + $tests = [ "{var:3}" => "val", "{var:30}" => "value", "{list}" => "red,green,blue", @@ -221,7 +221,7 @@ public function testLevelFour() "{&keys*}" => "&semi=%3B&dot=.&comma=%2C", "find{?list*}" => "find?list=red&list=green&list=blue", "www{.list*}" => "www.red.green.blue" - ); + ]; $urit = new UriTemplate(); @@ -239,15 +239,15 @@ public function testMultipleAnnotations() "http://www.google.com/Hello%20World!?var=value", $urit->parse( "http://www.google.com/%7B+hello%7D%7B?var}", - array("var" => $var, "hello" => $hello) + ["var" => $var, "hello" => $hello] ) ); - $params = array( + $params = [ "playerId" => "me", "leaderboardId" => "CgkIhcG1jYEbEAIQAw", "timeSpan" => "ALL_TIME", "other" => "irrelevant" - ); + ]; $this->assertEquals( "players/me/leaderboards/CgkIhcG1jYEbEAIQAw/scores/ALL_TIME", $urit->parse( From a9a27c25d625a95133f790f3b4a1036ad5b4e9eb Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 25 Apr 2022 08:56:53 -0600 Subject: [PATCH 229/301] fix: add missing public var used by subclasses (#2254) --- src/Service.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Service.php b/src/Service.php index 923e7caef..c97ee9d4f 100644 --- a/src/Service.php +++ b/src/Service.php @@ -26,6 +26,7 @@ class Service public $rootUrl; public $version; public $servicePath; + public $serviceName; public $availableScopes; public $resource; private $client; From 56d4365eda623cab569ec647e7ed76af55ea88c3 Mon Sep 17 00:00:00 2001 From: Stephan Vierkant Date: Fri, 27 May 2022 18:05:25 +0200 Subject: [PATCH 230/301] chore: support for monolog 3 (#2268) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 04d65f650..9c3b7d233 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ "google/auth": "^1.10", "google/apiclient-services": "~0.200", "firebase/php-jwt": "~2.0||~3.0||~4.0||~5.0||~6.0", - "monolog/monolog": "^1.17||^2.0", + "monolog/monolog": "^1.17||^2.0||^3.0", "phpseclib/phpseclib": "~2.0||^3.0.2", "guzzlehttp/guzzle": "~5.3.3||~6.0||~7.0", "guzzlehttp/psr7": "^1.8.4||^2.2.1" From 097b82b82b054691013ff12425e69e8f9cbd09af Mon Sep 17 00:00:00 2001 From: Steffen Weber Date: Tue, 31 May 2022 16:37:26 +0200 Subject: [PATCH 231/301] fix: PHP 8.1 compatibility for Task\Runner::backOff (#2259) --- src/Task/Runner.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Task/Runner.php b/src/Task/Runner.php index 8494f8c9e..1047f1f01 100644 --- a/src/Task/Runner.php +++ b/src/Task/Runner.php @@ -236,7 +236,7 @@ private function backOff() { $delay = $this->getDelay(); - usleep($delay * 1000000); + usleep((int) ($delay * 1000000)); } /** From eb10f733eb0ebec058776cda206009d01af9f9e3 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 31 May 2022 07:44:17 -0700 Subject: [PATCH 232/301] chore: unimportant whitespace fixes (#2269) --- tests/Google/ClientTest.php | 8 +- tests/Google/Http/RESTTest.php | 4 +- tests/Google/Task/RunnerTest.php | 306 +++++++++++++++---------------- 3 files changed, 159 insertions(+), 159 deletions(-) diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 0f1dcc56c..23eebd562 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -249,16 +249,16 @@ public function testPrepareService() $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); $response->getBody() - ->shouldBeCalledTimes(1) - ->willReturn($stream->reveal()); + ->shouldBeCalledTimes(1) + ->willReturn($stream->reveal()); $response->getStatusCode()->willReturn(200); $http = $this->prophesize('GuzzleHttp\ClientInterface'); $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) - ->shouldBeCalledTimes(1) - ->willReturn($response->reveal()); + ->shouldBeCalledTimes(1) + ->willReturn($response->reveal()); $client->setHttpClient($http->reveal()); $dr_service = new Drive($client); diff --git a/tests/Google/Http/RESTTest.php b/tests/Google/Http/RESTTest.php index 1d09c0261..2c73dceb6 100644 --- a/tests/Google/Http/RESTTest.php +++ b/tests/Google/Http/RESTTest.php @@ -28,8 +28,8 @@ class RESTTest extends BaseTest { /** - * @var REST $rest - */ + * @var REST $rest + */ private $rest; public function set_up() diff --git a/tests/Google/Task/RunnerTest.php b/tests/Google/Task/RunnerTest.php index 62d5a987d..ce868158d 100644 --- a/tests/Google/Task/RunnerTest.php +++ b/tests/Google/Task/RunnerTest.php @@ -48,8 +48,8 @@ protected function set_up() } /** - * @dataProvider defaultRestErrorProvider - */ + * @dataProvider defaultRestErrorProvider + */ public function testRestRetryOffByDefault($errorCode, $errorBody = '{}') { $this->expectException(ServiceException::class); @@ -57,8 +57,8 @@ public function testRestRetryOffByDefault($errorCode, $errorBody = '{}') } /** - * @dataProvider defaultRestErrorProvider - */ + * @dataProvider defaultRestErrorProvider + */ public function testOneRestRetryWithError($errorCode, $errorBody = '{}') { $this->expectException(ServiceException::class); @@ -67,8 +67,8 @@ public function testOneRestRetryWithError($errorCode, $errorBody = '{}') } /** - * @dataProvider defaultRestErrorProvider - */ + * @dataProvider defaultRestErrorProvider + */ public function testMultipleRestRetriesWithErrors( $errorCode, $errorBody = '{}' @@ -80,8 +80,8 @@ public function testMultipleRestRetriesWithErrors( } /** - * @dataProvider defaultRestErrorProvider - */ + * @dataProvider defaultRestErrorProvider + */ public function testOneRestRetryWithSuccess($errorCode, $errorBody = '{}') { $this->setRetryConfig(['retries' => 1]); @@ -93,8 +93,8 @@ public function testOneRestRetryWithSuccess($errorCode, $errorBody = '{}') } /** - * @dataProvider defaultRestErrorProvider - */ + * @dataProvider defaultRestErrorProvider + */ public function testMultipleRestRetriesWithSuccess( $errorCode, $errorBody = '{}' @@ -108,8 +108,8 @@ public function testMultipleRestRetriesWithSuccess( } /** - * @dataProvider defaultRestErrorProvider - */ + * @dataProvider defaultRestErrorProvider + */ public function testCustomRestRetryMapReplacesDefaults( $errorCode, $errorBody = '{}' @@ -137,8 +137,8 @@ public function testCustomRestRetryMapAddsNewHandlers() } /** - * @dataProvider customLimitsProvider - */ + * @dataProvider customLimitsProvider + */ public function testCustomRestRetryMapWithCustomLimits($limit) { $this->expectException(ServiceException::class); @@ -152,8 +152,8 @@ public function testCustomRestRetryMapWithCustomLimits($limit) } /** - * @dataProvider timeoutProvider - */ + * @dataProvider timeoutProvider + */ public function testRestTimeouts($config, $minTime) { $this->setRetryConfig($config); @@ -168,9 +168,9 @@ public function testRestTimeouts($config, $minTime) } /** - * @requires extension curl - * @dataProvider defaultCurlErrorProvider - */ + * @requires extension curl + * @dataProvider defaultCurlErrorProvider + */ public function testCurlRetryOffByDefault($errorCode, $errorMessage = '') { $this->expectException(ServiceException::class); @@ -179,9 +179,9 @@ public function testCurlRetryOffByDefault($errorCode, $errorMessage = '') } /** - * @requires extension curl - * @dataProvider defaultCurlErrorProvider - */ + * @requires extension curl + * @dataProvider defaultCurlErrorProvider + */ public function testOneCurlRetryWithError($errorCode, $errorMessage = '') { $this->expectException(ServiceException::class); @@ -191,9 +191,9 @@ public function testOneCurlRetryWithError($errorCode, $errorMessage = '') } /** - * @requires extension curl - * @dataProvider defaultCurlErrorProvider - */ + * @requires extension curl + * @dataProvider defaultCurlErrorProvider + */ public function testMultipleCurlRetriesWithErrors( $errorCode, $errorMessage = '' @@ -205,9 +205,9 @@ public function testMultipleCurlRetriesWithErrors( } /** - * @requires extension curl - * @dataProvider defaultCurlErrorProvider - */ + * @requires extension curl + * @dataProvider defaultCurlErrorProvider + */ public function testOneCurlRetryWithSuccess($errorCode, $errorMessage = '') { $this->setRetryConfig(['retries' => 1]); @@ -219,9 +219,9 @@ public function testOneCurlRetryWithSuccess($errorCode, $errorMessage = '') } /** - * @requires extension curl - * @dataProvider defaultCurlErrorProvider - */ + * @requires extension curl + * @dataProvider defaultCurlErrorProvider + */ public function testMultipleCurlRetriesWithSuccess( $errorCode, $errorMessage = '' @@ -235,9 +235,9 @@ public function testMultipleCurlRetriesWithSuccess( } /** - * @requires extension curl - * @dataProvider defaultCurlErrorProvider - */ + * @requires extension curl + * @dataProvider defaultCurlErrorProvider + */ public function testCustomCurlRetryMapReplacesDefaults( $errorCode, $errorMessage = '' @@ -251,8 +251,8 @@ public function testCustomCurlRetryMapReplacesDefaults( } /** - * @requires extension curl - */ + * @requires extension curl + */ public function testCustomCurlRetryMapAddsNewHandlers() { $this->setRetryMap( @@ -268,9 +268,9 @@ public function testCustomCurlRetryMapAddsNewHandlers() } /** - * @requires extension curl - * @dataProvider customLimitsProvider - */ + * @requires extension curl + * @dataProvider customLimitsProvider + */ public function testCustomCurlRetryMapWithCustomLimits($limit) { $this->expectException(ServiceException::class); @@ -285,9 +285,9 @@ public function testCustomCurlRetryMapWithCustomLimits($limit) } /** - * @requires extension curl - * @dataProvider timeoutProvider - */ + * @requires extension curl + * @dataProvider timeoutProvider + */ public function testCurlTimeouts($config, $minTime) { $this->setRetryConfig($config); @@ -302,8 +302,8 @@ public function testCurlTimeouts($config, $minTime) } /** - * @dataProvider badTaskConfigProvider - */ + * @dataProvider badTaskConfigProvider + */ public function testBadTaskConfig($config, $message) { $this->expectException(TaskException::class); @@ -318,8 +318,8 @@ public function testBadTaskConfig($config, $message) } /** - * @expectedExceptionMessage must be a valid callable - */ + * @expectedExceptionMessage must be a valid callable + */ public function testBadTaskCallback() { $this->expectException(TaskException::class); @@ -374,8 +374,8 @@ public function testMultipleTaskRetriesWithSuccess() } /** - * @dataProvider customLimitsProvider - */ + * @dataProvider customLimitsProvider + */ public function testTaskRetryWithCustomLimits($limit) { $this->expectException(ServiceException::class); @@ -386,8 +386,8 @@ public function testTaskRetryWithCustomLimits($limit) } /** - * @dataProvider timeoutProvider - */ + * @dataProvider timeoutProvider + */ public function testTaskTimeouts($config, $minTime) { $this->setRetryConfig($config); @@ -426,10 +426,10 @@ public function testTaskWithManualRetries() } /** - * Provider for backoff configurations and expected minimum runtimes. - * - * @return array - */ + * Provider for backoff configurations and expected minimum runtimes. + * + * @return array + */ public function timeoutProvider() { $config = ['initial_delay' => .001, 'max_delay' => .01]; @@ -444,10 +444,10 @@ public function timeoutProvider() } /** - * Provider for custom retry limits. - * - * @return array - */ + * Provider for custom retry limits. + * + * @return array + */ public function customLimitsProvider() { return [ @@ -457,10 +457,10 @@ public function customLimitsProvider() } /** - * Provider for invalid task configurations. - * - * @return array - */ + * Provider for invalid task configurations. + * + * @return array + */ public function badTaskConfigProvider() { return [ @@ -473,10 +473,10 @@ public function badTaskConfigProvider() } /** - * Provider for the default REST errors. - * - * @return array - */ + * Provider for the default REST errors. + * + * @return array + */ public function defaultRestErrorProvider() { return [ @@ -488,10 +488,10 @@ public function defaultRestErrorProvider() } /** - * Provider for the default cURL errors. - * - * @return array - */ + * Provider for the default cURL errors. + * + * @return array + */ public function defaultCurlErrorProvider() { return [ @@ -504,16 +504,16 @@ public function defaultCurlErrorProvider() } /** - * Assert the minimum amount of time required to run a task. - * - * NOTE: Intentionally crude for brevity. - * - * @param float $expected The expected minimum execution time - * @param callable $callback The task to time - * @param float $delta Allowable relative error - * - * @throws PHPUnit_Framework_ExpectationFailedException - */ + * Assert the minimum amount of time required to run a task. + * + * NOTE: Intentionally crude for brevity. + * + * @param float $expected The expected minimum execution time + * @param callable $callback The task to time + * @param float $delta Allowable relative error + * + * @throws PHPUnit_Framework_ExpectationFailedException + */ public static function assertTaskTimeGreaterThanOrEqual( $expected, $callback, @@ -533,10 +533,10 @@ public static function assertTaskTimeGreaterThanOrEqual( } /** - * Sets the task runner configurations. - * - * @param array $config The task runner configurations - */ + * Sets the task runner configurations. + * + * @param array $config The task runner configurations + */ private function setRetryConfig(array $config) { $config += [ @@ -555,15 +555,15 @@ private function setRetryMap(array $retryMap) } /** - * Sets the next responses. - * - * @param integer $count The number of responses - * @param string $code The response code - * @param string $body The response body - * @param array $headers The response headers - * - * @return TaskTest - */ + * Sets the next responses. + * + * @param integer $count The number of responses + * @param string $code The response code + * @param string $body The response body + * @param array $headers The response headers + * + * @return TaskTest + */ private function setNextResponses( $count, $code = '200', @@ -578,14 +578,14 @@ private function setNextResponses( } /** - * Sets the next response. - * - * @param string $code The response code - * @param string $body The response body - * @param array $headers The response headers - * - * @return TaskTest - */ + * Sets the next response. + * + * @param string $code The response code + * @param string $body The response body + * @param array $headers The response headers + * + * @return TaskTest + */ private function setNextResponse( $code = '200', $body = '{}', @@ -601,14 +601,14 @@ private function setNextResponse( } /** - * Forces the next responses to throw an IO exception. - * - * @param integer $count The number of responses - * @param string $message The exception messages - * @param string $code The exception code - * - * @return TaskTest - */ + * Forces the next responses to throw an IO exception. + * + * @param integer $count The number of responses + * @param string $message The exception messages + * @param string $code The exception code + * + * @return TaskTest + */ private function setNextResponsesThrow($count, $message, $code) { while ($count-- > 0) { @@ -619,13 +619,13 @@ private function setNextResponsesThrow($count, $message, $code) } /** - * Forces the next response to throw an IO exception. - * - * @param string $message The exception messages - * @param string $code The exception code - * - * @return TaskTest - */ + * Forces the next response to throw an IO exception. + * + * @param string $message The exception messages + * @param string $code The exception code + * + * @return TaskTest + */ private function setNextResponseThrows($message, $code) { $this->mockedCalls[$this->mockedCallsCount++] = new ServiceException( @@ -639,10 +639,10 @@ private function setNextResponseThrows($message, $code) } /** - * Runs the defined request. - * - * @return array - */ + * Runs the defined request. + * + * @return array + */ private function makeRequest() { $request = new Request('GET', '/test'); @@ -666,12 +666,12 @@ private function makeRequest() } /** - * Gets the next mocked response. - * - * @param GoogleRequest $request The mocked request - * - * @return GoogleRequest - */ + * Gets the next mocked response. + * + * @param GoogleRequest $request The mocked request + * + * @return GoogleRequest + */ public function getNextMockedCall($request) { $current = $this->mockedCalls[$this->currentMockedCall++]; @@ -692,12 +692,12 @@ public function getNextMockedCall($request) } /** - * Sets the next task return value. - * - * @param mixed $value The next return value - * - * @return TaskTest - */ + * Sets the next task return value. + * + * @param mixed $value The next return value + * + * @return TaskTest + */ private function setNextTaskReturnValue($value) { $this->mockedCalls[$this->mockedCallsCount++] = $value; @@ -705,12 +705,12 @@ private function setNextTaskReturnValue($value) } /** - * Sets the next exception `allowedRetries()` return value. - * - * @param boolean $allowedRetries The next `allowedRetries()` return value. - * - * @return TaskTest - */ + * Sets the next exception `allowedRetries()` return value. + * + * @param boolean $allowedRetries The next `allowedRetries()` return value. + * + * @return TaskTest + */ private function setNextTaskAllowedRetries($allowedRetries) { $this->mockedCalls[$this->mockedCallsCount++] = $allowedRetries; @@ -718,13 +718,13 @@ private function setNextTaskAllowedRetries($allowedRetries) } /** - * Sets multiple exception `allowedRetries()` return value. - * - * @param integer $count The number of `allowedRetries()` return values. - * @param boolean $allowedRetries The `allowedRetries()` return value. - * - * @return TaskTest - */ + * Sets multiple exception `allowedRetries()` return value. + * + * @param integer $count The number of `allowedRetries()` return values. + * @param boolean $allowedRetries The `allowedRetries()` return value. + * + * @return TaskTest + */ private function setNextTasksAllowedRetries($count, $allowedRetries) { while ($count-- > 0) { @@ -735,10 +735,10 @@ private function setNextTasksAllowedRetries($count, $allowedRetries) } /** - * Runs the defined task. - * - * @return mixed - */ + * Runs the defined task. + * + * @return mixed + */ private function runTask() { $task = new Runner( @@ -769,10 +769,10 @@ private function runTask() } /** - * Gets the next task return value. - * - * @return mixed - */ + * Gets the next task return value. + * + * @return mixed + */ public function runNextTask() { $current = $this->mockedCalls[$this->currentMockedCall++]; From 94e1964a7e0fd848eafdb1b485e0b68b9b5f08f1 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 31 May 2022 07:46:57 -0700 Subject: [PATCH 233/301] chore: increment VERSION constant --- src/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index 20f70044f..8998da321 100644 --- a/src/Client.php +++ b/src/Client.php @@ -51,7 +51,7 @@ */ class Client { - const LIBVER = "2.12.1"; + const LIBVER = "2.12.5"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; From 8ec5b5f8d821fb1470b1ba1f41783d429d606475 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 6 Jun 2022 15:59:38 -0400 Subject: [PATCH 234/301] fix: MediaFileUpload fatal error (#2274) --- src/Client.php | 2 +- src/Http/MediaFileUpload.php | 2 +- src/Http/REST.php | 18 +++++++++++------- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Client.php b/src/Client.php index 8998da321..d86dc44d6 100644 --- a/src/Client.php +++ b/src/Client.php @@ -876,7 +876,7 @@ public function prepareScopes() * * @template T * @param RequestInterface $request - * @param class-string $expectedClass + * @param class-string|false|null $expectedClass * @throws \Google\Exception * @return mixed|T|ResponseInterface */ diff --git a/src/Http/MediaFileUpload.php b/src/Http/MediaFileUpload.php index 25c98938b..2713ea415 100644 --- a/src/Http/MediaFileUpload.php +++ b/src/Http/MediaFileUpload.php @@ -306,7 +306,7 @@ private function fetchResumeUri() $this->request = $this->request->withHeader($key, $value); } - $response = $this->client->execute($this->request, null); + $response = $this->client->execute($this->request, false); $location = $response->getHeaderLine('location'); $code = $response->getStatusCode(); diff --git a/src/Http/REST.php b/src/Http/REST.php index 0e7c11e5d..1519f60da 100644 --- a/src/Http/REST.php +++ b/src/Http/REST.php @@ -35,12 +35,13 @@ class REST * Executes a Psr\Http\Message\RequestInterface and (if applicable) automatically retries * when errors occur. * + * @template T * @param ClientInterface $client * @param RequestInterface $request - * @param string $expectedClass + * @param class-string|false|null $expectedClass * @param array $config * @param array $retryMap - * @return mixed decoded result + * @return mixed|T|null * @throws \Google\Service\Exception on server side error (ie: not authenticated, * invalid or malformed post body, invalid url) */ @@ -68,10 +69,11 @@ public static function execute( /** * Executes a Psr\Http\Message\RequestInterface * + * @template T * @param ClientInterface $client * @param RequestInterface $request - * @param string $expectedClass - * @return array decoded result + * @param class-string|false|null $expectedClass + * @return mixed|T|null * @throws \Google\Service\Exception on server side error (ie: not authenticated, * invalid or malformed post body, invalid url) */ @@ -108,11 +110,13 @@ interface_exists('\GuzzleHttp\Message\ResponseInterface') /** * Decode an HTTP Response. * @static - * @throws \Google\Service\Exception + * + * @template T * @param RequestInterface $response The http response to be decoded. * @param ResponseInterface $response - * @param string $expectedClass - * @return mixed|null + * @param class-string|false|null $expectedClass + * @return mixed|T|null + * @throws \Google\Service\Exception */ public static function decodeHttpResponse( ResponseInterface $response, From f92aa126903a9e2da5bd41a280d9633cb249e79e Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 6 Jun 2022 13:00:19 -0700 Subject: [PATCH 235/301] chore: increment client version to 2.12.6 --- src/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index d86dc44d6..0b44dfae3 100644 --- a/src/Client.php +++ b/src/Client.php @@ -51,7 +51,7 @@ */ class Client { - const LIBVER = "2.12.5"; + const LIBVER = "2.12.6"; const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; From 5738766b3f5150f88916395784d22f70abf80f07 Mon Sep 17 00:00:00 2001 From: Michiel Devriese Date: Tue, 7 Jun 2022 22:12:27 +0200 Subject: [PATCH 236/301] chore: correct phpdoc types for Exception::getErrors (#2271) --- src/Service/Exception.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Service/Exception.php b/src/Service/Exception.php index 3212611a6..45e72d7b8 100644 --- a/src/Service/Exception.php +++ b/src/Service/Exception.php @@ -33,7 +33,7 @@ class Exception extends GoogleException * @param string $message * @param int $code * @param Exception|null $previous - * @param array $errors List of errors returned in an HTTP + * @param array> $errors List of errors returned in an HTTP * response. Defaults to []. */ public function __construct( @@ -54,15 +54,17 @@ public function __construct( /** * An example of the possible errors returned. * - * { - * "domain": "global", - * "reason": "authError", - * "message": "Invalid Credentials", - * "locationType": "header", - * "location": "Authorization", - * } + * [ + * { + * "domain": "global", + * "reason": "authError", + * "message": "Invalid Credentials", + * "locationType": "header", + * "location": "Authorization", + * } + * ] * - * @return array List of errors return in an HTTP response or []. + * @return array> List of errors return in an HTTP response or []. */ public function getErrors() { From 5602ba6f004eabc235c51f2d7151b9276c4eeb87 Mon Sep 17 00:00:00 2001 From: erikn69 Date: Wed, 15 Jun 2022 14:44:56 -0500 Subject: [PATCH 237/301] chore: phpstan.neon.dist export-ignore (#2281) --- .gitattributes | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitattributes b/.gitattributes index e98a4d1b3..63ede231b 100644 --- a/.gitattributes +++ b/.gitattributes @@ -10,3 +10,4 @@ /style export-ignore /tests export-ignore /UPGRADING.md export-ignore +/phpstan.neon.dist export-ignore From a69131b6488735d112a529a278cfc8b875e18647 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 16 Jun 2022 18:25:50 -0400 Subject: [PATCH 238/301] fix: ensure new redirect_uri propogates to OAuth2 class (#2282) --- src/Client.php | 1 + tests/Google/ClientTest.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/Client.php b/src/Client.php index 0b44dfae3..cfb59bbb4 100644 --- a/src/Client.php +++ b/src/Client.php @@ -386,6 +386,7 @@ public function createAuthUrl($scope = null) 'login_hint' => $this->config['login_hint'], 'openid.realm' => $this->config['openid.realm'], 'prompt' => $this->config['prompt'], + 'redirect_uri' => $this->config['redirect_uri'], 'response_type' => 'code', 'scope' => $scope, 'state' => $this->config['state'], diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 23eebd562..5046f38bf 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -1015,4 +1015,20 @@ public function testCredentialsOptionWithCredentialsLoader() $this->assertNotNull($authHeader); $this->assertEquals('Bearer abc', $authHeader); } + + public function testSetNewRedirectUri() + { + $client = new Client(); + $redirectUri1 = 'https://foo.com/test1'; + $client->setRedirectUri($redirectUri1); + + $authUrl1 = $client->createAuthUrl(); + $this->assertStringContainsString(urlencode($redirectUri1), $authUrl1); + + $redirectUri2 = 'https://foo.com/test2'; + $client->setRedirectUri($redirectUri2); + + $authUrl2 = $client->createAuthUrl(); + $this->assertStringContainsString(urlencode($redirectUri2), $authUrl2); + } } From 654c0e29ab78aba8bfef52fd3d06a3b2b39c4e0d Mon Sep 17 00:00:00 2001 From: Konstantin Kopachev Date: Mon, 18 Jul 2022 15:23:54 -0700 Subject: [PATCH 239/301] fix: don't send content-type header if no post body exists (#2288) --- src/Service/Resource.php | 2 +- tests/Google/Service/ResourceTest.php | 39 +++++++++++++++++++++------ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/Service/Resource.php b/src/Service/Resource.php index b8b08d3cd..ecf402b18 100644 --- a/src/Service/Resource.php +++ b/src/Service/Resource.php @@ -203,7 +203,7 @@ public function call($name, $arguments, $expectedClass = null) $request = new Request( $method['httpMethod'], $url, - ['content-type' => 'application/json'], + $postBody ? ['content-type' => 'application/json'] : [], $postBody ? json_encode($postBody) : '' ); diff --git a/tests/Google/Service/ResourceTest.php b/tests/Google/Service/ResourceTest.php index 870286608..c2c814d4e 100644 --- a/tests/Google/Service/ResourceTest.php +++ b/tests/Google/Service/ResourceTest.php @@ -96,15 +96,38 @@ public function testCall() "methods" => [ "testMethod" => [ "parameters" => [], - "path" => "method/path", - "httpMethod" => "POST", - ] + "path" => "method/path", + "httpMethod" => "POST", + ] ] ] ); $request = $resource->call("testMethod", [[]]); $this->assertEquals("https://test.example.com/method/path", (string) $request->getUri()); $this->assertEquals("POST", $request->getMethod()); + $this->assertFalse($request->hasHeader('Content-Type')); + } + + public function testCallWithPostBody() + { + $resource = new GoogleResource( + $this->service, + "test", + "testResource", + [ + "methods" => [ + "testMethod" => [ + "parameters" => [], + "path" => "method/path", + "httpMethod" => "POST", + ] + ] + ] + ); + $request = $resource->call("testMethod", [['postBody' => ['foo' => 'bar']]]); + $this->assertEquals("https://test.example.com/method/path", (string) $request->getUri()); + $this->assertEquals("POST", $request->getMethod()); + $this->assertTrue($request->hasHeader('Content-Type')); } public function testCallServiceDefinedRoot() @@ -130,11 +153,11 @@ public function testCallServiceDefinedRoot() } /** - * Some Google Service (Google\Service\Directory\Resource\Channels and - * Google\Service\Reports\Resource\Channels) use a different servicePath value - * that should override the default servicePath value, it's represented by a / - * before the resource path. All other Services have no / before the path - */ + * Some Google Service (Google\Service\Directory\Resource\Channels and + * Google\Service\Reports\Resource\Channels) use a different servicePath value + * that should override the default servicePath value, it's represented by a / + * before the resource path. All other Services have no / before the path + */ public function testCreateRequestUriForASelfDefinedServicePath() { $this->service->servicePath = '/admin/directory/v1/'; From 59ee192d7989d58db9bdc5b0b047584df20a1935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tinjo=20Sch=C3=B6ni?= <32767367+tscni@users.noreply.github.com> Date: Tue, 23 Aug 2022 16:52:34 +0200 Subject: [PATCH 240/301] chore(docs): add null type to Service\Exception errors (#2309) --- src/Service/Exception.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Service/Exception.php b/src/Service/Exception.php index 45e72d7b8..6e88169c2 100644 --- a/src/Service/Exception.php +++ b/src/Service/Exception.php @@ -33,8 +33,8 @@ class Exception extends GoogleException * @param string $message * @param int $code * @param Exception|null $previous - * @param array> $errors List of errors returned in an HTTP - * response. Defaults to []. + * @param array>|null $errors List of errors returned in an HTTP + * response or null. Defaults to []. */ public function __construct( $message, @@ -64,7 +64,7 @@ public function __construct( * } * ] * - * @return array> List of errors return in an HTTP response or []. + * @return array>|null List of errors returned in an HTTP response or null. */ public function getErrors() { From 88cc63c38b0cf88629f66fdf8ba6006f6c6d5a2c Mon Sep 17 00:00:00 2001 From: Vishwaraj Anand Date: Thu, 15 Sep 2022 15:05:21 +0530 Subject: [PATCH 241/301] fix: lint errors (#2315) --- README.md | 2 +- examples/batch.php | 2 +- examples/idtoken.php | 2 +- examples/large-file-download.php | 2 +- examples/large-file-upload.php | 2 +- examples/multi-api.php | 2 +- examples/simple-file-upload.php | 2 +- examples/simple-query.php | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ac5d4ef48..2feb44bdc 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ The Google API Client Library enables you to work with Google APIs such as Gmail, Drive or YouTube on your server. -These client libraries are officially supported by Google. However, the libraries are considered complete and are in maintenance mode. This means that we will address critical bugs and security issues but will not add any new features. +These client libraries are officially supported by Google. However, the libraries are considered complete and are in maintenance mode. This means that we will address critical bugs and security issues but will not add any new features. ## Google Cloud Platform diff --git a/examples/batch.php b/examples/batch.php index d9b359c35..3fea6085f 100644 --- a/examples/batch.php +++ b/examples/batch.php @@ -90,4 +90,4 @@
    - - - - - - - Date: Thu, 27 Oct 2022 13:20:32 -0400 Subject: [PATCH 242/301] fix: update accounts.google.com authorization URI (#2275) --- src/Client.php | 2 +- tests/Google/ClientTest.php | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Client.php b/src/Client.php index cfb59bbb4..ba9963d6d 100644 --- a/src/Client.php +++ b/src/Client.php @@ -55,7 +55,7 @@ class Client const USER_AGENT_SUFFIX = "google-api-php-client/"; const OAUTH2_REVOKE_URI = 'https://oauth2.googleapis.com/revoke'; const OAUTH2_TOKEN_URI = 'https://oauth2.googleapis.com/token'; - const OAUTH2_AUTH_URL = 'https://accounts.google.com/o/oauth2/auth'; + const OAUTH2_AUTH_URL = 'https://accounts.google.com/o/oauth2/v2/auth'; const API_BASE_PATH = 'https://www.googleapis.com/'; /** diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 5046f38bf..391678423 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -157,7 +157,7 @@ public function testCreateAuthUrl() $client->setLoginHint('bob@example.org'); $authUrl = $client->createAuthUrl("http://googleapis.com/scope/foo"); - $expected = "https://accounts.google.com/o/oauth2/auth" + $expected = "https://accounts.google.com/o/oauth2/v2/auth" . "?response_type=code" . "&access_type=offline" . "&client_id=clientId1" @@ -176,7 +176,7 @@ public function testCreateAuthUrl() $client->setPrompt('select_account'); $client->setIncludeGrantedScopes(true); $authUrl = $client->createAuthUrl("http://googleapis.com/scope/foo"); - $expected = "https://accounts.google.com/o/oauth2/auth" + $expected = "https://accounts.google.com/o/oauth2/v2/auth" . "?response_type=code" . "&access_type=offline" . "&client_id=clientId1" @@ -233,7 +233,7 @@ public function testPrepareService() $this->assertEquals("http://test.com scope2", $scopes); $this->assertEquals( '' - . 'https://accounts.google.com/o/oauth2/auth' + . 'https://accounts.google.com/o/oauth2/v2/auth' . '?response_type=code' . '&access_type=online' . '&client_id=test1' @@ -383,7 +383,7 @@ public function testJsonConfig() // Device config $client = new Client(); $device = - '{"installed":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret"'. + '{"installed":{"auth_uri":"https://accounts.google.com/o/oauth2/v2/auth","client_secret"'. ':"N0aHCBT1qX1VAcF5J1pJAn6S","token_uri":"https://oauth2.googleapis.com/token",'. '"client_email":"","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","oob"],"client_x509_cert_url"'. ':"","client_id":"123456789.apps.googleusercontent.com","auth_provider_x509_cert_url":'. @@ -396,7 +396,7 @@ public function testJsonConfig() // Web config $client = new Client(); - $web = '{"web":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret"' . + $web = '{"web":{"auth_uri":"https://accounts.google.com/o/oauth2/v2/auth","client_secret"' . ':"lpoubuib8bj-Fmke_YhhyHGgXc","token_uri":"https://oauth2.googleapis.com/token"' . ',"client_email":"123456789@developer.gserviceaccount.com","client_x509_cert_url":'. '"https://www.googleapis.com/robot/v1/metadata/x509/123456789@developer.gserviceaccount.com"'. From 2640250c7bab479f378972733dcc0a3e9b2e14f8 Mon Sep 17 00:00:00 2001 From: geoffrey-brier Date: Mon, 19 Dec 2022 22:05:34 +0100 Subject: [PATCH 243/301] feat: make auth http client config extends from default client config (#2348) Co-authored-by: Brent Shaffer --- src/AuthHandler/Guzzle6AuthHandler.php | 7 +------ tests/Google/ClientTest.php | 8 +------- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/src/AuthHandler/Guzzle6AuthHandler.php b/src/AuthHandler/Guzzle6AuthHandler.php index 13f9ee59d..7e8a815c2 100644 --- a/src/AuthHandler/Guzzle6AuthHandler.php +++ b/src/AuthHandler/Guzzle6AuthHandler.php @@ -105,11 +105,6 @@ public function attachKey(ClientInterface $http, $key) private function createAuthHttp(ClientInterface $http) { - return new Client([ - 'base_uri' => $http->getConfig('base_uri'), - 'http_errors' => true, - 'verify' => $http->getConfig('verify'), - 'proxy' => $http->getConfig('proxy'), - ]); + return new Client(['http_errors' => true] + $http->getConfig()); } } diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 391678423..963ea071b 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -997,14 +997,8 @@ public function testCredentialsOptionWithCredentialsLoader() $httpClient = $this->prophesize('GuzzleHttp\ClientInterface'); $httpClient->getConfig() - ->shouldBeCalledOnce() + ->shouldBeCalled() ->willReturn(['handler' => $handler->reveal()]); - $httpClient->getConfig('base_uri') - ->shouldBeCalledOnce(); - $httpClient->getConfig('verify') - ->shouldBeCalledOnce(); - $httpClient->getConfig('proxy') - ->shouldBeCalledOnce(); $httpClient->send(Argument::any(), Argument::any()) ->shouldNotBeCalled(); From 1a611945593a09841a57beaddeeba6625402582d Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 19 Dec 2022 13:14:19 -0800 Subject: [PATCH 244/301] chore: remove ignore lines where phpztan now works as expected --- src/AuthHandler/AuthHandlerFactory.php | 1 - src/Client.php | 1 - 2 files changed, 2 deletions(-) diff --git a/src/AuthHandler/AuthHandlerFactory.php b/src/AuthHandler/AuthHandlerFactory.php index 67f6fc145..63c266165 100644 --- a/src/AuthHandler/AuthHandlerFactory.php +++ b/src/AuthHandler/AuthHandlerFactory.php @@ -34,7 +34,6 @@ public static function build($cache = null, array $cacheConfig = []) if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) { $guzzleVersion = ClientInterface::MAJOR_VERSION; } elseif (defined('\GuzzleHttp\ClientInterface::VERSION')) { - // @phpstan-ignore-next-line $guzzleVersion = (int) substr(ClientInterface::VERSION, 0, 1); } diff --git a/src/Client.php b/src/Client.php index ba9963d6d..3366899f7 100644 --- a/src/Client.php +++ b/src/Client.php @@ -1185,7 +1185,6 @@ protected function createDefaultHttpClient() if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) { $guzzleVersion = ClientInterface::MAJOR_VERSION; } elseif (defined('\GuzzleHttp\ClientInterface::VERSION')) { - // @phpstan-ignore-next-line $guzzleVersion = (int)substr(ClientInterface::VERSION, 0, 1); } From af40e9e9b6e42bc03e5259b52685fc3cac9dba79 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 19 Dec 2022 13:18:47 -0800 Subject: [PATCH 245/301] chore: run github actions workflow on main --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 4a2b8fced..84d9eddd4 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -2,7 +2,7 @@ name: Test Suite on: push: branches: - - master + - main pull_request: jobs: From b0940748cab3c6af9ce6be8764c6d58a2520c9e0 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 19 Dec 2022 13:41:18 -0800 Subject: [PATCH 246/301] chore: add release-please for release management --- .github/release-please.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .github/release-please.yml diff --git a/.github/release-please.yml b/.github/release-please.yml new file mode 100644 index 000000000..0a6e0cc27 --- /dev/null +++ b/.github/release-please.yml @@ -0,0 +1,3 @@ +releaseType: simple +handleGHRelease: true +primaryBranch: main From b653a338c5a658adf6df4bb2f44c2cc02fe7eb1d Mon Sep 17 00:00:00 2001 From: "release-please[bot]" <55107282+release-please[bot]@users.noreply.github.com> Date: Mon, 19 Dec 2022 14:17:11 -0800 Subject: [PATCH 247/301] chore(main): release 2.13.0 (#2349) --- CHANGELOG.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 000000000..c2e28f7c1 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,16 @@ +# Changelog + +## [2.13.0](https://github.com/googleapis/google-api-php-client/compare/v2.12.6...v2.13.0) (2022-12-19) + + +### Features + +* Make auth http client config extends from default client config ([#2348](https://github.com/googleapis/google-api-php-client/issues/2348)) ([2640250](https://github.com/googleapis/google-api-php-client/commit/2640250c7bab479f378972733dcc0a3e9b2e14f8)) + + +### Bug Fixes + +* Don't send content-type header if no post body exists ([#2288](https://github.com/googleapis/google-api-php-client/issues/2288)) ([654c0e2](https://github.com/googleapis/google-api-php-client/commit/654c0e29ab78aba8bfef52fd3d06a3b2b39c4e0d)) +* Ensure new redirect_uri propogates to OAuth2 class ([#2282](https://github.com/googleapis/google-api-php-client/issues/2282)) ([a69131b](https://github.com/googleapis/google-api-php-client/commit/a69131b6488735d112a529a278cfc8b875e18647)) +* Lint errors ([#2315](https://github.com/googleapis/google-api-php-client/issues/2315)) ([88cc63c](https://github.com/googleapis/google-api-php-client/commit/88cc63c38b0cf88629f66fdf8ba6006f6c6d5a2c)) +* Update accounts.google.com authorization URI ([#2275](https://github.com/googleapis/google-api-php-client/issues/2275)) ([b2624d2](https://github.com/googleapis/google-api-php-client/commit/b2624d21fce894126b9975a872cf5cda8038b254)) From f2791c27235b9306895d7ce3244cb2b690f6b5c8 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 9 Jan 2023 14:25:32 -0800 Subject: [PATCH 248/301] feat: add support for PHP 8.2 (#2350) --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 84d9eddd4..3e81e73b4 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -11,7 +11,7 @@ jobs: strategy: fail-fast: false matrix: - php: [ "5.6", "7.0", "7.1", "7.2", "7.3", "7.4", "8.0", "8.1" ] + php: [ "5.6", "7.0", "7.1", "7.2", "7.3", "7.4", "8.0", "8.1", "8.2" ] composer-flags: [""] include: - php: "5.6" From cabac122ddbe19298913ed2da4e086dad41e31dd Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 15 Feb 2023 14:06:48 -0500 Subject: [PATCH 249/301] chore: add link to ref docs in README (#2384) --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 2feb44bdc..0e623c0c7 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,10 @@ # Google APIs Client Library for PHP # +**NOTE**: please check to see if the package you'd like to install is available in our +list of [Google cloud packages](https://cloud.google.com/php/docs/reference) first, as +these are the recommended libraries. +
    Reference Docs
    https://googleapis.github.io/google-api-php-client/main/
    License
    Apache 2.0
    From 11080d5e85a040751a13aca8131f93c7d910db11 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 13 Mar 2023 14:04:24 -0600 Subject: [PATCH 250/301] fix: allow dynamic properties on model classes (#2408) --- src/Model.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Model.php b/src/Model.php index 6dea45523..95328f5d3 100644 --- a/src/Model.php +++ b/src/Model.php @@ -28,6 +28,7 @@ * http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5 * */ +#[\AllowDynamicProperties] class Model implements \ArrayAccess { /** From 895749dd4f14c349ca0b4aad020ff973f961e519 Mon Sep 17 00:00:00 2001 From: "release-please[bot]" <55107282+release-please[bot]@users.noreply.github.com> Date: Mon, 13 Mar 2023 13:38:21 -0700 Subject: [PATCH 251/301] chore(main): release 2.13.1 (#2367) --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2e28f7c1..e5e516a65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [2.13.1](https://github.com/googleapis/google-api-php-client/compare/v2.13.0...v2.13.1) (2023-03-13) + + +### Bug Fixes + +* Allow dynamic properties on model classes ([#2408](https://github.com/googleapis/google-api-php-client/issues/2408)) ([11080d5](https://github.com/googleapis/google-api-php-client/commit/11080d5e85a040751a13aca8131f93c7d910db11)) + ## [2.13.0](https://github.com/googleapis/google-api-php-client/compare/v2.12.6...v2.13.0) (2022-12-19) From 5ed4edc9315110a715e9763d27ee6761e1aaa00a Mon Sep 17 00:00:00 2001 From: Carlos Granados Date: Fri, 17 Mar 2023 12:15:09 +0100 Subject: [PATCH 252/301] fix: calling class_exists with null in Google\Model (#2405) --- src/Model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model.php b/src/Model.php index 95328f5d3..87f437d66 100644 --- a/src/Model.php +++ b/src/Model.php @@ -294,7 +294,7 @@ protected function keyType($key) $keyType = $key . "Type"; // ensure keyType is a valid class - if (property_exists($this, $keyType) && class_exists($this->$keyType)) { + if (property_exists($this, $keyType) && $this->$keyType !== null && class_exists($this->$keyType)) { return $this->$keyType; } } From 7bac7a5b8710962e05c376a40b8216393ce2fe6d Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 22 Mar 2023 17:01:45 -0700 Subject: [PATCH 253/301] chore(docs): ensure doc generation fails on error --- .github/actions/docs/entrypoint.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/actions/docs/entrypoint.sh b/.github/actions/docs/entrypoint.sh index 203f98e62..08d50b049 100755 --- a/.github/actions/docs/entrypoint.sh +++ b/.github/actions/docs/entrypoint.sh @@ -1,5 +1,7 @@ #!/bin/sh -l +set -e + apt-get update apt-get install -y git wget git reset --hard HEAD From bd7b138bbc1c5016b7e85ff85f0533ae3684b9ed Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 22 Mar 2023 17:09:12 -0700 Subject: [PATCH 254/301] chore(docs): attempt to fix github "detected dubious ownership" error --- .github/actions/docs/entrypoint.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/actions/docs/entrypoint.sh b/.github/actions/docs/entrypoint.sh index 08d50b049..7ac74e427 100755 --- a/.github/actions/docs/entrypoint.sh +++ b/.github/actions/docs/entrypoint.sh @@ -4,6 +4,10 @@ set -e apt-get update apt-get install -y git wget + +# Fix github "detected dubious ownership" error +git config --global --add safe.directory /github/workspace + git reset --hard HEAD # Create the directories From 53c3168fd1836ec21d28a768f78a8c0e44046ec4 Mon Sep 17 00:00:00 2001 From: "release-please[bot]" <55107282+release-please[bot]@users.noreply.github.com> Date: Thu, 6 Apr 2023 07:59:47 -0700 Subject: [PATCH 255/301] chore(main): release 2.13.2 (#2412) --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5e516a65..f484d6fc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [2.13.2](https://github.com/googleapis/google-api-php-client/compare/v2.13.1...v2.13.2) (2023-03-23) + + +### Bug Fixes + +* Calling class_exists with null in Google\Model ([#2405](https://github.com/googleapis/google-api-php-client/issues/2405)) ([5ed4edc](https://github.com/googleapis/google-api-php-client/commit/5ed4edc9315110a715e9763d27ee6761e1aaa00a)) + ## [2.13.1](https://github.com/googleapis/google-api-php-client/compare/v2.13.0...v2.13.1) (2023-03-13) From 74a7d7b838acb08afc02b449f338fbe6577cb03c Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 11 May 2023 12:45:54 -0700 Subject: [PATCH 256/301] feat: user-supplied query params for auth url (#2432) --- README.md | 22 ++++++++++++++++++++++ src/Client.php | 5 +++-- tests/Google/ClientTest.php | 10 ++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0e623c0c7..d9c653ae1 100644 --- a/README.md +++ b/README.md @@ -422,6 +422,28 @@ $client->setHttpClient($httpClient); Other Guzzle features such as [Handlers and Middleware](http://docs.guzzlephp.org/en/stable/handlers-and-middleware.html) offer even more control. +### Partial Consent and Granted Scopes + +When using OAuth2 3LO (e.g. you're a client requesting credentials from a 3rd +party, such as in the [simple file upload example](examples/simple-file-upload.php)), +you may want to take advantage of Partial Consent. + +To allow clients to only grant certain scopes in the OAuth2 screen, pass the +querystring parameter for `enable_serial_consent` when generating the +authorization URL: + +```php +$authUrl = $client->createAuthUrl($scope, ['enable_serial_consent' => 'true']); +``` + +Once the flow is completed, you can see which scopes were granted by calling +`getGrantedScope` on the OAuth2 object: + +```php +// Space-separated string of granted scopes if it exists, otherwise null. +echo $client->getOAuth2Service()->getGrantedScope(); +``` + ### Service Specific Examples ### YouTube: https://github.com/youtube/api-samples/tree/master/php diff --git a/src/Client.php b/src/Client.php index 3366899f7..383726160 100644 --- a/src/Client.php +++ b/src/Client.php @@ -357,9 +357,10 @@ public function fetchAccessTokenWithRefreshToken($refreshToken = null) * The authorization endpoint allows the user to first * authenticate, and then grant/deny the access request. * @param string|array $scope The scope is expressed as an array or list of space-delimited strings. + * @param array $queryParams Querystring params to add to the authorization URL. * @return string */ - public function createAuthUrl($scope = null) + public function createAuthUrl($scope = null, array $queryParams = []) { if (empty($scope)) { $scope = $this->prepareScopes(); @@ -390,7 +391,7 @@ public function createAuthUrl($scope = null) 'response_type' => 'code', 'scope' => $scope, 'state' => $this->config['state'], - ]); + ]) + $queryParams; // If the list of scopes contains plus.login, add request_visible_actions // to auth URL. diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 963ea071b..04da74ef9 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -1025,4 +1025,14 @@ public function testSetNewRedirectUri() $authUrl2 = $client->createAuthUrl(); $this->assertStringContainsString(urlencode($redirectUri2), $authUrl2); } + + public function testQueryParamsForAuthUrl() + { + $client = new Client(); + $client->setRedirectUri('https://example.com/'); + $authUrl1 = $client->createAuthUrl(null, [ + 'enable_serial_consent' => 'true' + ]); + $this->assertStringContainsString('&enable_serial_consent=true', $authUrl1); + } } From 8a0c1cc8b564bf9a8c02c8bb2fd817b8e9f72d7d Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 11 May 2023 14:48:31 -0700 Subject: [PATCH 257/301] chore: fix tests for mock updates --- tests/Google/ServiceTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/Google/ServiceTest.php b/tests/Google/ServiceTest.php index 75d49bfbe..a6f40539e 100644 --- a/tests/Google/ServiceTest.php +++ b/tests/Google/ServiceTest.php @@ -28,6 +28,7 @@ use Prophecy\Argument; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\StreamInterface; class TestModel extends Model { @@ -66,7 +67,13 @@ class ServiceTest extends TestCase public function testCreateBatch() { + $body = $this->prophesize(StreamInterface::class); + $body->__toString()->willReturn(''); $response = $this->prophesize(ResponseInterface::class); + $response->getHeaderLine('content-type') + ->willReturn(''); + $response->getBody() + ->willReturn($body->reveal()); $client = $this->prophesize(Client::class); $client->execute( From 789c8b07cad97f420ac0467c782036f955a2ad89 Mon Sep 17 00:00:00 2001 From: "release-please[bot]" <55107282+release-please[bot]@users.noreply.github.com> Date: Thu, 11 May 2023 14:54:55 -0700 Subject: [PATCH 258/301] chore(main): release 2.14.0 (#2441) --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f484d6fc1..b24515ba3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [2.14.0](https://github.com/googleapis/google-api-php-client/compare/v2.13.2...v2.14.0) (2023-05-11) + + +### Features + +* User-supplied query params for auth url ([#2432](https://github.com/googleapis/google-api-php-client/issues/2432)) ([74a7d7b](https://github.com/googleapis/google-api-php-client/commit/74a7d7b838acb08afc02b449f338fbe6577cb03c)) + ## [2.13.2](https://github.com/googleapis/google-api-php-client/compare/v2.13.1...v2.13.2) (2023-03-23) From c765b379e95ab272b6a87aa802d9f5507eaeb2e7 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 16 May 2023 14:14:58 -0600 Subject: [PATCH 259/301] feat: drop support for 7.3 and below (#2431) Release-As: v2.15.0 --- .github/sync-repo-settings.yaml | 11 +- .github/workflows/asset-release.yml | 2 +- .github/workflows/docs.yml | 2 +- .github/workflows/tests.yml | 15 +- README.md | 2 +- composer.json | 19 ++- phpstan.neon.dist | 3 - phpunit.xml.dist | 31 ++-- src/AccessToken/Verify.php | 94 +++--------- src/AuthHandler/AuthHandlerFactory.php | 4 +- src/AuthHandler/Guzzle5AuthHandler.php | 108 ------------- src/Collection.php | 1 + src/aliases.php | 4 - tests/BaseTest.php | 95 +----------- tests/Google/AccessToken/RevokeTest.php | 81 +--------- tests/Google/AccessToken/VerifyTest.php | 32 +--- tests/Google/CacheTest.php | 1 - tests/Google/ClientTest.php | 193 +++++------------------- tests/Google/Http/RESTTest.php | 2 +- tests/Google/Service/AdSenseTest.php | 2 +- tests/Google/Service/ResourceTest.php | 94 +++--------- tests/Google/Service/TasksTest.php | 2 +- tests/Google/Service/YouTubeTest.php | 2 +- tests/Google/ServiceTest.php | 16 +- tests/Google/Task/RunnerTest.php | 29 +--- 25 files changed, 135 insertions(+), 710 deletions(-) delete mode 100644 src/AuthHandler/Guzzle5AuthHandler.php diff --git a/.github/sync-repo-settings.yaml b/.github/sync-repo-settings.yaml index 6e411b4c9..1fa17b12f 100644 --- a/.github/sync-repo-settings.yaml +++ b/.github/sync-repo-settings.yaml @@ -5,15 +5,12 @@ branchProtectionRules: - pattern: master isAdminEnforced: true requiredStatusCheckContexts: - - 'PHP 5.6 Unit Test' - - 'PHP 5.6 --prefer-lowest Unit Test' - - 'PHP 7.0 Unit Test' - - 'PHP 7.1 Unit Test' - - 'PHP 7.2 Unit Test' - - 'PHP 7.3 Unit Test' - 'PHP 7.4 Unit Test' + - 'PHP 7.4 --prefer-lowest Unit Test' - 'PHP 8.0 Unit Test' - - 'PHP 8.0 --prefer-lowest Unit Test' + - 'PHP 8.1 Unit Test' + - 'PHP 8.2 Unit Test' + - 'PHP 8.2 --prefer-lowest Unit Test' - 'PHP Style Check' - 'cla/google' requiredApprovingReviewCount: 1 diff --git a/.github/workflows/asset-release.yml b/.github/workflows/asset-release.yml index 6f86fdecf..59d5cdd56 100644 --- a/.github/workflows/asset-release.yml +++ b/.github/workflows/asset-release.yml @@ -10,7 +10,7 @@ jobs: strategy: matrix: operating-system: [ ubuntu-latest ] - php: [ "5.6", "7.0", "7.4", "8.0" ] + php: [ "7.4", "8.0", "8.2" ] name: Upload Release Assets steps: diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 0c6125ad8..02d757bc1 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -18,7 +18,7 @@ jobs: max_attempts: 3 command: composer install - name: Generate and Push Documentation - uses: docker://php:7.3-cli + uses: docker://php:7.4-cli env: GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} with: diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3e81e73b4..df9f1565a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -11,12 +11,12 @@ jobs: strategy: fail-fast: false matrix: - php: [ "5.6", "7.0", "7.1", "7.2", "7.3", "7.4", "8.0", "8.1", "8.2" ] + php: [ "7.4", "8.0", "8.1", "8.2" ] composer-flags: [""] include: - - php: "5.6" + - php: "7.4" composer-flags: "--prefer-lowest " - - php: "8.0" + - php: "8.2" composer-flags: "--prefer-lowest " name: PHP ${{ matrix.php }} ${{ matrix.composer-flags }}Unit Test steps: @@ -26,14 +26,11 @@ jobs: with: php-version: ${{ matrix.php }} - name: Install Dependencies - uses: nick-invision/retry@v1 + uses: nick-invision/retry@v2 with: timeout_minutes: 10 max_attempts: 3 command: composer update ${{ matrix.composer-flags }} - - if: ${{ matrix.php == '8.0' && matrix.composer-flags == '--prefer-lowest ' }} - name: Update guzzlehttp/ringphp dependency - run: composer update guzzlehttp/ringphp - name: Run Script run: vendor/bin/phpunit @@ -45,9 +42,9 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: "7.3" + php-version: "7.4" - name: Install Dependencies - uses: nick-invision/retry@v1 + uses: nick-invision/retry@v2 with: timeout_minutes: 10 max_attempts: 3 diff --git a/README.md b/README.md index d9c653ae1..1beb6c88d 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ For Google Cloud Platform APIs such as [Datastore][cloud-datastore], [Cloud Stor [cloud-compute]: https://github.com/googleapis/google-cloud-php-compute ## Requirements ## -* [PHP 5.6.0 or higher](https://www.php.net/) +* [PHP 7.4 or higher](https://www.php.net/) ## Developer Documentation ## diff --git a/composer.json b/composer.json index 9c3b7d233..70ca62945 100644 --- a/composer.json +++ b/composer.json @@ -6,25 +6,24 @@ "homepage": "http://developers.google.com/api-client-library/php", "license": "Apache-2.0", "require": { - "php": "^5.6|^7.0|^8.0", - "google/auth": "^1.10", + "php": "^7.4|^8.0", + "google/auth": "^1.26", "google/apiclient-services": "~0.200", - "firebase/php-jwt": "~2.0||~3.0||~4.0||~5.0||~6.0", - "monolog/monolog": "^1.17||^2.0||^3.0", - "phpseclib/phpseclib": "~2.0||^3.0.2", - "guzzlehttp/guzzle": "~5.3.3||~6.0||~7.0", + "firebase/php-jwt": "~6.0", + "monolog/monolog": "^2.9||^3.0", + "phpseclib/phpseclib": "^3.0.2", + "guzzlehttp/guzzle": "~6.5||~7.0", "guzzlehttp/psr7": "^1.8.4||^2.2.1" }, "require-dev": { "squizlabs/php_codesniffer": "^3.0", "symfony/dom-crawler": "~2.1", "symfony/css-selector": "~2.1", - "cache/filesystem-adapter": "^0.3.2|^1.1", + "cache/filesystem-adapter": "^1.1", "phpcompatibility/php-compatibility": "^9.2", "composer/composer": "^1.10.22", - "yoast/phpunit-polyfills": "^1.0", - "phpspec/prophecy-phpunit": "^1.1||^2.0", - "phpunit/phpunit": "^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0" + "phpspec/prophecy-phpunit": "^2.0", + "phpunit/phpunit": "^9.5" }, "suggest": { "cache/filesystem-adapter": "For caching certs and tokens (using Google\\Client::setCache)" diff --git a/phpstan.neon.dist b/phpstan.neon.dist index a70dd0e9a..dcef11342 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -3,6 +3,3 @@ parameters: level: 5 paths: - src - excludePaths: - - src/AuthHandler/Guzzle5AuthHandler.php - diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 0063e91f1..1e07db961 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,19 +1,16 @@ - - - - tests/Google - - - tests/examples - - - - - ./src - - + + + + ./src + + + + + tests/Google + + + tests/examples + + diff --git a/src/AccessToken/Verify.php b/src/AccessToken/Verify.php index 8b46ab1c8..d957908ba 100644 --- a/src/AccessToken/Verify.php +++ b/src/AccessToken/Verify.php @@ -23,6 +23,7 @@ use Exception; use ExpiredException; use Firebase\JWT\ExpiredException as ExpiredExceptionV3; +use Firebase\JWT\JWT; use Firebase\JWT\Key; use Firebase\JWT\SignatureInvalidException; use Google\Auth\Cache\MemoryCacheItemPool; @@ -31,8 +32,9 @@ use GuzzleHttp\ClientInterface; use InvalidArgumentException; use LogicException; +use phpseclib3\Crypt\AES; use phpseclib3\Crypt\PublicKeyLoader; -use phpseclib3\Crypt\RSA\PublicKey; // Firebase v2 +use phpseclib3\Math\BigInteger; use Psr\Cache\CacheItemPoolInterface; /** @@ -219,93 +221,35 @@ private function getFederatedSignOnCerts() private function getJwtService() { - $jwtClass = 'JWT'; - if (class_exists('\Firebase\JWT\JWT')) { - $jwtClass = 'Firebase\JWT\JWT'; - } - - if (property_exists($jwtClass, 'leeway') && $jwtClass::$leeway < 1) { + $jwt = new JWT(); + if ($jwt::$leeway < 1) { // Ensures JWT leeway is at least 1 // @see https://github.com/google/google-api-php-client/issues/827 - $jwtClass::$leeway = 1; + $jwt::$leeway = 1; } - // @phpstan-ignore-next-line - return new $jwtClass(); + return $jwt; } private function getPublicKey($cert) { - $bigIntClass = $this->getBigIntClass(); - $modulus = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['n']), 256); - $exponent = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['e']), 256); + $modulus = new BigInteger($this->jwt->urlsafeB64Decode($cert['n']), 256); + $exponent = new BigInteger($this->jwt->urlsafeB64Decode($cert['e']), 256); $component = ['n' => $modulus, 'e' => $exponent]; - if (class_exists('phpseclib3\Crypt\RSA\PublicKey')) { - /** @var PublicKey $loader */ - $loader = PublicKeyLoader::load($component); - - return $loader->toString('PKCS8'); - } - - $rsaClass = $this->getRsaClass(); - $rsa = new $rsaClass(); - $rsa->loadKey($component); - - return $rsa->getPublicKey(); - } - - private function getRsaClass() - { - if (class_exists('phpseclib3\Crypt\RSA')) { - return 'phpseclib3\Crypt\RSA'; - } - - if (class_exists('phpseclib\Crypt\RSA')) { - return 'phpseclib\Crypt\RSA'; - } + $loader = PublicKeyLoader::load($component); - return 'Crypt_RSA'; - } - - private function getBigIntClass() - { - if (class_exists('phpseclib3\Math\BigInteger')) { - return 'phpseclib3\Math\BigInteger'; - } - - if (class_exists('phpseclib\Math\BigInteger')) { - return 'phpseclib\Math\BigInteger'; - } - - return 'Math_BigInteger'; - } - - private function getOpenSslConstant() - { - if (class_exists('phpseclib3\Crypt\AES')) { - return 'phpseclib3\Crypt\AES::ENGINE_OPENSSL'; - } - - if (class_exists('phpseclib\Crypt\RSA')) { - return 'phpseclib\Crypt\RSA::MODE_OPENSSL'; - } - - if (class_exists('Crypt_RSA')) { - return 'CRYPT_RSA_MODE_OPENSSL'; - } - - throw new Exception('Cannot find RSA class'); + return $loader->toString('PKCS8'); } /** - * phpseclib calls "phpinfo" by default, which requires special - * whitelisting in the AppEngine VM environment. This function - * sets constants to bypass the need for phpseclib to check phpinfo - * - * @see phpseclib/Math/BigInteger - * @see https://github.com/GoogleCloudPlatform/getting-started-php/issues/85 - */ + * phpseclib calls "phpinfo" by default, which requires special + * whitelisting in the AppEngine VM environment. This function + * sets constants to bypass the need for phpseclib to check phpinfo + * + * @see phpseclib/Math/BigInteger + * @see https://github.com/GoogleCloudPlatform/getting-started-php/issues/85 + */ private function setPhpsecConstants() { if (filter_var(getenv('GAE_VM'), FILTER_VALIDATE_BOOLEAN)) { @@ -313,7 +257,7 @@ private function setPhpsecConstants() define('MATH_BIGINTEGER_OPENSSL_ENABLED', true); } if (!defined('CRYPT_RSA_MODE')) { - define('CRYPT_RSA_MODE', constant($this->getOpenSslConstant())); + define('CRYPT_RSA_MODE', AES::ENGINE_OPENSSL); } } } diff --git a/src/AuthHandler/AuthHandlerFactory.php b/src/AuthHandler/AuthHandlerFactory.php index 63c266165..98a0ab166 100644 --- a/src/AuthHandler/AuthHandlerFactory.php +++ b/src/AuthHandler/AuthHandlerFactory.php @@ -25,7 +25,7 @@ class AuthHandlerFactory /** * Builds out a default http handler for the installed version of guzzle. * - * @return Guzzle5AuthHandler|Guzzle6AuthHandler|Guzzle7AuthHandler + * @return Guzzle6AuthHandler|Guzzle7AuthHandler * @throws Exception */ public static function build($cache = null, array $cacheConfig = []) @@ -38,8 +38,6 @@ public static function build($cache = null, array $cacheConfig = []) } switch ($guzzleVersion) { - case 5: - return new Guzzle5AuthHandler($cache, $cacheConfig); case 6: return new Guzzle6AuthHandler($cache, $cacheConfig); case 7: diff --git a/src/AuthHandler/Guzzle5AuthHandler.php b/src/AuthHandler/Guzzle5AuthHandler.php deleted file mode 100644 index f8a76f0aa..000000000 --- a/src/AuthHandler/Guzzle5AuthHandler.php +++ /dev/null @@ -1,108 +0,0 @@ -cache = $cache; - $this->cacheConfig = $cacheConfig; - } - - public function attachCredentials( - ClientInterface $http, - CredentialsLoader $credentials, - callable $tokenCallback = null - ) { - // use the provided cache - if ($this->cache) { - $credentials = new FetchAuthTokenCache( - $credentials, - $this->cacheConfig, - $this->cache - ); - } - - return $this->attachCredentialsCache($http, $credentials, $tokenCallback); - } - - public function attachCredentialsCache( - ClientInterface $http, - FetchAuthTokenCache $credentials, - callable $tokenCallback = null - ) { - // if we end up needing to make an HTTP request to retrieve credentials, we - // can use our existing one, but we need to throw exceptions so the error - // bubbles up. - $authHttp = $this->createAuthHttp($http); - $authHttpHandler = HttpHandlerFactory::build($authHttp); - $subscriber = new AuthTokenSubscriber( - $credentials, - $authHttpHandler, - $tokenCallback - ); - - $http->setDefaultOption('auth', 'google_auth'); - $http->getEmitter()->attach($subscriber); - - return $http; - } - - public function attachToken(ClientInterface $http, array $token, array $scopes) - { - $tokenFunc = function ($scopes) use ($token) { - return $token['access_token']; - }; - - $subscriber = new ScopedAccessTokenSubscriber( - $tokenFunc, - $scopes, - $this->cacheConfig, - $this->cache - ); - - $http->setDefaultOption('auth', 'scoped'); - $http->getEmitter()->attach($subscriber); - - return $http; - } - - public function attachKey(ClientInterface $http, $key) - { - $subscriber = new SimpleSubscriber(['key' => $key]); - - $http->setDefaultOption('auth', 'simple'); - $http->getEmitter()->attach($subscriber); - - return $http; - } - - private function createAuthHttp(ClientInterface $http) - { - return new Client([ - 'base_url' => $http->getBaseUrl(), - 'defaults' => [ - 'exceptions' => true, - 'verify' => $http->getDefaultOption('verify'), - 'proxy' => $http->getDefaultOption('proxy'), - ] - ]); - } -} diff --git a/src/Collection.php b/src/Collection.php index c164c12a2..fe2c62fec 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -81,6 +81,7 @@ public function offsetExists($offset) } /** @return mixed */ + #[\ReturnTypeWillChange] public function offsetGet($offset) { if (!is_numeric($offset)) { diff --git a/src/aliases.php b/src/aliases.php index 4419ba7e3..3224a030f 100644 --- a/src/aliases.php +++ b/src/aliases.php @@ -15,7 +15,6 @@ 'Google\\Utils\\UriTemplate' => 'Google_Utils_UriTemplate', 'Google\\AuthHandler\\Guzzle6AuthHandler' => 'Google_AuthHandler_Guzzle6AuthHandler', 'Google\\AuthHandler\\Guzzle7AuthHandler' => 'Google_AuthHandler_Guzzle7AuthHandler', - 'Google\\AuthHandler\\Guzzle5AuthHandler' => 'Google_AuthHandler_Guzzle5AuthHandler', 'Google\\AuthHandler\\AuthHandlerFactory' => 'Google_AuthHandler_AuthHandlerFactory', 'Google\\Http\\Batch' => 'Google_Http_Batch', 'Google\\Http\\MediaFileUpload' => 'Google_Http_MediaFileUpload', @@ -52,9 +51,6 @@ class Google_AccessToken_Verify extends \Google\AccessToken\Verify class Google_AuthHandler_AuthHandlerFactory extends \Google\AuthHandler\AuthHandlerFactory { } - class Google_AuthHandler_Guzzle5AuthHandler extends \Google\AuthHandler\Guzzle5AuthHandler - { - } class Google_AuthHandler_Guzzle6AuthHandler extends \Google\AuthHandler\Guzzle6AuthHandler { } diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 66df1bd4b..144c7d789 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -24,26 +24,16 @@ use League\Flysystem\Adapter\Local; use League\Flysystem\Filesystem; use Cache\Adapter\Filesystem\FilesystemCachePool; -use Yoast\PHPUnitPolyfills\TestCases\TestCase; - -if (trait_exists('\Prophecy\PhpUnit\ProphecyTrait')) { - trait BaseTestTrait - { - use \Prophecy\PhpUnit\ProphecyTrait; - } -} else { - trait BaseTestTrait - { - } -} +use PHPUnit\Framework\TestCase; +use Prophecy\PhpUnit\ProphecyTrait; class BaseTest extends TestCase { + use ProphecyTrait; + private $key; private $client; - use BaseTestTrait; - public function getClient() { if (!$this->client) { @@ -74,11 +64,6 @@ private function createClient() $options['verify'] = false; } - // adjust constructor depending on guzzle version - if ($this->isGuzzle5()) { - $options = ['defaults' => $options]; - } - $httpClient = new GuzzleClient($options); $client = new Client(); @@ -219,76 +204,4 @@ protected function loadExample($example) return false; } - - protected function isGuzzle7() - { - if (!defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) { - return false; - } - - return (7 === ClientInterface::MAJOR_VERSION); - } - - protected function isGuzzle6() - { - if (!defined('\GuzzleHttp\ClientInterface::VERSION')) { - return false; - } - $version = ClientInterface::VERSION; - - return ('6' === $version[0]); - } - - protected function isGuzzle5() - { - if (!defined('\GuzzleHttp\ClientInterface::VERSION')) { - return false; - } - - $version = ClientInterface::VERSION; - - return ('5' === $version[0]); - } - - public function onlyGuzzle6() - { - if (!$this->isGuzzle6()) { - $this->markTestSkipped('Guzzle 6 only'); - } - } - - public function onlyPhp55AndAbove() - { - if (version_compare(PHP_VERSION, '5.5', '<')) { - $this->markTestSkipped('PHP 5.5 and above only'); - } - } - - public function onlyGuzzle5() - { - if (!$this->isGuzzle5()) { - $this->markTestSkipped('Guzzle 5 only'); - } - } - - public function onlyGuzzle6Or7() - { - if (!$this->isGuzzle6() && !$this->isGuzzle7()) { - $this->markTestSkipped('Guzzle 6 or 7 only'); - } - } - - protected function getGuzzle5ResponseMock() - { - $response = $this->prophesize('GuzzleHttp\Message\ResponseInterface'); - $response->getStatusCode() - ->willReturn(200); - - $response->getHeaders()->willReturn([]); - $response->getBody()->willReturn(''); - $response->getProtocolVersion()->willReturn(''); - $response->getReasonPhrase()->willReturn(''); - - return $response; - } } diff --git a/tests/Google/AccessToken/RevokeTest.php b/tests/Google/AccessToken/RevokeTest.php index 1db83fa41..c6dd714f7 100644 --- a/tests/Google/AccessToken/RevokeTest.php +++ b/tests/Google/AccessToken/RevokeTest.php @@ -27,87 +27,8 @@ class RevokeTest extends BaseTest { - public function testRevokeAccessGuzzle5() + public function testRevokeAccess() { - $this->onlyGuzzle5(); - - $accessToken = 'ACCESS_TOKEN'; - $refreshToken = 'REFRESH_TOKEN'; - $token = ''; - - $response = $this->prophesize('GuzzleHttp\Message\ResponseInterface'); - $response->getStatusCode() - ->shouldBeCalledTimes(3) - ->willReturn(200); - - $response->getHeaders()->willReturn([]); - $response->getBody()->willReturn(''); - $response->getProtocolVersion()->willReturn(''); - $response->getReasonPhrase()->willReturn(''); - - $http = $this->prophesize('GuzzleHttp\ClientInterface'); - $http->send(Argument::type('GuzzleHttp\Message\RequestInterface')) - ->shouldBeCalledTimes(3) - ->will(function ($args) use (&$token, $response) { - $request = $args[0]; - parse_str((string) $request->getBody(), $fields); - $token = isset($fields['token']) ? $fields['token'] : null; - - return $response->reveal(); - }); - - $requestToken = null; - $request = $this->prophesize('GuzzleHttp\Message\RequestInterface'); - $request->getBody() - ->shouldBeCalledTimes(3) - ->will(function () use (&$requestToken) { - return 'token='.$requestToken; - }); - - $http->createRequest(Argument::any(), Argument::any(), Argument::any()) - ->shouldBeCalledTimes(3) - ->will(function ($args) use (&$requestToken, $request) { - $params = $args[2]; - parse_str((string) $params['body'], $fields); - $requestToken = isset($fields['token']) ? $fields['token'] : null; - - return $request; - }); - - $t = [ - 'access_token' => $accessToken, - 'created' => time(), - 'expires_in' => '3600' - ]; - - // Test with access token. - $revoke = new Revoke($http->reveal()); - $this->assertTrue($revoke->revokeToken($t)); - $this->assertEquals($accessToken, $token); - - // Test with refresh token. - $revoke = new Revoke($http->reveal()); - $t = [ - 'access_token' => $accessToken, - 'refresh_token' => $refreshToken, - 'created' => time(), - 'expires_in' => '3600' - ]; - - $this->assertTrue($revoke->revokeToken($t)); - $this->assertEquals($refreshToken, $token); - - // Test with token string. - $revoke = new Revoke($http->reveal()); - $t = $accessToken; - $this->assertTrue($revoke->revokeToken($t)); - $this->assertEquals($accessToken, $token); - } - - public function testRevokeAccessGuzzle6Or7() - { - $this->onlyGuzzle6Or7(); - $accessToken = 'ACCESS_TOKEN'; $refreshToken = 'REFRESH_TOKEN'; $token = ''; diff --git a/tests/Google/AccessToken/VerifyTest.php b/tests/Google/AccessToken/VerifyTest.php index 25a7224b2..7d37209e4 100644 --- a/tests/Google/AccessToken/VerifyTest.php +++ b/tests/Google/AccessToken/VerifyTest.php @@ -21,9 +21,11 @@ namespace Google\Tests\AccessToken; +use Firebase\JWT\JWT; use Google\AccessToken\Verify; use Google\Tests\BaseTest; use ReflectionMethod; +use phpseclib3\Crypt\AES; class VerifyTest extends BaseTest { @@ -51,7 +53,7 @@ public function testPhpsecConstants() $openSslEnable = constant('MATH_BIGINTEGER_OPENSSL_ENABLED'); $rsaMode = constant('CRYPT_RSA_MODE'); $this->assertTrue($openSslEnable); - $this->assertEquals(constant($this->getOpenSslConstant()), $rsaMode); + $this->assertEquals(AES::ENGINE_OPENSSL, $rsaMode); } /** @@ -63,7 +65,7 @@ public function testValidateIdToken() { $this->checkToken(); - $jwt = $this->getJwtService(); + $jwt = new JWT(); $client = $this->getClient(); $http = $client->getHttpClient(); $token = $client->getAccessToken(); @@ -100,7 +102,7 @@ public function testLeewayIsUnchangedWhenPassingInJwt() { $this->checkToken(); - $jwt = $this->getJwtService(); + $jwt = new JWT(); // set arbitrary leeway so we can check this later $jwt::$leeway = $leeway = 1.5; $client = $this->getClient(); @@ -133,28 +135,4 @@ public function testRetrieveCertsFromLocation() $this->assertArrayHasKey('alg', $certs['keys'][0]); $this->assertEquals('RS256', $certs['keys'][0]['alg']); } - - private function getJwtService() - { - if (class_exists('\Firebase\JWT\JWT')) { - return new \Firebase\JWT\JWT; - } - - return new \JWT; - } - - private function getOpenSslConstant() - { - if (class_exists('phpseclib3\Crypt\AES')) { - return 'phpseclib3\Crypt\AES::ENGINE_OPENSSL'; - } - - if (class_exists('phpseclib\Crypt\RSA')) { - return 'phpseclib\Crypt\RSA::MODE_OPENSSL'; - } - - if (class_exists('Crypt_RSA')) { - return 'CRYPT_RSA_MODE_OPENSSL'; - } - } } diff --git a/tests/Google/CacheTest.php b/tests/Google/CacheTest.php index e6743a45c..153b2669c 100644 --- a/tests/Google/CacheTest.php +++ b/tests/Google/CacheTest.php @@ -50,7 +50,6 @@ public function testInMemoryCache() public function testFileCache() { - $this->onlyPhp55AndAbove(); $this->checkServiceAccountCredentials(); $client = new Client(); diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 04da74ef9..2a9f4a0e2 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -58,50 +58,31 @@ public function testSignAppKey() private function checkAuthHandler($http, $className) { - if ($this->isGuzzle6() || $this->isGuzzle7()) { - $stack = $http->getConfig('handler'); - $class = new ReflectionClass(get_class($stack)); - $property = $class->getProperty('stack'); - $property->setAccessible(true); - $middlewares = $property->getValue($stack); - $middleware = array_pop($middlewares); - - if (null === $className) { - // only the default middlewares have been added - $this->assertCount(3, $middlewares); - } else { - $authClass = sprintf('Google\Auth\Middleware\%sMiddleware', $className); - $this->assertInstanceOf($authClass, $middleware[0]); - } + $stack = $http->getConfig('handler'); + $class = new ReflectionClass(get_class($stack)); + $property = $class->getProperty('stack'); + $property->setAccessible(true); + $middlewares = $property->getValue($stack); + $middleware = array_pop($middlewares); + + if (null === $className) { + // only the default middlewares have been added + $this->assertCount(3, $middlewares); } else { - $listeners = $http->getEmitter()->listeners('before'); - - if (null === $className) { - $this->assertCount(0, $listeners); - } else { - $authClass = sprintf('Google\Auth\Subscriber\%sSubscriber', $className); - $this->assertCount(1, $listeners); - $this->assertCount(2, $listeners[0]); - $this->assertInstanceOf($authClass, $listeners[0][0]); - } + $authClass = sprintf('Google\Auth\Middleware\%sMiddleware', $className); + $this->assertInstanceOf($authClass, $middleware[0]); } } private function checkCredentials($http, $fetcherClass, $sub = null) { - if ($this->isGuzzle6() || $this->isGuzzle7()) { - $stack = $http->getConfig('handler'); - $class = new ReflectionClass(get_class($stack)); - $property = $class->getProperty('stack'); - $property->setAccessible(true); - $middlewares = $property->getValue($stack); // Works - $middleware = array_pop($middlewares); - $auth = $middleware[0]; - } else { - // access the protected $fetcher property - $listeners = $http->getEmitter()->listeners('before'); - $auth = $listeners[0][0]; - } + $stack = $http->getConfig('handler'); + $class = new ReflectionClass(get_class($stack)); + $property = $class->getProperty('stack'); + $property->setAccessible(true); + $middlewares = $property->getValue($stack); // Works + $middleware = array_pop($middlewares); + $auth = $middleware[0]; $class = new ReflectionClass(get_class($auth)); $property = $class->getProperty('fetcher'); @@ -208,8 +189,6 @@ public function testNoAuthIsNull() public function testPrepareService() { - $this->onlyGuzzle6Or7(); - $client = new Client(); $client->setScopes(["scope1", "scope2"]); $scopes = $client->prepareScopes(); @@ -327,55 +306,9 @@ public function testSetAccessTokenValidation() public function testDefaultConfigOptions() { $client = new Client(); - if ($this->isGuzzle6() || $this->isGuzzle7()) { - $this->assertArrayHasKey('http_errors', $client->getHttpClient()->getConfig()); - $this->assertArrayNotHasKey('exceptions', $client->getHttpClient()->getConfig()); - $this->assertFalse($client->getHttpClient()->getConfig()['http_errors']); - } - if ($this->isGuzzle5()) { - $this->assertArrayHasKey('exceptions', $client->getHttpClient()->getDefaultOption()); - $this->assertArrayNotHasKey('http_errors', $client->getHttpClient()->getDefaultOption()); - $this->assertFalse($client->getHttpClient()->getDefaultOption()['exceptions']); - } - } - - public function testAppEngineStreamHandlerConfig() - { - $this->onlyGuzzle5(); - - $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine'; - $client = new Client(); - - // check Stream Handler is used - $http = $client->getHttpClient(); - $class = new ReflectionClass(get_class($http)); - $property = $class->getProperty('fsm'); - $property->setAccessible(true); - $fsm = $property->getValue($http); - - $class = new ReflectionClass(get_class($fsm)); - $property = $class->getProperty('handler'); - $property->setAccessible(true); - $handler = $property->getValue($fsm); - - $this->assertInstanceOf('GuzzleHttp\Ring\Client\StreamHandler', $handler); - - unset($_SERVER['SERVER_SOFTWARE']); - } - - public function testAppEngineVerifyConfig() - { - $this->onlyGuzzle5(); - - $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine'; - $client = new Client(); - - $this->assertEquals( - '/etc/ca-certificates.crt', - $client->getHttpClient()->getDefaultOption('verify') - ); - - unset($_SERVER['SERVER_SOFTWARE']); + $this->assertArrayHasKey('http_errors', $client->getHttpClient()->getConfig()); + $this->assertArrayNotHasKey('exceptions', $client->getHttpClient()->getConfig()); + $this->assertFalse($client->getHttpClient()->getConfig()['http_errors']); } public function testJsonConfig() @@ -486,14 +419,7 @@ public function testRefreshTokenSetsValues() ->shouldBeCalledTimes(1) ->willReturn($token); - if ($this->isGuzzle5()) { - $response = $this->getGuzzle5ResponseMock(); - $response->getStatusCode() - ->shouldBeCalledTimes(1) - ->willReturn(200); - } else { - $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); - } + $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); $response->getBody() ->shouldBeCalledTimes(1) @@ -503,20 +429,9 @@ public function testRefreshTokenSetsValues() $http = $this->prophesize('GuzzleHttp\ClientInterface'); - if ($this->isGuzzle5()) { - $guzzle5Request = new \GuzzleHttp\Message\Request('POST', '/', ['body' => $token]); - $http->createRequest(Argument::any(), Argument::any(), Argument::any()) - ->shouldBeCalledTimes(1) - ->willReturn($guzzle5Request); - - $http->send(Argument::type('GuzzleHttp\Message\Request')) - ->shouldBeCalledTimes(1) - ->willReturn($response->reveal()); - } else { - $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) - ->shouldBeCalledTimes(1) - ->willReturn($response->reveal()); - } + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response->reveal()); $client = $this->getClient(); $client->setHttpClient($http->reveal()); @@ -540,14 +455,7 @@ public function testRefreshTokenIsSetOnRefresh() ->shouldBeCalledTimes(1) ->willReturn($token); - if ($this->isGuzzle5()) { - $response = $this->getGuzzle5ResponseMock(); - $response->getStatusCode() - ->shouldBeCalledTimes(1) - ->willReturn(200); - } else { - $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); - } + $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); $response->getBody() ->shouldBeCalledTimes(1) @@ -557,19 +465,9 @@ public function testRefreshTokenIsSetOnRefresh() $http = $this->prophesize('GuzzleHttp\ClientInterface'); - if ($this->isGuzzle5()) { - $guzzle5Request = new \GuzzleHttp\Message\Request('POST', '/', ['body' => $token]); - $http->createRequest(Argument::any(), Argument::any(), Argument::any()) - ->willReturn($guzzle5Request); - - $http->send(Argument::type('GuzzleHttp\Message\Request')) - ->shouldBeCalledTimes(1) - ->willReturn($response->reveal()); - } else { - $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) - ->shouldBeCalledTimes(1) - ->willReturn($response->reveal()); - } + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response->reveal()); $client = $this->getClient(); $client->setHttpClient($http->reveal()); @@ -594,13 +492,7 @@ public function testRefreshTokenIsNotSetWhenNewRefreshTokenIsReturned() $postBody->__toString() ->wilLReturn($token); - if ($this->isGuzzle5()) { - $response = $this->getGuzzle5ResponseMock(); - $response->getStatusCode() - ->willReturn(200); - } else { - $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); - } + $response = $this->prophesize('Psr\Http\Message\ResponseInterface'); $response->getBody() ->willReturn($postBody->reveal()); @@ -609,18 +501,9 @@ public function testRefreshTokenIsNotSetWhenNewRefreshTokenIsReturned() $http = $this->prophesize('GuzzleHttp\ClientInterface'); - if ($this->isGuzzle5()) { - $guzzle5Request = new \GuzzleHttp\Message\Request('POST', '/', ['body' => $token]); - $http->createRequest(Argument::any(), Argument::any(), Argument::any()) - ->willReturn($guzzle5Request); - - $http->send(Argument::type('GuzzleHttp\Message\Request')) - ->willReturn($response->reveal()); - } else { - $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) - ->shouldBeCalledTimes(1) - ->willReturn($response->reveal()); - } + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response->reveal()); $client = $this->getClient(); $client->setHttpClient($http->reveal()); @@ -707,7 +590,6 @@ public function testBadSubjectThrowsException() public function testTokenCallback() { - $this->onlyPhp55AndAbove(); $this->checkToken(); $client = $this->getClient(); @@ -758,7 +640,6 @@ public function testTokenCallback() public function testDefaultTokenCallback() { - $this->onlyPhp55AndAbove(); $this->checkToken(); $client = $this->getClient(); @@ -857,8 +738,6 @@ public function testCacheClientOption() public function testExecuteWithFormat() { - $this->onlyGuzzle6Or7(); - $client = new Client([ 'api_format_v2' => true ]); @@ -882,8 +761,6 @@ public function testExecuteWithFormat() public function testExecuteSetsCorrectHeaders() { - $this->onlyGuzzle6Or7(); - $client = new Client(); $guzzle = $this->prophesize('GuzzleHttp\Client'); @@ -968,8 +845,6 @@ public function testClientOptions() public function testCredentialsOptionWithCredentialsLoader() { - $this->onlyGuzzle6Or7(); - $request = null; $credentials = $this->prophesize('Google\Auth\CredentialsLoader'); $credentials->getCacheKey() diff --git a/tests/Google/Http/RESTTest.php b/tests/Google/Http/RESTTest.php index 2c73dceb6..ef44ed0a2 100644 --- a/tests/Google/Http/RESTTest.php +++ b/tests/Google/Http/RESTTest.php @@ -32,7 +32,7 @@ class RESTTest extends BaseTest */ private $rest; - public function set_up() + public function setUp(): void { $this->rest = new REST(); $this->request = new Request('GET', '/'); diff --git a/tests/Google/Service/AdSenseTest.php b/tests/Google/Service/AdSenseTest.php index 453908ced..234075aea 100644 --- a/tests/Google/Service/AdSenseTest.php +++ b/tests/Google/Service/AdSenseTest.php @@ -23,7 +23,7 @@ class AdSenseTest extends BaseTest { public $adsense; - public function set_up() + public function setUp(): void { $this->markTestSkipped('Thesse tests need to be fixed'); $this->checkToken(); diff --git a/tests/Google/Service/ResourceTest.php b/tests/Google/Service/ResourceTest.php index c2c814d4e..17000880a 100644 --- a/tests/Google/Service/ResourceTest.php +++ b/tests/Google/Service/ResourceTest.php @@ -26,13 +26,11 @@ use Google\Service\Exception as ServiceException; use Google\Service\Resource as GoogleResource; use Google\Exception as GoogleException; -use GuzzleHttp\Message\Response as Guzzle5Response; use GuzzleHttp\Psr7; use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Stream; -use GuzzleHttp\Stream\Stream as Guzzle5Stream; use Prophecy\Argument; class TestService extends \Google\Service @@ -52,7 +50,7 @@ class ResourceTest extends BaseTest private $client; private $service; - public function set_up() + public function setUp(): void { $this->client = $this->prophesize(Client::class); @@ -231,24 +229,12 @@ public function testNoExpectedClassForAltMediaWithHttpSuccess() $http = $this->prophesize("GuzzleHttp\Client"); - if ($this->isGuzzle5()) { - $body = Guzzle5Stream::factory('thisisnotvalidjson'); - $response = new Guzzle5Response(200, [], $body); + $body = Psr7\Utils::streamFor('thisisnotvalidjson'); + $response = new Response(200, [], $body); - $http->createRequest(Argument::any(), Argument::any(), Argument::any()) - ->willReturn(new \GuzzleHttp\Message\Request('GET', '/?alt=media')); - - $http->send(Argument::type('GuzzleHttp\Message\Request')) - ->shouldBeCalledTimes(1) - ->willReturn($response); - } else { - $body = Psr7\Utils::streamFor('thisisnotvalidjson'); - $response = new Response(200, [], $body); - - $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) - ->shouldBeCalledTimes(1) - ->willReturn($response); - } + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response); $client = new Client(); $client->setHttpClient($http->reveal()); @@ -284,24 +270,12 @@ public function testNoExpectedClassForAltMediaWithHttpFail() $http = $this->prophesize("GuzzleHttp\Client"); - if ($this->isGuzzle5()) { - $body = Guzzle5Stream::factory('thisisnotvalidjson'); - $response = new Guzzle5Response(400, [], $body); - - $http->createRequest(Argument::any(), Argument::any(), Argument::any()) - ->willReturn(new \GuzzleHttp\Message\Request('GET', '/?alt=media')); + $body = Psr7\Utils::streamFor('thisisnotvalidjson'); + $response = new Response(400, [], $body); - $http->send(Argument::type('GuzzleHttp\Message\Request')) - ->shouldBeCalledTimes(1) - ->willReturn($response); - } else { - $body = Psr7\Utils::streamFor('thisisnotvalidjson'); - $response = new Response(400, [], $body); - - $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) - ->shouldBeCalledTimes(1) - ->willReturn($response); - } + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response); $client = new Client(); $client->setHttpClient($http->reveal()); @@ -341,24 +315,12 @@ public function testErrorResponseWithVeryLongBody() $http = $this->prophesize("GuzzleHttp\Client"); - if ($this->isGuzzle5()) { - $body = Guzzle5Stream::factory('this will be pulled into memory'); - $response = new Guzzle5Response(400, [], $body); - - $http->createRequest(Argument::any(), Argument::any(), Argument::any()) - ->willReturn(new \GuzzleHttp\Message\Request('GET', '/?alt=media')); + $body = Psr7\Utils::streamFor('this will be pulled into memory'); + $response = new Response(400, [], $body); - $http->send(Argument::type('GuzzleHttp\Message\Request')) - ->shouldBeCalledTimes(1) - ->willReturn($response); - } else { - $body = Psr7\Utils::streamFor('this will be pulled into memory'); - $response = new Response(400, [], $body); - - $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) - ->shouldBeCalledTimes(1) - ->willReturn($response); - } + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response); $client = new Client(); $client->setHttpClient($http->reveal()); @@ -392,8 +354,6 @@ public function testErrorResponseWithVeryLongBody() public function testSuccessResponseWithVeryLongBody() { - $this->onlyGuzzle6Or7(); - // set the "alt" parameter to "media" $arguments = [['alt' => 'media']]; $stream = $this->prophesize(Stream::class); @@ -446,24 +406,12 @@ public function testExceptionMessage() $http = $this->prophesize("GuzzleHttp\Client"); - if ($this->isGuzzle5()) { - $body = Guzzle5Stream::factory($content); - $response = new Guzzle5Response(400, [], $body); + $body = Psr7\Utils::streamFor($content); + $response = new Response(400, [], $body); - $http->createRequest(Argument::any(), Argument::any(), Argument::any()) - ->willReturn(new \GuzzleHttp\Message\Request('GET', '/?alt=media')); - - $http->send(Argument::type('GuzzleHttp\Message\Request')) - ->shouldBeCalledTimes(1) - ->willReturn($response); - } else { - $body = Psr7\Utils::streamFor($content); - $response = new Response(400, [], $body); - - $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) - ->shouldBeCalledTimes(1) - ->willReturn($response); - } + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes(1) + ->willReturn($response); $client = new Client(); $client->setHttpClient($http->reveal()); diff --git a/tests/Google/Service/TasksTest.php b/tests/Google/Service/TasksTest.php index 341b0e7ff..7e035fd57 100644 --- a/tests/Google/Service/TasksTest.php +++ b/tests/Google/Service/TasksTest.php @@ -25,7 +25,7 @@ class TasksTest extends BaseTest /** @var Tasks */ public $taskService; - public function set_up() + public function setUp(): void { $this->checkToken(); $this->taskService = new Tasks($this->getClient()); diff --git a/tests/Google/Service/YouTubeTest.php b/tests/Google/Service/YouTubeTest.php index c6e4032ee..2b08035f1 100644 --- a/tests/Google/Service/YouTubeTest.php +++ b/tests/Google/Service/YouTubeTest.php @@ -24,7 +24,7 @@ class YouTubeTest extends BaseTest { /** @var YouTube */ public $youtube; - public function set_up() + public function setUp(): void { $this->checkToken(); $this->youtube = new YouTube($this->getClient()); diff --git a/tests/Google/ServiceTest.php b/tests/Google/ServiceTest.php index a6f40539e..82abeb423 100644 --- a/tests/Google/ServiceTest.php +++ b/tests/Google/ServiceTest.php @@ -24,11 +24,12 @@ use Google\Model; use Google\Service; use Google\Http\Batch; -use Yoast\PHPUnitPolyfills\TestCases\TestCase; +use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; +use Prophecy\PhpUnit\ProphecyTrait; class TestModel extends Model { @@ -48,22 +49,11 @@ class TestService extends Service public $batchPath = 'batch/test'; } -if (trait_exists('\Prophecy\PhpUnit\ProphecyTrait')) { - trait ServiceTestTrait - { - use \Prophecy\PhpUnit\ProphecyTrait; - } -} else { - trait ServiceTestTrait - { - } -} - class ServiceTest extends TestCase { private static $errorMessage; - use ServiceTestTrait; + use ProphecyTrait; public function testCreateBatch() { diff --git a/tests/Google/Task/RunnerTest.php b/tests/Google/Task/RunnerTest.php index ce868158d..650ef5c75 100644 --- a/tests/Google/Task/RunnerTest.php +++ b/tests/Google/Task/RunnerTest.php @@ -24,11 +24,9 @@ use Google\Http\REST; use Google\Service\Exception as ServiceException; use Google\Task\Exception as TaskException; -use GuzzleHttp\Message\Response as Guzzle5Response; use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; -use GuzzleHttp\Stream\Stream as Guzzle5Stream; use Prophecy\Argument; use Exception; @@ -42,7 +40,7 @@ class RunnerTest extends BaseTest private $retryMap; private $retryConfig; - protected function set_up() + public function setUp(): void { $this->client = new Client(); } @@ -648,19 +646,9 @@ private function makeRequest() $request = new Request('GET', '/test'); $http = $this->prophesize('GuzzleHttp\ClientInterface'); - if ($this->isGuzzle5()) { - $http->createRequest(Argument::any(), Argument::any(), Argument::any()) - ->shouldBeCalledTimes($this->mockedCallsCount) - ->willReturn(new \GuzzleHttp\Message\Request('GET', '/test')); - - $http->send(Argument::type('GuzzleHttp\Message\Request')) - ->shouldBeCalledTimes($this->mockedCallsCount) - ->will([$this, 'getNextMockedCall']); - } else { - $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) - ->shouldBeCalledTimes($this->mockedCallsCount) - ->will([$this, 'getNextMockedCall']); - } + $http->send(Argument::type('Psr\Http\Message\RequestInterface'), []) + ->shouldBeCalledTimes($this->mockedCallsCount) + ->will([$this, 'getNextMockedCall']); return REST::execute($http->reveal(), $request, '', $this->retryConfig, $this->retryMap); } @@ -680,13 +668,8 @@ public function getNextMockedCall($request) throw $current; } - if ($this->isGuzzle5()) { - $stream = Guzzle5Stream::factory($current['body']); - $response = new Guzzle5Response($current['code'], $current['headers'], $stream); - } else { - $stream = Psr7\Utils::streamFor($current['body']); - $response = new Response($current['code'], $current['headers'], $stream); - } + $stream = Psr7\Utils::streamFor($current['body']); + $response = new Response($current['code'], $current['headers'], $stream); return $response; } From bded223ece445a6130cde82417b20180b1d6698a Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 18 May 2023 05:59:44 -0600 Subject: [PATCH 260/301] feat: add pkce support and upgrade examples (#2438) --- composer.json | 2 +- examples/idtoken.php | 3 ++- examples/large-file-download.php | 3 ++- examples/large-file-upload.php | 3 ++- examples/multi-api.php | 3 ++- examples/simple-file-upload.php | 3 ++- src/Client.php | 6 +++++- 7 files changed, 16 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 70ca62945..e557bb465 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "license": "Apache-2.0", "require": { "php": "^7.4|^8.0", - "google/auth": "^1.26", + "google/auth": "^1.28", "google/apiclient-services": "~0.200", "firebase/php-jwt": "~6.0", "monolog/monolog": "^2.9||^3.0", diff --git a/examples/idtoken.php b/examples/idtoken.php index f592d7ae5..1c628d958 100644 --- a/examples/idtoken.php +++ b/examples/idtoken.php @@ -57,7 +57,7 @@ * bundle in the session, and redirect to ourself. ************************************************/ if (isset($_GET['code'])) { - $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); + $token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']); // store in the session also $_SESSION['id_token_token'] = $token; @@ -77,6 +77,7 @@ ) { $client->setAccessToken($_SESSION['id_token_token']); } else { + $_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier(); $authUrl = $client->createAuthUrl(); } diff --git a/examples/large-file-download.php b/examples/large-file-download.php index 72bf7ff6f..a3c99d0e2 100644 --- a/examples/large-file-download.php +++ b/examples/large-file-download.php @@ -48,7 +48,7 @@ * bundle in the session, and redirect to ourself. ************************************************/ if (isset($_GET['code'])) { - $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); + $token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']); $client->setAccessToken($token); // store in the session also @@ -65,6 +65,7 @@ unset($_SESSION['upload_token']); } } else { + $_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier(); $authUrl = $client->createAuthUrl(); } diff --git a/examples/large-file-upload.php b/examples/large-file-upload.php index a45743f62..17abdad72 100644 --- a/examples/large-file-upload.php +++ b/examples/large-file-upload.php @@ -53,7 +53,7 @@ * bundle in the session, and redirect to ourself. ************************************************/ if (isset($_GET['code'])) { - $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); + $token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']); $client->setAccessToken($token); // store in the session also @@ -70,6 +70,7 @@ unset($_SESSION['upload_token']); } } else { + $_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier(); $authUrl = $client->createAuthUrl(); } diff --git a/examples/multi-api.php b/examples/multi-api.php index 78aa0ecf4..e247e4139 100644 --- a/examples/multi-api.php +++ b/examples/multi-api.php @@ -54,7 +54,7 @@ * bundle in the session, and redirect to ourself. ************************************************/ if (isset($_GET['code'])) { - $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); + $token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']); $client->setAccessToken($token); // store in the session also @@ -71,6 +71,7 @@ unset($_SESSION['multi-api-token']); } } else { + $_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier(); $authUrl = $client->createAuthUrl(); } diff --git a/examples/simple-file-upload.php b/examples/simple-file-upload.php index 20bcdf9a8..b85a7a96f 100644 --- a/examples/simple-file-upload.php +++ b/examples/simple-file-upload.php @@ -53,7 +53,7 @@ * bundle in the session, and redirect to ourself. ************************************************/ if (isset($_GET['code'])) { - $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); + $token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']); $client->setAccessToken($token); // store in the session also @@ -70,6 +70,7 @@ unset($_SESSION['upload_token']); } } else { + $_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier(); $authUrl = $client->createAuthUrl(); } diff --git a/src/Client.php b/src/Client.php index 383726160..31b3f1d5f 100644 --- a/src/Client.php +++ b/src/Client.php @@ -240,9 +240,10 @@ public function authenticate($code) * Helper wrapped around the OAuth 2.0 implementation. * * @param string $code code from accounts.google.com + * @param string $codeVerifier the code verifier used for PKCE (if applicable) * @return array access token */ - public function fetchAccessTokenWithAuthCode($code) + public function fetchAccessTokenWithAuthCode($code, $codeVerifier = null) { if (strlen($code) == 0) { throw new InvalidArgumentException("Invalid code"); @@ -251,6 +252,9 @@ public function fetchAccessTokenWithAuthCode($code) $auth = $this->getOAuth2Service(); $auth->setCode($code); $auth->setRedirectUri($this->getRedirectUri()); + if ($codeVerifier) { + $auth->setCodeVerifier($codeVerifier); + } $httpHandler = HttpHandlerFactory::build($this->getHttpClient()); $creds = $auth->fetchAuthToken($httpHandler); From 49787fa30b8d8313146a61efbf77ed1fede723c2 Mon Sep 17 00:00:00 2001 From: "release-please[bot]" <55107282+release-please[bot]@users.noreply.github.com> Date: Thu, 18 May 2023 06:51:33 -0700 Subject: [PATCH 261/301] chore(main): release 2.15.0 (#2442) --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b24515ba3..79305bb5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## [2.15.0](https://github.com/googleapis/google-api-php-client/compare/v2.14.0...v2.15.0) (2023-05-18) + + +### Features + +* Add pkce support and upgrade examples ([#2438](https://github.com/googleapis/google-api-php-client/issues/2438)) ([bded223](https://github.com/googleapis/google-api-php-client/commit/bded223ece445a6130cde82417b20180b1d6698a)) +* Drop support for 7.3 and below ([#2431](https://github.com/googleapis/google-api-php-client/issues/2431)) ([c765b37](https://github.com/googleapis/google-api-php-client/commit/c765b379e95ab272b6a87aa802d9f5507eaeb2e7)) + ## [2.14.0](https://github.com/googleapis/google-api-php-client/compare/v2.13.2...v2.14.0) (2023-05-11) From c73bdc5734425455f748c134b53598a34e9a4735 Mon Sep 17 00:00:00 2001 From: Diptanshu Mittal <43611881+diptanshumittal@users.noreply.github.com> Date: Thu, 20 Jul 2023 17:14:13 +0000 Subject: [PATCH 262/301] chore(main): Enable release trigger (#2478) --- .github/release-trigger.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .github/release-trigger.yml diff --git a/.github/release-trigger.yml b/.github/release-trigger.yml new file mode 100644 index 000000000..ed17d0705 --- /dev/null +++ b/.github/release-trigger.yml @@ -0,0 +1,2 @@ +enabled: true +multiScmName: google-api-php-client From 21cb9449d6cc9cd3398a0c93230377fe0c01afda Mon Sep 17 00:00:00 2001 From: Abhishek jain <15982920+abhij89@users.noreply.github.com> Date: Tue, 15 Aug 2023 23:19:47 +0530 Subject: [PATCH 263/301] docs: update readme.md to use latest version of the package (#2486) --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1beb6c88d..73bc73de5 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ composer installed. Once composer is installed, execute the following command in your project root to install this library: ```sh -composer require google/apiclient:^2.12.1 +composer require google/apiclient:^2.15.0 ``` Finally, be sure to include the autoloader: @@ -65,7 +65,7 @@ you want to keep in `composer.json`: ```json { "require": { - "google/apiclient": "^2.12.1" + "google/apiclient": "^2.15.0" }, "scripts": { "pre-autoload-dump": "Google\\Task\\Composer::cleanup" From e48449e1c06e46e46267e09dd12d6b921c6a1bf0 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 8 Sep 2023 09:11:54 -0700 Subject: [PATCH 264/301] Update README.md --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 73bc73de5..b82f44f14 100644 --- a/README.md +++ b/README.md @@ -245,9 +245,10 @@ The classes used to call the API in [google-api-php-client-services](https://git A JSON request to the [Datastore API](https://developers.google.com/apis-explorer/#p/datastore/v1beta3/datastore.projects.runQuery) would look like this: -```json +``` POST https://datastore.googleapis.com/v1beta3/projects/YOUR_PROJECT_ID:runQuery?key=YOUR_API_KEY - +``` +```json { "query": { "kind": [{ From 8e7fae2b79cfc1b72026347abf6314d91442a018 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 12 Sep 2023 09:01:06 -0700 Subject: [PATCH 265/301] fix: upgrade min phpseclib version (#2499) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index e557bb465..d483f34ea 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "google/apiclient-services": "~0.200", "firebase/php-jwt": "~6.0", "monolog/monolog": "^2.9||^3.0", - "phpseclib/phpseclib": "^3.0.2", + "phpseclib/phpseclib": "^3.0.19", "guzzlehttp/guzzle": "~6.5||~7.0", "guzzlehttp/psr7": "^1.8.4||^2.2.1" }, From 7a95ed29e4b6c6859d2d22300c5455a92e2622ad Mon Sep 17 00:00:00 2001 From: "release-please[bot]" <55107282+release-please[bot]@users.noreply.github.com> Date: Wed, 13 Sep 2023 14:46:39 -0700 Subject: [PATCH 266/301] chore(main): release 2.15.1 (#2500) --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79305bb5a..b6d29393c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [2.15.1](https://github.com/googleapis/google-api-php-client/compare/v2.15.0...v2.15.1) (2023-09-12) + + +### Bug Fixes + +* Upgrade min phpseclib version ([#2499](https://github.com/googleapis/google-api-php-client/issues/2499)) ([8e7fae2](https://github.com/googleapis/google-api-php-client/commit/8e7fae2b79cfc1b72026347abf6314d91442a018)) + ## [2.15.0](https://github.com/googleapis/google-api-php-client/compare/v2.14.0...v2.15.0) (2023-05-18) From a206b70348a32a14b5c1e914238c44f9db9c057a Mon Sep 17 00:00:00 2001 From: Saransh Dhingra Date: Sat, 16 Dec 2023 01:09:06 +0530 Subject: [PATCH 267/301] docs: Update readme to add timeout fix for composer (#2504) --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index b82f44f14..34960da0d 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,15 @@ Once composer is installed, execute the following command in your project root t composer require google/apiclient:^2.15.0 ``` +If you're facing a timeout error then either increase the timeout for composer by adding the env flag as `COMPOSER_PROCESS_TIMEOUT=600 composer install` or you can put this in the `config` section of the composer schema: +``` +{ + "config": { + "process-timeout": 600 + } +} +``` + Finally, be sure to include the autoloader: ```php From a6408fe1da10d0262e3116499e7ab2ac191b7315 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 15 Dec 2023 16:20:56 -0600 Subject: [PATCH 268/301] chore: fix tests for google/auth v1.33 (#2531) --- composer.json | 4 ++-- phpcs.xml.dist | 6 +----- tests/Google/ClientTest.php | 7 +++++-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index d483f34ea..e1f9498a0 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "license": "Apache-2.0", "require": { "php": "^7.4|^8.0", - "google/auth": "^1.28", + "google/auth": "^1.33", "google/apiclient-services": "~0.200", "firebase/php-jwt": "~6.0", "monolog/monolog": "^2.9||^3.0", @@ -16,7 +16,7 @@ "guzzlehttp/psr7": "^1.8.4||^2.2.1" }, "require-dev": { - "squizlabs/php_codesniffer": "^3.0", + "squizlabs/php_codesniffer": "^3.8", "symfony/dom-crawler": "~2.1", "symfony/css-selector": "~2.1", "cache/filesystem-adapter": "^1.1", diff --git a/phpcs.xml.dist b/phpcs.xml.dist index c508de9c9..5bd578a07 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -148,11 +148,7 @@ There MUST be one space between the closing parenthesis and the opening brace The structure body MUST be indented once The closing brace MUST be on the next line after the body --> - - - - - + src/aliases\.php diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 2a9f4a0e2..94ce8b876 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -851,9 +851,12 @@ public function testCredentialsOptionWithCredentialsLoader() ->willReturn('cache-key'); // Ensure the access token provided by our credentials loader is used - $credentials->fetchAuthToken(Argument::any()) + $credentials->updateMetadata([], null, Argument::any()) ->shouldBeCalledOnce() - ->willReturn(['access_token' => 'abc']); + ->willReturn(['authorization' => 'Bearer abc']); + $credentials->getLastReceivedToken() + ->shouldBeCalledTimes(2) + ->willReturn(null); $client = new Client(['credentials' => $credentials->reveal()]); From 8c6602119b631e1a9da4dbe219af18d51c8dab8e Mon Sep 17 00:00:00 2001 From: Seyed AmirHossein Adhami Mirhosseini Date: Fri, 15 Dec 2023 14:22:44 -0800 Subject: [PATCH 269/301] fix: php 8.3 deprecated get_class method call without argument (#2509) --- src/Http/REST.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/REST.php b/src/Http/REST.php index 1519f60da..70e48e4b8 100644 --- a/src/Http/REST.php +++ b/src/Http/REST.php @@ -55,7 +55,7 @@ public static function execute( $runner = new Runner( $config, sprintf('%s %s', $request->getMethod(), (string) $request->getUri()), - [get_class(), 'doExecute'], + [self::class, 'doExecute'], [$client, $request, $expectedClass] ); From e7147523f0d14e00c8a9e3809e53523d5629fa87 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 20 Dec 2023 13:52:19 -0600 Subject: [PATCH 270/301] chore(ci): add php 8.3, upgrade EOL PHP versions to 8.1 (#2534) --- .github/sync-repo-settings.yaml | 3 ++- .github/workflows/tests.yml | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/sync-repo-settings.yaml b/.github/sync-repo-settings.yaml index 1fa17b12f..7a538ecaf 100644 --- a/.github/sync-repo-settings.yaml +++ b/.github/sync-repo-settings.yaml @@ -10,7 +10,8 @@ branchProtectionRules: - 'PHP 8.0 Unit Test' - 'PHP 8.1 Unit Test' - 'PHP 8.2 Unit Test' - - 'PHP 8.2 --prefer-lowest Unit Test' + - 'PHP 8.3 Unit Test' + - 'PHP 8.3 --prefer-lowest Unit Test' - 'PHP Style Check' - 'cla/google' requiredApprovingReviewCount: 1 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index df9f1565a..614045f13 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -11,12 +11,12 @@ jobs: strategy: fail-fast: false matrix: - php: [ "7.4", "8.0", "8.1", "8.2" ] + php: [ "7.4", "8.0", "8.1", "8.2", "8.3" ] composer-flags: [""] include: - php: "7.4" composer-flags: "--prefer-lowest " - - php: "8.2" + - php: "8.3" composer-flags: "--prefer-lowest " name: PHP ${{ matrix.php }} ${{ matrix.composer-flags }}Unit Test steps: @@ -42,7 +42,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: "7.4" + php-version: "8.1" - name: Install Dependencies uses: nick-invision/retry@v2 with: @@ -59,7 +59,7 @@ jobs: - name: Install PHP uses: shivammathur/setup-php@v2 with: - php-version: '8.0' + php-version: '8.1' - name: Run Script run: | composer install From d1830ede17114a4951ab9e60b3b9bcd9393b8668 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 26 Dec 2023 10:01:17 -0700 Subject: [PATCH 271/301] fix: disallow vulnerable guzzle versions (#2536) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index e1f9498a0..2a930f398 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "firebase/php-jwt": "~6.0", "monolog/monolog": "^2.9||^3.0", "phpseclib/phpseclib": "^3.0.19", - "guzzlehttp/guzzle": "~6.5||~7.0", + "guzzlehttp/guzzle": "~6.5.8||~7.4.5", "guzzlehttp/psr7": "^1.8.4||^2.2.1" }, "require-dev": { From 73705c2a65bfc01fa6d7717b7f401b8288fe0587 Mon Sep 17 00:00:00 2001 From: Yoeri Boven Date: Wed, 3 Jan 2024 18:24:46 +0100 Subject: [PATCH 272/301] fix: phpseclib security vulnerability (#2524) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 2a930f398..687c2ed24 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "google/apiclient-services": "~0.200", "firebase/php-jwt": "~6.0", "monolog/monolog": "^2.9||^3.0", - "phpseclib/phpseclib": "^3.0.19", + "phpseclib/phpseclib": "^3.0.34", "guzzlehttp/guzzle": "~6.5.8||~7.4.5", "guzzlehttp/psr7": "^1.8.4||^2.2.1" }, From 9c98945c49ad4bb46c4971b0863b60fa9680fd0c Mon Sep 17 00:00:00 2001 From: "release-please[bot]" <55107282+release-please[bot]@users.noreply.github.com> Date: Wed, 3 Jan 2024 17:27:58 +0000 Subject: [PATCH 273/301] chore(main): release 2.15.2 (#2533) --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6d29393c..dfb009aa6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## [2.15.2](https://github.com/googleapis/google-api-php-client/compare/v2.15.1...v2.15.2) (2024-01-03) + + +### Bug Fixes + +* Disallow vulnerable guzzle versions ([#2536](https://github.com/googleapis/google-api-php-client/issues/2536)) ([d1830ed](https://github.com/googleapis/google-api-php-client/commit/d1830ede17114a4951ab9e60b3b9bcd9393b8668)) +* Php 8.3 deprecated get_class method call without argument ([#2509](https://github.com/googleapis/google-api-php-client/issues/2509)) ([8c66021](https://github.com/googleapis/google-api-php-client/commit/8c6602119b631e1a9da4dbe219af18d51c8dab8e)) +* Phpseclib security vulnerability ([#2524](https://github.com/googleapis/google-api-php-client/issues/2524)) ([73705c2](https://github.com/googleapis/google-api-php-client/commit/73705c2a65bfc01fa6d7717b7f401b8288fe0587)) + ## [2.15.1](https://github.com/googleapis/google-api-php-client/compare/v2.15.0...v2.15.1) (2023-09-12) From c270f28b00594a151a887edd3cfd205594a1256a Mon Sep 17 00:00:00 2001 From: Vojta Svoboda Date: Thu, 4 Jan 2024 20:11:32 +0100 Subject: [PATCH 274/301] fix: guzzle dependency version (#2546) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 687c2ed24..981421845 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "firebase/php-jwt": "~6.0", "monolog/monolog": "^2.9||^3.0", "phpseclib/phpseclib": "^3.0.34", - "guzzlehttp/guzzle": "~6.5.8||~7.4.5", + "guzzlehttp/guzzle": "^6.5.8||^7.4.5", "guzzlehttp/psr7": "^1.8.4||^2.2.1" }, "require-dev": { From e70273c06d18824de77e114247ae3102f8aec64d Mon Sep 17 00:00:00 2001 From: "release-please[bot]" <55107282+release-please[bot]@users.noreply.github.com> Date: Thu, 4 Jan 2024 11:15:22 -0800 Subject: [PATCH 275/301] chore(main): release 2.15.3 (#2547) --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dfb009aa6..7813e2ba4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [2.15.3](https://github.com/googleapis/google-api-php-client/compare/v2.15.2...v2.15.3) (2024-01-04) + + +### Bug Fixes + +* Guzzle dependency version ([#2546](https://github.com/googleapis/google-api-php-client/issues/2546)) ([c270f28](https://github.com/googleapis/google-api-php-client/commit/c270f28b00594a151a887edd3cfd205594a1256a)) + ## [2.15.2](https://github.com/googleapis/google-api-php-client/compare/v2.15.1...v2.15.2) (2024-01-03) From 36a2454bcd8e79ac61020a8cceb32eb234ca9ceb Mon Sep 17 00:00:00 2001 From: Yash Sahu <54198301+yash30201@users.noreply.github.com> Date: Thu, 18 Jan 2024 03:44:55 +0530 Subject: [PATCH 276/301] chore: increase minimum supported version for guzzle psr7 (#2554) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 981421845..867674e5e 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "monolog/monolog": "^2.9||^3.0", "phpseclib/phpseclib": "^3.0.34", "guzzlehttp/guzzle": "^6.5.8||^7.4.5", - "guzzlehttp/psr7": "^1.8.4||^2.2.1" + "guzzlehttp/psr7": "^1.9.1||^2.2.1" }, "require-dev": { "squizlabs/php_codesniffer": "^3.8", From ab9bfc33eda876968a45ef6c56e906c79469a56c Mon Sep 17 00:00:00 2001 From: Yash Sahu <54198301+yash30201@users.noreply.github.com> Date: Thu, 18 Jan 2024 03:47:03 +0530 Subject: [PATCH 277/301] chore: update minimum composer version (#2553) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 867674e5e..8d8aa95fc 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "symfony/css-selector": "~2.1", "cache/filesystem-adapter": "^1.1", "phpcompatibility/php-compatibility": "^9.2", - "composer/composer": "^1.10.22", + "composer/composer": "^1.10.23", "phpspec/prophecy-phpunit": "^2.0", "phpunit/phpunit": "^9.5" }, From 9a5fa994273c7b59207f47a954dada0c7d9f4c1c Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 23 Feb 2024 17:53:27 -0600 Subject: [PATCH 278/301] chore: fix client phpdoc (#2568) --- src/Client.php | 99 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 60 insertions(+), 39 deletions(-) diff --git a/src/Client.php b/src/Client.php index 31b3f1d5f..046670551 100644 --- a/src/Client.php +++ b/src/Client.php @@ -105,47 +105,84 @@ class Client /** * Construct the Google Client. * - * @param array $config + * @param array $config { + * An array of required and optional arguments. + * + * @type string $application_name + * The name of your application + * @type string $base_path + * The base URL for the service. This is only accounted for when calling + * {@see Client::authorize()} directly. + * @type string $client_id + * Your Google Cloud client ID found in https://developers.google.com/console + * @type string $client_secret + * Your Google Cloud client secret found in https://developers.google.com/console + * @type string|array|CredentialsLoader $credentials + * Can be a path to JSON credentials or an array representing those + * credentials (@see Google\Client::setAuthConfig), or an instance of + * {@see CredentialsLoader}. + * @type string|array $scopes + * {@see Google\Client::setScopes} + * @type string $quota_project + * Sets X-Goog-User-Project, which specifies a user project to bill + * for access charges associated with the request. + * @type string $redirect_uri + * @type string $state + * @type string $developer_key + * Simple API access key, also from the API console. Ensure you get + * a Server key, and not a Browser key. + * @type bool $use_application_default_credentials + * For use with Google Cloud Platform + * fetch the ApplicationDefaultCredentials, if applicable + * {@see https://developers.google.com/identity/protocols/application-default-credentials} + * @type string $signing_key + * @type string $signing_algorithm + * @type string $subject + * @type string $hd + * @type string $prompt + * @type string $openid + * @type bool $include_granted_scopes + * @type string $login_hint + * @type string $request_visible_actions + * @type string $access_type + * @type string $approval_prompt + * @type array $retry + * Task Runner retry configuration + * {@see \Google\Task\Runner} + * @type array $retry_map + * @type CacheItemPoolInterface $cache + * Cache class implementing {@see CacheItemPoolInterface}. Defaults + * to {@see MemoryCacheItemPool}. + * @type array $cache_config + * Cache config for downstream auth caching. + * @type callable $token_callback + * Function to be called when an access token is fetched. Follows + * the signature `function (string $cacheKey, string $accessToken)`. + * @type \Firebase\JWT $jwt + * Service class used in {@see Client::verifyIdToken()}. Explicitly + * pass this in to avoid setting {@see \Firebase\JWT::$leeway} + * @type bool $api_format_v2 + * Setting api_format_v2 will return more detailed error messages + * from certain APIs. + * } */ public function __construct(array $config = []) { $this->config = array_merge([ 'application_name' => '', - - // Don't change these unless you're working against a special development - // or testing environment. 'base_path' => self::API_BASE_PATH, - - // https://developers.google.com/console 'client_id' => '', 'client_secret' => '', - - // Can be a path to JSON credentials or an array representing those - // credentials (@see Google\Client::setAuthConfig), or an instance of - // Google\Auth\CredentialsLoader. 'credentials' => null, - // @see Google\Client::setScopes 'scopes' => null, - // Sets X-Goog-User-Project, which specifies a user project to bill - // for access charges associated with the request 'quota_project' => null, - 'redirect_uri' => null, 'state' => null, - - // Simple API access key, also from the API console. Ensure you get - // a Server key, and not a Browser key. 'developer_key' => '', - - // For use with Google Cloud Platform - // fetch the ApplicationDefaultCredentials, if applicable - // @see https://developers.google.com/identity/protocols/application-default-credentials 'use_application_default_credentials' => false, 'signing_key' => null, 'signing_algorithm' => null, 'subject' => null, - - // Other OAuth2 parameters. 'hd' => '', 'prompt' => '', 'openid.realm' => '', @@ -154,28 +191,12 @@ public function __construct(array $config = []) 'request_visible_actions' => '', 'access_type' => 'online', 'approval_prompt' => 'auto', - - // Task Runner retry configuration - // @see Google\Task\Runner 'retry' => [], 'retry_map' => null, - - // Cache class implementing Psr\Cache\CacheItemPoolInterface. - // Defaults to Google\Auth\Cache\MemoryCacheItemPool. 'cache' => null, - // cache config for downstream auth caching 'cache_config' => [], - - // function to be called when an access token is fetched - // follows the signature function ($cacheKey, $accessToken) 'token_callback' => null, - - // Service class used in Google\Client::verifyIdToken. - // Explicitly pass this in to avoid setting JWT::$leeway 'jwt' => null, - - // Setting api_format_v2 will return more detailed error messages - // from certain APIs. 'api_format_v2' => false ], $config); From 633d41f1b65fdb71a83bf747f7a3ad9857f6d02a Mon Sep 17 00:00:00 2001 From: Yoeri Boven Date: Wed, 6 Mar 2024 08:18:29 +0100 Subject: [PATCH 279/301] fix: updates phpseclib because of a security issue (#2574) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8d8aa95fc..8cb8b941d 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "google/apiclient-services": "~0.200", "firebase/php-jwt": "~6.0", "monolog/monolog": "^2.9||^3.0", - "phpseclib/phpseclib": "^3.0.34", + "phpseclib/phpseclib": "^3.0.36", "guzzlehttp/guzzle": "^6.5.8||^7.4.5", "guzzlehttp/psr7": "^1.9.1||^2.2.1" }, From 73fa9cf8d8886db7269bcda0457d0a251a02cfd9 Mon Sep 17 00:00:00 2001 From: "release-please[bot]" <55107282+release-please[bot]@users.noreply.github.com> Date: Tue, 23 Apr 2024 17:56:12 -0700 Subject: [PATCH 280/301] chore(main): release 2.15.4 (#2576) --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7813e2ba4..e8f4dccc6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [2.15.4](https://github.com/googleapis/google-api-php-client/compare/v2.15.3...v2.15.4) (2024-03-06) + + +### Bug Fixes + +* Updates phpseclib because of a security issue ([#2574](https://github.com/googleapis/google-api-php-client/issues/2574)) ([633d41f](https://github.com/googleapis/google-api-php-client/commit/633d41f1b65fdb71a83bf747f7a3ad9857f6d02a)) + ## [2.15.3](https://github.com/googleapis/google-api-php-client/compare/v2.15.2...v2.15.3) (2024-01-04) From 35895ded90b507074b3430a94a5790ddd01f39f0 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Tue, 23 Apr 2024 17:57:59 -0700 Subject: [PATCH 281/301] feat: add universe domain support (#2563) --- composer.json | 4 +-- src/Client.php | 40 +++++++++++++++++++++++++- src/Http/Batch.php | 7 ++++- src/Service.php | 6 +++- src/Service/Resource.php | 16 ++++++----- tests/Google/ClientTest.php | 31 ++++++++++++++++++++ tests/Google/Service/ResourceTest.php | 41 ++++++++++++++++++++++++--- tests/Google/ServiceTest.php | 1 + 8 files changed, 130 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index 8cb8b941d..14c5d4207 100644 --- a/composer.json +++ b/composer.json @@ -7,8 +7,8 @@ "license": "Apache-2.0", "require": { "php": "^7.4|^8.0", - "google/auth": "^1.33", - "google/apiclient-services": "~0.200", + "google/auth": "^1.37", + "google/apiclient-services": "~0.350", "firebase/php-jwt": "~6.0", "monolog/monolog": "^2.9||^3.0", "phpseclib/phpseclib": "^3.0.36", diff --git a/src/Client.php b/src/Client.php index 046670551..c7724bd08 100644 --- a/src/Client.php +++ b/src/Client.php @@ -27,6 +27,7 @@ use Google\Auth\Credentials\UserRefreshCredentials; use Google\Auth\CredentialsLoader; use Google\Auth\FetchAuthTokenCache; +use Google\Auth\GetUniverseDomainInterface; use Google\Auth\HttpHandler\HttpHandlerFactory; use Google\Auth\OAuth2; use Google\AuthHandler\AuthHandlerFactory; @@ -131,6 +132,10 @@ class Client * @type string $developer_key * Simple API access key, also from the API console. Ensure you get * a Server key, and not a Browser key. + * **NOTE:** The universe domain is assumed to be "googleapis.com" unless + * explicitly set. When setting an API ley directly via this option, there + * is no way to verify the universe domain. Be sure to set the + * "universe_domain" option if "googleapis.com" is not intended. * @type bool $use_application_default_credentials * For use with Google Cloud Platform * fetch the ApplicationDefaultCredentials, if applicable @@ -164,6 +169,10 @@ class Client * @type bool $api_format_v2 * Setting api_format_v2 will return more detailed error messages * from certain APIs. + * @type string $universe_domain + * Setting the universe domain will change the default rootUrl of the service. + * If not set explicitly, the universe domain will be the value provided in the + *. "GOOGLE_CLOUD_UNIVERSE_DOMAIN" environment variable, or "googleapis.com". * } */ public function __construct(array $config = []) @@ -197,7 +206,9 @@ public function __construct(array $config = []) 'cache_config' => [], 'token_callback' => null, 'jwt' => null, - 'api_format_v2' => false + 'api_format_v2' => false, + 'universe_domain' => getenv('GOOGLE_CLOUD_UNIVERSE_DOMAIN') + ?: GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN, ], $config); if (!is_null($this->config['credentials'])) { @@ -449,6 +460,7 @@ public function authorize(ClientInterface $http = null) // 3b. If access token exists but is expired, try to refresh it // 4. Check for API Key if ($this->credentials) { + $this->checkUniverseDomain($this->credentials); return $authHandler->attachCredentials( $http, $this->credentials, @@ -458,6 +470,7 @@ public function authorize(ClientInterface $http = null) if ($this->isUsingApplicationDefaultCredentials()) { $credentials = $this->createApplicationDefaultCredentials(); + $this->checkUniverseDomain($credentials); return $authHandler->attachCredentialsCache( $http, $credentials, @@ -473,6 +486,7 @@ public function authorize(ClientInterface $http = null) $scopes, $token['refresh_token'] ); + $this->checkUniverseDomain($credentials); return $authHandler->attachCredentials( $http, $credentials, @@ -525,6 +539,11 @@ public function isUsingApplicationDefaultCredentials() * as calling `clear()` will remove all cache items, including any items not * related to Google API PHP Client.) * + * **NOTE:** The universe domain is assumed to be "googleapis.com" unless + * explicitly set. When setting an access token directly via this method, there + * is no way to verify the universe domain. Be sure to set the "universe_domain" + * option if "googleapis.com" is not intended. + * * @param string|array $token * @throws InvalidArgumentException */ @@ -1318,4 +1337,23 @@ private function createUserRefreshCredentials($scope, $refreshToken) return new UserRefreshCredentials($scope, $creds); } + + private function checkUniverseDomain($credentials) + { + $credentialsUniverse = $credentials instanceof GetUniverseDomainInterface + ? $credentials->getUniverseDomain() + : GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN; + if ($credentialsUniverse !== $this->getUniverseDomain()) { + throw new DomainException(sprintf( + 'The configured universe domain (%s) does not match the credential universe domain (%s)', + $this->getUniverseDomain(), + $credentialsUniverse + )); + } + } + + public function getUniverseDomain() + { + return $this->config['universe_domain']; + } } diff --git a/src/Http/Batch.php b/src/Http/Batch.php index f37045c01..d16708f20 100644 --- a/src/Http/Batch.php +++ b/src/Http/Batch.php @@ -62,7 +62,12 @@ public function __construct( ) { $this->client = $client; $this->boundary = $boundary ?: mt_rand(); - $this->rootUrl = rtrim($rootUrl ?: $this->client->getConfig('base_path'), '/'); + $rootUrl = rtrim($rootUrl ?: $this->client->getConfig('base_path'), '/'); + $this->rootUrl = str_replace( + 'UNIVERSE_DOMAIN', + $this->client->getUniverseDomain(), + $rootUrl + ); $this->batchPath = $batchPath ?: self::BATCH_PATH; } diff --git a/src/Service.php b/src/Service.php index c97ee9d4f..8c8fe5fa7 100644 --- a/src/Service.php +++ b/src/Service.php @@ -23,7 +23,11 @@ class Service { public $batchPath; + /** + * Only used in getBatch + */ public $rootUrl; + public $rootUrlTemplate; public $version; public $servicePath; public $serviceName; @@ -65,7 +69,7 @@ public function createBatch() return new Batch( $this->client, false, - $this->rootUrl, + $this->rootUrlTemplate ?? $this->rootUrl, $this->batchPath ); } diff --git a/src/Service/Resource.php b/src/Service/Resource.php index ecf402b18..edc3a36ee 100644 --- a/src/Service/Resource.php +++ b/src/Service/Resource.php @@ -45,8 +45,8 @@ class Resource 'prettyPrint' => ['type' => 'string', 'location' => 'query'], ]; - /** @var string $rootUrl */ - private $rootUrl; + /** @var string $rootUrlTemplate */ + private $rootUrlTemplate; /** @var \Google\Client $client */ private $client; @@ -65,7 +65,7 @@ class Resource public function __construct($service, $serviceName, $resourceName, $resource) { - $this->rootUrl = $service->rootUrl; + $this->rootUrlTemplate = $service->rootUrlTemplate ?? $service->rootUrl; $this->client = $service->getClient(); $this->servicePath = $service->servicePath; $this->serviceName = $serviceName; @@ -268,12 +268,14 @@ public function createRequestUri($restPath, $params) $requestUrl = $this->servicePath . $restPath; } - // code for leading slash - if ($this->rootUrl) { - if ('/' !== substr($this->rootUrl, -1) && '/' !== substr($requestUrl, 0, 1)) { + if ($this->rootUrlTemplate) { + // code for universe domain + $rootUrl = str_replace('UNIVERSE_DOMAIN', $this->client->getUniverseDomain(), $this->rootUrlTemplate); + // code for leading slash + if ('/' !== substr($rootUrl, -1) && '/' !== substr($requestUrl, 0, 1)) { $requestUrl = '/' . $requestUrl; } - $requestUrl = $this->rootUrl . $requestUrl; + $requestUrl = $rootUrl . $requestUrl; } $uriTemplateVars = []; $queryVars = []; diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 94ce8b876..81ea7525e 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -24,7 +24,9 @@ use Google\Service\Drive; use Google\AuthHandler\AuthHandlerFactory; use Google\Auth\FetchAuthTokenCache; +use Google\Auth\CredentialsLoader; use Google\Auth\GCECache; +use Google\Auth\Credentials\GCECredentials; use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; @@ -37,6 +39,7 @@ use ReflectionMethod; use InvalidArgumentException; use Exception; +use DomainException; class ClientTest extends BaseTest { @@ -689,11 +692,20 @@ public function testOnGceCacheAndCacheOptions() $mockCacheItem->get() ->shouldBeCalledTimes(1) ->willReturn(true); + $mockUniverseDomainCacheItem = $this->prophesize(CacheItemInterface::class); + $mockUniverseDomainCacheItem->isHit() + ->willReturn(true); + $mockUniverseDomainCacheItem->get() + ->shouldBeCalledTimes(1) + ->willReturn('googleapis.com'); $mockCache = $this->prophesize(CacheItemPoolInterface::class); $mockCache->getItem($prefix . GCECache::GCE_CACHE_KEY) ->shouldBeCalledTimes(1) ->willReturn($mockCacheItem->reveal()); + $mockCache->getItem(GCECredentials::cacheKey . 'universe_domain') + ->shouldBeCalledTimes(1) + ->willReturn($mockUniverseDomainCacheItem->reveal()); $client = new Client(['cache_config' => $cacheConfig]); $client->setCache($mockCache->reveal()); @@ -849,6 +861,8 @@ public function testCredentialsOptionWithCredentialsLoader() $credentials = $this->prophesize('Google\Auth\CredentialsLoader'); $credentials->getCacheKey() ->willReturn('cache-key'); + $credentials->getUniverseDomain() + ->willReturn('googleapis.com'); // Ensure the access token provided by our credentials loader is used $credentials->updateMetadata([], null, Argument::any()) @@ -913,4 +927,21 @@ public function testQueryParamsForAuthUrl() ]); $this->assertStringContainsString('&enable_serial_consent=true', $authUrl1); } + public function testUniverseDomainMismatch() + { + $this->expectException(DomainException::class); + $this->expectExceptionMessage( + 'The configured universe domain (example.com) does not match the credential universe domain (foo.com)' + ); + + $credentials = $this->prophesize(CredentialsLoader::class); + $credentials->getUniverseDomain() + ->shouldBeCalledOnce() + ->willReturn('foo.com'); + $client = new Client([ + 'universe_domain' => 'example.com', + 'credentials' => $credentials->reveal(), + ]); + $client->authorize(); + } } diff --git a/tests/Google/Service/ResourceTest.php b/tests/Google/Service/ResourceTest.php index 17000880a..86e0bef24 100644 --- a/tests/Google/Service/ResourceTest.php +++ b/tests/Google/Service/ResourceTest.php @@ -35,10 +35,11 @@ class TestService extends \Google\Service { - public function __construct(Client $client) + public function __construct(Client $client, $rootUrl = null) { parent::__construct($client); - $this->rootUrl = "https://test.example.com/"; + $this->rootUrl = $rootUrl ?: "https://test.example.com/"; + $this->rootUrlTemplate = $rootUrl ?: "https://test.universe_domain/"; $this->servicePath = ""; $this->version = "v1beta1"; $this->serviceName = "test"; @@ -59,6 +60,7 @@ public function setUp(): void $this->client->getLogger()->willReturn($logger->reveal()); $this->client->shouldDefer()->willReturn(true); $this->client->getHttpClient()->willReturn(new GuzzleClient()); + $this->client->getUniverseDomain()->willReturn('example.com'); $this->service = new TestService($this->client->reveal()); } @@ -106,6 +108,37 @@ public function testCall() $this->assertFalse($request->hasHeader('Content-Type')); } + public function testCallWithUniverseDomainTemplate() + { + $client = $this->prophesize(Client::class); + $logger = $this->prophesize("Monolog\Logger"); + $this->client->getLogger()->willReturn($logger->reveal()); + $this->client->shouldDefer()->willReturn(true); + $this->client->getHttpClient()->willReturn(new GuzzleClient()); + $this->client->getUniverseDomain()->willReturn('example-universe-domain.com'); + + $this->service = new TestService($this->client->reveal()); + + $resource = new GoogleResource( + $this->service, + "test", + "testResource", + [ + "methods" => [ + "testMethod" => [ + "parameters" => [], + "path" => "method/path", + "httpMethod" => "POST", + ] + ] + ] + ); + $request = $resource->call("testMethod", [[]]); + $this->assertEquals("https://test.example-universe-domain.com/method/path", (string) $request->getUri()); + $this->assertEquals("POST", $request->getMethod()); + $this->assertFalse($request->hasHeader('Content-Type')); + } + public function testCallWithPostBody() { $resource = new GoogleResource( @@ -130,9 +163,9 @@ public function testCallWithPostBody() public function testCallServiceDefinedRoot() { - $this->service->rootUrl = "https://sample.example.com/"; + $service = new TestService($this->client->reveal(), "https://sample.example.com/"); $resource = new GoogleResource( - $this->service, + $service, "test", "testResource", [ diff --git a/tests/Google/ServiceTest.php b/tests/Google/ServiceTest.php index 82abeb423..10bb44c7d 100644 --- a/tests/Google/ServiceTest.php +++ b/tests/Google/ServiceTest.php @@ -80,6 +80,7 @@ function ($request) { )->willReturn($response->reveal()); $client->getConfig('base_path')->willReturn(''); + $client->getUniverseDomain()->willReturn(''); $model = new TestService($client->reveal()); $batch = $model->createBatch(); From 017400f609c1fb71ab5ad824c50eabd4c3eaf779 Mon Sep 17 00:00:00 2001 From: "release-please[bot]" <55107282+release-please[bot]@users.noreply.github.com> Date: Wed, 24 Apr 2024 00:59:47 +0000 Subject: [PATCH 282/301] chore(main): release 2.16.0 (#2589) --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8f4dccc6..800b01c16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [2.16.0](https://github.com/googleapis/google-api-php-client/compare/v2.15.4...v2.16.0) (2024-04-24) + + +### Features + +* Add universe domain support ([#2563](https://github.com/googleapis/google-api-php-client/issues/2563)) ([35895de](https://github.com/googleapis/google-api-php-client/commit/35895ded90b507074b3430a94a5790ddd01f39f0)) + ## [2.15.4](https://github.com/googleapis/google-api-php-client/compare/v2.15.3...v2.15.4) (2024-03-06) From 7e79f3d7be4811f760e19cc4a2c558e04196ec1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Mendoza?= Date: Thu, 2 May 2024 12:34:51 -0400 Subject: [PATCH 283/301] feat: add the protected apiVersion property (#2588) --- src/Service/Resource.php | 10 +++++++++ tests/Google/Service/ResourceTest.php | 29 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/Service/Resource.php b/src/Service/Resource.php index edc3a36ee..693aaa781 100644 --- a/src/Service/Resource.php +++ b/src/Service/Resource.php @@ -48,6 +48,9 @@ class Resource /** @var string $rootUrlTemplate */ private $rootUrlTemplate; + /** @var string $apiVersion */ + protected $apiVersion; + /** @var \Google\Client $client */ private $client; @@ -225,6 +228,13 @@ public function call($name, $arguments, $expectedClass = null) $expectedClass = null; } + // If the class which is extending from this one contains + // an Api Version, add it to the header + if ($this->apiVersion) { + $request = $request + ->withHeader('X-Goog-Api-Version', $this->apiVersion); + } + // if the client is marked for deferring, rather than // execute the request, return the response if ($this->client->shouldDefer()) { diff --git a/tests/Google/Service/ResourceTest.php b/tests/Google/Service/ResourceTest.php index 86e0bef24..ccf29a7f7 100644 --- a/tests/Google/Service/ResourceTest.php +++ b/tests/Google/Service/ResourceTest.php @@ -106,6 +106,7 @@ public function testCall() $this->assertEquals("https://test.example.com/method/path", (string) $request->getUri()); $this->assertEquals("POST", $request->getMethod()); $this->assertFalse($request->hasHeader('Content-Type')); + $this->assertFalse($request->hasHeader('X-Goog-Api-Version')); } public function testCallWithUniverseDomainTemplate() @@ -474,4 +475,32 @@ public function testExceptionMessage() $this->assertEquals($errors, $e->getErrors()); } } + + public function testVersionedResource() + { + $resource = new VersionedResource( + $this->service, + "test", + "testResource", + [ + "methods" => [ + "testMethod" => [ + "parameters" => [], + "path" => "method/path", + "httpMethod" => "POST", + ] + ] + ] + ); + $request = $resource->call("testMethod", [['postBody' => ['foo' => 'bar']]]); + $this->assertEquals("https://test.example.com/method/path", (string) $request->getUri()); + $this->assertEquals("POST", $request->getMethod()); + $this->assertTrue($request->hasHeader('X-Goog-Api-Version')); + $this->assertEquals('v1_20240101', $request->getHeaderLine('X-Goog-Api-Version')); + } +} + +class VersionedResource extends GoogleResource +{ + protected $apiVersion = 'v1_20240101'; } From dae35dfc3118d3a752c9ef08635fd0fefe84417d Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 17 May 2024 11:31:11 -0600 Subject: [PATCH 284/301] chore: drop support for PHP 7.4 (#2562) --- .github/sync-repo-settings.yaml | 3 +-- .github/workflows/asset-release.yml | 2 +- .github/workflows/docs.yml | 2 +- .github/workflows/tests.yml | 4 ++-- README.md | 4 ++-- composer.json | 12 ++++++------ 6 files changed, 13 insertions(+), 14 deletions(-) diff --git a/.github/sync-repo-settings.yaml b/.github/sync-repo-settings.yaml index 7a538ecaf..c4543198e 100644 --- a/.github/sync-repo-settings.yaml +++ b/.github/sync-repo-settings.yaml @@ -5,9 +5,8 @@ branchProtectionRules: - pattern: master isAdminEnforced: true requiredStatusCheckContexts: - - 'PHP 7.4 Unit Test' - - 'PHP 7.4 --prefer-lowest Unit Test' - 'PHP 8.0 Unit Test' + - 'PHP 8.0 --prefer-lowest Unit Test' - 'PHP 8.1 Unit Test' - 'PHP 8.2 Unit Test' - 'PHP 8.3 Unit Test' diff --git a/.github/workflows/asset-release.yml b/.github/workflows/asset-release.yml index 59d5cdd56..bc4b2bf8d 100644 --- a/.github/workflows/asset-release.yml +++ b/.github/workflows/asset-release.yml @@ -10,7 +10,7 @@ jobs: strategy: matrix: operating-system: [ ubuntu-latest ] - php: [ "7.4", "8.0", "8.2" ] + php: [ "8.0", "8.3" ] name: Upload Release Assets steps: diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 02d757bc1..ae9bf2ed0 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -18,7 +18,7 @@ jobs: max_attempts: 3 command: composer install - name: Generate and Push Documentation - uses: docker://php:7.4-cli + uses: docker://php:8.1-cli env: GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} with: diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 614045f13..8f833c134 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -11,10 +11,10 @@ jobs: strategy: fail-fast: false matrix: - php: [ "7.4", "8.0", "8.1", "8.2", "8.3" ] + php: [ "8.0", "8.1", "8.2", "8.3" ] composer-flags: [""] include: - - php: "7.4" + - php: "8.0" composer-flags: "--prefer-lowest " - php: "8.3" composer-flags: "--prefer-lowest " diff --git a/README.md b/README.md index 34960da0d..abd64f123 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ list of [Google cloud packages](https://cloud.google.com/php/docs/reference) fir these are the recommended libraries.
    -
    Reference Docs
    https://googleapis.github.io/google-api-php-client/main/
    +
    Reference Docs
    https://googleapis.github.io/google-api-php-client/main/
    License
    Apache 2.0
    @@ -25,7 +25,7 @@ For Google Cloud Platform APIs such as [Datastore][cloud-datastore], [Cloud Stor [cloud-compute]: https://github.com/googleapis/google-cloud-php-compute ## Requirements ## -* [PHP 7.4 or higher](https://www.php.net/) +* [PHP 8.0 or higher](https://www.php.net/) ## Developer Documentation ## diff --git a/composer.json b/composer.json index 14c5d4207..abacbe84b 100644 --- a/composer.json +++ b/composer.json @@ -6,14 +6,14 @@ "homepage": "http://developers.google.com/api-client-library/php", "license": "Apache-2.0", "require": { - "php": "^7.4|^8.0", + "php": "^8.0", "google/auth": "^1.37", "google/apiclient-services": "~0.350", - "firebase/php-jwt": "~6.0", + "firebase/php-jwt": "^6.0", "monolog/monolog": "^2.9||^3.0", "phpseclib/phpseclib": "^3.0.36", - "guzzlehttp/guzzle": "^6.5.8||^7.4.5", - "guzzlehttp/psr7": "^1.9.1||^2.2.1" + "guzzlehttp/guzzle": "^7.4.5", + "guzzlehttp/psr7": "^2.6" }, "require-dev": { "squizlabs/php_codesniffer": "^3.8", @@ -22,8 +22,8 @@ "cache/filesystem-adapter": "^1.1", "phpcompatibility/php-compatibility": "^9.2", "composer/composer": "^1.10.23", - "phpspec/prophecy-phpunit": "^2.0", - "phpunit/phpunit": "^9.5" + "phpspec/prophecy-phpunit": "^2.1", + "phpunit/phpunit": "^9.6" }, "suggest": { "cache/filesystem-adapter": "For caching certs and tokens (using Google\\Client::setCache)" From 71579b70a11f1d57ee85b89ac8c9c1d27a6aec27 Mon Sep 17 00:00:00 2001 From: Adrian Kirchner <149483+adriankirchner@users.noreply.github.com> Date: Wed, 22 May 2024 17:07:21 +0200 Subject: [PATCH 285/301] docs: fix link to github hosted reference docs (#2598) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index abd64f123..95150d9f1 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ list of [Google cloud packages](https://cloud.google.com/php/docs/reference) fir these are the recommended libraries.
    -
    Reference Docs
    https://googleapis.github.io/google-api-php-client/main/
    +
    Reference Docs
    https://googleapis.github.io/google-api-php-client/main/
    License
    Apache 2.0
    From 1f4713329d71111a317cda8ef8603fa1bdc88858 Mon Sep 17 00:00:00 2001 From: Razvan Grigore Date: Wed, 10 Jul 2024 17:56:12 +0300 Subject: [PATCH 286/301] feat: add logger to client constructor config (#2606) --- src/Client.php | 6 ++++++ tests/Google/ClientTest.php | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/Client.php b/src/Client.php index c7724bd08..330e8ac6b 100644 --- a/src/Client.php +++ b/src/Client.php @@ -196,6 +196,7 @@ public function __construct(array $config = []) 'prompt' => '', 'openid.realm' => '', 'include_granted_scopes' => null, + 'logger' => null, 'login_hint' => '', 'request_visible_actions' => '', 'access_type' => 'online', @@ -242,6 +243,11 @@ public function __construct(array $config = []) $this->setCache($this->config['cache']); unset($this->config['cache']); } + + if (!is_null($this->config['logger'])) { + $this->setLogger($this->config['logger']); + unset($this->config['logger']); + } } /** diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 81ea7525e..7aea7e9ef 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -268,6 +268,16 @@ public function testDefaultLoggerAppEngine() $this->assertInstanceOf('Monolog\Handler\SyslogHandler', $handler); } + public function testLoggerFromConstructor() + { + $logger1 = new \Monolog\Logger('unit-test'); + $client = new Client(['logger' => $logger1]); + $logger2 = $client->getLogger(); + $this->assertInstanceOf('Monolog\Logger', $logger2); + $this->assertEquals('unit-test', $logger2->getName()); + $this->assertSame($logger1, $logger2); + } + public function testSettersGetters() { $client = new Client(); @@ -279,6 +289,7 @@ public function testSettersGetters() $client->setRedirectUri('localhost'); $client->setConfig('application_name', 'me'); + $client->setLogger(new \Monolog\Logger('test')); $cache = $this->prophesize(CacheItemPoolInterface::class); $client->setCache($cache->reveal()); From b1f63d72c44307ec8ef7bf18f1012de35d8944ed Mon Sep 17 00:00:00 2001 From: "release-please[bot]" <55107282+release-please[bot]@users.noreply.github.com> Date: Wed, 10 Jul 2024 14:57:54 +0000 Subject: [PATCH 287/301] chore(main): release 2.17.0 (#2590) --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 800b01c16..a43c59d90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## [2.17.0](https://github.com/googleapis/google-api-php-client/compare/v2.16.0...v2.17.0) (2024-07-10) + + +### Features + +* Add logger to client constructor config ([#2606](https://github.com/googleapis/google-api-php-client/issues/2606)) ([1f47133](https://github.com/googleapis/google-api-php-client/commit/1f4713329d71111a317cda8ef8603fa1bdc88858)) +* Add the protected apiVersion property ([#2588](https://github.com/googleapis/google-api-php-client/issues/2588)) ([7e79f3d](https://github.com/googleapis/google-api-php-client/commit/7e79f3d7be4811f760e19cc4a2c558e04196ec1d)) + ## [2.16.0](https://github.com/googleapis/google-api-php-client/compare/v2.15.4...v2.16.0) (2024-04-24) From 242e2cb09ad5b25b047a862b4d521037e74cae29 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 26 Aug 2024 09:47:56 -0700 Subject: [PATCH 288/301] feat(docs): use doctum shared workflow for reference docs (#2618) --- .github/actions/docs/entrypoint.sh | 21 -------------- .github/actions/docs/sami.php | 29 ------------------- .github/workflows/docs.yml | 45 +++++++++++++----------------- README.md | 2 +- src/Client.php | 3 +- tests/Google/ClientTest.php | 6 ++-- 6 files changed, 27 insertions(+), 79 deletions(-) delete mode 100755 .github/actions/docs/entrypoint.sh delete mode 100644 .github/actions/docs/sami.php diff --git a/.github/actions/docs/entrypoint.sh b/.github/actions/docs/entrypoint.sh deleted file mode 100755 index 7ac74e427..000000000 --- a/.github/actions/docs/entrypoint.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/sh -l - -set -e - -apt-get update -apt-get install -y git wget - -# Fix github "detected dubious ownership" error -git config --global --add safe.directory /github/workspace - -git reset --hard HEAD - -# Create the directories -mkdir .docs -mkdir .cache - -wget https://github.com/jdpedrie/Sami/releases/download/v4.3.0/sami.phar - -# Run the docs generation command -php sami.phar update .github/actions/docs/sami.php -chmod -R 0777 . diff --git a/.github/actions/docs/sami.php b/.github/actions/docs/sami.php deleted file mode 100644 index b3d3da8f7..000000000 --- a/.github/actions/docs/sami.php +++ /dev/null @@ -1,29 +0,0 @@ -files() - ->name('*.php') - ->exclude('vendor') - ->exclude('tests') - ->in($projectRoot); - -$versions = GitVersionCollection::create($projectRoot) - ->addFromTags(function($tag) { - return 0 === strpos($tag, 'v2.') && false === strpos($tag, 'RC'); - }) - ->add('main', 'main branch'); - -return new Sami($iterator, [ - 'title' => 'Google APIs Client Library for PHP API Reference', - 'build_dir' => $projectRoot . '/.docs/%version%', - 'cache_dir' => $projectRoot . '/.cache/%version%', - 'remote_repository' => new GitHubRemoteRepository('googleapis/google-api-php-client', $projectRoot), - 'versions' => $versions -]); diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index ae9bf2ed0..b9de0cfb9 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -1,31 +1,26 @@ name: Generate Documentation on: push: - branches: [main] + tags: + - "*" + workflow_dispatch: + inputs: + tag: + description: 'Tag to generate documentation for' + required: false + pull_request: + +permissions: + contents: write jobs: docs: - name: "Generate Project Documentation" - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v2 - - run: git fetch --depth=1 origin +refs/tags/*:refs/tags/* - - name: Install Dependencies - uses: nick-invision/retry@v1 - with: - timeout_minutes: 10 - max_attempts: 3 - command: composer install - - name: Generate and Push Documentation - uses: docker://php:8.1-cli - env: - GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} - with: - entrypoint: ./.github/actions/docs/entrypoint.sh - - name: Deploy 🚀 - uses: JamesIves/github-pages-deploy-action@releases/v3 - with: - ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }} - BRANCH: gh-pages - FOLDER: .docs + name: "Generate and Deploy Documentation" + uses: GoogleCloudPlatform/php-tools/.github/workflows/doctum.yml@main + with: + title: "Google Cloud PHP Client" + default_version: ${{ inputs.tag || github.ref_name }} + exclude_file: aliases.php + tag_pattern: "v2.*" + dry_run: ${{ github.event_name == 'pull_request' }} + diff --git a/README.md b/README.md index 95150d9f1..93b52cacb 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ list of [Google cloud packages](https://cloud.google.com/php/docs/reference) fir these are the recommended libraries.
    -
    Reference Docs
    https://googleapis.github.io/google-api-php-client/main/
    +
    Reference Docs
    https://googleapis.github.io/google-api-php-client/
    License
    Apache 2.0
    diff --git a/src/Client.php b/src/Client.php index 330e8ac6b..1285c49a7 100644 --- a/src/Client.php +++ b/src/Client.php @@ -1043,7 +1043,8 @@ public function setAuthConfig($config) $key = isset($config['installed']) ? 'installed' : 'web'; if (isset($config['type']) && $config['type'] == 'service_account') { - // application default credentials + // @TODO(v3): Remove this, as it isn't accurate. ADC applies only to determining + // credentials based on the user's environment. $this->useApplicationDefaultCredentials(); // set the information from the config diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 7aea7e9ef..4e53e34b4 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -714,8 +714,10 @@ public function testOnGceCacheAndCacheOptions() $mockCache->getItem($prefix . GCECache::GCE_CACHE_KEY) ->shouldBeCalledTimes(1) ->willReturn($mockCacheItem->reveal()); - $mockCache->getItem(GCECredentials::cacheKey . 'universe_domain') - ->shouldBeCalledTimes(1) + // cache key from GCECredentials::getTokenUri() . 'universe_domain' + $mockCache->getItem('cc685e3a0717258b6a4cefcb020e96de6bcf904e76fd9fc1647669f42deff9bf') // google/auth < 1.41.0 + ->willReturn($mockUniverseDomainCacheItem->reveal()); + $mockCache->getItem(GCECredentials::cacheKey . 'universe_domain') // google/auth >= 1.41.0 ->willReturn($mockUniverseDomainCacheItem->reveal()); $client = new Client(['cache_config' => $cacheConfig]); From 76c312e2696575d315f56aba31f8979d06da06ff Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 26 Aug 2024 09:50:03 -0700 Subject: [PATCH 289/301] chore: fix docs dry-run job --- .github/workflows/docs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index b9de0cfb9..5d09dabb0 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -19,7 +19,7 @@ jobs: uses: GoogleCloudPlatform/php-tools/.github/workflows/doctum.yml@main with: title: "Google Cloud PHP Client" - default_version: ${{ inputs.tag || github.ref_name }} + default_version: ${{ inputs.tag || github.head_ref || github.ref_name }} exclude_file: aliases.php tag_pattern: "v2.*" dry_run: ${{ github.event_name == 'pull_request' }} From dc13e5e3f517148d3c66d151a5ab133b7840d8fb Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 16 Oct 2024 14:41:17 -0600 Subject: [PATCH 290/301] fix: explicit token caching issue (#2358) --- src/AuthHandler/Guzzle6AuthHandler.php | 10 ++- tests/Google/AuthHandler/AuthHandlerTest.php | 82 ++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 tests/Google/AuthHandler/AuthHandlerTest.php diff --git a/src/AuthHandler/Guzzle6AuthHandler.php b/src/AuthHandler/Guzzle6AuthHandler.php index 7e8a815c2..0c4d12852 100644 --- a/src/AuthHandler/Guzzle6AuthHandler.php +++ b/src/AuthHandler/Guzzle6AuthHandler.php @@ -74,10 +74,18 @@ public function attachToken(ClientInterface $http, array $token, array $scopes) return $token['access_token']; }; + // Derive a cache prefix from the token, to ensure setting a new token + // results in a cache-miss. + // Note: Supplying a custom "prefix" will bust this behavior. + $cacheConfig = $this->cacheConfig; + if (!isset($cacheConfig['prefix']) && isset($token['access_token'])) { + $cacheConfig['prefix'] = substr(sha1($token['access_token']), -10); + } + $middleware = new ScopedAccessTokenMiddleware( $tokenFunc, $scopes, - $this->cacheConfig, + $cacheConfig, $this->cache ); diff --git a/tests/Google/AuthHandler/AuthHandlerTest.php b/tests/Google/AuthHandler/AuthHandlerTest.php new file mode 100644 index 000000000..0bc34aa82 --- /dev/null +++ b/tests/Google/AuthHandler/AuthHandlerTest.php @@ -0,0 +1,82 @@ +attachToken( + $client, + ['access_token' => '1234'], + $scopes + ); + + // Call our middleware and verify the token is set + $scopedMiddleware = $this->getGoogleAuthMiddleware($http1); + $request = $scopedMiddleware(new Request('GET', '/'), ['auth' => 'scoped']); + $this->assertEquals(['Bearer 1234'], $request->getHeader('Authorization')); + + // Attach a new token to the HTTP client + $http2 = $authHandler->attachToken( + $client, + ['access_token' => '5678'], + $scopes + ); + + // Call our middleware and verify the NEW token is set + $scopedMiddleware = $this->getGoogleAuthMiddleware($http2); + $request = $scopedMiddleware(new Request('GET', '/'), ['auth' => 'scoped']); + $this->assertEquals(['Bearer 5678'], $request->getHeader('Authorization')); + } + + private function getGoogleAuthMiddleware(Client $http) + { + // All sorts of horrible reflection to get at our middleware + $handler = $http->getConfig()['handler']; + $reflectionMethod = new \ReflectionMethod($handler, 'findByName'); + $reflectionMethod->setAccessible(true); + $authMiddlewareIdx = $reflectionMethod->invoke($handler, 'google_auth'); + + $reflectionProperty = new \ReflectionProperty($handler, 'stack'); + $reflectionProperty->setAccessible(true); + $stack = $reflectionProperty->getValue($handler); + + $callable = $stack[$authMiddlewareIdx][0]; + return $callable(function ($request) { + return $request; + }); + } +} From 846f149c9f879449145326dad99ef00bf1d879f3 Mon Sep 17 00:00:00 2001 From: "release-please[bot]" <55107282+release-please[bot]@users.noreply.github.com> Date: Wed, 16 Oct 2024 14:58:58 -0700 Subject: [PATCH 291/301] chore(main): release 2.18.0 (#2621) --- CHANGELOG.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a43c59d90..68cb72f1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +## [2.18.0](https://github.com/googleapis/google-api-php-client/compare/v2.17.0...v2.18.0) (2024-10-16) + + +### Features + +* **docs:** Use doctum shared workflow for reference docs ([#2618](https://github.com/googleapis/google-api-php-client/issues/2618)) ([242e2cb](https://github.com/googleapis/google-api-php-client/commit/242e2cb09ad5b25b047a862b4d521037e74cae29)) + + +### Bug Fixes + +* Explicit token caching issue ([#2358](https://github.com/googleapis/google-api-php-client/issues/2358)) ([dc13e5e](https://github.com/googleapis/google-api-php-client/commit/dc13e5e3f517148d3c66d151a5ab133b7840d8fb)) + ## [2.17.0](https://github.com/googleapis/google-api-php-client/compare/v2.16.0...v2.17.0) (2024-07-10) From de57db2fdc0d56de1abbf778b28b77c3347eb3fd Mon Sep 17 00:00:00 2001 From: Feroz Date: Sun, 24 Nov 2024 19:18:35 +0600 Subject: [PATCH 292/301] fix: Implicitly marking parameter as nullable is deprecated (#2638) --- src/AccessToken/Revoke.php | 2 +- src/AccessToken/Verify.php | 18 +++++++++--------- src/AuthHandler/Guzzle6AuthHandler.php | 6 +++--- src/Client.php | 4 ++-- src/Http/REST.php | 14 +++++++------- src/Service/Exception.php | 2 +- src/Task/Composer.php | 2 +- 7 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/AccessToken/Revoke.php b/src/AccessToken/Revoke.php index dd94fe0f2..dc8b0c351 100644 --- a/src/AccessToken/Revoke.php +++ b/src/AccessToken/Revoke.php @@ -39,7 +39,7 @@ class Revoke * Instantiates the class, but does not initiate the login flow, leaving it * to the discretion of the caller. */ - public function __construct(ClientInterface $http = null) + public function __construct(?ClientInterface $http = null) { $this->http = $http; } diff --git a/src/AccessToken/Verify.php b/src/AccessToken/Verify.php index d957908ba..714b5d12d 100644 --- a/src/AccessToken/Verify.php +++ b/src/AccessToken/Verify.php @@ -59,7 +59,7 @@ class Verify /** * @var \Firebase\JWT\JWT - */ + */ public $jwt; /** @@ -67,9 +67,9 @@ class Verify * to the discretion of the caller. */ public function __construct( - ClientInterface $http = null, - CacheItemPoolInterface $cache = null, - $jwt = null + ?ClientInterface $http = null, + ?CacheItemPoolInterface $cache = null, + ?string $jwt = null ) { if (null === $http) { $http = new Client(); @@ -130,7 +130,7 @@ public function verifyIdToken($idToken, $audience = null) return false; } - return (array) $payload; + return (array)$payload; } catch (ExpiredException $e) { // @phpstan-ignore-line return false; } catch (ExpiredExceptionV3 $e) { @@ -154,8 +154,8 @@ private function getCache() * Retrieve and cache a certificates file. * * @param string $url location - * @throws \Google\Exception * @return array certificates + * @throws \Google\Exception */ private function retrieveCertsFromLocation($url) { @@ -163,8 +163,8 @@ private function retrieveCertsFromLocation($url) if (0 !== strpos($url, 'http')) { if (!$file = file_get_contents($url)) { throw new GoogleException( - "Failed to retrieve verification certificates: '" . - $url . "'." + "Failed to retrieve verification certificates: '". + $url."'." ); } @@ -175,7 +175,7 @@ private function retrieveCertsFromLocation($url) $response = $this->http->get($url); if ($response->getStatusCode() == 200) { - return json_decode((string) $response->getBody(), true); + return json_decode((string)$response->getBody(), true); } throw new GoogleException( sprintf( diff --git a/src/AuthHandler/Guzzle6AuthHandler.php b/src/AuthHandler/Guzzle6AuthHandler.php index 0c4d12852..352cf915c 100644 --- a/src/AuthHandler/Guzzle6AuthHandler.php +++ b/src/AuthHandler/Guzzle6AuthHandler.php @@ -20,7 +20,7 @@ class Guzzle6AuthHandler protected $cache; protected $cacheConfig; - public function __construct(CacheItemPoolInterface $cache = null, array $cacheConfig = []) + public function __construct(?CacheItemPoolInterface $cache = null, array $cacheConfig = []) { $this->cache = $cache; $this->cacheConfig = $cacheConfig; @@ -29,7 +29,7 @@ public function __construct(CacheItemPoolInterface $cache = null, array $cacheCo public function attachCredentials( ClientInterface $http, CredentialsLoader $credentials, - callable $tokenCallback = null + ?callable $tokenCallback = null ) { // use the provided cache if ($this->cache) { @@ -46,7 +46,7 @@ public function attachCredentials( public function attachCredentialsCache( ClientInterface $http, FetchAuthTokenCache $credentials, - callable $tokenCallback = null + ?callable $tokenCallback = null ) { // if we end up needing to make an HTTP request to retrieve credentials, we // can use our existing one, but we need to throw exceptions so the error diff --git a/src/Client.php b/src/Client.php index 1285c49a7..edfb1f83e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -321,7 +321,7 @@ public function refreshTokenWithAssertion() * @param ClientInterface $authHttp optional. * @return array access token */ - public function fetchAccessTokenWithAssertion(ClientInterface $authHttp = null) + public function fetchAccessTokenWithAssertion(?ClientInterface $authHttp = null) { if (!$this->isUsingApplicationDefaultCredentials()) { throw new DomainException( @@ -454,7 +454,7 @@ public function createAuthUrl($scope = null, array $queryParams = []) * @param ClientInterface $http the http client object. * @return ClientInterface the http client object */ - public function authorize(ClientInterface $http = null) + public function authorize(?ClientInterface $http = null) { $http = $http ?: $this->getHttpClient(); $authHandler = $this->getAuthHandler(); diff --git a/src/Http/REST.php b/src/Http/REST.php index 70e48e4b8..c3d3270db 100644 --- a/src/Http/REST.php +++ b/src/Http/REST.php @@ -54,7 +54,7 @@ public static function execute( ) { $runner = new Runner( $config, - sprintf('%s %s', $request->getMethod(), (string) $request->getUri()), + sprintf('%s %s', $request->getMethod(), (string)$request->getUri()), [self::class, 'doExecute'], [$client, $request, $expectedClass] ); @@ -120,7 +120,7 @@ interface_exists('\GuzzleHttp\Message\ResponseInterface') */ public static function decodeHttpResponse( ResponseInterface $response, - RequestInterface $request = null, + ?RequestInterface $request = null, $expectedClass = null ) { $code = $response->getStatusCode(); @@ -128,7 +128,7 @@ public static function decodeHttpResponse( // retry strategy if (intVal($code) >= 400) { // if we errored out, it should be safe to grab the response body - $body = (string) $response->getBody(); + $body = (string)$response->getBody(); // Check if we received errors, and add those to the Exception for convenience throw new GoogleServiceException($body, $code, null, self::getResponseErrors($body)); @@ -147,17 +147,17 @@ public static function decodeHttpResponse( return $response; } - private static function decodeBody(ResponseInterface $response, RequestInterface $request = null) + private static function decodeBody(ResponseInterface $response, ?RequestInterface $request = null) { if (self::isAltMedia($request)) { // don't decode the body, it's probably a really long string return ''; } - return (string) $response->getBody(); + return (string)$response->getBody(); } - private static function determineExpectedClass($expectedClass, RequestInterface $request = null) + private static function determineExpectedClass($expectedClass, ?RequestInterface $request = null) { // "false" is used to explicitly prevent an expected class from being returned if (false === $expectedClass) { @@ -184,7 +184,7 @@ private static function getResponseErrors($body) return null; } - private static function isAltMedia(RequestInterface $request = null) + private static function isAltMedia(?RequestInterface $request = null) { if ($request && $qs = $request->getUri()->getQuery()) { parse_str($qs, $query); diff --git a/src/Service/Exception.php b/src/Service/Exception.php index 6e88169c2..d79a2e75d 100644 --- a/src/Service/Exception.php +++ b/src/Service/Exception.php @@ -39,7 +39,7 @@ class Exception extends GoogleException public function __construct( $message, $code = 0, - Exception $previous = null, + ?Exception $previous = null, $errors = [] ) { if (version_compare(PHP_VERSION, '5.3.0') >= 0) { diff --git a/src/Task/Composer.php b/src/Task/Composer.php index 04969f207..fcad6bd13 100644 --- a/src/Task/Composer.php +++ b/src/Task/Composer.php @@ -30,7 +30,7 @@ class Composer */ public static function cleanup( Event $event, - Filesystem $filesystem = null + ?Filesystem $filesystem = null ) { $composer = $event->getComposer(); $extra = $composer->getPackage()->getExtra(); From 3f6cb1a970fe2d210823a79de8d5dbae405a9616 Mon Sep 17 00:00:00 2001 From: "release-please[bot]" <55107282+release-please[bot]@users.noreply.github.com> Date: Sun, 24 Nov 2024 13:21:03 +0000 Subject: [PATCH 293/301] chore(main): release 2.18.1 (#2639) --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68cb72f1b..ecb2406b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [2.18.1](https://github.com/googleapis/google-api-php-client/compare/v2.18.0...v2.18.1) (2024-11-24) + + +### Bug Fixes + +* Implicitly marking parameter as nullable is deprecated ([#2638](https://github.com/googleapis/google-api-php-client/issues/2638)) ([de57db2](https://github.com/googleapis/google-api-php-client/commit/de57db2fdc0d56de1abbf778b28b77c3347eb3fd)) + ## [2.18.0](https://github.com/googleapis/google-api-php-client/compare/v2.17.0...v2.18.0) (2024-10-16) From 8142a3e523299b999aa4833235a07f0f5fe24c82 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 4 Dec 2024 09:23:09 -0600 Subject: [PATCH 294/301] chore: add php 8.4 to test matrix (#2643) --- .github/sync-repo-settings.yaml | 3 ++- .github/workflows/tests.yml | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/sync-repo-settings.yaml b/.github/sync-repo-settings.yaml index c4543198e..a4444366a 100644 --- a/.github/sync-repo-settings.yaml +++ b/.github/sync-repo-settings.yaml @@ -10,7 +10,8 @@ branchProtectionRules: - 'PHP 8.1 Unit Test' - 'PHP 8.2 Unit Test' - 'PHP 8.3 Unit Test' - - 'PHP 8.3 --prefer-lowest Unit Test' + - 'PHP 8.4' + - 'PHP 8.4 --prefer-lowest Unit Test' - 'PHP Style Check' - 'cla/google' requiredApprovingReviewCount: 1 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 8f833c134..9f86e493a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -11,12 +11,12 @@ jobs: strategy: fail-fast: false matrix: - php: [ "8.0", "8.1", "8.2", "8.3" ] + php: [ "8.0", "8.1", "8.2", "8.3", "8.4" ] composer-flags: [""] include: - php: "8.0" composer-flags: "--prefer-lowest " - - php: "8.3" + - php: "8.4" composer-flags: "--prefer-lowest " name: PHP ${{ matrix.php }} ${{ matrix.composer-flags }}Unit Test steps: From 31a9861af02a8e9070b395f05caed7ffce0ef8be Mon Sep 17 00:00:00 2001 From: Jose Ochoa Date: Mon, 16 Dec 2024 16:50:30 -0600 Subject: [PATCH 295/301] fix: correct type for jwt constructor arg (#2648) Co-authored-by: Brent Shaffer --- src/AccessToken/Verify.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AccessToken/Verify.php b/src/AccessToken/Verify.php index 714b5d12d..5529450e5 100644 --- a/src/AccessToken/Verify.php +++ b/src/AccessToken/Verify.php @@ -69,7 +69,7 @@ class Verify public function __construct( ?ClientInterface $http = null, ?CacheItemPoolInterface $cache = null, - ?string $jwt = null + ?JWT $jwt = null ) { if (null === $http) { $http = new Client(); From d8d201ba8a189a3cd7fb34e4da569f2ed440eee7 Mon Sep 17 00:00:00 2001 From: "release-please[bot]" <55107282+release-please[bot]@users.noreply.github.com> Date: Mon, 16 Dec 2024 14:52:40 -0800 Subject: [PATCH 296/301] chore(main): release 2.18.2 (#2649) --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ecb2406b2..00f046fb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [2.18.2](https://github.com/googleapis/google-api-php-client/compare/v2.18.1...v2.18.2) (2024-12-16) + + +### Bug Fixes + +* Correct type for jwt constructor arg ([#2648](https://github.com/googleapis/google-api-php-client/issues/2648)) ([31a9861](https://github.com/googleapis/google-api-php-client/commit/31a9861af02a8e9070b395f05caed7ffce0ef8be)) + ## [2.18.1](https://github.com/googleapis/google-api-php-client/compare/v2.18.0...v2.18.1) (2024-11-24) From a0da60ef45caffbc75d44c08b85fa32c4d41f691 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 27 Dec 2024 10:27:51 -0800 Subject: [PATCH 297/301] docs: update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 93b52cacb..7bac1824f 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ composer installed. Once composer is installed, execute the following command in your project root to install this library: ```sh -composer require google/apiclient:^2.15.0 +composer require google/apiclient ``` If you're facing a timeout error then either increase the timeout for composer by adding the env flag as `COMPOSER_PROCESS_TIMEOUT=600 composer install` or you can put this in the `config` section of the composer schema: From 058b9fb6b7f2f92acfe050ce2b3168bf190af5e4 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 12 Feb 2025 10:57:25 -0800 Subject: [PATCH 298/301] docs: update examples README.md --- examples/README.md | 7 ------- 1 file changed, 7 deletions(-) diff --git a/examples/README.md b/examples/README.md index e62da9cf2..b3f62d4cb 100644 --- a/examples/README.md +++ b/examples/README.md @@ -10,10 +10,3 @@ ``` 1. Point your browser to the host and port you specified, i.e `http://localhost:8000`. - -## G Suite OAuth Samples - -G Suite APIs such as Slides, Sheets, and Drive use 3-legged OAuth authentification. -Samples using OAuth can be found here: - -https://github.com/gsuitedevs/php-samples From c6994051af1568359c97d267d9ef34ccbda31387 Mon Sep 17 00:00:00 2001 From: ilyaplot Date: Tue, 8 Apr 2025 14:58:04 -0700 Subject: [PATCH 299/301] fix: convert Finder lazy iterator to array before deletion (#2663) Co-authored-by: Ilya Plotnikov --- src/Task/Composer.php | 54 ++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/Task/Composer.php b/src/Task/Composer.php index fcad6bd13..fff4de22d 100644 --- a/src/Task/Composer.php +++ b/src/Task/Composer.php @@ -34,35 +34,37 @@ public static function cleanup( ) { $composer = $event->getComposer(); $extra = $composer->getPackage()->getExtra(); - $servicesToKeep = isset($extra['google/apiclient-services']) - ? $extra['google/apiclient-services'] - : []; - if ($servicesToKeep) { - $vendorDir = $composer->getConfig()->get('vendor-dir'); + $servicesToKeep = $extra['google/apiclient-services'] ?? []; + if (empty($servicesToKeep)) { + return; + } + $vendorDir = $composer->getConfig()->get('vendor-dir'); + $serviceDir = sprintf( + '%s/google/apiclient-services/src/Google/Service', + $vendorDir + ); + if (!is_dir($serviceDir)) { + // path for google/apiclient-services >= 0.200.0 $serviceDir = sprintf( - '%s/google/apiclient-services/src/Google/Service', + '%s/google/apiclient-services/src', $vendorDir ); - if (!is_dir($serviceDir)) { - // path for google/apiclient-services >= 0.200.0 - $serviceDir = sprintf( - '%s/google/apiclient-services/src', - $vendorDir - ); - } - self::verifyServicesToKeep($serviceDir, $servicesToKeep); - $finder = self::getServicesToRemove($serviceDir, $servicesToKeep); - $filesystem = $filesystem ?: new Filesystem(); - if (0 !== $count = count($finder)) { - $event->getIO()->write( - sprintf('Removing %s google services', $count) - ); - foreach ($finder as $file) { - $realpath = $file->getRealPath(); - $filesystem->remove($realpath); - $filesystem->remove($realpath . '.php'); - } - } + } + self::verifyServicesToKeep($serviceDir, $servicesToKeep); + $finder = self::getServicesToRemove($serviceDir, $servicesToKeep); + $filesystem = $filesystem ?: new Filesystem(); + $servicesToRemoveCount = $finder->count(); + if (0 === $servicesToRemoveCount) { + return; + } + $event->getIO()->write( + sprintf('Removing %d google services', $servicesToRemoveCount) + ); + $pathsToRemove = iterator_to_array($finder); + foreach ($pathsToRemove as $pathToRemove) { + $realpath = $pathToRemove->getRealPath(); + $filesystem->remove($realpath); + $filesystem->remove($realpath . '.php'); } } From 4eee42d201eff054428a4836ec132944d271f051 Mon Sep 17 00:00:00 2001 From: "release-please[bot]" <55107282+release-please[bot]@users.noreply.github.com> Date: Tue, 8 Apr 2025 21:59:36 +0000 Subject: [PATCH 300/301] chore(main): release 2.18.3 (#2664) --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00f046fb5..9fd015477 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [2.18.3](https://github.com/googleapis/google-api-php-client/compare/v2.18.2...v2.18.3) (2025-04-08) + + +### Bug Fixes + +* Convert Finder lazy iterator to array before deletion ([#2663](https://github.com/googleapis/google-api-php-client/issues/2663)) ([c699405](https://github.com/googleapis/google-api-php-client/commit/c6994051af1568359c97d267d9ef34ccbda31387)) + ## [2.18.2](https://github.com/googleapis/google-api-php-client/compare/v2.18.1...v2.18.2) (2024-12-16) From e009e9c1345b40481d981167e0858bee391d627c Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 7 May 2025 10:49:42 -0700 Subject: [PATCH 301/301] chore: add workflow dispatch trigger to asset-release --- .github/workflows/asset-release.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/asset-release.yml b/.github/workflows/asset-release.yml index bc4b2bf8d..1f553fbb9 100644 --- a/.github/workflows/asset-release.yml +++ b/.github/workflows/asset-release.yml @@ -3,6 +3,7 @@ name: Add Release Assets on: release: types: [published] + workflow_dispatch: jobs: asset: