From c72e04523bd60f460c1db0a951f7f72454c58425 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 1 Dec 2021 12:55:09 -0500 Subject: [PATCH 001/101] 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 002/101] 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 003/101] 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 004/101] 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 005/101] 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 006/101] 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 007/101] 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 008/101] 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 009/101] 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 010/101] 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 011/101] 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 012/101] 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 013/101] 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 014/101] 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 015/101] 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 016/101] 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 017/101] 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 018/101] 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 019/101] 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 020/101] 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 021/101] 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 022/101] 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 023/101] 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 024/101] 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 025/101] 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 026/101] 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 027/101] 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 028/101] 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 029/101] 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 030/101] 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 031/101] 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 032/101] 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 033/101] 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 034/101] 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 035/101] 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 036/101] 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 037/101] 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 038/101] 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 039/101] 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 040/101] 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 041/101] 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 042/101] 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 043/101] 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 044/101] 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 045/101] 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 046/101] 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 047/101] 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 048/101] 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 049/101] 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 050/101] 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 051/101] 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 052/101] 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 053/101] 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 054/101] 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 055/101] 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 056/101] 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 057/101] 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 058/101] 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 059/101] 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 060/101] 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 061/101] 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 062/101] 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 063/101] 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 064/101] 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 065/101] 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 066/101] 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 067/101] 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 068/101] 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 069/101] 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 070/101] 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 071/101] 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 072/101] 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 073/101] 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 074/101] 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 075/101] 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 076/101] 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 077/101] 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 078/101] 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 079/101] 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 080/101] 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 081/101] 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 082/101] 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 083/101] 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 084/101] 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 085/101] 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 086/101] 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 087/101] 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 088/101] 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 089/101] 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 090/101] 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 091/101] 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 092/101] 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 093/101] 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 094/101] 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: From 3507706f6dea90e93866ebecf507ccaedaa0dd0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Mendoza?= Date: Thu, 11 Sep 2025 14:23:28 -0400 Subject: [PATCH 095/101] docs: Update OAUTH documentation to reflect a deprecation of a method (#2683) --- docs/oauth-web.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/oauth-web.md b/docs/oauth-web.md index 904745ed5..18bc5e32b 100644 --- a/docs/oauth-web.md +++ b/docs/oauth-web.md @@ -353,7 +353,7 @@ require_once __DIR__.'/vendor/autoload.php'; session_start(); $client = new Google\Client(); -$client->setAuthConfigFile('client_secrets.json'); +$client->setAuthConfig('client_secrets.json'); $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php'); $client->addScope(Google\Service\Drive::DRIVE_METADATA_READONLY); @@ -421,4 +421,4 @@ $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 +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. From 6530c6ba1b1c3bd4311c354c12948a48b46b15a3 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 11 Sep 2025 13:09:59 -0700 Subject: [PATCH 096/101] chore: drop PHP 8.0 (#2682) --- .github/workflows/asset-release.yml | 2 +- .github/workflows/tests.yml | 4 ++-- composer.json | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/asset-release.yml b/.github/workflows/asset-release.yml index 1f553fbb9..24a459254 100644 --- a/.github/workflows/asset-release.yml +++ b/.github/workflows/asset-release.yml @@ -11,7 +11,7 @@ jobs: strategy: matrix: operating-system: [ ubuntu-latest ] - php: [ "8.0", "8.3" ] + php: [ "8.1", "8.3" ] name: Upload Release Assets steps: diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9f86e493a..ade84792a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -11,10 +11,10 @@ jobs: strategy: fail-fast: false matrix: - php: [ "8.0", "8.1", "8.2", "8.3", "8.4" ] + php: [ "8.1", "8.2", "8.3", "8.4" ] composer-flags: [""] include: - - php: "8.0" + - php: "8.1" composer-flags: "--prefer-lowest " - php: "8.4" composer-flags: "--prefer-lowest " diff --git a/composer.json b/composer.json index abacbe84b..240f9d1c8 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "homepage": "http://developers.google.com/api-client-library/php", "license": "Apache-2.0", "require": { - "php": "^8.0", + "php": "^8.1", "google/auth": "^1.37", "google/apiclient-services": "~0.350", "firebase/php-jwt": "^6.0", From ed70802cc4886ef1f513a2c0b56a8a972db5e7ab Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 29 Sep 2025 09:20:16 -0700 Subject: [PATCH 097/101] fix: ensure credentials can be of type FetchAuthTokenInterface (#2684) --- src/AuthHandler/Guzzle6AuthHandler.php | 18 ++++++++++++------ src/Client.php | 12 +++++++----- tests/Google/ClientTest.php | 13 +++++++++---- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/AuthHandler/Guzzle6AuthHandler.php b/src/AuthHandler/Guzzle6AuthHandler.php index 352cf915c..05fe1b2b9 100644 --- a/src/AuthHandler/Guzzle6AuthHandler.php +++ b/src/AuthHandler/Guzzle6AuthHandler.php @@ -2,8 +2,8 @@ namespace Google\AuthHandler; -use Google\Auth\CredentialsLoader; use Google\Auth\FetchAuthTokenCache; +use Google\Auth\FetchAuthTokenInterface; use Google\Auth\HttpHandler\HttpHandlerFactory; use Google\Auth\Middleware\AuthTokenMiddleware; use Google\Auth\Middleware\ScopedAccessTokenMiddleware; @@ -28,7 +28,7 @@ public function __construct(?CacheItemPoolInterface $cache = null, array $cacheC public function attachCredentials( ClientInterface $http, - CredentialsLoader $credentials, + FetchAuthTokenInterface $credentials, ?callable $tokenCallback = null ) { // use the provided cache @@ -40,13 +40,21 @@ public function attachCredentials( ); } - return $this->attachCredentialsCache($http, $credentials, $tokenCallback); + return $this->attachToHttp($http, $credentials, $tokenCallback); } public function attachCredentialsCache( ClientInterface $http, FetchAuthTokenCache $credentials, ?callable $tokenCallback = null + ) { + return $this->attachToHttp($http, $credentials, $tokenCallback); + } + + private function attachToHttp( + ClientInterface $http, + FetchAuthTokenInterface $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 @@ -63,9 +71,7 @@ public function attachCredentialsCache( $config['handler']->remove('google_auth'); $config['handler']->push($middleware, 'google_auth'); $config['auth'] = 'google_auth'; - $http = new Client($config); - - return $http; + return new Client($config); } public function attachToken(ClientInterface $http, array $token, array $scopes) diff --git a/src/Client.php b/src/Client.php index edfb1f83e..33147925f 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\FetchAuthTokenInterface; use Google\Auth\GetUniverseDomainInterface; use Google\Auth\HttpHandler\HttpHandlerFactory; use Google\Auth\OAuth2; @@ -90,7 +91,7 @@ class Client private $logger; /** - * @var ?CredentialsLoader $credentials + * @var ?FetchAuthTokenInterface $credentials */ private $credentials; @@ -118,10 +119,10 @@ class Client * 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 + * @type string|array|FetchAuthTokenInterface $credentials * Can be a path to JSON credentials or an array representing those * credentials (@see Google\Client::setAuthConfig), or an instance of - * {@see CredentialsLoader}. + * {@see FetchAuthTokenInterface}. * @type string|array $scopes * {@see Google\Client::setScopes} * @type string $quota_project @@ -213,7 +214,7 @@ public function __construct(array $config = []) ], $config); if (!is_null($this->config['credentials'])) { - if ($this->config['credentials'] instanceof CredentialsLoader) { + if ($this->config['credentials'] instanceof FetchAuthTokenInterface) { $this->credentials = $this->config['credentials']; } else { $this->setAuthConfig($this->config['credentials']); @@ -460,7 +461,8 @@ public function authorize(?ClientInterface $http = null) $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 + // 1. Check if an instance of Google\Auth\FetchAuthTokenInterface 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 diff --git a/tests/Google/ClientTest.php b/tests/Google/ClientTest.php index 4e53e34b4..4a336705d 100644 --- a/tests/Google/ClientTest.php +++ b/tests/Google/ClientTest.php @@ -24,7 +24,7 @@ use Google\Service\Drive; use Google\AuthHandler\AuthHandlerFactory; use Google\Auth\FetchAuthTokenCache; -use Google\Auth\CredentialsLoader; +use Google\Auth\FetchAuthTokenInterface; use Google\Auth\GCECache; use Google\Auth\Credentials\GCECredentials; use GuzzleHttp\Client as GuzzleClient; @@ -40,6 +40,8 @@ use InvalidArgumentException; use Exception; use DomainException; +use Google\Auth\GetUniverseDomainInterface; +use Google\Auth\UpdateMetadataInterface; class ClientTest extends BaseTest { @@ -868,10 +870,12 @@ public function testClientOptions() $this->assertEquals('some-quota-project', $credentials->getQuotaProject()); } - public function testCredentialsOptionWithCredentialsLoader() + public function testCredentialsOptionWithFetchAuthTokenInterface() { $request = null; - $credentials = $this->prophesize('Google\Auth\CredentialsLoader'); + $credentials = $this->prophesize(FetchAuthTokenInterface::class) + ->willImplement(GetUniverseDomainInterface::class) + ->willImplement(UpdateMetadataInterface::class); $credentials->getCacheKey() ->willReturn('cache-key'); $credentials->getUniverseDomain() @@ -947,7 +951,8 @@ public function testUniverseDomainMismatch() 'The configured universe domain (example.com) does not match the credential universe domain (foo.com)' ); - $credentials = $this->prophesize(CredentialsLoader::class); + $credentials = $this->prophesize(FetchAuthTokenInterface::class) + ->willImplement(GetUniverseDomainInterface::class); $credentials->getUniverseDomain() ->shouldBeCalledOnce() ->willReturn('foo.com'); From 5b51fdb2cbd2a96088e3dfc6f565bdf6fb0af94b Mon Sep 17 00:00:00 2001 From: "release-please[bot]" <55107282+release-please[bot]@users.noreply.github.com> Date: Mon, 29 Sep 2025 21:23:07 -0700 Subject: [PATCH 098/101] chore(main): release 2.18.4 (#2689) --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fd015477..a0418c111 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [2.18.4](https://github.com/googleapis/google-api-php-client/compare/v2.18.3...v2.18.4) (2025-09-29) + + +### Bug Fixes + +* Ensure credentials can be of type FetchAuthTokenInterface ([#2684](https://github.com/googleapis/google-api-php-client/issues/2684)) ([ed70802](https://github.com/googleapis/google-api-php-client/commit/ed70802cc4886ef1f513a2c0b56a8a972db5e7ab)) + ## [2.18.3](https://github.com/googleapis/google-api-php-client/compare/v2.18.2...v2.18.3) (2025-04-08) From 3003f8b8ea878b18660ba338aafabfc4dc57d71b Mon Sep 17 00:00:00 2001 From: Kasumi Date: Sat, 25 Oct 2025 02:33:42 +0200 Subject: [PATCH 099/101] docs: Updated OAuth Web Doc to be up-to-date (#2685) --- docs/oauth-web.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/oauth-web.md b/docs/oauth-web.md index 18bc5e32b..7d21b5620 100644 --- a/docs/oauth-web.md +++ b/docs/oauth-web.md @@ -258,10 +258,10 @@ After completing the OAuth 2.0 flow, you should be redirected to `http://localho 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: +To exchange an authorization code for an access token, use the `fetchAccessTokenWithAuthCode` method: ```php -$client->authenticate($_GET['code']); +$client->fetchAccessTokenWithAuthCode($_GET['code']); ``` You can retrieve the access token with the `getAccessToken` method: @@ -361,7 +361,7 @@ if (! isset($_GET['code'])) { $auth_url = $client->createAuthUrl(); header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL)); } else { - $client->authenticate($_GET['code']); + $client->fetchAccessTokenWithAuthCode($_GET['code']); $_SESSION['access_token'] = $client->getAccessToken(); $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/'; header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); From 217104df02359a2e3ffbe9ad2d1dc3bacf302378 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 14 Nov 2025 15:27:25 -0700 Subject: [PATCH 100/101] chore: add conventional commit lint (#2693) --- .github/conventional-commit-lint.yaml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .github/conventional-commit-lint.yaml diff --git a/.github/conventional-commit-lint.yaml b/.github/conventional-commit-lint.yaml new file mode 100644 index 000000000..0c96b611f --- /dev/null +++ b/.github/conventional-commit-lint.yaml @@ -0,0 +1,2 @@ +always_check_pr_title: true + From 2ccc7cc6bda38a4db37c9e1fb7b1df796b2073b4 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 19 Nov 2025 14:58:49 -0700 Subject: [PATCH 101/101] chore: remove unused guzzle6 check (#2694) --- src/Http/REST.php | 13 ------------- tests/Google/Http/RESTTest.php | 1 + 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/src/Http/REST.php b/src/Http/REST.php index c3d3270db..ea6eb668b 100644 --- a/src/Http/REST.php +++ b/src/Http/REST.php @@ -89,19 +89,6 @@ public static function doExecute(ClientInterface $client, RequestInterface $requ } $response = $e->getResponse(); - // specific checking for Guzzle 5: convert to PSR7 response - if ( - interface_exists('\GuzzleHttp\Message\ResponseInterface') - && $response instanceof \GuzzleHttp\Message\ResponseInterface - ) { - $response = new Response( - $response->getStatusCode(), - $response->getHeaders() ?: [], - $response->getBody(), - $response->getProtocolVersion(), - $response->getReasonPhrase() - ); - } } return self::decodeHttpResponse($response, $request, $expectedClass); diff --git a/tests/Google/Http/RESTTest.php b/tests/Google/Http/RESTTest.php index ef44ed0a2..7b05b0cb4 100644 --- a/tests/Google/Http/RESTTest.php +++ b/tests/Google/Http/RESTTest.php @@ -31,6 +31,7 @@ class RESTTest extends BaseTest * @var REST $rest */ private $rest; + private Request $request; public function setUp(): void {