TYPO3 CMS  TYPO3_8-7
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 
22 class BulkInsertTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
23 {
27  protected $connection;
28 
32  protected $platform;
33 
37  protected $testTable = 'testTable';
38 
42  protected function setUp()
43  {
44  parent::setUp();
45 
46  $this->connection = $this->createMock(Connection::class);
47 
48  $this->connection->expects($this->any())
49  ->method('quoteIdentifier')
50  ->will($this->returnArgument(0));
51  $this->connection->expects($this->any())
52  ->method('getDatabasePlatform')
53  ->will($this->returnValue(new MockPlatform()));
54  }
55 
60  {
61  $this->expectException(\LogicException::class);
62  $this->expectExceptionMessage('You need to add at least one set of values before generating the SQL.');
63 
64  $query = new BulkInsertQuery($this->connection, $this->testTable);
65 
66  $query->getSQL();
67  }
68 
73  {
74  $query = new BulkInsertQuery($this->connection, $this->testTable);
75 
76  $query->addValues([]);
77 
78  $this->assertSame("INSERT INTO {$this->testTable} VALUES ()", (string)$query);
79  $this->assertSame([], $query->getParameters());
80  $this->assertSame([], $query->getParameterTypes());
81  }
82 
84  {
85  $query = new BulkInsertQuery($this->connection, $this->testTable);
86 
87  $query->addValues([], [Connection::PARAM_BOOL]);
88 
89  $this->assertSame("INSERT INTO {$this->testTable} VALUES ()", (string)$query);
90  $this->assertSame([], $query->getParameters());
91  $this->assertSame([], $query->getParameterTypes());
92  }
93 
98  {
99  $query = new BulkInsertQuery($this->connection, $this->testTable);
100 
101  $query->addValues(['bar', 'baz', 'named' => 'bloo']);
102 
103  $this->assertSame("INSERT INTO {$this->testTable} VALUES (?, ?, ?)", (string)$query);
104  $this->assertSame(['bar', 'baz', 'bloo'], $query->getParameters());
105  $this->assertSame([null, null, null], $query->getParameterTypes());
106 
107  $query = new BulkInsertQuery($this->connection, $this->testTable);
108 
109  $query->addValues(
110  ['bar', 'baz', 'named' => 'bloo'],
112  );
113 
114  $this->assertSame("INSERT INTO {$this->testTable} VALUES (?, ?, ?)", (string)$query);
115  $this->assertSame(['bar', 'baz', 'bloo'], $query->getParameters());
116  $this->assertSame([null, Connection::PARAM_INT, Connection::PARAM_BOOL], $query->getParameterTypes());
117  }
118 
123  {
124  $query = new BulkInsertQuery($this->connection, $this->testTable);
125 
126  $query->addValues([]);
127  $query->addValues(['bar', 'baz']);
128  $query->addValues(['bar', 'baz', 'bloo']);
129  $query->addValues(['bar', 'baz', 'named' => 'bloo']);
130 
131  $this->assertSame("INSERT INTO {$this->testTable} VALUES (), (?, ?), (?, ?, ?), (?, ?, ?)", (string)$query);
132  $this->assertSame(['bar', 'baz', 'bar', 'baz', 'bloo', 'bar', 'baz', 'bloo'], $query->getParameters());
133  $this->assertSame([null, null, null, null, null, null, null, null], $query->getParameterTypes());
134 
135  $query = new BulkInsertQuery($this->connection, $this->testTable);
136 
137  $query->addValues([], [Connection::PARAM_INT]);
138  $query->addValues(['bar', 'baz'], [1 => Connection::PARAM_BOOL]);
139  $query->addValues(['bar', 'baz', 'bloo'], [Connection::PARAM_INT, null, Connection::PARAM_BOOL]);
140  $query->addValues(
141  ['bar', 'baz', 'named' => 'bloo'],
143  );
144 
145  $this->assertSame("INSERT INTO {$this->testTable} VALUES (), (?, ?), (?, ?, ?), (?, ?, ?)", (string)$query);
146  $this->assertSame(['bar', 'baz', 'bar', 'baz', 'bloo', 'bar', 'baz', 'bloo'], $query->getParameters());
147  $this->assertSame(
148  [
149  null,
152  null,
154  null,
157  ],
158  $query->getParameterTypes()
159  );
160  }
161 
166  {
167  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
168 
169  $query->addValues(['bar', 'baz']);
170 
171  $this->assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
172  $this->assertSame(['bar', 'baz'], $query->getParameters());
173  $this->assertSame([null, null], $query->getParameterTypes());
174 
175  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
176 
177  $query->addValues(['bar', 'baz'], [1 => Connection::PARAM_BOOL]);
178 
179  $this->assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
180  $this->assertSame(['bar', 'baz'], $query->getParameters());
181  $this->assertSame([null, Connection::PARAM_BOOL], $query->getParameterTypes());
182  }
183 
188  {
189  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
190 
191  $query->addValues(['baz' => 'baz', 'bar' => 'bar']);
192 
193  $this->assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
194  $this->assertSame(['bar', 'baz'], $query->getParameters());
195  $this->assertSame([null, null], $query->getParameterTypes());
196 
197  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
198 
199  $query->addValues(['baz' => 'baz', 'bar' => 'bar'], [null, Connection::PARAM_INT]);
200 
201  $this->assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
202  $this->assertSame(['bar', 'baz'], $query->getParameters());
203  $this->assertSame([null, Connection::PARAM_INT], $query->getParameterTypes());
204  }
205 
210  {
211  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
212 
213  $query->addValues([1 => 'baz', 'bar' => 'bar']);
214 
215  $this->assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
216  $this->assertSame(['bar', 'baz'], $query->getParameters());
217  $this->assertSame([null, null], $query->getParameterTypes());
218 
219  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
220 
221  $query->addValues([1 => 'baz', 'bar' => 'bar'], [Connection::PARAM_INT, Connection::PARAM_BOOL]);
222 
223  $this->assertSame("INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?)", (string)$query);
224  $this->assertSame(['bar', 'baz'], $query->getParameters());
225  $this->assertSame([Connection::PARAM_INT, Connection::PARAM_BOOL], $query->getParameterTypes());
226  }
227 
232  {
233  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
234 
235  $query->addValues(['bar', 'baz']);
236  $query->addValues([1 => 'baz', 'bar' => 'bar']);
237  $query->addValues(['bar', 'baz' => 'baz']);
238  $query->addValues(['bar' => 'bar', 'baz' => 'baz']);
239 
240  $this->assertSame(
241  "INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?), (?, ?), (?, ?), (?, ?)",
242  (string)$query
243  );
244  $this->assertSame(['bar', 'baz', 'bar', 'baz', 'bar', 'baz', 'bar', 'baz'], $query->getParameters());
245  $this->assertSame([null, null, null, null, null, null, null, null], $query->getParameterTypes());
246 
247  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
248 
249  $query->addValues(['bar', 'baz'], ['baz' => Connection::PARAM_BOOL, 'bar' => Connection::PARAM_INT]);
250  $query->addValues([1 => 'baz', 'bar' => 'bar'], [1 => Connection::PARAM_BOOL, 'bar' => Connection::PARAM_INT]);
251  $query->addValues(['bar', 'baz' => 'baz'], [null, null]);
252  $query->addValues(
253  ['bar' => 'bar', 'baz' => 'baz'],
254  ['bar' => Connection::PARAM_INT, 'baz' => Connection::PARAM_BOOL]
255  );
256 
257  $this->assertSame(
258  "INSERT INTO {$this->testTable} (bar, baz) VALUES (?, ?), (?, ?), (?, ?), (?, ?)",
259  (string)$query
260  );
261  $this->assertSame(['bar', 'baz', 'bar', 'baz', 'bar', 'baz', 'bar', 'baz'], $query->getParameters());
262  $this->assertSame(
263  [
268  null,
269  null,
272  ],
273  $query->getParameterTypes()
274  );
275  }
276 
281  {
282  $this->expectException(\InvalidArgumentException::class);
283  $this->expectExceptionMessage('No value specified for column bar (index 0).');
284 
285  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
286  $query->addValues([]);
287  }
288 
293  {
294  $this->expectException(\InvalidArgumentException::class);
295  $this->expectExceptionMessage('Multiple values specified for column baz (index 1).');
296 
297  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
298  $query->addValues(['bar', 'baz', 'baz' => 666]);
299  }
300 
305  {
306  $this->expectException(\InvalidArgumentException::class);
307  $this->expectExceptionMessage('Multiple types specified for column baz (index 1).');
308 
309  $query = new BulkInsertQuery($this->connection, $this->testTable, ['bar', 'baz']);
310  $query->addValues(
311  ['bar', 'baz'],
313  );
314  }
315 
319  public function executeWithMaxInsertRowsPerStatementExceededThrowsException()
320  {
321  $this->expectException(\LogicException::class);
322  $this->expectExceptionMessage('You can only insert 10 rows in a single INSERT statement with platform "mock".');
323 
325  $subject = $this->getAccessibleMock(
326  BulkInsertQuery::class,
327  ['getInsertMaxRows'],
328  [$this->connection, $this->testTable],
329  ''
330  );
331 
332  $subject->expects($this->any())
333  ->method('getInsertMaxRows')
334  ->will($this->returnValue(10));
335 
336  for ($i = 0; $i <= 10; $i++) {
337  $subject->addValues([]);
338  }
339 
340  $subject->execute();
341  }
342 }