merged mongo into master
This commit is contained in:
@ -58,14 +58,22 @@ abstract class MongoDbCommand
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function arrayToString($array)
|
||||||
|
{
|
||||||
|
$string = '[';
|
||||||
|
if(!empty($array)) {
|
||||||
|
$string .= PHP_EOL;
|
||||||
|
}
|
||||||
|
foreach($array as $key => $value) {
|
||||||
|
$string .= "\t" . $key . ' = ' . $value . PHP_EOL;
|
||||||
|
}
|
||||||
|
$string .= ']' . PHP_EOL;
|
||||||
|
return $string;
|
||||||
|
}
|
||||||
|
|
||||||
abstract protected function concreteExecute();
|
abstract protected function concreteExecute();
|
||||||
|
|
||||||
abstract protected function checkParams();
|
abstract protected function checkParams();
|
||||||
|
|
||||||
/**
|
|
||||||
* @TODO: implement method in subclasses for Profiler
|
|
||||||
*/
|
|
||||||
abstract public function __toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class FindMongoCommand extends MongoDbCommand
|
class FindMongoCommand extends MongoDbCommand
|
||||||
@ -98,15 +106,9 @@ class FindMongoCommand extends MongoDbCommand
|
|||||||
{
|
{
|
||||||
if ($this->checkParams()) {
|
if ($this->checkParams()) {
|
||||||
$result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
|
$result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
|
||||||
ob_start();
|
$result .= 'Condition: ' . $this->arrayToString($this->condition);
|
||||||
var_dump($this->condition);
|
|
||||||
$condition = ob_get_clean();
|
|
||||||
$result .= 'Condition: ' . $condition;
|
|
||||||
$result .= 'Type: FIND' . PHP_EOL;
|
$result .= 'Type: FIND' . PHP_EOL;
|
||||||
ob_start();
|
$result .= 'Fields: ' . $this->arrayToString($this->fields);
|
||||||
var_dump($this->fields);
|
|
||||||
$fields = ob_get_clean();
|
|
||||||
$result .= 'Fields: ' . $fields . PHP_EOL;
|
|
||||||
$mult = $this->multiple ? 'TRUE' : 'FALSE';
|
$mult = $this->multiple ? 'TRUE' : 'FALSE';
|
||||||
$result .= 'Multiple fields: ' . $mult . PHP_EOL;
|
$result .= 'Multiple fields: ' . $mult . PHP_EOL;
|
||||||
return $result;
|
return $result;
|
||||||
@ -145,10 +147,7 @@ class CountMongoCommand extends MongoDbCommand
|
|||||||
if ($this->checkParams()) {
|
if ($this->checkParams()) {
|
||||||
$result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
|
$result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
|
||||||
$result .= 'Type: COUNT' . PHP_EOL;
|
$result .= 'Type: COUNT' . PHP_EOL;
|
||||||
ob_start();
|
$result .= 'Condition: ' . $this->arrayToString($this->condition);
|
||||||
var_dump($this->condition);
|
|
||||||
$condition = ob_get_clean();
|
|
||||||
$result .= 'Condition: ' . $condition;
|
|
||||||
$result .= 'Limit: ' . $this->limit . PHP_EOL;
|
$result .= 'Limit: ' . $this->limit . PHP_EOL;
|
||||||
$result .= 'Skip: ' . $this->skip . PHP_EOL;
|
$result .= 'Skip: ' . $this->skip . PHP_EOL;
|
||||||
return $result;
|
return $result;
|
||||||
@ -201,10 +200,7 @@ class InsertMongoCommand extends MongoDbCommand
|
|||||||
if ($this->checkParams()) {
|
if ($this->checkParams()) {
|
||||||
$result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
|
$result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
|
||||||
$result .= 'Type: INSERT' . PHP_EOL;
|
$result .= 'Type: INSERT' . PHP_EOL;
|
||||||
ob_start();
|
$result .= 'Data: ' . $this->arrayToString($this->data);
|
||||||
var_dump($this->data);
|
|
||||||
$data = ob_get_clean();
|
|
||||||
$result .= 'Data: ' . $data;
|
|
||||||
$mult = $this->multiple ? 'TRUE' : 'FALSE';
|
$mult = $this->multiple ? 'TRUE' : 'FALSE';
|
||||||
$result .= 'Bulk insert: ' . $mult . PHP_EOL;
|
$result .= 'Bulk insert: ' . $mult . PHP_EOL;
|
||||||
$safe = $this->safe ? 'TRUE' : 'FALSE';
|
$safe = $this->safe ? 'TRUE' : 'FALSE';
|
||||||
@ -250,14 +246,8 @@ class UpdateMongoCommand extends MongoDbCommand
|
|||||||
if ($this->checkParams()) {
|
if ($this->checkParams()) {
|
||||||
$result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
|
$result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
|
||||||
$result .= 'Type: UPDATE' . PHP_EOL;
|
$result .= 'Type: UPDATE' . PHP_EOL;
|
||||||
ob_start();
|
$result .= 'Condition: ' . $this->arrayToString($this->condition);
|
||||||
var_dump($this->condition);
|
$result .= 'Data: ' . $this->arrayToString($this->data);
|
||||||
$condition = ob_get_clean();
|
|
||||||
$result .= 'Condition: ' . $condition;
|
|
||||||
ob_start();
|
|
||||||
var_dump($this->data);
|
|
||||||
$data = ob_get_clean();
|
|
||||||
$result .= 'Data: ' . $data;
|
|
||||||
$mult = $this->multiple ? 'TRUE' : 'FALSE';
|
$mult = $this->multiple ? 'TRUE' : 'FALSE';
|
||||||
$result .= 'Multiple fields: ' . $mult . PHP_EOL;
|
$result .= 'Multiple fields: ' . $mult . PHP_EOL;
|
||||||
$upsert = $this->upsert ? 'TRUE' : 'FALSE';
|
$upsert = $this->upsert ? 'TRUE' : 'FALSE';
|
||||||
@ -296,10 +286,7 @@ class RemoveMongoCommand extends MongoDbCommand
|
|||||||
if ($this->checkParams()) {
|
if ($this->checkParams()) {
|
||||||
$result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
|
$result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
|
||||||
$result .= 'Type: REMOVE' . PHP_EOL;
|
$result .= 'Type: REMOVE' . PHP_EOL;
|
||||||
ob_start();
|
$result .= 'Condition: ' . $this->arrayToString($this->condition);
|
||||||
var_dump($this->condition);
|
|
||||||
$condition = ob_get_clean();
|
|
||||||
$result .= 'Condition: ' . $condition;
|
|
||||||
$safe = $this->safe ? 'TRUE' : 'FALSE';
|
$safe = $this->safe ? 'TRUE' : 'FALSE';
|
||||||
$result .= 'Safe operation: ' . $safe . PHP_EOL;
|
$result .= 'Safe operation: ' . $safe . PHP_EOL;
|
||||||
return $result;
|
return $result;
|
||||||
@ -337,10 +324,7 @@ class CommandMongoCommand extends MongoDbCommand
|
|||||||
if ($this->checkParams()) {
|
if ($this->checkParams()) {
|
||||||
$result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
|
$result = PHP_EOL . 'Collection: ' . trim($this->collection, "\n") . PHP_EOL;
|
||||||
$result .= 'Type: COMMAND' . PHP_EOL;
|
$result .= 'Type: COMMAND' . PHP_EOL;
|
||||||
ob_start();
|
$result .= 'Command: ' . $this->arrayToString($this->command);
|
||||||
var_dump($this->command);
|
|
||||||
$command = ob_get_clean();
|
|
||||||
$result .= 'Command: ' . $command;
|
|
||||||
return $result;
|
return $result;
|
||||||
} else {
|
} else {
|
||||||
return 'Command properties not set';
|
return 'Command properties not set';
|
||||||
|
@ -60,8 +60,10 @@ class MongoStatement extends DbStatement
|
|||||||
if (!$this->result) {
|
if (!$this->result) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (is_array($this->result) && isset($this->result['retval'])) {
|
||||||
|
return $this->result['retval'];
|
||||||
|
}
|
||||||
|
|
||||||
$row = false;
|
|
||||||
switch ($style) {
|
switch ($style) {
|
||||||
case Db::FETCH_OBJ:
|
case Db::FETCH_OBJ:
|
||||||
$row = $this->fetchObject();
|
$row = $this->fetchObject();
|
||||||
@ -133,6 +135,7 @@ class MongoStatement extends DbStatement
|
|||||||
*/
|
*/
|
||||||
protected function driverExecute($request)
|
protected function driverExecute($request)
|
||||||
{
|
{
|
||||||
|
$this->result = false;
|
||||||
$mongo = $this->driver->getConnection();
|
$mongo = $this->driver->getConnection();
|
||||||
if ($mongo instanceof Mongo) {
|
if ($mongo instanceof Mongo) {
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
@ -179,4 +182,4 @@ class MongoStatement extends DbStatement
|
|||||||
{
|
{
|
||||||
return $this->insertId;
|
return $this->insertId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @copyright NetMonsters <team@netmonsters.ru>
|
* @copyright NetMonsters <team@netmonsters.ru>
|
||||||
* @link http://netmonsters.ru
|
* @link http://netmonsters.ru
|
||||||
* @package Majestic
|
* @package Majestic
|
||||||
* @subpackage UnitTests
|
* @subpackage UnitTests
|
||||||
* @since 2011-11-10
|
* @since 2011-11-10
|
||||||
*
|
*
|
||||||
* Unit tests for MongoDriver class
|
* Unit tests for MongoDriver class
|
||||||
*/
|
*/
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/../../model/DbDriver.php';
|
require_once dirname(__FILE__) . '/../../model/DbDriver.php';
|
||||||
require_once dirname(__FILE__) . '/../../model/NoSqlDbDriver.php';
|
require_once dirname(__FILE__) . '/../../model/NoSqlDbDriver.php';
|
||||||
@ -110,7 +110,6 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals(0, $count_result);
|
$this->assertEquals(0, $count_result);
|
||||||
$this->assertEquals($count_result, $find_result->count());
|
$this->assertEquals($count_result, $find_result->count());
|
||||||
|
|
||||||
|
|
||||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
|
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
|
||||||
$cmd
|
$cmd
|
||||||
->bindParam('data', array('name' => 'insert'))
|
->bindParam('data', array('name' => 'insert'))
|
||||||
@ -126,7 +125,6 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals(2, $count_cmd->execute());
|
$this->assertEquals(2, $count_cmd->execute());
|
||||||
$find_cmd->bindParam('condition', array('name' => 'insert'));
|
$find_cmd->bindParam('condition', array('name' => 'insert'));
|
||||||
$this->assertEquals($find_cmd->execute()->count(), $count_cmd->execute());
|
$this->assertEquals($find_cmd->execute()->count(), $count_cmd->execute());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -186,8 +184,6 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
|
|||||||
$cmd->bindParam('condition', array('name' => 'equal object'))->bindParam('fields', array());
|
$cmd->bindParam('condition', array('name' => 'equal object'))->bindParam('fields', array());
|
||||||
$result = $cmd->execute();
|
$result = $cmd->execute();
|
||||||
$this->assertEquals(2, $result->count());
|
$this->assertEquals(2, $result->count());
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -329,24 +325,26 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COUNT, new CollectionMock());
|
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COUNT, new CollectionMock());
|
||||||
$cmd->bindParam('condition', array());
|
$cmd->bindParam('condition', array());
|
||||||
$this->assertStringStartsWith(PHP_EOL . 'Collection: CollectionMock', $cmd->__toString());
|
$this->assertStringStartsWith("\n" . 'Collection: CollectionMock', $cmd->__toString());
|
||||||
|
|
||||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, new CollectionMock());
|
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, new CollectionMock());
|
||||||
$cmd->bindParam('condition', array());
|
$cmd->bindParam('condition', array());
|
||||||
$this->assertStringStartsWith(PHP_EOL . 'Collection: CollectionMock', $cmd->__toString());
|
$this->assertStringStartsWith("\n" . 'Collection: CollectionMock', $cmd->__toString());
|
||||||
|
$this->assertContains('Condition: ' . '[]' . PHP_EOL, $cmd->__toString());
|
||||||
|
|
||||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, new CollectionMock());
|
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, new CollectionMock());
|
||||||
$this->assertSame('Command properties not set', $cmd->__toString());
|
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||||
$cmd->bindParam('command', array());
|
$cmd->bindParam('command', array());
|
||||||
$this->assertStringStartsWith(PHP_EOL . 'Collection: CollectionMock', $cmd->__toString());
|
$this->assertStringStartsWith("\n" . 'Collection: CollectionMock', $cmd->__toString());
|
||||||
|
|
||||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
|
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
|
||||||
$this->assertSame('Command properties not set', $cmd->__toString());
|
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||||
$cmd
|
$cmd
|
||||||
->bindParam('data', array('name' => 'insert'))
|
->bindParam('data', array('name' => 'insert'))
|
||||||
->bindParam('safe', true);
|
->bindParam('safe', true);
|
||||||
$this->assertStringStartsWith(PHP_EOL . 'Collection: ', $cmd->__toString());
|
$this->assertStringStartsWith("\n" . 'Collection: ', $cmd->__toString());
|
||||||
$this->assertContains('Bulk insert: FALSE', $cmd->__toString());
|
$this->assertContains('Bulk insert: FALSE', $cmd->__toString());
|
||||||
|
$this->assertContains('Data: ' . '[' . PHP_EOL . "\tname = insert" . PHP_EOL . ']' . PHP_EOL, $cmd->__toString());
|
||||||
|
|
||||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
|
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection);
|
||||||
$this->assertSame('Command properties not set', $cmd->__toString());
|
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||||
@ -357,14 +355,14 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertContains('Bulk insert: TRUE', $cmd->__toString());
|
$this->assertContains('Bulk insert: TRUE', $cmd->__toString());
|
||||||
|
|
||||||
$cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
|
$cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array());
|
||||||
$this->assertStringStartsWith(PHP_EOL . 'Collection: ', $cmd->__toString());
|
$this->assertStringStartsWith("\n" . 'Collection: ', $cmd->__toString());
|
||||||
|
|
||||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
|
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::REMOVE, $this->collection);
|
||||||
$this->assertSame('Command properties not set', $cmd->__toString());
|
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||||
$cmd
|
$cmd
|
||||||
->bindParam('condition', array('name' => 'insert'))
|
->bindParam('condition', array('name' => 'insert'))
|
||||||
->bindParam('safe', true);
|
->bindParam('safe', true);
|
||||||
$this->assertStringStartsWith(PHP_EOL . 'Collection: ', $cmd->__toString());
|
$this->assertStringStartsWith("\n" . 'Collection: ', $cmd->__toString());
|
||||||
|
|
||||||
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
|
$cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection);
|
||||||
$this->assertSame('Command properties not set', $cmd->__toString());
|
$this->assertSame('Command properties not set', $cmd->__toString());
|
||||||
@ -373,7 +371,7 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase
|
|||||||
->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->assertStringStartsWith(PHP_EOL . 'Collection: ', $cmd->__toString());
|
$this->assertStringStartsWith("\n" . 'Collection: ', $cmd->__toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -369,6 +369,59 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase
|
|||||||
* @runInSeparateProcess
|
* @runInSeparateProcess
|
||||||
* @group Mongo
|
* @group Mongo
|
||||||
*/
|
*/
|
||||||
|
public function testFindAndModifyNoItem()
|
||||||
|
{
|
||||||
|
if (!defined('DEBUG')) {
|
||||||
|
define('DEBUG', false);
|
||||||
|
}
|
||||||
|
|
||||||
|
$mongo = new MongoDriver($this->conf);
|
||||||
|
|
||||||
|
$this->assertEquals(10, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity);
|
||||||
|
$result = $mongo->findAndModify('items', array('name' => 'breading'), array('$set' => array('quantity' => 20)))->fetch();
|
||||||
|
$this->assertFalse($result);
|
||||||
|
$this->assertEquals(10, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess
|
||||||
|
* @group Mongo
|
||||||
|
*/
|
||||||
|
public function testEvalCommand()
|
||||||
|
{
|
||||||
|
if (!defined('DEBUG')) {
|
||||||
|
define('DEBUG', false);
|
||||||
|
}
|
||||||
|
$mongo = new MongoDriver($this->conf);
|
||||||
|
$result = $mongo->command('items', array('$eval' => 'function() { return db.items.count();}'));
|
||||||
|
$this->assertEquals(5, $result->fetch());
|
||||||
|
$this->assertEquals(5, $mongo->count('items'));
|
||||||
|
$result = $mongo->command('items', array('$eval' => 'function() { return "HELLO!";}'));
|
||||||
|
$this->assertEquals("HELLO!", $result->fetch());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess
|
||||||
|
* @group Mongo
|
||||||
|
*/
|
||||||
|
public function testEval()
|
||||||
|
{
|
||||||
|
if (!defined('DEBUG')) {
|
||||||
|
define('DEBUG', false);
|
||||||
|
}
|
||||||
|
$mongo = new MongoDriver($this->conf);
|
||||||
|
$result = $mongo->command('items', array('$eval' => 'function() {return true; }'));
|
||||||
|
$this->assertTrue($result->fetch());
|
||||||
|
$result = $mongo->command('items', array('$eval' => 'function() {return "Hello"; }'));
|
||||||
|
$this->assertSame('Hello', $result->fetch());
|
||||||
|
$result = $mongo->command('items', array('$eval' => 'function() {return db.items.count(); }'));
|
||||||
|
$this->assertEquals(5, $result->fetch());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @runInSeparateProcess
|
||||||
|
* @group Mongo
|
||||||
|
*/
|
||||||
public function testCommand()
|
public function testCommand()
|
||||||
{
|
{
|
||||||
if (!defined('DEBUG')) {
|
if (!defined('DEBUG')) {
|
||||||
|
@ -245,10 +245,10 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->request
|
$this->request
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
->method('execute')
|
->method('execute')
|
||||||
->will($this->returnValue(array('some' => 'val')));
|
->will($this->returnValue(array('retval' => 'val')));
|
||||||
|
|
||||||
$this->stmt->execute();
|
$this->stmt->execute();
|
||||||
$this->assertFalse($this->stmt->fetch());
|
$this->assertEquals('val', $this->stmt->fetch());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user