2 declare(strict_types = 1);
18 use Doctrine\DBAL\Driver\Mysqli\MysqliConnection;
19 use Doctrine\DBAL\Statement;
20 use Prophecy\Prophecy\ObjectProphecy;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
51 protected function setUp()
55 $this->connection = $this->getMockBuilder(Connection::class)
56 ->disableOriginalConstructor()
62 'getDatabasePlatform',
64 'getExpressionBuilder',
65 'getWrappedConnection',
70 $this->connection->expects($this->any())
71 ->method(
'getExpressionBuilder')
72 ->will($this->returnValue(GeneralUtility::makeInstance(ExpressionBuilder::class, $this->connection)));
74 $this->connection->expects($this->any())
77 $this->connection->expects($this->any())
78 ->method(
'getDatabasePlatform')
87 $this->assertInstanceOf(QueryBuilder::class, $this->connection->createQueryBuilder());
110 '"""double"" quotes"',
117 'multiple double quotes' => [
119 '"""""multiple"""""',
121 'multiple single quotes' => [
123 '"\'\'multiple\'\'"',
148 $this->assertSame($expected, $this->connection->quoteIdentifier($input));
166 $this->assertSame($expected, $this->connection->quoteIdentifiers($input));
176 [
'aTestTable', [
'aField' =>
'aValue']],
177 'INSERT INTO "aTestTable" ("aField") VALUES (?)',
181 'multiple values' => [
182 [
'aTestTable', [
'aField' =>
'aValue',
'bField' =>
'bValue']],
183 'INSERT INTO "aTestTable" ("aField", "bField") VALUES (?, ?)',
184 [
'aValue',
'bValue'],
189 'INSERT INTO "aTestTable" ("aField", "bField") VALUES (?, ?)',
190 [
'aValue',
'bValue'],
193 'with types for field' => [
196 [
'aField' => 123,
'bField' =>
'bValue'],
199 'INSERT INTO "aTestTable" ("aField", "bField") VALUES (?, ?)',
214 public function insertQueries(array
$args,
string $expectedQuery, array $expectedValues, array $expectedTypes)
216 $this->connection->expects($this->once())
217 ->method(
'executeUpdate')
218 ->with($expectedQuery, $expectedValues, $expectedTypes)
219 ->will($this->returnValue(1));
221 $this->connection->insert(...
$args);
229 $this->connection->expects($this->once())
230 ->method(
'executeUpdate')
231 ->with(
'INSERT INTO "aTestTable" ("aField") VALUES (?), (?)', [
'aValue',
'anotherValue'])
232 ->will($this->returnValue(2));
234 $this->connection->bulkInsert(
'aTestTable', [[
'aField' =>
'aValue'], [
'aField' =>
'anotherValue']], [
'aField']);
244 [
'aTestTable', [
'aField' =>
'aValue'], [
'uid' => 1]],
245 'UPDATE "aTestTable" SET "aField" = ? WHERE "uid" = ?',
249 'multiple values' => [
250 [
'aTestTable', [
'aField' =>
'aValue',
'bField' =>
'bValue'], [
'uid' => 1]],
251 'UPDATE "aTestTable" SET "aField" = ?, "bField" = ? WHERE "uid" = ?',
252 [
'aValue',
'bValue', 1],
257 'UPDATE "aTestTable" SET "aField" = ? WHERE "uid" = ?',
261 'with types for field' => [
263 'UPDATE "aTestTable" SET "aField" = ? WHERE "uid" = ?',
278 public function updateQueries(array
$args,
string $expectedQuery, array $expectedValues, array $expectedTypes)
280 $this->connection->expects($this->once())
281 ->method(
'executeUpdate')
282 ->with($expectedQuery, $expectedValues, $expectedTypes)
283 ->will($this->returnValue(1));
285 $this->connection->update(...
$args);
294 'single condition' => [
295 [
'aTestTable', [
'aField' =>
'aValue']],
296 'DELETE FROM "aTestTable" WHERE "aField" = ?',
300 'multiple conditions' => [
301 [
'aTestTable', [
'aField' =>
'aValue',
'bField' =>
'bValue']],
302 'DELETE FROM "aTestTable" WHERE "aField" = ? AND "bField" = ?',
303 [
'aValue',
'bValue'],
308 'DELETE FROM "aTestTable" WHERE "aField" = ?',
312 'with types for field' => [
314 'DELETE FROM "aTestTable" WHERE "aField" = ?',
329 public function deleteQueries(array
$args,
string $expectedQuery, array $expectedValues, array $expectedTypes)
331 $this->connection->expects($this->once())
332 ->method(
'executeUpdate')
333 ->with($expectedQuery, $expectedValues, $expectedTypes)
334 ->will($this->returnValue(1));
336 $this->connection->delete(...
$args);
354 'SELECT * FROM "aTable"',
357 'subset of columns' => [
358 [[
'aField',
'anotherField'],
'aTable'],
359 'SELECT "aField", "anotherField" FROM "aTable"',
363 [[
'*'],
'aTable', [
'aField' =>
'aValue']],
364 'SELECT * FROM "aTable" WHERE "aField" = :dcValue1',
365 [
'dcValue1' =>
'aValue'],
368 [[
'*'],
'aTable', [], [
'aField']],
369 'SELECT * FROM "aTable" GROUP BY "aField"',
373 [[
'*'],
'aTable', [], [], [
'aField' =>
'ASC']],
374 'SELECT * FROM "aTable" ORDER BY "aField" ASC',
378 [[
'*'],
'aTable', [], [], [], 1],
379 'SELECT * FROM "aTable" LIMIT 1',
383 [[
'*'],
'aTable', [], [], [], 1, 10],
384 'SELECT * FROM "aTable" LIMIT 1 OFFSET 10',
389 [
'aField',
'anotherField'],
391 [
'aField' =>
'aValue'],
397 'SELECT "aField", "anotherField" FROM "aTable" WHERE "aField" = :dcValue1 ' .
398 'GROUP BY "anotherField" ORDER BY "aField" ASC LIMIT 1 OFFSET 10',
399 [
'dcValue1' =>
'aValue'],
413 $resultStatement = $this->createMock(Statement::class);
415 $this->connection->expects($this->once())
416 ->method(
'executeQuery')
417 ->with($expectedQuery, $expectedParameters)
418 ->will($this->returnValue($resultStatement));
420 $this->connection->select(...
$args);
438 'SELECT COUNT(*) FROM "aTable"',
441 'specified columns' => [
442 [
'aField',
'aTable', []],
443 'SELECT COUNT("aField") FROM "aTable"',
447 [
'aTable.aField',
'aTable', [
'aField' =>
'aValue']],
448 'SELECT COUNT("aTable"."aField") FROM "aTable" WHERE "aField" = :dcValue1',
449 [
'dcValue1' =>
'aValue'],
461 public function countQueries(array
$args,
string $expectedQuery, array $expectedParameters)
463 $resultStatement = $this->createMock(Statement::class);
465 $resultStatement->expects($this->once())
466 ->method(
'fetchColumn')
468 ->will($this->returnValue(0));
470 $this->connection->expects($this->once())
471 ->method(
'executeQuery')
472 ->with($expectedQuery, $expectedParameters)
473 ->will($this->returnValue($resultStatement));
475 $this->connection->count(...
$args);
483 $this->connection->expects($this->once())
484 ->method(
'executeUpdate')
485 ->with(
'TRUNCATE "aTestTable"')
486 ->will($this->returnValue(0));
488 $this->connection->truncate(
'aTestTable',
false);
497 $driverProphet = $this->prophesize(\Doctrine\DBAL\Driver\Mysqli\Driver::class);
498 $driverProphet->willImplement(\Doctrine\DBAL\VersionAwarePlatformDriver::class);
501 $wrappedConnectionProphet = $this->prophesize(\Doctrine\DBAL\Driver\Mysqli\MysqliConnection::class);
502 $wrappedConnectionProphet->willImplement(\Doctrine\DBAL\Driver\ServerInfoAwareConnection::class);
503 $wrappedConnectionProphet->requiresQueryForServerVersion()->willReturn(
false);
504 $wrappedConnectionProphet->getServerVersion()->willReturn(
'5.7.11');
506 $this->connection->expects($this->any())
507 ->method(
'getDriver')
508 ->willReturn($driverProphet->reveal());
509 $this->connection->expects($this->any())
510 ->method(
'getWrappedConnection')
511 ->willReturn($wrappedConnectionProphet->reveal());
513 $this->assertSame(
'mock 5.7.11', $this->connection->getServerVersion());