‪TYPO3CMS  11.5
QueryTest.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
20 use PHPUnit\Framework\MockObject\MockObject;
21 use Prophecy\PhpUnit\ProphecyTrait;
22 use Psr\Container\ContainerInterface;
34 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
35 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
36 
40 class ‪QueryTest extends UnitTestCase
41 {
42  use ProphecyTrait;
43 
47  protected ‪$query;
48 
52  protected ‪$querySettings;
53 
58 
62  protected ‪$dataMapFactory;
63 
67  protected ‪$container;
68 
72  protected function ‪setUp(): void
73  {
74  parent::setUp();
75  $this->query = $this->getAccessibleMock(Query::class, ['getSelectorName'], [], '', false);
76  $this->querySettings = $this->createMock(QuerySettingsInterface::class);
77  $this->query->_set('querySettings', $this->querySettings);
78  $this->persistenceManager = $this->createMock(PersistenceManagerInterface::class);
79  $this->query->_set('persistenceManager', $this->persistenceManager);
80  $this->dataMapFactory = $this->createMock(DataMapFactory::class);
81  $this->query->_set('dataMapFactory', $this->dataMapFactory);
82  $this->container = $this->createMock(ContainerInterface::class);
83  $this->query->_set('container', $this->container);
84  }
85 
90  {
91  $queryResult = $this->createMock(QueryResult::class);
92  $this->container->expects(self::once())->method('has')->with(QueryResultInterface::class)->willReturn(true);
93  $this->container->expects(self::once())->method('get')->with(QueryResultInterface::class)->willReturn($queryResult);
94  $actualResult = $this->query->execute();
95  self::assertSame($queryResult, $actualResult);
96  }
97 
102  {
103  $this->persistenceManager->expects(self::once())->method('getObjectDataByQuery')->with($this->query)->willReturn('rawQueryResult');
104  $expectedResult = 'rawQueryResult';
105  $actualResult = $this->query->execute(true);
106  self::assertEquals($expectedResult, $actualResult);
107  }
108 
112  public function ‪setLimitAcceptsOnlyIntegers(): void
113  {
114  $this->expectException(\InvalidArgumentException::class);
115  $this->expectExceptionCode(1245071870);
116  $this->query->setLimit(1.5);
117  }
118 
122  public function ‪setLimitRejectsIntegersLessThanOne(): void
123  {
124  $this->expectException(\InvalidArgumentException::class);
125  $this->expectExceptionCode(1245071870);
126  $this->query->setLimit(0);
127  }
128 
132  public function ‪setOffsetAcceptsOnlyIntegers(): void
133  {
134  $this->expectException(\InvalidArgumentException::class);
135  $this->expectExceptionCode(1245071872);
136  $this->query->setOffset(1.5);
137  }
138 
142  public function ‪setOffsetRejectsIntegersLessThanZero(): void
143  {
144  $this->expectException(\InvalidArgumentException::class);
145  $this->expectExceptionCode(1245071872);
146  $this->query->setOffset(-1);
147  }
148 
153  {
154  return [
155  'Polish alphabet' => ['name', 'ĄĆĘŁŃÓŚŹŻABCDEFGHIJKLMNOPRSTUWYZQXVąćęłńóśźżabcdefghijklmnoprstuwyzqxv', 'ąćęłńóśźżabcdefghijklmnoprstuwyzqxvąćęłńóśźżabcdefghijklmnoprstuwyzqxv'],
156  'German alphabet' => ['name', 'ßÜÖÄüöä', 'ßüöäüöä'],
157  'Greek alphabet' => ['name', 'Τάχιστη αλώπηξ βαφής ψημένη γη', 'τάχιστη αλώπηξ βαφής ψημένη γη'],
158  'Russian alphabet' => ['name', 'АВСТРАЛИЯавстралия', 'австралияавстралия'],
159  ];
160  }
161 
171  public function ‪equalsForCaseSensitiveFalseLowercasesOperand(string $propertyName, $operand, string $expectedOperand): void
172  {
173  $qomFactory = $this->getAccessibleMock(QueryObjectModelFactory::class, ['comparison']);
174  $qomFactory->expects(self::once())->method('comparison')->with(self::anything(), self::anything(), $expectedOperand);
175  $this->query->method('getSelectorName')->willReturn('someSelector');
176  $this->query->_set('qomFactory', $qomFactory);
177  $this->query->equals($propertyName, $operand, false);
178  }
179 
185  public function ‪logicalAndSupportsASingleConstraint(): void
186  {
187  $subject = new ‪Query(
188  $this->prophesize(DataMapFactory::class)->reveal(),
189  $this->prophesize(PersistenceManagerInterface::class)->reveal(),
191  $this->prophesize(ContainerInterface::class)->reveal()
192  );
193 
194  $constraint1 = new ‪Comparison(new ‪PropertyValue('propertyName1'), '=', 'value1');
195 
196  $logicalAnd = $subject->logicalAnd($constraint1);
197  self::assertSame($constraint1, $logicalAnd);
198  }
199 
204  {
205  $subject = new ‪Query(
206  $this->prophesize(DataMapFactory::class)->reveal(),
207  $this->prophesize(PersistenceManagerInterface::class)->reveal(),
209  $this->prophesize(ContainerInterface::class)->reveal()
210  );
211 
212  $constraint1 = new ‪Comparison(new ‪PropertyValue('propertyName1'), '=', 'value1');
213  $constraint2 = new ‪Comparison(new ‪PropertyValue('propertyName2'), '=', 'value2');
214 
215  $logicalAnd = $subject->logicalAnd([$constraint1, $constraint2]);
216  self::assertEquals(
217  new ‪LogicalAnd($constraint1, $constraint2),
218  $logicalAnd
219  );
220  }
221 
226  {
227  $subject = new ‪Query(
228  $this->prophesize(DataMapFactory::class)->reveal(),
229  $this->prophesize(PersistenceManagerInterface::class)->reveal(),
231  $this->prophesize(ContainerInterface::class)->reveal()
232  );
233 
234  $constraint1 = new ‪Comparison(new ‪PropertyValue('propertyName1'), '=', 'value1');
235  $constraint2 = new ‪Comparison(new ‪PropertyValue('propertyName2'), '=', 'value2');
236  $constraint3 = new ‪Comparison(new ‪PropertyValue('propertyName3'), '=', 'value3');
237 
238  $logicalAnd = $subject->logicalAnd($constraint1, $constraint2, $constraint3);
239  self::assertEquals(
240  new ‪LogicalAnd(new ‪LogicalAnd($constraint1, $constraint2), $constraint3),
241  $logicalAnd
242  );
243  }
244 
249  {
250  $subject = new ‪Query(
251  $this->prophesize(DataMapFactory::class)->reveal(),
252  $this->prophesize(PersistenceManagerInterface::class)->reveal(),
254  $this->prophesize(ContainerInterface::class)->reveal()
255  );
256 
257  $constraint1 = new ‪Comparison(new ‪PropertyValue('propertyName1'), '=', 'value1');
258  $constraint2 = new ‪Comparison(new ‪PropertyValue('propertyName2'), '=', 'value2');
259  $constraint3 = new ‪Comparison(new ‪PropertyValue('propertyName3'), '=', 'value3');
260  $constraint4 = new ‪Comparison(new ‪PropertyValue('propertyName4'), '=', 'value4');
261 
262  $logicalAnd = $subject->logicalAnd([$constraint1, $constraint2], $constraint3, $constraint4);
263 
264  self::assertEquals(
265  new ‪LogicalAnd(new ‪LogicalAnd(new ‪LogicalAnd($constraint1, $constraint2), $constraint3), $constraint4),
266  $logicalAnd
267  );
268  }
269 
275  public function ‪logicalOrSupportsASingleConstraint(): void
276  {
277  $subject = new ‪Query(
278  $this->prophesize(DataMapFactory::class)->reveal(),
279  $this->prophesize(PersistenceManagerInterface::class)->reveal(),
281  $this->prophesize(ContainerInterface::class)->reveal()
282  );
283 
284  $constraint1 = new ‪Comparison(new ‪PropertyValue('propertyName1'), '=', 'value1');
285 
286  $logicalOr = $subject->logicalOr($constraint1);
287  self::assertSame($constraint1, $logicalOr);
288  }
289 
293  public function ‪logicalOrSupportsMultipleConstraintsAsArray(): void
294  {
295  $subject = new ‪Query(
296  $this->prophesize(DataMapFactory::class)->reveal(),
297  $this->prophesize(PersistenceManagerInterface::class)->reveal(),
299  $this->prophesize(ContainerInterface::class)->reveal()
300  );
301 
302  $constraint1 = new ‪Comparison(new ‪PropertyValue('propertyName1'), '=', 'value1');
303  $constraint2 = new ‪Comparison(new ‪PropertyValue('propertyName2'), '=', 'value2');
304 
305  $logicalOr = $subject->logicalOr([$constraint1, $constraint2]);
306  self::assertEquals(
307  new ‪LogicalOr($constraint1, $constraint2),
308  $logicalOr
309  );
310  }
311 
316  {
317  $subject = new ‪Query(
318  $this->prophesize(DataMapFactory::class)->reveal(),
319  $this->prophesize(PersistenceManagerInterface::class)->reveal(),
321  $this->prophesize(ContainerInterface::class)->reveal()
322  );
323 
324  $constraint1 = new ‪Comparison(new ‪PropertyValue('propertyName1'), '=', 'value1');
325  $constraint2 = new ‪Comparison(new ‪PropertyValue('propertyName2'), '=', 'value2');
326  $constraint3 = new ‪Comparison(new ‪PropertyValue('propertyName3'), '=', 'value3');
327 
328  $logicalOr = $subject->logicalOr($constraint1, $constraint2, $constraint3);
329  self::assertEquals(
330  new ‪LogicalOr(new ‪LogicalOr($constraint1, $constraint2), $constraint3),
331  $logicalOr
332  );
333  }
334 
339  {
340  $subject = new ‪Query(
341  $this->prophesize(DataMapFactory::class)->reveal(),
342  $this->prophesize(PersistenceManagerInterface::class)->reveal(),
344  $this->prophesize(ContainerInterface::class)->reveal()
345  );
346 
347  $constraint1 = new ‪Comparison(new ‪PropertyValue('propertyName1'), '=', 'value1');
348  $constraint2 = new ‪Comparison(new ‪PropertyValue('propertyName2'), '=', 'value2');
349  $constraint3 = new ‪Comparison(new ‪PropertyValue('propertyName3'), '=', 'value3');
350  $constraint4 = new ‪Comparison(new ‪PropertyValue('propertyName4'), '=', 'value4');
351 
352  $logicalOr = $subject->logicalOr([$constraint1, $constraint2], $constraint3, $constraint4);
353 
354  self::assertEquals(
355  new ‪LogicalOr(new ‪LogicalOr(new ‪LogicalOr($constraint1, $constraint2), $constraint3), $constraint4),
356  $logicalOr
357  );
358  }
359 }
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\PropertyValue
Definition: PropertyValue.php:32
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\setOffsetAcceptsOnlyIntegers
‪setOffsetAcceptsOnlyIntegers()
Definition: QueryTest.php:126
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:22
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\logicalAndSupportsMultipleConstraintsAsMethodArguments
‪logicalAndSupportsMultipleConstraintsAsMethodArguments()
Definition: QueryTest.php:219
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\equalsForCaseSensitiveFalseLowercasesOperand
‪equalsForCaseSensitiveFalseLowercasesOperand(string $propertyName, $operand, string $expectedOperand)
Definition: QueryTest.php:165
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\$query
‪Query MockObject AccessibleObjectInterface $query
Definition: QueryTest.php:45
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory
Definition: QueryObjectModelFactory.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\logicalAndSupportsASingleConstraint
‪logicalAndSupportsASingleConstraint()
Definition: QueryTest.php:179
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\$querySettings
‪QuerySettingsInterface $querySettings
Definition: QueryTest.php:49
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\setLimitAcceptsOnlyIntegers
‪setLimitAcceptsOnlyIntegers()
Definition: QueryTest.php:106
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\logicalOrSupportsMultipleConstraintsWithArrayAsFirstArgumentAndFurtherConstraintArguments
‪logicalOrSupportsMultipleConstraintsWithArrayAsFirstArgumentAndFurtherConstraintArguments()
Definition: QueryTest.php:332
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory
Definition: DataMapFactory.php:39
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\logicalOrSupportsMultipleConstraintsAsMethodArguments
‪logicalOrSupportsMultipleConstraintsAsMethodArguments()
Definition: QueryTest.php:309
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\logicalOrSupportsASingleConstraint
‪logicalOrSupportsASingleConstraint()
Definition: QueryTest.php:269
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\logicalAndSupportsMultipleConstraintsWithArrayAsFirstArgumentAndFurtherConstraintArguments
‪logicalAndSupportsMultipleConstraintsWithArrayAsFirstArgumentAndFurtherConstraintArguments()
Definition: QueryTest.php:242
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\setLimitRejectsIntegersLessThanOne
‪setLimitRejectsIntegersLessThanOne()
Definition: QueryTest.php:116
‪TYPO3\CMS\Extbase\Persistence\QueryResultInterface
Definition: QueryResultInterface.php:22
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic
Definition: BackendTest.php:18
‪TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
Definition: QuerySettingsInterface.php:22
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\logicalAndSupportsMultipleConstraintsAsArray
‪logicalAndSupportsMultipleConstraintsAsArray()
Definition: QueryTest.php:197
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\executeReturnsRawObjectDataIfReturnRawQueryResultIsSet
‪executeReturnsRawObjectDataIfReturnRawQueryResultIsSet()
Definition: QueryTest.php:95
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult
Definition: QueryResult.php:32
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\LogicalOr
Definition: LogicalOr.php:28
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest
Definition: QueryTest.php:41
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\executeReturnsQueryResultInstanceAndInjectsItself
‪executeReturnsQueryResultInstanceAndInjectsItself()
Definition: QueryTest.php:83
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\$dataMapFactory
‪DataMapFactory $dataMapFactory
Definition: QueryTest.php:57
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\equalsForCaseSensitiveFalseLowercasesOperandProvider
‪array equalsForCaseSensitiveFalseLowercasesOperandProvider()
Definition: QueryTest.php:146
‪TYPO3\CMS\Extbase\Persistence\Generic\Query
Definition: Query.php:43
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\Comparison
Definition: Comparison.php:65
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\logicalOrSupportsMultipleConstraintsAsArray
‪logicalOrSupportsMultipleConstraintsAsArray()
Definition: QueryTest.php:287
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\setOffsetRejectsIntegersLessThanZero
‪setOffsetRejectsIntegersLessThanZero()
Definition: QueryTest.php:136
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\setUp
‪setUp()
Definition: QueryTest.php:66
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\$container
‪ContainerInterface $container
Definition: QueryTest.php:61
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\$persistenceManager
‪PersistenceManagerInterface $persistenceManager
Definition: QueryTest.php:53
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\LogicalAnd
Definition: LogicalAnd.php:26