From 2cd71ea19056b44465b7681df9e46bdfc3a2391e Mon Sep 17 00:00:00 2001 From: japatel Date: Mon, 5 Jan 2015 14:10:42 -0600 Subject: [PATCH] Refactored Authorization Cache - Updated Authorization Cache Common Code - Unit Tests --- lib/PayPal/Cache/AuthorizationCache.php | 25 ++++--- .../Test/Cache/AuthorizationCacheTest.php | 68 +++++++++++++++++++ 2 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 tests/PayPal/Test/Cache/AuthorizationCacheTest.php diff --git a/lib/PayPal/Cache/AuthorizationCache.php b/lib/PayPal/Cache/AuthorizationCache.php index 6667ef7..32649a2 100644 --- a/lib/PayPal/Cache/AuthorizationCache.php +++ b/lib/PayPal/Cache/AuthorizationCache.php @@ -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; + } + } diff --git a/tests/PayPal/Test/Cache/AuthorizationCacheTest.php b/tests/PayPal/Test/Cache/AuthorizationCacheTest.php new file mode 100644 index 0000000..b3b4b0c --- /dev/null +++ b/tests/PayPal/Test/Cache/AuthorizationCacheTest.php @@ -0,0 +1,68 @@ + '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); + } + +}