Permalink
Browse files

Support both Mongo and MongoClient in Connection class

  • Loading branch information...
1 parent 6019b0d commit ae2c68bf9859eaf8854d442acf3ee075150a448d @jmikola jmikola committed Dec 11, 2012
Showing with 72 additions and 5 deletions.
  1. +12 −4 lib/Doctrine/MongoDB/Connection.php
  2. +60 −1 tests/Doctrine/MongoDB/Tests/ConnectionTest.php
View
16 lib/Doctrine/MongoDB/Connection.php
@@ -69,12 +69,12 @@ class Connection
/**
* Create a new MongoClient wrapper instance.
*
- * @param mixed $server A string server name, an existing Mongo instance or can be omitted.
+ * @param mixed $server A string server name, an existing MongoClient or Mongo instance, or null
* @param array $options
*/
public function __construct($server = null, array $options = array(), Configuration $config = null, EventManager $evm = null)
{
- if ($server instanceof \MongoClient) {
+ if ($server instanceof \MongoClient || $server instanceof \Mongo) {
$this->mongo = $server;
} elseif ($server !== null) {
$this->server = $server;
@@ -95,6 +95,10 @@ public function initialize($reinitialize = false)
$server = $this->server;
$options = $this->options;
$this->mongo = $this->retry(function() use($server, $options) {
+ if (version_compare(phpversion('mongo'), '1.3.0', '<')) {
+ return new \Mongo($server ?: 'mongodb://localhost:27017', $options);
+ }
+
return new \MongoClient($server ?: 'mongodb://localhost:27017', $options);
});
@@ -147,10 +151,14 @@ public function log(array $log)
/**
* Set the PHP MongoClient instance to wrap.
*
- * @param MongoCient $mongo The PHP Mongo instance
+ * @param MongoClient $mongo The PHP Mongo instance
*/
- public function setMongo(\MongoClient $mongo)
+ public function setMongo($mongo)
{
+ if ( ! ($mongo instanceof \MongoClient || $mongo instanceof \Mongo)) {
+ throw new \InvalidArgumentException('MongoClient or Mongo instance required');
+ }
+
$this->mongo = $mongo;
}
View
61 tests/Doctrine/MongoDB/Tests/ConnectionTest.php
@@ -8,8 +8,24 @@
class ConnectionTest extends PHPUnit_Framework_TestCase
{
- public function testInitialize()
+ public function testInitializeMongo()
{
+ if (version_compare(phpversion('mongo'), '1.3.0', '>=')) {
+ $this->markTestSkipped('This test is not applicable to driver versions >= 1.3.0');
+ }
+
+ $conn = new Connection();
+ $this->assertNull($conn->getMongo());
+ $conn->initialize();
+ $this->assertInstanceOf('Mongo', $conn->getMongo());
+ }
+
+ public function testInitializeMongoClient()
+ {
+ if (version_compare(phpversion('mongo'), '1.3.0', '<')) {
+ $this->markTestSkipped('This test is not applicable to driver versions < 1.3.0');
+ }
+
$conn = new Connection();
$this->assertNull($conn->getMongo());
$conn->initialize();
@@ -27,6 +43,49 @@ public function testLog()
$this->assertEquals(array('test'), $called);
}
+ public function testSetMongo()
+ {
+ if (version_compare(phpversion('mongo'), '1.3.0', '>=')) {
+ $this->markTestSkipped('This test is not applicable to driver versions >= 1.3.0');
+ }
+
+ $mongo = $this->getMockBuilder('Mongo')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $conn = new Connection();
+ $conn->setMongo($mongo);
+ $this->assertSame($mongo, $conn->getMongo());
+ }
+
+ public function testSetMongoClient()
+ {
+ if (version_compare(phpversion('mongo'), '1.3.0', '<')) {
+ $this->markTestSkipped('This test is not applicable to driver versions < 1.3.0');
+ }
+
+ $mongoClient = $this->getMockBuilder('MongoClient')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $conn = new Connection();
+ $conn->setMongo($mongoClient);
+ $this->assertSame($mongoClient, $conn->getMongo());
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testSetMongoShouldThrowExceptionForInvalidArgument()
+ {
+ $mongoDB = $this->getMockBuilder('MongoDB')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $conn = new Connection();
+ $conn->setMongo($mongoDB);
+ }
+
public function testClose()
{
$mockMongo = $this->getMockMongo();

0 comments on commit ae2c68b

Please sign in to comment.