Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredchu committed Aug 3, 2017
1 parent 85e2ae3 commit 73aedc5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 46 deletions.
18 changes: 2 additions & 16 deletions src/JCFirebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,7 @@ public function __construct($firebaseURI, OAuth $auth, $rootPath = '/')
*/
public static function fromJson($firebaseURI, $jsonString, $rootPath = '/')
{
if ($jsonString) {
$serviceAccount = $jsonString->client_email;
$privateKey = $jsonString->private_key;

return new self($firebaseURI, new OAuth($privateKey, $serviceAccount), $rootPath);
} else {
throw new \Exception("can't get data from key file");
}
return new self($firebaseURI, OAuth::fromJson($jsonString), $rootPath);
}

/**
Expand All @@ -82,14 +75,7 @@ public static function fromJson($firebaseURI, $jsonString, $rootPath = '/')
*/
public static function fromKeyFile($firebaseURI, $keyFile, $rootPath = '/')
{
$jsonString = null;
try {
$jsonString = json_decode(file_get_contents($keyFile));
} catch (\Exception $exception) {
$jsonString = json_decode(JCRequest::get($keyFile));
}

return self::fromJson($firebaseURI, $jsonString, $rootPath);
return new self($firebaseURI, OAuth::fromKeyFile($keyFile), $rootPath);
}

public function getPathURI($path = '', $print = '')
Expand Down
29 changes: 28 additions & 1 deletion src/OAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,45 @@ class OAuth
protected $expireTimestamp;
protected $accessToken;

protected $cache;
const CACHE_KEY = 'firebase-oauth';

/**
* OAuth constructor.
*
* @param $key
* @param $iss
* @param $lifeTime
*/
public function __construct($key, $iss, $lifeTime = 3600)
public function __construct($key, $iss, $lifeTime = 3600, $cache = false)
{
$this->key = $key;
$this->iss = $iss;
$this->tokenLifeTime = $lifeTime;
$this->cache = $cache;
}

public static function fromJson($jsonString, $lifeTime = 3600, $cache = false)
{
if ($jsonString) {
$privateKey = $jsonString->private_key;
$serviceAccount = $jsonString->client_email;

return new static($privateKey, $serviceAccount, $lifeTime, $cache);
} else {
throw new \Exception("can't get data from key file");
}
}

public static function fromKeyFile($keyFile, $lifeTime = 3600, $cache = false)
{
try {
$jsonString = json_decode(file_get_contents($keyFile));
} catch (\Exception $exception) {
$jsonString = json_decode(JCRequest::get($keyFile));
}

return static::fromJson($jsonString, $lifeTime, $cache);
}

protected function requestAccessToken()
Expand Down
59 changes: 30 additions & 29 deletions tests/JCFirebaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@

use JCFirebase\Enums\PrintType;
use JCFirebase\JCFirebase;
use JCFirebase\OAuth;
use JCFirebase\Option;

