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