1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DronePHP (http://www.dronephp.com) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/Pleets/DronePHP |
6
|
|
|
* @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) |
7
|
|
|
* @license http://www.dronephp.com/license |
8
|
|
|
* @author Darío Rivera <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace DroneTest\Util; |
12
|
|
|
|
13
|
|
|
use Drone\Db\Driver\MySQL; |
14
|
|
|
use Drone\Db\Driver\Exception\ConnectionException; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
|
17
|
|
|
class MySQLTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Database parameters |
21
|
|
|
*/ |
22
|
|
|
private $options = [ |
23
|
|
|
"dbhost" => "localhost", |
24
|
|
|
"dbuser" => "root", |
25
|
|
|
"dbpass" => "", |
26
|
|
|
"dbname" => "test", |
27
|
|
|
"dbchar" => "utf8", |
28
|
|
|
"dbport" => "3306", |
29
|
|
|
"auto_connect" => false |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/* |
33
|
|
|
|-------------------------------------------------------------------------- |
34
|
|
|
| Stablishing connections |
35
|
|
|
|-------------------------------------------------------------------------- |
36
|
|
|
| |
37
|
|
|
| The following tests are related to the connection methods and its |
38
|
|
|
| exceptions and returned values. |
39
|
|
|
| |
40
|
|
|
*/ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Tests if we can connect to the database server |
44
|
|
|
* |
45
|
|
|
* @return null |
46
|
|
|
*/ |
47
|
|
|
public function testCanStablishConnection() |
48
|
|
|
{ |
49
|
|
|
$conn = new MySQL($this->options); |
50
|
|
|
|
51
|
|
|
$mysqliObject = $conn->connect(); |
52
|
|
|
|
53
|
|
|
$this->assertInstanceOf('\mysqli', $mysqliObject); |
54
|
|
|
$this->assertTrue($conn->isConnected()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Tests if we can disconnect from the database server |
59
|
|
|
* |
60
|
|
|
* @return null |
61
|
|
|
*/ |
62
|
|
|
public function testCanDownConnection() |
63
|
|
|
{ |
64
|
|
|
$conn = new MySQL($this->options); |
65
|
|
|
|
66
|
|
|
$conn->connect(); |
67
|
|
|
$result = $conn->disconnect(); |
68
|
|
|
|
69
|
|
|
$this->assertNotTrue($conn->isConnected()); |
70
|
|
|
$this->assertTrue($result); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Tests if we can disconnect from server when there is not a connection stablished |
75
|
|
|
* |
76
|
|
|
* @return null |
77
|
|
|
*/ |
78
|
|
|
public function testCannotDisconectWhenNotConnected() |
79
|
|
|
{ |
80
|
|
|
$conn = new MySQL($this->options); |
81
|
|
|
|
82
|
|
|
$errorObject = null; |
83
|
|
|
|
84
|
|
|
try { |
85
|
|
|
$conn->disconnect(); |
86
|
|
|
} |
87
|
|
|
catch (\Exception $e) |
88
|
|
|
{ |
89
|
|
|
$errorObject = ($e instanceof \LogicException); |
90
|
|
|
} |
91
|
|
|
finally |
92
|
|
|
{ |
93
|
|
|
$this->assertNotTrue($conn->isConnected()); |
94
|
|
|
$this->assertTrue($errorObject, $e->getMessage()); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Tests if we can reconnect to the database server |
100
|
|
|
* |
101
|
|
|
* @return null |
102
|
|
|
*/ |
103
|
|
|
public function testCanStablishConnectionAgain() |
104
|
|
|
{ |
105
|
|
|
$conn = new MySQL($this->options); |
106
|
|
|
|
107
|
|
|
$conn->connect(); |
108
|
|
|
$mysqliObject = $conn->reconnect(); |
109
|
|
|
|
110
|
|
|
$this->assertInstanceOf('\mysqli', $mysqliObject); |
111
|
|
|
$this->assertTrue($conn->isConnected()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Tests if we can reconnect to the database server when there is not a connection stablished |
116
|
|
|
* |
117
|
|
|
* @return null |
118
|
|
|
*/ |
119
|
|
|
public function testCannotStablishConnectionAgain() |
120
|
|
|
{ |
121
|
|
|
$conn = new MySQL($this->options); |
122
|
|
|
|
123
|
|
|
$errorObject = null; |
124
|
|
|
|
125
|
|
|
try { |
126
|
|
|
$conn->reconnect(); |
127
|
|
|
} |
128
|
|
|
catch (\Exception $e) |
129
|
|
|
{ |
130
|
|
|
$errorObject = ($e instanceof \LogicException); |
131
|
|
|
} |
132
|
|
|
finally |
133
|
|
|
{ |
134
|
|
|
$this->assertTrue($errorObject, $e->getMessage()); |
|
|
|
|
135
|
|
|
$this->assertNotTrue($conn->isConnected()); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Tests if a connection failed throws a ConnectionException |
141
|
|
|
* |
142
|
|
|
* @return null |
143
|
|
|
*/ |
144
|
|
|
public function testCannotStablishConnection() |
145
|
|
|
{ |
146
|
|
|
$this->options["dbhost"] = "myserver"; // this server does not exists |
147
|
|
|
$this->options["auto_connect"] = false; |
148
|
|
|
|
149
|
|
|
$conn = new MySQL($this->options); |
150
|
|
|
|
151
|
|
|
$mysqliObject = $errorObject = null; |
|
|
|
|
152
|
|
|
|
153
|
|
|
try { |
154
|
|
|
$mysqliObject = $conn->connect(); |
155
|
|
|
} |
156
|
|
|
catch (\Exception $e) |
157
|
|
|
{ |
158
|
|
|
$errorObject = ($e instanceof ConnectionException); |
159
|
|
|
} |
160
|
|
|
finally |
161
|
|
|
{ |
162
|
|
|
$this->assertTrue($errorObject, $e->getMessage()); |
|
|
|
|
163
|
|
|
$this->assertNotTrue($conn->isConnected()); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/* |
168
|
|
|
|-------------------------------------------------------------------------- |
169
|
|
|
| Quering and Transactions |
170
|
|
|
|-------------------------------------------------------------------------- |
171
|
|
|
| |
172
|
|
|
| The following tests are related to query and transaction operations and its |
173
|
|
|
| exceptions and returned values. |
174
|
|
|
| |
175
|
|
|
*/ |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Tests if we can execute DDL statements |
179
|
|
|
* |
180
|
|
|
* @return null |
181
|
|
|
*/ |
182
|
|
|
public function testCanExecuteDLLStatement() |
183
|
|
|
{ |
184
|
|
|
$this->options["auto_connect"] = true; |
185
|
|
|
|
186
|
|
|
$conn = new MySQL($this->options); |
187
|
|
|
$sql = "CREATE TABLE MYTABLE (ID INTEGER(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, DESCRIPTION VARCHAR(100))"; |
188
|
|
|
$result = $conn->execute($sql); |
189
|
|
|
|
190
|
|
|
$this->assertTrue(is_resource($result)); |
191
|
|
|
|
192
|
|
|
# properties modified by execute() method |
193
|
|
|
$this->assertEquals(0, $conn->getNumRows()); |
194
|
|
|
$this->assertEquals(0, $conn->getNumFields()); |
195
|
|
|
$this->assertEquals(0, $conn->getRowsAffected()); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Tests if we can execute DML statements |
200
|
|
|
* |
201
|
|
|
* @return null |
202
|
|
|
*/ |
203
|
|
|
public function testCanExecuteDMLStatement() |
204
|
|
|
{ |
205
|
|
|
$this->options["auto_connect"] = true; |
206
|
|
|
|
207
|
|
|
$conn = new MySQL($this->options); |
208
|
|
|
$sql = "INSERT INTO MYTABLE (DESCRIPTION) VALUES ('Hello world!')"; |
209
|
|
|
$result = $conn->execute($sql); |
210
|
|
|
|
211
|
|
|
$this->assertTrue(is_resource($result)); |
212
|
|
|
|
213
|
|
|
# properties modified by execute() method |
214
|
|
|
$this->assertEquals(0, $conn->getNumRows()); |
215
|
|
|
$this->assertEquals(0, $conn->getNumFields()); |
216
|
|
|
$this->assertEquals(1, $conn->getRowsAffected()); |
217
|
|
|
} |
218
|
|
|
} |