diff --git a/core/lib/Drupal/Core/Database/Connection.php b/core/lib/Drupal/Core/Database/Connection.php
index 2aa6315..672025f 100644
--- a/core/lib/Drupal/Core/Database/Connection.php
+++ b/core/lib/Drupal/Core/Database/Connection.php
@@ -6,6 +6,7 @@
  */
 
 namespace Drupal\Core\Database;
+use GuzzleHttp\Psr7\Uri;
 
 /**
  * Base Database API class.
@@ -1433,4 +1434,73 @@ public function __sleep() {
     throw new \LogicException('The database connection is not serializable. This probably means you are serializing an object that has an indirect reference to the database connection. Adjust your code so that is not necessary. Alternatively, look at DependencySerializationTrait as a temporary solution.');
   }
 
+  /**
+   * A helper function to convert a URI into connection options.
+   *
+   * This method can be overridden in order to customise how a URI is converted.
+   *
+   * @internal
+   *   This method should not be called. Use
+   *   \Drupal\Core\Database\Database::convertDbUrlToConnectionInfo() instead.
+   *
+   * @param \GuzzleHttp\Psr7\Uri $uri
+   *   The URI to be converted to a connection array.
+   * @param string $root
+   *   The root directory of the Drupal installation.
+   * @param array $connection_options
+   *   The connection options.
+   *
+   * @return array
+   *   The connection options.
+   *
+   * @see \Drupal\Core\Database\Database::convertDbUrlToConnectionInfo()
+   */
+  public static function convertDbUrlToConnectionInfoHelper(Uri $uri, $root, array $connection_options) {
+    $port = $uri->getPort();
+    if (!empty($port)) {
+      $connection_options['port'] = $port;
+    }
+    $user_info = explode(':', $uri->getUserInfo(), 2);
+    $connection_options['username'] = $user_info[0];
+    $connection_options['password'] = isset($user_info[1]) ? $user_info[1] : '';
+
+    return $connection_options;
+  }
+
+  /**
+   * A helper function to convert connection options into a URI.
+   *
+   * This method can be overridden in order to customise how connection options
+   * are converted.
+   *
+   * @internal
+   *   This method should not be called. Use
+   *   \Drupal\Core\Database\Database::getConnectionInfoAsUrl() instead.
+   *
+   * @param array $connection_options
+   *   The array of connection options for a database connection.
+   * @param \GuzzleHttp\Psr7\Uri $uri
+   *   The URI to connect to the database.
+   *
+   * @return $this|\GuzzleHttp\Psr7\Uri
+   *   The URI to connect to the database.
+   *
+   * @see \Drupal\Core\Database\Database::getConnectionInfoAsUrl()
+   */
+  public static function getConnectionInfoAsUrlHelper(array $connection_options, Uri $uri) {
+    $user = '';
+    if ($connection_options['username']) {
+      $user = $connection_options['username'];
+    }
+    $password = '';
+    if ($connection_options['password']) {
+      $password = $connection_options['password'];
+    }
+    $uri = $uri->withUserInfo($user, $password);
+    if (!empty($connection_options['port'])) {
+      $uri = $uri->withPort($connection_options['port']);
+    }
+    return $uri;
+  }
+
 }
diff --git a/core/lib/Drupal/Core/Database/Database.php b/core/lib/Drupal/Core/Database/Database.php
index d48c6f2..0e2fab2 100644
--- a/core/lib/Drupal/Core/Database/Database.php
+++ b/core/lib/Drupal/Core/Database/Database.php
@@ -7,6 +7,8 @@
 
 namespace Drupal\Core\Database;
 
