Skip to content

Commit

Permalink
Merge pull request #16 from maxwellhealth/mongo-adapter
Browse files Browse the repository at this point in the history
Adds MongoDB Storage Adapter
  • Loading branch information
richardfullmer authored Nov 8, 2016
2 parents 0c55cab + 7c8b26e commit 41083bc
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 2 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: php

services:
- mongodb
php:
- 5.6
- 7.0
Expand All @@ -11,5 +12,5 @@ matrix:
allow_failures:
- php: nightly
- php: hhvm

before_script: echo "extension = mongo.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
install: composer install -n
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ There are a number of different storage implementations for where the configurat
* DoctrineCacheStorageAdapter - requires [doctrine/cache][doctrine-cache]
* PDOStorageAdapter - persistent using [PDO][pdo]
* RedisStorageAdapter - persistent using [Redis][redis]
* MongoDBStorageAdapter - persistent using [Mongo][mongo]

[doctrine-cache]: https://packagist.org/packages/doctrine/cache
[pdo]: http://php.net/pdo
[redis]: http://redis.io
[mongo]: http://mongodb.org

All storage adapters must implement `Opensoft\Rollout\Storage\StorageInterface`.

Expand Down
65 changes: 65 additions & 0 deletions src/Storage/MongoDBStorageAdapter.php
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]);
}

}
91 changes: 91 additions & 0 deletions tests/Storage/MongoDBStorageAdapterTest.php
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;
}
}

0 comments on commit 41083bc

Please sign in to comment.