‪TYPO3CMS  9.5
ConnectionTest.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 use Doctrine\DBAL\Driver\Mysqli\MysqliConnection;
19 use Doctrine\DBAL\Statement;
20 use Prophecy\Prophecy\ObjectProphecy;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
31 class ‪ConnectionTest extends UnitTestCase
32 {
36  protected ‪$connection;
37 
41  protected ‪$platform;
42 
46  protected ‪$testTable = 'testTable';
47 
51  protected function ‪setUp()
52  {
53  parent::setUp();
54 
55  $this->connection = $this->getMockBuilder(Connection::class)
56  ->disableOriginalConstructor()
57  ->setMethods(
58  [
59  'connect',
60  'executeQuery',
61  'executeUpdate',
62  'getDatabasePlatform',
63  'getDriver',
64  'getExpressionBuilder',
65  'getWrappedConnection',
66  ]
67  )
68  ->getMock();
69 
70  $this->connection->expects($this->any())
71  ->method('getExpressionBuilder')
72  ->will($this->returnValue(GeneralUtility::makeInstance(ExpressionBuilder::class, $this->connection)));
73 
74  $this->connection->expects($this->any())
75  ->method('connect');
76 
77  $this->connection->expects($this->any())
78  ->method('getDatabasePlatform')
79  ->will($this->returnValue(new ‪MockPlatform()));
80  }
81 
86  {
87  $this->assertInstanceOf(QueryBuilder::class, $this->connection->createQueryBuilder());
88  }
89 
93  public function ‪quoteIdentifierDataProvider()
94  {
95  return [
96  'SQL star' => [
97  '*',
98  '*',
99  ],
100  'fieldname' => [
101  'aField',
102  '"aField"',
103  ],
104  'whitespace' => [
105  'with blanks',
106  '"with blanks"',
107  ],
108  'double quotes' => [
109  '"double" quotes',
110  '"""double"" quotes"',
111  ],
112  'single quotes' => [
113  "'single'",
114  '"\'single\'"',
115 
116  ],
117  'multiple double quotes' => [
118  '""multiple""',
119  '"""""multiple"""""',
120  ],
121  'multiple single quotes' => [
122  "''multiple''",
123  '"\'\'multiple\'\'"',
124  ],
125  'backticks' => [
126  '`backticks`',
127  '"`backticks`"',
128  ],
129  'slashes' => [
130  '/slashes/',
131  '"/slashes/"',
132  ],
133  'backslashes' => [
134  '\\backslashes\\',
135  '"\\backslashes\\"',
136  ],
137  ];
138  }
139 
146  public function ‪quoteIdentifier(string $input, string $expected)
147  {
148  $this->assertSame($expected, $this->connection->quoteIdentifier($input));
149  }
150 
154  public function ‪quoteIdentifiers()
155  {
156  $input = [
157  'aField',
158  'anotherField',
159  ];
160 
161  $expected = [
162  '"aField"',
163  '"anotherField"',
164  ];
165 
166  $this->assertSame($expected, $this->connection->quoteIdentifiers($input));
167  }
168 
172  public function ‪insertQueriesDataProvider()
173  {
174  return [
175  'single value' => [
176  ['aTestTable', ['aField' => 'aValue']],
177  'INSERT INTO "aTestTable" ("aField") VALUES (?)',
178  ['aValue'],
179  [],
180  ],
181  'multiple values' => [
182  ['aTestTable', ['aField' => 'aValue', 'bField' => 'bValue']],
183  'INSERT INTO "aTestTable" ("aField", "bField") VALUES (?, ?)',
184  ['aValue', 'bValue'],
185  [],
186  ],
187  'with types' => [
188  ['aTestTable', ['aField' => 'aValue', 'bField' => 'bValue'], [‪Connection::PARAM_STR, ‪Connection::PARAM_STR]],
189  'INSERT INTO "aTestTable" ("aField", "bField") VALUES (?, ?)',
190  ['aValue', 'bValue'],
192  ],
193  'with types for field' => [
194  [
195  'aTestTable',
196  ['aField' => 123, 'bField' => 'bValue'],
197  ['aField' => ‪Connection::PARAM_INT, 'bField' => ‪Connection::PARAM_LOB]
198  ],
199  'INSERT INTO "aTestTable" ("aField", "bField") VALUES (?, ?)',
200  [123, 'bValue'],
202  ],
203  ];
204  }
205 
214  public function ‪insertQueries(array ‪$args, string $expectedQuery, array $expectedValues, array $expectedTypes)
215  {
216  $this->connection->expects($this->once())
217  ->method('executeUpdate')
218  ->with($expectedQuery, $expectedValues, $expectedTypes)
219  ->will($this->returnValue(1));
220 
221  $this->connection->insert(...‪$args);
222  }
223 
227  public function ‪bulkInsert()
228  {
229  $this->connection->expects($this->once())
230  ->method('executeUpdate')
231  ->with('INSERT INTO "aTestTable" ("aField") VALUES (?), (?)', ['aValue', 'anotherValue'])
232  ->will($this->returnValue(2));
233 
234  $this->connection->bulkInsert('aTestTable', [['aField' => 'aValue'], ['aField' => 'anotherValue']], ['aField']);
235  }
236 
240  public function ‪updateQueriesDataProvider()
241  {
242  return [
243  'single value' => [
244  ['aTestTable', ['aField' => 'aValue'], ['uid' => 1]],
245  'UPDATE "aTestTable" SET "aField" = ? WHERE "uid" = ?',
246  ['aValue', 1],
247  [],
248  ],
249  'multiple values' => [
250  ['aTestTable', ['aField' => 'aValue', 'bField' => 'bValue'], ['uid' => 1]],
251  'UPDATE "aTestTable" SET "aField" = ?, "bField" = ? WHERE "uid" = ?',
252  ['aValue', 'bValue', 1],
253  [],
254  ],
255  'with types' => [
256  ['aTestTable', ['aField' => 'aValue'], ['uid' => 1], [‪Connection::PARAM_STR]],
257  'UPDATE "aTestTable" SET "aField" = ? WHERE "uid" = ?',
258  ['aValue', 1],
260  ],
261  'with types for field' => [
262  ['aTestTable', ['aField' => 'aValue'], ['uid' => 1], ['aField' => ‪Connection::PARAM_LOB]],
263  'UPDATE "aTestTable" SET "aField" = ? WHERE "uid" = ?',
264  ['aValue', 1],
266  ],
267  ];
268  }
269 
278  public function ‪updateQueries(array ‪$args, string $expectedQuery, array $expectedValues, array $expectedTypes)
279  {
280  $this->connection->expects($this->once())
281  ->method('executeUpdate')
282  ->with($expectedQuery, $expectedValues, $expectedTypes)
283  ->will($this->returnValue(1));
284 
285  $this->connection->update(...‪$args);
286  }
287 
291  public function ‪deleteQueriesDataProvider()
292  {
293  return [
294  'single condition' => [
295  ['aTestTable', ['aField' => 'aValue']],
296  'DELETE FROM "aTestTable" WHERE "aField" = ?',
297  ['aValue'],
298  [],
299  ],
300  'multiple conditions' => [
301  ['aTestTable', ['aField' => 'aValue', 'bField' => 'bValue']],
302  'DELETE FROM "aTestTable" WHERE "aField" = ? AND "bField" = ?',
303  ['aValue', 'bValue'],
304  [],
305  ],
306  'with types' => [
307  ['aTestTable', ['aField' => 'aValue'], [‪Connection::PARAM_STR]],
308  'DELETE FROM "aTestTable" WHERE "aField" = ?',
309  ['aValue'],
311  ],
312  'with types for field' => [
313  ['aTestTable', ['aField' => 'aValue'], ['aField' => ‪Connection::PARAM_STR]],
314  'DELETE FROM "aTestTable" WHERE "aField" = ?',
315  ['aValue'],
317  ],
318  ];
319  }
320 
329  public function ‪deleteQueries(array ‪$args, string $expectedQuery, array $expectedValues, array $expectedTypes)
330  {
331  $this->connection->expects($this->once())
332  ->method('executeUpdate')
333  ->with($expectedQuery, $expectedValues, $expectedTypes)
334  ->will($this->returnValue(1));
335 
336  $this->connection->delete(...‪$args);
337  }
338 
349  public function ‪selectQueriesDataProvider()
350  {
351  return [
352  'all columns' => [
353  [['*'], 'aTable'],
354  'SELECT * FROM "aTable"',
355  [],
356  ],
357  'subset of columns' => [
358  [['aField', 'anotherField'], 'aTable'],
359  'SELECT "aField", "anotherField" FROM "aTable"',
360  [],
361  ],
362  'conditions' => [
363  [['*'], 'aTable', ['aField' => 'aValue']],
364  'SELECT * FROM "aTable" WHERE "aField" = :dcValue1',
365  ['dcValue1' => 'aValue'],
366  ],
367  'grouping' => [
368  [['*'], 'aTable', [], ['aField']],
369  'SELECT * FROM "aTable" GROUP BY "aField"',
370  [],
371  ],
372  'ordering' => [
373  [['*'], 'aTable', [], [], ['aField' => 'ASC']],
374  'SELECT * FROM "aTable" ORDER BY "aField" ASC',
375  [],
376  ],
377  'limit' => [
378  [['*'], 'aTable', [], [], [], 1],
379  'SELECT * FROM "aTable" LIMIT 1',
380  [],
381  ],
382  'offset' => [
383  [['*'], 'aTable', [], [], [], 1, 10],
384  'SELECT * FROM "aTable" LIMIT 1 OFFSET 10',
385  [],
386  ],
387  'everything' => [
388  [
389  ['aField', 'anotherField'],
390  'aTable',
391  ['aField' => 'aValue'],
392  ['anotherField'],
393  ['aField' => 'ASC'],
394  1,
395  10,
396  ],
397  'SELECT "aField", "anotherField" FROM "aTable" WHERE "aField" = :dcValue1 ' .
398  'GROUP BY "anotherField" ORDER BY "aField" ASC LIMIT 1 OFFSET 10',
399  ['dcValue1' => 'aValue'],
400  ],
401  ];
402  }
403 
411  public function ‪selectQueries(array ‪$args, string $expectedQuery, array $expectedParameters)
412  {
413  $resultStatement = $this->createMock(Statement::class);
414 
415  $this->connection->expects($this->once())
416  ->method('executeQuery')
417  ->with($expectedQuery, $expectedParameters)
418  ->will($this->returnValue($resultStatement));
419 
420  $this->connection->select(...‪$args);
421  }
422 
433  public function ‪countQueriesDataProvider()
434  {
435  return [
436  'all columns' => [
437  ['*', 'aTable', []],
438  'SELECT COUNT(*) FROM "aTable"',
439  [],
440  ],
441  'specified columns' => [
442  ['aField', 'aTable', []],
443  'SELECT COUNT("aField") FROM "aTable"',
444  [],
445  ],
446  'conditions' => [
447  ['aTable.aField', 'aTable', ['aField' => 'aValue']],
448  'SELECT COUNT("aTable"."aField") FROM "aTable" WHERE "aField" = :dcValue1',
449  ['dcValue1' => 'aValue'],
450  ],
451  ];
452  }
453 
461  public function ‪countQueries(array ‪$args, string $expectedQuery, array $expectedParameters)
462  {
463  $resultStatement = $this->createMock(Statement::class);
464 
465  $resultStatement->expects($this->once())
466  ->method('fetchColumn')
467  ->with(0)
468  ->will($this->returnValue(0));
469 
470  $this->connection->expects($this->once())
471  ->method('executeQuery')
472  ->with($expectedQuery, $expectedParameters)
473  ->will($this->returnValue($resultStatement));
474 
475  $this->connection->count(...‪$args);
476  }
477 
481  public function ‪truncateQuery()
482  {
483  $this->connection->expects($this->once())
484  ->method('executeUpdate')
485  ->with('TRUNCATE "aTestTable"')
486  ->will($this->returnValue(0));
487 
488  $this->connection->truncate('aTestTable', false);
489  }
490 
495  {
497  $driverProphet = $this->prophesize(\Doctrine\DBAL\Driver\Mysqli\Driver::class);
498  $driverProphet->willImplement(\Doctrine\DBAL\VersionAwarePlatformDriver::class);
499 
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');
505 
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());
512 
513  $this->assertSame('mock 5.7.11', $this->connection->getServerVersion());
514  }
515 }
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest\updateQueries
‪updateQueries(array $args, string $expectedQuery, array $expectedValues, array $expectedTypes)
Definition: ConnectionTest.php:275
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:42
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder
Definition: ExpressionBuilder.php:33
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest\setUp
‪setUp()
Definition: ConnectionTest.php:48
‪$args
‪$args
Definition: checkIntegrityCsvFixtures.php:230
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest\$connection
‪Connection PHPUnit_Framework_MockObject_MockObject $connection
Definition: ConnectionTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest\countQueriesDataProvider
‪array countQueriesDataProvider()
Definition: ConnectionTest.php:430
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest\updateQueriesDataProvider
‪array updateQueriesDataProvider()
Definition: ConnectionTest.php:237
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest\createQueryBuilderReturnsInstanceOfTypo3QueryBuilder
‪createQueryBuilderReturnsInstanceOfTypo3QueryBuilder()
Definition: ConnectionTest.php:82
‪TYPO3\CMS\Core\Database\Connection\PARAM_STR
‪const PARAM_STR
Definition: Connection.php:47
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest\insertQueries
‪insertQueries(array $args, string $expectedQuery, array $expectedValues, array $expectedTypes)
Definition: ConnectionTest.php:211
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest\getServerVersionReportsPlatformVersion
‪getServerVersionReportsPlatformVersion()
Definition: ConnectionTest.php:491
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest\countQueries
‪countQueries(array $args, string $expectedQuery, array $expectedParameters)
Definition: ConnectionTest.php:458
‪TYPO3\CMS\Core\Database\Query\QueryBuilder
Definition: QueryBuilder.php:47
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest\selectQueriesDataProvider
‪array selectQueriesDataProvider()
Definition: ConnectionTest.php:346
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest\quoteIdentifier
‪quoteIdentifier(string $input, string $expected)
Definition: ConnectionTest.php:143
‪TYPO3\CMS\Core\Tests\Unit\Database
Definition: ConnectionPoolTest.php:3
‪TYPO3\CMS\Core\Tests\Unit\Database\Mocks\MockPlatform
Definition: MockPlatform.php:21
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest\deleteQueries
‪deleteQueries(array $args, string $expectedQuery, array $expectedValues, array $expectedTypes)
Definition: ConnectionTest.php:326
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest
Definition: ConnectionTest.php:32
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest\selectQueries
‪selectQueries(array $args, string $expectedQuery, array $expectedParameters)
Definition: ConnectionTest.php:408
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest\bulkInsert
‪bulkInsert()
Definition: ConnectionTest.php:224
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest\truncateQuery
‪truncateQuery()
Definition: ConnectionTest.php:478
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest\deleteQueriesDataProvider
‪array deleteQueriesDataProvider()
Definition: ConnectionTest.php:288
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest\$testTable
‪string $testTable
Definition: ConnectionTest.php:43
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest\quoteIdentifiers
‪quoteIdentifiers()
Definition: ConnectionTest.php:151
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:31
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest\insertQueriesDataProvider
‪array insertQueriesDataProvider()
Definition: ConnectionTest.php:169
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest\$platform
‪Doctrine DBAL Platforms AbstractPlatform $platform
Definition: ConnectionTest.php:39
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Tests\Unit\Database\ConnectionTest\quoteIdentifierDataProvider
‪array quoteIdentifierDataProvider()
Definition: ConnectionTest.php:90
‪TYPO3\CMS\Core\Database\Connection\PARAM_LOB
‪const PARAM_LOB
Definition: Connection.php:52