9 Commits

Author SHA1 Message Date
Emanuele Minotto
e32cd91113 Merge pull request #94 from doctrine/issues/91
RiakStorage upgrade
2019-09-20 21:42:29 +02:00
EmanueleMinotto
8f07e47320 Replace riak client with php-riak/riak-client 2019-09-20 21:28:57 +02:00
EmanueleMinotto
186c8e04d2 Add riak to Travis 2019-09-11 21:55:39 +02:00
Emanuele Minotto
fe7dcc5c79 Merge pull request #95 from doctrine/issues/93
MongoDbStorage upgrade
2019-09-11 21:42:11 +02:00
EmanueleMinotto
218a870dd3 Create UPGRADE.md 2019-09-11 21:36:23 +02:00
EmanueleMinotto
b62b9ea098 Drop PHP 5.5 support 2019-09-11 21:36:23 +02:00
EmanueleMinotto
3800fa4d5c Add mongo to Travis 2019-09-11 21:36:23 +02:00
EmanueleMinotto
a50dade76d Replace legacy Mongo class with MongoDB Client 2019-09-11 21:36:23 +02:00
EmanueleMinotto
6f154f5378 Add mongodb/mongodb dependency 2019-09-08 08:23:09 +02:00
15 changed files with 331 additions and 479 deletions

View File

@@ -1,17 +1,9 @@
language: php
services:
- cassandra
- couchdb
- mongodb
- redis-server
matrix:
include:
-
dist: precise
php: 5.5
php:
- 5.6
- 7.0
@@ -19,7 +11,24 @@ php:
- 7.2
- 7.3
addons:
apt:
sources:
-
key_url: 'https://packagecloud.io/gpg.key'
sourceline: 'deb https://packagecloud.io/basho/riak/ubuntu/ trusty main'
-
key_url: 'https://packagecloud.io/gpg.key'
sourceline: 'deb-src https://packagecloud.io/basho/riak/ubuntu/ trusty main'
update: true
cache:
apt: true
before_install:
- sudo apt-get install -y --allow-unauthenticated riak
- sudo service riak start
- pecl install --force mongodb
- if [[ ${TRAVIS_PHP_VERSION:0:1} != "7" ]]; then sh ./tests/travis.sh; fi
- composer self-update

7
UPGRADE.md Normal file
View File

@@ -0,0 +1,7 @@
# Upgrade to 0.3
## BC Break: Fixed MongoDB storage usage
Before v0.3 the storage name associated to a class wasn't used when the storage is `MongoDbStorage`.
In order to be consistent with other storage drivers, the `storageName` is now used for the collection name when storing and data.
To get the same behavior as in older versions, pass the collection name given in the constructor arguments as storage name.

View File

