Skip to content

Commit 9bd492f

Browse files
Hidde Boomsmayannickl88
authored andcommitted
Make use of already started mysql database on Travis (#4)
1 parent 4ea44fe commit 9bd492f

File tree

6 files changed

+60
-28
lines changed

6 files changed

+60
-28
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ services:
44
- mysql
55

66
php:
7-
- 5.6
87
- 7.0
8+
- 7.1
99
- nightly
1010

1111
matrix:
@@ -18,4 +18,5 @@ before_script:
1818

1919
script:
2020
- vendor/bin/phpunit --coverage-text
21+
- cat $HOME/database_test.log
2122
- vendor/bin/phpcs

bin/mysql_travis.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
# Configure data locations
4+
LOG="$HOME/database_test.log"
5+
6+
exec 3>> "$LOG"
7+
8+
finish() {
9+
echo "### Cleaning Database $DBNAME" >&3
10+
echo "DROP DATABASE \`$DBNAME\`;" | mysql >&3
11+
}
12+
13+
# Create the database
14+
DBNAME="$(date +%s%N)"
15+
echo "CREATE DATABASE \`$DBNAME\`;" | mysql >&3
16+
17+
echo "### Trap Set" >&3
18+
trap finish EXIT
19+
20+
echo "driver: pdo_mysql, server_version: 5.6, host: localhost, dbname: ${DBNAME}, user: root"
21+
echo "### Connection params sent" >&3
22+
23+
# Wait on parent process before cleaning up the database
24+
while read -r DATA; do
25+
sleep .1
26+
done

composer.json

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
{
22
"name": "hostnet/database-test-lib",
3-
"description": "Provide a real database, safe for testing purposes",
43
"type": "lib",
4+
"description": "Provide a real database, safe for testing purposes",
55
"license": "MIT",
6-
"authors": [
7-
{
8-
"name": "Hidde Boomsma",
9-
"email": "hboomsma@hostnet.nl"
10-
}
11-
],
126
"require": {
7+
"php": "^7.0",
138
"doctrine/orm": "^2.5.4"
149
},
1510
"require-dev": {
16-
"hostnet/phpcs-tool": "^4.0.6",
17-
"phpunit/phpunit": "^5.3.2"
11+
"hostnet/phpcs-tool": "^5.2.1",
12+
"phpunit/phpunit": "^6.4.1"
1813
},
1914
"autoload": {
2015
"psr-4": {
@@ -26,12 +21,13 @@
2621
"Hostnet\\Component\\DatabaseTest\\": "test/"
2722
}
2823
},
24+
"bin": [
25+
"bin/mysql_persistent.sh",
26+
"bin/mysql_travis.sh"
27+
],
2928
"archive": {
3029
"exclude": [
3130
"/test"
3231
]
33-
},
34-
"bin": [
35-
"bin/mysql_persistent.sh"
36-
]
32+
}
3733
}

src/ConnectionInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/**
33
* @copyright 2016-2017 Hostnet B.V.
44
*/
5+
declare(strict_types = 1);
56
namespace Hostnet\Component\DatabaseTest;
67

78
/**
@@ -21,5 +22,5 @@ interface ConnectionInterface
2122
*
2223
* @return array
2324
*/
24-
public function getConnectionParams();
25+
public function getConnectionParams(): array;
2526
}

src/MysqlPersistentConnection.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/**
33
* @copyright 2016-2017 Hostnet B.V.
44
*/
5+
declare(strict_types = 1);
56
namespace Hostnet\Component\DatabaseTest;
67

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

30+
const CMD_TRAVIS = __DIR__ . '/../bin/mysql_travis.sh';
31+
2932
/**
3033
* @var array
3134
*/
@@ -34,30 +37,31 @@ class MysqlPersistentConnection implements ConnectionInterface
3437
/**
3538
* @var resource
3639
*/
37-
private $pipe = null;
40+
private $pipe;
3841

3942
/**
4043
* @var resource
4144
*/
42-
private $process = null;
45+
private $process;
4346

4447
/**
4548
* Start the daemon if needed and create a database.
4649
*/
4750
public function __construct()
4851
{
4952
$descriptor_spec = [
50-
0 => ["pipe", "r"], // stdin is a pipe that the child will read from
51-
1 => ["pipe", "w"], // stdout is a pipe that the child will write to
53+
0 => ['pipe', 'r'], // stdin is a pipe that the child will read from
54+
1 => ['pipe', 'w'], // stdout is a pipe that the child will write to
5255
];
5356

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

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

60-
foreach (explode(",", $data) as $param) {
64+
foreach (explode(',', $data) as $param) {
6165
if (strpos($param, ':') !== false) {
6266
list($key, $value) = explode(':', $param);
6367
$this->connection_params[trim($key)] = trim($value);
@@ -78,7 +82,7 @@ public function __destruct()
7882
* {@inheritdoc}
7983
* @return array
8084
*/
81-
public function getConnectionParams()
85+
public function getConnectionParams(): array
8286
{
8387
return $this->connection_params;
8488
}

test/MysqlPersistentConnectionTest.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@
22
/**
33
* @copyright 2016-2017 Hostnet B.V.
44
*/
5+
declare(strict_types=1);
6+
57
namespace Hostnet\Component\DatabaseTest;
68

79
use Doctrine\DBAL\DriverManager;
10+
use PHPUnit\Framework\TestCase;
811

912
/**
10-
* @covers Hostnet\Component\DatabaseTest\MysqlPersistentConnection
13+
* @covers \Hostnet\Component\DatabaseTest\MysqlPersistentConnection
1114
*/
12-
class MysqlPersistentConnectionTest extends \PHPUnit_Framework_TestCase
15+
class MysqlPersistentConnectionTest extends TestCase
1316
{
1417
public function testConstruction()
1518
{
1619
$connection = new MysqlPersistentConnection();
1720
$params = $connection->getConnectionParams();
18-
$this->assertEquals(1, count($this->listDatabases($params)));
21+
$this->assertCount(1, $this->listDatabases($params));
1922
}
2023

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

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

4447
/**
@@ -50,7 +53,7 @@ public function testDestruction()
5053
* @return string[]
5154
* @throws \Doctrine\DBAL\DBALException
5255
*/
53-
private function listDatabases(array $params)
56+
private function listDatabases(array $params): array
5457
{
5558
$doctrine = DriverManager::getConnection($params);
5659
$statement = $doctrine->executeQuery('SHOW DATABASES');
@@ -63,6 +66,7 @@ function ($database) {
6366
case 'mysql':
6467
case 'information_schema':
6568
case 'performance_schema':
69+
case 'travis':
6670
return false;
6771
default:
6872
return true;

0 commit comments

Comments
 (0)