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
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ services:
- mysql

php:
- 5.6
- 7.0
- 7.1
- nightly

matrix:
Expand All @@ -18,4 +18,5 @@ before_script:

script:
- vendor/bin/phpunit --coverage-text
- cat $HOME/database_test.log
- vendor/bin/phpcs
26 changes: 26 additions & 0 deletions bin/mysql_travis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

# Configure data locations
LOG="$HOME/database_test.log"

exec 3>> "$LOG"

finish() {
echo "### Cleaning Database $DBNAME" >&3
echo "DROP DATABASE \`$DBNAME\`;" | mysql >&3
}

# Create the database
DBNAME="$(date +%s%N)"
echo "CREATE DATABASE \`$DBNAME\`;" | mysql >&3

echo "### Trap Set" >&3
trap finish EXIT

echo "driver: pdo_mysql, server_version: 5.6, host: localhost, dbname: ${DBNAME}, user: root"
echo "### Connection params sent" >&3

# Wait on parent process before cleaning up the database
while read -r DATA; do
sleep .1
done
22 changes: 9 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
{
"name": "hostnet/database-test-lib",
"description": "Provide a real database, safe for testing purposes",
"type": "lib",
"description": "Provide a real database, safe for testing purposes",
"license": "MIT",
"authors": [
{
"name": "Hidde Boomsma",
"email": "hboomsma@hostnet.nl"
}
],
"require": {
"php": "^7.0",
"doctrine/orm": "^2.5.4"
},
"require-dev": {
"hostnet/phpcs-tool": "^4.0.6",
"phpunit/phpunit": "^5.3.2"
"hostnet/phpcs-tool": "^5.2.1",
"phpunit/phpunit": "^6.4.1"
},
"autoload": {
"psr-4": {
Expand All @@ -26,12 +21,13 @@
"Hostnet\\Component\\DatabaseTest\\": "test/"
}
},
"bin": [
"bin/mysql_persistent.sh",
"bin/mysql_travis.sh"
],
"archive": {
"exclude": [
"/test"
]
},
"bin": [
"bin/mysql_persistent.sh"
]
}
}
3 changes: 2 additions & 1 deletion src/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/**
* @copyright 2016-2017 Hostnet B.V.
*/
declare(strict_types = 1);
namespace Hostnet\Component\DatabaseTest;

/**
Expand All @@ -21,5 +22,5 @@ interface ConnectionInterface
*
* @return array
*/
public function getConnectionParams();
public function getConnectionParams(): array;
}
18 changes: 11 additions & 7 deletions src/MysqlPersistentConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/**
* @copyright 2016-2017 Hostnet B.V.
*/
declare(strict_types = 1);
namespace Hostnet\Component\DatabaseTest;

/**
Expand All @@ -26,6 +27,8 @@ class MysqlPersistentConnection implements ConnectionInterface
*/
const CMD_PERSISTENT = __DIR__ . '/../bin/mysql_persistent.sh';

const CMD_TRAVIS = __DIR__ . '/../bin/mysql_travis.sh';

/**
* @var array
*/
Expand All @@ -34,30 +37,31 @@ class MysqlPersistentConnection implements ConnectionInterface
/**
* @var resource
*/
private $pipe = null;
private $pipe;

/**
* @var resource
*/
private $process = null;
private $process;

/**
* Start the daemon if needed and create a database.
*/
public function __construct()
{
$descriptor_spec = [
0 => ["pipe", "r"], // stdin is a pipe that the child will read from
1 => ["pipe", "w"], // stdout is a pipe that the child will write to
0 => ['pipe', 'r'], // stdin is a pipe that the child will read from
1 => ['pipe', 'w'], // stdout is a pipe that the child will write to
];

$this->process = proc_open(self::CMD_PERSISTENT, $descriptor_spec, $pipes);
$cmd = getenv('TRAVIS') ? self::CMD_TRAVIS : self::CMD_PERSISTENT;
$this->process = proc_open($cmd, $descriptor_spec, $pipes);
$data = fread($pipes[1], 1024);

fclose($pipes[1]);
$this->pipe = $pipes[0];

foreach (explode(",", $data) as $param) {
foreach (explode(',', $data) as $param) {
if (strpos($param, ':') !== false) {
list($key, $value) = explode(':', $param);
$this->connection_params[trim($key)] = trim($value);
Expand All @@ -78,7 +82,7 @@ public function __destruct()
* {@inheritdoc}
* @return array
*/
public function getConnectionParams()
public function getConnectionParams(): array
{
return $this->connection_params;
}
Expand Down
16 changes: 10 additions & 6 deletions test/MysqlPersistentConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
/**
* @copyright 2016-2017 Hostnet B.V.
*/
declare(strict_types=1);

namespace Hostnet\Component\DatabaseTest;

use Doctrine\DBAL\DriverManager;
use PHPUnit\Framework\TestCase;

/**
* @covers Hostnet\Component\DatabaseTest\MysqlPersistentConnection
* @covers \Hostnet\Component\DatabaseTest\MysqlPersistentConnection
*/
class MysqlPersistentConnectionTest extends \PHPUnit_Framework_TestCase
class MysqlPersistentConnectionTest extends TestCase
{
public function testConstruction()
{
$connection = new MysqlPersistentConnection();
$params = $connection->getConnectionParams();
$this->assertEquals(1, count($this->listDatabases($params)));
$this->assertCount(1, $this->listDatabases($params));
}

/**
Expand All @@ -34,11 +37,11 @@ public function testDestruction()
//
// Having two databases now ensures that multiple databases with
// unique names are created by the connection class.
$this->assertEquals(2, count($this->listDatabases($params)));
$this->assertCount(2, $this->listDatabases($params));

// If a connection disappears, it should clean up it's databases.
unset($tmp);
$this->assertEquals(1, count($this->listDatabases($params)));
$this->assertCount(1, $this->listDatabases($params));
}

/**
Expand All @@ -50,7 +53,7 @@ public function testDestruction()
* @return string[]
* @throws \Doctrine\DBAL\DBALException
*/
private function listDatabases(array $params)
private function listDatabases(array $params): array
{
$doctrine = DriverManager::getConnection($params);
$statement = $doctrine->executeQuery('SHOW DATABASES');
Expand All @@ -63,6 +66,7 @@ function ($database) {
case 'mysql':
case 'information_schema':
case 'performance_schema':
case 'travis':
return false;
default:
return true;
Expand Down