From 839493da520abccbd92416f63149a0aa78169450 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Mon, 12 Aug 2013 19:05:41 -0400 Subject: [PATCH] Make LoggableDatabase::createCollection() API consistent with Database LoggableDatabase must support the same arguments as the base method, which means duplicate the logic. Ensure the created collection's name is logged, as well as the new options array. Deprecate the older log format (akin to what was done in CreateCollectionEventArgs). --- lib/Doctrine/MongoDB/LoggableDatabase.php | 17 ++- .../MongoDB/Tests/LoggableDatabaseTest.php | 105 ++++++++++++++++++ 2 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 tests/Doctrine/MongoDB/Tests/LoggableDatabaseTest.php 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/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; + } +}