‪TYPO3CMS  10.4
QueryTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Prophecy\Argument;
32 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
33 
37 class ‪QueryTest extends UnitTestCase
38 {
42  protected ‪$query;
43 
47  protected ‪$querySettings;
48 
52  protected ‪$persistenceManager;
53 
57  protected ‪$dataMapFactory;
58 
62  protected function ‪setUp(): void
63  {
64  parent::setUp();
65  $this->query = $this->getAccessibleMock(Query::class, ['getSelectorName'], ['someType']);
66  $this->querySettings = $this->createMock(QuerySettingsInterface::class);
67  $this->query->_set('querySettings', $this->querySettings);
68  $this->persistenceManager = $this->createMock(PersistenceManagerInterface::class);
69  $this->query->_set('persistenceManager', $this->persistenceManager);
70  $this->dataMapFactory = $this->createMock(DataMapFactory::class);
71  $this->query->_set('dataMapFactory', $this->dataMapFactory);
72  }
73 
78  {
80  $objectManager = $this->createMock(ObjectManager::class);
81  $this->query->_set('objectManager', $objectManager);
82  $queryResult = $this->createMock(QueryResult::class);
83  $objectManager->expects(self::once())->method('get')->with(QueryResultInterface::class, $this->query)->willReturn($queryResult);
84  $actualResult = $this->query->execute();
85  self::assertSame($queryResult, $actualResult);
86  }
87 
92  {
93  $this->persistenceManager->expects(self::once())->method('getObjectDataByQuery')->with($this->query)->willReturn('rawQueryResult');
94  $expectedResult = 'rawQueryResult';
95  $actualResult = $this->query->execute(true);
96  self::assertEquals($expectedResult, $actualResult);
97  }
98 
102  public function ‪setLimitAcceptsOnlyIntegers()
103  {
104  $this->expectException(\InvalidArgumentException::class);
105  $this->expectExceptionCode(1245071870);
106  $this->query->setLimit(1.5);
107  }
108 
112  public function ‪setLimitRejectsIntegersLessThanOne()
113  {
114  $this->expectException(\InvalidArgumentException::class);
115  $this->expectExceptionCode(1245071870);
116  $this->query->setLimit(0);
117  }
118 
122  public function ‪setOffsetAcceptsOnlyIntegers()
123  {
124  $this->expectException(\InvalidArgumentException::class);
125  $this->expectExceptionCode(1245071872);
126  $this->query->setOffset(1.5);
127  }
128 
133  {
134  $this->expectException(\InvalidArgumentException::class);
135  $this->expectExceptionCode(1245071872);
136  $this->query->setOffset(-1);
137  }
138 
143  {
144  return [
145  'Polish alphabet' => ['name', 'ĄĆĘŁŃÓŚŹŻABCDEFGHIJKLMNOPRSTUWYZQXVąćęłńóśźżabcdefghijklmnoprstuwyzqxv', 'ąćęłńóśźżabcdefghijklmnoprstuwyzqxvąćęłńóśźżabcdefghijklmnoprstuwyzqxv'],
146  'German alphabet' => ['name', 'ßÜÖÄüöä', 'ßüöäüöä'],
147  'Greek alphabet' => ['name', 'Τάχιστη αλώπηξ βαφής ψημένη γη', 'τάχιστη αλώπηξ βαφής ψημένη γη'],
148  'Russian alphabet' => ['name', 'АВСТРАЛИЯавстралия', 'австралияавстралия']
149  ];
150  }
151 
161  public function ‪equalsForCaseSensitiveFalseLowercasesOperand($propertyName, $operand, $expectedOperand)
162  {
164  $objectManager = $this->createMock(ObjectManager::class);
166  $dynamicOperand = $this->createMock(PropertyValueInterface::class);
167  $objectManager->expects(self::any())->method('get')->willReturn($dynamicOperand);
169  $qomFactory = $this->getAccessibleMock(QueryObjectModelFactory::class, ['comparison']);
170  $qomFactory->_set('objectManager', $objectManager);
171  $qomFactory->expects(self::once())->method('comparison')->with(self::anything(), self::anything(), $expectedOperand);
172  $this->query->expects(self::any())->method('getSelectorName')->willReturn('someSelector');
173  $this->query->_set('qomFactory', $qomFactory);
174  $this->query->equals($propertyName, $operand, false);
175  }
176 
182  public function ‪logicalAndSupportsASingleConstraint(): void
183  {
184  $subject = $this->‪createQueryForLogicalAndTests();
185 
186  $constraint1 = new Comparison(new PropertyValue('propertyName1'), '=', 'value1');
187 
188  $logicalAnd = $subject->logicalAnd($constraint1);
189  self::assertSame($constraint1, $logicalAnd);
190  }
191 
196  {
197  $subject = $this->‪createQueryForLogicalAndTests();
198 
199  $constraint1 = new ‪Comparison(new ‪PropertyValue('propertyName1'), '=', 'value1');
200  $constraint2 = new ‪Comparison(new ‪PropertyValue('propertyName2'), '=', 'value2');
201 
202  $logicalAnd = $subject->logicalAnd([$constraint1, $constraint2]);
203  self::assertEquals(
204  new ‪LogicalAnd($constraint1, $constraint2),
205  $logicalAnd
206  );
207  }
208 
213  {
214  $subject = $this->‪createQueryForLogicalAndTests();
215 
216  $constraint1 = new ‪Comparison(new ‪PropertyValue('propertyName1'), '=', 'value1');
217  $constraint2 = new ‪Comparison(new ‪PropertyValue('propertyName2'), '=', 'value2');
218  $constraint3 = new ‪Comparison(new ‪PropertyValue('propertyName3'), '=', 'value3');
219 
220  $logicalAnd = $subject->logicalAnd($constraint1, $constraint2, $constraint3);
221  self::assertEquals(
222  new ‪LogicalAnd(new ‪LogicalAnd($constraint1, $constraint2), $constraint3),
223  $logicalAnd
224  );
225  }
226 
231  {
232  $subject = $this->‪createQueryForLogicalAndTests();
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  $constraint4 = new ‪Comparison(new ‪PropertyValue('propertyName4'), '=', 'value4');
238 
239  $logicalAnd = $subject->logicalAnd([$constraint1, $constraint2], $constraint3, $constraint4);
240 
241  self::assertEquals(
242  new ‪LogicalAnd(new ‪LogicalAnd(new ‪LogicalAnd($constraint1, $constraint2), $constraint3), $constraint4),
243  $logicalAnd
244  );
245  }
246 
247  private function ‪createQueryForLogicalAndTests(): ‪Query
248  {
249  $objectManager = $this->prophesize(ObjectManager::class);
250  $objectManager
251  ->get(Argument::exact(LogicalAnd::class), Argument::cetera())
252  ->will(
253  function (array $methodArguments) {
254  return new ‪LogicalAnd($methodArguments[1], $methodArguments[2]);
255  }
256  )
257  ;
258 
259  $qomFactory = new QueryObjectModelFactory();
260  $qomFactory->injectObjectManager($objectManager->reveal());
261 
262  ‪$query = new ‪Query('type');
263  ‪$query->‪injectQomFactory($qomFactory);
264 
265  return ‪$query;
266  }
267 
273  public function ‪logicalOrSupportsASingleConstraint(): void
274  {
275  $subject = $this->‪createQueryForLogicalOrTests();
276 
277  $constraint1 = new Comparison(new PropertyValue('propertyName1'), '=', 'value1');
278 
279  $logicalOr = $subject->logicalOr($constraint1);
280  self::assertSame($constraint1, $logicalOr);
281  }
282 
286  public function ‪logicalOrSupportsMultipleConstraintsAsArray(): void
287  {
288  $subject = $this->‪createQueryForLogicalOrTests();
289 
290  $constraint1 = new ‪Comparison(new ‪PropertyValue('propertyName1'), '=', 'value1');
291  $constraint2 = new ‪Comparison(new ‪PropertyValue('propertyName2'), '=', 'value2');
292 
293  $logicalOr = $subject->logicalOr([$constraint1, $constraint2]);
294  self::assertEquals(
295  new ‪LogicalOr($constraint1, $constraint2),
296  $logicalOr
297  );
298  }
299 
304  {
305  $subject = $this->‪createQueryForLogicalOrTests();
306 
307  $constraint1 = new ‪Comparison(new ‪PropertyValue('propertyName1'), '=', 'value1');
308  $constraint2 = new ‪Comparison(new ‪PropertyValue('propertyName2'), '=', 'value2');
309  $constraint3 = new ‪Comparison(new ‪PropertyValue('propertyName3'), '=', 'value3');
310 
311  $logicalOr = $subject->logicalOr($constraint1, $constraint2, $constraint3);
312  self::assertEquals(
313  new ‪LogicalOr(new ‪LogicalOr($constraint1, $constraint2), $constraint3),
314  $logicalOr
315  );
316  }
317 
322  {
323  $subject = $this->‪createQueryForLogicalOrTests();
324 
325  $constraint1 = new ‪Comparison(new ‪PropertyValue('propertyName1'), '=', 'value1');
326  $constraint2 = new ‪Comparison(new ‪PropertyValue('propertyName2'), '=', 'value2');
327  $constraint3 = new ‪Comparison(new ‪PropertyValue('propertyName3'), '=', 'value3');
328  $constraint4 = new ‪Comparison(new ‪PropertyValue('propertyName4'), '=', 'value4');
329 
330  $logicalOr = $subject->logicalOr([$constraint1, $constraint2], $constraint3, $constraint4);
331 
332  self::assertEquals(
333  new ‪LogicalOr(new ‪LogicalOr(new ‪LogicalOr($constraint1, $constraint2), $constraint3), $constraint4),
334  $logicalOr
335  );
336  }
337 
338  private function ‪createQueryForLogicalOrTests(): ‪Query
339  {
340  $objectManager = $this->prophesize(ObjectManager::class);
341  $objectManager
342  ->get(Argument::exact(LogicalOr::class), Argument::cetera())
343  ->will(
344  function (array $methodArguments) {
345  return new ‪LogicalOr($methodArguments[1], $methodArguments[2]);
346  }
347  )
348  ;
349 
350  $qomFactory = new QueryObjectModelFactory();
351  $qomFactory->injectObjectManager($objectManager->reveal());
352 
353  ‪$query = new ‪Query('type');
354  ‪$query->‪injectQomFactory($qomFactory);
355 
356  return ‪$query;
357  }
358 }
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\PropertyValue
Definition: PropertyValue.php:32
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\setOffsetAcceptsOnlyIntegers
‪setOffsetAcceptsOnlyIntegers()
Definition: QueryTest.php:118
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:22
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\logicalAndSupportsMultipleConstraintsAsMethodArguments
‪logicalAndSupportsMultipleConstraintsAsMethodArguments()
Definition: QueryTest.php:208
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\equalsForCaseSensitiveFalseLowercasesOperand
‪equalsForCaseSensitiveFalseLowercasesOperand($propertyName, $operand, $expectedOperand)
Definition: QueryTest.php:157
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\createQueryForLogicalAndTests
‪createQueryForLogicalAndTests()
Definition: QueryTest.php:243
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory
Definition: QueryObjectModelFactory.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\logicalAndSupportsASingleConstraint
‪logicalAndSupportsASingleConstraint()
Definition: QueryTest.php:178
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\setLimitAcceptsOnlyIntegers
‪setLimitAcceptsOnlyIntegers()
Definition: QueryTest.php:98
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\logicalOrSupportsMultipleConstraintsWithArrayAsFirstArgumentAndFurtherConstraintArguments
‪logicalOrSupportsMultipleConstraintsWithArrayAsFirstArgumentAndFurtherConstraintArguments()
Definition: QueryTest.php:317
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory
Definition: DataMapFactory.php:42
‪TYPO3\CMS\Extbase\Persistence\Generic\Query\injectQomFactory
‪injectQomFactory(QueryObjectModelFactory $qomFactory)
Definition: Query.php:141
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\PropertyValueInterface
Definition: PropertyValueInterface.php:30
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\logicalOrSupportsMultipleConstraintsAsMethodArguments
‪logicalOrSupportsMultipleConstraintsAsMethodArguments()
Definition: QueryTest.php:299
‪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:226
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\$persistenceManager
‪TYPO3 CMS Extbase Persistence PersistenceManagerInterface $persistenceManager
Definition: QueryTest.php:49
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\setLimitRejectsIntegersLessThanOne
‪setLimitRejectsIntegersLessThanOne()
Definition: QueryTest.php:108
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\$query
‪TYPO3 CMS Extbase Persistence Generic Query PHPUnit Framework MockObject MockObject TYPO3 TestingFramework Core AccessibleObjectInterface $query
Definition: QueryTest.php:41
‪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:191
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\executeReturnsRawObjectDataIfReturnRawQueryResultIsSet
‪executeReturnsRawObjectDataIfReturnRawQueryResultIsSet()
Definition: QueryTest.php:87
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\createQueryForLogicalOrTests
‪createQueryForLogicalOrTests()
Definition: QueryTest.php:334
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult
Definition: QueryResult.php:30
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\LogicalOr
Definition: LogicalOr.php:28
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest
Definition: QueryTest.php:38
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\executeReturnsQueryResultInstanceAndInjectsItself
‪executeReturnsQueryResultInstanceAndInjectsItself()
Definition: QueryTest.php:73
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\$querySettings
‪TYPO3 CMS Extbase Persistence Generic QuerySettingsInterface $querySettings
Definition: QueryTest.php:45
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\equalsForCaseSensitiveFalseLowercasesOperandProvider
‪array equalsForCaseSensitiveFalseLowercasesOperandProvider()
Definition: QueryTest.php:138
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\$dataMapFactory
‪TYPO3 CMS Extbase Persistence Generic Mapper DataMapFactory $dataMapFactory
Definition: QueryTest.php:53
‪TYPO3\CMS\Extbase\Persistence\Generic\Query
Definition: Query.php:38
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\Comparison
Definition: Comparison.php:65
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\logicalOrSupportsMultipleConstraintsAsArray
‪logicalOrSupportsMultipleConstraintsAsArray()
Definition: QueryTest.php:282
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\setOffsetRejectsIntegersLessThanZero
‪setOffsetRejectsIntegersLessThanZero()
Definition: QueryTest.php:128
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\setUp
‪setUp()
Definition: QueryTest.php:58
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:28
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\LogicalAnd
Definition: LogicalAnd.php:26