This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
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 #11 from SimonSimCity/master
Added couchbase storage
- Loading branch information
Showing
4 changed files
with
264 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
116 changes: 116 additions & 0 deletions
116
lib/Doctrine/KeyValueStore/Storage/CouchbaseStorage.php
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,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'; | ||
} | ||
} |
145 changes: 145 additions & 0 deletions
145
tests/Doctrine/Tests/KeyValueStore/Storage/CouchbaseStorageTest.php
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,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()); | ||
} | ||
} |