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