Prechádzať zdrojové kódy

Allow mongodb+srv:// protocol

Maciej Malarz 7 rokov pred
rodič
commit
5507f8ffd3

+ 1 - 1
lib/Mongo/MongoClient.php

@@ -93,7 +93,7 @@ class MongoClient
         $this->applyConnectionOptions($server, $options);
 
         $this->server = $server;
-        if (false === strpos($this->server, 'mongodb://')) {
+        if (false === strpos($this->server, '://')) {
             $this->server = 'mongodb://' . $this->server;
         }
         $this->client = new Client($this->server, $options, $driverOptions);

+ 19 - 0
tests/Alcaeus/MongoDbAdapter/Mongo/MongoClientTest.php

@@ -9,6 +9,25 @@ use Alcaeus\MongoDbAdapter\Tests\TestCase;
  */
 class MongoClientTest extends TestCase
 {
+    /**
+     * @dataProvider provideConnectionUri
+     */
+    public function testConnectionUri($uri, $expected)
+    {
+        $this->skipTestIf(extension_loaded('mongo'));
+        $this->assertSame($expected, (string) (new \MongoClient($uri, ['connect' => false])));
+    }
+
+    public function provideConnectionUri()
+    {
+        yield ['default', sprintf('mongodb://%s:%d', \MongoClient::DEFAULT_HOST, \MongoClient::DEFAULT_PORT)];
+        yield ['localhost', 'mongodb://localhost'];
+        yield ['mongodb://localhost', 'mongodb://localhost'];
+        if (version_compare(phpversion('mongodb'), '1.4.0', '>=')) {
+            yield ['mongodb+srv://foo.example.com', 'mongodb+srv://foo.example.com'];
+        }
+    }
+
     public function testSerialize()
     {
         $this->assertInternalType('string', serialize($this->getClient()));