Refactored Authorization Cache

- Updated Authorization Cache Common Code
- Unit Tests
This commit is contained in:
japatel
2015-01-05 14:10:42 -06:00
parent 8f312a108a
commit 2cd71ea190
2 changed files with 85 additions and 8 deletions

View File

@@ -84,12 +84,8 @@ abstract class AuthorizationCache
*/
public static function isEnabled($config)
{
$config = ($config && is_array($config)) ? $config : PayPalConfigManager::getInstance()->getConfigHashmap();
if (array_key_exists("cache.enabled", $config)) {
$value = $config['cache.enabled'];
return (trim($value) == true || trim($value) == 'true') ? true : false;
}
return false;
$value = self::getConfigValue('cache.enabled', $config);
return empty($value) ? false : ((trim($value) == true || trim($value) == 'true') ? true : false);
}
/**
@@ -100,10 +96,23 @@ abstract class AuthorizationCache
*/
public static function cachePath($config)
{
$config = ($config && is_array($config)) ? $config : PPConfigManager::getInstance()->getConfigHashmap();
$cachePath = (array_key_exists("cache.FileName", $config)) ? trim($config['cache.FileName']) : null;
$cachePath = self::getConfigValue('cache.FileName', $config);
return empty($cachePath) ? __DIR__ . self::$CACHE_PATH : $cachePath;
}
/**
* Returns the Value of the key if found in given config, or from PayPal Config Manager
* Returns null if not found
*
* @param $key
* @param $config
* @return null|string
*/
private static function getConfigValue($key, $config)
{
$config = ($config && is_array($config)) ? $config : PayPalConfigManager::getInstance()->getConfigHashmap();
return (array_key_exists($key, $config)) ? trim($config[$key]) : null;
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace PayPal\Test\Cache;
use PayPal\Cache\AuthorizationCache;
/**
* Test class for AuthorizationCacheTest.
*
*/
class AuthorizationCacheTest extends \PHPUnit_Framework_TestCase
{
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
public static function EnabledProvider()
{
return array(
array(array('cache.enabled' => 'true'), true),
array(array('cache.enabled' => true), true),
array(array(), false),
array(null, false)
);
}
public static function CachePathProvider()
{
return array(
array(array('cache.FileName' => 'temp.cache'), 'temp.cache'),
array(array(), 'auth.cache'),
array(null, 'auth.cache')
);
}
/**
*
* @dataProvider EnabledProvider
*/
public function testIsEnabled($config, $expected)
{
$result = AuthorizationCache::isEnabled($config);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider CachePathProvider
*/
public function testCachePath($config, $expected)
{
$result = AuthorizationCache::cachePath($config);
$this->assertContains($expected, $result);
}
}