Browse Source

Expose private method used to parse URI strings.

This is useful for 3rd party libraries such as PredisServiceProvider
so it makes sense to have it public and static.
Daniele Alessandri 12 years ago
parent
commit
a1df4f20da

+ 2 - 2
lib/Predis/Connection/ConnectionParameters.php

@@ -36,7 +36,7 @@ class ConnectionParameters implements ConnectionParametersInterface
     public function __construct($parameters = array())
     public function __construct($parameters = array())
     {
     {
         if (!is_array($parameters)) {
         if (!is_array($parameters)) {
-            $parameters = $this->parseURI($parameters);
+            $parameters = self::parseURI($parameters);
         }
         }
 
 
         $this->parameters = $this->filter($parameters) + $this->getDefaults();
         $this->parameters = $this->filter($parameters) + $this->getDefaults();
@@ -108,7 +108,7 @@ class ConnectionParameters implements ConnectionParametersInterface
      * @param string $uri Connection string.
      * @param string $uri Connection string.
      * @return array
      * @return array
      */
      */
-    private function parseURI($uri)
+    public static function parseURI($uri)
     {
     {
         if (stripos($uri, 'unix') === 0) {
         if (stripos($uri, 'unix') === 0) {
             // Hack to support URIs for UNIX sockets with minimal effort.
             // Hack to support URIs for UNIX sockets with minimal effort.

+ 46 - 0
tests/Predis/Connection/ConnectionParametersTest.php

@@ -123,6 +123,52 @@ class ParametersTest extends StandardTestCase
         $this->assertNull($unserialized->unknown);
         $this->assertNull($unserialized->unknown);
     }
     }
 
 
+    /**
+     * @group disconnected
+     */
+    public function testParsingURI()
+    {
+        $uri = 'tcp://10.10.10.10:6400?timeout=0.5&persistent=1';
+
+        $expected = array(
+            'scheme' => 'tcp',
+            'host' => '10.10.10.10',
+            'port' => 6400,
+            'timeout' => '0.5',
+            'persistent' => '1',
+        );
+
+        $this->assertSame($expected, ConnectionParameters::parseURI($uri));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testParsingUnixDomainURI()
+    {
+        $uri = 'unix:///tmp/redis.sock?timeout=0.5&persistent=1';
+
+        $expected = array(
+            'scheme' => 'unix',
+            'host' => 'localhost',
+            'path' => '/tmp/redis.sock',
+            'timeout' => '0.5',
+            'persistent' => '1',
+        );
+
+        $this->assertSame($expected, ConnectionParameters::parseURI($uri));
+    }
+
+    /**
+     * @group disconnected
+     * @expectedException Predis\ClientException
+     * @expectedExceptionMessage Invalid URI: tcp://invalid:uri
+     */
+    public function testParsingURIThrowOnInvalidURI()
+    {
+        ConnectionParameters::parseURI('tcp://invalid:uri');
+    }
+
     // ******************************************************************** //
     // ******************************************************************** //
     // ---- HELPER METHODS ------------------------------------------------ //
     // ---- HELPER METHODS ------------------------------------------------ //
     // ******************************************************************** //
     // ******************************************************************** //