Browse Source

modified MongoCommand classes

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

40
model/MongoDbCommand.php

@ -18,15 +18,19 @@ class MongoCommandBuilder
const REMOVE = 'Remove';
static public function factory($type)
static public function factory($type, $collection = null)
{
$class = ucfirst($type) . 'MongoCommand';
return new $class();
$command = new $class();
$command->setCollection($collection);
return $command;
}
}
abstract class MongoDbCommand
{
protected $collection;
public function execute()
{
if ($this->checkParams()) {
@ -38,17 +42,29 @@ abstract class MongoDbCommand
public function bindParam($name, $value)
{
$this->$name = $value;
if (property_exists($this, $name)) {
$this->$name = $value;
}
return $this;
}
public function setCollection($collection)
{
$this->collection = $collection;
return $this;
}
abstract protected function concreteExecute();
abstract protected function concreteExecute();
abstract protected function checkParams();
}
class FindMongoCommand extends MongoDbCommand
{
protected $condition;
protected $fields;
protected function concreteExecute()
{
return $this->collection->find($this->condition, $this->fields);
@ -66,6 +82,10 @@ class FindMongoCommand extends MongoDbCommand
class InsertMongoCommand extends MongoDbCommand
{
protected $data;
protected $safe;
protected function concreteExecute()
{
return $this->collection->insert($this->data, array('safe' => $this->safe));
@ -83,6 +103,14 @@ class InsertMongoCommand extends MongoDbCommand
class UpdateMongoCommand extends MongoDbCommand
{
protected $condition;
protected $data;
protected $upsert;
protected $safe;
protected function concreteExecute()
{
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
{
protected $condition;
protected $safe;
protected function concreteExecute()
{
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()
{
$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();
$this->assertEquals(0, $result->count());
}
@ -72,22 +72,22 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
public function testFindCommandNotAllParamsBinded()
{
$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();
}
public function testInsertCommand()
{
$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('safe', true);
$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();
$this->assertEquals(1, $result->count());
}
@ -99,33 +99,33 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
public function testInsertCommandNotAllParamsBinded()
{
$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();
}
public function testUpdateCommand()
{
$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('safe', true);
$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('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());
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $collection);
$cmd->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());
$cmd->bindParam('condition', array('name' => 'update'))->bindParam('fields', array());
$result = $cmd->execute();
$this->assertEquals(1, $result->count());
}
@ -137,33 +137,33 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
public function testUpdateCommandNotAllParamsBinded()
{
$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();
}
public function testRemoveCommand()
{
$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('safe', true);
$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();
$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('safe', true);
$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();
$this->assertEquals(0, $result->count());
}
@ -175,8 +175,7 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
public function testRemoveCommandNotAllParamsBinded()
{
$collection = $this->driver->getCollection('items');
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE);
$cmd->bindParam('collection', $collection);
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $collection);
$cmd->execute();
}

Loading…
Cancel
Save