From b8f8de8ae6df623ab8a72f0699914fbbaa7e9757 Mon Sep 17 00:00:00 2001 From: Hidde Boomsma Date: Fri, 13 Oct 2017 14:22:25 +0200 Subject: [PATCH] Make use of already started mysql database on Travis --- .travis.yml | 3 ++- bin/mysql_travis.sh | 26 ++++++++++++++++++++++++++ composer.json | 22 +++++++++------------- src/ConnectionInterface.php | 3 ++- src/MysqlPersistentConnection.php | 18 +++++++++++------- test/MysqlPersistentConnectionTest.php | 16 ++++++++++------ 6 files changed, 60 insertions(+), 28 deletions(-) create mode 100755 bin/mysql_travis.sh diff --git a/.travis.yml b/.travis.yml index fff1b84..7117037 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,8 +4,8 @@ services: - mysql php: - - 5.6 - 7.0 + - 7.1 - nightly matrix: @@ -18,4 +18,5 @@ before_script: script: - vendor/bin/phpunit --coverage-text + - cat $HOME/database_test.log - vendor/bin/phpcs diff --git a/bin/mysql_travis.sh b/bin/mysql_travis.sh new file mode 100755 index 0000000..cdce2c9 --- /dev/null +++ b/bin/mysql_travis.sh @@ -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 diff --git a/composer.json b/composer.json index 414710a..ca80f08 100644 --- a/composer.json +++ b/composer.json @@ -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": { @@ -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" - ] + } } diff --git a/src/ConnectionInterface.php b/src/ConnectionInterface.php index 139bb38..6b6f434 100644 --- a/src/ConnectionInterface.php +++ b/src/ConnectionInterface.php @@ -2,6 +2,7 @@ /** * @copyright 2016-2017 Hostnet B.V. */ +declare(strict_types = 1); namespace Hostnet\Component\DatabaseTest; /** @@ -21,5 +22,5 @@ interface ConnectionInterface * * @return array */ - public function getConnectionParams(); + public function getConnectionParams(): array; } diff --git a/src/MysqlPersistentConnection.php b/src/MysqlPersistentConnection.php index ed32b3e..f55543e 100644 --- a/src/MysqlPersistentConnection.php +++ b/src/MysqlPersistentConnection.php @@ -2,6 +2,7 @@ /** * @copyright 2016-2017 Hostnet B.V. */ +declare(strict_types = 1); namespace Hostnet\Component\DatabaseTest; /** @@ -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 */ @@ -34,12 +37,12 @@ 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. @@ -47,17 +50,18 @@ class MysqlPersistentConnection implements ConnectionInterface 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); @@ -78,7 +82,7 @@ public function __destruct() * {@inheritdoc} * @return array */ - public function getConnectionParams() + public function getConnectionParams(): array { return $this->connection_params; } diff --git a/test/MysqlPersistentConnectionTest.php b/test/MysqlPersistentConnectionTest.php index fe33a69..a3b83f4 100644 --- a/test/MysqlPersistentConnectionTest.php +++ b/test/MysqlPersistentConnectionTest.php @@ -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)); } /** @@ -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)); } /** @@ -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'); @@ -63,6 +66,7 @@ function ($database) { case 'mysql': case 'information_schema': case 'performance_schema': + case 'travis': return false; default: return true;