Skip to content

Commit bb7ee1b

Browse files
committed
Improve database testing.
1 parent ff2a1cb commit bb7ee1b

File tree

10 files changed

+209
-78
lines changed

10 files changed

+209
-78
lines changed

.travis.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
language: php
22

33
php:
4-
- 5.4
5-
- 5.5
64
- 5.6
5+
- 5.5
76

8-
install: composer install
7+
install:
8+
- mysql -e 'CREATE DATABASE react_mysql_test;'
9+
- travis_retry composer install --prefer-source --no-interaction
910

10-
script: php vendor/bin/phpunit --coverage-text
11+
script:
12+
- php vendor/bin/phpunit --coverage-text

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Here is an example of what is currently working for the most part.
5454

5555
$loop = React\EventLoop\Factory::create();
5656

57-
ConnectionFactory::init($loop);
57+
ConnectionFactory::init($loop, ['db_host', 'db_user', 'db_pass', 'db_name']);
5858

5959
$db = new \DustinGraham\ReactMysql\Database();
6060

@@ -75,7 +75,7 @@ Here is an example of what is currently working for the most part.
7575
Here are some examples of how it may be, eventually.
7676
It would be nice to hide away some of the current boilerplate.
7777

78-
Connection::init($loop);
78+
Connection::init($loop, ['db_host', 'db_user', 'db_pass', 'db_name']);
7979

