-
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 #16 from maxwellhealth/mongo-adapter
Adds MongoDB Storage Adapter
- Loading branch information
Showing
4 changed files
with
161 additions
and
2 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
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,65 @@ | ||
<?php | ||
|
||
namespace Opensoft\Rollout\Storage; | ||
|
||
/** | ||
* Storage adapter using MongoDB | ||
* | ||
* @author James Hrisho <@securingsincity> | ||
*/ | ||
class MongoDBStorageAdapter implements StorageInterface | ||
{ | ||
|
||
/** | ||
* @var object | ||
*/ | ||
private $mongo; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $collection; | ||
|
||
public function __construct($mongo, $collection = "rollout_feature") | ||
{ | ||
$this->mongo = $mongo; | ||
$this->collection = $collection; | ||
} | ||
public function getCollectionName() | ||
{ | ||
return $this->collection; | ||
} | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function get($key) | ||
{ | ||
$collection = $this->getCollectionName(); | ||
$result = $this->mongo->$collection->findOne(['name' => $key]); | ||
|
||
if (!$result) { | ||
return null; | ||
} | ||
|
||
return $result['value']; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function set($key, $value) | ||
{ | ||
$collection = $this->getCollectionName(); | ||
$this->mongo->$collection->update(['name' => $key], ['$set' => ['value' => $value]], ['upsert' => true]); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function remove($key) | ||
{ | ||
$collection = $this->getCollectionName(); | ||
$this->mongo->$collection->remove(['name' => $key]); | ||
} | ||
|
||
} |
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,91 @@ | ||
<?php | ||
|
||
namespace Opensoft\Tests\Storage; | ||
|
||
use Opensoft\Rollout\Storage\MongoDBStorageAdapter; | ||
|
||
class MongoDBStorageAdapterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private $mongo; | ||
|
||
public function setUp() | ||
{ | ||
$this->mongo = new mockMongo(); | ||
$collection = $this->mockCollection(); | ||
$collection->method('findOne')->will($this->returnValue(['name' => 'key', 'value' => true])); | ||
$collection->method('update')->will($this->returnValue(null)); | ||
|
||
$this->mongo->setCollection($collection); | ||
} | ||
|
||
public function testGet() | ||
{ | ||
|
||
$adapter = new MongoDBStorageAdapter($this->mongo); | ||
|
||
$result = $adapter->get('key'); | ||
$this->assertSame(true, $result); | ||
} | ||
|
||
public function testGetNoValue() | ||
{ | ||
|
||
$adapter = new MongoDBStorageAdapter($this->mongo); | ||
$this->mongo->coll->method('findOne')->will($this->returnValue(null)); | ||
$result = $adapter->get('key'); | ||
$this->assertSame(true, $result); | ||
} | ||
|
||
public function testSet() | ||
{ | ||
|
||
$adapter = new MongoDBStorageAdapter($this->mongo); | ||
|
||
$adapter->set('key', 'value'); | ||
|
||
} | ||
|
||
public function testRemove() | ||
{ | ||
|
||
$adapter = new MongoDBStorageAdapter($this->mongo); | ||
|
||
$adapter->remove('key'); | ||
|
||
} | ||
|
||
public function testGetCollectionName() | ||
{ | ||
|
||
$adapter = new MongoDBStorageAdapter($this->mongo, 'feature_test'); | ||
|
||
$result = $adapter->getCollectionName(); | ||
$this->assertSame('feature_test', $result); | ||
|
||
} | ||
|
||
public function mockCollection() | ||
{ | ||
return $this->createMock('MongoCollection', ['find', 'findOne', 'update', 'remove'], [], '', false); | ||
|
||
} | ||
} | ||
|
||
class mockMongo | ||
{ | ||
public $collection; | ||
public function __construct() | ||
{ | ||
|
||
} | ||
|
||
public function setCollection($mongoCollection) | ||
{ | ||
$this->collection = $mongoCollection; | ||
} | ||
|
||
public function __get($name) | ||
{ | ||
return $this->collection; | ||
} | ||
} |