From 00b22adba28adb2ba046ecce62c48824de18f2f8 Mon Sep 17 00:00:00 2001 From: Hidde Boomsma Date: Mon, 7 May 2018 15:59:30 +0200 Subject: [PATCH] add getConnectionUrl() with interface --- src/MysqlPersistentConnection.php | 28 +++++++++++++++++++++++++- src/UrlConnectionInterface.php | 28 ++++++++++++++++++++++++++ test/MysqlPersistentConnectionTest.php | 4 ++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/UrlConnectionInterface.php 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])); } /**