Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #11 from SimonSimCity/master

Added couchbase storage
  • Loading branch information...
commit 0d7c5f02c63a159e0cbad94561ed096f28049bb7 2 parents a21f94d + 3ddf79a
@beberlei beberlei authored
View
2  README.md
@@ -22,7 +22,7 @@ Following vendors are targeted:
* Microsoft Azure Table (Implemented)
* Doctrine\Common\Cache provider (Implemented)
* RDBMS (Implemented)
-* Couchbase
+* Couchbase (Implemented)
* Amazon DynamoDB
* CouchDB
* MongoDB
View
3  composer.json
@@ -14,7 +14,8 @@
}
],
"suggest": {
- "riak/riak-client": "to use the Riak storage"
+ "riak/riak-client": "to use the Riak storage",
+ "ext-couchbase": "to use the Couchbase storage"
},
"description": "Simple Key-Value Store Abstraction Layer that maps to PHP objects, allowing for many backends.",
"license": "MIT",
View
116 lib/Doctrine/KeyValueStore/Storage/CouchbaseStorage.php
@@ -0,0 +1,116 @@
+<?php
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the MIT license. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+
+namespace Doctrine\KeyValueStore\Storage;
+
+use Doctrine\KeyValueStore\NotFoundException;
+
+/**
+ * @author Simon Schick <simonsimcity@gmail.com>
+ */
+class CouchbaseStorage implements Storage
+{
+ /**
+ * @var \Couchbase
+ */
+ protected $client;
+
+ /**
+ * Constructor
+ *
+ * @param \Couchbase $couchbase
+ */
+ public function __construct(\Couchbase $couchbase)
+ {
+ $this->client = $couchbase;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function supportsPartialUpdates()
+ {
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function supportsCompositePrimaryKeys()
+ {
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function requiresCompositePrimaryKeys()
+ {
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function insert($storageName, $key, array $data)
+ {
+ $this->client->add($key, $data);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function update($storageName, $key, array $data)
+ {
+ $this->client->replace($key, $data);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function delete($storageName, $key)
+ {
+ $this->client->delete($key);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function find($storageName, $key)
+ {
+ $value = $this->client->get($key);
+
+ if ($value === null) {
+ throw new NotFoundException();
+ }
+
+ return $value;
+ }
+
+ /**
+ * Return a name of the underlying storage.
+ *
+ * @return string
+ */
+ public function getName()
+ {
+ return 'couchbase';
+ }
+}
View
145 tests/Doctrine/Tests/KeyValueStore/Storage/CouchbaseStorageTest.php
@@ -0,0 +1,145 @@
+<?php
+
+namespace Doctrine\Tests\KeyValueStore\Storage;
+
+use Doctrine\KeyValueStore\Storage\CouchbaseStorage;
+
+/**
+ * Couchbase storage testcase
+ *
+ * @author Simon Schick <simonsimcity@gmail.com>
+ */
+class CouchbaseStorageTest extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * @var \PHPUnit_Framework_MockObject_MockObject
+ */
+ private $couchbase;
+
+ /**
+ * @var CouchbaseStorage
+ */
+ private $storage;
+
+ protected function setUp()
+ {
+ if (!class_exists('\Couchbase'))
+ $this->markTestSkipped("The PHP extension 'couchbase' is not installed");
+
+ $this->couchbase = $this->getMockBuilder('\Couchbase')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->storage = new CouchbaseStorage($this->couchbase);
+ }
+
+ public function testSupportsPartialUpdates()
+ {
+ $this->assertFalse($this->storage->supportsPartialUpdates());
+ }
+
+ public function testSupportsCompositePrimaryKeys()
+ {
+ $this->assertFalse($this->storage->supportsCompositePrimaryKeys());
+ }
+
+ public function testRequiresCompositePrimaryKeys()
+ {
+ $this->assertFalse($this->storage->requiresCompositePrimaryKeys());
+ }
+
+ public function testInsert()
+ {
+ $data = array(
+ 'author' => 'John Doe',
+ 'title' => 'example book',
+ );
+
+ $dbDataset = array();
+
+ $this->couchbase->expects($this->once())
+ ->method('add')
+ ->will($this->returnCallback(function($key, $data) use (&$dbDataset) {
+ $dbDataset[] = array('key' => $key, 'value' => $data);
+ }));
+
+ $this->storage->insert('', '1', $data);
+ $this->assertCount(1, $dbDataset);
+
+ $this->assertEquals(array(array('key' => '1', 'value' => $data)), $dbDataset);
+ }
+
+ public function testUpdate()
+ {
+ $data = array(
+ 'author' => 'John Doe',
+ 'title' => 'example book',
+ );
+
+ $dbDataset = array();
+
+ $this->couchbase->expects($this->once())
+ ->method('replace')
+ ->will($this->returnCallback(function($key, $data) use (&$dbDataset) {
+ $dbDataset[$key] = $data;
+ }));
+
+ $this->storage->update('', '1', $data);
+
+ $this->assertEquals(array('1' => $data), $dbDataset);
+ }
+
+ public function testDelete()
+ {
+ $dataset = array(
+ 'foobar' => array(
+ 'author' => 'John Doe',
+ 'title' => 'example book',
+ ),
+ );
+
+ $this->couchbase->expects($this->once())
+ ->method('delete')
+ ->will($this->returnCallback(function($key) use (&$dataset) {
+ foreach ($dataset as $id => $row) {
+ if ($id === $key) {
+ unset($dataset[$key]);
+ }
+ }
+ }
+ ));
+
+ $this->storage->delete('test', 'foobar');
+
+ $this->assertCount(0, $dataset);
+ }
+
+ public function testFind()
+ {
+ $dataset = array(
+ 'foobar' => array(
+ 'author' => 'John Doe',
+ 'title' => 'example book',
+ ),
+ );
+
+ $this->couchbase->expects($this->once())
+ ->method('get')
+ ->will($this->returnCallback(function($key) use (&$dataset) {
+ if (isset($dataset[$key])) {
+ return $dataset[$key];
+ }
+ return null;
+ }
+ ));
+
+ $data = $this->storage->find('test', 'foobar');
+
+ $this->assertEquals($dataset['foobar'], $data);
+ }
+
+ public function testGetName()
+ {
+ $this->assertEquals('couchbase', $this->storage->getName());
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.