added __toString() for MongoDbCommands
This commit is contained in:
@ -63,10 +63,7 @@ abstract class MongoDbCommand
|
|||||||
/**
|
/**
|
||||||
* @TODO: implement method in subclasses for Profiler
|
* @TODO: implement method in subclasses for Profiler
|
||||||
*/
|
*/
|
||||||
public function __toString()
|
abstract public function __toString();
|
||||||
{
|
|
||||||
return get_called_class();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class FindMongoCommand extends MongoDbCommand
|
class FindMongoCommand extends MongoDbCommand
|
||||||
@ -79,7 +76,7 @@ class FindMongoCommand extends MongoDbCommand
|
|||||||
|
|
||||||
protected function concreteExecute()
|
protected function concreteExecute()
|
||||||
{
|
{
|
||||||
if($this->multiple) {
|
if ($this->multiple) {
|
||||||
return $this->collection->find($this->condition, $this->fields);
|
return $this->collection->find($this->condition, $this->fields);
|
||||||
} else {
|
} else {
|
||||||
return $this->collection->findOne($this->condition, $this->fields);
|
return $this->collection->findOne($this->condition, $this->fields);
|
||||||
@ -94,6 +91,26 @@ class FindMongoCommand extends MongoDbCommand
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
if ($this->checkParams()) {
|
||||||
|
$result = 'Collection: ' . $this->collection . PHP_EOL;
|
||||||
|
ob_start();
|
||||||
|
var_dump($this->condition);
|
||||||
|
$condition = ob_get_clean();
|
||||||
|
$result .= 'Condition: ' . $condition . PHP_EOL;
|
||||||
|
ob_start();
|
||||||
|
var_dump($this->fields);
|
||||||
|
$fields = ob_get_clean();
|
||||||
|
$result .= 'Fields: ' . $fields . PHP_EOL;
|
||||||
|
$mult = $this->multiple ? 'TRUE' : 'FALSE';
|
||||||
|
$result .= 'Multiple fields: ' . $mult . PHP_EOL;
|
||||||
|
return $result;
|
||||||
|
} else {
|
||||||
|
return 'Command properties not set';
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class InsertMongoCommand extends MongoDbCommand
|
class InsertMongoCommand extends MongoDbCommand
|
||||||
@ -124,6 +141,22 @@ class InsertMongoCommand extends MongoDbCommand
|
|||||||
{
|
{
|
||||||
return $this->insertId;
|
return $this->insertId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
if ($this->checkParams()) {
|
||||||
|
$result = 'Collection: ' . $this->collection . PHP_EOL;
|
||||||
|
ob_start();
|
||||||
|
var_dump($this->data);
|
||||||
|
$data = ob_get_clean();
|
||||||
|
$result .= 'Data: ' . $data . PHP_EOL;
|
||||||
|
$safe = $this->safe ? 'TRUE' : 'FALSE';
|
||||||
|
$result .= 'Safe operation: ' . $safe . PHP_EOL;
|
||||||
|
return $result;
|
||||||
|
} else {
|
||||||
|
return 'Command properties not set';
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class UpdateMongoCommand extends MongoDbCommand
|
class UpdateMongoCommand extends MongoDbCommand
|
||||||
@ -154,6 +187,30 @@ class UpdateMongoCommand extends MongoDbCommand
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
if ($this->checkParams()) {
|
||||||
|
$result = 'Collection: ' . $this->collection . PHP_EOL;
|
||||||
|
ob_start();
|
||||||
|
var_dump($this->condition);
|
||||||
|
$condition = ob_get_clean();
|
||||||
|
$result .= 'Condition: ' . $condition . PHP_EOL;
|
||||||
|
ob_start();
|
||||||
|
var_dump($this->data);
|
||||||
|
$data = ob_get_clean();
|
||||||
|
$result .= 'Data: ' . $data . PHP_EOL;
|
||||||
|
$mult = $this->multiple ? 'TRUE' : 'FALSE';
|
||||||
|
$result .= 'Multiple fields: ' . $mult . PHP_EOL;
|
||||||
|
$upsert = $this->upsert ? 'TRUE' : 'FALSE';
|
||||||
|
$result .= 'Upsert: ' . $upsert . PHP_EOL;
|
||||||
|
$safe = $this->safe ? 'TRUE' : 'FALSE';
|
||||||
|
$result .= 'Safe operation: ' . $safe . PHP_EOL;
|
||||||
|
return $result;
|
||||||
|
} else {
|
||||||
|
return 'Command properties not set';
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class RemoveMongoCommand extends MongoDbCommand
|
class RemoveMongoCommand extends MongoDbCommand
|
||||||
@ -175,21 +232,37 @@ class RemoveMongoCommand extends MongoDbCommand
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
if ($this->checkParams()) {
|
||||||
|
$result = 'Collection: ' . $this->collection . PHP_EOL;
|
||||||
|
ob_start();
|
||||||
|
var_dump($this->condition);
|
||||||
|
$condition = ob_get_clean();
|
||||||
|
$result .= 'Condition: ' . $condition . PHP_EOL;
|
||||||
|
$safe = $this->safe ? 'TRUE' : 'FALSE';
|
||||||
|
$result .= 'Safe operation: ' . $safe . PHP_EOL;
|
||||||
|
return $result;
|
||||||
|
} else {
|
||||||
|
return 'Command properties not set';
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class CommandMongoCommand extends MongoDbCommand
|
class CommandMongoCommand extends MongoDbCommand
|
||||||
{
|
{
|
||||||
protected $command;
|
protected $command;
|
||||||
|
|
||||||
protected function concreteExecute()
|
protected function concreteExecute()
|
||||||
{
|
{
|
||||||
$db = $this->collection->db;
|
$db = $this->collection->db;
|
||||||
if($db instanceof MongoDB) {
|
if ($db instanceof MongoDB) {
|
||||||
return $db->command($this->command);
|
return $db->command($this->command);
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function checkParams()
|
protected function checkParams()
|
||||||
{
|
{
|
||||||
@ -199,4 +272,18 @@ class RemoveMongoCommand extends MongoDbCommand
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
if ($this->checkParams()) {
|
||||||
|
$result = 'Collection: ' . $this->collection . PHP_EOL;
|
||||||
|
ob_start();
|
||||||
|
var_dump($this->command);
|
||||||
|
$command = ob_get_clean();
|
||||||
|
$result .= 'Command: ' . $command . PHP_EOL;
|
||||||
|
return $result;
|
||||||
|
} else {
|
||||||
|
return 'Command properties not set';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -90,14 +90,11 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('insert', $result['name']);
|
$this->assertEquals('insert', $result['name']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException GeneralException
|
|
||||||
* @expectedExceptionMessage FindMongoCommand error. Bind all required params first.
|
|
||||||
*/
|
|
||||||
public function testFindCommandNotAllParamsBinded()
|
public function testFindCommandNotAllParamsBinded()
|
||||||
{
|
{
|
||||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
|
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
|
||||||
$cmd->bindParam('condition', array('name' => 'bread'));
|
$cmd->bindParam('condition', array('name' => 'bread'));
|
||||||
|
$this->setExpectedException('GeneralException', 'FindMongoCommand error. Bind all required params first');
|
||||||
$cmd->execute();
|
$cmd->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,13 +117,10 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals(1, $result->count());
|
$this->assertEquals(1, $result->count());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException GeneralException
|
|
||||||
* @expectedExceptionMessage InsertMongoCommand error. Bind all required params first.
|
|
||||||
*/
|
|
||||||
public function testInsertCommandNotAllParamsBinded()
|
public function testInsertCommandNotAllParamsBinded()
|
||||||
{
|
{
|
||||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
|
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
|
||||||
|
$this->setExpectedException('GeneralException', 'InsertMongoCommand error. Bind all required params first');
|
||||||
$cmd->execute();
|
$cmd->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,14 +149,11 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals(1, $result->count());
|
$this->assertEquals(1, $result->count());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException GeneralException
|
|
||||||
* @expectedExceptionMessage UpdateMongoCommand error. Bind all required params first.
|
|
||||||
*/
|
|
||||||
public function testUpdateCommandNotAllParamsBinded()
|
public function testUpdateCommandNotAllParamsBinded()
|
||||||
{
|
{
|
||||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
|
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
|
||||||
$cmd->bindParam('data', array('name' => 'bread'));
|
$cmd->bindParam('data', array('name' => 'bread'));
|
||||||
|
$this->setExpectedException('GeneralException', 'UpdateMongoCommand error. Bind all required params first');
|
||||||
$cmd->execute();
|
$cmd->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,23 +182,17 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals(0, $result->count());
|
$this->assertEquals(0, $result->count());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException GeneralException
|
|
||||||
* @expectedExceptionMessage RemoveMongoCommand error. Bind all required params first.
|
|
||||||
*/
|
|
||||||
public function testRemoveCommandNotAllParamsBinded()
|
public function testRemoveCommandNotAllParamsBinded()
|
||||||
{
|
{
|
||||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
|
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
|
||||||
|
$this->setExpectedException('GeneralException', 'RemoveMongoCommand error. Bind all required params first.');
|
||||||
$cmd->execute();
|
$cmd->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException GeneralException
|
|
||||||
* @expectedExceptionMessage CommandMongoCommand error. Bind all required params first.
|
|
||||||
*/
|
|
||||||
public function testCommandCommandNotAllParamsBinded()
|
public function testCommandCommandNotAllParamsBinded()
|
||||||
{
|
{
|
||||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, $this->collection);
|
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, $this->collection);
|
||||||
|
$this->setExpectedException('GeneralException', 'CommandMongoCommand error. Bind all required params first');
|
||||||
$cmd->execute();
|
$cmd->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -217,9 +202,69 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
|
|||||||
$cmd->bindParam('command', array());
|
$cmd->bindParam('command', array());
|
||||||
$this->assertFalse($cmd->execute());
|
$this->assertFalse($cmd->execute());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testCommandCommand()
|
||||||
|
{
|
||||||
|
$col = new CollectionMock();
|
||||||
|
$col->db = $this->getMock('MongoDb', array('command'), array(), 'SomeMongoMock', false);
|
||||||
|
$col->db
|
||||||
|
->expects($this->once())
|
||||||
|
->method('command')
|
||||||
|
->will($this->returnValue(true));
|
||||||
|
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, $col);
|
||||||
|
$cmd->bindParam('command', array());
|
||||||
|
$this->assertTrue($cmd->execute());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToStringParamsNotSet()
|
||||||
|
{
|
||||||
|
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, new CollectionMock());
|
||||||
|
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToString()
|
||||||
|
{
|
||||||
|
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, new CollectionMock());
|
||||||
|
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||||
|
$cmd->bindParam('command', array());
|
||||||
|
$this->assertStringStartsWith('Collection: CollectionMock', $cmd->__toString());
|
||||||
|
|
||||||
|
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
|
||||||
|
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||||
|
$cmd
|
||||||
|
->bindParam('data', array('name' => 'insert'))
|
||||||
|
->bindParam('safe', true);
|
||||||
|
$this->assertStringStartsWith('Collection: ', $cmd->__toString());
|
||||||
|
|
||||||
|
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
|
||||||
|
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||||
|
$cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
|
||||||
|
$this->assertStringStartsWith('Collection: ', $cmd->__toString());
|
||||||
|
|
||||||
|
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
|
||||||
|
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||||
|
$cmd
|
||||||
|
->bindParam('condition', array('name' => 'insert'))
|
||||||
|
->bindParam('safe', true);
|
||||||
|
$this->assertStringStartsWith('Collection: ', $cmd->__toString());
|
||||||
|
|
||||||
|
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
|
||||||
|
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||||
|
$cmd
|
||||||
|
->bindParam('condition', array('name' => 'insert'))
|
||||||
|
->bindParam('data', array('$set' => array('name' => 'update')))
|
||||||
|
->bindParam('upsert', false)
|
||||||
|
->bindParam('safe', true);
|
||||||
|
$this->assertStringStartsWith('Collection: ', $cmd->__toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class CollectionMock
|
class CollectionMock
|
||||||
{
|
{
|
||||||
public $db = array();
|
public $db = array();
|
||||||
|
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
return 'CollectionMock';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user