Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add interfaces to be possible the mock of the client, batch and service #2599

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
* The Google API Client
* https://github.com/google/google-api-php-client
*/
class Client
class Client implements GoogleClientInterface
{
const LIBVER = "2.12.6";
const USER_AGENT_SUFFIX = "google-api-php-client/";
Expand All @@ -65,7 +65,7 @@ class Client
private $auth;

/**
* @var ClientInterface $http
* @var GoogleClientInterface $http
*/
private $http;

Expand Down Expand Up @@ -312,7 +312,7 @@ public function refreshTokenWithAssertion()

/**
* Fetches a fresh access token with a given assertion token.
* @param ClientInterface $authHttp optional.
* @param GoogleClientInterface $authHttp optional.
* @return array access token
*/
public function fetchAccessTokenWithAssertion(ClientInterface $authHttp = null)
Expand Down Expand Up @@ -444,9 +444,8 @@ public function createAuthUrl($scope = null, array $queryParams = [])
/**
* 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
* @param GoogleClientInterface $http the http client object.
* @return GoogleClientInterface the http client object
*/
public function authorize(ClientInterface $http = null)
{
Expand Down Expand Up @@ -1193,15 +1192,15 @@ protected function createDefaultCache()

/**
* Set the Http Client object
* @param ClientInterface $http
* @param GoogleClientInterface $http
*/
public function setHttpClient(ClientInterface $http)
{
$this->http = $http;
}

/**
* @return ClientInterface
* @return GoogleClientInterface
*/
public function getHttpClient()
{
Expand Down
75 changes: 75 additions & 0 deletions src/GoogleClientInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
declare(strict_types=1);

namespace Google;

use GuzzleHttp\ClientInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Log\LoggerInterface;

interface GoogleClientInterface
{
public function __construct(array $config = []);
public function getLibraryVersion();
public function authenticate($code);
public function fetchAccessTokenWithAuthCode($code, $codeVerifier = null);
public function refreshTokenWithAssertion();
public function fetchAccessTokenWithAssertion(ClientInterface $authHttp = null);
public function refreshToken($refreshToken);
public function fetchAccessTokenWithRefreshToken($refreshToken = null);
public function createAuthUrl($scope = null, array $queryParams = []);
public function authorize(ClientInterface $http = null);
public function useApplicationDefaultCredentials($useAppCreds = true);
public function isUsingApplicationDefaultCredentials();
public function setAccessToken($token);
public function getAccessToken();
public function getRefreshToken();
public function isAccessTokenExpired();
public function getAuth();
public function setAuth($auth);
public function setClientId($clientId);
public function getClientId();
public function setClientSecret($clientSecret);
public function getClientSecret();
public function setRedirectUri($redirectUri);
public function getRedirectUri();
public function setState($state);
public function setAccessType($accessType);
public function setApprovalPrompt($approvalPrompt);
public function setLoginHint($loginHint);
public function setApplicationName($applicationName);
public function setRequestVisibleActions($requestVisibleActions);
public function setDeveloperKey($developerKey);
public function setHostedDomain($hd);
public function setPrompt($prompt);
public function setOpenidRealm($realm);
public function setIncludeGrantedScopes($include);
public function setTokenCallback(callable $tokenCallback);
public function revokeToken($token = null);
public function verifyIdToken($idToken = null);
public function setScopes($scope_or_scopes);
public function addScope($scope_or_scopes);
public function getScopes();
public function prepareScopes();
public function execute(RequestInterface $request, $expectedClass = null);
public function setUseBatch($useBatch);
public function isAppEngine();
public function setConfig($name, $value);
public function getConfig($name, $default = null);
public function setAuthConfigFile($file);
public function setAuthConfig($config);
public function setSubject($subject);
public function setDefer($defer);
public function shouldDefer();
public function getOAuth2Service();
public function setCache(CacheItemPoolInterface $cache);
public function getCache();
public function setCacheConfig(array $cacheConfig);
public function setLogger(LoggerInterface $logger);
public function getLogger();
public function setHttpClient(ClientInterface $http);
public function getHttpClient();
public function setApiFormatV2($value);
public function getUniverseDomain();
}
8 changes: 4 additions & 4 deletions src/Http/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace Google\Http;

use Google\Client;
use Google\GoogleClientInterface;
use Google\Service\Exception as GoogleServiceException;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Request;
Expand All @@ -32,7 +32,7 @@
* requests. To start a new batch, be sure to create a new instance of this
* class.
*/
class Batch
class Batch implements BatchInterface
{
const BATCH_PATH = 'batch';

Expand All @@ -47,15 +47,15 @@ class Batch
/** @var array service requests to be executed. */
private $requests = [];

/** @var Client */
/** @var GoogleClientInterface */
private $client;

private $rootUrl;

private $batchPath;

public function __construct(
Client $client,
GoogleClientInterface $client,
$boundary = false,
$rootUrl = null,
$batchPath = null
Expand Down
14 changes: 14 additions & 0 deletions src/Http/BatchInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);

namespace Google\Http;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

interface BatchInterface
{
public function add(RequestInterface $request, $key = false);
public function execute();
public function parseResponse(ResponseInterface $response, $classes = []);
}
11 changes: 6 additions & 5 deletions src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
namespace Google;

use Google\Http\Batch;
use Google\Http\BatchInterface;
use TypeError;

class Service
class Service implements ServiceInterface
{
public $batchPath;
/**
Expand All @@ -37,7 +38,7 @@ class Service

public function __construct($clientOrConfig = [])
{
if ($clientOrConfig instanceof Client) {
if ($clientOrConfig instanceof GoogleClientInterface) {
$this->client = $clientOrConfig;
} elseif (is_array($clientOrConfig)) {
$this->client = new Client($clientOrConfig ?: []);
Expand All @@ -51,8 +52,8 @@ public function __construct($clientOrConfig = [])
}

/**
* Return the associated Google\Client class.
* @return \Google\Client
* Return the associated Google\GoogleClientInterface class.
* @return \Google\GoogleClientInterface
*/
public function getClient()
{
Expand All @@ -62,7 +63,7 @@ public function getClient()
/**
* Create a new HTTP Batch handler for this service
*
* @return Batch
* @return BatchInterface
*/
public function createBatch()
{
Expand Down
10 changes: 10 additions & 0 deletions src/ServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace Google;

interface ServiceInterface
{
public function getClient();
public function createBatch();
}