Skip to content

Commit 01fcd41

Browse files
Hidde Boomsmanicoschoenmaker
authored andcommitted
add getConnectionUrl() with interface (#8)
1 parent 1d8cc18 commit 01fcd41

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

src/MysqlPersistentConnection.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* When PHP exists or crashes, the bash script will notice and
1717
* remove the database(s).
1818
*/
19-
class MysqlPersistentConnection implements ConnectionInterface
19+
class MysqlPersistentConnection implements ConnectionInterface, UrlConnectionInterface
2020
{
2121
/**
2222
* Bash script taking care of daemon start,
@@ -97,4 +97,30 @@ public function getConnectionParams()
9797
{
9898
return $this->connection_params;
9999
}
100+
101+
/**
102+
* {@inheritdoc}
103+
* @return string
104+
*/
105+
public function getConnectionUrl(): string
106+
{
107+
if (array_key_exists('unix_socket', $this->connection_params)) {
108+
return sprintf(
109+
'mysql://%s@localhost/%s?unix_socket=%s&server_version=%s',
110+
$this->connection_params['user'] ?? get_current_user(),
111+
$this->connection_params['dbname'] ?? 'test',
112+
$this->connection_params['unix_socket'],
113+
$this->connection_params['server_version'] ?? '5.6'
114+
);
115+
}
116+
117+
return sprintf(
118+
'mysql://%s@%s:%s/%s?server_version=%s',
119+
$this->connection_params['user'] ?? get_current_user(),
120+
$this->connection_params['hostname'] ?? 'localhost',
121+
$this->connection_params['port'] ?? 3306,
122+
$this->connection_params['dbname'] ?? 'test',
123+
$this->connection_params['server_version'] ?? '5.6'
124+
);
125+
}
100126
}

src/UrlConnectionInterface.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* @copyright 2016-2017 Hostnet B.V.
4+
*/
5+
declare(strict_types=1);
6+
7+
namespace Hostnet\Component\DatabaseTest;
8+
9+
/**
10+
* A connection will make sure a (new) test database
11+
* will exist after construction and will clean up
12+
* after destruction.
13+
*
14+
* The connection url for the database is
15+
* provided through the getConnectionUrl method.
16+
*/
17+
interface UrlConnectionInterface
18+
{
19+
/**
20+
* Doctrine compatible database connection parameters,
21+
* those could be fed directly into a new Doctrine
22+
* or Symfony/Doctrine configuration file.
23+
*
24+
* @Example mysql://db_user:db_password@127.0.0.1:3306/db_name?server_version=5.6
25+
* mysql://db_user@localhost/db_name?unix_socket=/tmp/socket&server_version=5.6
26+
*/
27+
public function getConnectionUrl(): string;
28+
}

test/MysqlPersistentConnectionTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public function testConstruction()
1919
$connection = new MysqlPersistentConnection();
2020
$params = $connection->getConnectionParams();
2121
$this->assertCount(1, $this->listDatabases($params));
22+
23+
$connection = new MysqlPersistentConnection();
24+
$url = $connection->getConnectionUrl();
25+
$this->assertCount(1, $this->listDatabases(['url' => $url]));
2226
}
2327

2428
/**

0 commit comments

Comments
 (0)