[TASK] Doctrine: Create named parameters for value setting operations
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Database / Query / QueryBuilderTest.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 Prophecy\Argument;
19 use TYPO3\CMS\Core\Database\Connection;
20 use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
21 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
22 use TYPO3\CMS\Core\Tests\Unit\Database\Mocks\MockPlatform;
23 use TYPO3\CMS\Core\Tests\UnitTestCase;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26 class QueryBuilderTest extends UnitTestCase
27 {
28 /**
29 * @var Connection|\Prophecy\Prophecy\ObjectProphecy
30 */
31 protected $connection;
32
33 /**
34 * @var \Doctrine\DBAL\Platforms\AbstractPlatform
35 */
36 protected $platform;
37
38 /**
39 * @var QueryBuilder
40 */
41 protected $subject;
42
43 /**
44 * @var \Doctrine\DBAL\Query\QueryBuilder|\Prophecy\Prophecy\ObjectProphecy
45 */
46 protected $concreteQueryBuilder;
47
48 /**
49 * Create a new database connection mock object for every test.
50 *
51 * @return void
52 */
53 protected function setUp()
54 {
55 parent::setUp();
56
57 $this->concreteQueryBuilder = $this->prophesize(\Doctrine\DBAL\Query\QueryBuilder::class);
58
59 $this->connection = $this->prophesize(Connection::class);
60 $this->connection->getDatabasePlatform()->willReturn(new MockPlatform());
61
62 $this->subject = GeneralUtility::makeInstance(
63 QueryBuilder::class,
64 $this->connection->reveal(),
65 null,
66 $this->concreteQueryBuilder->reveal()
67 );
68 }
69
70 /**
71 * @test
72 */
73 public function exprReturnsExpressionBuilderForConnection()
74 {
75 $this->connection->getExpressionBuilder()
76 ->shouldBeCalled()
77 ->willReturn(GeneralUtility::makeInstance(ExpressionBuilder::class, $this->connection->reveal()));
78
79 $this->subject->expr();
80 }
81
82 /**
83 * @test
84 */
85 public function getTypeDelegatesToConcreteQueryBuilder()
86 {
87 $this->concreteQueryBuilder->getType()
88 ->shouldBeCalled()
89 ->willReturn(\Doctrine\DBAL\Query\QueryBuilder::INSERT);
90
91 $this->subject->getType();
92 }
93
94 /**
95 * @test
96 */
97 public function getStateDelegatesToConcreteQueryBuilder()
98 {
99 $this->concreteQueryBuilder->getState()
100 ->shouldBeCalled()
101 ->willReturn(\Doctrine\DBAL\Query\QueryBuilder::STATE_CLEAN);
102
103 $this->subject->getState();
104 }
105
106 /**
107 * @test
108 */
109 public function getSQLDelegatesToConcreteQueryBuilder()
110 {
111 $this->concreteQueryBuilder->getSQL()
112 ->shouldBeCalled()
113 ->willReturn('UPDATE aTable SET pid = 7');
114 $this->concreteQueryBuilder->getType()
115 ->willReturn(2); // Update Type
116
117 $this->subject->getSQL();
118 }
119
120 /**
121 * @test
122 */
123 public function setParameterDelegatesToConcreteQueryBuilder()
124 {
125 $this->concreteQueryBuilder->setParameter(Argument::exact('aField'), Argument::exact(5), Argument::cetera())
126 ->shouldBeCalled()
127 ->willReturn($this->subject);
128
129 $this->subject->setParameter('aField', 5);
130 }
131
132 /**
133 * @test
134 */
135 public function setParametersDelegatesToConcreteQueryBuilder()
136 {
137 $this->concreteQueryBuilder->setParameters(Argument::exact(['aField' => 'aValue']), Argument::exact([]))
138 ->shouldBeCalled()
139 ->willReturn($this->subject);
140
141 $this->subject->setParameters(['aField' => 'aValue']);
142 }
143
144 /**
145 * @test
146 */
147 public function getParametersDelegatesToConcreteQueryBuilder()
148 {
149 $this->concreteQueryBuilder->getParameters()
150 ->shouldBeCalled()
151 ->willReturn(['aField' => 'aValue']);
152
153 $this->subject->getParameters();
154 }
155
156 /**
157 * @test
158 */
159 public function getParameterDelegatesToConcreteQueryBuilder()
160 {
161 $this->concreteQueryBuilder->getParameter(Argument::exact('aField'))
162 ->shouldBeCalled()
163 ->willReturn('aValue');
164
165 $this->subject->getParameter('aField');
166 }
167
168 /**
169 * @test
170 */
171 public function getParameterTypesDelegatesToConcreteQueryBuilder()
172 {
173 $this->concreteQueryBuilder->getParameterTypes()
174 ->shouldBeCalled()
175 ->willReturn([]);
176
177 $this->subject->getParameterTypes();
178 }
179
180 /**
181 * @test
182 */
183 public function getParameterTypeDelegatesToConcreteQueryBuilder()
184 {
185 $this->concreteQueryBuilder->getParameterType(Argument::exact('aField'))
186 ->shouldBeCalled()
187 ->willReturn(Connection::PARAM_STR);
188
189 $this->subject->getParameterType('aField');
190 }
191
192 /**
193 * @test
194 */
195 public function setFirstResultDelegatesToConcreteQueryBuilder()
196 {
197 $this->concreteQueryBuilder->setFirstResult(Argument::cetera())
198 ->shouldBeCalled()
199 ->willReturn($this->subject);
200
201 $this->subject->setFirstResult(1);
202 }
203
204 /**
205 * @test
206 */
207 public function getFirstResultDelegatesToConcreteQueryBuilder()
208 {
209 $this->concreteQueryBuilder->getFirstResult()
210 ->shouldBeCalled()
211 ->willReturn(1);
212
213 $this->subject->getFirstResult();
214 }
215
216 /**
217 * @test
218 */
219 public function setMaxResultsDelegatesToConcreteQueryBuilder()
220 {
221 $this->concreteQueryBuilder->setMaxResults(Argument::cetera())
222 ->shouldBeCalled()
223 ->willReturn($this->subject);
224
225 $this->subject->setMaxResults(1);
226 }
227
228 /**
229 * @test
230 */
231 public function getMaxResultsDelegatesToConcreteQueryBuilder()
232 {
233 $this->concreteQueryBuilder->getMaxResults()
234 ->shouldBeCalled()
235 ->willReturn(1);
236
237 $this->subject->getMaxResults();
238 }
239
240 /**
241 * @test
242 */
243 public function addDelegatesToConcreteQueryBuilder()
244 {
245 $this->concreteQueryBuilder->add(Argument::exact('select'), Argument::exact('aField'), Argument::cetera())
246 ->shouldBeCalled()
247 ->willReturn($this->subject);
248
249 $this->subject->add('select', 'aField');
250 }
251
252 /**
253 * @test
254 */
255 public function countBuildsExpressionAndCallsSelect()
256 {
257 $this->concreteQueryBuilder->select(Argument::exact('COUNT(*)'))
258 ->shouldBeCalled()
259 ->willReturn($this->subject);
260
261 $this->subject->count('*');
262 }
263
264 /**
265 * @test
266 */
267 public function selectQuotesIdentifiersAndDelegatesToConcreteQueryBuilder()
268 {
269 $this->connection->quoteIdentifier('aField')
270 ->shouldBeCalled()
271 ->willReturnArgument(0);
272 $this->connection->quoteIdentifier('anotherField')
273 ->shouldBeCalled()
274 ->willReturnArgument(0);
275 $this->concreteQueryBuilder->select(Argument::exact('aField'), Argument::exact('anotherField'))
276 ->shouldBeCalled()
277 ->willReturn($this->subject);
278
279 $this->subject->select('aField', 'anotherField');
280 }
281
282 /**
283 * @test
284 */
285 public function selectDoesNotQuoteStarPlaceholder()
286 {
287 $this->connection->quoteIdentifier('aField')
288 ->shouldBeCalled()
289 ->willReturnArgument(0);
290 $this->connection->quoteIdentifier('*')
291 ->shouldNotBeCalled();
292 $this->concreteQueryBuilder->select(Argument::exact('aField'), Argument::exact('*'))
293 ->shouldBeCalled()
294 ->willReturn($this->subject);
295
296 $this->subject->select('aField', '*');
297 }
298
299 /**
300 * @test
301 */
302 public function addSelectQuotesIdentifiersAndDelegatesToConcreteQueryBuilder()
303 {
304 $this->connection->quoteIdentifier('aField')
305 ->shouldBeCalled()
306 ->willReturnArgument(0);
307 $this->connection->quoteIdentifier('anotherField')
308 ->shouldBeCalled()
309 ->willReturnArgument(0);
310 $this->concreteQueryBuilder->addSelect(Argument::exact('aField'), Argument::exact('anotherField'))
311 ->shouldBeCalled()
312 ->willReturn($this->subject);
313
314 $this->subject->addSelect('aField', 'anotherField');
315 }
316
317 /**
318 * @test
319 */
320 public function addSelectDoesNotQuoteStarPlaceholder()
321 {
322 $this->connection->quoteIdentifier('aField')
323 ->shouldBeCalled()
324 ->willReturnArgument(0);
325 $this->connection->quoteIdentifier('*')
326 ->shouldNotBeCalled();
327 $this->concreteQueryBuilder->addSelect(Argument::exact('aField'), Argument::exact('*'))
328 ->shouldBeCalled()
329 ->willReturn($this->subject);
330
331 $this->subject->addSelect('aField', '*');
332 }
333
334 /**
335 * @test
336 * @todo: Test with alias
337 */
338 public function deleteQuotesIdentifierAndDelegatesToConcreteQueryBuilder()
339 {
340 $this->connection->quoteIdentifier('aTable')
341 ->shouldBeCalled()
342 ->willReturnArgument(0);
343 $this->concreteQueryBuilder->delete(Argument::exact('aTable'), Argument::cetera())
344 ->shouldBeCalled()
345 ->willReturn($this->subject);
346
347 $this->subject->delete('aTable');
348 }
349
350 /**
351 * @test
352 * @todo: Test with alias
353 */
354 public function updateQuotesIdentifierAndDelegatesToConcreteQueryBuilder()
355 {
356 $this->connection->quoteIdentifier('aTable')
357 ->shouldBeCalled()
358 ->willReturnArgument(0);
359 $this->concreteQueryBuilder->update(Argument::exact('aTable'), Argument::cetera())
360 ->shouldBeCalled()
361 ->willReturn($this->subject);
362
363 $this->subject->update('aTable');
364 }
365
366 /**
367 * @test
368 */
369 public function insertQuotesIdentifierAndDelegatesToConcreteQueryBuilder()
370 {
371 $this->connection->quoteIdentifier('aTable')
372 ->shouldBeCalled()
373 ->willReturnArgument(0);
374 $this->concreteQueryBuilder->insert(Argument::exact('aTable'))
375 ->shouldBeCalled()
376 ->willReturn($this->subject);
377
378 $this->subject->insert('aTable');
379 }
380
381 /**
382 * @test
383 * @todo: Test with alias
384 */
385 public function fromQuotesIdentifierAndDelegatesToConcreteQueryBuilder()
386 {
387 $this->connection->quoteIdentifier('aTable')
388 ->shouldBeCalled()
389 ->willReturnArgument(0);
390 $this->concreteQueryBuilder->from(Argument::exact('aTable'), Argument::cetera())
391 ->shouldBeCalled()
392 ->willReturn($this->subject);
393
394 $this->subject->from('aTable');
395 }
396
397 /**
398 * @test
399 */
400 public function joinQuotesIdentifiersAndDelegatesToConcreteQueryBuilder()
401 {
402 $this->connection->quoteIdentifier('fromAlias')
403 ->shouldBeCalled()
404 ->willReturnArgument(0);
405 $this->connection->quoteIdentifier('join')
406 ->shouldBeCalled()
407 ->willReturnArgument(0);
408 $this->connection->quoteIdentifier('alias')
409 ->shouldBeCalled()
410 ->willReturnArgument(0);
411 $this->concreteQueryBuilder->innerJoin('fromAlias', 'join', 'alias', null)
412 ->shouldBeCalled()
413 ->willReturn($this->subject);
414
415 $this->subject->join('fromAlias', 'join', 'alias');
416 }
417
418 /**
419 * @test
420 */
421 public function innerJoinQuotesIdentifiersAndDelegatesToConcreteQueryBuilder()
422 {
423 $this->connection->quoteIdentifier('fromAlias')
424 ->shouldBeCalled()
425 ->willReturnArgument(0);
426 $this->connection->quoteIdentifier('join')
427 ->shouldBeCalled()
428 ->willReturnArgument(0);
429 $this->connection->quoteIdentifier('alias')
430 ->shouldBeCalled()
431 ->willReturnArgument(0);
432 $this->concreteQueryBuilder->innerJoin('fromAlias', 'join', 'alias', null)
433 ->shouldBeCalled()
434 ->willReturn($this->subject);
435
436 $this->subject->innerJoin('fromAlias', 'join', 'alias');
437 }
438
439 /**
440 * @test
441 */
442 public function leftJoinQuotesIdentifiersAndDelegatesToConcreteQueryBuilder()
443 {
444 $this->connection->quoteIdentifier('fromAlias')
445 ->shouldBeCalled()
446 ->willReturnArgument(0);
447 $this->connection->quoteIdentifier('join')
448 ->shouldBeCalled()
449 ->willReturnArgument(0);
450 $this->connection->quoteIdentifier('alias')
451 ->shouldBeCalled()
452 ->willReturnArgument(0);
453 $this->concreteQueryBuilder->leftJoin('fromAlias', 'join', 'alias', null)
454 ->shouldBeCalled()
455 ->willReturn($this->subject);
456
457 $this->subject->leftJoin('fromAlias', 'join', 'alias');
458 }
459
460 /**
461 * @test
462 */
463 public function rightJoinQuotesIdentifiersAndDelegatesToConcreteQueryBuilder()
464 {
465 $this->connection->quoteIdentifier('fromAlias')
466 ->shouldBeCalled()
467 ->willReturnArgument(0);
468 $this->connection->quoteIdentifier('join')
469 ->shouldBeCalled()
470 ->willReturnArgument(0);
471 $this->connection->quoteIdentifier('alias')
472 ->shouldBeCalled()
473 ->willReturnArgument(0);
474 $this->concreteQueryBuilder->rightJoin('fromAlias', 'join', 'alias', null)
475 ->shouldBeCalled()
476 ->willReturn($this->subject);
477
478 $this->subject->rightJoin('fromAlias', 'join', 'alias');
479 }
480
481 /**
482 * @test
483 */
484 public function setQuotesIdentifierAndDelegatesToConcreteQueryBuilder()
485 {
486 $this->connection->quoteIdentifier('aField')
487 ->shouldBeCalled()
488 ->willReturnArgument(0);
489 $this->concreteQueryBuilder->createNamedParameter('aValue', Argument::cetera())
490 ->shouldBeCalled()
491 ->willReturn(':dcValue1');
492 $this->concreteQueryBuilder->set('aField', ':dcValue1')
493 ->shouldBeCalled()
494 ->willReturn($this->subject);
495
496 $this->subject->set('aField', 'aValue');
497 }
498
499 /**
500 * @test
501 */
502 public function setWithoutNamedParameterQuotesIdentifierAndDelegatesToConcreteQueryBuilder()
503 {
504 $this->connection->quoteIdentifier('aField')
505 ->shouldBeCalled()
506 ->willReturnArgument(0);
507 $this->concreteQueryBuilder->createNamedParameter(Argument::cetera())->shouldNotBeCalled();
508 $this->concreteQueryBuilder->set('aField', 'aValue')
509 ->shouldBeCalled()
510 ->willReturn($this->subject);
511
512 $this->subject->set('aField', 'aValue', false);
513 }
514
515 /**
516 * @test
517 */
518 public function whereDelegatesToConcreteQueryBuilder()
519 {
520 $this->concreteQueryBuilder->where('uid=1', 'type=9')
521 ->shouldBeCalled()
522 ->willReturn($this->subject);
523
524 $this->subject->where('uid=1', 'type=9');
525 }
526
527 /**
528 * @test
529 */
530 public function andWhereDelegatesToConcreteQueryBuilder()
531 {
532 $this->concreteQueryBuilder->andWhere('uid=1', 'type=9')
533 ->shouldBeCalled()
534 ->willReturn($this->subject);
535
536 $this->subject->andWhere('uid=1', 'type=9');
537 }
538
539 /**
540 * @test
541 */
542 public function orWhereDelegatesToConcreteQueryBuilder()
543 {
544 $this->concreteQueryBuilder->orWhere('uid=1', 'type=9')
545 ->shouldBeCalled()
546 ->willReturn($this->subject);
547
548 $this->subject->orWhere('uid=1', 'type=9');
549 }
550
551 /**
552 * @test
553 */
554 public function groupByQuotesIdentifierAndDelegatesToConcreteQueryBuilder()
555 {
556 $this->connection->quoteIdentifiers(['aField', 'anotherField'])
557 ->shouldBeCalled()
558 ->willReturnArgument(0);
559 $this->concreteQueryBuilder->groupBy('aField', 'anotherField')
560 ->shouldBeCalled()
561 ->willReturn($this->subject);
562
563 $this->subject->groupBy('aField', 'anotherField');
564 }
565
566 /**
567 * @test
568 */
569 public function addGroupByQuotesIdentifierAndDelegatesToConcreteQueryBuilder()
570 {
571 $this->connection->quoteIdentifiers(['aField', 'anotherField'])
572 ->shouldBeCalled()
573 ->willReturnArgument(0);
574 $this->concreteQueryBuilder->addGroupBy('aField', 'anotherField')
575 ->shouldBeCalled()
576 ->willReturn($this->subject);
577
578 $this->subject->addGroupBy('aField', 'anotherField');
579 }
580
581 /**
582 * @test
583 */
584 public function setValueQuotesIdentifierAndDelegatesToConcreteQueryBuilder()
585 {
586 $this->connection->quoteIdentifier('aField')
587 ->shouldBeCalled()
588 ->willReturnArgument(0);
589 $this->concreteQueryBuilder->createNamedParameter('aValue', Argument::cetera())
590 ->shouldBeCalled()
591 ->willReturn(':dcValue1');
592 $this->concreteQueryBuilder->setValue('aField', ':dcValue1')
593 ->shouldBeCalled()
594 ->willReturn($this->subject);
595
596 $this->subject->setValue('aField', 'aValue');
597 }
598
599 /**
600 * @test
601 */
602 public function setValueWithoudNamedParameterQuotesIdentifierAndDelegatesToConcreteQueryBuilder()
603 {
604 $this->connection->quoteIdentifier('aField')
605 ->shouldBeCalled()
606 ->willReturnArgument(0);
607 $this->concreteQueryBuilder->setValue('aField', 'aValue')
608 ->shouldBeCalled()
609 ->willReturn($this->subject);
610
611 $this->subject->setValue('aField', 'aValue', false);
612 }
613
614 /**
615 * @test
616 */
617 public function valuesQuotesIdentifiersAndDelegatesToConcreteQueryBuilder()
618 {
619 $this->connection->quoteColumnValuePairs(['aField' => ':dcValue1', 'aValue' => ':dcValue2'])
620 ->shouldBeCalled()
621 ->willReturnArgument(0);
622 $this->concreteQueryBuilder->createNamedParameter(1, Argument::cetera())
623 ->shouldBeCalled()
624 ->willReturn(':dcValue1');
625 $this->concreteQueryBuilder->createNamedParameter(2, Argument::cetera())
626 ->shouldBeCalled()
627 ->willReturn(':dcValue2');
628 $this->concreteQueryBuilder->values(['aField' => ':dcValue1', 'aValue' => ':dcValue2'])
629 ->shouldBeCalled()
630 ->willReturn($this->subject);
631
632 $this->subject->values(['aField' => 1, 'aValue' => 2]);
633 }
634
635 /**
636 * @test
637 */
638 public function valuesWithoutNamedParametersQuotesIdentifiersAndDelegatesToConcreteQueryBuilder()
639 {
640 $this->connection->quoteColumnValuePairs(['aField' => 1, 'aValue' => 2])
641 ->shouldBeCalled()
642 ->willReturnArgument(0);
643 $this->concreteQueryBuilder->values(['aField' => 1, 'aValue' => 2])
644 ->shouldBeCalled()
645 ->willReturn($this->subject);
646
647 $this->subject->values(['aField' => 1, 'aValue' => 2], false);
648 }
649
650 /**
651 * @test
652 */
653 public function havingDelegatesToConcreteQueryBuilder()
654 {
655 $this->concreteQueryBuilder->having('uid=1', 'type=9')
656 ->shouldBeCalled()
657 ->willReturn($this->subject);
658
659 $this->subject->having('uid=1', 'type=9');
660 }
661
662 /**
663 * @test
664 */
665 public function andHavingDelegatesToConcreteQueryBuilder()
666 {
667 $this->concreteQueryBuilder->andHaving('uid=1', 'type=9')
668 ->shouldBeCalled()
669 ->willReturn($this->subject);
670
671 $this->subject->andHaving('uid=1', 'type=9');
672 }
673
674 /**
675 * @test
676 */
677 public function orHavingDelegatesToConcreteQueryBuilder()
678 {
679 $this->concreteQueryBuilder->orHaving('uid=1', 'type=9')
680 ->shouldBeCalled()
681 ->willReturn($this->subject);
682
683 $this->subject->orHaving('uid=1', 'type=9');
684 }
685
686 /**
687 * @test
688 */
689 public function orderByQuotesIdentifierAndDelegatesToConcreteQueryBuilder()
690 {
691 $this->connection->quoteIdentifier('aField')
692 ->shouldBeCalled()
693 ->willReturnArgument(0);
694 $this->concreteQueryBuilder->orderBy('aField', null)
695 ->shouldBeCalled()
696 ->willReturn($this->subject);
697
698 $this->subject->orderBy('aField');
699 }
700
701 /**
702 * @test
703 */
704 public function addOrderByQuotesIdentifierAndDelegatesToConcreteQueryBuilder()
705 {
706 $this->connection->quoteIdentifier('aField')
707 ->shouldBeCalled()
708 ->willReturnArgument(0);
709 $this->concreteQueryBuilder->addOrderBy('aField', 'DESC')
710 ->shouldBeCalled()
711 ->willReturn($this->subject);
712
713 $this->subject->addOrderBy('aField', 'DESC');
714 }
715
716 /**
717 * @test
718 */
719 public function getQueryPartDelegatesToConcreteQueryBuilder()
720 {
721 $this->concreteQueryBuilder->getQueryPart('from')
722 ->shouldBeCalled()
723 ->willReturn('aTable');
724
725 $this->subject->getQueryPart('from');
726 }
727
728 /**
729 * @test
730 */
731 public function getQueryPartsDelegatesToConcreteQueryBuilder()
732 {
733 $this->concreteQueryBuilder->getQueryParts()
734 ->shouldBeCalled()
735 ->willReturn([]);
736
737 $this->subject->getQueryParts();
738 }
739
740 /**
741 * @test
742 */
743 public function resetQueryPartsDelegatesToConcreteQueryBuilder()
744 {
745 $this->concreteQueryBuilder->resetQueryParts(['select', 'from'])
746 ->shouldBeCalled()
747 ->willReturn($this->subject);
748
749 $this->subject->resetQueryParts(['select', 'from']);
750 }
751
752 /**
753 * @test
754 */
755 public function resetQueryPartDelegatesToConcreteQueryBuilder()
756 {
757 $this->concreteQueryBuilder->resetQueryPart('select')
758 ->shouldBeCalled()
759 ->willReturn($this->subject);
760
761 $this->subject->resetQueryPart('select');
762 }
763
764 /**
765 * @test
766 */
767 public function createNamedParameterDelegatesToConcreteQueryBuilder()
768 {
769 $this->concreteQueryBuilder->createNamedParameter(5, Argument::cetera())
770 ->shouldBeCalled()
771 ->willReturn(':dcValue1');
772
773 $this->subject->createNamedParameter(5);
774 }
775
776 /**
777 * @test
778 */
779 public function createPositionalParameterDelegatesToConcreteQueryBuilder()
780 {
781 $this->concreteQueryBuilder->createPositionalParameter(5, Argument::cetera())
782 ->shouldBeCalled()
783 ->willReturn('?');
784
785 $this->subject->createPositionalParameter(5);
786 }
787
788 /**
789 * @test
790 */
791 public function queryRestrictionsAreAddedForSelectOnExecute()
792 {
793 $GLOBALS['TCA']['pages']['ctrl'] = [
794 'tstamp' => 'tstamp',
795 'versioningWS' => true,
796 'delete' => 'deleted',
797 'crdate' => 'crdate',
798 'enablecolumns' => [
799 'disabled' => 'hidden',
800 ],
801 ];
802
803 $this->connection->quoteIdentifier(Argument::cetera())
804 ->willReturnArgument(0);
805 $this->connection->quoteIdentifiers(Argument::cetera())
806 ->willReturnArgument(0);
807
808 $connectionBuilder = GeneralUtility::makeInstance(
809 \Doctrine\DBAL\Query\QueryBuilder::class,
810 $this->connection->reveal()
811 );
812
813 $expressionBuilder = GeneralUtility::makeInstance(ExpressionBuilder::class, $this->connection->reveal());
814 $this->connection->getExpressionBuilder()
815 ->willReturn($expressionBuilder);
816
817 $subject = GeneralUtility::makeInstance(
818 QueryBuilder::class,
819 $this->connection->reveal(),
820 null,
821 $connectionBuilder
822 );
823
824 $subject->select('*')
825 ->from('pages')
826 ->where('uid=1');
827
828 $expectedSQL = 'SELECT * FROM pages WHERE (uid=1) AND ((pages.hidden = 0) AND (pages.deleted = 0))';
829 $this->connection->executeQuery($expectedSQL, Argument::cetera())
830 ->shouldBeCalled();
831
832 $subject->execute();
833 }
834
835 /**
836 * @test
837 */
838 public function queryRestrictionsAreAddedForCountOnExecute()
839 {
840 $GLOBALS['TCA']['pages']['ctrl'] = [
841 'tstamp' => 'tstamp',
842 'versioningWS' => true,
843 'delete' => 'deleted',
844 'crdate' => 'crdate',
845 'enablecolumns' => [
846 'disabled' => 'hidden',
847 ],
848 ];
849
850 $this->connection->quoteIdentifier(Argument::cetera())
851 ->willReturnArgument(0);
852 $this->connection->quoteIdentifiers(Argument::cetera())
853 ->willReturnArgument(0);
854
855 $connectionBuilder = GeneralUtility::makeInstance(
856 \Doctrine\DBAL\Query\QueryBuilder::class,
857 $this->connection->reveal()
858 );
859
860 $expressionBuilder = GeneralUtility::makeInstance(ExpressionBuilder::class, $this->connection->reveal());
861 $this->connection->getExpressionBuilder()
862 ->willReturn($expressionBuilder);
863
864 $subject = GeneralUtility::makeInstance(
865 QueryBuilder::class,
866 $this->connection->reveal(),
867 null,
868 $connectionBuilder
869 );
870
871 $subject->count('uid')
872 ->from('pages')
873 ->where('uid=1');
874
875 $expectedSQL = 'SELECT COUNT(uid) FROM pages WHERE (uid=1) AND ((pages.hidden = 0) AND (pages.deleted = 0))';
876 $this->connection->executeQuery($expectedSQL, Argument::cetera())
877 ->shouldBeCalled();
878
879 $subject->execute();
880 }
881
882 /**
883 * @test
884 */
885 public function queryRestrictionsAreReevaluatedOnSettingsChangeForGetSQL()
886 {
887 $GLOBALS['TCA']['pages']['ctrl'] = [
888 'tstamp' => 'tstamp',
889 'versioningWS' => true,
890 'delete' => 'deleted',
891 'crdate' => 'crdate',
892 'enablecolumns' => [
893 'disabled' => 'hidden',
894 ],
895 ];
896
897 $this->connection->quoteIdentifier(Argument::cetera())
898 ->willReturnArgument(0);
899 $this->connection->quoteIdentifiers(Argument::cetera())
900 ->willReturnArgument(0);
901 $this->connection->getExpressionBuilder()
902 ->willReturn(GeneralUtility::makeInstance(ExpressionBuilder::class, $this->connection->reveal()));
903
904 $concreteQueryBuilder = GeneralUtility::makeInstance(
905 \Doctrine\DBAL\Query\QueryBuilder::class,
906 $this->connection->reveal()
907 );
908
909 $subject = GeneralUtility::makeInstance(
910 QueryBuilder::class,
911 $this->connection->reveal(),
912 null,
913 $concreteQueryBuilder
914 );
915
916 $subject->select('*')
917 ->from('pages')
918 ->where('uid=1');
919
920 $expectedSQL = 'SELECT * FROM pages WHERE (uid=1) AND ((pages.hidden = 0) AND (pages.deleted = 0))';
921 $this->assertSame($expectedSQL, $subject->getSQL());
922
923 $subject->getQueryContext()
924 ->setIgnoreEnableFields(true)
925 ->setIgnoredEnableFields(['disabled']);
926
927 $expectedSQL = 'SELECT * FROM pages WHERE (uid=1) AND (pages.deleted = 0)';
928 $this->assertSame($expectedSQL, $subject->getSQL());
929 }
930
931 /**
932 * @test
933 */
934 public function queryRestrictionsAreReevaluatedOnSettingsChangeForExecute()
935 {
936 $GLOBALS['TCA']['pages']['ctrl'] = [
937 'tstamp' => 'tstamp',
938 'versioningWS' => true,
939 'delete' => 'deleted',
940 'crdate' => 'crdate',
941 'enablecolumns' => [
942 'disabled' => 'hidden',
943 ],
944 ];
945
946 $this->connection->quoteIdentifier(Argument::cetera())
947 ->willReturnArgument(0);
948 $this->connection->quoteIdentifiers(Argument::cetera())
949 ->willReturnArgument(0);
950 $this->connection->getExpressionBuilder()
951 ->willReturn(GeneralUtility::makeInstance(ExpressionBuilder::class, $this->connection->reveal()));
952
953 $concreteQueryBuilder = GeneralUtility::makeInstance(
954 \Doctrine\DBAL\Query\QueryBuilder::class,
955 $this->connection->reveal()
956 );
957
958 $subject = GeneralUtility::makeInstance(
959 QueryBuilder::class,
960 $this->connection->reveal(),
961 null,
962 $concreteQueryBuilder
963 );
964
965 $subject->select('*')
966 ->from('pages')
967 ->where('uid=1');
968
969 $subject->getQueryContext()
970 ->setIgnoreEnableFields(true)
971 ->setIgnoredEnableFields(['disabled']);
972
973 $expectedSQL = 'SELECT * FROM pages WHERE (uid=1) AND (pages.deleted = 0)';
974 $this->connection->executeQuery($expectedSQL, Argument::cetera())
975 ->shouldBeCalled();
976
977 $subject->execute();
978
979 $subject->getQueryContext()
980 ->setIgnoreEnableFields(false);
981
982 $expectedSQL = 'SELECT * FROM pages WHERE (uid=1) AND ((pages.hidden = 0) AND (pages.deleted = 0))';
983 $this->connection->executeQuery($expectedSQL, Argument::cetera())
984 ->shouldBeCalled();
985
986 $subject->execute();
987 }
988 }