[TASK] Remove unneeded SingletonInstance resets in recent extbase tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Persistence / Generic / QueryTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic;
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\TestingFramework\Core\Unit\UnitTestCase;
18
19 /**
20 * Test case
21 */
22 class QueryTest extends UnitTestCase
23 {
24 /**
25 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Query|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface
26 */
27 protected $query;
28
29 /**
30 * @var \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
31 */
32 protected $querySettings;
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
36 */
37 protected $persistenceManager;
38
39 /**
40 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory
41 */
42 protected $dataMapFactory;
43
44 /**
45 * Sets up this test case
46 */
47 protected function setUp(): void
48 {
49 parent::setUp();
50 $this->query = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Query::class, ['getSelectorName'], ['someType']);
51 $this->querySettings = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface::class);
52 $this->query->_set('querySettings', $this->querySettings);
53 $this->persistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
54 $this->query->_set('persistenceManager', $this->persistenceManager);
55 $this->dataMapFactory = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class);
56 $this->query->_set('dataMapFactory', $this->dataMapFactory);
57 }
58
59 /**
60 * @test
61 */
62 public function executeReturnsQueryResultInstanceAndInjectsItself()
63 {
64 /** @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject $objectManager */
65 $objectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
66 $this->query->_set('objectManager', $objectManager);
67 $queryResult = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\QueryResult::class);
68 $objectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class, $this->query)->will($this->returnValue($queryResult));
69 $actualResult = $this->query->execute();
70 $this->assertSame($queryResult, $actualResult);
71 }
72
73 /**
74 * @test
75 */
76 public function executeReturnsRawObjectDataIfReturnRawQueryResultIsSet()
77 {
78 $this->persistenceManager->expects($this->once())->method('getObjectDataByQuery')->with($this->query)->will($this->returnValue('rawQueryResult'));
79 $expectedResult = 'rawQueryResult';
80 $actualResult = $this->query->execute(true);
81 $this->assertEquals($expectedResult, $actualResult);
82 }
83
84 /**
85 * @test
86 */
87 public function setLimitAcceptsOnlyIntegers()
88 {
89 $this->expectException(\InvalidArgumentException::class);
90 $this->expectExceptionCode(1245071870);
91 $this->query->setLimit(1.5);
92 }
93
94 /**
95 * @test
96 */
97 public function setLimitRejectsIntegersLessThanOne()
98 {
99 $this->expectException(\InvalidArgumentException::class);
100 $this->expectExceptionCode(1245071870);
101 $this->query->setLimit(0);
102 }
103
104 /**
105 * @test
106 */
107 public function setOffsetAcceptsOnlyIntegers()
108 {
109 $this->expectException(\InvalidArgumentException::class);
110 $this->expectExceptionCode(1245071872);
111 $this->query->setOffset(1.5);
112 }
113
114 /**
115 * @test
116 */
117 public function setOffsetRejectsIntegersLessThanZero()
118 {
119 $this->expectException(\InvalidArgumentException::class);
120 $this->expectExceptionCode(1245071872);
121 $this->query->setOffset(-1);
122 }
123
124 /**
125 * @return array
126 */
127 public function equalsForCaseSensitiveFalseLowercasesOperandProvider()
128 {
129 return [
130 'Polish alphabet' => ['name', 'ĄĆĘŁŃÓŚŹŻABCDEFGHIJKLMNOPRSTUWYZQXVąćęłńóśźżabcdefghijklmnoprstuwyzqxv', 'ąćęłńóśźżabcdefghijklmnoprstuwyzqxvąćęłńóśźżabcdefghijklmnoprstuwyzqxv'],
131 'German alphabet' => ['name', 'ßÜÖÄüöä', 'ßüöäüöä'],
132 'Greek alphabet' => ['name', 'Τάχιστη αλώπηξ βαφής ψημένη γη', 'τάχιστη αλώπηξ βαφής ψημένη γη'],
133 'Russian alphabet' => ['name', 'АВСТРАЛИЯавстралия', 'австралияавстралия']
134 ];
135 }
136
137 /**
138 * Checks if equals condition makes utf-8 argument lowercase correctly
139 *
140 * @test
141 * @dataProvider equalsForCaseSensitiveFalseLowercasesOperandProvider
142 * @param string $propertyName The name of the property to compare against
143 * @param mixed $operand The value to compare with
144 * @param string $expectedOperand
145 */
146 public function equalsForCaseSensitiveFalseLowercasesOperand($propertyName, $operand, $expectedOperand)
147 {
148 /** @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject $objectManager */
149 $objectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
150 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Qom\DynamicOperandInterface $dynamicOperand */
151 $dynamicOperand = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\PropertyValueInterface::class);
152 $objectManager->expects($this->any())->method('get')->will($this->returnValue($dynamicOperand));
153 /** @var $qomFactory \TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory */
154 $qomFactory = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory::class, ['comparison']);
155 $qomFactory->_set('objectManager', $objectManager);
156 $qomFactory->expects($this->once())->method('comparison')->with($this->anything(), $this->anything(), $expectedOperand);
157 $this->query->expects($this->any())->method('getSelectorName')->will($this->returnValue('someSelector'));
158 $this->query->_set('qomFactory', $qomFactory);
159 $this->query->equals($propertyName, $operand, false);
160 }
161 }