|
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; |
|
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
|
|
|
$options = [ |
|
81
|
|
|
"dbhost" => "localhost", |
|
82
|
|
|
"dbuser" => "root", |
|
83
|
|
|
"dbpass" => "", |
|
84
|
|
|
"dbname" => "test", |
|
85
|
|
|
"dbchar" => "utf8", |
|
86
|
|
|
"dbport" => "3306", |
|
87
|
|
|
"auto_connect" => false |
|
88
|
|
|
]; |
|
89
|
|
|
|
|
90
|
|
|
$conn = new MySQL($options); |
|
91
|
|
|
|
|
92
|
|
|
$errorObject = null; |
|
93
|
|
|
|
|
94
|
|
|
try { |
|
95
|
|
|
$conn->disconnect(); |
|
96
|
|
|
} |
|
97
|
|
|
catch (\Exception $e) |
|
98
|
|
|
{ |
|
99
|
|
|
$errorObject = ($e instanceof \LogicException); |
|
100
|
|
|
} |
|
101
|
|
|
finally |
|
102
|
|
|
{ |
|
103
|
|
|
$this->assertNotTrue($conn->isConnected()); |
|
104
|
|
|
$this->assertTrue($errorObject, $e->getMessage()); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Tests if we can reconnect to the database server |
|
110
|
|
|
* |
|
111
|
|
|
* @return null |
|
112
|
|
|
*/ |
|
113
|
|
|
public function testCanStablishConnectionAgain() |
|
114
|
|
|
{ |
|
115
|
|
|
$conn = new MySQL($this->options); |
|
116
|
|
|
|
|
117
|
|
|
$conn->connect(); |
|
118
|
|
|
$mysqliObject = $conn->reconnect(); |
|
119
|
|
|
|
|
120
|
|
|
$this->assertInstanceOf(\mysqli, $mysqliObject); |
|
|
|
|
|
|
121
|
|
|
$this->assertTrue($conn->isConnected()); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Tests if we can reconnect to the database server when there is not a connection stablished |
|
126
|
|
|
* |
|
127
|
|
|
* @return null |
|
128
|
|
|
*/ |
|
129
|
|
|
public function testCannotStablishConnectionAgain() |
|
130
|
|
|
{ |
|
131
|
|
|
$conn = new MySQL($this->options); |
|
132
|
|
|
|
|
133
|
|
|
$errorObject = null; |
|
134
|
|
|
|
|
135
|
|
|
try { |
|
136
|
|
|
$conn->reconnect(); |
|
137
|
|
|
} |
|
138
|
|
|
catch (\Exception $e) |
|
139
|
|
|
{ |
|
140
|
|
|
$errorObject = ($e instanceof \LogicException); |
|
141
|
|
|
} |
|
142
|
|
|
finally |
|
143
|
|
|
{ |
|
144
|
|
|
$this->assertTrue($errorObject, $e->getMessage()); |
|
|
|
|
|
|
145
|
|
|
$this->assertNotTrue($conn->isConnected()); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Tests if a connection failed throws a ConnectionException |
|
151
|
|
|
* |
|
152
|
|
|
* @return null |
|
153
|
|
|
*/ |
|
154
|
|
|
public function testCannotStablishConnection() |
|
155
|
|
|
{ |
|
156
|
|
|
$this->options["dbhost"] = "myserver"; // this server does not exists |
|
157
|
|
|
|
|
158
|
|
|
$conn = new MySQL($this->options); |
|
159
|
|
|
|
|
160
|
|
|
$mysqliObject = $errorObject = null; |
|
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
try { |
|
163
|
|
|
$mysqliObject = $conn->connect(); |
|
164
|
|
|
} |
|
165
|
|
|
catch (\Exception $e) |
|
166
|
|
|
{ |
|
167
|
|
|
$errorObject = ($e instanceof ConnectionException); |
|
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
finally |
|
170
|
|
|
{ |
|
171
|
|
|
$this->assertTrue($errorObject, $e->getMessage()); |
|
|
|
|
|
|
172
|
|
|
$this->assertNotTrue($conn->isConnected()); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/* |
|
177
|
|
|
|-------------------------------------------------------------------------- |
|
178
|
|
|
| Quering and Transactions |
|
179
|
|
|
|-------------------------------------------------------------------------- |
|
180
|
|
|
| |
|
181
|
|
|
| The following tests are related to query and transaction operations and its |
|
182
|
|
|
| exceptions and returned values. |
|
183
|
|
|
| |
|
184
|
|
|
*/ |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Tests if we can execute DDL statements |
|
188
|
|
|
* |
|
189
|
|
|
* @return null |
|
190
|
|
|
*/ |
|
191
|
|
|
public function textCanExecuteDLLStatement() |
|
192
|
|
|
{ |
|
193
|
|
|
$this->options["auto_connect"] = true; |
|
194
|
|
|
|
|
195
|
|
|
$conn = new MySQL($this->options); |
|
196
|
|
|
$sql = "CREATE TABLE MYTABLE (ID INTEGER AUTO_INCREMENT, DESCRIPTION VARCHAR(100))"; |
|
197
|
|
|
$result = $conn->execute($sql); |
|
198
|
|
|
|
|
199
|
|
|
$this->assertTrue(is_resource($result)); |
|
200
|
|
|
|
|
201
|
|
|
# properties modified by execute() method |
|
202
|
|
|
$this->assertEquals(0, $conn->getNumRows()); |
|
203
|
|
|
$this->assertEquals(0, $conn->getNumFields()); |
|
204
|
|
|
$this->assertEquals(0, $conn->getRowsAffected()); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Tests if we can execute DML statements |
|
209
|
|
|
* |
|
210
|
|
|
* @return null |
|
211
|
|
|
*/ |
|
212
|
|
|
public function textCanExecuteDMLStatement() |
|
213
|
|
|
{ |
|
214
|
|
|
$this->options["auto_connect"] = true; |
|
215
|
|
|
|
|
216
|
|
|
$conn = new MySQL($this->options); |
|
217
|
|
|
$sql = "INSERT INTO MYTABLE VALUES (1, 'Hello world!')"; |
|
218
|
|
|
$result = $conn->execute($sql); |
|
219
|
|
|
|
|
220
|
|
|
$this->assertTrue(is_resource($result)); |
|
221
|
|
|
|
|
222
|
|
|
# properties modified by execute() method |
|
223
|
|
|
$this->assertEquals(0, $conn->getNumRows()); |
|
224
|
|
|
$this->assertEquals(0, $conn->getNumFields()); |
|
225
|
|
|
$this->assertEquals(1, $conn->getRowsAffected()); |
|
226
|
|
|
} |
|
227
|
|
|
} |