‪TYPO3CMS  10.4
BulkInsertTest.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24 
25 class ‪BulkInsertTest extends UnitTestCase
26 {
30  protected ‪$connection;
31 
35  protected ‪$platform;
36 
40  protected ‪$testTable = 'testTable';
41 
45  protected function ‪setUp(): void
46  {
47  parent::setUp();
48 
49  $this->connection = $this->createMock(Connection::class);
50 
51  $this->connection->expects(self::any())
52  ->method('quoteIdentifier')
53  ->willReturnArgument(0);
54  $this->connection->expects(self::any())
55  ->method('getDatabasePlatform')
56  ->willReturn(new ‪MockPlatform());
57  }
58 
63  {
64  $this->expectException(\LogicException::class);
65  $this->expectExceptionMessage('You need to add at least one set of values before generating the SQL.');
66 
67  $query = new ‪BulkInsertQuery($this->connection, $this->testTable);
68 
69  $query->getSQL();
70  }
71 
76  {
77  $query = new ‪BulkInsertQuery($this->connection, $this->testTable);
78 
79  $query->addValues([]);
80 
81  self::assertSame("INSERT INTO {$this->testTable} VALUES ()", (string)$query);
82  self::assertSame([], $query->getParameters());
83  self::assertSame([], $query->getParameterTypes());
84  }
85 
86  public function ‪insertWithoutColumnSpecification()
87  {
88  $query = new ‪BulkInsertQuery($this->connection, $this->testTable);
89 
90  $query->addValues([], [‪Connection::PARAM_BOOL]);
91 
92  self::assertSame("INSERT INTO {$this->testTable} VALUES ()", (string)$query);
93  self::assertSame([], $query->getParameters());
94  self::assertSame([], $query->getParameterTypes());
95  }
96 
101  {
102  $query = new BulkInsertQuery($this->connection, $this->testTable);
103 
104  $query->addValues(['bar', 'baz', 'named' => 'bloo']);
105 
106  self::assertSame("INSERT INTO {$this->testTable} VALUES (?, ?, ?)", (string)$query);
107  self::assertSame(['bar', 'baz', 'bloo'], $query->getParameters());
108  self::assertSame([null, null, null], $query->getParameterTypes());
109 
110  $query = new BulkInsertQuery($this->connection, $this->testTable);
111 
112  $query->addValues(
113  ['bar', 'baz', 'named' => 'bloo'],
115  );
116 
117  self::assertSame("INSERT INTO {$this->testTable} VALUES (?, ?, ?)", (string)$query);
118  self::assertSame(['bar', 'baz', 'bloo'], $query->getParameters());
119  self::assertSame([null, ‪Connection::PARAM_INT, ‪Connection::PARAM_BOOL], $query->getParameterTypes());
120  }
121 
126  {
127  $query = new BulkInsertQuery($this->connection, $this->testTable);
128 
129  $query->addValues([]);
130  $query->addValues(['bar', 'baz']);
131  $query->addValues(['bar', 'baz', 'bloo']);
132  $query->addValues(['bar', 'baz', 'named' => 'bloo']);
133 
134  self::assertSame("INSERT INTO {$this->testTable} VALUES (), (?, ?), (?, ?, ?), (?, ?, ?)", (string)$query);
135  self::assertSame(['bar', 'baz', 'bar', 'baz', 'bloo', 'bar', 'baz', 'bloo'], $query->getParameters());
136  self::assertSame([null, null, null, null, null, null, null, null], $query->getParameterTypes());
137 
138  $query = new BulkInsertQuery($this->connection, $this->testTable);
139 
140  $query->addValues([], [‪Connection::PARAM_INT]);
141  $query->addValues(['bar', 'baz'], [1 => ‪Connection::PARAM_BOOL]);
142  $query->addValues(['bar', 'baz', 'bloo'], [‪Connection::PARAM_INT, null, ‪Connection::PARAM_BOOL]);
143  $query->addValues(
144  ['bar', 'baz', 'named' => 'bloo'],
146  );
147 
148  self::assertSame("INSERT INTO {$this->testTable} VALUES (), (?, ?), (?, ?, ?), (?, ?, ?)", (string)$query);
149  self::assertSame(['bar', 'baz', 'bar', 'baz', 'bloo', 'bar', 'baz', 'bloo'], $query->getParameters());
150  self::assertSame(
151  [
152  null,
155  null,
157  null,
160  ],
161  $query->getParameterTypes()
162  );
163  }
164 
169  {
170  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
171 
172  $query->addValues(['bar', 'baz']);
173 
174  self::assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
175  self::assertSame(['bar', 'baz'], $query->getParameters());
176  self::assertSame([null, null], $query->getParameterTypes());
177 
178  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
179 
180  $query->addValues(['bar', 'baz'], [1 => ‪Connection::PARAM_BOOL]);
181 
182  self::assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
183  self::assertSame(['bar', 'baz'], $query->getParameters());
184  self::assertSame([null, ‪Connection::PARAM_BOOL], $query->getParameterTypes());
185  }
186 
191  {
192  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
193 
194  $query->addValues(['baz' => 'baz', 'bar' => 'bar']);
195 
196  self::assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
197  self::assertSame(['bar', 'baz'], $query->getParameters());
198  self::assertSame([null, null], $query->getParameterTypes());
199 
200  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
201 
202  $query->addValues(['baz' => 'baz', 'bar' => 'bar'], [null, ‪Connection::PARAM_INT]);
203 
204  self::assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
205  self::assertSame(['bar', 'baz'], $query->getParameters());
206  self::assertSame([null, ‪Connection::PARAM_INT], $query->getParameterTypes());
207  }
208 
213  {
214  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
215 
216  $query->addValues([1 => 'baz', 'bar' => 'bar']);
217 
218  self::assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
219  self::assertSame(['bar', 'baz'], $query->getParameters());
220  self::assertSame([null, null], $query->getParameterTypes());
221 
222  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
223 
224  $query->addValues([1 => 'baz', 'bar' => 'bar'], [‪Connection::PARAM_INT, ‪Connection::PARAM_BOOL]);
225 
226  self::assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
227  self::assertSame(['bar', 'baz'], $query->getParameters());
228  self::assertSame([‪Connection::PARAM_INT, ‪Connection::PARAM_BOOL], $query->getParameterTypes());
229  }
230 
234  public function ‪multiInsertWithColumnSpecification()
235  {
236  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
237 
238  $query->addValues(['bar', 'baz']);
239  $query->addValues([1 => 'baz', 'bar' => 'bar']);
240  $query->addValues(['bar', 'baz' => 'baz']);
241  $query->addValues(['bar' => 'bar', 'baz' => 'baz']);
242 
243  self::assertSame(
244  "INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?), (?, ?), (?, ?), (?, ?)",
245  (string)$query
246  );
247  self::assertSame(['bar', 'baz', 'bar', 'baz', 'bar', 'baz', 'bar', 'baz'], $query->getParameters());
248  self::assertSame([null, null, null, null, null, null, null, null], $query->getParameterTypes());
249 
250  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
251 
252  $query->addValues(['bar', 'baz'], ['baz' => ‪Connection::PARAM_BOOL, 'bar' => ‪Connection::PARAM_INT]);
253  $query->addValues([1 => 'baz', 'bar' => 'bar'], [1 => ‪Connection::PARAM_BOOL, 'bar' => ‪Connection::PARAM_INT]);
254  $query->addValues(['bar', 'baz' => 'baz'], [null, null]);
255  $query->addValues(
256  ['bar' => 'bar', 'baz' => 'baz'],
258  );
259 
260  self::assertSame(
261  "INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?), (?, ?), (?, ?), (?, ?)",
262  (string)$query
263  );
264  self::assertSame(['bar', 'baz', 'bar', 'baz', 'bar', 'baz', 'bar', 'baz'], $query->getParameters());
265  self::assertSame(
266  [
271  null,
272  null,
275  ],
276  $query->getParameterTypes()
277  );
278  }
279 
284  {
285  $this->expectException(\InvalidArgumentException::class);
286  $this->expectExceptionMessage('No value specified for column bar (index 0).');
287 
288  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
289  $query->addValues([]);
290  }
291 
296  {
297  $this->expectException(\InvalidArgumentException::class);
298  $this->expectExceptionMessage('Multiple values specified for column baz (index 1).');
299 
300  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
301  $query->addValues(['bar', 'baz', 'baz' => 666]);
302  }
303 
308  {
309  $this->expectException(\InvalidArgumentException::class);
310  $this->expectExceptionMessage('Multiple types specified for column baz (index 1).');
311 
312  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
313  $query->addValues(
314  ['bar', 'baz'],
316  );
317  }
318 
323  {
324  $this->expectException(\LogicException::class);
325  $this->expectExceptionMessage('You can only insert 10 rows in a single INSERT statement with platform "mock".');
326 
328  $subject = $this->getAccessibleMock(
329  BulkInsertQuery::class,
330  ['getInsertMaxRows'],
331  [$this->connection, $this->testTable],
332  ''
333  );
334 
335  $subject->expects(self::any())
336  ->method('getInsertMaxRows')
337  ->willReturn(10);
338 
339  for ($i = 0; $i <= 10; $i++) {
340  $subject->addValues([]);
341  }
342 
343  $subject->execute();
344  }
345 }
‪TYPO3\CMS\Core\Database\Connection\PARAM_BOOL
‪const PARAM_BOOL
Definition: Connection.php:67
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:47
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\insertWithoutColumnAndTypeSpecification
‪insertWithoutColumnAndTypeSpecification()
Definition: BulkInsertTest.php:72
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\singleInsertWithoutColumnSpecification
‪singleInsertWithoutColumnSpecification()
Definition: BulkInsertTest.php:97
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\$connection
‪Connection $connection
Definition: BulkInsertTest.php:29
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\executeWithMaxInsertRowsPerStatementExceededThrowsException
‪executeWithMaxInsertRowsPerStatementExceededThrowsException()
Definition: BulkInsertTest.php:319
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\$testTable
‪string $testTable
Definition: BulkInsertTest.php:37
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest
Definition: BulkInsertTest.php:26
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\insertWithColumnSpecificationAndMultipleTypesForColumnThrowsException
‪insertWithColumnSpecificationAndMultipleTypesForColumnThrowsException()
Definition: BulkInsertTest.php:304
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\multiInsertWithoutColumnSpecification
‪multiInsertWithoutColumnSpecification()
Definition: BulkInsertTest.php:122
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\singleInsertWithColumnSpecificationAndMixedTypeValues
‪singleInsertWithColumnSpecificationAndMixedTypeValues()
Definition: BulkInsertTest.php:209
‪TYPO3\CMS\Core\Database\Connection\PARAM_STR
‪const PARAM_STR
Definition: Connection.php:52
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\singleInsertWithColumnSpecificationAndNamedTypeValues
‪singleInsertWithColumnSpecificationAndNamedTypeValues()
Definition: BulkInsertTest.php:187
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\setUp
‪setUp()
Definition: BulkInsertTest.php:42
‪TYPO3\CMS\Core\Tests\Unit\Database\Mocks\MockPlatform
Definition: MockPlatform.php:22
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\singleInsertWithColumnSpecificationAndPositionalTypeValues
‪singleInsertWithColumnSpecificationAndPositionalTypeValues()
Definition: BulkInsertTest.php:165
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\multiInsertWithColumnSpecification
‪multiInsertWithColumnSpecification()
Definition: BulkInsertTest.php:231
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:36
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\$platform
‪Doctrine DBAL Platforms AbstractPlatform $platform
Definition: BulkInsertTest.php:33
‪TYPO3\CMS\Core\Database\Query\BulkInsertQuery
Definition: BulkInsertQuery.php:35
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\insertWithoutColumnSpecification
‪insertWithoutColumnSpecification()
Definition: BulkInsertTest.php:83
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\emptyInsertWithColumnSpecificationThrowsException
‪emptyInsertWithColumnSpecificationThrowsException()
Definition: BulkInsertTest.php:280
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\getSQLWithoutSpecifiedValuesThrowsException
‪getSQLWithoutSpecifiedValuesThrowsException()
Definition: BulkInsertTest.php:59
‪TYPO3\CMS\Core\Tests\Unit\Database\Query
Definition: BulkInsertTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\insertWithColumnSpecificationAndMultipleValuesForColumnThrowsException
‪insertWithColumnSpecificationAndMultipleValuesForColumnThrowsException()
Definition: BulkInsertTest.php:292