Skip to content

Commit ed13073

Browse files
authored
Merge pull request #34 from clue-labs/tests
Add test instructions and Travis CI
2 parents 854f02a + b25d2ca commit ed13073

File tree

10 files changed

+110
-41
lines changed

10 files changed

+110
-41
lines changed

.gitignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
.*
2-
*.lock
3-
vendor
4-
phpunit.xml
1+
vendor/
2+
composer.lock

.travis.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
language: php
2+
3+
php:
4+
- 5.4
5+
- 5.5
6+
- 5.6
7+
- 7.0
8+
- 7.1
9+
- 7.2
10+
- hhvm # ignore errors, see below
11+
12+
# lock distro so new future defaults will not break the build
13+
dist: trusty
14+
15+
matrix:
16+
allow_failures:
17+
- php: hhvm
18+
19+
services:
20+
- mysql
21+
22+
sudo: false
23+
24+
install:
25+
- composer install --no-interaction
26+
27+
before_script:
28+
- mysql -e 'CREATE DATABASE IF NOT EXISTS test;'
29+
30+
script:
31+
- DB_USER=root DB_PASSWD= ./vendor/bin/phpunit --coverage-text

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# MySQL
22

3+
[![Build Status](https://travis-ci.org/friends-of-reactphp/mysql.svg?branch=master)](https://travis-ci.org/friends-of-reactphp/mysql)
4+
35
Async, [Promise](https://github.com/reactphp/promise)-based MySQL database client
46
for [ReactPHP](https://reactphp.org/).
57

@@ -60,6 +62,41 @@ This will install the latest supported version:
6062
$ composer require react/mysql:^0.2
6163
```
6264

65+
This project aims to run on any platform and thus does not require any PHP
66+
extensions and supports running on legacy PHP 5.4 through current PHP 7+ and
67+
HHVM.
68+
It's *highly recommended to use PHP 7+* for this project.
69+
70+
## Tests
71+
72+
To run the test suite, you first need to clone this repo and then install all
73+
dependencies [through Composer](https://getcomposer.org):
74+
75+
```bash
76+
$ composer install
77+
```
78+
79+
The test suite contains a number of functional integration tests that send
80+
actual test SQL queries against your local database and thus rely on a local
81+
MySQL test database with appropriate write access.
82+
The test suite creates and modifies a test table in this database, so make sure
83+
to not a production database!
84+
You can change your test database credentials by passing these ENV variables:
85+
86+
```bash
87+
$ export DB_HOST=localhost
88+
$ export DB_PORT=3306
89+
$ export DB_USER=test
90+
$ export DB_PASSWD=test
91+
$ export DB_DBNAME=test
92+
```
93+
94+
To run the test suite, go to the project root and run:
95+
96+
```bash
97+
$ php vendor/bin/phpunit
98+
```
99+
63100
## License
64101

65102
MIT, see [LICENSE file](LICENSE).

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@
99
"react/dns": "0.4.*"
1010
},
1111
"require-dev": {
12-
"phpunit/dbunit": "1.4.*"
12+
"phpunit/dbunit": "1.4.*",
13+
"ext-pdo_mysql": "*"
1314
},
1415
"autoload": {
1516
"psr-4": {
1617
"React\\MySQL\\": "src"
1718
}
19+
},
20+
"autoload-dev": {
21+
"psr-4": {
22+
"React\\Tests\\MySQL\\": "tests"
23+
}
1824
}
1925
}

phpunit.xml.dist

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
processIsolation="false"
1010
stopOnFailure="false"
1111
syntaxCheck="false"
12-
bootstrap="tests/bootstrap.php"
12+
bootstrap="vendor/autoload.php"
1313
>
1414
<testsuites>
1515
<testsuite name="React.MySQL Test Suite">
@@ -23,9 +23,10 @@
2323
</whitelist>
2424
</filter>
2525
<php>
26-
<var name="db_dsn" value="mysql:dbname=test;host=localhost"/>
27-
<var name="db_user" value="test" />
28-
<var name="db_passwd" value="test" />
29-
<var name="db_dbname" value="test" />
26+
<env name="DB_HOST" value="localhost"/>
27+
<env name="DB_PORT" value="3306"/>
28+
<env name="DB_USER" value="test" />
29+
<env name="DB_PASSWD" value="test" />
30+
<env name="DB_DBNAME" value="test" />
3031
</php>
3132
</phpunit>

tests/BaseTestCase.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ protected function getConnection()
1111
{
1212
if ($this->conn === null) {
1313
if (self::$pdo == null) {
14-
self::$pdo = new \PDO($GLOBALS['db_dsn'], $GLOBALS['db_user'], $GLOBALS['db_passwd']);
14+
$conf = $this->getConnectionOptions();
15+
$dsn = 'mysql:host=' . $conf['host'] . ';port=' . $conf['port'] . ';dbname=' . $conf['dbname'];
16+
self::$pdo = new \PDO($dsn, $conf['user'], $conf['passwd']);
1517
}
1618

1719
self::$pdo->query('
1820
CREATE TABLE IF NOT EXISTS `book` (
19-
`id` INT(11) NOT NULL,
21+
`id` INT(11) NOT NULL AUTO_INCREMENT,
2022
`name` VARCHAR(255) NOT NULL,
21-
`isbn` VARCHAR(255) NOT NULL,
22-
`author` VARCHAR(255) NOT NULL,
23-
`created` INT(11) NOT NULL,
23+
`isbn` VARCHAR(255) NULL,
24+
`author` VARCHAR(255) NULL,
25+
`created` INT(11) NULL,
2426
PRIMARY KEY (`id`)
2527
)
2628
');
@@ -39,9 +41,11 @@ protected function getDataSet()
3941
protected function getConnectionOptions()
4042
{
4143
return [
42-
'dbname' => $GLOBALS['db_dbname'],
43-
'user' => $GLOBALS['db_user'],
44-
'passwd' => $GLOBALS['db_passwd'],
44+
'host' => getenv('DB_HOST'),
45+
'port' => (int)getenv('DB_PORT'),
46+
'dbname' => getenv('DB_DBNAME'),
47+
'user' => getenv('DB_USER'),
48+
'passwd' => getenv('DB_PASSWD'),
4549
];
4650
}
4751
}

tests/ConnectionTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ public function testConnectWithInvalidPass()
1515

1616
$conn->connect(function ($err, $conn) use ($loop, $options) {
1717
$this->assertEquals(sprintf(
18-
"Access denied for user '%s'@'localhost' (using password: YES)",
19-
$options['user']
18+
"Access denied for user '%s'@'%s' (using password: YES)",
19+
$options['user'],
20+
$options['host']
2021
), $err->getMessage());
2122
$this->assertInstanceOf('React\MySQL\Connection', $conn);
2223
//$loop->stop();

tests/NoResultQueryTest.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,13 @@
22

33
namespace React\Tests\MySQL;
44

5-
use React\MySQL\Query;
6-
75
class NoResultQueryTest extends BaseTestCase
86
{
97
public function testUpdateSimple()
108
{
119
$loop = \React\EventLoop\Factory::create();
1210

13-
$connection = new \React\MySQL\Connection($loop, array(
14-
'dbname' => 'test',
15-
'user' => 'test',
16-
'passwd' => 'test',
17-
));
11+
$connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
1812

1913
$connection->connect(function () {});
2014
$that = $this;
@@ -31,11 +25,7 @@ public function testInsertSimple()
3125
{
3226
$loop = \React\EventLoop\Factory::create();
3327

34-
$connection = new \React\MySQL\Connection($loop, array(
35-
'dbname' => 'test',
36-
'user' => 'test',
37-
'passwd' => 'test',
38-
));
28+
$connection = new \React\MySQL\Connection($loop, $this->getConnectionOptions());
3929

4030
$connection->connect(function () {});
4131

tests/ResultQueryTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ public function testSimpleSelect()
1818
$loop->stop();
1919
});
2020
$loop->run();
21+
}
22+
23+
public function testInvalidSelect()
24+
{
25+
$loop = \React\EventLoop\Factory::create();
2126

27+
$options = $this->getConnectionOptions();
28+
$db = $options['dbname'];
29+
$connection = new \React\MySQL\Connection($loop, $options);
2230
$connection->connect(function () {});
2331

24-
$connection->query('select * from invalid_table', function ($command, $conn) use ($loop) {
32+
$connection->query('select * from invalid_table', function ($command, $conn) use ($loop, $db) {
2533
$this->assertEquals(true, $command->hasError());
26-
$this->assertEquals("Table 'test.invalid_table' doesn't exist", $command->getError()->getMessage());
34+
$this->assertEquals("Table '$db.invalid_table' doesn't exist", $command->getError()->getMessage());
2735

2836
$loop->stop();
2937
});

tests/bootstrap.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)