1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Tests; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Cache\Cache; |
8
|
|
|
use Doctrine\Common\EventManager; |
9
|
|
|
use Doctrine\DBAL\Cache\ArrayStatement; |
10
|
|
|
use Doctrine\DBAL\Cache\QueryCacheProfile; |
11
|
|
|
use Doctrine\DBAL\Configuration; |
12
|
|
|
use Doctrine\DBAL\Connection; |
13
|
|
|
use Doctrine\DBAL\ConnectionException; |
14
|
|
|
use Doctrine\DBAL\DBALException; |
15
|
|
|
use Doctrine\DBAL\Driver; |
16
|
|
|
use Doctrine\DBAL\Driver\Connection as DriverConnection; |
17
|
|
|
use Doctrine\DBAL\Driver\ServerInfoAwareConnection; |
18
|
|
|
use Doctrine\DBAL\Driver\Statement; |
19
|
|
|
use Doctrine\DBAL\DriverManager; |
20
|
|
|
use Doctrine\DBAL\Events; |
21
|
|
|
use Doctrine\DBAL\Exception\InvalidArgumentException; |
22
|
|
|
use Doctrine\DBAL\Logging\DebugStack; |
23
|
|
|
use Doctrine\DBAL\ParameterType; |
24
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
25
|
|
|
use Doctrine\DBAL\VersionAwarePlatformDriver; |
26
|
|
|
use Exception; |
27
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
28
|
|
|
use PHPUnit\Framework\TestCase; |
29
|
|
|
use stdClass; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @requires extension pdo_mysql |
33
|
|
|
*/ |
34
|
|
|
class ConnectionTest extends TestCase |
35
|
|
|
{ |
36
|
|
|
/** @var Connection */ |
37
|
|
|
private $connection; |
38
|
|
|
|
39
|
|
|
/** @var array<string, mixed> */ |
40
|
|
|
protected $params = [ |
41
|
|
|
'driver' => 'pdo_mysql', |
42
|
|
|
'host' => 'localhost', |
43
|
|
|
'user' => 'root', |
44
|
|
|
'password' => 'password', |
45
|
|
|
'port' => 1234, |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
protected function setUp() : void |
49
|
|
|
{ |
50
|
|
|
$this->connection = DriverManager::getConnection($this->params); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return Connection|MockObject |
55
|
|
|
*/ |
56
|
|
|
private function getExecuteUpdateMockConnection() |
57
|
|
|
{ |
58
|
|
|
$driverMock = $this->createMock(Driver::class); |
59
|
|
|
|
60
|
|
|
$driverMock->expects(self::any()) |
61
|
|
|
->method('connect') |
62
|
|
|
->will(self::returnValue( |
63
|
|
|
$this->createMock(DriverConnection::class) |
64
|
|
|
)); |
65
|
|
|
|
66
|
|
|
$platform = $this->getMockForAbstractClass(AbstractPlatform::class); |
67
|
|
|
|
68
|
|
|
return $this->getMockBuilder(Connection::class) |
69
|
|
|
->onlyMethods(['executeUpdate']) |
70
|
|
|
->setConstructorArgs([['platform' => $platform], $driverMock]) |
71
|
|
|
->getMock(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testIsConnected() : void |
75
|
|
|
{ |
76
|
|
|
self::assertFalse($this->connection->isConnected()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testNoTransactionActiveByDefault() : void |
80
|
|
|
{ |
81
|
|
|
self::assertFalse($this->connection->isTransactionActive()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testCommitWithNoActiveTransactionThrowsException() : void |
85
|
|
|
{ |
86
|
|
|
$this->expectException(ConnectionException::class); |
87
|
|
|
$this->connection->commit(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testRollbackWithNoActiveTransactionThrowsException() : void |
91
|
|
|
{ |
92
|
|
|
$this->expectException(ConnectionException::class); |
93
|
|
|
$this->connection->rollBack(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testSetRollbackOnlyNoActiveTransactionThrowsException() : void |
97
|
|
|
{ |
98
|
|
|
$this->expectException(ConnectionException::class); |
99
|
|
|
$this->connection->setRollbackOnly(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testIsRollbackOnlyNoActiveTransactionThrowsException() : void |
103
|
|
|
{ |
104
|
|
|
$this->expectException(ConnectionException::class); |
105
|
|
|
$this->connection->isRollbackOnly(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testGetDriver() : void |
109
|
|
|
{ |
110
|
|
|
self::assertInstanceOf(\Doctrine\DBAL\Driver\PDOMySql\Driver::class, $this->connection->getDriver()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testConnectDispatchEvent() : void |
114
|
|
|
{ |
115
|
|
|
$listenerMock = $this->getMockBuilder($this->getMockClass('ConnectDispatchEventListener')) |
116
|
|
|
->addMethods(['postConnect']) |
117
|
|
|
->getMock(); |
118
|
|
|
$listenerMock->expects(self::once())->method('postConnect'); |
119
|
|
|
|
120
|
|
|
$eventManager = new EventManager(); |
121
|
|
|
$eventManager->addEventListener([Events::postConnect], $listenerMock); |
122
|
|
|
|
123
|
|
|
$driverMock = $this->createMock(Driver::class); |
124
|
|
|
$driverMock->expects(self::at(0)) |
125
|
|
|
->method('connect'); |
126
|
|
|
|
127
|
|
|
$conn = new Connection([], $driverMock, new Configuration(), $eventManager); |
128
|
|
|
$conn->connect(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testEventManagerPassedToPlatform() : void |
132
|
|
|
{ |
133
|
|
|
$eventManager = new EventManager(); |
134
|
|
|
|
135
|
|
|
$platform = $this->createMock(AbstractPlatform::class); |
136
|
|
|
$platform->expects(self::once()) |
137
|
|
|
->method('setEventManager') |
138
|
|
|
->with($eventManager); |
139
|
|
|
|
140
|
|
|
$driver = $this->createMock(Driver::class); |
141
|
|
|
$driver->expects(self::any()) |
142
|
|
|
->method('getDatabasePlatform') |
143
|
|
|
->willReturn($platform); |
144
|
|
|
|
145
|
|
|
$connection = new Connection($this->params, $driver, null, $eventManager); |
146
|
|
|
$connection->getDatabasePlatform(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @requires extension pdo_sqlite |
151
|
|
|
* @dataProvider getQueryMethods |
152
|
|
|
*/ |
153
|
|
|
public function testDriverExceptionIsWrapped(callable $callback) : void |
154
|
|
|
{ |
155
|
|
|
$this->expectException(DBALException::class); |
156
|
|
|
$this->expectExceptionMessage("An exception occurred while executing \"MUUHAAAAHAAAA\":\n\nSQLSTATE[HY000]: General error: 1 near \"MUUHAAAAHAAAA\""); |
157
|
|
|
|
158
|
|
|
$connection = DriverManager::getConnection([ |
159
|
|
|
'driver' => 'pdo_sqlite', |
160
|
|
|
'memory' => true, |
161
|
|
|
]); |
162
|
|
|
|
163
|
|
|
$callback($connection, 'MUUHAAAAHAAAA'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return iterable<string, array<int, callable>> |
168
|
|
|
*/ |
169
|
|
|
public static function getQueryMethods() : iterable |
170
|
|
|
{ |
171
|
|
|
yield 'exec' => [ |
172
|
|
|
static function (Connection $connection, string $statement) : void { |
173
|
|
|
$connection->exec($statement); |
174
|
|
|
}, |
175
|
|
|
]; |
176
|
|
|
|
177
|
|
|
yield 'query' => [ |
178
|
|
|
static function (Connection $connection, string $statement) : void { |
179
|
|
|
$connection->query($statement); |
180
|
|
|
}, |
181
|
|
|
]; |
182
|
|
|
|
183
|
|
|
yield 'executeQuery' => [ |
184
|
|
|
static function (Connection $connection, string $statement) : void { |
185
|
|
|
$connection->executeQuery($statement); |
186
|
|
|
}, |
187
|
|
|
]; |
188
|
|
|
|
189
|
|
|
yield 'executeUpdate' => [ |
190
|
|
|
static function (Connection $connection, string $statement) : void { |
191
|
|
|
$connection->executeUpdate($statement); |
192
|
|
|
}, |
193
|
|
|
]; |
194
|
|
|
|
195
|
|
|
yield 'prepare' => [ |
196
|
|
|
static function (Connection $connection, string $statement) : void { |
197
|
|
|
$connection->prepare($statement); |
198
|
|
|
}, |
199
|
|
|
]; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Pretty dumb test, however we want to check that the DebugStack correctly implements the interface. |
204
|
|
|
* |
205
|
|
|
* @group DBAL-11 |
206
|
|
|
*/ |
207
|
|
|
public function testDebugSQLStack() : void |
208
|
|
|
{ |
209
|
|
|
$logger = new DebugStack(); |
210
|
|
|
$this->connection->getConfiguration()->setSQLLogger($logger); |
211
|
|
|
self::assertSame($logger, $this->connection->getConfiguration()->getSQLLogger()); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @group DBAL-81 |
216
|
|
|
*/ |
217
|
|
|
public function testIsAutoCommit() : void |
218
|
|
|
{ |
219
|
|
|
self::assertTrue($this->connection->isAutoCommit()); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @group DBAL-81 |
224
|
|
|
*/ |
225
|
|
|
public function testSetAutoCommit() : void |
226
|
|
|
{ |
227
|
|
|
$this->connection->setAutoCommit(false); |
228
|
|
|
self::assertFalse($this->connection->isAutoCommit()); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @group DBAL-81 |
233
|
|
|
*/ |
234
|
|
|
public function testConnectStartsTransactionInNoAutoCommitMode() : void |
235
|
|
|
{ |
236
|
|
|
$driverMock = $this->createMock(Driver::class); |
237
|
|
|
$driverMock->expects(self::any()) |
238
|
|
|
->method('connect') |
239
|
|
|
->will(self::returnValue( |
240
|
|
|
$this->createMock(DriverConnection::class) |
241
|
|
|
)); |
242
|
|
|
$conn = new Connection([], $driverMock); |
243
|
|
|
|
244
|
|
|
$conn->setAutoCommit(false); |
245
|
|
|
|
246
|
|
|
self::assertFalse($conn->isTransactionActive()); |
247
|
|
|
|
248
|
|
|
$conn->connect(); |
249
|
|
|
|
250
|
|
|
self::assertTrue($conn->isTransactionActive()); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @group DBAL-81 |
255
|
|
|
*/ |
256
|
|
|
public function testCommitStartsTransactionInNoAutoCommitMode() : void |
257
|
|
|
{ |
258
|
|
|
$driverMock = $this->createMock(Driver::class); |
259
|
|
|
$driverMock->expects(self::any()) |
260
|
|
|
->method('connect') |
261
|
|
|
->will(self::returnValue( |
262
|
|
|
$this->createMock(DriverConnection::class) |
263
|
|
|
)); |
264
|
|
|
$conn = new Connection([], $driverMock); |
265
|
|
|
|
266
|
|
|
$conn->setAutoCommit(false); |
267
|
|
|
$conn->connect(); |
268
|
|
|
$conn->commit(); |
269
|
|
|
|
270
|
|
|
self::assertTrue($conn->isTransactionActive()); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @return bool[][] |
275
|
|
|
*/ |
276
|
|
|
public function resultProvider() : array |
277
|
|
|
{ |
278
|
|
|
return [[true], [false]]; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @group DBAL-81 |
283
|
|
|
*/ |
284
|
|
|
public function testRollBackStartsTransactionInNoAutoCommitMode() : void |
285
|
|
|
{ |
286
|
|
|
$driverMock = $this->createMock(Driver::class); |
287
|
|
|
$driverMock->expects(self::any()) |
288
|
|
|
->method('connect') |
289
|
|
|
->will(self::returnValue( |
290
|
|
|
$this->createMock(DriverConnection::class) |
291
|
|
|
)); |
292
|
|
|
$conn = new Connection([], $driverMock); |
293
|
|
|
|
294
|
|
|
$conn->setAutoCommit(false); |
295
|
|
|
$conn->connect(); |
296
|
|
|
$conn->rollBack(); |
297
|
|
|
|
298
|
|
|
self::assertTrue($conn->isTransactionActive()); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @group DBAL-81 |
303
|
|
|
*/ |
304
|
|
|
public function testSwitchingAutoCommitModeCommitsAllCurrentTransactions() : void |
305
|
|
|
{ |
306
|
|
|
$driverMock = $this->createMock(Driver::class); |
307
|
|
|
$driverMock->expects(self::any()) |
308
|
|
|
->method('connect') |
309
|
|
|
->will(self::returnValue( |
310
|
|
|
$this->createMock(DriverConnection::class) |
311
|
|
|
)); |
312
|
|
|
$conn = new Connection([], $driverMock); |
313
|
|
|
|
314
|
|
|
$conn->connect(); |
315
|
|
|
$conn->beginTransaction(); |
316
|
|
|
$conn->beginTransaction(); |
317
|
|
|
$conn->setAutoCommit(false); |
318
|
|
|
|
319
|
|
|
self::assertSame(1, $conn->getTransactionNestingLevel()); |
320
|
|
|
|
321
|
|
|
$conn->beginTransaction(); |
322
|
|
|
$conn->beginTransaction(); |
323
|
|
|
$conn->setAutoCommit(true); |
324
|
|
|
|
325
|
|
|
self::assertFalse($conn->isTransactionActive()); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
public function testEmptyInsert() : void |
329
|
|
|
{ |
330
|
|
|
$conn = $this->getExecuteUpdateMockConnection(); |
331
|
|
|
|
332
|
|
|
$conn->expects(self::once()) |
333
|
|
|
->method('executeUpdate') |
334
|
|
|
->with('INSERT INTO footable () VALUES ()'); |
335
|
|
|
|
336
|
|
|
$conn->insert('footable', []); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @group DBAL-2511 |
341
|
|
|
*/ |
342
|
|
|
public function testUpdateWithDifferentColumnsInDataAndIdentifiers() : void |
343
|
|
|
{ |
344
|
|
|
$conn = $this->getExecuteUpdateMockConnection(); |
345
|
|
|
|
346
|
|
|
$conn->expects(self::once()) |
347
|
|
|
->method('executeUpdate') |
348
|
|
|
->with( |
349
|
|
|
'UPDATE TestTable SET text = ?, is_edited = ? WHERE id = ? AND name = ?', |
350
|
|
|
[ |
351
|
|
|
'some text', |
352
|
|
|
true, |
353
|
|
|
1, |
354
|
|
|
'foo', |
355
|
|
|
], |
356
|
|
|
[ |
357
|
|
|
'string', |
358
|
|
|
'boolean', |
359
|
|
|
'integer', |
360
|
|
|
'string', |
361
|
|
|
] |
362
|
|
|
); |
363
|
|
|
|
364
|
|
|
$conn->update( |
365
|
|
|
'TestTable', |
366
|
|
|
[ |
367
|
|
|
'text' => 'some text', |
368
|
|
|
'is_edited' => true, |
369
|
|
|
], |
370
|
|
|
[ |
371
|
|
|
'id' => 1, |
372
|
|
|
'name' => 'foo', |
373
|
|
|
], |
374
|
|
|
[ |
375
|
|
|
'text' => 'string', |
376
|
|
|
'is_edited' => 'boolean', |
377
|
|
|
'id' => 'integer', |
378
|
|
|
'name' => 'string', |
379
|
|
|
] |
380
|
|
|
); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* @group DBAL-2511 |
385
|
|
|
*/ |
386
|
|
|
public function testUpdateWithSameColumnInDataAndIdentifiers() : void |
387
|
|
|
{ |
388
|
|
|
$conn = $this->getExecuteUpdateMockConnection(); |
389
|
|
|
|
390
|
|
|
$conn->expects(self::once()) |
391
|
|
|
->method('executeUpdate') |
392
|
|
|
->with( |
393
|
|
|
'UPDATE TestTable SET text = ?, is_edited = ? WHERE id = ? AND is_edited = ?', |
394
|
|
|
[ |
395
|
|
|
'some text', |
396
|
|
|
true, |
397
|
|
|
1, |
398
|
|
|
false, |
399
|
|
|
], |
400
|
|
|
[ |
401
|
|
|
'string', |
402
|
|
|
'boolean', |
403
|
|
|
'integer', |
404
|
|
|
'boolean', |
405
|
|
|
] |
406
|
|
|
); |
407
|
|
|
|
408
|
|
|
$conn->update( |
409
|
|
|
'TestTable', |
410
|
|
|
[ |
411
|
|
|
'text' => 'some text', |
412
|
|
|
'is_edited' => true, |
413
|
|
|
], |
414
|
|
|
[ |
415
|
|
|
'id' => 1, |
416
|
|
|
'is_edited' => false, |
417
|
|
|
], |
418
|
|
|
[ |
419
|
|
|
'text' => 'string', |
420
|
|
|
'is_edited' => 'boolean', |
421
|
|
|
'id' => 'integer', |
422
|
|
|
] |
423
|
|
|
); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* @group DBAL-2688 |
428
|
|
|
*/ |
429
|
|
|
public function testUpdateWithIsNull() : void |
430
|
|
|
{ |
431
|
|
|
$conn = $this->getExecuteUpdateMockConnection(); |
432
|
|
|
|
433
|
|
|
$conn->expects(self::once()) |
434
|
|
|
->method('executeUpdate') |
435
|
|
|
->with( |
436
|
|
|
'UPDATE TestTable SET text = ?, is_edited = ? WHERE id IS NULL AND name = ?', |
437
|
|
|
[ |
438
|
|
|
'some text', |
439
|
|
|
null, |
440
|
|
|
'foo', |
441
|
|
|
], |
442
|
|
|
[ |
443
|
|
|
'string', |
444
|
|
|
'boolean', |
445
|
|
|
'string', |
446
|
|
|
] |
447
|
|
|
); |
448
|
|
|
|
449
|
|
|
$conn->update( |
450
|
|
|
'TestTable', |
451
|
|
|
[ |
452
|
|
|
'text' => 'some text', |
453
|
|
|
'is_edited' => null, |
454
|
|
|
], |
455
|
|
|
[ |
456
|
|
|
'id' => null, |
457
|
|
|
'name' => 'foo', |
458
|
|
|
], |
459
|
|
|
[ |
460
|
|
|
'text' => 'string', |
461
|
|
|
'is_edited' => 'boolean', |
462
|
|
|
'id' => 'integer', |
463
|
|
|
'name' => 'string', |
464
|
|
|
] |
465
|
|
|
); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* @group DBAL-2688 |
470
|
|
|
*/ |
471
|
|
|
public function testDeleteWithIsNull() : void |
472
|
|
|
{ |
473
|
|
|
$conn = $this->getExecuteUpdateMockConnection(); |
474
|
|
|
|
475
|
|
|
$conn->expects(self::once()) |
476
|
|
|
->method('executeUpdate') |
477
|
|
|
->with( |
478
|
|
|
'DELETE FROM TestTable WHERE id IS NULL AND name = ?', |
479
|
|
|
['foo'], |
480
|
|
|
['string'] |
481
|
|
|
); |
482
|
|
|
|
483
|
|
|
$conn->delete( |
484
|
|
|
'TestTable', |
485
|
|
|
[ |
486
|
|
|
'id' => null, |
487
|
|
|
'name' => 'foo', |
488
|
|
|
], |
489
|
|
|
[ |
490
|
|
|
'id' => 'integer', |
491
|
|
|
'name' => 'string', |
492
|
|
|
] |
493
|
|
|
); |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
public function testFetchAssociative() : void |
497
|
|
|
{ |
498
|
|
|
$statement = 'SELECT * FROM foo WHERE bar = ?'; |
499
|
|
|
$params = [666]; |
500
|
|
|
$types = [ParameterType::INTEGER]; |
501
|
|
|
$result = []; |
502
|
|
|
|
503
|
|
|
$driverMock = $this->createMock(Driver::class); |
504
|
|
|
|
505
|
|
|
$driverMock->expects(self::any()) |
506
|
|
|
->method('connect') |
507
|
|
|
->will(self::returnValue( |
508
|
|
|
$this->createMock(DriverConnection::class) |
509
|
|
|
)); |
510
|
|
|
|
511
|
|
|
$driverStatementMock = $this->createMock(Statement::class); |
512
|
|
|
|
513
|
|
|
$driverStatementMock->expects(self::once()) |
514
|
|
|
->method('fetchAssociative') |
515
|
|
|
->will(self::returnValue($result)); |
516
|
|
|
|
517
|
|
|
$conn = $this->getMockBuilder(Connection::class) |
518
|
|
|
->onlyMethods(['executeQuery']) |
519
|
|
|
->setConstructorArgs([[], $driverMock]) |
520
|
|
|
->getMock(); |
521
|
|
|
|
522
|
|
|
$conn->expects(self::once()) |
523
|
|
|
->method('executeQuery') |
524
|
|
|
->with($statement, $params, $types) |
525
|
|
|
->will(self::returnValue($driverStatementMock)); |
526
|
|
|
|
527
|
|
|
self::assertSame($result, $conn->fetchAssociative($statement, $params, $types)); |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
public function testFetchNumeric() : void |
531
|
|
|
{ |
532
|
|
|
$statement = 'SELECT * FROM foo WHERE bar = ?'; |
533
|
|
|
$params = [666]; |
534
|
|
|
$types = [ParameterType::INTEGER]; |
535
|
|
|
$result = []; |
536
|
|
|
|
537
|
|
|
$driverMock = $this->createMock(Driver::class); |
538
|
|
|
|
539
|
|
|
$driverMock->expects(self::any()) |
540
|
|
|
->method('connect') |
541
|
|
|
->will(self::returnValue( |
542
|
|
|
$this->createMock(DriverConnection::class) |
543
|
|
|
)); |
544
|
|
|
|
545
|
|
|
$driverStatementMock = $this->createMock(Statement::class); |
546
|
|
|
|
547
|
|
|
$driverStatementMock->expects(self::once()) |
548
|
|
|
->method('fetchNumeric') |
549
|
|
|
->will(self::returnValue($result)); |
550
|
|
|
|
551
|
|
|
$conn = $this->getMockBuilder(Connection::class) |
552
|
|
|
->onlyMethods(['executeQuery']) |
553
|
|
|
->setConstructorArgs([[], $driverMock]) |
554
|
|
|
->getMock(); |
555
|
|
|
|
556
|
|
|
$conn->expects(self::once()) |
557
|
|
|
->method('executeQuery') |
558
|
|
|
->with($statement, $params, $types) |
559
|
|
|
->will(self::returnValue($driverStatementMock)); |
560
|
|
|
|
561
|
|
|
self::assertSame($result, $conn->fetchNumeric($statement, $params, $types)); |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
public function testFetchOne() : void |
565
|
|
|
{ |
566
|
|
|
$statement = 'SELECT * FROM foo WHERE bar = ?'; |
567
|
|
|
$params = [666]; |
568
|
|
|
$types = [ParameterType::INTEGER]; |
569
|
|
|
$result = []; |
570
|
|
|
|
571
|
|
|
$driverMock = $this->createMock(Driver::class); |
572
|
|
|
|
573
|
|
|
$driverMock->expects(self::any()) |
574
|
|
|
->method('connect') |
575
|
|
|
->will(self::returnValue( |
576
|
|
|
$this->createMock(DriverConnection::class) |
577
|
|
|
)); |
578
|
|
|
|
579
|
|
|
$driverStatementMock = $this->createMock(Statement::class); |
580
|
|
|
|
581
|
|
|
$driverStatementMock->expects(self::once()) |
582
|
|
|
->method('fetchOne') |
583
|
|
|
->will(self::returnValue($result)); |
584
|
|
|
|
585
|
|
|
$conn = $this->getMockBuilder(Connection::class) |
586
|
|
|
->onlyMethods(['executeQuery']) |
587
|
|
|
->setConstructorArgs([[], $driverMock]) |
588
|
|
|
->getMock(); |
589
|
|
|
|
590
|
|
|
$conn->expects(self::once()) |
591
|
|
|
->method('executeQuery') |
592
|
|
|
->with($statement, $params, $types) |
593
|
|
|
->will(self::returnValue($driverStatementMock)); |
594
|
|
|
|
595
|
|
|
self::assertSame($result, $conn->fetchOne($statement, $params, $types)); |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
public function testFetchAllAssociative() : void |
599
|
|
|
{ |
600
|
|
|
$statement = 'SELECT * FROM foo WHERE bar = ?'; |
601
|
|
|
$params = [666]; |
602
|
|
|
$types = [ParameterType::INTEGER]; |
603
|
|
|
$result = []; |
604
|
|
|
|
605
|
|
|
$driverMock = $this->createMock(Driver::class); |
606
|
|
|
|
607
|
|
|
$driverMock->expects(self::any()) |
608
|
|
|
->method('connect') |
609
|
|
|
->will(self::returnValue( |
610
|
|
|
$this->createMock(DriverConnection::class) |
611
|
|
|
)); |
612
|
|
|
|
613
|
|
|
$driverStatementMock = $this->createMock(Statement::class); |
614
|
|
|
|
615
|
|
|
$driverStatementMock->expects(self::once()) |
616
|
|
|
->method('fetchAllAssociative') |
617
|
|
|
->will(self::returnValue($result)); |
618
|
|
|
|
619
|
|
|
$conn = $this->getMockBuilder(Connection::class) |
620
|
|
|
->onlyMethods(['executeQuery']) |
621
|
|
|
->setConstructorArgs([[], $driverMock]) |
622
|
|
|
->getMock(); |
623
|
|
|
|
624
|
|
|
$conn->expects(self::once()) |
625
|
|
|
->method('executeQuery') |
626
|
|
|
->with($statement, $params, $types) |
627
|
|
|
->will(self::returnValue($driverStatementMock)); |
628
|
|
|
|
629
|
|
|
self::assertSame($result, $conn->fetchAllAssociative($statement, $params, $types)); |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
public function testCallingDeleteWithNoDeletionCriteriaResultsInInvalidArgumentException() : void |
633
|
|
|
{ |
634
|
|
|
$driver = $this->createMock(Driver::class); |
635
|
|
|
$conn = new Connection([], $driver); |
636
|
|
|
|
637
|
|
|
$this->expectException(InvalidArgumentException::class); |
638
|
|
|
$conn->delete('kittens', []); |
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
public function testCallConnectOnce() : void |
642
|
|
|
{ |
643
|
|
|
$driver = $this->createMock(Driver::class); |
644
|
|
|
$driver->expects(self::once()) |
645
|
|
|
->method('connect'); |
646
|
|
|
|
647
|
|
|
$platform = $this->createMock(AbstractPlatform::class); |
648
|
|
|
|
649
|
|
|
$conn = new Connection(['platform' => $platform], $driver); |
650
|
|
|
$conn->connect(); |
651
|
|
|
$conn->connect(); |
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
/** |
655
|
|
|
* @group DBAL-1127 |
656
|
|
|
*/ |
657
|
|
|
public function testPlatformDetectionIsTriggerOnlyOnceOnRetrievingPlatform() : void |
658
|
|
|
{ |
659
|
|
|
$driverMock = $this->createMock(VersionAwarePlatformDriver::class); |
660
|
|
|
|
661
|
|
|
$driverConnectionMock = $this->createMock(ServerInfoAwareConnection::class); |
662
|
|
|
|
663
|
|
|
$platformMock = $this->getMockForAbstractClass(AbstractPlatform::class); |
664
|
|
|
|
665
|
|
|
$connection = new Connection([], $driverMock); |
666
|
|
|
|
667
|
|
|
$driverMock->expects(self::once()) |
668
|
|
|
->method('connect') |
669
|
|
|
->will(self::returnValue($driverConnectionMock)); |
670
|
|
|
|
671
|
|
|
$driverConnectionMock->expects(self::once()) |
672
|
|
|
->method('getServerVersion') |
673
|
|
|
->will(self::returnValue('6.6.6')); |
674
|
|
|
|
675
|
|
|
$driverMock->expects(self::once()) |
676
|
|
|
->method('createDatabasePlatformForVersion') |
677
|
|
|
->with('6.6.6') |
678
|
|
|
->will(self::returnValue($platformMock)); |
679
|
|
|
|
680
|
|
|
self::assertSame($platformMock, $connection->getDatabasePlatform()); |
681
|
|
|
} |
682
|
|
|
|
683
|
|
|
public function testConnectionParamsArePassedToTheQueryCacheProfileInExecuteCacheQuery() : void |
684
|
|
|
{ |
685
|
|
|
$resultCacheDriverMock = $this->createMock(Cache::class); |
686
|
|
|
|
687
|
|
|
$resultCacheDriverMock |
688
|
|
|
->expects(self::atLeastOnce()) |
689
|
|
|
->method('fetch') |
690
|
|
|
->with('cacheKey') |
691
|
|
|
->will(self::returnValue(['realKey' => []])); |
692
|
|
|
|
693
|
|
|
$query = 'SELECT * FROM foo WHERE bar = ?'; |
694
|
|
|
$params = [666]; |
695
|
|
|
$types = [ParameterType::INTEGER]; |
696
|
|
|
|
697
|
|
|
$queryCacheProfileMock = $this->createMock(QueryCacheProfile::class); |
698
|
|
|
|
699
|
|
|
$queryCacheProfileMock |
700
|
|
|
->expects(self::any()) |
701
|
|
|
->method('getResultCacheDriver') |
702
|
|
|
->will(self::returnValue($resultCacheDriverMock)); |
703
|
|
|
|
704
|
|
|
// This is our main expectation |
705
|
|
|
$queryCacheProfileMock |
706
|
|
|
->expects(self::once()) |
707
|
|
|
->method('generateCacheKeys') |
708
|
|
|
->with($query, $params, $types, $this->params) |
709
|
|
|
->will(self::returnValue(['cacheKey', 'realKey'])); |
710
|
|
|
|
711
|
|
|
$driver = $this->createMock(Driver::class); |
712
|
|
|
|
713
|
|
|
self::assertInstanceOf( |
714
|
|
|
ArrayStatement::class, |
715
|
|
|
(new Connection($this->params, $driver))->executeCacheQuery($query, $params, $types, $queryCacheProfileMock) |
716
|
|
|
); |
717
|
|
|
} |
718
|
|
|
|
719
|
|
|
/** |
720
|
|
|
* @group #2821 |
721
|
|
|
*/ |
722
|
|
|
public function testShouldNotPassPlatformInParamsToTheQueryCacheProfileInExecuteCacheQuery() : void |
723
|
|
|
{ |
724
|
|
|
$resultCacheDriverMock = $this->createMock(Cache::class); |
725
|
|
|
|
726
|
|
|
$resultCacheDriverMock |
727
|
|
|
->expects(self::atLeastOnce()) |
728
|
|
|
->method('fetch') |
729
|
|
|
->with('cacheKey') |
730
|
|
|
->will(self::returnValue(['realKey' => []])); |
731
|
|
|
|
732
|
|
|
$queryCacheProfileMock = $this->createMock(QueryCacheProfile::class); |
733
|
|
|
|
734
|
|
|
$queryCacheProfileMock |
735
|
|
|
->expects(self::any()) |
736
|
|
|
->method('getResultCacheDriver') |
737
|
|
|
->will(self::returnValue($resultCacheDriverMock)); |
738
|
|
|
|
739
|
|
|
$query = 'SELECT 1'; |
740
|
|
|
|
741
|
|
|
$connectionParams = $this->params; |
742
|
|
|
|
743
|
|
|
$queryCacheProfileMock |
744
|
|
|
->expects(self::once()) |
745
|
|
|
->method('generateCacheKeys') |
746
|
|
|
->with($query, [], [], $connectionParams) |
747
|
|
|
->will(self::returnValue(['cacheKey', 'realKey'])); |
748
|
|
|
|
749
|
|
|
$connectionParams['platform'] = $this->createMock(AbstractPlatform::class); |
750
|
|
|
|
751
|
|
|
$driver = $this->createMock(Driver::class); |
752
|
|
|
|
753
|
|
|
(new Connection($connectionParams, $driver))->executeCacheQuery($query, [], [], $queryCacheProfileMock); |
754
|
|
|
} |
755
|
|
|
|
756
|
|
|
/** |
757
|
|
|
* @group #2821 |
758
|
|
|
*/ |
759
|
|
|
public function testThrowsExceptionWhenInValidPlatformSpecified() : void |
760
|
|
|
{ |
761
|
|
|
$connectionParams = $this->params; |
762
|
|
|
$connectionParams['platform'] = new stdClass(); |
763
|
|
|
|
764
|
|
|
$driver = $this->createMock(Driver::class); |
765
|
|
|
|
766
|
|
|
$this->expectException(DBALException::class); |
767
|
|
|
|
768
|
|
|
new Connection($connectionParams, $driver); |
769
|
|
|
} |
770
|
|
|
|
771
|
|
|
/** |
772
|
|
|
* @group DBAL-990 |
773
|
|
|
*/ |
774
|
|
|
public function testRethrowsOriginalExceptionOnDeterminingPlatformWhenConnectingToNonExistentDatabase() : void |
775
|
|
|
{ |
776
|
|
|
$driverMock = $this->createMock(VersionAwarePlatformDriver::class); |
777
|
|
|
|
778
|
|
|
$connection = new Connection(['dbname' => 'foo'], $driverMock); |
779
|
|
|
$originalException = new Exception('Original exception'); |
780
|
|
|
$fallbackException = new Exception('Fallback exception'); |
781
|
|
|
|
782
|
|
|
$driverMock->expects(self::at(0)) |
783
|
|
|
->method('connect') |
784
|
|
|
->willThrowException($originalException); |
785
|
|
|
|
786
|
|
|
$driverMock->expects(self::at(1)) |
787
|
|
|
->method('connect') |
788
|
|
|
->willThrowException($fallbackException); |
789
|
|
|
|
790
|
|
|
$this->expectExceptionMessage($originalException->getMessage()); |
791
|
|
|
|
792
|
|
|
$connection->getDatabasePlatform(); |
793
|
|
|
} |
794
|
|
|
|
795
|
|
|
/** |
796
|
|
|
* @group #3194 |
797
|
|
|
*/ |
798
|
|
|
public function testExecuteCacheQueryStripsPlatformFromConnectionParamsBeforeGeneratingCacheKeys() : void |
799
|
|
|
{ |
800
|
|
|
$driver = $this->createMock(Driver::class); |
801
|
|
|
|
802
|
|
|
$platform = $this->createMock(AbstractPlatform::class); |
803
|
|
|
|
804
|
|
|
$queryCacheProfile = $this->createMock(QueryCacheProfile::class); |
805
|
|
|
|
806
|
|
|
$resultCacheDriver = $this->createMock(Cache::class); |
807
|
|
|
|
808
|
|
|
$queryCacheProfile |
809
|
|
|
->expects(self::any()) |
810
|
|
|
->method('getResultCacheDriver') |
811
|
|
|
->will(self::returnValue($resultCacheDriver)); |
812
|
|
|
|
813
|
|
|
$resultCacheDriver |
814
|
|
|
->expects(self::atLeastOnce()) |
815
|
|
|
->method('fetch') |
816
|
|
|
->with('cacheKey') |
817
|
|
|
->will(self::returnValue(['realKey' => []])); |
818
|
|
|
|
819
|
|
|
$query = 'SELECT 1'; |
820
|
|
|
|
821
|
|
|
$params = [ |
822
|
|
|
'dbname' => 'foo', |
823
|
|
|
'platform' => $platform, |
824
|
|
|
]; |
825
|
|
|
|
826
|
|
|
$paramsWithoutPlatform = $params; |
827
|
|
|
unset($paramsWithoutPlatform['platform']); |
828
|
|
|
|
829
|
|
|
$queryCacheProfile |
830
|
|
|
->expects(self::once()) |
831
|
|
|
->method('generateCacheKeys') |
832
|
|
|
->with($query, [], [], $paramsWithoutPlatform) |
833
|
|
|
->will(self::returnValue(['cacheKey', 'realKey'])); |
834
|
|
|
|
835
|
|
|
$connection = new Connection($params, $driver); |
836
|
|
|
|
837
|
|
|
self::assertSame($params, $connection->getParams()); |
838
|
|
|
|
839
|
|
|
$connection->executeCacheQuery($query, [], [], $queryCacheProfile); |
840
|
|
|
} |
841
|
|
|
} |
842
|
|
|
|