Skip to content
This repository has been archived by the owner on May 23, 2020. It is now read-only.

Commit

Permalink
Improve file token storage and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rdrenth committed Jul 7, 2018
1 parent f1d9bb3 commit c6bbb20
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 8 deletions.
7 changes: 4 additions & 3 deletions composer.json
Expand Up @@ -19,8 +19,9 @@
"guzzlehttp/guzzle": "^6.3"
},
"require-dev": {
"phpunit/phpunit": "^6.3",
"illuminate/support": "5.6.*"
"illuminate/support": "5.6.*",
"mikey179/vfsStream": "^1.6",
"phpunit/phpunit": "^7.0"
},
"config": {
"preferred-install": "dist",
Expand All @@ -40,7 +41,7 @@
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
"AdrenthTests\\Raindrop\\": "tests/"
}
}
}
18 changes: 18 additions & 0 deletions phpunit.xml.dist
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php">

<testsuites>
<testsuite name="Hydro Raindrop SDK tests">
<directory>src/</directory>
<directory>tests/</directory>
</testsuite>
</testsuites>

</phpunit>
9 changes: 7 additions & 2 deletions src/ApiAccessToken.php
Expand Up @@ -11,6 +11,8 @@
*/
class ApiAccessToken
{
public const EXPIRE_OFFSET = 60;

/**
* @var string
*/
Expand All @@ -37,8 +39,11 @@ public function __construct(string $token, int $expiresIn)
* @param int $expireOffset
* @return ApiAccessToken
*/
public static function create(string $token, int $expiresIn, int $expireOffset = 60): ApiAccessToken
{
public static function create(
string $token,
int $expiresIn,
int $expireOffset = self::EXPIRE_OFFSET
): ApiAccessToken {
return new self(
$token,
$expiresIn - $expireOffset
Expand Down
12 changes: 9 additions & 3 deletions src/TokenStorage/FileTokenStorage.php
Expand Up @@ -31,11 +31,15 @@ public function __construct(string $filename)
*/
public function getAccessToken(): ?ApiAccessToken
{
if (!is_readable($this->filename)) {
return null;
}

$data = file_get_contents($this->filename);

if (!empty($data)) {
if (!empty($data) && substr_count($data, '|') === 1) {
$data = explode('|', $data);
return new ApiAccessToken($data[0] ?? '', (int) ($data[1] ?? 0));
return ApiAccessToken::create($data[0] ?? '', (int) ($data[1] ?? 0));
}

return null;
Expand All @@ -54,6 +58,8 @@ public function setAccessToken(ApiAccessToken $token): void
*/
public function unsetAccessToken(): void
{
unlink($this->filename);
if (file_exists($this->filename)) {
unlink($this->filename);
}
}
}
118 changes: 118 additions & 0 deletions tests/TokenStorage/FileTokenStorageTest.php
@@ -0,0 +1,118 @@
<?php

declare(strict_types=1);

namespace AdrenthTests\Raindrop\TokenStorage;

use Adrenth\Raindrop\ApiAccessToken;
use Adrenth\Raindrop\TokenStorage\FileTokenStorage;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamFile;
use PHPUnit\Framework\TestCase;

/**
* Class FileTokenStorageTest
*
* @package AdrenthTests\Raindrop\TokenStorage
*/
final class FileTokenStorageTest extends TestCase
{
/**
* @var vfsStreamDirectory
*/
private $root;

/**
* {@inheritdoc}
*/
protected function setUp(): void
{
$this->root = vfsStream::setup();
}

/**
* @test
*/
public function itShouldReturnNullWhenFileDoesNotExist(): void
{
$storage = new FileTokenStorage(vfsStream::url('root/token.txt'));

self::assertNull($storage->getAccessToken());
}

/**
* @test
*/
public function itShouldReturnNullWhenFileIsEmpty(): void
{
$this->root->addChild((new vfsStreamFile('token.txt'))->withContent(''));

$storage = new FileTokenStorage(vfsStream::url('root/token.txt'));

self::assertNull($storage->getAccessToken());
}

/**
* @test
*/
public function itShouldReturnNullWhenFileIsInvalid(): void
{
$this->root->addChild((new vfsStreamFile('token.txt'))->withContent('invalid_contents'));

$storage = new FileTokenStorage(vfsStream::url('root/token.txt'));

self::assertNull($storage->getAccessToken());
}

/**
* @test
*/
public function itShouldReturnAnAccessToken(): void
{
$token = 'access_token';
$expiresIn = 3600;

$this->root->addChild((new vfsStreamFile('token.txt'))->withContent("$token|$expiresIn"));

$storage = new FileTokenStorage(vfsStream::url('root/token.txt'));
$accessToken = $storage->getAccessToken();

self::assertNotNull($accessToken);
self::assertEquals($token, $accessToken->getToken());
self::assertEquals($expiresIn - ApiAccessToken::EXPIRE_OFFSET, $accessToken->getExpiresIn());
}

/**
* @test
*/
public function itShouldCreateAnFileWhenSettingTheAccessToken(): void
{
$token = 'access_token';
$expiresIn = 3600;

$storage = new FileTokenStorage(vfsStream::url('root/token.txt'));
$storage->setAccessToken(ApiAccessToken::create($token, $expiresIn));

self::assertTrue($this->root->hasChild('root/token.txt'));

/** @var vfsStreamFile $child */
$child = $this->root->getChild('root/token.txt');
self::assertEquals($token . '|' . ($expiresIn - ApiAccessToken::EXPIRE_OFFSET), $child->getContent());
}

/**
* @test
*/
public function itShouldDeleteTheFileWhenUnsettingTheAccessToken(): void
{
$storage = new FileTokenStorage(vfsStream::url('root/token.txt'));
$storage->setAccessToken(ApiAccessToken::create('token', 3600));

self::assertTrue($this->root->hasChild('root/token.txt'));

$storage->unsetAccessToken();

self::assertFalse($this->root->hasChild('root/token.txt'));
}
}

0 comments on commit c6bbb20

Please sign in to comment.