1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class DriverTestAbstract |
4
|
|
|
* |
5
|
|
|
* @filesource DriverTestAbstract.php |
6
|
|
|
* @created 29.05.2017 |
7
|
|
|
* @package chillerlan\DatabaseTest\Drivers |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2017 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\DatabaseTest\Drivers; |
14
|
|
|
|
15
|
|
|
use chillerlan\Database\Drivers\DBDriverInterface; |
16
|
|
|
use chillerlan\DatabaseTest\TestAbstract; |
17
|
|
|
use chillerlan\SimpleCache\Cache; |
18
|
|
|
use chillerlan\SimpleCache\Drivers\MemoryCacheDriver; |
19
|
|
|
use Psr\SimpleCache\CacheInterface; |
20
|
|
|
|
21
|
|
|
abstract class DriverTestAbstract extends TestAbstract{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \chillerlan\Database\Drivers\DBDriverInterface |
25
|
|
|
*/ |
26
|
|
|
protected $DBDriver; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \Psr\SimpleCache\CacheInterface |
30
|
|
|
*/ |
31
|
|
|
protected $Cache; |
32
|
|
|
|
33
|
|
|
protected $driver; |
34
|
|
|
|
35
|
|
|
protected $SQL_RAW_TRUNCATE = 'DELETE FROM test'; |
36
|
|
|
protected $SQL_RAW_SELECT_ALL = 'SELECT * FROM test'; |
37
|
|
|
protected $SQL_RAW_DROP = 'DROP TABLE IF EXISTS test'; |
38
|
|
|
protected $SQL_RAW_CREATE = 'CREATE TABLE IF NOT EXISTS test (id INTEGER NOT NULL, hash VARCHAR(32) NOT NULL)'; |
39
|
|
|
protected $SQL_RAW_INSERT = 'INSERT INTO test (id, hash) VALUES (%1$d, \'%2$s\')'; |
40
|
|
|
protected $SQL_RAW_ERROR = '-'; |
41
|
|
|
|
42
|
|
|
protected $SQL_PREPARED_INSERT = 'INSERT INTO test (id, hash) VALUES (?, ?)'; |
43
|
|
|
|
44
|
|
|
protected $SQL_INDEX_COL = 'id'; |
45
|
|
|
protected $SQL_DATA_COL = 'hash'; |
46
|
|
|
|
47
|
|
|
protected $SQL_ESCAPED; |
48
|
|
|
|
49
|
|
|
protected function setUp(){ |
50
|
|
|
parent::setUp(); |
51
|
|
|
|
52
|
|
|
$this->Cache = new Cache(new MemoryCacheDriver); |
53
|
|
|
$this->DBDriver = (new $this->driver($this->DBOptions, $this->Cache))->connect(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function tearDown(){ |
57
|
|
|
$this->assertTrue($this->DBDriver->disconnect()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testInstance(){ |
61
|
|
|
$this->assertInstanceOf(DBDriverInterface::class, $this->DBDriver); |
62
|
|
|
$this->assertInstanceOf(CacheInterface::class, $this->Cache); |
63
|
|
|
$this->assertSame($this->DBDriver, $this->DBDriver->connect()); |
64
|
|
|
$this->assertNotNull($this->DBDriver->getDBResource()); |
65
|
|
|
#/* |
66
|
|
|
print_r([ |
67
|
|
|
'driver' => get_class($this->DBDriver), |
68
|
|
|
'client' => $this->DBDriver->getClientInfo(), |
69
|
|
|
'server' => $this->DBDriver->getServerInfo(), |
70
|
|
|
]); |
71
|
|
|
#*/ |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @expectedException \chillerlan\Database\DBException |
76
|
|
|
* @expectedExceptionMessage db error: |
77
|
|
|
*/ |
78
|
|
|
public function testConnectDBconnectError(){ |
79
|
|
|
$this->DBOptions->host = '...'; |
80
|
|
|
$this->DBOptions->database = '...'; |
81
|
|
|
(new $this->driver($this->DBOptions))->connect(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// coverage |
85
|
|
|
public function testEscape(){ |
86
|
|
|
$this->assertSame($this->SQL_ESCAPED, $this->DBDriver->escape("'\\\''")); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testCreateTable(){ |
90
|
|
|
$this->assertTrue($this->DBDriver->raw($this->SQL_RAW_DROP)); |
|
|
|
|
91
|
|
|
$this->assertTrue($this->DBDriver->raw($this->SQL_RAW_CREATE)); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testTruncate(){ |
95
|
|
|
$this->assertTrue($this->DBDriver->raw($this->SQL_RAW_TRUNCATE)); |
|
|
|
|
96
|
|
|
$this->assertTrue($this->DBDriver->raw($this->SQL_RAW_SELECT_ALL)); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @expectedException \chillerlan\Database\DBException |
101
|
|
|
* @expectedExceptionMessage sql error: |
102
|
|
|
*/ |
103
|
|
|
public function testRawSQLError(){ |
104
|
|
|
$this->DBDriver->raw($this->SQL_RAW_ERROR); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function resultTest(){ |
108
|
|
|
$DBResult = $this->DBDriver->raw($this->SQL_RAW_SELECT_ALL, $this->SQL_INDEX_COL); |
109
|
|
|
$DBResultPrepared = $this->DBDriver->prepared($this->SQL_RAW_SELECT_ALL, [], $this->SQL_INDEX_COL); // coverage |
110
|
|
|
|
111
|
|
|
$this->assertEquals($DBResult, $DBResultPrepared); |
112
|
|
|
|
113
|
|
|
$this->assertCount(10, $DBResult); |
114
|
|
|
|
115
|
|
|
$DBResult->__each( |
116
|
|
|
function($row, $i){ |
117
|
|
|
/** @var \chillerlan\Database\DBResultRow $row */ |
118
|
|
|
$this->assertEquals($row->{$this->SQL_INDEX_COL}, $i); |
119
|
|
|
$this->assertSame(md5($row->{$this->SQL_INDEX_COL}), $row->{$this->SQL_DATA_COL}()); |
120
|
|
|
|
121
|
|
|
$row->__each( |
122
|
|
|
function($v, $j) use ($row){ |
123
|
|
|
$this->assertEquals($row->{$j}, $v); |
124
|
|
|
$this->assertEquals($row[$j], $v); |
125
|
|
|
} |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
$this->assertTrue($this->DBDriver->raw($this->SQL_RAW_TRUNCATE)); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testRaw(){ |
134
|
|
|
|
135
|
|
|
// don't try this at home! |
136
|
|
|
foreach(range(0, 9) as $k){ |
137
|
|
|
$this->assertTrue($this->DBDriver->raw(sprintf($this->SQL_RAW_INSERT, $k, md5($k)))); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$this->resultTest(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @expectedException \chillerlan\Database\DBException |
145
|
|
|
* @expectedExceptionMessage sql error: |
146
|
|
|
*/ |
147
|
|
|
public function testPreparedSQLError(){ |
148
|
|
|
$this->DBDriver->prepared($this->SQL_RAW_ERROR); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
View Code Duplication |
public function testPrepared(){ |
|
|
|
|
152
|
|
|
|
153
|
|
|
foreach(range(0, 9) as $k){ |
154
|
|
|
$this->assertTrue($this->DBDriver->prepared($this->SQL_PREPARED_INSERT, [$k, md5($k)])); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$this->resultTest(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @expectedException \chillerlan\Database\DBException |
162
|
|
|
* @expectedExceptionMessage invalid data |
163
|
|
|
*/ |
164
|
|
|
public function testMultiInvalidData(){ |
165
|
|
|
$this->DBDriver->multi($this->SQL_RAW_ERROR, []); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @expectedException \chillerlan\Database\DBException |
170
|
|
|
* @expectedExceptionMessage sql error: |
171
|
|
|
*/ |
172
|
|
|
public function testMultiSQLError(){ |
173
|
|
|
$this->DBDriver->multi($this->SQL_RAW_ERROR, [[0]]); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
View Code Duplication |
public function testMulti(){ |
|
|
|
|
177
|
|
|
|
178
|
|
|
$this->assertTrue( |
179
|
|
|
$this->DBDriver->multi( |
180
|
|
|
$this->SQL_PREPARED_INSERT, array_map( |
181
|
|
|
function($k){ |
182
|
|
|
return [$k, md5($k)]; |
183
|
|
|
}, range(0, 9) |
184
|
|
|
) |
185
|
|
|
) |
186
|
|
|
); |
187
|
|
|
|
188
|
|
|
$this->resultTest(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @expectedException \chillerlan\Database\DBException |
193
|
|
|
* @expectedExceptionMessage invalid data |
194
|
|
|
*/ |
195
|
|
|
public function testMultiCallbackInvalidData(){ |
196
|
|
|
$this->DBDriver->multi_callback( |
197
|
|
|
$this->SQL_RAW_ERROR, [], function(){ |
198
|
|
|
}); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @expectedException \chillerlan\Database\DBException |
203
|
|
|
* @expectedExceptionMessage invalid callback |
204
|
|
|
*/ |
205
|
|
|
public function testMultiCallbackInvalidCallback(){ |
206
|
|
|
$this->DBDriver->multi_callback($this->SQL_RAW_ERROR, [[0]], ''); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @expectedException \chillerlan\Database\DBException |
211
|
|
|
* @expectedExceptionMessage sql error: |
212
|
|
|
*/ |
213
|
|
|
public function testMultiCallbackSQLError(){ |
214
|
|
|
$this->DBDriver->multi_callback( |
215
|
|
|
$this->SQL_RAW_ERROR, [[0]], function(){ |
216
|
|
|
return []; |
217
|
|
|
}); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
View Code Duplication |
public function testMultiCallback(){ |
|
|
|
|
221
|
|
|
|
222
|
|
|
$this->assertTrue($this->DBDriver->multi_callback( |
223
|
|
|
$this->SQL_PREPARED_INSERT, range(0, 9), function($k){ |
224
|
|
|
return [$k, md5($k)]; |
225
|
|
|
})); |
226
|
|
|
|
227
|
|
|
$this->resultTest(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function testCached(){ |
231
|
|
|
|
232
|
|
|
$this->assertTrue($this->DBDriver->multi_callback( |
233
|
|
|
$this->SQL_PREPARED_INSERT, range(0, 9), function($k){ |
234
|
|
|
return [$k, md5($k)]; |
235
|
|
|
})); |
236
|
|
|
|
237
|
|
|
$cacheKey = $this->DBDriver->cacheKey($this->SQL_RAW_SELECT_ALL, [], $this->SQL_INDEX_COL); |
238
|
|
|
|
239
|
|
|
// uncached |
240
|
|
|
$this->assertFalse($this->Cache->has($cacheKey)); |
241
|
|
|
$this->DBDriver->rawCached($this->SQL_RAW_SELECT_ALL, $this->SQL_INDEX_COL, true, 2); |
242
|
|
|
|
243
|
|
|
// cached |
244
|
|
|
$this->assertTrue($this->Cache->has($cacheKey)); |
245
|
|
|
$this->DBDriver->rawCached($this->SQL_RAW_SELECT_ALL, $this->SQL_INDEX_COL, true, 2); |
246
|
|
|
|
247
|
|
|
sleep(3); |
248
|
|
|
|
249
|
|
|
$this->Cache->clear(); |
250
|
|
|
|
251
|
|
|
// prepared uncached |
252
|
|
|
$this->assertFalse($this->Cache->has($cacheKey)); |
253
|
|
|
$this->DBDriver->preparedCached($this->SQL_RAW_SELECT_ALL, [], $this->SQL_INDEX_COL, true, 2); |
254
|
|
|
|
255
|
|
|
// prepared cached |
256
|
|
|
$this->assertTrue($this->Cache->has($cacheKey)); |
257
|
|
|
$this->DBDriver->preparedCached($this->SQL_RAW_SELECT_ALL, [], $this->SQL_INDEX_COL, true, 2); |
258
|
|
|
|
259
|
|
|
sleep(3); |
260
|
|
|
$this->Cache->clear(); |
261
|
|
|
|
262
|
|
|
$this->assertFalse($this->Cache->has($cacheKey)); |
263
|
|
|
|
264
|
|
|
$this->assertTrue($this->DBDriver->raw($this->SQL_RAW_TRUNCATE)); |
|
|
|
|
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
} |
268
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.