‪TYPO3CMS  9.5
BulkInsertTest.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 
21 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
22 
23 class ‪BulkInsertTest extends UnitTestCase
24 {
28  protected ‪$connection;
29 
33  protected ‪$platform;
34 
38  protected ‪$testTable = 'testTable';
39 
43  protected function ‪setUp()
44  {
45  parent::setUp();
46 
47  $this->connection = $this->createMock(Connection::class);
48 
49  $this->connection->expects($this->any())
50  ->method('quoteIdentifier')
51  ->will($this->returnArgument(0));
52  $this->connection->expects($this->any())
53  ->method('getDatabasePlatform')
54  ->will($this->returnValue(new ‪MockPlatform()));
55  }
56 
61  {
62  $this->expectException(\LogicException::class);
63  $this->expectExceptionMessage('You need to add at least one set of values before generating the SQL.');
64 
65  $query = new ‪BulkInsertQuery($this->connection, $this->testTable);
66 
67  $query->getSQL();
68  }
69 
74  {
75  $query = new ‪BulkInsertQuery($this->connection, $this->testTable);
76 
77  $query->addValues([]);
78 
79  $this->assertSame("INSERT INTO {$this->testTable} VALUES ()", (string)$query);
80  $this->assertSame([], $query->getParameters());
81  $this->assertSame([], $query->getParameterTypes());
82  }
83 
84  public function ‪insertWithoutColumnSpecification()
85  {
86  $query = new ‪BulkInsertQuery($this->connection, $this->testTable);
87 
88  $query->addValues([], [‪Connection::PARAM_BOOL]);
89 
90  $this->assertSame("INSERT INTO {$this->testTable} VALUES ()", (string)$query);
91  $this->assertSame([], $query->getParameters());
92  $this->assertSame([], $query->getParameterTypes());
93  }
94 
99  {
100  $query = new BulkInsertQuery($this->connection, $this->testTable);
101 
102  $query->addValues(['bar', 'baz', 'named' => 'bloo']);
103 
104  $this->assertSame("INSERT INTO {$this->testTable} VALUES (?, ?, ?)", (string)$query);
105  $this->assertSame(['bar', 'baz', 'bloo'], $query->getParameters());
106  $this->assertSame([null, null, null], $query->getParameterTypes());
107 
108  $query = new BulkInsertQuery($this->connection, $this->testTable);
109 
110  $query->addValues(
111  ['bar', 'baz', 'named' => 'bloo'],
113  );
114 
115  $this->assertSame("INSERT INTO {$this->testTable} VALUES (?, ?, ?)", (string)$query);
116  $this->assertSame(['bar', 'baz', 'bloo'], $query->getParameters());
117  $this->assertSame([null, ‪Connection::PARAM_INT, ‪Connection::PARAM_BOOL], $query->getParameterTypes());
118  }
119 
124  {
125  $query = new BulkInsertQuery($this->connection, $this->testTable);
126 
127  $query->addValues([]);
128  $query->addValues(['bar', 'baz']);
129  $query->addValues(['bar', 'baz', 'bloo']);
130  $query->addValues(['bar', 'baz', 'named' => 'bloo']);
131 
132  $this->assertSame("INSERT INTO {$this->testTable} VALUES (), (?, ?), (?, ?, ?), (?, ?, ?)", (string)$query);
133  $this->assertSame(['bar', 'baz', 'bar', 'baz', 'bloo', 'bar', 'baz', 'bloo'], $query->getParameters());
134  $this->assertSame([null, null, null, null, null, null, null, null], $query->getParameterTypes());
135 
136  $query = new BulkInsertQuery($this->connection, $this->testTable);
137 
138  $query->addValues([], [‪Connection::PARAM_INT]);
139  $query->addValues(['bar', 'baz'], [1 => ‪Connection::PARAM_BOOL]);
140  $query->addValues(['bar', 'baz', 'bloo'], [‪Connection::PARAM_INT, null, ‪Connection::PARAM_BOOL]);
141  $query->addValues(
142  ['bar', 'baz', 'named' => 'bloo'],
144  );
145 
146  $this->assertSame("INSERT INTO {$this->testTable} VALUES (), (?, ?), (?, ?, ?), (?, ?, ?)", (string)$query);
147  $this->assertSame(['bar', 'baz', 'bar', 'baz', 'bloo', 'bar', 'baz', 'bloo'], $query->getParameters());
148  $this->assertSame(
149  [
150  null,
153  null,
155  null,
158  ],
159  $query->getParameterTypes()
160  );
161  }
162 
167  {
168  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
169 
170  $query->addValues(['bar', 'baz']);
171 
172  $this->assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
173  $this->assertSame(['bar', 'baz'], $query->getParameters());
174  $this->assertSame([null, null], $query->getParameterTypes());
175 
176  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
177 
178  $query->addValues(['bar', 'baz'], [1 => ‪Connection::PARAM_BOOL]);
179 
180  $this->assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
181  $this->assertSame(['bar', 'baz'], $query->getParameters());
182  $this->assertSame([null, ‪Connection::PARAM_BOOL], $query->getParameterTypes());
183  }
184 
189  {
190  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
191 
192  $query->addValues(['baz' => 'baz', 'bar' => 'bar']);
193 
194  $this->assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
195  $this->assertSame(['bar', 'baz'], $query->getParameters());
196  $this->assertSame([null, null], $query->getParameterTypes());
197 
198  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
199 
200  $query->addValues(['baz' => 'baz', 'bar' => 'bar'], [null, ‪Connection::PARAM_INT]);
201 
202  $this->assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
203  $this->assertSame(['bar', 'baz'], $query->getParameters());
204  $this->assertSame([null, ‪Connection::PARAM_INT], $query->getParameterTypes());
205  }
206 
211  {
212  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
213 
214  $query->addValues([1 => 'baz', 'bar' => 'bar']);
215 
216  $this->assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
217  $this->assertSame(['bar', 'baz'], $query->getParameters());
218  $this->assertSame([null, null], $query->getParameterTypes());
219 
220  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
221 
222  $query->addValues([1 => 'baz', 'bar' => 'bar'], [‪Connection::PARAM_INT, ‪Connection::PARAM_BOOL]);
223 
224  $this->assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
225  $this->assertSame(['bar', 'baz'], $query->getParameters());
226  $this->assertSame([‪Connection::PARAM_INT, ‪Connection::PARAM_BOOL], $query->getParameterTypes());
227  }
228 
232  public function ‪multiInsertWithColumnSpecification()
233  {
234  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
235 
236  $query->addValues(['bar', 'baz']);
237  $query->addValues([1 => 'baz', 'bar' => 'bar']);
238  $query->addValues(['bar', 'baz' => 'baz']);
239  $query->addValues(['bar' => 'bar', 'baz' => 'baz']);
240 
241  $this->assertSame(
242  "INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?), (?, ?), (?, ?), (?, ?)",
243  (string)$query
244  );
245  $this->assertSame(['bar', 'baz', 'bar', 'baz', 'bar', 'baz', 'bar', 'baz'], $query->getParameters());
246  $this->assertSame([null, null, null, null, null, null, null, null], $query->getParameterTypes());
247 
248  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
249 
250  $query->addValues(['bar', 'baz'], ['baz' => ‪Connection::PARAM_BOOL, 'bar' => ‪Connection::PARAM_INT]);
251  $query->addValues([1 => 'baz', 'bar' => 'bar'], [1 => ‪Connection::PARAM_BOOL, 'bar' => ‪Connection::PARAM_INT]);
252  $query->addValues(['bar', 'baz' => 'baz'], [null, null]);
253  $query->addValues(
254  ['bar' => 'bar', 'baz' => 'baz'],
256  );
257 
258  $this->assertSame(
259  "INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?), (?, ?), (?, ?), (?, ?)",
260  (string)$query
261  );
262  $this->assertSame(['bar', 'baz', 'bar', 'baz', 'bar', 'baz', 'bar', 'baz'], $query->getParameters());
263  $this->assertSame(
264  [
269  null,
270  null,
273  ],
274  $query->getParameterTypes()
275  );
276  }
277 
282  {
283  $this->expectException(\InvalidArgumentException::class);
284  $this->expectExceptionMessage('No value specified for column bar (index 0).');
285 
286  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
287  $query->addValues([]);
288  }
289 
294  {
295  $this->expectException(\InvalidArgumentException::class);
296  $this->expectExceptionMessage('Multiple values specified for column baz (index 1).');
297 
298  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
299  $query->addValues(['bar', 'baz', 'baz' => 666]);
300  }
301 
306  {
307  $this->expectException(\InvalidArgumentException::class);
308  $this->expectExceptionMessage('Multiple types specified for column baz (index 1).');
309 
310  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
311  $query->addValues(
312  ['bar', 'baz'],
314  );
315  }
316 
321  {
322  $this->expectException(\LogicException::class);
323  $this->expectExceptionMessage('You can only insert 10 rows in a single INSERT statement with platform "mock".');
324 
326  $subject = $this->getAccessibleMock(
327  BulkInsertQuery::class,
328  ['getInsertMaxRows'],
329  [$this->connection, $this->testTable],
330  ''
331  );
332 
333  $subject->expects($this->any())
334  ->method('getInsertMaxRows')
335  ->will($this->returnValue(10));
336 
337  for ($i = 0; $i <= 10; $i++) {
338  $subject->addValues([]);
339  }
340 
341  $subject->execute();
342  }
343 }
‪TYPO3\CMS\Core\Database\Connection\PARAM_BOOL
‪const PARAM_BOOL
Definition: Connection.php:62
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:42
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\insertWithoutColumnAndTypeSpecification
‪insertWithoutColumnAndTypeSpecification()
Definition: BulkInsertTest.php:70
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\singleInsertWithoutColumnSpecification
‪singleInsertWithoutColumnSpecification()
Definition: BulkInsertTest.php:95
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\$connection
‪Connection $connection
Definition: BulkInsertTest.php:27
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\executeWithMaxInsertRowsPerStatementExceededThrowsException
‪executeWithMaxInsertRowsPerStatementExceededThrowsException()
Definition: BulkInsertTest.php:317
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\$testTable
‪string $testTable
Definition: BulkInsertTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest
Definition: BulkInsertTest.php:24
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\insertWithColumnSpecificationAndMultipleTypesForColumnThrowsException
‪insertWithColumnSpecificationAndMultipleTypesForColumnThrowsException()
Definition: BulkInsertTest.php:302
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\multiInsertWithoutColumnSpecification
‪multiInsertWithoutColumnSpecification()
Definition: BulkInsertTest.php:120
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\singleInsertWithColumnSpecificationAndMixedTypeValues
‪singleInsertWithColumnSpecificationAndMixedTypeValues()
Definition: BulkInsertTest.php:207
‪TYPO3\CMS\Core\Database\Connection\PARAM_STR
‪const PARAM_STR
Definition: Connection.php:47
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\singleInsertWithColumnSpecificationAndNamedTypeValues
‪singleInsertWithColumnSpecificationAndNamedTypeValues()
Definition: BulkInsertTest.php:185
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\setUp
‪setUp()
Definition: BulkInsertTest.php:40
‪TYPO3\CMS\Core\Tests\Unit\Database\Mocks\MockPlatform
Definition: MockPlatform.php:21
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\singleInsertWithColumnSpecificationAndPositionalTypeValues
‪singleInsertWithColumnSpecificationAndPositionalTypeValues()
Definition: BulkInsertTest.php:163
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\multiInsertWithColumnSpecification
‪multiInsertWithColumnSpecification()
Definition: BulkInsertTest.php:229
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:31
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\$platform
‪Doctrine DBAL Platforms AbstractPlatform $platform
Definition: BulkInsertTest.php:31
‪TYPO3\CMS\Core\Database\Query\BulkInsertQuery
Definition: BulkInsertQuery.php:33
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\insertWithoutColumnSpecification
‪insertWithoutColumnSpecification()
Definition: BulkInsertTest.php:81
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\emptyInsertWithColumnSpecificationThrowsException
‪emptyInsertWithColumnSpecificationThrowsException()
Definition: BulkInsertTest.php:278
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\getSQLWithoutSpecifiedValuesThrowsException
‪getSQLWithoutSpecifiedValuesThrowsException()
Definition: BulkInsertTest.php:57
‪TYPO3\CMS\Core\Tests\Unit\Database\Query
Definition: BulkInsertTest.php:3
‪TYPO3\CMS\Core\Tests\Unit\Database\Query\BulkInsertTest\insertWithColumnSpecificationAndMultipleValuesForColumnThrowsException
‪insertWithColumnSpecificationAndMultipleValuesForColumnThrowsException()
Definition: BulkInsertTest.php:290