class JCFirebaseTest extends PHPUnit_Framework_TestCase
{
const FIREBASE_URI = 'https://fir-php-test-c7fa2.firebaseio.com/';
const KEY_FILE = '/resource/firebase-php-test-0a49b34e5f4a.json';
const KEY_FILE = __DIR__ . '/../resource/firebase-php-test-0a49b34e5f4a.json';

/**
* @var JCFirebase
Expand All @@ -23,40 +24,40 @@ class JCFirebaseTest extends PHPUnit_Framework_TestCase

public static function setUpBeforeClass()
{
self::$firebase = JCFirebase::fromKeyFile(self::FIREBASE_URI, getcwd() . self::KEY_FILE);
self::$firebase = JCFirebase::fromKeyFile(self::FIREBASE_URI, self::KEY_FILE);
}

public function testGetPathURI()
{
$firebase = self::$firebase;
$fb = self::$firebase;

self::assertEquals(self::FIREBASE_URI . '.json', $firebase->getPathURI());
self::assertEquals(self::FIREBASE_URI . '.json', $firebase->getPathURI('/'));
self::assertEquals(self::FIREBASE_URI . '.json', $fb->getPathURI());
self::assertEquals(self::FIREBASE_URI . '.json', $fb->getPathURI('/'));

self::assertEquals(self::FIREBASE_URI . 'path.json', $firebase->getPathURI('path'));
self::assertEquals(self::FIREBASE_URI . 'path.json', $firebase->getPathURI('/path/'));
self::assertEquals(self::FIREBASE_URI . 'path.json', $firebase->getPathURI('//path//'));
self::assertEquals(self::FIREBASE_URI . 'path.json', $fb->getPathURI('path'));
self::assertEquals(self::FIREBASE_URI . 'path.json', $fb->getPathURI('/path/'));
self::assertEquals(self::FIREBASE_URI . 'path.json', $fb->getPathURI('//path//'));

self::assertEquals(self::FIREBASE_URI . 'path/to/your.json', $firebase->getPathURI('path/to/your'));
self::assertEquals(self::FIREBASE_URI . 'path/to/your.json', $firebase->getPathURI('/path/to/your/'));
self::assertEquals(self::FIREBASE_URI . 'path/to/your.json', $firebase->getPathURI('//path/to/your//'));
self::assertEquals(self::FIREBASE_URI . 'path/to/your.json', $fb->getPathURI('path/to/your'));
self::assertEquals(self::FIREBASE_URI . 'path/to/your.json', $fb->getPathURI('/path/to/your/'));
self::assertEquals(self::FIREBASE_URI . 'path/to/your.json', $fb->getPathURI('//path/to/your//'));

}

public function testGet()
{
$firebase = self::$firebase;
$fb = self::$firebase;

$response = $firebase->get();
$response = $fb->get();
self::assertEquals(200, $response->status());
}

public function testPut()
{
$firebase = self::$firebase;
$fb = self::$firebase;
$subPath = 'put_test';

$response = $firebase->put($subPath, array(
$response = $fb->put($subPath, array(
'data' => self::data()
));

Expand All @@ -75,10 +76,10 @@ private function data()

public function testPost()
{
$firebase = self::$firebase;
$fb = self::$firebase;
$subPath = 'post_test';

$response = $firebase->post($subPath, array(
$response = $fb->post($subPath, array(
'data' => self::data()
));

Expand All @@ -88,14 +89,14 @@ public function testPost()

public function testPatch()
{
$firebase = self::$firebase;
$fb = self::$firebase;
$subPath = 'patch_test';

$firebase->put($subPath, array(
$fb->put($subPath, array(
'data' => self::data()
));

$response = $firebase->patch($subPath, array(
$response = $fb->patch($subPath, array(
'data' => array(
'number' => 2,
'string' => 'hello2'
Expand All @@ -109,41 +110,41 @@ public function testPatch()

public function testDelete()
{
$firebase = self::$firebase;
$fb = self::$firebase;
$subPath = 'delete_test';

$firebase->put($subPath, array(
$fb->put($subPath, array(
'data' => self::data()
));

$subPath = 'delete_test/number';

$response = $firebase->delete($subPath);
$response = $fb->delete($subPath);

self::assertEquals(200, $response->status());
}

public function testGetShallow()
{
$firebase = self::$firebase;
$fb = self::$firebase;
$subPath = 'get_shallow_test';

$firebase->put($subPath, array('data' => self::data()));
$fb->put($subPath, array('data' => self::data()));

$response = $firebase->getShallow($subPath);
$response = $fb->getShallow($subPath);
self::assertEquals(200, $response->status());
self::assertTrue($response->json()->number);
self::assertTrue($response->json()->string);
}

public function testGetPrint()
{
$firebase = self::$firebase;
$fb = self::$firebase;

self::assertContains(" ", $firebase->get(null, array(
self::assertContains(" ", $fb->get(null, array(
Option::OPT_PRINT => PrintType::PRETTY
))->body());

self::assertTrue($firebase->isValid());
self::assertTrue($fb->isValid());
}
}

0 comments on commit 73aedc5

Please sign in to comment.