3
3
namespace Amp \Mysql \Test ;
4
4
5
5
use Amp \Mysql \MysqlColumnDefinition ;
6
+ use Amp \Mysql \MysqlConfig ;
6
7
use Amp \Mysql \MysqlDataType ;
7
8
use Amp \Mysql \MysqlLink ;
8
9
use Amp \Mysql \MysqlResult ;
@@ -14,11 +15,21 @@ abstract class LinkTest extends AsyncTestCase
14
15
/**
15
16
* Returns the Link class to be tested.
16
17
*/
17
- abstract protected function getLink (string $ connectionString ): MysqlLink ;
18
+ abstract protected function getLink (bool $ useCompression = false ): MysqlLink ;
19
+
20
+ protected function getConfig (bool $ useCompression = false ): MysqlConfig
21
+ {
22
+ $ config = MysqlConfig::fromAuthority (DB_HOST , DB_USER , DB_PASS , 'test ' );
23
+ if ($ useCompression ) {
24
+ $ config = $ config ->withCompression ();
25
+ }
26
+
27
+ return $ config ;
28
+ }
18
29
19
30
public function testQuery ()
20
31
{
21
- $ db = $ this ->getLink (" host= " . DB_HOST . " ;user= " . DB_USER . " ;pass= " . DB_PASS . " ;db=test " );
32
+ $ db = $ this ->getLink ();
22
33
23
34
$ resultset = $ db ->execute ("SELECT ? AS a " , [M_PI ]);
24
35
$ this ->assertInstanceOf (MysqlResult::class, $ resultset );
@@ -34,7 +45,7 @@ public function testQuery()
34
45
35
46
public function testQueryFetchRow ()
36
47
{
37
- $ db = $ this ->getLink (" host= " . DB_HOST . " ;user= " . DB_USER . " ;pass= " . DB_PASS . " ;db=test " );
48
+ $ db = $ this ->getLink ();
38
49
39
50
$ resultset = $ db ->query ('SELECT a FROM main WHERE a < 4 ' );
40
51
$ this ->assertInstanceOf (MysqlResult::class, $ resultset );
@@ -54,14 +65,14 @@ public function testQueryWithInvalidQuery()
54
65
$ this ->expectException (QueryError::class);
55
66
$ this ->expectExceptionMessage ('You have an error in your SQL syntax ' );
56
67
57
- $ db = $ this ->getLink (" host= " . DB_HOST . " ;user= " . DB_USER . " ;pass= " . DB_PASS . " ;db=test " );
68
+ $ db = $ this ->getLink ();
58
69
59
70
$ db ->query ("SELECT & FROM main WHERE a = 1 " );
60
71
}
61
72
62
73
public function testMultiStmt ()
63
74
{
64
- $ db = $ this ->getLink (" host= " . DB_HOST . " ;user= " . DB_USER . " ;pass= " . DB_PASS . " ;db=test;useCompression= true" );
75
+ $ db = $ this ->getLink (true );
65
76
66
77
$ resultset = $ db ->query ("SELECT a FROM main; SELECT b FROM main WHERE a = 5; SELECT b AS d, a + 1 AS c FROM main WHERE b > 4 " );
67
78
$ this ->assertInstanceOf (MysqlResult::class, $ resultset );
@@ -100,7 +111,7 @@ public function testMultiStmt()
100
111
101
112
public function testPrepared ()
102
113
{
103
- $ db = $ this ->getLink (" host= " . DB_HOST . " ;user= " . DB_USER . " ;pass= " . DB_PASS . " ;db=test;useCompression= true" );
114
+ $ db = $ this ->getLink (true );
104
115
105
116
$ stmt = $ db ->prepare ("SELECT id as no, a, b FROM main as table_alias WHERE a = ? OR b = :num " );
106
117
$ base = [
@@ -168,7 +179,7 @@ public function testPrepareWithInvalidQuery()
168
179
$ this ->expectException (QueryError::class);
169
180
$ this ->expectExceptionMessage ('You have an error in your SQL syntax ' );
170
181
171
- $ db = $ this ->getLink (" host= " . DB_HOST . " ;user= " . DB_USER . " ;pass= " . DB_PASS . " ;db=test " );
182
+ $ db = $ this ->getLink ();
172
183
173
184
$ statement = $ db ->prepare ("SELECT & FROM main WHERE a = ? " );
174
185
@@ -180,7 +191,7 @@ public function testBindWithInvalidParamId()
180
191
$ this ->expectException (\Error::class);
181
192
$ this ->expectExceptionMessage ('Parameter 1 is not defined for this prepared statement ' );
182
193
183
- $ db = $ this ->getLink (" host= " . DB_HOST . " ;user= " . DB_USER . " ;pass= " . DB_PASS . " ;db=test " );
194
+ $ db = $ this ->getLink ();
184
195
185
196
$ statement = $ db ->prepare ("SELECT * FROM main WHERE a = ? " );
186
197
@@ -194,7 +205,7 @@ public function testBindWithInvalidParamName()
194
205
$ this ->expectException (\Error::class);
195
206
$ this ->expectExceptionMessage ('Parameter :b is not defined for this prepared statement ' );
196
207
197
- $ db = $ this ->getLink (" host= " . DB_HOST . " ;user= " . DB_USER . " ;pass= " . DB_PASS . " ;db=test " );
208
+ $ db = $ this ->getLink ();
198
209
199
210
$ statement = $ db ->prepare ("SELECT * FROM main WHERE a = :a " );
200
211
@@ -208,15 +219,15 @@ public function testStatementExecuteWithTooFewParams()
208
219
$ this ->expectException (\Error::class);
209
220
$ this ->expectExceptionMessage ('Parameter 1 for prepared statement missing ' );
210
221
211
- $ db = $ this ->getLink (" host= " . DB_HOST . " ;user= " . DB_USER . " ;pass= " . DB_PASS . " ;db=test " );
222
+ $ db = $ this ->getLink ();
212
223
213
224
$ stmt = $ db ->prepare ("SELECT * FROM main WHERE a = ? AND b = ? " );
214
225
$ stmt ->execute ([1 ]);
215
226
}
216
227
217
228
public function testExecute ()
218
229
{
219
- $ db = $ this ->getLink (" host= " . DB_HOST . " ;user= " . DB_USER . " ;pass= " . DB_PASS . " ;db=test " );
230
+ $ db = $ this ->getLink ();
220
231
221
232
$ result = $ db ->execute ("SELECT * FROM test.main WHERE a = ? OR b = ? " , [2 , 5 ]);
222
233
$ this ->assertInstanceOf (MysqlResult::class, $ result );
@@ -240,7 +251,7 @@ public function testExecuteWithInvalidQuery()
240
251
$ this ->expectException (QueryError::class);
241
252
$ this ->expectExceptionMessage ('You have an error in your SQL syntax ' );
242
253
243
- $ db = $ this ->getLink (" host= " . DB_HOST . " ;user= " . DB_USER . " ;pass= " . DB_PASS . " ;db=test " );
254
+ $ db = $ this ->getLink ();
244
255
245
256
$ db ->execute ("SELECT & FROM main WHERE a = ? " , [1 ]);
246
257
@@ -252,7 +263,7 @@ public function testExecuteWithTooFewParams()
252
263
$ this ->expectException (\Error::class);
253
264
$ this ->expectExceptionMessage ('Parameter 1 for prepared statement missing ' );
254
265
255
- $ db = $ this ->getLink (" host= " . DB_HOST . " ;user= " . DB_USER . " ;pass= " . DB_PASS . " ;db=test " );
266
+ $ db = $ this ->getLink ();
256
267
257
268
$ db ->execute ("SELECT * FROM main WHERE a = ? AND b = ? " , [1 ]);
258
269
@@ -261,7 +272,7 @@ public function testExecuteWithTooFewParams()
261
272
262
273
public function testPreparedWithNegativeValue ()
263
274
{
264
- $ db = $ this ->getLink (" host= " . DB_HOST . " ;user= " . DB_USER . " ;pass= " . DB_PASS . " ;db=test " );
275
+ $ db = $ this ->getLink ();
265
276
266
277
$ db ->query ("DROP TABLE IF EXISTS tmp " );
267
278
@@ -279,7 +290,7 @@ public function testPreparedWithNegativeValue()
279
290
280
291
public function testTransaction ()
281
292
{
282
- $ db = $ this ->getLink (" host= " . DB_HOST . " ;user= " . DB_USER . " ;pass= " . DB_PASS . " ;db=test " );
293
+ $ db = $ this ->getLink ();
283
294
284
295
$ transaction = $ db ->beginTransaction ();
285
296
@@ -324,7 +335,7 @@ public function testTransaction()
324
335
*/
325
336
public function testInsertSelect ()
326
337
{
327
- $ db = $ this ->getLink (" host= " . DB_HOST . " ;user= " . DB_USER . " ;pass= " . DB_PASS . " ;db=test " );
338
+ $ db = $ this ->getLink ();
328
339
329
340
$ a = 1 ;
330
341
@@ -350,7 +361,7 @@ public function testInsertSelect()
350
361
351
362
public function testJsonDecoding ()
352
363
{
353
- $ db = $ this ->getLink (" host= " . DB_HOST . " ;user= " . DB_USER . " ;pass= " . DB_PASS . " ;db=test " );
364
+ $ db = $ this ->getLink ();
354
365
355
366
$ result = $ db ->execute ("SELECT a FROM test.json " );
356
367
0 commit comments