From c832bd2ffe51d621e8ba13e08f584dd107080b72 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Tue, 29 Apr 2014 15:01:22 -0400 Subject: [PATCH] Cast createCollection() options for MongoDB 2.6 compatibility MongoDB 2.6 complains if the size/max options are non-numeric (e.g. null). See: doctrine/DoctrineMongoDBBundle#239 --- lib/Doctrine/MongoDB/Database.php | 4 +++ tests/Doctrine/MongoDB/Tests/DatabaseTest.php | 25 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/Doctrine/MongoDB/Database.php b/lib/Doctrine/MongoDB/Database.php index efe2cbfd..d5a2de45 100644 --- a/lib/Doctrine/MongoDB/Database.php +++ b/lib/Doctrine/MongoDB/Database.php @@ -128,6 +128,10 @@ public function createCollection($name, $cappedOrOptions = false, $size = 0, $ma ? array_merge(array('capped' => false, 'size' => 0, 'max' => 0), $cappedOrOptions) : array('capped' => $cappedOrOptions, 'size' => $size, 'max' => $max); + $options['capped'] = (boolean) $options['capped']; + $options['size'] = (integer) $options['size']; + $options['max'] = (integer) $options['max']; + if ($this->eventManager->hasListeners(Events::preCreateCollection)) { $this->eventManager->dispatchEvent(Events::preCreateCollection, new CreateCollectionEventArgs($this, $name, $options)); } diff --git a/tests/Doctrine/MongoDB/Tests/DatabaseTest.php b/tests/Doctrine/MongoDB/Tests/DatabaseTest.php index 290fec6f..89ec70c8 100644 --- a/tests/Doctrine/MongoDB/Tests/DatabaseTest.php +++ b/tests/Doctrine/MongoDB/Tests/DatabaseTest.php @@ -60,6 +60,31 @@ public function testCreateCollectionWithOptionsArgument() $this->assertInstanceOf('Doctrine\MongoDB\Collection', $collection); } + public function testCreateCollectionCappedOptionsAreCast() + { + $mongoDB = $this->getMockMongoDB(); + + if (version_compare(phpversion('mongo'), '1.4.0', '>=')) { + $mongoDB->expects($this->once()) + ->method('createCollection') + ->with('foo', array('capped' => false, 'size' => 0, 'max' => 0)); + } else { + $mongoDB->expects($this->once()) + ->method('createCollection') + ->with('foo', false, 0, 0); + } + + $mongoDB->expects($this->once()) + ->method('selectCollection') + ->with('foo') + ->will($this->returnValue($this->getMockMongoCollection())); + + $database = new Database($this->getMockConnection(), $mongoDB, $this->getMockEventManager()); + $collection = $database->createCollection('foo', array('capped' => 0, 'size' => null, 'max' => null)); + + $this->assertInstanceOf('Doctrine\MongoDB\Collection', $collection); + } + public function testGetSetSlaveOkay() { if (version_compare(phpversion('mongo'), '1.3.0', '>=')) {