diff --git a/lib/Doctrine/MongoDB/Database.php b/lib/Doctrine/MongoDB/Database.php index 70608569..3a7185f1 100644 --- a/lib/Doctrine/MongoDB/Database.php +++ b/lib/Doctrine/MongoDB/Database.php @@ -119,7 +119,10 @@ public function command(array $data, array $options = array()) /** * Wrapper method for MongoDB::createCollection(). * - * @see http://php.net/manual/en/mongodb.command.php + * This method will dispatch preCreateCollection and postCreateCollection + * events. + * + * @see http://php.net/manual/en/mongodb.createcollection.php * @param string $name Collection name * @param boolean|array $cappedOrOptions Capped collection indicator or an * options array (for driver 1.4+) @@ -139,16 +142,10 @@ public function createCollection($name, $cappedOrOptions = false, $size = 0, $ma $this->eventManager->dispatchEvent(Events::preCreateCollection, new CreateCollectionEventArgs($this, $name, $options)); } - if (version_compare(phpversion('mongo'), '1.4.0', '>=')) { - $this->getMongoDB()->createCollection($name, $options); - } else { - $this->getMongoDB()->createCollection($name, $options['capped'], $options['size'], $options['max']); - } - - $result = $this->selectCollection($name); + $result = $this->doCreateCollection($name, $options); if ($this->eventManager->hasListeners(Events::postCreateCollection)) { - $this->eventManager->dispatchEvent(Events::postCreateCollection, new EventArgs($this, $prefix)); + $this->eventManager->dispatchEvent(Events::postCreateCollection, new EventArgs($this, $result)); } return $result; @@ -170,6 +167,8 @@ public function createDBRef($collection, $a) /** * Wrapper method for MongoDB::drop(). * + * This method will dispatch preDropDatabase and postDropDatabase events. + * * @see http://php.net/manual/en/mongodb.drop.php * @return array */ @@ -251,6 +250,8 @@ public function getDBRef(array $reference) /** * Wrapper method for MongoDB::getGridFS(). * + * This method will dispatch preGetGridFS and postGetGridFS events. + * * @see http://php.net/manual/en/mongodb.getgridfs.php * @param string $prefix * @return GridFS @@ -261,13 +262,13 @@ public function getGridFS($prefix = 'fs') $this->eventManager->dispatchEvent(Events::preGetGridFS, new EventArgs($this, $prefix)); } - $gridFS = $this->doGetGridFs($prefix); + $gridfs = $this->doGetGridFS($prefix); - if ($this->eventManager->hasListeners(Events::preGetGridFS)) { - $this->eventManager->dispatchEvent(Events::preGetGridFS, new EventArgs($this, $gridFS)); + if ($this->eventManager->hasListeners(Events::postGetGridFS)) { + $this->eventManager->dispatchEvent(Events::postGetGridFS, new EventArgs($this, $gridfs)); } - return $gridFS; + return $gridfs; } /** @@ -519,6 +520,25 @@ protected function doGetDBRef(array $reference) }); } + /** + * Creates a collection. + * + * @see Database::createCollection() + * @param string $name + * @param array $options + * @return Collection + */ + protected function doCreateCollection($name, array $options) + { + if (version_compare(phpversion('mongo'), '1.4.0', '>=')) { + $this->getMongoDB()->createCollection($name, $options); + } else { + $this->getMongoDB()->createCollection($name, $options['capped'], $options['size'], $options['max']); + } + + return $this->doSelectCollection($name); + } + /** * Return a new GridFS instance. * @@ -526,7 +546,7 @@ protected function doGetDBRef(array $reference) * @param string $prefix * @return GridFS */ - protected function doGetGridFs($prefix) + protected function doGetGridFS($prefix) { return new GridFS($this->connection, $prefix, $this, $this->eventManager, $this->cmd); } diff --git a/lib/Doctrine/MongoDB/LoggableDatabase.php b/lib/Doctrine/MongoDB/LoggableDatabase.php index dd68e756..4ab6f64b 100644 --- a/lib/Doctrine/MongoDB/LoggableDatabase.php +++ b/lib/Doctrine/MongoDB/LoggableDatabase.php @@ -99,16 +99,23 @@ public function command(array $data, array $options = array()) /** * @see Database::createCollection() */ - public function createCollection($name, $capped = false, $size = 0, $max = 0) + public function createCollection($name, $cappedOrOptions = false, $size = 0, $max = 0) { + $options = is_array($cappedOrOptions) + ? array_merge(array('capped' => false, 'size' => 0, 'max' => 0), $cappedOrOptions) + : array('capped' => $cappedOrOptions, 'size' => $size, 'max' => $max); + $this->log(array( 'createCollection' => true, - 'capped' => $capped, - 'size' => $size, - 'max' => $max, + 'name' => $name, + 'options' => $options, + /* @deprecated 1.1 Replaced by options; will be removed for 2.0 */ + 'capped' => $options['capped'], + 'size' => $options['size'], + 'max' => $options['max'], )); - return parent::createCollection($name, $capped, $size, $max); + return parent::createCollection($name, $options); } /** diff --git a/tests/Doctrine/MongoDB/Tests/DatabaseEventsTest.php b/tests/Doctrine/MongoDB/Tests/DatabaseEventsTest.php index 14c001f4..896c548f 100644 --- a/tests/Doctrine/MongoDB/Tests/DatabaseEventsTest.php +++ b/tests/Doctrine/MongoDB/Tests/DatabaseEventsTest.php @@ -4,6 +4,7 @@ use Doctrine\Common\EventManager; use Doctrine\MongoDB\Events; +use Doctrine\MongoDB\Event\CreateCollectionEventArgs; use Doctrine\MongoDB\Event\EventArgs; use Doctrine\MongoDB\Event\MutableEventArgs; @@ -11,6 +12,43 @@ class DatabaseEventsTest extends \PHPUnit_Framework_TestCase { const databaseName = 'database'; + public function testCreateCollection() + { + $name = 'collection'; + $options = array('capped' => false, 'size' => 0, 'max' => 0); + $result = $this->getMockCollection(); + + $eventManager = $this->getMockEventManager(); + $db = $this->getMockDatabase($eventManager, array('doCreateCollection' => $result)); + + $this->expectEvents($eventManager, array( + array(Events::preCreateCollection, new CreateCollectionEventArgs($db, $name, $options)), + array(Events::postCreateCollection, new EventArgs($db, $result)), + )); + + $this->assertSame($result, $db->createCollection($name, $options)); + } + + public function testDrop() + { + $result = array('dropped' => self::databaseName, 'ok' => 1); + + $mongoDB = $this->getMockMongoDB(); + $mongoDB->expects($this->once()) + ->method('drop') + ->will($this->returnValue($result)); + + $eventManager = $this->getMockEventManager(); + $db = $this->getMockDatabase($eventManager, array('getMongoDB' => $mongoDB)); + + $this->expectEvents($eventManager, array( + array(Events::preDropDatabase, new EventArgs($db)), + array(Events::postDropDatabase, new EventArgs($db)), + )); + + $this->assertSame($result, $db->drop()); + } + public function testGetDBRef() { $reference = array('$ref' => 'collection', '$id' => 1); @@ -27,6 +65,38 @@ public function testGetDBRef() $this->assertSame($result, $db->getDBRef($reference)); } + public function testGetGridFS() + { + $prefix = 'fs'; + $result = $this->getMockGridFS(); + + $eventManager = $this->getMockEventManager(); + $db = $this->getMockDatabase($eventManager, array('doGetGridFS' => $result)); + + $this->expectEvents($eventManager, array( + array(Events::preGetGridFS, new EventArgs($db, $prefix)), + array(Events::postGetGridFS, new EventArgs($db, $result)), + )); + + $this->assertSame($result, $db->getGridFS()); + } + + public function testSelectCollection() + { + $name = 'collection'; + $result = $this->getMockCollection(); + + $eventManager = $this->getMockEventManager(); + $db = $this->getMockDatabase($eventManager, array('doSelectCollection' => $result)); + + $this->expectEvents($eventManager, array( + array(Events::preSelectCollection, new EventArgs($db, $name)), + array(Events::postSelectCollection, new EventArgs($db, $result)), + )); + + $this->assertSame($result, $db->selectCollection($name)); + } + /** * Expect events to be dispatched by the event manager in the given order. * @@ -54,6 +124,13 @@ private function expectEvents(EventManager $em, array $events) } } + private function getMockCollection() + { + return $this->getMockBuilder('Doctrine\MongoDB\Collection') + ->disableOriginalConstructor() + ->getMock(); + } + private function getMockDatabase(EventManager $em, array $methods) { $c = $this->getMockBuilder('Doctrine\MongoDB\Connection') @@ -80,4 +157,18 @@ private function getMockEventManager() ->disableOriginalConstructor() ->getMock(); } + + private function getMockGridFS() + { + return $this->getMockBuilder('Doctrine\MongoDB\GridFS') + ->disableOriginalConstructor() + ->getMock(); + } + + private function getMockMongoDB() + { + return $this->getMockBuilder('MongoDB') + ->disableOriginalConstructor() + ->getMock(); + } } diff --git a/tests/Doctrine/MongoDB/Tests/DatabaseTest.php b/tests/Doctrine/MongoDB/Tests/DatabaseTest.php index f137e5e9..bc719178 100644 --- a/tests/Doctrine/MongoDB/Tests/DatabaseTest.php +++ b/tests/Doctrine/MongoDB/Tests/DatabaseTest.php @@ -53,7 +53,7 @@ public function testCreateCollectionWithOptionsArgument() if (version_compare(phpversion('mongo'), '1.4.0', '>=')) { $this->mongodb->expects($this->once()) ->method('createCollection') - ->with('foo', array('capped' => true, 'size' => 10485760, 'autoIndexId' => false, 'max' => 0)); + ->with('foo', array('capped' => true, 'size' => 10485760, 'max' => 0, 'autoIndexId' => false,)); } else { $this->mongodb->expects($this->once()) ->method('createCollection') diff --git a/tests/Doctrine/MongoDB/Tests/LoggableCollectionTest.php b/tests/Doctrine/MongoDB/Tests/LoggableCollectionTest.php index 3f0cde84..5045e6ef 100644 --- a/tests/Doctrine/MongoDB/Tests/LoggableCollectionTest.php +++ b/tests/Doctrine/MongoDB/Tests/LoggableCollectionTest.php @@ -3,10 +3,12 @@ namespace Doctrine\MongoDB\Tests; use Doctrine\MongoDB\LoggableCollection; -use Doctrine\Common\EventManager; class LoggableCollectionTest extends \PHPUnit_Framework_TestCase { + const collectionName = 'collection'; + const databaseName = 'database'; + public function testLog() { $called = false; @@ -15,29 +17,30 @@ public function testLog() $called = $msg; }; - $database = $this->getMockDatabase(); - - $database->expects($this->once()) - ->method('getName') - ->will($this->returnValue('test')); - - $coll = new LoggableCollection($this->getMockConnection(), 'foo', $database, new EventManager(), '$', $loggerCallable); - $coll->log(array('test' => 'test')); + $collection = $this->getTestLoggableDatabase($loggerCallable); + $collection->log(array('test' => 'test')); - $this->assertEquals(array('collection' => 'foo', 'db' => 'test', 'test' => 'test'), $called); + $this->assertEquals(array('collection' => self::collectionName, 'db' => self::databaseName, 'test' => 'test'), $called); } - private function getMockDatabase() + private function getTestLoggableDatabase($loggerCallable) { - return $this->getMockBuilder('Doctrine\MongoDB\Database') + $c = $this->getMockBuilder('Doctrine\MongoDB\Connection') ->disableOriginalConstructor() ->getMock(); - } - private function getMockConnection() - { - return $this->getMockBuilder('Doctrine\MongoDB\Connection') + $db = $this->getMockBuilder('Doctrine\MongoDB\Database') ->disableOriginalConstructor() ->getMock(); + + $db->expects($this->any()) + ->method('getName') + ->will($this->returnValue(self::databaseName)); + + $em = $this->getMockBuilder('Doctrine\Common\EventManager') + ->disableOriginalConstructor() + ->getMock(); + + return new LoggableCollection($c, self::collectionName, $db, $em, '$', $loggerCallable); } } diff --git a/tests/Doctrine/MongoDB/Tests/LoggableDatabaseTest.php b/tests/Doctrine/MongoDB/Tests/LoggableDatabaseTest.php new file mode 100644 index 00000000..8ca2393d --- /dev/null +++ b/tests/Doctrine/MongoDB/Tests/LoggableDatabaseTest.php @@ -0,0 +1,105 @@ +getTestLoggableDatabase($loggerCallable); + $db->log(array('test' => 'test')); + + $this->assertEquals(array('db' => self::databaseName, 'test' => 'test'), $called); + } + + public function testCreateCollectionWithMultipleArguments() + { + $called = false; + + $loggerCallable = function($msg) use (&$called) { + $called = $msg; + }; + + $db = $this->getTestLoggableDatabase($loggerCallable); + $db->createCollection('foo', true, 10485760, 0); + + $expected = array( + 'createCollection' => true, + 'name' => 'foo', + 'options' => array('capped' => true, 'size' => 10485760, 'max' => 0), + 'capped' => true, + 'size' => 10485760, + 'max' => 0, + 'db' => self::databaseName, + ); + + $this->assertEquals($expected, $called); + } + + public function testCreateCollectionWithOptionsArgument() + { + $called = false; + + $loggerCallable = function($msg) use (&$called) { + $called = $msg; + }; + + $db = $this->getTestLoggableDatabase($loggerCallable); + $db->createCollection('foo', array('capped' => true, 'size' => 10485760, 'autoIndexId' => false)); + + $expected = array( + 'createCollection' => true, + 'name' => 'foo', + 'options' => array('capped' => true, 'size' => 10485760, 'max' => 0, 'autoIndexId' => false), + 'capped' => true, + 'size' => 10485760, + 'max' => 0, + 'db' => self::databaseName, + ); + + $this->assertEquals($expected, $called); + } + + private function getTestLoggableDatabase($loggerCallable) + { + $c = $this->getMockBuilder('Doctrine\MongoDB\Connection') + ->disableOriginalConstructor() + ->getMock(); + + $mdb = $this->getMockBuilder('MongoDB') + ->disableOriginalConstructor() + ->getMock(); + + $em = $this->getMockBuilder('Doctrine\Common\EventManager') + ->disableOriginalConstructor() + ->getMock(); + + $db = new TestLoggableDatabaseStub($c, self::databaseName, $em, '$', 0, $loggerCallable); + $db->setMongoDB($mdb); + + return $db; + } +} + +class TestLoggableDatabaseStub extends LoggableDatabase +{ + public function setMongoDB($mongoDB) + { + $this->mongoDB = $mongoDB; + } + + public function getMongoDB() + { + return $this->mongoDB; + } +}