Browse Source

modified MongoCommand classes

master
Anton Grebnev 13 years ago
parent
commit
229a39d682
  1. 36
      model/MongoDbCommand.php
  2. 57
      tests/model/MongoDbCommandTest.php

36
model/MongoDbCommand.php

@ -18,15 +18,19 @@ class MongoCommandBuilder
const REMOVE = 'Remove'; const REMOVE = 'Remove';
static public function factory($type)
static public function factory($type, $collection = null)
{ {
$class = ucfirst($type) . 'MongoCommand'; $class = ucfirst($type) . 'MongoCommand';
return new $class();
$command = new $class();
$command->setCollection($collection);
return $command;
} }
} }
abstract class MongoDbCommand abstract class MongoDbCommand
{ {
protected $collection;
public function execute() public function execute()
{ {
if ($this->checkParams()) { if ($this->checkParams()) {
@ -38,7 +42,15 @@ abstract class MongoDbCommand
public function bindParam($name, $value) public function bindParam($name, $value)
{ {
if (property_exists($this, $name)) {
$this->$name = $value; $this->$name = $value;
}
return $this;
}
public function setCollection($collection)
{
$this->collection = $collection;
return $this; return $this;
} }
@ -49,6 +61,10 @@ abstract class MongoDbCommand
class FindMongoCommand extends MongoDbCommand class FindMongoCommand extends MongoDbCommand
{ {
protected $condition;
protected $fields;
protected function concreteExecute() protected function concreteExecute()
{ {
return $this->collection->find($this->condition, $this->fields); return $this->collection->find($this->condition, $this->fields);
@ -66,6 +82,10 @@ class FindMongoCommand extends MongoDbCommand
class InsertMongoCommand extends MongoDbCommand class InsertMongoCommand extends MongoDbCommand
{ {
protected $data;
protected $safe;
protected function concreteExecute() protected function concreteExecute()
{ {
return $this->collection->insert($this->data, array('safe' => $this->safe)); return $this->collection->insert($this->data, array('safe' => $this->safe));
@ -83,6 +103,14 @@ class InsertMongoCommand extends MongoDbCommand
class UpdateMongoCommand extends MongoDbCommand class UpdateMongoCommand extends MongoDbCommand
{ {
protected $condition;
protected $data;
protected $upsert;
protected $safe;
protected function concreteExecute() protected function concreteExecute()
{ {
return $this->collection->update($this->condition, $this->data, array('upsert' => $this->upsert, 'safe' => $this->safe)); return $this->collection->update($this->condition, $this->data, array('upsert' => $this->upsert, 'safe' => $this->safe));
@ -102,6 +130,10 @@ class UpdateMongoCommand extends MongoDbCommand
class RemoveMongoCommand extends MongoDbCommand class RemoveMongoCommand extends MongoDbCommand
{ {
protected $condition;
protected $safe;
protected function concreteExecute() protected function concreteExecute()
{ {
return $this->collection->remove($this->condition, array('safe' => $this->safe)); return $this->collection->remove($this->condition, array('safe' => $this->safe));

57
tests/model/MongoDbCommandTest.php

@ -59,8 +59,8 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
public function testFindCommand() public function testFindCommand()
{ {
$collection = $this->driver->getCollection('items'); $collection = $this->driver->getCollection('items');
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND);
$cmd->bindParam('collection', $collection)->bindParam('condition', array('name' => 'bread'))->bindParam('fields', array());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $collection);
$cmd->bindParam('condition', array('name' => 'bread'))->bindParam('fields', array());
$result = $cmd->execute(); $result = $cmd->execute();
$this->assertEquals(0, $result->count()); $this->assertEquals(0, $result->count());
} }
@ -72,22 +72,22 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
public function testFindCommandNotAllParamsBinded() public function testFindCommandNotAllParamsBinded()
{ {
$collection = $this->driver->getCollection('items'); $collection = $this->driver->getCollection('items');
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND);
$cmd->bindParam('collection', $collection)->bindParam('condition', array('name' => 'bread'));
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $collection);
$cmd->bindParam('condition', array('name' => 'bread'));
$cmd->execute(); $cmd->execute();
} }
public function testInsertCommand() public function testInsertCommand()
{ {
$collection = $this->driver->getCollection('items'); $collection = $this->driver->getCollection('items');
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT);
$cmd->bindParam('collection', $collection)
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $collection);
$cmd
->bindParam('data', array('name' => 'insert')) ->bindParam('data', array('name' => 'insert'))
->bindParam('safe', true); ->bindParam('safe', true);
$this->assertArrayHasKey('n', $cmd->execute()); $this->assertArrayHasKey('n', $cmd->execute());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND);
$cmd->bindParam('collection', $collection)->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $collection);
$cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
$result = $cmd->execute(); $result = $cmd->execute();
$this->assertEquals(1, $result->count()); $this->assertEquals(1, $result->count());
} }
@ -99,33 +99,33 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
public function testInsertCommandNotAllParamsBinded() public function testInsertCommandNotAllParamsBinded()
{ {
$collection = $this->driver->getCollection('items'); $collection = $this->driver->getCollection('items');
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT);
$cmd->bindParam('collection', $collection)->bindParam('data', array('name' => 'bread'));
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $collection);
$cmd->bindParam('data', array('name' => 'bread'));
$cmd->execute(); $cmd->execute();
} }
public function testUpdateCommand() public function testUpdateCommand()
{ {
$collection = $this->driver->getCollection('items'); $collection = $this->driver->getCollection('items');
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT);
$cmd->bindParam('collection', $collection)
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $collection);
$cmd
->bindParam('data', array('name' => 'insert')) ->bindParam('data', array('name' => 'insert'))
->bindParam('safe', true); ->bindParam('safe', true);
$this->assertArrayHasKey('n', $cmd->execute()); $this->assertArrayHasKey('n', $cmd->execute());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE);
$cmd->bindParam('collection', $collection)
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $collection);
$cmd
->bindParam('condition', array('name' => 'insert')) ->bindParam('condition', array('name' => 'insert'))
->bindParam('data', array('$set' => array('name' => 'update'))) ->bindParam('data', array('$set' => array('name' => 'update')))
->bindParam('upsert', false) ->bindParam('upsert', false)
->bindParam('safe', true); ->bindParam('safe', true);
$this->assertArrayHasKey('n', $cmd->execute()); $this->assertArrayHasKey('n', $cmd->execute());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND);
$cmd->bindParam('collection', $collection)->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $collection);
$cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
$result = $cmd->execute(); $result = $cmd->execute();
$this->assertEquals(0, $result->count()); $this->assertEquals(0, $result->count());
$cmd->bindParam('collection', $collection)->bindParam('condition', array('name' => 'update'))->bindParam('fields', array());
$cmd->bindParam('condition', array('name' => 'update'))->bindParam('fields', array());
$result = $cmd->execute(); $result = $cmd->execute();
$this->assertEquals(1, $result->count()); $this->assertEquals(1, $result->count());
} }
@ -137,33 +137,33 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
public function testUpdateCommandNotAllParamsBinded() public function testUpdateCommandNotAllParamsBinded()
{ {
$collection = $this->driver->getCollection('items'); $collection = $this->driver->getCollection('items');
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE);
$cmd->bindParam('collection', $collection)->bindParam('data', array('name' => 'bread'));
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $collection);
$cmd->bindParam('data', array('name' => 'bread'));
$cmd->execute(); $cmd->execute();
} }
public function testRemoveCommand() public function testRemoveCommand()
{ {
$collection = $this->driver->getCollection('items'); $collection = $this->driver->getCollection('items');
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT);
$cmd->bindParam('collection', $collection)
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $collection);
$cmd
->bindParam('data', array('name' => 'insert')) ->bindParam('data', array('name' => 'insert'))
->bindParam('safe', true); ->bindParam('safe', true);
$this->assertArrayHasKey('n', $cmd->execute()); $this->assertArrayHasKey('n', $cmd->execute());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND);
$cmd->bindParam('collection', $collection)->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $collection);
$cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
$result = $cmd->execute(); $result = $cmd->execute();
$this->assertEquals(1, $result->count()); $this->assertEquals(1, $result->count());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE);
$cmd->bindParam('collection', $collection)
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $collection);
$cmd
->bindParam('condition', array('name' => 'insert')) ->bindParam('condition', array('name' => 'insert'))
->bindParam('safe', true); ->bindParam('safe', true);
$this->assertArrayHasKey('n', $cmd->execute()); $this->assertArrayHasKey('n', $cmd->execute());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND);
$cmd->bindParam('collection', $collection)->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $collection);
$cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
$result = $cmd->execute(); $result = $cmd->execute();
$this->assertEquals(0, $result->count()); $this->assertEquals(0, $result->count());
} }
@ -175,8 +175,7 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
public function testRemoveCommandNotAllParamsBinded() public function testRemoveCommandNotAllParamsBinded()
{ {
$collection = $this->driver->getCollection('items'); $collection = $this->driver->getCollection('items');
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE);
$cmd->bindParam('collection', $collection);
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $collection);
$cmd->execute(); $cmd->execute();
} }

Loading…
Cancel
Save