‪TYPO3CMS  ‪main
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\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
22 use PHPUnit\Framework\MockObject\MockObject;
23 use Psr\Container\ContainerInterface;
36 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
37 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
38 
39 final class ‪QueryTest extends UnitTestCase
40 {
41  protected ‪Query&MockObject&AccessibleObjectInterface ‪$query;
45  protected ContainerInterface ‪$container;
46 
50  protected function ‪setUp(): void
51  {
52  parent::setUp();
53  $this->query = $this->getAccessibleMock(Query::class, ['getSelectorName'], [], '', false);
54  $this->querySettings = $this->createMock(QuerySettingsInterface::class);
55  $this->query->_set('querySettings', $this->querySettings);
56  $this->persistenceManager = $this->createMock(PersistenceManagerInterface::class);
57  $this->query->_set('persistenceManager', $this->persistenceManager);
58  $this->dataMapFactory = $this->createMock(DataMapFactory::class);
59  $this->query->_set('dataMapFactory', $this->dataMapFactory);
60  $this->container = $this->createMock(ContainerInterface::class);
61  $this->query->_set('container', $this->container);
62  }
63 
64  #[Test]
66  {
67  $queryResult = $this->createMock(QueryResult::class);
68  $this->container->expects(self::once())->method('get')->with(QueryResultInterface::class)->willReturn($queryResult);
69  $actualResult = $this->query->execute();
70  self::assertSame($queryResult, $actualResult);
71  }
72 
73  #[Test]
75  {
76  $expectedResult = [];
77  $this->persistenceManager->expects(self::once())->method('getObjectDataByQuery')->with($this->query)->willReturn($expectedResult);
78  $actualResult = $this->query->execute(true);
79  self::assertSame($expectedResult, $actualResult);
80  }
81 
82  #[Test]
83  public function ‪setLimitAcceptsOnlyIntegers(): void
84  {
85  $this->expectException(\InvalidArgumentException::class);
86  $this->expectExceptionCode(1245071870);
87  $this->query->setLimit(1.5);
88  }
89 
90  #[Test]
91  public function ‪setLimitRejectsIntegersLessThanOne(): void
92  {
93  $this->expectException(\InvalidArgumentException::class);
94  $this->expectExceptionCode(1245071870);
95  $this->query->setLimit(0);
96  }
97 
98  #[Test]
99  public function ‪setLimitSetsLimit(): void
100  {
101  $this->query->setLimit(15);
102 
103  self::assertSame(
104  15,
105  $this->query->getLimit()
106  );
107  }
108 
109  #[Test]
110  public function ‪unsetLimitWillResetLimitToNull(): void
111  {
112  $this->query->setLimit(15);
113  $this->query->unsetLimit();
114 
115  self::assertNull(
116  $this->query->getLimit()
117  );
118  }
119 
120  #[Test]
121  public function ‪setOffsetAcceptsOnlyIntegers(): void
122  {
123  $this->expectException(\InvalidArgumentException::class);
124  $this->expectExceptionCode(1245071872);
125  $this->query->setOffset(1.5);
126  }
127 
128  #[Test]
130  {
131  $this->expectException(\InvalidArgumentException::class);
132  $this->expectExceptionCode(1245071872);
133  $this->query->setOffset(-1);
134  }
135 
137  {
138  return [
139  'Polish alphabet' => ['name', 'ĄĆĘŁŃÓŚŹŻABCDEFGHIJKLMNOPRSTUWYZQXVąćęłńóśźżabcdefghijklmnoprstuwyzqxv', 'ąćęłńóśźżabcdefghijklmnoprstuwyzqxvąćęłńóśźżabcdefghijklmnoprstuwyzqxv'],
140  'German alphabet' => ['name', 'ßÜÖÄüöä', 'ßüöäüöä'],
141  'Greek alphabet' => ['name', 'Τάχιστη αλώπηξ βαφής ψημένη γη', 'τάχιστη αλώπηξ βαφής ψημένη γη'],
142  'Russian alphabet' => ['name', 'АВСТРАЛИЯавстралия', 'австралияавстралия'],
143  ];
144  }
145 
152  #[DataProvider('equalsForCaseSensitiveFalseLowercasesOperandProvider')]
153  #[Test]
154  public function ‪equalsForCaseSensitiveFalseLowercasesOperand(string $propertyName, $operand, string $expectedOperand): void
155  {
156  $qomFactory = $this->getAccessibleMock(QueryObjectModelFactory::class, ['comparison']);
157  $qomFactory->expects(self::once())->method('comparison')->with(self::anything(), self::anything(), $expectedOperand);
158  $this->query->method('getSelectorName')->willReturn('someSelector');
159  $this->query->_set('qomFactory', $qomFactory);
160  $this->query->equals($propertyName, $operand, false);
161  }
162 
163  #[Test]
165  {
166  $subject = new ‪Query(
167  $this->createMock(DataMapFactory::class),
168  $this->createMock(PersistenceManagerInterface::class),
170  $this->createMock(ContainerInterface::class)
171  );
172 
173  $constraint1 = new ‪Comparison(new ‪PropertyValue('propertyName1'), ‪QueryInterface::OPERATOR_EQUAL_TO, 'value1');
174  $constraint2 = new ‪Comparison(new ‪PropertyValue('propertyName2'), ‪QueryInterface::OPERATOR_EQUAL_TO, 'value2');
175  $constraint3 = new ‪Comparison(new ‪PropertyValue('propertyName3'), ‪QueryInterface::OPERATOR_EQUAL_TO, 'value3');
176 
177  $logicalAnd = $subject->logicalAnd($constraint1, $constraint2, $constraint3);
178  self::assertEquals(
179  new ‪LogicalAnd(new ‪LogicalAnd($constraint1, $constraint2), $constraint3),
180  $logicalAnd
181  );
182  }
183 
184  #[Test]
186  {
187  $subject = new ‪Query(
188  $this->createMock(DataMapFactory::class),
189  $this->createMock(PersistenceManagerInterface::class),
191  $this->createMock(ContainerInterface::class)
192  );
193 
194  $constraint1 = new ‪Comparison(new ‪PropertyValue('propertyName1'), ‪QueryInterface::OPERATOR_EQUAL_TO, 'value1');
195  $constraint2 = new ‪Comparison(new ‪PropertyValue('propertyName2'), ‪QueryInterface::OPERATOR_EQUAL_TO, 'value2');
196  $constraint3 = new ‪Comparison(new ‪PropertyValue('propertyName3'), ‪QueryInterface::OPERATOR_EQUAL_TO, 'value3');
197 
198  $logicalOr = $subject->logicalOr($constraint1, $constraint2, $constraint3);
199  self::assertEquals(
200  new ‪LogicalOr(new ‪LogicalOr($constraint1, $constraint2), $constraint3),
201  $logicalOr
202  );
203  }
204 }
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\PropertyValue
Definition: PropertyValue.php:34
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\setOffsetAcceptsOnlyIntegers
‪setOffsetAcceptsOnlyIntegers()
Definition: QueryTest.php:121
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:24
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\logicalAndSupportsMultipleConstraintsAsMethodArguments
‪logicalAndSupportsMultipleConstraintsAsMethodArguments()
Definition: QueryTest.php:164
‪TYPO3\CMS\Extbase\Persistence\QueryInterface
Definition: QueryInterface.php:30
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\equalsForCaseSensitiveFalseLowercasesOperand
‪equalsForCaseSensitiveFalseLowercasesOperand(string $propertyName, $operand, string $expectedOperand)
Definition: QueryTest.php:154
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\$query
‪Query &MockObject &AccessibleObjectInterface $query
Definition: QueryTest.php:41
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory
Definition: QueryObjectModelFactory.php:30
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\$querySettings
‪QuerySettingsInterface $querySettings
Definition: QueryTest.php:42
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\setLimitAcceptsOnlyIntegers
‪setLimitAcceptsOnlyIntegers()
Definition: QueryTest.php:83
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory
Definition: DataMapFactory.php:34
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\setLimitSetsLimit
‪setLimitSetsLimit()
Definition: QueryTest.php:99
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\logicalOrSupportsMultipleConstraintsAsMethodArguments
‪logicalOrSupportsMultipleConstraintsAsMethodArguments()
Definition: QueryTest.php:185
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\unsetLimitWillResetLimitToNull
‪unsetLimitWillResetLimitToNull()
Definition: QueryTest.php:110
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\setLimitRejectsIntegersLessThanOne
‪setLimitRejectsIntegersLessThanOne()
Definition: QueryTest.php:91
‪TYPO3\CMS\Extbase\Persistence\QueryResultInterface
Definition: QueryResultInterface.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic
Definition: BackendTest.php:18
‪TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
Definition: QuerySettingsInterface.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\executeReturnsRawObjectDataIfReturnRawQueryResultIsSet
‪executeReturnsRawObjectDataIfReturnRawQueryResultIsSet()
Definition: QueryTest.php:74
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\equalsForCaseSensitiveFalseLowercasesOperandProvider
‪static equalsForCaseSensitiveFalseLowercasesOperandProvider()
Definition: QueryTest.php:136
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\OPERATOR_EQUAL_TO
‪const OPERATOR_EQUAL_TO
Definition: QueryInterface.php:34
‪TYPO3\CMS\Extbase\Persistence\Generic\QueryResult
Definition: QueryResult.php:32
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\LogicalOr
Definition: LogicalOr.php:31
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest
Definition: QueryTest.php:40
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\executeReturnsQueryResultInstanceAndInjectsItself
‪executeReturnsQueryResultInstanceAndInjectsItself()
Definition: QueryTest.php:65
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\$dataMapFactory
‪DataMapFactory $dataMapFactory
Definition: QueryTest.php:44
‪TYPO3\CMS\Extbase\Persistence\Generic\Query
Definition: Query.php:41
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\Comparison
Definition: Comparison.php:68
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\setOffsetRejectsIntegersLessThanZero
‪setOffsetRejectsIntegersLessThanZero()
Definition: QueryTest.php:129
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\setUp
‪setUp()
Definition: QueryTest.php:50
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\$container
‪ContainerInterface $container
Definition: QueryTest.php:45
‪TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\QueryTest\$persistenceManager
‪PersistenceManagerInterface $persistenceManager
Definition: QueryTest.php:43
‪TYPO3\CMS\Extbase\Persistence\Generic\Qom\LogicalAnd
Definition: LogicalAnd.php:29