8080
Connection::query(
8181
'SELECT * FROM `table` WHERE `column` = ? AND `column2` = ?;',

run

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ echo 'Starting loop...'.PHP_EOL;
99

1010
$loop = React\EventLoop\Factory::create();
1111

12-
\DustinGraham\ReactMysql\ConnectionFactory::init($loop);
12+
\DustinGraham\ReactMysql\ConnectionFactory::init(
13+
$loop,
14+
['localhost', 'apache', 'apache', 'react_mysql_test']
15+
);
1316

1417
$db = new \DustinGraham\ReactMysql\Database();
1518
echo 'DB Created.'.PHP_EOL;
@@ -30,7 +33,7 @@ $loop->addPeriodicTimer(0.1, function ($timer) use (&$i, $db)
3033
echo "Run Query: $i\n";
3134

3235
$db->createCommand(
33-
'SELECT * FROM `react`.`react` WHERE id = :test',
36+
'SELECT * FROM `simple_table` WHERE id = :test',
3437
[':test' => $i]
3538
)->execute()->then(
3639
function($result)

src/Connection.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ public function escape($data)
3131
{
3232
if (is_array($data))
3333
{
34-
$data = array_map([$this, 'escape'], $data);
34+
$data = array_map([
35+
$this,
36+
'escape',
37+
], $data);
3538
}
3639
else
3740
{
@@ -79,13 +82,19 @@ function (TimerInterface $timer) use ($deferred)
7982
$deferred->resolve($result);
8083
}
8184
}
82-
else if ($error)
85+
else
8386
{
84-
$deferred->reject(new \Exception($this->mysqli->error));
85-
}
86-
else if ($reject)
87-
{
88-
$deferred->reject(new \Exception($this->mysqli->error));
87+
if ($error)
88+
{
89+
$deferred->reject(new \Exception($this->mysqli->error));
90+
}
91+
else
92+
{
93+
if ($reject)
94+
{
95+
$deferred->reject(new \Exception($this->mysqli->error));
96+
}
97+
}
8998
}
9099

91100
// If poll yielded something for this connection, we're done!

src/ConnectionFactory.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ class ConnectionFactory
99
*/
1010
protected static $loop;
1111

12-
public static function init(LoopInterface $loop)
12+
/**
13+
* @var array
14+
*/
15+
protected static $credentials;
16+
17+
public static function init(LoopInterface $loop, $credentials)
1318
{
1419
self::$loop = $loop;
20+
self::$credentials = $credentials;
1521
}
1622

1723
public static function createConnection()
@@ -21,8 +27,12 @@ public static function createConnection()
2127
throw new \Exception('Loop not provided.');
2228
}
2329

24-
//$mysqli = mysqli_connect('localhost', 'user', 'pass', 'dbname');
25-
$mysqli = mysqli_connect('localhost', 'react', 'react', 'react');
30+
$mysqli = new \mysqli(
31+
self::$credentials[0],
32+
self::$credentials[1],
33+
self::$credentials[2],
34+
self::$credentials[3]
35+
);
2636

2737
if ($mysqli === false)
2838
{

src/ConnectionPool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function getConnection()
6767

6868
/**
6969
* Once a connection has finished being used...
70-
* @param \mysqli $connection
70+
* @param Connection $connection
7171
*/
7272
public function releaseConnection(Connection $connection)
7373
{

tests/DatabaseTest.php

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,117 @@
11
<?php namespace DustinGraham\ReactMysql\Tests;
22

3-
class DatabaseTest extends TestCase
3+
use DustinGraham\ReactMysql\Command;
4+
use DustinGraham\ReactMysql\ConnectionFactory;
5+
use DustinGraham\ReactMysql\Database;
6+
use React\Promise\PromiseInterface;
7+
8+
class DatabaseTest extends TestCaseDatabase
49
{
5-
public $tableName = '`react`.`react`';
6-
710
public function testCommandClass()
811
{
9-
$database = new \DustinGraham\ReactMysql\Database();
12+
$database = new Database();
1013

1114
$command = $database->createCommand();
1215

13-
$this->assertInstanceOf(\DustinGraham\ReactMysql\Command::class, $command);
16+
$this->assertInstanceOf(Command::class, $command);
1417

1518
$command->bindValues([]);
1619
}
1720

1821
public function testMysqliConnection()
1922
{
20-
$c = \mysqli_connect('localhost', 'react', 'react', 'react');
23+
$c = $this->getMysqliConnection();
2124

2225
$this->assertInstanceOf(\mysqli::class, $c);
2326

2427
$this->assertNull($c->connect_error);
2528

2629
$this->assertEquals(0, $c->connect_errno);
2730

31+
$this->assertTrue(in_array($c->server_version, [
32+
50505,
33+
50547,
34+
]));
35+
2836
// Don't know if we care about these.
2937
// This is what the development environment was.
3038
// We can remove these as we understand them better.
3139
$this->assertEquals(10, $c->protocol_version);
3240
$this->assertEquals(50011, $c->client_version);
33-
$this->assertEquals(50505, $c->server_version);
3441
$this->assertEquals(0, $c->warning_count);
3542
$this->assertEquals('00000', $c->sqlstate);
36-
37-
$c->close();
3843
}
3944

4045
public function testMysqliSynchronous()
4146
{
42-
$c = \mysqli_connect('localhost', 'react', 'react', 'react');
47+
$c = $this->getMysqliConnection();
4348

44-
$result = $c->query('SELECT * FROM ' . $this->tableName);
45-
$this->assertEquals(3, $result->num_rows);
49+
$result = $c->query('SELECT * FROM simple_table;');
50+
$this->assertEquals(2, $result->num_rows);
4651

4752
$tempTableName = 'temptable123';
48-
$c->query('CREATE TEMPORARY TABLE ' . $tempTableName . ' LIKE ' . $this->tableName);
53+
$c->query('CREATE TEMPORARY TABLE ' . $tempTableName . ' LIKE simple_table;');
4954
$result = $c->query('SELECT * FROM ' . $tempTableName);
5055
$this->assertEquals(0, $result->num_rows);
5156

52-
$stmt = $c->prepare('INSERT INTO ' . $tempTableName . ' (`id`, `created_by`, `created_for`, `created_at`) VALUES (?, ?, ?, ?)');
53-
54-
$stmt->bind_param('isid', $id, $created_by, $created_for, $created_at);
57+
$stmt = $c->prepare('INSERT INTO ' . $tempTableName . ' (`id`, `name`) VALUES (?, ?)');
5558

5659
$id = null;
57-
$created_by = 'john';
58-
$created_for = 3;
59-
$created_at = 'NOW()';
60+
$name = 'john';
61+
62+
$stmt->bind_param('is', $id, $name);
6063

6164
$stmt->execute();
6265
$this->assertEquals(1, $stmt->affected_rows, 'Did not insert the row.');
6366
$stmt->close();
64-
65-
$c->close();
6667
}
6768

6869
public function testMysqliAsynchronous()
6970
{
70-
$c = \mysqli_connect('localhost', 'react', 'react', 'react');
71+
$c = $this->getMysqliConnection();
7172

72-
$c->query('SELECT * FROM ' . $this->tableName, MYSQLI_ASYNC);
73+
$c->query('SELECT * FROM simple_table;', MYSQLI_ASYNC);
7374

7475
$result = $c->reap_async_query();
75-
$this->assertEquals(3, $result->num_rows);
76-
77-
$c->close();
76+
$this->assertEquals(2, $result->num_rows);
7877
}
7978

8079
public function testCreateCommandGetPromise()
8180
{
82-
$db = new \DustinGraham\ReactMysql\Database();
81+
$db = new Database();
8382

8483
$cmd = $db->createCommand();
85-
$cmd->sql = 'SELECT * FROM ' . $this->tableName . ' WHERE id = :id';
84+
85+
$cmd->sql = 'SELECT * FROM simple_table WHERE id = :id';
8686
$cmd->bind(':id', 1);
8787

8888
$promise = $cmd->execute();
89-
$this->assertInstanceOf(\React\Promise\PromiseInterface::class, $promise);
89+
$this->assertInstanceOf(PromiseInterface::class, $promise);
9090

9191
////
9292

9393
$promise = $db->createCommand(
94-
'SELECT * FROM ' . $this->tableName . ' WHERE id = :test',
95-
[':test', 1]
94+
'SELECT * FROM simple_table WHERE id = :test',
95+
[
96+
':test',
97+
1,
98+
]
9699
)->execute();
97-
$this->assertInstanceOf(\React\Promise\PromiseInterface::class, $promise);
100+
$this->assertInstanceOf(PromiseInterface::class, $promise);
98101
}
99102

100103
public function testCommandResolvedResults()
101104
{
102105
$this->markTestSkipped('Still to do.');
103106

104107
$didItWork = false;
105-
$db = new \DustinGraham\ReactMysql\Database();
108+
$db = new Database();
106109
$db->createCommand(
107-
'SELECT * FROM ' . $this->tableName . ' WHERE id = :test',
108-
[':test', 1]
110+
'SELECT * FROM simple_table WHERE id = :test',
111+
[
112+
':test',
113+
1,
114+
]
109115
)->execute()->then(
110116
function ($results) use (&$didItWork)
111117
{
@@ -125,51 +131,45 @@ public function testAssertStrings()
125131

126132
public function testSimpleCommandParameterBinding()
127133
{
128-
$db = new \DustinGraham\ReactMysql\Database();
134+
$db = new Database();
129135

130136
$cmd = $db->createCommand();
131-
$cmd->sql = 'SELECT * FROM ' . $this->tableName . ' WHERE id = :id';
137+
$cmd->sql = 'SELECT * FROM simple_table WHERE id = :id';
132138
$cmd->bind(':id', 1);
133139

134-
$connection = \DustinGraham\ReactMysql\ConnectionFactory::createConnection();
140+
$connection = ConnectionFactory::createConnection();
135141
$query = $cmd->getPreparedQuery($connection);
136142

137-
$this->assertEquals('SELECT * FROM ' . $this->tableName . ' WHERE id = 1', $query);
143+
$this->assertEquals('SELECT * FROM simple_table WHERE id = 1', $query);
138144
}
139145

140146
public function testComplexCommandParameterBinding()
141147
{
142-
$this->markTestSkipped('TODO: Implement complex binding.');
148+
$this->markTestSkipped('TODO: Implement binding null values.');
143149

144-
$db = new \DustinGraham\ReactMysql\Database();
150+
$db = new Database();
145151

146152
$cmd = $db->createCommand();
147153
$cmd->sql = "
148-
INSERT INTO {$this->tableName} (
154+
INSERT INTO simple_table (
149155
`id`,
150-
`created_by`,
151-
`created_for`,
152-
`created_at`
156+
`name`
153157
) VALUES (
154158
:id,
155-
:created_by,
156-
:created_for,
157-
:created_at
159+
:name
158160
);
159161
";
160162

161163
$cmd->bind([
162164
':id' => null,
163-
':created_by' => 'neth',
164-
':created_for' => 3,
165-
':created_at' => 'NOW()',
165+
':name' => 'John Cash',
166166
]);
167167

168-
$connection = \DustinGraham\ReactMysql\ConnectionFactory::createConnection();
168+
$connection = ConnectionFactory::createConnection();
169169
$query = $cmd->getPreparedQuery($connection);
170170

171171
$this->assertStringEqualsIgnoreSpacing(
172-
"INSERT INTO `react`.`react` ( `id`, `created_by`, `created_for`, `created_at` ) VALUES ( NULL, 'test', '3', NOW() );",
172+
"INSERT INTO simple_table ( `id`, `name` ) VALUES ( NULL, 'John Cash' );",
173173
$query
174174
);
175175
}

tests/TestCase.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
<?php namespace DustinGraham\ReactMysql\Tests;
22

3-
use DustinGraham\ReactMysql\ConnectionFactory;
4-
53
class TestCase extends \PHPUnit_Framework_TestCase
64
{
75
public function setUp()
86
{
97
parent::setUp();
10-
11-
ConnectionFactory::init(
12-
\React\EventLoop\Factory::create()
13-
);
148
}
159

1610
public function assertStringEqualsIgnoreSpacing($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)

0 commit comments

Comments
 (0)