Skip to content

Commit 41083bc

Browse files
Merge pull request #16 from maxwellhealth/mongo-adapter
Adds MongoDB Storage Adapter
2 parents 0c55cab + 7c8b26e commit 41083bc

File tree

4 files changed

+161
-2
lines changed

4 files changed

+161
-2
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
language: php
2-
2+
services:
3+
- mongodb
34
php:
45
- 5.6
56
- 7.0
@@ -11,5 +12,5 @@ matrix:
1112
allow_failures:
1213
- php: nightly
1314
- php: hhvm
14-
15+
before_script: echo "extension = mongo.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
1516
install: composer install -n

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ There are a number of different storage implementations for where the configurat
4343
* DoctrineCacheStorageAdapter - requires [doctrine/cache][doctrine-cache]
4444
* PDOStorageAdapter - persistent using [PDO][pdo]
4545
* RedisStorageAdapter - persistent using [Redis][redis]
46+
* MongoDBStorageAdapter - persistent using [Mongo][mongo]
4647

4748
[doctrine-cache]: https://packagist.org/packages/doctrine/cache
4849
[pdo]: http://php.net/pdo
4950
[redis]: http://redis.io
51+
[mongo]: http://mongodb.org
5052

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Opensoft\Rollout\Storage;
4+
5+
/**
6+
* Storage adapter using MongoDB
7+
*
8+
* @author James Hrisho <@securingsincity>
9+
*/
10+
class MongoDBStorageAdapter implements StorageInterface
11+
{
12+
13+
/**
14+
* @var object
15+
*/
16+
private $mongo;
17+
18+
/**
19+
* @var string
20+
*/
21+
private $collection;
22+
23+
public function __construct($mongo, $collection = "rollout_feature")
24+
{
25+
$this->mongo = $mongo;
26+
$this->collection = $collection;
27+
}
28+
public function getCollectionName()
29+
{
30+
return $this->collection;
31+
}
32+
/**
33+
* @inheritdoc
34+
*/
35+
public function get($key)
36+
{
37+
$collection = $this->getCollectionName();
38+
$result = $this->mongo->$collection->findOne(['name' => $key]);
39+
40+
if (!$result) {
41+
return null;
42+
}
43+
44+
return $result['value'];
45+
}
46+
47+
/**
48+
* @inheritdoc
49+
*/
50+
public function set($key, $value)
51+
{
52+
$collection = $this->getCollectionName();
53+
$this->mongo->$collection->update(['name' => $key], ['$set' => ['value' => $value]], ['upsert' => true]);
54+
}
55+
56+
/**
57+
* @inheritdoc
58+
*/
59+
public function remove($key)
60+
{
61+
$collection = $this->getCollectionName();
62+
$this->mongo->$collection->remove(['name' => $key]);
63+
}
64+
65+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Opensoft\Tests\Storage;
4+
5+
use Opensoft\Rollout\Storage\MongoDBStorageAdapter;
6+
7+
class MongoDBStorageAdapterTest extends \PHPUnit_Framework_TestCase
8+
{
9+
private $mongo;
10+
11+
public function setUp()
12+
{
13+
$this->mongo = new mockMongo();
14+
$collection = $this->mockCollection();
15+
$collection->method('findOne')->will($this->returnValue(['name' => 'key', 'value' => true]));
16+
$collection->method('update')->will($this->returnValue(null));
17+
18+
$this->mongo->setCollection($collection);
19+
}
20+
21+
public function testGet()
22+
{
23+
24+
$adapter = new MongoDBStorageAdapter($this->mongo);
25+
26+
$result = $adapter->get('key');
27+
$this->assertSame(true, $result);
28+
}
29+
30+
public function testGetNoValue()
31+
{
32+
33+
$adapter = new MongoDBStorageAdapter($this->mongo);
34+
$this->mongo->coll->method('findOne')->will($this->returnValue(null));
35+
$result = $adapter->get('key');
36+
$this->assertSame(true, $result);
37+
}
38+
39+
public function testSet()
40+
{
41+
42+
$adapter = new MongoDBStorageAdapter($this->mongo);
43+
44+
$adapter->set('key', 'value');
45+
46+
}
47+
48+
public function testRemove()
49+
{
50+
51+
$adapter = new MongoDBStorageAdapter($this->mongo);
52+
53+
$adapter->remove('key');
54+
55+
}
56+
57+
public function testGetCollectionName()
58+
{
59+
60+
$adapter = new MongoDBStorageAdapter($this->mongo, 'feature_test');
61+
62+
$result = $adapter->getCollectionName();
63+
$this->assertSame('feature_test', $result);
64+
65+
}
66+
67+
public function mockCollection()
68+
{
69+
return $this->createMock('MongoCollection', ['find', 'findOne', 'update', 'remove'], [], '', false);
70+
71+
}
72+
}
73+
74+
class mockMongo
75+
{
76+
public $collection;
77+
public function __construct()
78+
{
79+
80+
}
81+
82+
public function setCollection($mongoCollection)
83+
{
84+
$this->collection = $mongoCollection;
85+
}
86+
87+
public function __get($name)
88+
{
89+
return $this->collection;
90+
}
91+
}

0 commit comments

Comments
 (0)