[TASK] Shorten the extend class in all unit tests
[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\TestingFramework\Core\Unit\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 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
57 /**
58 * @test
59 */
60 public function getSQLWithoutSpecifiedValuesThrowsException()
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
70 /**
71 * @test
72 */
73 public function insertWithoutColumnAndTypeSpecification()
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
95 /**
96 * @test
97 */
98 public function singleInsertWithoutColumnSpecification()
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'],
112 ['named' => Connection::PARAM_BOOL, null, Connection::PARAM_INT]
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
120 /**
121 * @test
122 */
123 public function multiInsertWithoutColumnSpecification()
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'],
143 ['named' => Connection::PARAM_INT, null, Connection::PARAM_BOOL]
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,
151 Connection::PARAM_BOOL,
152 Connection::PARAM_INT,
153 null,
154 Connection::PARAM_BOOL,
155 null,
156 Connection::PARAM_BOOL,
157 Connection::PARAM_INT
158 ],
159 $query->getParameterTypes()
160 );
161 }
162
163 /**
164 * @test
165 */
166 public function singleInsertWithColumnSpecificationAndPositionalTypeValues()
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
185 /**
186 * @test
187 */
188 public function singleInsertWithColumnSpecificationAndNamedTypeValues()
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
207 /**
208 * @test
209 */
210 public function singleInsertWithColumnSpecificationAndMixedTypeValues()
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
229 /**
230 * @test
231 */
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'],
255 ['bar' => Connection::PARAM_INT, 'baz' => Connection::PARAM_BOOL]
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 [
265 Connection::PARAM_INT,
266 Connection::PARAM_BOOL,
267 Connection::PARAM_INT,
268 Connection::PARAM_BOOL,
269 null,
270 null,
271 Connection::PARAM_INT,
272 Connection::PARAM_BOOL,
273 ],
274 $query->getParameterTypes()
275 );
276 }
277
278 /**
279 * @test
280 */
281 public function emptyInsertWithColumnSpecificationThrowsException()
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
290 /**
291 * @test
292 */
293 public function insertWithColumnSpecificationAndMultipleValuesForColumnThrowsException()
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
302 /**
303 * @test
304 */
305 public function insertWithColumnSpecificationAndMultipleTypesForColumnThrowsException()
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'],
313 [Connection::PARAM_INT, Connection::PARAM_INT, 'baz' => Connection::PARAM_STR]
314 );
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 }