-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from shadowhand/feature/redis-adapter
Add Redis storage adapter
- Loading branch information
Showing
4 changed files
with
170 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,8 @@ | |
</whitelist> | ||
</filter> | ||
|
||
<logging> | ||
<log type="coverage-text" target="php://stdout"/> | ||
</logging> | ||
|
||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Opensoft\Rollout\Storage; | ||
|
||
/** | ||
* Storage adapter using Redis | ||
* | ||
* @author Woody Gilk <@shadowhand> | ||
*/ | ||
class RedisStorageAdapter implements StorageInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
const DEFAULT_GROUP = 'rollout_feature'; | ||
|
||
/** | ||
* @var object | ||
*/ | ||
private $redis; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $group = self::DEFAULT_GROUP; | ||
|
||
public function __construct($redis, $group = null) | ||
{ | ||
$this->redis = $redis; | ||
|
||
if ($group) { | ||
$this->group = $group; | ||
} | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function get($key) | ||
{ | ||
$result = $this->redis->hget($this->group, $key); | ||
|
||
if (empty($result)) { | ||
return null; | ||
} | ||
|
||
$result = json_decode($result, true); | ||
|
||
if (JSON_ERROR_NONE !== json_last_error()) { | ||
return null; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function set($key, $value) | ||
{ | ||
$this->redis->hset($this->group, $key, json_encode($value)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace Opensoft\Tests\Storage; | ||
|
||
use Opensoft\Rollout\Storage\RedisStorageAdapter; | ||
|
||
class RedisStorageAdapterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private $redis; | ||
|
||
public function setUp() | ||
{ | ||
$this->redis = $this->getMockBuilder('\Opensoft\Tests\Storage\mockRedis')->getMock(); | ||
} | ||
|
||
public function testGet() | ||
{ | ||
$this->redis->expects($this->once()) | ||
->method('hget') | ||
->with(RedisStorageAdapter::DEFAULT_GROUP, 'key') | ||
->willReturn(json_encode('success')); | ||
|
||
$adapter = new RedisStorageAdapter($this->redis); | ||
|
||
$result = $adapter->get('key'); | ||
$this->assertSame('success', $result); | ||
} | ||
|
||
public function testGetWithCustomGroup() | ||
{ | ||
$this->redis->expects($this->once()) | ||
->method('hget') | ||
->with('rollout_test', 'key') | ||
->willReturn(json_encode('success')); | ||
|
||
$adapter = new RedisStorageAdapter($this->redis, 'rollout_test'); | ||
|
||
$result = $adapter->get('key'); | ||
$this->assertSame('success', $result); | ||
} | ||
|
||
public function testGetNotExistsFailure() | ||
{ | ||
$this->redis->expects($this->once()) | ||
->method('hget') | ||
->with(RedisStorageAdapter::DEFAULT_GROUP, 'key') | ||
->willReturn(''); | ||
|
||
$adapter = new RedisStorageAdapter($this->redis); | ||
|
||
$result = $adapter->get('key'); | ||
$this->assertNull($result); | ||
} | ||
|
||
public function testGetJsonDecodeFailure() | ||
{ | ||
$this->redis->expects($this->once()) | ||
->method('hget') | ||
->with(RedisStorageAdapter::DEFAULT_GROUP, 'key') | ||
->willReturn('not json'); | ||
|
||
$adapter = new RedisStorageAdapter($this->redis); | ||
|
||
$result = $adapter->get('key'); | ||
$this->assertNull($result); | ||
} | ||
|
||
public function testSet() | ||
{ | ||
$this->redis->expects($this->once()) | ||
->method('hset') | ||
->with(RedisStorageAdapter::DEFAULT_GROUP, 'key', json_encode('value')); | ||
|
||
$adapter = new RedisStorageAdapter($this->redis); | ||
|
||
$adapter->set('key', 'value'); | ||
} | ||
|
||
public function testSetWithCustomGroup() | ||
{ | ||
$this->redis->expects($this->once()) | ||
->method('hset') | ||
->with('rollout_test', 'key', json_encode('value')); | ||
|
||
$adapter = new RedisStorageAdapter($this->redis, 'rollout_test'); | ||
|
||
$adapter->set('key', 'value'); | ||
} | ||
} | ||
|
||
interface mockRedis | ||
{ | ||
public function hget($key, $field); | ||
public function hset($key, $field, $value); | ||
} |