forked from basho/riak-php-client
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Luke Bakken
committed
Jan 18, 2017
1 parent
4221663
commit 3992169
Showing
9 changed files
with
359 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Basho\Riak\Command\Builder; | ||
|
||
use Basho\Riak; | ||
use Basho\Riak\Command; | ||
|
||
/** | ||
* @author Luke Bakken <[email protected]> | ||
*/ | ||
class UpdateGSet extends Command\Builder implements Command\BuilderInterface | ||
{ | ||
use LocationTrait; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $add_all = []; | ||
|
||
/** | ||
* @param mixed $element | ||
* | ||
* @return $this | ||
*/ | ||
public function add($element) | ||
{ | ||
settype($element, 'string'); | ||
$this->add_all[] = $element; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getAddAll() | ||
{ | ||
return $this->add_all; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @return Command\DataType\GSet\Store | ||
*/ | ||
public function build() | ||
{ | ||
$this->validate(); | ||
|
||
return new Command\DataType\GSet\Store($this); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function validate() | ||
{ | ||
$this->required('Bucket'); | ||
|
||
$count_add = count($this->add_all); | ||
|
||
if ($count_add < 1) { | ||
throw new Exception('At least one element to add needs to be defined.'); | ||
} | ||
} | ||
} |
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,64 @@ | ||
<?php | ||
|
||
namespace Basho\Riak\Command\DataType\GSet; | ||
|
||
use Basho\Riak\Command; | ||
use Basho\Riak\CommandInterface; | ||
use Basho\Riak\Location; | ||
|
||
/** | ||
* Stores a write update to a gset | ||
* | ||
* @author Luke Bakken <[email protected]> | ||
*/ | ||
class Store extends Command implements CommandInterface | ||
{ | ||
protected $method = 'POST'; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $add_all = []; | ||
|
||
/** | ||
* @var Command\DataType\Set\Response|null | ||
*/ | ||
protected $response = NULL; | ||
|
||
/** | ||
* @var Location|null | ||
*/ | ||
protected $location = NULL; | ||
|
||
public function __construct(Command\Builder\UpdateGSet $builder) | ||
{ | ||
parent::__construct($builder); | ||
|
||
$this->add_all = $builder->getAddAll(); | ||
$this->bucket = $builder->getBucket(); | ||
$this->location = $builder->getLocation(); | ||
} | ||
|
||
public function getLocation() | ||
{ | ||
return $this->location; | ||
} | ||
|
||
public function getEncodedData() | ||
{ | ||
return json_encode($this->getData()); | ||
} | ||
|
||
public function getData() | ||
{ | ||
return ['add_all' => $this->add_all]; | ||
} | ||
|
||
/** | ||
* @return Command\DataType\Set\Response | ||
*/ | ||
public function execute() | ||
{ | ||
return parent::execute(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
namespace Basho\Tests; | ||
|
||
use Basho\Riak\Command; | ||
|
||
/** | ||
* Functional tests related to GSet CRDTs | ||
* | ||
* @author Luke Bakken <[email protected]> | ||
*/ | ||
class GSetTest extends TestCase | ||
{ | ||
/** | ||
* Key to be used for tests | ||
* | ||
* @var string | ||
*/ | ||
private static $key = ''; | ||
|
||
public static function setUpBeforeClass() | ||
{ | ||
parent::setUpBeforeClass(); | ||
|
||
// make completely random key based on time | ||
static::$key = md5(rand(0, 99) . time()); | ||
|
||
try | ||
{ | ||
// Skip this suite if the "gsets" bucket type is not present | ||
$command = (new Command\Builder\FetchBucketProperties(static::$riak)) | ||
->buildBucket('test', static::GSET_BUCKET_TYPE) | ||
->build(); | ||
|
||
$response = $command->execute(); | ||
|
||
if (!$response->isSuccess() || $response->getCode() != 200) { | ||
throw new \PHPUnit_Framework_SkippedTestSuiteError("gsets bucket type is not enabled and activated, skipping"); | ||
} | ||
} | ||
catch (\Exception $ex) | ||
{ | ||
throw new \PHPUnit_Framework_SkippedTestSuiteError("gsets bucket type is not enabled and activated, skipping"); | ||
} | ||
} | ||
|
||
public function testAddWithoutKey() | ||
{ | ||
// build an object | ||
$command = (new Command\Builder\UpdateGSet(static::$riak)) | ||
->add('gosabres poked you.') | ||
->add('phprocks viewed your profile.') | ||
->add('phprocks started following you.') | ||
->buildBucket('default', static::GSET_BUCKET_TYPE) | ||
->build(); | ||
|
||
$response = $command->execute(); | ||
|
||
// expects 201 - Created | ||
$this->assertEquals('201', $response->getCode()); | ||
$this->assertNotEmpty($response->getLocation()); | ||
} | ||
|
||
public function testFetchNotFound() | ||
{ | ||
$command = (new Command\Builder\FetchSet(static::$riak)) | ||
->buildLocation(static::$key, 'default', static::GSET_BUCKET_TYPE) | ||
->build(); | ||
|
||
$response = $command->execute(); | ||
|
||
$this->assertEquals('404', $response->getCode()); | ||
} | ||
|
||
/** | ||
* @depends testFetchNotFound | ||
*/ | ||
public function testAddNewWithKey() | ||
{ | ||
$command = (new Command\Builder\UpdateGSet(static::$riak)) | ||
->add('Sabres') | ||
->add('Canadiens') | ||
->add('Bruins') | ||
->add('Maple Leafs') | ||
->add('Senators') | ||
->add('Red Wings') | ||
->add('Thrashers') | ||
->buildLocation(static::$key, 'Teams', static::GSET_BUCKET_TYPE) | ||
->build(); | ||
|
||
$response = $command->execute(); | ||
|
||
// expects 204 - No Content | ||
// this is wonky, its not 201 because the key may have been generated on another node | ||
$this->assertEquals('204', $response->getCode()); | ||
$this->assertEmpty($response->getLocation()); | ||
|
||
$command = (new Command\Builder\FetchSet(static::$riak)) | ||
->buildLocation(static::$key, 'Teams', static::GSET_BUCKET_TYPE) | ||
->build(); | ||
|
||
$response = $command->execute(); | ||
|
||
$this->assertEquals('200', $response->getCode()); | ||
$this->assertInstanceOf('Basho\Riak\DataType\Set', $response->getSet()); | ||
$this->assertNotEmpty($response->getSet()->getData()); | ||
$this->assertTrue(is_array($response->getSet()->getData())); | ||
$this->assertEquals(7, count($response->getSet()->getData())); | ||
$this->assertEmpty($response->getSet()->getContext()); | ||
} | ||
|
||
/** | ||
* @depends testAddNewWithKey | ||
*/ | ||
public function testAddAnotherNew() | ||
{ | ||
// add without context | ||
$command = (new Command\Builder\UpdateGSet(static::$riak)) | ||
->add('Lightning') | ||
->buildLocation(static::$key, 'Teams', static::GSET_BUCKET_TYPE) | ||
->build(); | ||
|
||
$response = $command->execute(); | ||
|
||
// 204 - No Content | ||
$this->assertEquals('204', $response->getCode()); | ||
|
||
$command = (new Command\Builder\FetchSet(static::$riak)) | ||
->buildLocation(static::$key, 'Teams', static::GSET_BUCKET_TYPE) | ||
->build(); | ||
|
||
$response = $command->execute(); | ||
|
||
$this->assertEquals('200', $response->getCode()); | ||
$this->assertInstanceOf('Basho\Riak\DataType\Set', $response->getSet()); | ||
$this->assertNotEmpty($response->getSet()->getData()); | ||
$this->assertTrue(is_array($response->getSet()->getData())); | ||
$this->assertEquals(8, count($response->getSet()->getData())); | ||
$this->assertEmpty($response->getSet()->getContext()); | ||
} | ||
} |
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
Oops, something went wrong.