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