@@ -6,16 +6,16 @@
},
"require-dev": {
"datastax/php-driver": "^1.0",
"doctrine/couchdb": "^2.0@alpha",
"doctrine/couchdb": "^1.0.0-beta4",
"phpunit/phpunit": "^4.8|^5.0",
"aws/aws-sdk-php": "^3.8",
"riak/riak-client": "dev-master"
"php-riak/riak-client": "^1.0@alpha",
"mongodb/mongodb": "^1.4"
},
"suggest": {
"aws/aws-sdk-php": "to use the DynamoDB storage",
"doctrine/couchdb": "to use the CouchDB storage",
"ext-couchbase": "to use the Couchbase 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 File

@@ -8,7 +8,7 @@ This guide covers getting started with the Doctrine Key Value Store.
To use the KeyValueStore you actually need:
- PHP 5.5 or above
- PHP 5.6 or above
- Composer Package Manager (`Install Composer
<http://getcomposer.org/doc/00-intro.md>`_)

View File

@@ -224,36 +224,35 @@ See the `AWS docs <http://docs.aws.amazon.com/amazondynamodb/latest/developergui
MongoDB
-------
Mongo support is provided using a `Mongo <http://php.net/manual/en/class.mongo.php>`_
instance, the collection name and the database name.
Both the options ``collection`` and ``database`` are required.
MongoDB is based on `mongodb/mongodb <https://github.com/mongodb/mongo-php-library>`_:
MongoDB support is provided using a `Database <https://docs.mongodb.com/php-library/current/reference/class/MongoDBDatabase/>`_
instance.
.. code-block:: php
<?php
use MongoDB\Client;
use Doctrine\KeyValueStore\Storage\MongoDbStorage;
$conn = new \Mongo(/* connection parameters and options */);
$client = new Client(/* connection parameters and options */);
$storage = new MongoDbStorage($conn, array(
'collection' => 'your_collection',
'database' => 'your_database',
));
$storage = new MongoDbStorage($client->your_database);
Riak
----
Riak support is provided through the library `riak/riak-client <https://github.com/nacmartin/riak-client>`_ :
Riak support is provided through the library `php-riak/riak-client <https://github.com/php-riak/riak-client>`_ :
.. code-block:: php
<?php
use Doctrine\KeyValueStore\Storage\RiakStorage;
use Riak\Client;
use Riak\Client\RiakClientBuilder;
$conn = new Riak(/* connection parameters */);
$conn = (new RiakClientBuilder())
->withNodeUri(/* connection DNS */)
->build();
$storage = new RiakStorage($conn);

View File

@@ -202,7 +202,7 @@ class RangeQuery
/**
* Execute query and return a result iterator.
*
* @return array
* @return ResultIterator
*/
public function execute()
{

View File

@@ -29,14 +29,14 @@ namespace Doctrine\KeyValueStore\Query;
interface RangeQueryStorage
{
/**
* Execute the range query and return an array
* Execute the range query and return a ResultIterator
*
* @param RangeQuery $query
* @param string $storageName
* @param array $key
* @param Closure $hydrateRow
*
* @return array
* @return ResultIterator
*/
public function executeRangeQuery(RangeQuery $query, $storageName, $key, \Closure $hydrateRow = null);
}

View File

@@ -23,8 +23,6 @@ namespace Doctrine\KeyValueStore\Storage;
use Cassandra\ExecutionOptions;
use Cassandra\Session;
use Doctrine\KeyValueStore\NotFoundException;
use Doctrine\KeyValueStore\Query\RangeQuery;
use Doctrine\KeyValueStore\Query\RangeQueryStorage;
/**
* Cassandra Storage Engine for KeyValueStore.
@@ -33,7 +31,7 @@ use Doctrine\KeyValueStore\Query\RangeQueryStorage;
*
* @uses https://github.com/datastax/php-driver
*/
class CassandraStorage implements Storage, RangeQueryStorage
class CassandraStorage implements Storage
{
/**
* @var \Cassandra\Session
@@ -178,13 +176,6 @@ class CassandraStorage implements Storage, RangeQueryStorage
return $data;
}
/**
* {@inheritDoc}
*/
public function executeRangeQuery(RangeQuery $query, $storageName, $key, \Closure $hydrateRow = null)
{
}
/**
* {@inheritDoc}
*/

View File

@@ -21,9 +21,6 @@
namespace Doctrine\KeyValueStore\Storage;
use Doctrine\CouchDB\CouchDBClient;
use Doctrine\CouchDB\Mango\MangoQuery;
use Doctrine\KeyValueStore\Query\RangeQuery;
use Doctrine\KeyValueStore\Query\RangeQueryStorage;
/**
* Key-Value-Storage using a Doctrine CouchDB Client library as backend.
@@ -33,7 +30,7 @@ use Doctrine\KeyValueStore\Query\RangeQueryStorage;
*
* @author Emanuele Minotto <minottoemanuele@gmail.com>
*/
final class CouchDbStorage implements Storage, RangeQueryStorage
final class CouchDbStorage implements Storage
{
/**
* @var CouchDBClient
@@ -119,7 +116,7 @@ final class CouchDbStorage implements Storage, RangeQueryStorage
/**
* @param string $storageName
* @param array|string $key
*
*
* @return string
*/
private function flattenKey($storageName, $key)
@@ -140,72 +137,4 @@ final class CouchDbStorage implements Storage, RangeQueryStorage
return $finalKey;
}
/**
* {@inheritDoc}
*/
public function executeRangeQuery(RangeQuery $query, $storageName, $key, \Closure $hydrateRow = null)
{
$mangoQuery = new MangoQuery();
$partitionKey = $query->getPartitionKey();
$conditions = [];
foreach ($query->getConditions() as $condition) {
switch ($condition[0]) {
case RangeQuery::CONDITION_LE:
$conditions[] = [
$partitionKey => [
'$lte' => $condition[1],
],
];
break;
case RangeQuery::CONDITION_GE:
$conditions[] = [
$partitionKey => [
'$gte' => $condition[1],
],
];
break;
case RangeQuery::CONDITION_NEQ:
$conditions[] = [
$partitionKey => [
'$ne' => $condition[1],
],
];
break;
case RangeQuery::CONDITION_STARTSWITH:
$conditions[] = [
$partitionKey => [
'$regex' => '^'.$condition[1],
],
];
break;
default:
$conditions[] = [
$partitionKey => [
'$'.$condition[0] => $condition[1],
],
];
break;
}
}
$mangoQuery
->select(['_id', $key])
->where(['$and' => $conditions])
->limit($query->getLimit());
$results = [];
$mangoResults = $this->client->find($query);
foreach ($mangoResults as $mangoResult) {
$results[] = $hydrateRow($mangoResult);
}
return $results;
}
}

View File

@@ -21,6 +21,7 @@
namespace Doctrine\KeyValueStore\Storage;
use Doctrine\KeyValueStore\NotFoundException;
use MongoDB\Database;
/**
* MongoDb storage
@@ -30,57 +31,16 @@ use Doctrine\KeyValueStore\NotFoundException;
class MongoDbStorage implements Storage
{
/**
* @var \Mongo
* @var Database
*/
protected $mongo;
private $database;
/**
* @var array
* @param Database $database
*/
protected $dbOptions;
/**
* @var \MongoCollection
*/
protected $collection;
/**
* Constructor
*
* @param \Mongo $mongo
* @param array $dbOptions
*/
public function __construct(\Mongo $mongo, array $dbOptions = [])
public function __construct(Database $database)
{
$this->mongo = $mongo;
$this->dbOptions = array_merge([
'database' => '',
'collection' => '',
], $dbOptions);
}
/**
* Initialize the mongodb collection
*
* @throws \RuntimeException
*/
public function initialize()
{
if (null !== $this->collection) {
return;
}
if (empty($this->dbOptions['database'])) {
throw new \RuntimeException('The option "database" must be set');
}
if (empty($this->dbOptions['collection'])) {
throw new \RuntimeException('The option "collection" must be set');
}
$this->collection = $this
->mongo
->selectDB($this->dbOptions['database'])
->selectCollection($this->dbOptions['collection']);
$this->database = $database;
}
/**
@@ -112,14 +72,12 @@ class MongoDbStorage implements Storage
*/
public function insert($storageName, $key, array $data)
{
$this->initialize();
$value = [
'key' => $key,
'value' => $data,
];
$this->collection->insert($value);
$this->database
->selectCollection($storageName)
->insertOne([
'key' => $key,
'value' => $data,
]);
}
/**
@@ -127,14 +85,14 @@ class MongoDbStorage implements Storage
*/
public function update($storageName, $key, array $data)
{
$this->initialize();
$value = [
'key' => $key,
'value' => $data,
];
$this->collection->update(['key' => $key], $value);
$this->database
->selectCollection($storageName)
->replaceOne([
'key' => $key,
], [
'key' => $key,
'value' => $data,
]);
}
/**
@@ -142,9 +100,11 @@ class MongoDbStorage implements Storage
*/
public function delete($storageName, $key)
{
$this->initialize();
$this->collection->remove(['key' => $key]);
$this->database
->selectCollection($storageName)
->deleteOne([
'key' => $key,
]);
}
/**
@@ -152,15 +112,23 @@ class MongoDbStorage implements Storage
*/
public function find($storageName, $key)
{
$this->initialize();
$result = $this->database
->selectCollection($storageName, [
'typeMap' => [
'array' => 'array',
'document' => 'array',
'root' => 'array',
],
])
->findOne([
'key' => $key,
]);
$value = $this->collection->findOne(['key' => $key], ['value']);
if ($value) {
return $value['value'];
if (! $result || ! $result['value']) {
throw new NotFoundException();
}
throw new NotFoundException();
return $result['value'];
}
/**

View File

@@ -21,7 +21,14 @@
namespace Doctrine\KeyValueStore\Storage;
use Doctrine\KeyValueStore\NotFoundException;
use Riak\Client;
use Riak\Client\Command\Kv\DeleteValue;
use Riak\Client\Command\Kv\FetchValue;
use Riak\Client\Command\Kv\StoreValue;
use Riak\Client\Core\Query\RiakLocation;
use Riak\Client\Core\Query\RiakNamespace;
use Riak\Client\Core\Query\RiakObject;
use Riak\Client\RiakClient;
use Riak\Client\RiakException;
/**
* @author Markus Bachmann <markus.bachmann@bachi.biz>
@@ -29,17 +36,11 @@ use Riak\Client;
class RiakStorage implements Storage
{
/**
* @var \Riak\Client
* @var RiakClient
*/
protected $client;
private $client;
/**
* Constructor
*
* @param \Riak\Client $riak
* @param string $bucketName
*/
public function __construct(Client $riak)
public function __construct(RiakClient $riak)
{
$this->client = $riak;
}
@@ -68,14 +69,25 @@ class RiakStorage implements Storage
return false;
}
private function store($storageName, $key, array $data)
{
$location = $this->getRiakLocation($storageName, $key);
$riakObject = new RiakObject();
$riakObject->setContentType('application/json');
$riakObject->setValue(json_encode($data));
$store = StoreValue::builder($location, $riakObject)->build();
$this->client->execute($store);
}
/**
* {@inheritDoc}
*/
public function insert($storageName, $key, array $data)
{
$bucket = $this->client->bucket($storageName);
$object = $bucket->newObject($key, $data);
$object->store();
$this->store($storageName, $key, $data);
}
/**
@@ -83,12 +95,7 @@ class RiakStorage implements Storage
*/
public function update($storageName, $key, array $data)
{
$bucket = $this->client->bucket($storageName);
/** @var $object \Riak\Object */
$object = $bucket->get($key);
$object->setData($data);
$object->store();
$this->store($storageName, $key, $data);
}
/**
@@ -96,17 +103,15 @@ class RiakStorage implements Storage
*/
public function delete($storageName, $key)
{
$bucket = $this->client->bucket($storageName);
$location = $this->getRiakLocation($storageName, $key);
/** @var $object \Riak\Object */
$object = $bucket->get($key);
$delete = DeleteValue::builder($location)->build();
if (! $object->exists()) {
// object does not exist, do nothing
return;
try {
$this->client->execute($delete);
} catch (RiakException $exception) {
// deletion can fail silent
}
$object->delete();
}
/**
@@ -114,16 +119,29 @@ class RiakStorage implements Storage
*/
public function find($storageName, $key)
{
$bucket = $this->client->bucket($storageName);
$location = $this->getRiakLocation($storageName, $key);
/** @var $object \Riak\Object */
$object = $bucket->get($key);
// fetch object
$fetch = FetchValue::builder($location)->build();
if (! $object->exists()) {
throw new NotFoundException;
try {
$result = $this->client->execute($fetch);
} catch (RiakException $exception) {
throw new NotFoundException();
}
return $object->getData();
$json = (string) $result
->getValue()
->getValue();
return json_decode($json, true);
}
private function getRiakLocation($storageName, $key)
{
$namespace = new RiakNamespace('default', $storageName);
return new RiakLocation($namespace, $key);
}
/**

View File

@@ -16,5 +16,6 @@
<var name="DOCTRINE_KEYVALUE_AZURE_AUTHSCHEMA" value="sharedlite" />
<var name="DOCTRINE_KEYVALUE_AZURE_NAME" value="" />
<var name="DOCTRINE_KEYVALUE_AZURE_KEY" value="" />
<env name="RIAK_DNS" value="" />
</php>
</phpunit>

View File

@@ -20,40 +20,34 @@
namespace Doctrine\Tests\KeyValueStore\Storage;
use Doctrine\KeyValueStore\NotFoundException;
use Doctrine\KeyValueStore\Storage\MongoDbStorage;
use MongoDB\Client;
/**
* MongoDb storage testcase
*
* @author Markus Bachmann <markus.bachmann@bachi.biz>
*
* @requires extension mongo
* @covers \Doctrine\KeyValueStore\Storage\MongoDbStorage
* @requires extension mongodb
*/
class MongoDbStorageTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Client
*/
private $client;
/**
* @var MongoDbStorage
*/
private $storage;
protected function setUp()
{
$this->mongo = $this
->getMockBuilder('\Mongo')
->disableOriginalConstructor()
->getMock();
$this->mongodb = $this->getMockBuilder('\MongoDB')->disableOriginalConstructor()->getMock();
$this->mongo->expects($this->any())
->method('selectDB')
->will($this->returnValue($this->mongodb));
$this->collection = $this->getMockBuilder('MongoCollection')->disableOriginalConstructor()->getMock();
$this->mongodb->expects($this->once())
->method('selectCollection')
->will($this->returnValue($this->collection));
$this->storage = new MongoDbStorage($this->mongo, [
'collection' => 'test',
'database' => 'test',
]);
$this->client = new Client();
$this->storage = new MongoDbStorage($this->client->test);
}
public function testInsert()
@@ -63,20 +57,21 @@ class MongoDbStorageTest extends \PHPUnit_Framework_TestCase
'title' => 'example book',
];
$dbDataset = [];
$this->storage->insert('mongodb', 'testInsert', $data);
$this->collection->expects($this->once())
->method('insert')
->will($this->returnCallback(function ($data) use (&$dbDataset) {
$dbDataset[] = $data;
}));
$result = $this->client
->test
->mongodb
->findOne([
'key' => 'testInsert',
]);
$this->storage->insert('mongodb', '1', $data);
$this->assertCount(1, $dbDataset);
$this->assertEquals([['key' => '1', 'value' => $data]], $dbDataset);
$this->assertSame($data, $result['value']->getArrayCopy());
}
/**
* @depends testInsert
*/
public function testUpdate()
{
$data = [
@@ -84,80 +79,67 @@ class MongoDbStorageTest extends \PHPUnit_Framework_TestCase
'title' => 'example book',
];
$dbDataset = [];
$this->storage->insert('mongodb', 'testUpdate', [
'foo' => 'bar',
]);
$this->storage->update('mongodb', 'testUpdate', $data);
$this->collection->expects($this->once())
->method('update')
->will($this->returnCallback(function ($citeria, $data) use (&$dbDataset) {
$dbDataset = [$citeria, $data];
}));
$result = $this->client
->test
->mongodb
->findOne([
'key' => 'testUpdate',
]);
$this->storage->update('mongodb', '1', $data);
$this->assertEquals(['key' => '1'], $dbDataset[0]);
$this->assertEquals(['key' => '1', 'value' => $data], $dbDataset[1]);
$this->assertSame($data, $result['value']->getArrayCopy());
}
/**
* @depends testInsert
*/
public function testDelete()
{
$dataset = [
[
'key' => 'foobar',
'value' => [
'author' => 'John Doe',
'title' => 'example book',
],
],
];
$this->storage->insert('mongodb', 'testDelete', [
'foo' => 'bar',
]);
$this->collection->expects($this->once())
->method('remove')
->will($this->returnCallback(function ($citeria) use (&$dataset) {
foreach ($dataset as $key => $row) {
if ($row['key'] === $citeria['key']) {
unset($dataset[$key]);
}
}
}
));
$this->storage->delete('mongodb', 'testDelete');
$this->storage->delete('test', 'foobar');
$result = $this->client
->test
->mongodb
->findOne([
'key' => 'testDelete',
]);
$this->assertCount(0, $dataset);
$this->assertNull($result);
}
/**
* @depends testInsert
*/
public function testFind()
{
$dataset = [
[
'key' => 'foobar',
'value' => [
'author' => 'John Doe',
'title' => 'example book',
],
],
'author' => 'John Doe',
'title' => 'example book',
];
$this->collection->expects($this->once())
->method('findOne')
->will($this->returnCallback(function ($citeria, $fields) use (&$dataset) {
foreach ($dataset as $key => $row) {
if ($row['key'] === $citeria['key']) {
return $row;
}
}
}
));
$this->storage->insert('mongodb', 'testFind', $dataset);
$data = $this->storage->find('test', 'foobar');
$data = $this->storage->find('mongodb', 'testFind');
$this->assertEquals($dataset[0]['value'], $data);
$this->assertEquals($dataset, $data);
}
public function testFindWithNotExistKey()
{
$this->setExpectedException(NotFoundException::class);
$this->storage->find('mongodb', 'not-existing-key');
}
public function testGetName()
{
$this->storage->initialize();
$this->assertEquals('mongodb', $this->storage->getName());
}
}

View File

@@ -20,34 +20,46 @@
namespace Doctrine\Tests\KeyValueStore\Storage;
use Doctrine\KeyValueStore\NotFoundException;
use Doctrine\KeyValueStore\Storage\RiakStorage;
use PHPUnit_Framework_TestCase;
use Riak\Client\Command\Kv\Builder\ListKeysBuilder;
use Riak\Client\Command\Kv\FetchValue;
use Riak\Client\Core\Query\RiakLocation;
use Riak\Client\Core\Query\RiakNamespace;
use Riak\Client\Core\Query\RiakObject;
use Riak\Client\Core\Transport\RiakTransportException;
use Riak\Client\RiakClient;
use Riak\Client\RiakClientBuilder;
/**
* @author Markus Bachmann <markus.bachmann@bachi.biz>
*/
class RiakStorageTest extends \PHPUnit_Framework_TestCase
class RiakStorageTest extends PHPUnit_Framework_TestCase
{
/**
* @var RiakClient
*/
private $client;
/**
* @var RiakStorage
*/
private $storage;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $riak;
protected function setup()
protected function setUp()
{
if (PHP_MAJOR_VERSION >= 7) {
$this->markTestSkipped('Riak extension is not available for PHP versions >= 7');
$dns = getenv('RIAK_DNS');
if (empty($dns)) {
$this->markTestSkipped('Missing Riak DNS');
}
$this->riak = $this->getMockBuilder('Riak\\Client')
->disableOriginalConstructor()
->getMock();
$this->client = (new RiakClientBuilder())
->withNodeUri($dns)
->build();
$this->storage = new RiakStorage($this->riak);
$this->storage = new RiakStorage($this->client);
}
public function testSupportsPartialUpdates()
@@ -67,186 +79,124 @@ class RiakStorageTest extends \PHPUnit_Framework_TestCase
public function testInsert()
{
$bucket = $this->getMockBuilder('Riak\Bucket')
->disableOriginalConstructor()
->getMock();
$data = [
'title' => 'Riak test',
];
$this->riak->expects($this->once())
->method('bucket')
->will($this->returnValue($bucket));
$this->storage->insert('riak-test', 'foobar', $data);
$objectMock = $this->getMockBuilder('Riak\Object')
->disableOriginalConstructor()
->getMock();
$location = $this->getRiakLocation();
$objectMock->expects($this->once())
->method('store');
$fetch = FetchValue::builder($location)->build();
$that = $this;
$bucket->expects($this->once())
->method('newObject')
->will($this->returnCallback(function ($key, $data) use ($objectMock, $that) {
$that->assertEquals('foobar', $key);
$that->assertEquals(['title' => 'Riak test'], $data);
return $objectMock;
}));
$json = (string) $this->client
->execute($fetch)
->getValue()
->getValue();
$this->storage->insert('riak-test', 'foobar', ['title' => 'Riak test']);
}
public function testUpdate()
{
$objectMock = $this->getMockBuilder('Riak\Object')
->disableOriginalConstructor()
->getMock();
$bucket = $this->getMockBuilder('Riak\Bucket')
->disableOriginalConstructor()
->getMock();
$this->riak->expects($this->once())
->method('bucket')
->will($this->returnValue($bucket));
$bucket->expects($this->once())
->method('get')
->will($this->returnValue($objectMock));
$that = $this;
$objectMock->expects($this->once())
->method('setData')
->will($this->returnCallback(function ($data) use ($that) {
$that->assertEquals(['title' => 'Riak cookbook'], $data);
}));
$objectMock->expects($this->once())
->method('store');
$this->storage->update('riak-test', 'foobar', ['title' => 'Riak cookbook']);
}
public function testDelete()
{
$objectMock = $this->getMockBuilder('Riak\Object')
->disableOriginalConstructor()
->getMock();
$bucket = $this->getMockBuilder('Riak\Bucket')
->disableOriginalConstructor()
->getMock();
$this->riak->expects($this->once())
->method('bucket')
->will($this->returnValue($bucket));
$bucket->expects($this->once())
->method('get')
->with('foobar')
->will($this->returnValue($objectMock));
$objectMock->expects($this->once())
->method('exists')
->will($this->returnValue(true));
$objectMock->expects($this->once())
->method('delete');
$this->storage->delete('riak-test', 'foobar');
}
public function testDeleteWithNotExistKey()
{
$objectMock = $this->getMockBuilder('Riak\Object')
->disableOriginalConstructor()
->getMock();
$bucket = $this->getMockBuilder('Riak\Bucket')
->disableOriginalConstructor()
->getMock();
$this->riak->expects($this->once())
->method('bucket')
->will($this->returnValue($bucket));
$bucket->expects($this->once())
->method('get')
->with('foobar')
->will($this->returnValue($objectMock));
$objectMock->expects($this->once())
->method('exists')
->will($this->returnValue(false));
$objectMock->expects($this->never())
->method('delete');
$this->storage->delete('riak-test', 'foobar');
}
public function testFind()
{
$objectMock = $this->getMockBuilder('Riak\Object')
->disableOriginalConstructor()
->getMock();
$bucket = $this->getMockBuilder('Riak\Bucket')
->disableOriginalConstructor()
->getMock();
$this->riak->expects($this->once())
->method('bucket')
->will($this->returnValue($bucket));
$bucket->expects($this->once())
->method('get')
->with('foobar')
->will($this->returnValue($objectMock));
$objectMock->expects($this->once())
->method('exists')
->will($this->returnValue(true));
$objectMock->expects($this->once())
->method('getData')
->will($this->returnValue(['title' => 'Riak Test']));
$this->assertEquals(['title' => 'Riak Test'], $this->storage->find('riaktest', 'foobar'));
$this->assertSame($data, json_decode($json, true));
}
/**
* @expectedException Doctrine\KeyValueStore\NotFoundException
* @depends testInsert
*/
public function testUpdate()
{
$data = [
'title' => 'Riak update',
];
$this->storage->insert('riak-test', 'foobar', [
'title' => 'Riak insert',
]);
$location = $this->getRiakLocation();
$this->assertTotalBucketKeys(1, $location);
$this->storage->update('riak-test', 'foobar', $data);
$fetch = FetchValue::builder($location)->build();
$json = (string) $this->client
->execute($fetch)
->getValue()
->getValue();
$this->assertSame($data, json_decode($json, true));
$this->assertTotalBucketKeys(1, $location);
}
/**
* @depends testInsert
*/
public function testDelete()
{
$this->testInsert();
$this->storage->delete('riak-test', 'foobar');
$location = $this->getRiakLocation();
$fetch = FetchValue::builder($location)->build();
$this->setExpectedException(RiakTransportException::class);
$this->client->execute($fetch);
$this->assertTotalBucketKeys(0, $location);
}
/**
* @depends testDelete
*/
public function testDeleteWithNotExistKey()
{
$this->storage->delete('riak-test', 'foobar');
$this->storage->delete('riak-test', 'foobar');
}
/**
* @depends testInsert
*/
public function testFind()
{
$data = [
'title' => 'Riak test',
];
$this->storage->insert('riak-test', 'foobar', $data);
$result = $this->storage->find('riak-test', 'foobar');
$this->assertSame($data, $result);
}
public function testFindWithNotExistKey()
{
$objectMock = $this->getMockBuilder('Riak\Object')
->disableOriginalConstructor()
->getMock();
$bucket = $this->getMockBuilder('Riak\Bucket')
->disableOriginalConstructor()
->getMock();
$this->riak->expects($this->once())
->method('bucket')
->will($this->returnValue($bucket));
$bucket->expects($this->once())
->method('get')
->with('foobar')
->will($this->returnValue($objectMock));
$objectMock->expects($this->once())
->method('exists')
->will($this->returnValue(false));
$objectMock->expects($this->never())
->method('getData');
$this->storage->find('riak-test', 'foobar');
$this->setExpectedException(NotFoundException::class);
$this->storage->find('riak-test', 'foobar-1');
}
public function testGetName()
{
$this->assertEquals('riak', $this->storage->getName());
}
private function assertTotalBucketKeys($expectedTotal, $location)
{
$command = (new ListKeysBuilder($location->getNamespace()))->build();
$iterator = $this->client
->execute($command)
->getIterator();
$this->assertCount($expectedTotal, iterator_to_array($iterator));
}
private function getRiakLocation()
{
$namespace = new RiakNamespace('default', 'riak-test');
return new RiakLocation($namespace, 'foobar');
}
}

View File

@@ -7,7 +7,5 @@ sudo apt-get install -y libuv-dev libssl-dev
cd /tmp && git clone https://github.com/datastax/php-driver.git && cd php-driver && git submodule update --init
cd ext && ./install.sh && cd "$TRAVIS_BUILD_DIR"
echo "extension=cassandra.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
# PHP extensions
yes | pecl install mongo
# PECL extensions
echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini