Skip to content

Commit

Permalink
refactor: renamed adapter property, added storage getter in GoogleCli…
Browse files Browse the repository at this point in the history
…ent and additonal test.
  • Loading branch information
tomshaw committed Apr 18, 2024
1 parent b984877 commit fd6e5c5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
17 changes: 11 additions & 6 deletions src/GoogleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class GoogleClient implements GoogleClientInterface
{
protected StorageAdapterInterface $tokenStorage;
protected StorageAdapterInterface $storageAdapter;

public static $rules = [
'access_token' => 'required|string',
Expand Down Expand Up @@ -73,25 +73,30 @@ public function __invoke(): Client
return $this->client;
}

public function setStorage(StorageAdapterInterface $tokenStorage): self
public function setStorage(StorageAdapterInterface $storageAdapter): self
{
if (! $tokenStorage instanceof StorageAdapterInterface) {
if (! $storageAdapter instanceof StorageAdapterInterface) {
throw new GoogleClientException('Invalid token storage.');
}

$this->tokenStorage = $tokenStorage;
$this->storageAdapter = $storageAdapter;

return $this;
}

public function getStorage(): StorageAdapterInterface
{
return $this->storageAdapter;
}

public function getAccessToken(): ?array
{
return $this->tokenStorage->get();
return $this->storageAdapter->get();
}

public function setAccessToken(array $accessToken): self
{
$this->tokenStorage->set($accessToken);
$this->storageAdapter->set($accessToken);

return $this;
}
Expand Down
24 changes: 17 additions & 7 deletions tests/GoogleApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use TomShaw\GoogleApi\GoogleApi;
use TomShaw\GoogleApi\GoogleClient;
use TomShaw\GoogleApi\Storage\SessionTokenStorage;
use TomShaw\GoogleApi\Storage\StorageAdapterInterface;

test('instance check', function () {
$this->assertTrue($this instanceof \PHPUnit\Framework\TestCase);
Expand All @@ -24,8 +25,7 @@

Config::set('google-api', require realpath(__DIR__.DIRECTORY_SEPARATOR.'Mock'.DIRECTORY_SEPARATOR.'config.php'));

$this->client = new Client();
$this->googleClient = new GoogleClient($this->client);
$this->client = new GoogleClient(new Client());
});

afterEach(function () {
Expand All @@ -35,27 +35,37 @@
it('returns null when no access token is set', function () {
Session::forget(SessionTokenStorage::SESSION_KEY);

$result = $this->googleClient->getAccessToken();
$result = $this->client->getAccessToken();

expect($result)->toBeNull();
});

it('returns session token when token is set in session', function () {
Session::put(SessionTokenStorage::SESSION_KEY, ['access_token' => 'test_token']);

$result = $this->googleClient->getAccessToken();
$result = $this->client->getAccessToken();

expect($result)->toBeArray()->and($result['access_token'])->toBe('test_token');
});

it('sets access token in session', function () {
$this->googleClient->setAccessToken(['access_token' => 'test_token']);
$this->client->setAccessToken(['access_token' => 'test_token']);

$token = Session::get(SessionTokenStorage::SESSION_KEY);

expect($token)->toBeArray()->and($token['access_token'])->toBe('test_token');
});

it('sets and gets the storage adapter correctly', function () {
$mockStorageAdapter = \Mockery::mock(StorageAdapterInterface::class);

$this->client->setStorage($mockStorageAdapter);

$result = $this->client->getStorage();

expect($result)->toBe($mockStorageAdapter);
});

it('returns GoogleMail instance', function () {
Session::put(SessionTokenStorage::SESSION_KEY, [
'access_token' => 'test_token',
Expand All @@ -66,7 +76,7 @@
'created' => time(),
]);

$result = GoogleApi::gmail($this->googleClient);
$result = GoogleApi::gmail($this->client);

expect($result)->toBeInstanceOf(GoogleMail::class);
});
Expand All @@ -81,7 +91,7 @@
'created' => time(),
]);

$result = GoogleApi::calendar($this->googleClient);
$result = GoogleApi::calendar($this->client);

expect($result)->toBeInstanceOf(GoogleCalendar::class);
});

0 comments on commit fd6e5c5

Please sign in to comment.