Skip to content

Commit e1b1558

Browse files
committed
Update tests
1 parent 8b2a31a commit e1b1558

File tree

3 files changed

+90
-89
lines changed

3 files changed

+90
-89
lines changed

test/Mysql/ConnectionTest.php

+73-43
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
<?php
22

3-
use Amp\Mysql\Connection;
43
use Amp\Mysql\DataTypes;
4+
use Amp\Mysql\ResultSet;
55
use PHPUnit\Framework\TestCase;
6+
use function Amp\Mysql\connect;
67

78
class ConnectionTest extends TestCase {
89
function testConnect() {
910
$complete = false;
1011
\Amp\Loop::run(function() use (&$complete) {
11-
$db = new Connection("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=connectiontest");
12-
yield $db->connect();
12+
/** @var \Amp\Mysql\Connection $db */
13+
$db = yield connect("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=connectiontest");
1314

1415
/* use an alternative charset... Default is utf8mb4_general_ci */
15-
$db->setCharset("latin1_general_ci");
16+
yield $db->setCharset("latin1_general_ci");
1617

1718
$db->close();
1819
$complete = true;
@@ -22,38 +23,35 @@ function testConnect() {
2223

2324
function testQuery() {
2425
\Amp\Loop::run(function() {
25-
$db = new Connection("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=connectiontest");
26-
$db->connect();
26+
/** @var \Amp\Mysql\Connection $db */
27+
$db = yield connect("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=connectiontest");
2728

29+
/** @var \Amp\Mysql\ResultSet $resultset */
2830
$resultset = yield $db->query("SELECT 1 AS a");
29-
$this->assertEquals(yield $resultset->rowCount(), 1);
30-
$this->assertEquals(yield $resultset->fetch(), ["a" => 1, 0 => 1]);
31-
$resultset = yield $db->query("SELECT 1 AS a");
32-
$this->assertEquals(yield $resultset->fetchRow(), [0 => 1]);
33-
$resultset = yield $db->query("SELECT 1 AS a");
34-
$this->assertEquals(yield $resultset->fetchObject(), (object) ["a" => 1]);
3531

36-
$this->assertEquals(yield $resultset->fetchAll(), [["a" => 1, 0 => 1]]);
37-
$this->assertEquals(yield $resultset->fetchRows(), [[0 => 1]]);
38-
$this->assertEquals(yield $resultset->fetchObjects(), [(object) ["a" => 1]]);
32+
for ($i = 0; yield $resultset->advance(); ++$i) {
33+
$this->assertEquals(["a" => 1], $resultset->getCurrent());
34+
}
35+
36+
$this->assertSame(1, $i);
3937
});
4038
}
4139

4240
function testQueryFetchRow() {
4341
\Amp\Loop::run(function () {
44-
$db = new Connection("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=connectiontest");
45-
$db->connect();
42+
/** @var \Amp\Mysql\Connection $db */
43+
$db = yield connect("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=connectiontest");
4644

4745
$db->query('DROP TABLE tmp');
4846
$db->query('CREATE TABLE tmp (a int)');
4947
$db->query('INSERT INTO tmp VALUES (1), (2), (3)');
5048

49+
/** @var \Amp\Mysql\ResultSet $resultset */
5150
$resultset = yield $db->query('SELECT a FROM tmp');
5251

5352
$got = [];
54-
55-
while ($row = yield $resultset->fetchRow()) {
56-
$got[] = $row;
53+
while (yield $resultset->advance(ResultSet::FETCH_ARRAY)) {
54+
$got[] = $resultset->getCurrent();
5755
}
5856

5957
$this->assertEquals($got, [[1], [2], [3]]);
@@ -62,25 +60,26 @@ function testQueryFetchRow() {
6260

6361
function testMultiStmt() {
6462
\Amp\Loop::run(function() {
65-
$db = new Connection("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=connectiontest");
66-
$db->connect();
63+
/** @var \Amp\Mysql\Connection $db */
64+
$db = yield connect("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=connectiontest");
6765

6866
$db->query("CREATE DATABASE IF NOT EXISTS alt");
6967
$db->useDb("alt");
7068

7169
$db->query("DROP TABLE tmp"); // just in case it would exist...
7270
$db->query("CREATE TABLE tmp SELECT 1 AS a, 2 AS b");
7371

72+
/** @var \Amp\Mysql\ResultSet $resultset */
7473
$resultset = yield $db->query("INSERT INTO tmp VALUES (5, 6), (8, 9); SELECT a FROM tmp; SELECT b FROM tmp WHERE a = 5; SELECT b AS d, a + 1 AS c FROM tmp WHERE b < 7");
75-
$this->assertEquals((yield $resultset->rowCount()), 0);
7674

77-
$resultset = yield $resultset->next();
78-
$this->assertEquals((yield $resultset->rowCount()), 3);
7975

80-
$resultset = yield $resultset->next();
81-
$this->assertEquals(yield $resultset->fetchRow(), [6]);
76+
$got = [];
77+
while (yield $resultset->advance(ResultSet::FETCH_ARRAY)) {
78+
$got[] = $resultset->getCurrent();
79+
}
80+
81+
$this->assertEquals($got, [[1], [5], [8], [6], [2, 2], [6, 6]]);
8282

83-
$resultset = yield $resultset->next();
8483
$fields = yield $resultset->getFields();
8584
$this->assertEquals(count($fields), 2);
8685
$this->assertEquals($fields[0]["original_name"], "b");
@@ -95,12 +94,16 @@ function testMultiStmt() {
9594

9695
function testPrepared() {
9796
\Amp\Loop::run(function() {
98-
$db = new Connection("host=" . DB_HOST . ";user=" . DB_USER . ";pass=" . DB_PASS . ";db=connectiontest");
99-
$db->connect();
97+
/** @var \Amp\Mysql\Connection $db */
98+
$db = yield connect("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=connectiontest");
10099

101100
$db->query("CREATE TEMPORARY TABLE tmp SELECT 1 AS a, 2 AS b");
102101
$db->query("INSERT INTO tmp VALUES (5, 6), (8, 9), (10, 11), (12, 13)");
103102

103+
/**
104+
* @var \Amp\Mysql\Statement $stmt
105+
* @var \Amp\Mysql\ResultSet $result
106+
*/
104107
$stmt = yield $db->prepare("SELECT * FROM tmp WHERE a = ? OR b = :num");
105108
$base = [
106109
"catalog" => "def",
@@ -114,31 +117,58 @@ function testPrepared() {
114117
"decimals" => 0,
115118
];
116119
$this->assertEquals(yield $stmt->getFields(), [$base + ["name" => "a", "original_name" => "a"], $base + ["name" => "b", "original_name" => "b"]]);
117-
$result = (yield $stmt->execute([5, "num" => 9]));
118-
$this->assertEquals(yield $result->rowCount(), 2);
120+
$stmt->bind("num", 9);
121+
$result = yield $stmt->execute(5);
122+
$got = [];
123+
while (yield $result->advance(ResultSet::FETCH_ARRAY)) {
124+
$got[] = $result->getCurrent();
125+
}
126+
$this->assertCount(2, $got);
119127

120-
$result = yield $db->prepare("SELECT * FROM tmp WHERE a = ? OR b = ?", [5, 8]);
121-
$this->assertEquals((yield $result->rowCount()), 1);
128+
/** @var \Amp\Mysql\Statement $stmt */
129+
$stmt = yield $db->prepare("SELECT * FROM tmp WHERE a = ? OR b = ?");
130+
$result = yield $stmt->execute(5, 8);
131+
$got = [];
132+
while (yield $result->advance(ResultSet::FETCH_ARRAY)) {
133+
$got[] = $result->getCurrent();
134+
}
135+
$this->assertCount(1, $got);
122136

123-
$result = yield $db->prepare("SELECT * FROM tmp WHERE a = :a OR b = ? OR a = :a", ["a" => [5, 10], 9]);
124-
$this->assertEquals((yield $result->rowCount()), 3);
137+
/** @var \Amp\Mysql\Statement $stmt */
138+
$stmt = yield $db->prepare("SELECT * FROM tmp WHERE a = :a OR b = ?");
139+
$stmt->bind("a", 5);
140+
$result = yield $stmt->execute(9);
141+
$got = [];
142+
while (yield $result->advance(ResultSet::FETCH_ARRAY)) {
143+
$got[] = $result->getCurrent();
144+
}
145+
$this->assertCount(2, $got);
125146

126147
$stmt = yield $db->prepare("INSERT INTO tmp VALUES (:foo, :bar)");
127148
$stmt->bind("foo", 5);
128-
$result = yield $stmt->execute(["bar" => 9]);
129-
$this->assertEquals($result->affectedRows, 1);
149+
$stmt->bind("bar", 9);
150+
/** @var \Amp\Mysql\CommandResult $result */
151+
$result = yield $stmt->execute();
152+
$this->assertSame(1, $result->affectedRows());
130153
});
131154
}
132155

133156
function testPreparedWithNegativeValue() {
134157
\Amp\Loop::run(function() {
135-
$db = new Connection("host=" . DB_HOST . ";user=" . DB_USER . ";pass=" . DB_PASS . ";db=connectiontest");
136-
$db->connect();
158+
/** @var \Amp\Mysql\Connection $db */
159+
$db = yield connect("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=connectiontest");
137160

161+
/** @var \Amp\Mysql\Statement $stmt */
138162
$db->query("DROP TABLE tmp"); // just in case it would exist...
139-
yield $db->prepare("CREATE TABLE tmp SELECT ? AS a", [-1]);
140-
$result = yield $db->prepare("SELECT a FROM tmp", []);
141-
$this->assertEquals(yield $result->fetchRow(), [-1]);
163+
$stmt = yield $db->prepare("CREATE TABLE tmp SELECT ? AS a");
164+
yield $stmt->execute(-1);
165+
166+
/** @var \Amp\Mysql\ResultSet $result */
167+
$stmt = yield $db->prepare("SELECT a FROM tmp");
168+
$result = yield $stmt->execute();
169+
yield $result->advance(ResultSet::FETCH_ARRAY);
170+
171+
$this->assertEquals($result->getCurrent(), [-1]);
142172
});
143173
}
144174
}

test/Mysql/PoolTest.php

+6-34
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,19 @@
11
<?php
22

3-
use Amp\Mysql\Pool;
43
use PHPUnit\Framework\TestCase;
4+
use function Amp\Mysql\pool;
55

66
class PoolTest extends TestCase {
7-
function testConnect() {
8-
$complete = false;
9-
\Amp\Loop::run(function() use (&$complete) {
10-
$db = new Pool("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=connectiontest");
11-
yield $db->init(); // force waiting for connection
12-
/* use an alternative charset... Default is utf8mb4_general_ci */
13-
$db->setCharset("latin1_general_ci");
14-
15-
$complete = true;
16-
});
17-
$this->assertEquals(true, $complete, "Database commands did not complete.");
18-
}
19-
20-
/** This should throw an exception as the password is incorrect. */
7+
/**
8+
* @expectedException \Amp\Mysql\InitializationException
9+
* @expectedExceptionMessage Access denied for user
10+
*/
2111
function testWrongPassword() {
22-
$this->expectException("Exception");
2312
\Amp\Loop::run(function() {
24-
$db = new Pool("host=".DB_HOST.";user=".DB_USER.";pass=the_wrong_password;db=connectiontest");
13+
$db = pool("host=".DB_HOST.";user=".DB_USER.";pass=the_wrong_password;db=connectiontest");
2514

2615
/* Try a query */
2716
yield $db->query("CREATE TABLE tmp SELECT 1 AS a, 2 AS b");
2817
});
2918
}
30-
31-
/* common test for all the Pool functions which are just a thin wrapper for the Connection class */
32-
function testVirtualConnection() {
33-
$complete = false;
34-
\Amp\Loop::run(function() use (&$complete) {
35-
$db = new Pool("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=connectiontest");
36-
37-
/* Multiple queries one after the other must be hold back and dispatched to new connections */
38-
for ($i = 0; $i < 5; $i++) {
39-
$pings[] = $db->ping();
40-
}
41-
42-
yield $pings;
43-
$complete = true;
44-
});
45-
$this->assertEquals(true, $complete, "Database commands did not complete.");
46-
}
4719
}

test/Mysql/StmtTest.php renamed to test/Mysql/StatementTest.php

+11-12
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,31 @@
22

33
use Amp\Mysql\Statement;
44
use PHPUnit\Framework\TestCase;
5-
use Amp\Mysql\ResultProxy;
5+
use Amp\Mysql\Internal\ResultProxy;
66

7-
class StmtTest extends TestCase
8-
{
7+
class StatementTest extends TestCase {
8+
/** @var \Prophecy\Prophecy\ObjectProphecy */
99
protected $processor;
10+
11+
/** @var \Amp\Mysql\Internal\ResultProxy */
1012
protected $resultProxy;
1113

12-
public function setUp()
13-
{
14-
$this->processor = $this->prophesize('Amp\Mysql\Processor');
15-
$this->resultProxy = new ResultProxy();
14+
public function setUp() {
15+
$this->processor = $this->prophesize('Amp\Mysql\Internal\Processor');
16+
$this->resultProxy = new ResultProxy;
1617
}
1718

1819
/**
1920
* @dataProvider provideTestBindDataTypes
2021
*/
21-
public function testBindDataTypes($data, $expectedException)
22-
{
22+
public function testBindDataTypes($data, $expectedException) {
2323
// arrange
2424
$query = 'SELECT * FROM test WHERE id = ?';
2525
$stmtId = 1;
2626
$paramId = 0;
2727
$named = [];
2828

29-
$this->processor->alive()->willReturn(true);
29+
$this->processor->isAlive()->willReturn(true);
3030
$this->processor->delRef()->shouldBeCalled();
3131
$this->processor->closeStmt(\Prophecy\Argument::any())->shouldBeCalled();
3232
$this->resultProxy->columnsToFetch = 1;
@@ -45,8 +45,7 @@ public function testBindDataTypes($data, $expectedException)
4545
$stmt->bind($paramId, $data);
4646
}
4747

48-
public function provideTestBindDataTypes()
49-
{
48+
public function provideTestBindDataTypes() {
5049
return [
5150
'test scalar' => [
5251
'data' => 1,

0 commit comments

Comments
 (0)