+use GuzzleHttp\Psr7\Uri;
+
 /**
  * Primary front-controller for the database system.
  *
@@ -370,13 +372,8 @@
       throw new DriverNotSpecifiedException('Driver not specified for this database connection: ' . $key);
     }
 
-    if (!empty(self::$databaseInfo[$key][$target]['namespace'])) {
-      $driver_class = self::$databaseInfo[$key][$target]['namespace'] . '\\Connection';
-    }
-    else {
-      // Fallback for Drupal 7 settings.php.
-      $driver_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Connection";
-    }
+    $namespace = !empty(self::$databaseInfo[$key][$target]['namespace']) ? self::$databaseInfo[$key][$target]['namespace'] : NULL;
+    $driver_class = static::getDatabaseConnectionClass($driver, $namespace);
 
     $pdo_connection = $driver_class::open(self::$databaseInfo[$key][$target]);
     $new_connection = new $driver_class($pdo_connection, self::$databaseInfo[$key][$target]);
@@ -460,36 +457,33 @@ public static function ignoreTarget($key, $target) {
    *   requirements.
    */
   public static function convertDbUrlToConnectionInfo($url, $root) {
-    $info = parse_url($url);
-    if (!isset($info['scheme'], $info['host'], $info['path'])) {
+    $uri = new Uri($url);
+    if (empty($uri->getHost()) || empty($uri->getHost()) || empty($uri->getPath())) {
       throw new \InvalidArgumentException('Minimum requirement: driver://host/database');
     }
-    $info += array(
-      'user' => '',
-      'pass' => '',
-      'fragment' => '',
-    );
 
-    // A SQLite database path with two leading slashes indicates a system path.
-    // Otherwise the path is relative to the Drupal root.
-    if ($info['path'][0] === '/') {
-      $info['path'] = substr($info['path'], 1);
-    }
-    if ($info['scheme'] === 'sqlite' && $info['path'][0] !== '/') {
-      $info['path'] = $root . '/' . $info['path'];
-    }
-
-    $database = array(
-      'driver' => $info['scheme'],
-      'username' => $info['user'],
-      'password' => $info['pass'],
-      'host' => $info['host'],
-      'database' => $info['path'],
+    // Discover if the URL has a driver namespace.
+    $parts = array();
+    parse_str($uri->getQuery(), $parts);
+    $namespace = isset($parts['namespace']) ? $parts['namespace'] : '';
+
+    $fragment = $uri->getFragment();
+    $connection_opptions = array(
+      'driver' => $uri->getScheme(),
+      'host' => $uri->getHost(),
+      // Strip the first leading slash of the path to get the database name.
+      // Note that additional leading slashes have meaning for some database
+      // drivers.
+      'database' => substr($uri->getPath(), 1),
+      'prefix' => $fragment ?: NULL,
+      'namespace' => $namespace,
     );
-    if (isset($info['port'])) {
-      $database['port'] = $info['port'];
+
+    $driver_class = static::getDatabaseConnectionClass($connection_opptions['driver'], $connection_opptions['namespace']);
+    if (!class_exists($driver_class)) {
+      throw new \RuntimeException("Can not convert $url to a database connection, class $driver_class does not exist");
     }
-    return $database;
+    return $driver_class::convertDbUrlToConnectionInfoHelper($uri, $root, $connection_opptions);
   }
 
   /**
@@ -503,29 +497,51 @@ public static function convertDbUrlToConnectionInfo($url, $root) {
    */
   public static function getConnectionInfoAsUrl($key = 'default') {
     $db_info = static::getConnectionInfo($key);
-    if ($db_info['default']['driver'] == 'sqlite') {
-      $db_url = 'sqlite://localhost/' . $db_info['default']['database'];
+    if (empty($db_info)) {
+      throw new \RuntimeException("Database connection $key not defined");
     }
-    else {
-      $user = '';
-      if ($db_info['default']['username']) {
-        $user = $db_info['default']['username'];
-        if ($db_info['default']['password']) {
-          $user .= ':' . $db_info['default']['password'];
-        }
-        $user .= '@';
-      }
 
-      $db_url = $db_info['default']['driver'] . '://' . $user . $db_info['default']['host'];
-      if (isset($db_info['default']['port'])) {
-        $db_url .= ':' . $db_info['default']['port'];
-      }
-      $db_url .= '/' . $db_info['default']['database'];
+    $namespace = isset($db_info['default']['namespace']) ? $db_info['default']['namespace'] : NULL;
+    $driver_class = static::getDatabaseConnectionClass($db_info['default']['driver'], $namespace);
+
+    // Some database driver do not need a host setting to work but in order to
+    // be converted into a URL they do.
+    $host = isset($db_info['default']['host']) ? $db_info['default']['host'] : 'localhost';
+
+    // Create a URI with the minimum requirement of driver://host/database.
+    $uri = new Uri($db_info['default']['driver'] . '://' . $host . '/' . $db_info['default']['database']);
+
+    if (!empty($db_info['default']['prefix']['default'])) {
+      $uri = $uri->withFragment($db_info['default']['prefix']['default']);
+    }
+    if (!empty($db_info['default']['namespace'])) {
+      $uri = Uri::withQueryValue($uri, 'namespace', $db_info['default']['namespace']);
+    }
+
+    $uri = $driver_class::getConnectionInfoAsUrlHelper($db_info['default'], $uri);
+    return (string) $uri;
+  }
+
+  /**
+   * Gets the fully qualified connection class name for a database driver.
+   *
+   * @param string $driver
+   *   The database driver to get the class for.
+   * @param string $namespace
+   *   A custom namespace to use.
+   *
+   * @return string
+   *   The fully qualified class name of the driver's database connection class.
+   */
+  public static function getDatabaseConnectionClass($driver, $namespace) {
+    if ($namespace) {
+      $driver_class = $namespace . '\\Connection';
     }
-    if ($db_info['default']['prefix']['default']) {
-      $db_url .= '#' . $db_info['default']['prefix']['default'];
+    else {
+      // Fallback for Drupal 7 settings.php.
+      $driver_class = 'Drupal\\Core\\Database\\Driver\\' . $driver . '\\Connection';
     }
-    return $db_url;
+    return $driver_class;
   }
 
 }
diff --git a/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php b/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php
index b94ae9f..1896dd7 100644
--- a/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php
+++ b/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php
@@ -10,6 +10,7 @@
 use Drupal\Core\Database\Database;
 use Drupal\Core\Database\DatabaseNotFoundException;
 use Drupal\Core\Database\Connection as DatabaseConnection;
+use GuzzleHttp\Psr7\Uri;
 
 /**
  * SQLite implementation of \Drupal\Core\Database\Connection.
@@ -415,4 +416,30 @@ public function getFullQualifiedTableName($table) {
     return $prefix . $table;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public static function convertDbUrlToConnectionInfoHelper(Uri $uri, $root, array $database) {
+    // A SQLite database with a leading slash indicates a system path.
+    // Otherwise the path is relative to the Drupal root.
+    if ($database['database'][0] !== '/') {
+      $database['database'] = $root . '/' . $database['database'];
+    }
+    // The host setting is meaningless for SQLite databases.
+    unset($database['host']);
+    return $database;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function getConnectionInfoAsUrlHelper(array $connection_options, Uri $uri) {
+    if ($connection_options['database'][0] === '/') {
+      // If the database is a absolute path add an additional leading slash to
+      // denote this.
+      $uri = $uri->withPath('/' . $connection_options['database']);
+    }
+    return $uri;
+  }
+
 }
diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module
index 4dc7544..d589466 100644
--- a/core/modules/simpletest/simpletest.module
+++ b/core/modules/simpletest/simpletest.module
@@ -262,9 +262,11 @@ function simpletest_phpunit_configuration_filepath() {
  *  The results as returned by exec().
  */
 function simpletest_phpunit_run_command(array $unescaped_test_classnames, $phpunit_file, &$status = NULL) {
-  // Setup an environment variable containing the database connection so that
-  // functional tests can connect to the database.
-  putenv('SIMPLETEST_DB=' . Database::getConnectionInfoAsUrl());
+  if (Database::getConnectionInfo()) {
+    // Setup an environment variable containing the database connection so that
+    // functional tests can connect to the database.
+    putenv('SIMPLETEST_DB=' . Database::getConnectionInfoAsUrl());
+  }
   $phpunit_bin = simpletest_phpunit_command();
 
   $command = array(
diff --git a/core/tests/Drupal/Tests/Core/Database/ConnectionTest.php b/core/tests/Drupal/Tests/Core/Database/ConnectionTest.php
index a91e241..47f92c6 100644
--- a/core/tests/Drupal/Tests/Core/Database/ConnectionTest.php
+++ b/core/tests/Drupal/Tests/Core/Database/ConnectionTest.php
@@ -7,7 +7,7 @@
 
 namespace Drupal\Tests\Core\Database;
 
-use Drupal\Tests\Core\Database\Stub\StubConnection;
+use Drupal\Tests\Core\Database\Stub\Connection;
 use Drupal\Tests\UnitTestCase;
 
 /**
@@ -51,10 +51,10 @@ public function providerPrefixRoundTrip() {
    */
   public function testPrefixRoundTrip($expected, $prefix_info) {
     $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
-    $connection = new StubConnection($mock_pdo, array());
+    $connection = new Connection($mock_pdo, array());
 
     // setPrefix() is protected, so we make it accessible with reflection.
-    $reflection = new \ReflectionClass('Drupal\Tests\Core\Database\Stub\StubConnection');
+    $reflection = new \ReflectionClass('Drupal\Tests\Core\Database\Stub\Connection');
     $set_prefix = $reflection->getMethod('setPrefix');
     $set_prefix->setAccessible(TRUE);
 
@@ -100,7 +100,7 @@ public function providerTestPrefixTables() {
    */
   public function testPrefixTables($expected, $prefix_info, $query) {
     $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
-    $connection = new StubConnection($mock_pdo, array('prefix' => $prefix_info));
+    $connection = new Connection($mock_pdo, array('prefix' => $prefix_info));
     $this->assertEquals($expected, $connection->prefixTables($query));
   }
 
@@ -134,7 +134,7 @@ public function providerEscapeMethods() {
    */
   public function testEscapeMethods($expected, $name) {
     $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
-    $connection = new StubConnection($mock_pdo, array());
+    $connection = new Connection($mock_pdo, array());
     $this->assertEquals($expected, $connection->escapeDatabase($name));
     $this->assertEquals($expected, $connection->escapeTable($name));
     $this->assertEquals($expected, $connection->escapeField($name));
@@ -181,7 +181,7 @@ public function providerGetDriverClass() {
    */
   public function testGetDriverClass($expected, $driver, $namespace, $class) {
     $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
-    $connection = new StubConnection($mock_pdo, array('namespace' => $namespace));
+    $connection = new Connection($mock_pdo, array('namespace' => $namespace));
     // Set the driver using our stub class' public property.
     $connection->driver = $driver;
     $this->assertEquals($expected, $connection->getDriverClass($class));
@@ -213,7 +213,7 @@ public function providerSchema() {
    */
   public function testSchema($expected, $driver, $namespace) {
     $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
-    $connection = new StubConnection($mock_pdo, array('namespace' => $namespace));
+    $connection = new Connection($mock_pdo, array('namespace' => $namespace));
     $connection->driver = $driver;
     $this->assertInstanceOf($expected, $connection->schema());
   }
@@ -223,9 +223,9 @@ public function testSchema($expected, $driver, $namespace) {
    */
   public function testDestroy() {
     $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
-    // Mocking StubConnection gives us access to the $schema attribute.
+    // Mocking Connection gives us access to the $schema attribute.
     $connection = $this->getMock(
-      'Drupal\Tests\Core\Database\Stub\StubConnection',
+      'Drupal\Tests\Core\Database\Stub\Connection',
       NULL,
       array($mock_pdo, array('namespace' => 'Drupal\\Tests\\Core\\Database\\Stub\\Driver'))
     );
@@ -270,7 +270,7 @@ public function providerMakeComments() {
    */
   public function testMakeComments($expected, $comment_array) {
     $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
-    $connection = new StubConnection($mock_pdo, array());
+    $connection = new Connection($mock_pdo, array());
     $this->assertEquals($expected, $connection->makeComment($comment_array));
   }
 
@@ -297,10 +297,10 @@ public function providerFilterComments() {
    */
   public function testFilterComments($expected, $comment) {
     $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
-    $connection = new StubConnection($mock_pdo, array());
+    $connection = new Connection($mock_pdo, array());
 
     // filterComment() is protected, so we make it accessible with reflection.
-    $reflection = new \ReflectionClass('Drupal\Tests\Core\Database\Stub\StubConnection');
+    $reflection = new \ReflectionClass('Drupal\Tests\Core\Database\Stub\Connection');
     $filter_comment = $reflection->getMethod('filterComment');
     $filter_comment->setAccessible(TRUE);
 
diff --git a/core/tests/Drupal/Tests/Core/Database/DatabaseTest.php b/core/tests/Drupal/Tests/Core/Database/DatabaseTest.php
new file mode 100644
index 0000000..7c17d06
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Database/DatabaseTest.php
@@ -0,0 +1,176 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Tests\Core\Database\DatabaseTest.
+ */
+
+namespace Drupal\Tests\Core\Database;
+
+use Drupal\Core\Database\Database;
+use Drupal\Tests\UnitTestCase;
+
+/**
+ * @coversDefaultClass \Drupal\Core\Database\Database
+ * @group Database
+ */
+class DatabaseTest extends UnitTestCase {
+
+  /**
+   * @covers ::convertDbUrlToConnectionInfo
+   */
+  public function testNonExistentDriver() {
+    $this->setExpectedException(\RuntimeException::class, 'Can not convert blah://foo/bar to a database connection, class Drupal\Core\Database\Driver\blah\Connection does not exist');
+    Database::convertDbUrlToConnectionInfo('blah://foo/bar', '\root');
+  }
+
+  /**
+   * @covers ::convertDbUrlToConnectionInfo
+   */
+  public function testMalformedUrl() {
+    $this->setExpectedException(\InvalidArgumentException::class, 'Minimum requirement: driver://host/database');
+    Database::convertDbUrlToConnectionInfo('foo/bar', '\root');
+  }
+
+  public function testNoConnectionInfo() {
+    $this->setExpectedException(\RuntimeException::class, 'Database connection default not defined');
+    Database::getConnectionInfoAsUrl();
+  }
+
+  /**
+   * @covers ::convertDbUrlToConnectionInfo
+   * @dataProvider providerTestconvertDbUrlToConnectionInfo
+   */
+  public function testconvertDbUrlToConnectionInfo($url, array $expected) {
+    $connection_options = Database::convertDbUrlToConnectionInfo($url, '/root');
+    $this->assertSame($expected, $connection_options);
+  }
+
+
+  /**
+   * Data provider for self::testconvertDbUrlToConnectionInfo().
+   */
+  public function providerTestconvertDbUrlToConnectionInfo() {
+    $data = $this->baseConnectionConvertProviderData();
+    $data[] = [
+      'sqlite://localhost/database_file',
+      [
+        'driver' => 'sqlite',
+        'database' => '/root/database_file',
+        'prefix' => NULL,
+        'namespace' => '',
+      ],
+    ];
+    return $data;
+  }
+
+  /**
+   * @covers ::getConnectionInfoAsUrl
+   * @dataProvider providerGetConnectionInfoAsUrl
+   */
+  public function testGetConnectionInfoAsUrl($expected, array $connection_options) {
+    Database::setMultipleConnectionInfo(
+      ['default' => ['default' => $connection_options]]
+    );
+    $uri = Database::getConnectionInfoAsUrl();
+    // Clean up the static.
+    Database::removeConnection('default');
+    $this->assertSame($expected, $uri);
+  }
+
+  /**
+   * Data provider for self::testGetConnectionInfoAsUrl().
+   */
+  public function providerGetConnectionInfoAsUrl() {
+    $data = $this->baseConnectionConvertProviderData();
+    $data[] = [
+      'sqlite://localhost/database_file',
+      [
+        'driver' => 'sqlite',
+        'database' => 'database_file',
+        'prefix' => NULL,
+        'namespace' => '',
+      ],
+    ];
+    return $data;
+  }
+
+  /**
+   * Creates a generic set of data to test
+   */
+  protected function baseConnectionConvertProviderData() {
+    return [
+      [
+        'mysql://username:password@database_server/database_name',
+        [
+          'driver' => 'mysql',
+          'host' => 'database_server',
+          'database' => 'database_name',
+          'prefix' => NULL,
+          'namespace' => '',
+          'username' => 'username',
+          'password' => 'password',
+        ],
+      ],
+      [
+        'mysql://database_server/database_name',
+        [
+          'driver' => 'mysql',
+          'host' => 'database_server',
+          'database' => 'database_name',
+          'prefix' => NULL,
+          'namespace' => '',
+          'username' => '',
+          'password' => '',
+        ],
+      ],
+      [
+        'pgsql://username:password@database_server:9999/database_name',
+        [
+          'driver' => 'pgsql',
+          'host' => 'database_server',
+          'database' => 'database_name',
+          'prefix' => NULL,
+          'namespace' => '',
+          'port' => 9999,
+          'username' => 'username',
+          'password' => 'password',
+        ],
+      ],
+      [
+        'sqlite://localhost//absolute/database_file',
+        [
+          'driver' => 'sqlite',
+          'database' => '/absolute/database_file',
+          'prefix' => NULL,
+          'namespace' => '',
+        ],
+      ],
+      [
+        'mysql://username:password@database_server/database_name#prefix',
+        [
+          'driver' => 'mysql',
+          'host' => 'database_server',
+          'database' => 'database_name',
+          'prefix' => 'prefix',
+          'namespace' => '',
+          'username' => 'username',
+          'password' => 'password',
+        ],
+      ],
+      [
+        'stub://username:password@database_server/database_name?namespace=%5CDrupal%5CTests%5CCore%5CDatabase%5CStub',
+        [
+          'driver' => 'stub',
+          'host' => 'database_server',
+          'database' => 'database_name',
+          'prefix' => NULL,
+          'namespace' => '\Drupal\Tests\Core\Database\Stub',
+          'username' => 'username',
+          'password' => 'password',
+        ],
+      ],
+    ];
+  }
+
+}
\ No newline at end of file
diff --git a/core/tests/Drupal/Tests/Core/Database/Stub/StubConnection.php b/core/tests/Drupal/Tests/Core/Database/Stub/Connection.php
similarity index 88%
rename from core/tests/Drupal/Tests/Core/Database/Stub/StubConnection.php
rename to core/tests/Drupal/Tests/Core/Database/Stub/Connection.php
index a02de80..faedac1 100644
--- a/core/tests/Drupal/Tests/Core/Database/Stub/StubConnection.php
+++ b/core/tests/Drupal/Tests/Core/Database/Stub/Connection.php
@@ -2,12 +2,12 @@
 
 /**
  * @file
- * Contains \Drupal\Tests\Core\Database\Stub\StubConnection.
+ * Contains \Drupal\Tests\Core\Database\Stub\Connection.
  */
 
 namespace Drupal\Tests\Core\Database\Stub;
 
-use Drupal\Core\Database\Connection;
+use Drupal\Core\Database\Connection as DatabaseConnection;
 use Drupal\Core\Database\StatementEmpty;
 
 /**
@@ -15,7 +15,7 @@
  *
  * Includes minimal implementations of Connection's abstract methods.
  */
-class StubConnection extends Connection {
+class Connection extends DatabaseConnection {
 
   /**
    * Public property so we can test driver loading mechanism.
