[TASK] Add more fixers for php-cs-fixer
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Database / Query / BulkInsertTest.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Database\Query;
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 TYPO3\CMS\Core\Database\Connection;
19 use TYPO3\CMS\Core\Database\Query\BulkInsertQuery;
20 use TYPO3\CMS\Core\Tests\Unit\Database\Mocks\MockPlatform;
21 use TYPO3\CMS\Core\Tests\UnitTestCase;
22
23 class BulkInsertTest extends UnitTestCase
24 {
25 /**
26 * @var Connection
27 */
28 protected $connection;
29
30 /**
31 * @var \Doctrine\DBAL\Platforms\AbstractPlatform
32 */
33 protected $platform;
34
35 /**
36 * @var string
37 */
38 protected $testTable = 'testTable';
39
40 /**
41 * Create a new database connection mock object for every test.
42 *
43 * @return void
44 */
45 protected function setUp()
46 {
47 parent::setUp();
48
49 $this->connection = $this->createMock(Connection::class);
50
51 $this->connection->expects($this->any())
52 ->method('quoteIdentifier')
53 ->will($this->returnArgument(0));
54 $this->connection->expects($this->any())
55 ->method('getDatabasePlatform')
56 ->will($this->returnValue(new MockPlatform()));
57 }
58
59 /**
60 * @test
61 */
62 public function getSQLWithoutSpecifiedValuesThrowsException()
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
72 /**
73 * @test
74 */
75 public function insertWithoutColumnAndTypeSpecification()
76 {
77 $query = new BulkInsertQuery($this->connection, $this->testTable);
78
79 $query->addValues([]);
80
81 $this->assertSame("INSERT INTO {$this->testTable} VALUES ()", (string)$query);
82 $this->assertSame([], $query->getParameters());
83 $this->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 $this->assertSame("INSERT INTO {$this->testTable} VALUES ()", (string)$query);
93 $this->assertSame([], $query->getParameters());
94 $this->assertSame([], $query->getParameterTypes());
95 }
96
97 /**
98 * @test
99 */
100 public function singleInsertWithoutColumnSpecification()
101 {
102 $query = new BulkInsertQuery($this->connection, $this->testTable);
103
104 $query->addValues(['bar', 'baz', 'named' => 'bloo']);
105
106 $this->assertSame("INSERT INTO {$this->testTable} VALUES (?, ?, ?)", (string)$query);
107 $this->assertSame(['bar', 'baz', 'bloo'], $query->getParameters());
108 $this->assertSame([null, null, null], $query->getParameterTypes());
109
110 $query = new BulkInsertQuery($this->connection, $this->testTable);
111
112 $query->addValues(
113 ['bar', 'baz', 'named' => 'bloo'],
114 ['named' => Connection::PARAM_BOOL, null, Connection::PARAM_INT]
115 );
116
117 $this->assertSame("INSERT INTO {$this->testTable} VALUES (?, ?, ?)", (string)$query);
118 $this->assertSame(['bar', 'baz', 'bloo'], $query->getParameters());
119 $this->assertSame([null, Connection::PARAM_INT, Connection::PARAM_BOOL], $query->getParameterTypes());
120 }
121
122 /**
123 * @test
124 */
125 public function multiInsertWithoutColumnSpecification()
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 $this->assertSame("INSERT INTO {$this->testTable} VALUES (), (?, ?), (?, ?, ?), (?, ?, ?)", (string)$query);
135 $this->assertSame(['bar', 'baz', 'bar', 'baz', 'bloo', 'bar', 'baz', 'bloo'], $query->getParameters());
136 $this->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'],
145 ['named' => Connection::PARAM_INT, null, Connection::PARAM_BOOL]
146 );
147
148 $this->assertSame("INSERT INTO {$this->testTable} VALUES (), (?, ?), (?, ?, ?), (?, ?, ?)", (string)$query);
149 $this->assertSame(['bar', 'baz', 'bar', 'baz', 'bloo', 'bar', 'baz', 'bloo'], $query->getParameters());
150 $this->assertSame(
151 [
152 null,
153 Connection::PARAM_BOOL,
154 Connection::PARAM_INT,
155 null,
156 Connection::PARAM_BOOL,
157 null,
158 Connection::PARAM_BOOL,
159 Connection::PARAM_INT
160 ],
161 $query->getParameterTypes()
162 );
163 }
164
165 /**
166 * @test
167 */
168 public function singleInsertWithColumnSpecificationAndPositionalTypeValues()
169 {
170 $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
171
172 $query->addValues(['bar', 'baz']);
173
174 $this->assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
175 $this->assertSame(['bar', 'baz'], $query->getParameters());
176 $this->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 $this->assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
183 $this->assertSame(['bar', 'baz'], $query->getParameters());
184 $this->assertSame([null, Connection::PARAM_BOOL], $query->getParameterTypes());
185 }
186
187 /**
188 * @test
189 */
190 public function singleInsertWithColumnSpecificationAndNamedTypeValues()
191 {
192 $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
193
194 $query->addValues(['baz' => 'baz', 'bar' => 'bar']);
195
196 $this->assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
197 $this->assertSame(['bar', 'baz'], $query->getParameters());
198 $this->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 $this->assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
205 $this->assertSame(['bar', 'baz'], $query->getParameters());
206 $this->assertSame([null, Connection::PARAM_INT], $query->getParameterTypes());
207 }
208
209 /**
210 * @test
211 */
212 public function singleInsertWithColumnSpecificationAndMixedTypeValues()
213 {
214 $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
215
216 $query->addValues([1 => 'baz', 'bar' => 'bar']);
217
218 $this->assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
219 $this->assertSame(['bar', 'baz'], $query->getParameters());
220 $this->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 $this->assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
227 $this->assertSame(['bar', 'baz'], $query->getParameters());
228 $this->assertSame([Connection::PARAM_INT, Connection::PARAM_BOOL], $query->getParameterTypes());
229 }
230
231 /**
232 * @test
233 */
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 $this->assertSame(
244 "INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?), (?, ?), (?, ?), (?, ?)",
245 (string)$query
246 );
247 $this->assertSame(['bar', 'baz', 'bar', 'baz', 'bar', 'baz', 'bar', 'baz'], $query->getParameters());
248 $this->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'],
257 ['bar' => Connection::PARAM_INT, 'baz' => Connection::PARAM_BOOL]
258 );
259
260 $this->assertSame(
261 "INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?), (?, ?), (?, ?), (?, ?)",
262 (string)$query
263 );
264 $this->assertSame(['bar', 'baz', 'bar', 'baz', 'bar', 'baz', 'bar', 'baz'], $query->getParameters());
265 $this->assertSame(
266 [
267 Connection::PARAM_INT,
268 Connection::PARAM_BOOL,
269 Connection::PARAM_INT,
270 Connection::PARAM_BOOL,
271 null,
272 null,
273 Connection::PARAM_INT,
274 Connection::PARAM_BOOL,
275 ],
276 $query->getParameterTypes()
277 );
278 }
279
280 /**
281 * @test
282 */
283 public function emptyInsertWithColumnSpecificationThrowsException()
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
292 /**
293 * @test
294 */
295 public function insertWithColumnSpecificationAndMultipleValuesForColumnThrowsException()
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
304 /**
305 * @test
306 */
307 public function insertWithColumnSpecificationAndMultipleTypesForColumnThrowsException()
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(['bar', 'baz'],
314 [Connection::PARAM_INT, Connection::PARAM_INT, 'baz' => Connection::PARAM_STR]);
315 }
316
317 /**
318 * @test
319 */
320 public function executeWithMaxInsertRowsPerStatementExceededThrowsException()
321 {
322 $this->expectException(\LogicException::class);
323 $this->expectExceptionMessage('You can only insert 10 rows in a single INSERT statement with platform "mock".');
324
325 /** @var \PHPUnit_Framework_MockObject_MockObject|BulkInsertQuery $subject */
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 }