diff --git a/src/MysqlPersistentConnection.php b/src/MysqlPersistentConnection.php index 74b8e0e..adb09fb 100644 --- a/src/MysqlPersistentConnection.php +++ b/src/MysqlPersistentConnection.php @@ -16,7 +16,7 @@ * When PHP exists or crashes, the bash script will notice and * remove the database(s). */ -class MysqlPersistentConnection implements ConnectionInterface +class MysqlPersistentConnection implements ConnectionInterface, UrlConnectionInterface { /** * Bash script taking care of daemon start, @@ -97,4 +97,30 @@ public function getConnectionParams() { return $this->connection_params; } + + /** + * {@inheritdoc} + * @return string + */ + public function getConnectionUrl(): string + { + if (array_key_exists('unix_socket', $this->connection_params)) { + return sprintf( + 'mysql://%s@localhost/%s?unix_socket=%s&server_version=%s', + $this->connection_params['user'] ?? get_current_user(), + $this->connection_params['dbname'] ?? 'test', + $this->connection_params['unix_socket'], + $this->connection_params['server_version'] ?? '5.6' + ); + } + + return sprintf( + 'mysql://%s@%s:%s/%s?server_version=%s', + $this->connection_params['user'] ?? get_current_user(), + $this->connection_params['hostname'] ?? 'localhost', + $this->connection_params['port'] ?? 3306, + $this->connection_params['dbname'] ?? 'test', + $this->connection_params['server_version'] ?? '5.6' + ); + } } diff --git a/src/UrlConnectionInterface.php b/src/UrlConnectionInterface.php new file mode 100644 index 0000000..168a63a --- /dev/null +++ b/src/UrlConnectionInterface.php @@ -0,0 +1,28 @@ +getConnectionParams(); $this->assertCount(1, $this->listDatabases($params)); + + $connection = new MysqlPersistentConnection(); + $url = $connection->getConnectionUrl(); + $this->assertCount(1, $this->listDatabases(['url' => $url])); } /**