[BUGFIX] Add missing namespace parts
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Database / DatabaseConnectionTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Database;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Database\DatabaseConnection;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\Components\TestingFramework\Core\AccessibleObjectInterface;
20
21 /**
22 * Test case
23 *
24 */
25 class DatabaseConnectionTest extends \TYPO3\Components\TestingFramework\Core\Unit\UnitTestCase
26 {
27 /**
28 * @var DatabaseConnection
29 */
30 protected $subject;
31 /**
32 * @var string
33 */
34 protected $testTable = 'test_database_connection';
35
36 /**
37 * @var string
38 */
39 protected $testField = 'test_field';
40
41 /**
42 * @var string
43 */
44 protected $anotherTestField = 'another_test_field';
45
46 /**
47 * Set the test up
48 *
49 * @return void
50 */
51 protected function setUp()
52 {
53 parent::setUp();
54 $this->subject = $this->getAccessibleMock(DatabaseConnection::class, ['dummy'], [], '', false);
55 $this->subject->_set('databaseName', 'typo3_test');
56 }
57
58 //////////////////////////////////////////////////
59 // Write/Read tests for charsets and binaries
60 //////////////////////////////////////////////////
61
62 /**
63 * @test
64 */
65 public function storedFullAsciiRangeCallsLinkObjectWithGivenData()
66 {
67 $binaryString = '';
68 for ($i = 0; $i < 256; $i++) {
69 $binaryString .= chr($i);
70 }
71
72 /** @var DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject|AccessibleObjectInterface $subject */
73 $subject = $this->getAccessibleMock(DatabaseConnection::class, ['fullQuoteStr'], [], '', false);
74 $subject->_set('isConnected', true);
75 $subject
76 ->expects($this->any())
77 ->method('fullQuoteStr')
78 ->will($this->returnCallback(function ($data) {
79 return $data;
80 }));
81 $mysqliProphecy = $this->prophesize(\mysqli::class);
82 $mysqliProphecy->query("INSERT INTO {$this->testTable} ({$this->testField}) VALUES ({$binaryString})")
83 ->shouldBeCalled();
84 $subject->_set('link', $mysqliProphecy->reveal());
85
86 $subject->exec_INSERTquery($this->testTable, [$this->testField => $binaryString]);
87 }
88
89 /**
90 * @test
91 * @requires function gzcompress
92 */
93 public function storedGzipCompressedDataReturnsSameData()
94 {
95 $testStringWithBinary = gzcompress('sdfkljer4587');
96
97 /** @var DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject|AccessibleObjectInterface $subject */
98 $subject = $this->getAccessibleMock(DatabaseConnection::class, ['fullQuoteStr'], [], '', false);
99 $subject->_set('isConnected', true);
100 $subject
101 ->expects($this->any())
102 ->method('fullQuoteStr')
103 ->will($this->returnCallback(function ($data) {
104 return $data;
105 }));
106 $mysqliProphecy = $this->prophesize(\mysqli::class);
107 $mysqliProphecy->query("INSERT INTO {$this->testTable} ({$this->testField}) VALUES ({$testStringWithBinary})")
108 ->shouldBeCalled();
109 $subject->_set('link', $mysqliProphecy->reveal());
110
111 $subject->exec_INSERTquery($this->testTable, [$this->testField => $testStringWithBinary]);
112 }
113
114 ////////////////////////////////
115 // Tests concerning listQuery
116 ////////////////////////////////
117
118 /**
119 * @test
120 * @see http://forge.typo3.org/issues/23253
121 */
122 public function listQueryWithIntegerCommaAsValue()
123 {
124 /** @var DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject|AccessibleObjectInterface $subject */
125 $subject = $this->getAccessibleMock(DatabaseConnection::class, ['quoteStr'], [], '', false);
126 $subject->_set('isConnected', true);
127 $subject
128 ->expects($this->any())
129 ->method('quoteStr')
130 ->will($this->returnCallback(function ($data) {
131 return $data;
132 }));
133 // Note: 44 = ord(',')
134 $this->assertEquals($subject->listQuery('dummy', 44, 'table'), $subject->listQuery('dummy', '44', 'table'));
135 }
136
137 /**
138 * @test
139 */
140 public function listQueryThrowsExceptionIfValueContainsComma()
141 {
142 $this->expectException(\InvalidArgumentException::class);
143 $this->expectExceptionCode(1294585862);
144
145 /** @var DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject|AccessibleObjectInterface $subject */
146 $subject = $this->getAccessibleMock(DatabaseConnection::class, ['quoteStr'], [], '', false);
147 $subject->_set('isConnected', true);
148 $subject->listQuery('aField', 'foo,bar', 'aTable');
149 }
150
151 ////////////////////////////////
152 // Tests concerning searchQuery
153 ////////////////////////////////
154
155 /**
156 * Data provider for searchQueryCreatesQuery
157 *
158 * @return array
159 */
160 public function searchQueryDataProvider()
161 {
162 return [
163 'One search word in one field' => [
164 '(pages.title LIKE \'%TYPO3%\')',
165 ['TYPO3'],
166 ['title'],
167 'pages',
168 'AND',
169 ],
170 'One search word with special chars (for like)' => [
171 '(pages.title LIKE \'%TYPO3\\_100\\%%\')',
172 ['TYPO3_100%'],
173 ['title'],
174 'pages',
175 'AND',
176 ],
177 'One search word in multiple fields' => [
178 "(pages.title LIKE '%TYPO3%' OR pages.keyword LIKE '%TYPO3%' OR pages.description LIKE '%TYPO3%')",
179 ['TYPO3'],
180 ['title', 'keyword', 'description'],
181 'pages',
182 'AND',
183 ],
184 'Multiple search words in one field with AND constraint' => [
185 "(pages.title LIKE '%TYPO3%') AND (pages.title LIKE '%is%') AND (pages.title LIKE '%great%')",
186 ['TYPO3', 'is', 'great'],
187 ['title'],
188 'pages',
189 'AND',
190 ],
191 'Multiple search words in one field with OR constraint' => [
192 "(pages.title LIKE '%TYPO3%') OR (pages.title LIKE '%is%') OR (pages.title LIKE '%great%')",
193 ['TYPO3', 'is', 'great'],
194 ['title'],
195 'pages',
196 'OR',
197 ],
198 'Multiple search words in multiple fields with AND constraint' => [
199 "(pages.title LIKE '%TYPO3%' OR pages.keywords LIKE '%TYPO3%' OR pages.description LIKE '%TYPO3%') " .
200 "AND (pages.title LIKE '%is%' OR pages.keywords LIKE '%is%' OR pages.description LIKE '%is%') " .
201 "AND (pages.title LIKE '%great%' OR pages.keywords LIKE '%great%' OR pages.description LIKE '%great%')",
202 ['TYPO3', 'is', 'great'],
203 ['title', 'keywords', 'description'],
204 'pages',
205 'AND',
206 ],
207 'Multiple search words in multiple fields with OR constraint' => [
208 "(pages.title LIKE '%TYPO3%' OR pages.keywords LIKE '%TYPO3%' OR pages.description LIKE '%TYPO3%') " .
209 "OR (pages.title LIKE '%is%' OR pages.keywords LIKE '%is%' OR pages.description LIKE '%is%') " .
210 "OR (pages.title LIKE '%great%' OR pages.keywords LIKE '%great%' OR pages.description LIKE '%great%')",
211 ['TYPO3', 'is', 'great'],
212 ['title', 'keywords', 'description'],
213 'pages',
214 'OR',
215 ],
216 ];
217 }
218
219 /**
220 * @test
221 * @dataProvider searchQueryDataProvider
222 * @param string $expectedResult
223 * @param array $searchWords
224 * @param array $fields
225 * @param string $table
226 * @param string $constraint
227 */
228 public function searchQueryCreatesQuery($expectedResult, array $searchWords, array $fields, $table, $constraint)
229 {
230 /** @var DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject $subject */
231 $subject = $this->getMockBuilder(DatabaseConnection::class)
232 ->setMethods(['quoteStr'])
233 ->disableOriginalConstructor()
234 ->getMock();
235 $subject
236 ->expects($this->any())
237 ->method('quoteStr')
238 ->will($this->returnCallback(function ($data) {
239 return $data;
240 }));
241
242 $this->assertSame($expectedResult, $subject->searchQuery($searchWords, $fields, $table, $constraint));
243 }
244
245 /////////////////////////////////////////////////
246 // Tests concerning escapeStringForLikeComparison
247 /////////////////////////////////////////////////
248
249 /**
250 * @test
251 */
252 public function escapeStringForLikeComparison()
253 {
254 /** @var DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject $subject */
255 $subject = $this->getMockBuilder(DatabaseConnection::class)
256 ->setMethods(['dummy'])
257 ->disableOriginalConstructor()
258 ->getMock();
259 $this->assertEquals('foo\\_bar\\%', $subject->escapeStrForLike('foo_bar%', 'table'));
260 }
261
262 /////////////////////////////////////////////////
263 // Tests concerning stripOrderByForOrderByKeyword
264 /////////////////////////////////////////////////
265
266 /**
267 * Data Provider for stripGroupByForGroupByKeyword()
268 *
269 * @see stripOrderByForOrderByKeyword()
270 * @return array
271 */
272 public function stripOrderByForOrderByKeywordDataProvider()
273 {
274 return [
275 'single ORDER BY' => [
276 'ORDER BY name, tstamp',
277 'name, tstamp'
278 ],
279 'single ORDER BY in lower case' => [
280 'order by name, tstamp',
281 'name, tstamp'
282 ],
283 'ORDER BY with additional space behind' => [
284 'ORDER BY name, tstamp',
285 'name, tstamp'
286 ],
287 'ORDER BY without space between the words' => [
288 'ORDERBY name, tstamp',
289 'name, tstamp'
290 ],
291 'ORDER BY added twice' => [
292 'ORDER BY ORDER BY name, tstamp',
293 'name, tstamp'
294 ],
295 'ORDER BY added twice without spaces in the first occurrence' => [
296 'ORDERBY ORDER BY name, tstamp',
297 'name, tstamp',
298 ],
299 'ORDER BY added twice without spaces in the second occurrence' => [
300 'ORDER BYORDERBY name, tstamp',
301 'name, tstamp',
302 ],
303 'ORDER BY added twice without spaces' => [
304 'ORDERBYORDERBY name, tstamp',
305 'name, tstamp'
306 ],
307 'ORDER BY added twice without spaces afterwards' => [
308 'ORDERBYORDERBYname, tstamp',
309 'name, tstamp'
310 ],
311 ];
312 }
313
314 /**
315 * @test
316 * @dataProvider stripOrderByForOrderByKeywordDataProvider
317 * @param string $orderByClause The clause to test
318 * @param string $expectedResult The expected result
319 * @return void
320 */
321 public function stripOrderByForOrderByKeyword($orderByClause, $expectedResult)
322 {
323 /** @var DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject $subject */
324 $subject = $this->getMockBuilder(DatabaseConnection::class)
325 ->setMethods(['dummy'])
326 ->disableOriginalConstructor()
327 ->getMock();
328 $strippedQuery = $subject->stripOrderBy($orderByClause);
329 $this->assertEquals($expectedResult, $strippedQuery);
330 }
331
332 /////////////////////////////////////////////////
333 // Tests concerning stripGroupByForGroupByKeyword
334 /////////////////////////////////////////////////
335
336 /**
337 * Data Provider for stripGroupByForGroupByKeyword()
338 *
339 * @see stripGroupByForGroupByKeyword()
340 * @return array
341 */
342 public function stripGroupByForGroupByKeywordDataProvider()
343 {
344 return [
345 'single GROUP BY' => [
346 'GROUP BY name, tstamp',
347 'name, tstamp'
348 ],
349 'single GROUP BY in lower case' => [
350 'group by name, tstamp',
351 'name, tstamp'
352 ],
353 'GROUP BY with additional space behind' => [
354 'GROUP BY name, tstamp',
355 'name, tstamp'
356 ],
357 'GROUP BY without space between the words' => [
358 'GROUPBY name, tstamp',
359 'name, tstamp'
360 ],
361 'GROUP BY added twice' => [
362 'GROUP BY GROUP BY name, tstamp',
363 'name, tstamp'
364 ],
365 'GROUP BY added twice without spaces in the first occurrence' => [
366 'GROUPBY GROUP BY name, tstamp',
367 'name, tstamp',
368 ],
369 'GROUP BY added twice without spaces in the second occurrence' => [
370 'GROUP BYGROUPBY name, tstamp',
371 'name, tstamp',
372 ],
373 'GROUP BY added twice without spaces' => [
374 'GROUPBYGROUPBY name, tstamp',
375 'name, tstamp'
376 ],
377 'GROUP BY added twice without spaces afterwards' => [
378 'GROUPBYGROUPBYname, tstamp',
379 'name, tstamp'
380 ],
381 ];
382 }
383
384 /**
385 * @test
386 * @dataProvider stripGroupByForGroupByKeywordDataProvider
387 * @param string $groupByClause The clause to test
388 * @param string $expectedResult The expected result
389 * @return void
390 */
391 public function stripGroupByForGroupByKeyword($groupByClause, $expectedResult)
392 {
393 /** @var DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject $subject */
394 $subject = $this->getMockBuilder(DatabaseConnection::class)
395 ->setMethods(['dummy'])
396 ->disableOriginalConstructor()
397 ->getMock();
398 $strippedQuery = $subject->stripGroupBy($groupByClause);
399 $this->assertEquals($expectedResult, $strippedQuery);
400 }
401
402 /////////////////////////////////////////////////
403 // Tests concerning stripOrderByForOrderByKeyword
404 /////////////////////////////////////////////////
405
406 /**
407 * Data Provider for stripGroupByForGroupByKeyword()
408 *
409 * @see stripOrderByForOrderByKeyword()
410 * @return array
411 */
412 public function cleanIntArrayDataProvider()
413 {
414 return [
415 'simple array' => [
416 [1, 2, 3],
417 [1, 2, 3],
418 ],
419 'string array' => [
420 ['2', '4', '8'],
421 [2, 4, 8],
422 ],
423 'string array with letters #1' => [
424 ['3', '6letters', '12'],
425 [3, 6, 12],
426 ],
427 'string array with letters #2' => [
428 ['3', 'letters6', '12'],
429 [3, 0, 12],
430 ],
431 'string array with letters #3' => [
432 ['3', '6letters4', '12'],
433 [3, 6, 12],
434 ],
435 'associative array' => [
436 ['apples' => 3, 'bananas' => 4, 'kiwis' => 9],
437 ['apples' => 3, 'bananas' => 4, 'kiwis' => 9],
438 ],
439 'associative string array' => [
440 ['apples' => '1', 'bananas' => '5', 'kiwis' => '7'],
441 ['apples' => 1, 'bananas' => 5, 'kiwis' => 7],
442 ],
443 'associative string array with letters #1' => [
444 ['apples' => '1', 'bananas' => 'no5', 'kiwis' => '7'],
445 ['apples' => 1, 'bananas' => 0, 'kiwis' => 7],
446 ],
447 'associative string array with letters #2' => [
448 ['apples' => '1', 'bananas' => '5yes', 'kiwis' => '7'],
449 ['apples' => 1, 'bananas' => 5, 'kiwis' => 7],
450 ],
451 'associative string array with letters #3' => [
452 ['apples' => '1', 'bananas' => '5yes9', 'kiwis' => '7'],
453 ['apples' => 1, 'bananas' => 5, 'kiwis' => 7],
454 ],
455 'multidimensional associative array' => [
456 ['apples' => '1', 'bananas' => [3, 4], 'kiwis' => '7'],
457 // intval(array(...)) is 1
458 // But by specification "cleanIntArray" should only get used on one-dimensional arrays
459 ['apples' => 1, 'bananas' => 1, 'kiwis' => 7],
460 ],
461 ];
462 }
463
464 /**
465 * @test
466 * @dataProvider cleanIntArrayDataProvider
467 * @param array $exampleData The array to sanitize
468 * @param array $expectedResult The expected result
469 * @return void
470 */
471 public function cleanIntArray($exampleData, $expectedResult)
472 {
473 $sanitizedArray = $this->subject->cleanIntArray($exampleData);
474 $this->assertEquals($expectedResult, $sanitizedArray);
475 }
476
477 /**
478 * @test
479 *
480 * @return void
481 */
482 public function cleanIntListReturnsCleanedString()
483 {
484 $str = '234,-434,4.3,0, 1';
485 $result = $this->subject->cleanIntList($str);
486 $this->assertSame('234,-434,4,0,1', $result);
487 }
488
489 /**
490 * @test
491 */
492 public function sqlForSelectMmQuery()
493 {
494 $result = $this->subject->SELECT_mm_query(
495 '*',
496 'sys_category',
497 'sys_category_record_mm',
498 'tt_content',
499 'AND sys_category.uid = 1',
500 '',
501 'sys_category.title DESC'
502 );
503 $expected = 'SELECT * FROM sys_category,sys_category_record_mm,tt_content ' .
504 'WHERE sys_category.uid=sys_category_record_mm.uid_local ' .
505 'AND tt_content.uid=sys_category_record_mm.uid_foreign ' .
506 'AND sys_category.uid = 1 ORDER BY sys_category.title DESC';
507 $this->assertEquals($expected, $result);
508 }
509
510 /**
511 * Data provider for searchQueryCreatesQuery
512 *
513 * @return array
514 */
515 public function noQuoteForFullQuoteArrayDataProvider()
516 {
517 return [
518 'noQuote boolean false' => [
519 ['aField' => 'aValue', 'anotherField' => 'anotherValue'],
520 ['aField' => "'aValue'", 'anotherField' => "'anotherValue'"],
521 false,
522 ],
523 'noQuote boolean true' => [
524 ['aField' => 'aValue', 'anotherField' => 'anotherValue'],
525 ['aField' => 'aValue', 'anotherField' => 'anotherValue'],
526 true,
527 ],
528 'noQuote list of fields' => [
529 ['aField' => 'aValue', 'anotherField' => 'anotherValue'],
530 ['aField' => "'aValue'", 'anotherField' => 'anotherValue'],
531 'anotherField',
532 ],
533 'noQuote array of fields' => [
534 ['aField' => 'aValue', 'anotherField' => 'anotherValue'],
535 ['aField' => 'aValue', 'anotherField' => "'anotherValue'"],
536 ['aField'],
537 ],
538 ];
539 }
540
541 /**
542 * @test
543 * @param array $input
544 * @param array $expected
545 * @param bool|array|string $noQuote
546 * @dataProvider noQuoteForFullQuoteArrayDataProvider
547 */
548 public function noQuoteForFullQuoteArray(array $input, array $expected, $noQuote)
549 {
550 /** @var DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject|AccessibleObjectInterface $subject */
551 $subject = $this->getMockBuilder(DatabaseConnection::class)
552 ->setMethods(['fullQuoteStr'])
553 ->disableOriginalConstructor()
554 ->getMock();
555
556 $subject
557 ->expects($this->any())
558 ->method('fullQuoteStr')
559 ->will($this->returnCallback(function ($data) {
560 return '\'' . (string)$data . '\'';
561 }));
562 $this->assertSame($expected, $subject->fullQuoteArray($input, 'aTable', $noQuote));
563 }
564
565 /**
566 * @test
567 */
568 public function sqlSelectDbReturnsTrue()
569 {
570 /** @var DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject|AccessibleObjectInterface $subject */
571 $subject = $this->getAccessibleMock(DatabaseConnection::class, ['dummy'], [], '', false);
572 $subject->_set('isConnected', true);
573 $subject->_set('databaseName', $this->testTable);
574
575 $mysqliProphecy = $this->prophesize(\mysqli::class);
576 $mysqliProphecy->select_db($this->testTable)->shouldBeCalled()->willReturn(true);
577 $subject->_set('link', $mysqliProphecy->reveal());
578
579 $this->assertTrue($subject->sql_select_db());
580 }
581
582 /**
583 * @test
584 */
585 public function sqlSelectDbReturnsFalse()
586 {
587 $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemLogLevel'] = GeneralUtility::SYSLOG_SEVERITY_WARNING;
588
589 /** @var DatabaseConnection|\PHPUnit_Framework_MockObject_MockObject|AccessibleObjectInterface $subject */
590 $subject = $this->getAccessibleMock(DatabaseConnection::class, ['sql_error'], [], '', false);
591 $subject->_set('isConnected', true);
592 $subject->_set('databaseName', $this->testTable);
593 $subject->expects($this->any())->method('sql_error')->will($this->returnValue(''));
594
595 $mysqliProphecy = $this->prophesize(\mysqli::class);
596 $mysqliProphecy->select_db($this->testTable)->shouldBeCalled()->willReturn(false);
597 $subject->_set('link', $mysqliProphecy->reveal());
598
599 $this->assertFalse($subject->sql_select_db());
600 }
601
602 /**
603 * @test
604 *
605 * @return void
606 */
607 public function insertQueryCreateValidQuery()
608 {
609 $this->subject = $this->getAccessibleMock(DatabaseConnection::class, ['fullQuoteStr'], [], '', false);
610 $this->subject->expects($this->any())
611 ->method('fullQuoteStr')
612 ->will($this->returnCallback(function ($data) {
613 return '\'' . (string)$data . '\'';
614 }));
615
616 $fieldValues = [$this->testField => 'Foo'];
617 $queryExpected = "INSERT INTO {$this->testTable} ({$this->testField}) VALUES ('Foo')";
618 $queryGenerated = $this->subject->INSERTquery($this->testTable, $fieldValues);
619 $this->assertSame($queryExpected, $queryGenerated);
620 }
621
622 /**
623 * @test
624 *
625 * @return void
626 */
627 public function insertQueryCreateValidQueryFromMultipleValues()
628 {
629 $this->subject = $this->getAccessibleMock(DatabaseConnection::class, ['fullQuoteStr'], [], '', false);
630 $this->subject->expects($this->any())
631 ->method('fullQuoteStr')
632 ->will($this->returnCallback(function ($data) {
633 return '\'' . (string)$data . '\'';
634 }));
635 $fieldValues = [
636 $this->testField => 'Foo',
637 $this->anotherTestField => 'Bar',
638 ];
639 $queryExpected = "INSERT INTO {$this->testTable} ({$this->testField},{$this->anotherTestField}) " .
640 "VALUES ('Foo','Bar')";
641 $queryGenerated = $this->subject->INSERTquery($this->testTable, $fieldValues);
642 $this->assertSame($queryExpected, $queryGenerated);
643 }
644
645 /**
646 * @test
647 *
648 * @return void
649 */
650 public function insertMultipleRowsCreateValidQuery()
651 {
652 $this->subject = $this->getAccessibleMock(DatabaseConnection::class, ['fullQuoteStr'], [], '', false);
653 $this->subject->expects($this->any())
654 ->method('fullQuoteStr')
655 ->will($this->returnCallback(function ($data) {
656 return '\'' . (string)$data . '\'';
657 }));
658 $fields = [$this->testField, $this->anotherTestField];
659 $values = [
660 ['Foo', 100],
661 ['Bar', 200],
662 ['Baz', 300],
663 ];
664 $queryExpected = "INSERT INTO {$this->testTable} ({$this->testField}, {$this->anotherTestField}) " .
665 "VALUES ('Foo', '100'), ('Bar', '200'), ('Baz', '300')";
666 $queryGenerated = $this->subject->INSERTmultipleRows($this->testTable, $fields, $values);
667 $this->assertSame($queryExpected, $queryGenerated);
668 }
669
670 /**
671 * @test
672 *
673 * @return void
674 */
675 public function updateQueryCreateValidQuery()
676 {
677 $this->subject = $this->getAccessibleMock(DatabaseConnection::class, ['fullQuoteStr'], [], '', false);
678 $this->subject->expects($this->any())
679 ->method('fullQuoteStr')
680 ->will($this->returnCallback(function ($data) {
681 return '\'' . (string)$data . '\'';
682 }));
683
684 $fieldsValues = [$this->testField => 'aTestValue'];
685 $queryExpected = "UPDATE {$this->testTable} SET {$this->testField}='aTestValue' WHERE id=1";
686 $queryGenerated = $this->subject->UPDATEquery($this->testTable, 'id=1', $fieldsValues);
687 $this->assertSame($queryExpected, $queryGenerated);
688 }
689
690 /**
691 * @test
692 *
693 * @return void
694 */
695 public function deleteQueryCreateValidQuery()
696 {
697 $queryExpected = "DELETE FROM {$this->testTable} WHERE id=1";
698 $queryGenerated = $this->subject->DELETEquery($this->testTable, 'id=1');
699 $this->assertSame($queryExpected, $queryGenerated);
700 }
701
702 /**
703 * @test
704 *
705 * @return void
706 */
707 public function selectQueryCreateValidQuery()
708 {
709 $queryExpected = "SELECT {$this->testField} FROM {$this->testTable} WHERE id=1";
710 $queryGenerated = $this->subject->SELECTquery($this->testField, $this->testTable, 'id=1');
711 $this->assertSame($queryExpected, $queryGenerated);
712 }
713
714 /**
715 * @test
716 *
717 * @return void
718 */
719 public function selectQueryCreateValidQueryWithEmptyWhereClause()
720 {
721 $queryExpected = "SELECT {$this->testField} FROM {$this->testTable}";
722 $queryGenerated = $this->subject->SELECTquery($this->testField, $this->testTable, '');
723 $this->assertSame($queryExpected, $queryGenerated);
724 }
725
726 /**
727 * @test
728 *
729 * @return void
730 */
731 public function selectQueryCreateValidQueryWithGroupByClause()
732 {
733 $queryExpected = "SELECT {$this->testField} FROM {$this->testTable} WHERE id=1 GROUP BY id";
734 $queryGenerated = $this->subject->SELECTquery($this->testField, $this->testTable, 'id=1', 'id');
735 $this->assertSame($queryExpected, $queryGenerated);
736 }
737
738 /**
739 * @test
740 *
741 * @return void
742 */
743 public function selectQueryCreateValidQueryWithOrderByClause()
744 {
745 $queryExpected = "SELECT {$this->testField} FROM {$this->testTable} WHERE id=1 ORDER BY id";
746 $queryGenerated = $this->subject->SELECTquery($this->testField, $this->testTable, 'id=1', '', 'id');
747 $this->assertSame($queryExpected, $queryGenerated);
748 }
749
750 /**
751 * @test
752 *
753 * @return void
754 */
755 public function selectQueryCreateValidQueryWithLimitClause()
756 {
757 $queryGenerated = $this->subject->SELECTquery($this->testField, $this->testTable, 'id=1', '', '', '1,2');
758 $queryExpected = "SELECT {$this->testField} FROM {$this->testTable} WHERE id=1 LIMIT 1,2";
759 $this->assertSame($queryExpected, $queryGenerated);
760 }
761
762 /**
763 * @test
764 *
765 * @return void
766 */
767 public function selectSubQueryCreateValidQuery()
768 {
769 $queryExpected = "SELECT {$this->testField} FROM {$this->testTable} WHERE id=1";
770 $queryGenerated = $this->subject->SELECTsubquery($this->testField, $this->testTable, 'id=1');
771 $this->assertSame($queryExpected, $queryGenerated);
772 }
773
774 /**
775 * @test
776 *
777 * @return void
778 */
779 public function truncateQueryCreateValidQuery()
780 {
781 $queryExpected = "TRUNCATE TABLE {$this->testTable}";
782 $queryGenerated = $this->subject->TRUNCATEquery($this->testTable);
783 $this->assertSame($queryExpected, $queryGenerated);
784 }
785 }