Skip to content

Commit e60e289

Browse files
committed
Test instructions and support ENV variables to change DB credentials
1 parent 9993aaa commit e60e289

File tree

8 files changed

+70
-37
lines changed

8 files changed

+70
-37
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,36 @@ This will install the latest supported version:
6060
$ composer require react/mysql:^0.2
6161
```
6262

63+
## Tests
64+
65+
To run the test suite, you first need to clone this repo and then install all
66+
dependencies [through Composer](https://getcomposer.org):
67+
68+
```bash
69+
$ composer install
70+
```
71+
72+
The test suite contains a number of functional integration tests that send
73+
actual test SQL queries against your local database and thus rely on a local
74+
MySQL test database with appropriate write access.
75+
The test suite creates and modifies a test table in this database, so make sure
76+
to not a production database!
77+
You can change your test database credentials by passing these ENV variables:
78+
79+
```bash
80+
$ export DB_HOST=localhost
81+
$ export DB_PORT=3306
82+
$ export DB_USER=test
83+
$ export DB_PASSWD=test
84+
$ export DB_DBNAME=test
85+
```
86+
87+
To run the test suite, go to the project root and run:
88+
89+
```bash
90+
$ php vendor/bin/phpunit
91+
```
92+
6393
## License
6494

6595
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)