Anton Grebnev
13 years ago
2 changed files with 303 additions and 0 deletions
@ -0,0 +1,120 @@ |
|||
<?php |
|||
/** |
|||
* @copyright NetMonsters <team@netmonsters.ru> |
|||
* @link http://netmonsters.ru |
|||
* @package Majestic |
|||
* @subpackage db |
|||
* @since 2011-11-15 |
|||
*/ |
|||
|
|||
class MongoCommandBuilder |
|||
{ |
|||
|
|||
const FIND = 'Find'; |
|||
|
|||
const INSERT = 'Insert'; |
|||
|
|||
const UPDATE = 'Update'; |
|||
|
|||
const REMOVE = 'Remove'; |
|||
|
|||
static public function factory($type) |
|||
{ |
|||
$class = ucfirst($type) . 'MongoCommand'; |
|||
return new $class(); |
|||
} |
|||
} |
|||
|
|||
abstract class MongoDbCommand |
|||
{ |
|||
public function execute() |
|||
{ |
|||
if ($this->checkParams()) { |
|||
return $this->concreteExecute(); |
|||
} else { |
|||
throw new Exception(get_called_class() . ' error. Bind all required params first.'); |
|||
} |
|||
} |
|||
|
|||
public function bindParam($name, $value) |
|||
{ |
|||
$this->$name = $value; |
|||
return $this; |
|||
} |
|||
|
|||
abstract protected function concreteExecute(); |
|||
|
|||
abstract protected function checkParams(); |
|||
} |
|||
|
|||
class FindMongoCommand extends MongoDbCommand |
|||
{ |
|||
protected function concreteExecute() |
|||
{ |
|||
return $this->collection->find($this->condition, $this->fields); |
|||
} |
|||
|
|||
protected function checkParams() |
|||
{ |
|||
if (isset($this->collection) && isset($this->condition) && isset($this->fields)) { |
|||
return true; |
|||
} else { |
|||
return false; |
|||
} |
|||
} |
|||
} |
|||
|
|||
class InsertMongoCommand extends MongoDbCommand |
|||
{ |
|||
protected function concreteExecute() |
|||
{ |
|||
return $this->collection->insert($this->data, array('safe' => $this->safe)); |
|||
} |
|||
|
|||
protected function checkParams() |
|||
{ |
|||
if (isset($this->collection) && isset($this->data) && isset($this->safe)) { |
|||
return true; |
|||
} else { |
|||
return false; |
|||
} |
|||
} |
|||
} |
|||
|
|||
class UpdateMongoCommand extends MongoDbCommand |
|||
{ |
|||
protected function concreteExecute() |
|||
{ |
|||
return $this->collection->update($this->condition, $this->data, array('upsert' => $this->upsert, 'safe' => $this->safe)); |
|||
} |
|||
|
|||
protected function checkParams() |
|||
{ |
|||
if (isset($this->collection) && isset($this->condition) && isset($this->data) && |
|||
isset($this->upsert) && isset($this->safe) |
|||
) { |
|||
return true; |
|||
} else { |
|||
return false; |
|||
} |
|||
} |
|||
} |
|||
|
|||
class RemoveMongoCommand extends MongoDbCommand |
|||
{ |
|||
protected function concreteExecute() |
|||
{ |
|||
return $this->collection->remove($this->condition, array('safe' => $this->safe)); |
|||
} |
|||
|
|||
protected function checkParams() |
|||
{ |
|||
if (isset($this->collection) && isset($this->condition) && isset($this->safe)) { |
|||
return true; |
|||
} else { |
|||
return false; |
|||
} |
|||
} |
|||
} |
|||
|
|||
|
@ -0,0 +1,183 @@ |
|||
<?php |
|||
|
|||
/* |
|||
* @copyright NetMonsters <team@netmonsters.ru> |
|||
* @link http://netmonsters.ru |
|||
* @package Majestic |
|||
* @subpackage UnitTests |
|||
* @since 2011-11-10 |
|||
* |
|||
* Unit tests for MongoDriver class |
|||
*/ |
|||
|
|||
require_once dirname(__FILE__) . '/../../model/DbDriver.php'; |
|||
require_once dirname(__FILE__) . '/../../model/NoSqlDbDriver.php'; |
|||
require_once dirname(__FILE__) . '/../../model/MongoDriver.php'; |
|||
require_once dirname(__FILE__) . '/../../model/MongoDbCommand.php'; |
|||
|
|||
class MongoDbCommandTest extends PHPUnit_Framework_TestCase |
|||
{ |
|||
|
|||
private $conf = array(); |
|||
|
|||
private $driver; |
|||
|
|||
public function setUp() |
|||
{ |
|||
$this->conf = array( |
|||
'hostname' => 'localhost', |
|||
'database' => 'test', |
|||
'username' => 'test', |
|||
'password' => '1234', |
|||
'port' => 27017 |
|||
); |
|||
$this->driver = new MongoDriver($this->conf); |
|||
$connection = $this->driver->getConnection(); |
|||
|
|||
$db = $connection->selectDB($this->conf['database']); |
|||
$db->authenticate($this->conf['username'], $this->conf['password']); |
|||
$collection = 'items'; |
|||
$db->dropCollection($collection); |
|||
} |
|||
|
|||
public function testCommandFactory() |
|||
{ |
|||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND); |
|||
$this->assertInstanceOf('MongoDbCommand', $cmd); |
|||
$this->assertInstanceOf('FindMongoCommand', $cmd); |
|||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT); |
|||
$this->assertInstanceOf('MongoDbCommand', $cmd); |
|||
$this->assertInstanceOf('InsertMongoCommand', $cmd); |
|||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE); |
|||
$this->assertInstanceOf('MongoDbCommand', $cmd); |
|||
$this->assertInstanceOf('UpdateMongoCommand', $cmd); |
|||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE); |
|||
$this->assertInstanceOf('MongoDbCommand', $cmd); |
|||
$this->assertInstanceOf('RemoveMongoCommand', $cmd); |
|||
} |
|||
|
|||
public function testFindCommand() |
|||
{ |
|||
$collection = $this->driver->getCollection('items'); |
|||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND); |
|||
$cmd->bindParam('collection', $collection)->bindParam('condition', array('name' => 'bread'))->bindParam('fields', array()); |
|||
$result = $cmd->execute(); |
|||
$this->assertEquals(0, $result->count()); |
|||
} |
|||
|
|||
/** |
|||
* @expectedException Exception |
|||
* @expectedExceptionMessage FindMongoCommand error. Bind all required params first. |
|||
*/ |
|||
public function testFindCommandNotAllParamsBinded() |
|||
{ |
|||
$collection = $this->driver->getCollection('items'); |
|||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND); |
|||
$cmd->bindParam('collection', $collection)->bindParam('condition', array('name' => 'bread')); |
|||
$cmd->execute(); |
|||
} |
|||
|
|||
public function testInsertCommand() |
|||
{ |
|||
$collection = $this->driver->getCollection('items'); |
|||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT); |
|||
$cmd->bindParam('collection', $collection) |
|||
->bindParam('data', array('name' => 'insert')) |
|||
->bindParam('safe', true); |
|||
$this->assertArrayHasKey('n', $cmd->execute()); |
|||
|
|||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND); |
|||
$cmd->bindParam('collection', $collection)->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array()); |
|||
$result = $cmd->execute(); |
|||
$this->assertEquals(1, $result->count()); |
|||
} |
|||
|
|||
/** |
|||
* @expectedException Exception |
|||
* @expectedExceptionMessage InsertMongoCommand error. Bind all required params first. |
|||
*/ |
|||
public function testInsertCommandNotAllParamsBinded() |
|||
{ |
|||
$collection = $this->driver->getCollection('items'); |
|||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT); |
|||
$cmd->bindParam('collection', $collection)->bindParam('data', array('name' => 'bread')); |
|||
$cmd->execute(); |
|||
} |
|||
|
|||
public function testUpdateCommand() |
|||
{ |
|||
$collection = $this->driver->getCollection('items'); |
|||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT); |
|||
$cmd->bindParam('collection', $collection) |
|||
->bindParam('data', array('name' => 'insert')) |
|||
->bindParam('safe', true); |
|||
$this->assertArrayHasKey('n', $cmd->execute()); |
|||
|
|||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE); |
|||
$cmd->bindParam('collection', $collection) |
|||
->bindParam('condition', array('name' => 'insert')) |
|||
->bindParam('data', array('$set' => array('name' => 'update'))) |
|||
->bindParam('upsert', false) |
|||
->bindParam('safe', true); |
|||
$this->assertArrayHasKey('n', $cmd->execute()); |
|||
|
|||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND); |
|||
$cmd->bindParam('collection', $collection)->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array()); |
|||
$result = $cmd->execute(); |
|||
$this->assertEquals(0, $result->count()); |
|||
$cmd->bindParam('collection', $collection)->bindParam('condition', array('name' => 'update'))->bindParam('fields', array()); |
|||
$result = $cmd->execute(); |
|||
$this->assertEquals(1, $result->count()); |
|||
} |
|||
|
|||
/** |
|||
* @expectedException Exception |
|||
* @expectedExceptionMessage UpdateMongoCommand error. Bind all required params first. |
|||
*/ |
|||
public function testUpdateCommandNotAllParamsBinded() |
|||
{ |
|||
$collection = $this->driver->getCollection('items'); |
|||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE); |
|||
$cmd->bindParam('collection', $collection)->bindParam('data', array('name' => 'bread')); |
|||
$cmd->execute(); |
|||
} |
|||
|
|||
public function testRemoveCommand() |
|||
{ |
|||
$collection = $this->driver->getCollection('items'); |
|||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT); |
|||
$cmd->bindParam('collection', $collection) |
|||
->bindParam('data', array('name' => 'insert')) |
|||
->bindParam('safe', true); |
|||
$this->assertArrayHasKey('n', $cmd->execute()); |
|||
|
|||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND); |
|||
$cmd->bindParam('collection', $collection)->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array()); |
|||
$result = $cmd->execute(); |
|||
$this->assertEquals(1, $result->count()); |
|||
|
|||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE); |
|||
$cmd->bindParam('collection', $collection) |
|||
->bindParam('condition', array('name' => 'insert')) |
|||
->bindParam('safe', true); |
|||
$this->assertArrayHasKey('n', $cmd->execute()); |
|||
|
|||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND); |
|||
$cmd->bindParam('collection', $collection)->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array()); |
|||
$result = $cmd->execute(); |
|||
$this->assertEquals(0, $result->count()); |
|||
} |
|||
|
|||
/** |
|||
* @expectedException Exception |
|||
* @expectedExceptionMessage RemoveMongoCommand error. Bind all required params first. |
|||
*/ |
|||
public function testRemoveCommandNotAllParamsBinded() |
|||
{ |
|||
$collection = $this->driver->getCollection('items'); |
|||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE); |
|||
$cmd->bindParam('collection', $collection); |
|||
$cmd->execute(); |
|||
} |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue