Browse Source

added __toString() for MongoDbCommands

master
Anton Grebnev 13 years ago
parent
commit
b9f7718063
  1. 123
      model/MongoDbCommand.php
  2. 85
      tests/model/MongoDbCommandTest.php

123
model/MongoDbCommand.php

@ -63,10 +63,7 @@ abstract class MongoDbCommand
/**
* @TODO: implement method in subclasses for Profiler
*/
public function __toString()
{
return get_called_class();
}
abstract public function __toString();
}
class FindMongoCommand extends MongoDbCommand
@ -79,7 +76,7 @@ class FindMongoCommand extends MongoDbCommand
protected function concreteExecute()
{
if($this->multiple) {
if ($this->multiple) {
return $this->collection->find($this->condition, $this->fields);
} else {
return $this->collection->findOne($this->condition, $this->fields);
@ -94,6 +91,26 @@ class FindMongoCommand extends MongoDbCommand
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
@ -124,6 +141,22 @@ class InsertMongoCommand extends MongoDbCommand
{
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
@ -154,6 +187,30 @@ class UpdateMongoCommand extends MongoDbCommand
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
@ -175,21 +232,37 @@ class RemoveMongoCommand extends MongoDbCommand
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
{
protected $command;
class CommandMongoCommand extends MongoDbCommand
{
protected $command;
protected function concreteExecute()
{
$db = $this->collection->db;
if($db instanceof MongoDB) {
return $db->command($this->command);
} else {
return false;
}
}
protected function concreteExecute()
{
$db = $this->collection->db;
if ($db instanceof MongoDB) {
return $db->command($this->command);
} else {
return false;
}
}
protected function checkParams()
{
@ -199,4 +272,18 @@ class RemoveMongoCommand extends MongoDbCommand
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';
}
}
}

85
tests/model/MongoDbCommandTest.php

@ -90,14 +90,11 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
$this->assertEquals('insert', $result['name']);
}
/**
* @expectedException GeneralException
* @expectedExceptionMessage FindMongoCommand error. Bind all required params first.
*/
public function testFindCommandNotAllParamsBinded()
{
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->collection);
$cmd->bindParam('condition', array('name' => 'bread'));
$this->setExpectedException('GeneralException', 'FindMongoCommand error. Bind all required params first');
$cmd->execute();
}
@ -120,13 +117,10 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
$this->assertEquals(1, $result->count());
}
/**
* @expectedException GeneralException
* @expectedExceptionMessage InsertMongoCommand error. Bind all required params first.
*/
public function testInsertCommandNotAllParamsBinded()
{
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
$this->setExpectedException('GeneralException', 'InsertMongoCommand error. Bind all required params first');
$cmd->execute();
}
@ -155,14 +149,11 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
$this->assertEquals(1, $result->count());
}
/**
* @expectedException GeneralException
* @expectedExceptionMessage UpdateMongoCommand error. Bind all required params first.
*/
public function testUpdateCommandNotAllParamsBinded()
{
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
$cmd->bindParam('data', array('name' => 'bread'));
$this->setExpectedException('GeneralException', 'UpdateMongoCommand error. Bind all required params first');
$cmd->execute();
}
@ -191,23 +182,17 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
$this->assertEquals(0, $result->count());
}
/**
* @expectedException GeneralException
* @expectedExceptionMessage RemoveMongoCommand error. Bind all required params first.
*/
public function testRemoveCommandNotAllParamsBinded()
{
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
$this->setExpectedException('GeneralException', 'RemoveMongoCommand error. Bind all required params first.');
$cmd->execute();
}
/**
* @expectedException GeneralException
* @expectedExceptionMessage CommandMongoCommand error. Bind all required params first.
*/
public function testCommandCommandNotAllParamsBinded()
{
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, $this->collection);
$this->setExpectedException('GeneralException', 'CommandMongoCommand error. Bind all required params first');
$cmd->execute();
}
@ -217,9 +202,69 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
$cmd->bindParam('command', array());
$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
{
public $db = array();
public function __toString()
{
return 'CollectionMock';
}
}
Loading…
Cancel
Save