From a950ff688f16574b6166b3497eadf4d7e1361fa1 Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Thu, 12 Apr 2012 21:01:54 +0400 Subject: [PATCH 1/4] updated MongoStatement to return retval on db.eval --- model/MongoStatement.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/model/MongoStatement.php b/model/MongoStatement.php index 3e43f0a..1e86908 100644 --- a/model/MongoStatement.php +++ b/model/MongoStatement.php @@ -60,6 +60,9 @@ class MongoStatement extends DbStatement if (!$this->result) { return false; } + if (isset($this->result['retval'])) { + return $this->result['retval']; + } $row = false; switch ($style) { @@ -112,6 +115,8 @@ class MongoStatement extends DbStatement } else { return false; } + } elseif (is_int($this->result)) { + return $this->result; } return false; } @@ -151,6 +156,8 @@ class MongoStatement extends DbStatement if (is_array($result) && isset($result['values'])) { $this->result = $result['values']; } + } elseif (is_int($result)) { + $this->result = $result; } if ($request instanceof InsertMongoCommand) { $this->insertId = $request->getInsertId(); From df491fdbbd341c8bdace973868c331bb01baca5c Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Fri, 13 Apr 2012 16:22:52 +0400 Subject: [PATCH 2/4] fixed ret value on eval --- model/MongoStatement.php | 3 +-- tests/model/MongoDbCommandTest.php | 14 +++++++------- tests/model/MongoDriverTest.php | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/model/MongoStatement.php b/model/MongoStatement.php index 1e86908..6097c10 100644 --- a/model/MongoStatement.php +++ b/model/MongoStatement.php @@ -60,11 +60,10 @@ class MongoStatement extends DbStatement if (!$this->result) { return false; } - if (isset($this->result['retval'])) { + if (is_array($this->result) && isset($this->result['retval'])) { return $this->result['retval']; } - $row = false; switch ($style) { case Db::FETCH_OBJ: $row = $this->fetchObject(); diff --git a/tests/model/MongoDbCommandTest.php b/tests/model/MongoDbCommandTest.php index 69fe5b5..c8d01fd 100644 --- a/tests/model/MongoDbCommandTest.php +++ b/tests/model/MongoDbCommandTest.php @@ -329,23 +329,23 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase { $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COUNT, new CollectionMock()); $cmd->bindParam('condition', array()); - $this->assertStringStartsWith('Collection: CollectionMock', $cmd->__toString()); + $this->assertStringStartsWith("\n" . 'Collection: CollectionMock', $cmd->__toString()); $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, new CollectionMock()); $cmd->bindParam('condition', array()); - $this->assertStringStartsWith('Collection: CollectionMock', $cmd->__toString()); + $this->assertStringStartsWith("\n" . 'Collection: CollectionMock', $cmd->__toString()); $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()); + $this->assertStringStartsWith("\n" . '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()); + $this->assertStringStartsWith("\n" . 'Collection: ', $cmd->__toString()); $this->assertContains('Bulk insert: FALSE', $cmd->__toString()); $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::INSERT, $this->collection); @@ -357,14 +357,14 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase $this->assertContains('Bulk insert: TRUE', $cmd->__toString()); $cmd->bindParam('condition', array('name' => 'insert'))->bindParam('fields', array()); - $this->assertStringStartsWith('Collection: ', $cmd->__toString()); + $this->assertStringStartsWith("\n" . '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()); + $this->assertStringStartsWith("\n" . 'Collection: ', $cmd->__toString()); $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::UPDATE, $this->collection); $this->assertSame('Command properties not set', $cmd->__toString()); @@ -373,7 +373,7 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase ->bindParam('data', array('$set' => array('name' => 'update'))) ->bindParam('upsert', false) ->bindParam('safe', true); - $this->assertStringStartsWith('Collection: ', $cmd->__toString()); + $this->assertStringStartsWith("\n" . 'Collection: ', $cmd->__toString()); } } diff --git a/tests/model/MongoDriverTest.php b/tests/model/MongoDriverTest.php index 4e4d3dd..e11bb2a 100644 --- a/tests/model/MongoDriverTest.php +++ b/tests/model/MongoDriverTest.php @@ -369,6 +369,24 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase * @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() { if (!defined('DEBUG')) { From c0fa542b3cb475d7397a78a198fb35f2f143ae5f Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Wed, 23 May 2012 18:59:20 +0400 Subject: [PATCH 3/4] removed output buffer(ob_start/ob_end_clean) usasge in MongoDbCommand profile output --- model/MongoDbCommand.php | 58 ++++++++++++++------------------------ tests/model/MongoDbCommandTest.php | 2 ++ 2 files changed, 23 insertions(+), 37 deletions(-) diff --git a/model/MongoDbCommand.php b/model/MongoDbCommand.php index c82c430..c746e71 100644 --- a/model/MongoDbCommand.php +++ b/model/MongoDbCommand.php @@ -58,14 +58,22 @@ abstract class MongoDbCommand 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 checkParams(); - - /** - * @TODO: implement method in subclasses for Profiler - */ - abstract public function __toString(); } class FindMongoCommand extends MongoDbCommand @@ -98,15 +106,9 @@ class FindMongoCommand extends MongoDbCommand { if ($this->checkParams()) { $result = PHP_EOL . 'Collection: ' . $this->collection . PHP_EOL; - ob_start(); - var_dump($this->condition); - $condition = ob_get_clean(); - $result .= 'Condition: ' . $condition; + $result .= 'Condition: ' . $this->arrayToString($this->condition); $result .= 'Type: FIND' . PHP_EOL; - ob_start(); - var_dump($this->fields); - $fields = ob_get_clean(); - $result .= 'Fields: ' . $fields . PHP_EOL; + $result .= 'Fields: ' . $this->arrayToString($this->fields); $mult = $this->multiple ? 'TRUE' : 'FALSE'; $result .= 'Multiple fields: ' . $mult . PHP_EOL; return $result; @@ -145,10 +147,7 @@ class CountMongoCommand extends MongoDbCommand if ($this->checkParams()) { $result = PHP_EOL . 'Collection: ' . $this->collection . PHP_EOL; $result .= 'Type: COUNT' . PHP_EOL; - ob_start(); - var_dump($this->condition); - $condition = ob_get_clean(); - $result .= 'Condition: ' . $condition; + $result .= 'Condition: ' . $this->arrayToString($this->condition); $result .= 'Limit: ' . $this->limit . PHP_EOL; $result .= 'Skip: ' . $this->skip . PHP_EOL; return $result; @@ -201,10 +200,7 @@ class InsertMongoCommand extends MongoDbCommand if ($this->checkParams()) { $result = PHP_EOL . 'Collection: ' . $this->collection . PHP_EOL; $result .= 'Type: INSERT' . PHP_EOL; - ob_start(); - var_dump($this->data); - $data = ob_get_clean(); - $result .= 'Data: ' . $data; + $result .= 'Data: ' . $this->arrayToString($this->data); $mult = $this->multiple ? 'TRUE' : 'FALSE'; $result .= 'Bulk insert: ' . $mult . PHP_EOL; $safe = $this->safe ? 'TRUE' : 'FALSE'; @@ -250,14 +246,8 @@ class UpdateMongoCommand extends MongoDbCommand if ($this->checkParams()) { $result = PHP_EOL . 'Collection: ' . $this->collection . PHP_EOL; $result .= 'Type: UPDATE' . PHP_EOL; - ob_start(); - var_dump($this->condition); - $condition = ob_get_clean(); - $result .= 'Condition: ' . $condition; - ob_start(); - var_dump($this->data); - $data = ob_get_clean(); - $result .= 'Data: ' . $data; + $result .= 'Condition: ' . $this->arrayToString($this->condition); + $result .= 'Data: ' . $this->arrayToString($this->data); $mult = $this->multiple ? 'TRUE' : 'FALSE'; $result .= 'Multiple fields: ' . $mult . PHP_EOL; $upsert = $this->upsert ? 'TRUE' : 'FALSE'; @@ -296,10 +286,7 @@ class RemoveMongoCommand extends MongoDbCommand if ($this->checkParams()) { $result = PHP_EOL . 'Collection: ' . $this->collection . PHP_EOL; $result .= 'Type: REMOVE' . PHP_EOL; - ob_start(); - var_dump($this->condition); - $condition = ob_get_clean(); - $result .= 'Condition: ' . $condition; + $result .= 'Condition: ' . $this->arrayToString($this->condition); $safe = $this->safe ? 'TRUE' : 'FALSE'; $result .= 'Safe operation: ' . $safe . PHP_EOL; return $result; @@ -337,10 +324,7 @@ class CommandMongoCommand extends MongoDbCommand if ($this->checkParams()) { $result = PHP_EOL . 'Collection: ' . $this->collection . PHP_EOL; $result .= 'Type: COMMAND' . PHP_EOL; - ob_start(); - var_dump($this->command); - $command = ob_get_clean(); - $result .= 'Command: ' . $command; + $result .= 'Command: ' . $this->arrayToString($this->command); return $result; } else { return 'Command properties not set'; diff --git a/tests/model/MongoDbCommandTest.php b/tests/model/MongoDbCommandTest.php index c8d01fd..6cf2e1e 100644 --- a/tests/model/MongoDbCommandTest.php +++ b/tests/model/MongoDbCommandTest.php @@ -334,6 +334,7 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, new CollectionMock()); $cmd->bindParam('condition', array()); $this->assertStringStartsWith("\n" . 'Collection: CollectionMock', $cmd->__toString()); + $this->assertContains('Condition: ' . '[]' . PHP_EOL, $cmd->__toString()); $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, new CollectionMock()); $this->assertSame('Command properties not set', $cmd->__toString()); @@ -347,6 +348,7 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase ->bindParam('safe', true); $this->assertStringStartsWith("\n" . 'Collection: ', $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); $this->assertSame('Command properties not set', $cmd->__toString()); From d5ae1334788fc98f0201abe540c98a7a8dd5b33f Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Tue, 5 Jun 2012 13:52:39 +0400 Subject: [PATCH 4/4] added test for findAndModify if no item in collection and test for eval --- model/MongoStatement.php | 3 ++- tests/model/MongoDriverTest.php | 35 +++++++++++++++++++++++++++++++++++ tests/model/MongoStatementTest.php | 4 ++-- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/model/MongoStatement.php b/model/MongoStatement.php index 6097c10..45d194c 100644 --- a/model/MongoStatement.php +++ b/model/MongoStatement.php @@ -135,6 +135,7 @@ class MongoStatement extends DbStatement */ protected function driverExecute($request) { + $this->result = false; $mongo = $this->driver->getConnection(); if ($mongo instanceof Mongo) { if (DEBUG) { @@ -181,4 +182,4 @@ class MongoStatement extends DbStatement { return $this->insertId; } -} \ No newline at end of file +} diff --git a/tests/model/MongoDriverTest.php b/tests/model/MongoDriverTest.php index e11bb2a..26c6a1c 100644 --- a/tests/model/MongoDriverTest.php +++ b/tests/model/MongoDriverTest.php @@ -369,6 +369,41 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase * @runInSeparateProcess * @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')) { diff --git a/tests/model/MongoStatementTest.php b/tests/model/MongoStatementTest.php index 1fb5fca..b2415f1 100644 --- a/tests/model/MongoStatementTest.php +++ b/tests/model/MongoStatementTest.php @@ -245,10 +245,10 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase $this->request ->expects($this->once()) ->method('execute') - ->will($this->returnValue(array('some' => 'val'))); + ->will($this->returnValue(array('retval' => 'val'))); $this->stmt->execute(); - $this->assertFalse($this->stmt->fetch()); + $this->assertEquals('val', $this->stmt->fetch()); } /**