Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/MysqlPersistentConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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'
);
}
}
28 changes: 28 additions & 0 deletions src/UrlConnectionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* @copyright 2016-2017 Hostnet B.V.
*/
declare(strict_types=1);

namespace Hostnet\Component\DatabaseTest;

/**
* A connection will make sure a (new) test database
* will exist after construction and will clean up
* after destruction.
*
* The connection url for the database is
* provided through the getConnectionUrl method.
*/
interface UrlConnectionInterface
{
/**
* Doctrine compatible database connection parameters,
* those could be fed directly into a new Doctrine
* or Symfony/Doctrine configuration file.
*
* @Example mysql://db_user:db_password@127.0.0.1:3306/db_name?server_version=5.6
* mysql://db_user@localhost/db_name?unix_socket=/tmp/socket&server_version=5.6
*/
public function getConnectionUrl(): string;
}
4 changes: 4 additions & 0 deletions test/MysqlPersistentConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public function testConstruction()
$connection = new MysqlPersistentConnection();
$params = $connection->getConnectionParams();
$this->assertCount(1, $this->listDatabases($params));

$connection = new MysqlPersistentConnection();
$url = $connection->getConnectionUrl();
$this->assertCount(1, $this->listDatabases(['url' => $url]));
}

/**
Expand Down