Skip to content

Commit

Permalink
feat: add support for Guzzle 7 (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez authored Jul 1, 2020
1 parent a996e46 commit a229c26
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 94 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ jobs:
command: composer update --prefer-lowest
- name: Run Script
run: vendor/bin/phpunit
guzzle6:
runs-on: ubuntu-latest
strategy:
matrix:
operating-system: [ ubuntu-latest ]
php: [ "5.6", "7.2" ]
name: PHP ${{ matrix.php }} Unit Test Guzzle 6
steps:
- uses: actions/checkout@v2
- name: Setup PHP
uses: nanasess/[email protected]
with:
php-version: ${{ matrix.php }}
- name: Install Dependencies
uses: nick-invision/retry@v1
with:
timeout_minutes: 10
max_attempts: 3
command: composer require guzzlehttp/guzzle:^6 && composer update
- name: Run Script
run: vendor/bin/phpunit
# use dockerfiles for oooooolllllldddd versions of php, setup-php times out for those.
test_php55:
name: "PHP 5.5 Unit Test"
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"require": {
"php": ">=5.4",
"firebase/php-jwt": "~2.0|~3.0|~4.0|~5.0",
"guzzlehttp/guzzle": "~5.3.1|~6.0",
"guzzlehttp/guzzle": "^5.3.1|^6.2.1|^7.0",
"guzzlehttp/psr7": "^1.2",
"psr/http-message": "^1.0",
"psr/cache": "^1.0"
Expand Down
70 changes: 42 additions & 28 deletions src/CredentialsLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Google\Auth\Credentials\InsecureCredentials;
use Google\Auth\Credentials\ServiceAccountCredentials;
use Google\Auth\Credentials\UserRefreshCredentials;
use GuzzleHttp\ClientInterface;

/**
* CredentialsLoader contains the behaviour used to locate and find default
Expand Down Expand Up @@ -54,6 +55,24 @@ private static function isOnWindows()
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
}

/**
* Returns the currently available major Guzzle version.
*
* @return int
*/
private static function getGuzzleMajorVersion()
{
if (defined('GuzzleHttp\ClientInterface::MAJOR_VERSION')) {
return ClientInterface::MAJOR_VERSION;
}

if (defined('GuzzleHttp\ClientInterface::VERSION')) {
return (int) substr(ClientInterface::VERSION, 0, 1);
}

throw new \Exception('Version not supported');
}

/**
* Load a JSON key from the path specified in the environment.
*
Expand Down Expand Up @@ -145,35 +164,30 @@ public static function makeHttpClient(
callable $httpHandler = null,
callable $tokenCallback = null
) {
$version = \GuzzleHttp\ClientInterface::VERSION;

switch ($version[0]) {
case '5':
$client = new \GuzzleHttp\Client($httpClientOptions);
$client->setDefaultOption('auth', 'google_auth');
$subscriber = new Subscriber\AuthTokenSubscriber(
$fetcher,
$httpHandler,
$tokenCallback
);
$client->getEmitter()->attach($subscriber);
return $client;
case '6':
$middleware = new Middleware\AuthTokenMiddleware(
$fetcher,
$httpHandler,
$tokenCallback
);
$stack = \GuzzleHttp\HandlerStack::create();
$stack->push($middleware);

return new \GuzzleHttp\Client([
'handler' => $stack,
'auth' => 'google_auth',
] + $httpClientOptions);
default:
throw new \Exception('Version not supported');
if (self::getGuzzleMajorVersion() === 5) {
$client = new \GuzzleHttp\Client($httpClientOptions);
$client->setDefaultOption('auth', 'google_auth');
$subscriber = new Subscriber\AuthTokenSubscriber(
$fetcher,
$httpHandler,
$tokenCallback
);
$client->getEmitter()->attach($subscriber);
return $client;
}

$middleware = new Middleware\AuthTokenMiddleware(
$fetcher,
$httpHandler,
$tokenCallback
);
$stack = \GuzzleHttp\HandlerStack::create();
$stack->push($middleware);

return new \GuzzleHttp\Client([
'handler' => $stack,
'auth' => 'google_auth',
] + $httpClientOptions);
}

/**
Expand Down
16 changes: 15 additions & 1 deletion src/HttpHandler/Guzzle6HttpHandler.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
<?php

/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Auth\HttpHandler;

use GuzzleHttp\ClientInterface;
Expand Down
21 changes: 21 additions & 0 deletions src/HttpHandler/Guzzle7HttpHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Auth\HttpHandler;

class Guzzle7HttpHandler extends Guzzle6HttpHandler
{
}
18 changes: 13 additions & 5 deletions src/HttpHandler/HttpHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,27 @@ class HttpHandlerFactory
* Builds out a default http handler for the installed version of guzzle.
*
* @param ClientInterface $client
* @return Guzzle5HttpHandler|Guzzle6HttpHandler
* @return Guzzle5HttpHandler|Guzzle6HttpHandler|Guzzle7HttpHandler
* @throws \Exception
*/
public static function build(ClientInterface $client = null)
{
$version = ClientInterface::VERSION;
$client = $client ?: new Client();

switch ($version[0]) {
case '5':
$version = null;
if (defined('GuzzleHttp\ClientInterface::MAJOR_VERSION')) {
$version = ClientInterface::MAJOR_VERSION;
} elseif (defined('GuzzleHttp\ClientInterface::VERSION')) {
$version = (int) substr(ClientInterface::VERSION, 0, 1);
}

switch ($version) {
case 5:
return new Guzzle5HttpHandler($client);
case '6':
case 6:
return new Guzzle6HttpHandler($client);
case 7:
return new Guzzle7HttpHandler($client);
default:
throw new \Exception('Version not supported');
}
Expand Down
39 changes: 32 additions & 7 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,47 @@

abstract class BaseTest extends TestCase
{
public function onlyGuzzle6()
protected function onlyGuzzle5()
{
$version = ClientInterface::VERSION;
if ('6' !== $version[0]) {
if ($this->getGuzzleMajorVersion() !== 5) {
$this->markTestSkipped('Guzzle 5 only');
}
}

protected function onlyGuzzle6()
{
if ($this->getGuzzleMajorVersion() !== 6) {
$this->markTestSkipped('Guzzle 6 only');
}
}

public function onlyGuzzle5()
protected function onlyGuzzle6And7()
{
$version = ClientInterface::VERSION;
if ('5' !== $version[0]) {
$this->markTestSkipped('Guzzle 5 only');
if (!in_array($this->getGuzzleMajorVersion(), [6, 7])) {
$this->markTestSkipped('Guzzle 6 and 7 only');
}
}

protected function onlyGuzzle7()
{
if ($this->getGuzzleMajorVersion() !== 7) {
$this->markTestSkipped('Guzzle 7 only');
}
}

protected function getGuzzleMajorVersion()
{
if (defined('GuzzleHttp\ClientInterface::MAJOR_VERSION')) {
return ClientInterface::MAJOR_VERSION;
}

if (defined('GuzzleHttp\ClientInterface::VERSION')) {
return (int) substr(ClientInterface::VERSION, 0, 1);
}

$this->fail('Unable to determine the currently used Guzzle Version');
}

/**
* @see Google\Auth\$this->getValidKeyName
*/
Expand Down
30 changes: 7 additions & 23 deletions tests/Credentials/GCECredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@

use Google\Auth\Credentials\GCECredentials;
use Google\Auth\HttpHandler\HttpClientCache;
use GuzzleHttp\ClientInterface;
use Google\Auth\Tests\BaseTest;
use GuzzleHttp\Psr7;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;

/**
* @group credentials
* @group credentials-gce
*/
class GCECredentialsTest extends TestCase
class GCECredentialsTest extends BaseTest
{
public function testOnGceMetadataFlavorHeader()
{
Expand Down Expand Up @@ -182,10 +181,7 @@ public function testSettingBothScopeAndTargetAudienceThrowsException()
*/
public function testFetchAuthTokenCustomScope($scope, $expected)
{
$guzzleVersion = ClientInterface::VERSION;
if ($guzzleVersion[0] === '5') {
$this->markTestSkipped('Only compatible with guzzle 6+');
}
$this->onlyGuzzle6And7();

$uri = null;
$client = $this->prophesize('GuzzleHttp\ClientInterface');
Expand Down Expand Up @@ -258,10 +254,7 @@ public function testGetClientNameShouldBeEmptyIfNotOnGCE()

public function testSignBlob()
{
$guzzleVersion = ClientInterface::VERSION;
if ($guzzleVersion[0] === '5') {
$this->markTestSkipped('Only compatible with guzzle 6+');
}
$this->onlyGuzzle6And7();

$expectedEmail = '[email protected]';
$expectedAccessToken = 'token';
Expand Down Expand Up @@ -294,10 +287,7 @@ public function testSignBlob()

public function testSignBlobWithLastReceivedAccessToken()
{
$guzzleVersion = ClientInterface::VERSION;
if ($guzzleVersion[0] === '5') {
$this->markTestSkipped('Only compatible with guzzle 6+');
}
$this->onlyGuzzle6And7();

$expectedEmail = '[email protected]';
$expectedAccessToken = 'token';
Expand Down Expand Up @@ -340,10 +330,7 @@ public function testSignBlobWithLastReceivedAccessToken()

public function testGetProjectId()
{
$guzzleVersion = ClientInterface::VERSION;
if ($guzzleVersion[0] === '5') {
$this->markTestSkipped('Only compatible with guzzle 6+');
}
$this->onlyGuzzle6And7();

$expected = 'foobar';

Expand All @@ -366,10 +353,7 @@ public function testGetProjectId()

public function testGetProjectIdShouldBeEmptyIfNotOnGCE()
{
$guzzleVersion = ClientInterface::VERSION;
if ($guzzleVersion[0] === '5') {
$this->markTestSkipped('Only compatible with guzzle 6+');
}
$this->onlyGuzzle6And7();

// simulate retry attempts by returning multiple 500s
$client = $this->prophesize('GuzzleHttp\ClientInterface');
Expand Down
19 changes: 13 additions & 6 deletions tests/FetchAuthTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,21 @@ class_implements($fetcherClass)
$this->assertEquals('xyz', $accessToken);
};

$client = CredentialsLoader::makeHttpClient(
$mockFetcher->reveal(),
[
if ($this->getGuzzleMajorVersion() === 5) {
$clientOptions = [
'base_url' => 'https://www.googleapis.com/books/v1/',
'defaults' => ['exceptions' => false],
];
} else {
$clientOptions = [
'base_uri' => 'https://www.googleapis.com/books/v1/',
'exceptions' => false,
'defaults' => ['exceptions' => false]
],
'http_errors' => false,
];
}

$client = CredentialsLoader::makeHttpClient(
$mockFetcher->reveal(),
$clientOptions,
$httpHandler,
$tokenCallback
);
Expand Down
Loading

0 comments on commit a229c26

Please sign in to comment.