[FEATURE] move configuration of returnRawQueryResult
[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 * Copyright notice
6 *
7 * (c) 2009 Christopher Hlubek <hlubek@networkteam.com>
8 * (c) 2010 Bastian Waidelich <bastian@typo3.org>
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 class QueryTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
28
29 /**
30 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Query
31 */
32 protected $query;
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
36 */
37 protected $querySettings;
38
39 /**
40 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
41 */
42 protected $objectManager;
43
44 /**
45 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
46 */
47 protected $persistenceManager;
48
49 /**
50 * @var \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
51 */
52 protected $backend;
53
54 /**
55 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
56 */
57 protected $dataMapper;
58
59 /**
60 * Sets up this test case
61 *
62 * @return void
63 */
64 public function setUp() {
65 $this->objectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
66
67 $this->query = $this->getAccessibleMock('TYPO3\CMS\Extbase\Persistence\Generic\Query', array('dummy'), array('someType'));
68 $this->query->_set('objectManager', $this->objectManager);
69 $this->querySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QuerySettingsInterface');
70 $this->query->_set('querySettings', $this->querySettings);
71 $this->persistenceManager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\PersistenceManagerInterface');
72 $this->backend = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\BackendInterface');
73 $this->backend->expects($this->any())->method('getQomFactory')->will($this->returnValue(NULL));
74 $this->persistenceManager->expects($this->any())->method('getBackend')->will($this->returnValue($this->backend));
75 $this->query->_set('persistenceManager', $this->persistenceManager);
76 $this->dataMapper = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper');
77 $this->query->_set('dataMapper', $this->dataMapper);
78 }
79
80 /**
81 * @test
82 */
83 public function executeReturnsQueryResultInstanceAndInjectsItself() {
84 $queryResult = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QueryResult', array(), array(), '', FALSE);
85 $this->objectManager->expects($this->once())->method('get')->with('TYPO3\\CMS\\Extbase\\Persistence\\QueryResultInterface', $this->query)->will($this->returnValue($queryResult));
86 $actualResult = $this->query->execute();
87 $this->assertSame($queryResult, $actualResult);
88 }
89
90 /**
91 * @test
92 */
93 public function executeReturnsRawObjectDataIfRawQueryResultSettingIsTrue() {
94 $this->querySettings->expects($this->once())->method('getReturnRawQueryResult')->will($this->returnValue(TRUE));
95 $this->persistenceManager->expects($this->once())->method('getObjectDataByQuery')->with($this->query)->will($this->returnValue('rawQueryResult'));
96 $expectedResult = 'rawQueryResult';
97 $actualResult = $this->query->execute();
98 $this->assertEquals($expectedResult, $actualResult);
99 }
100
101 /**
102 * @test
103 */
104 public function executeReturnsRawObjectDataIfReturnRawQueryResultIsSet() {
105 $this->persistenceManager->expects($this->once())->method('getObjectDataByQuery')->with($this->query)->will($this->returnValue('rawQueryResult'));
106 $expectedResult = 'rawQueryResult';
107 $actualResult = $this->query->execute(TRUE);
108 $this->assertEquals($expectedResult, $actualResult);
109 }
110
111 /**
112 * @test
113 * @expectedException \InvalidArgumentException
114 */
115 public function setLimitAcceptsOnlyIntegers() {
116 $this->query->setLimit(1.5);
117 }
118
119 /**
120 * @test
121 * @expectedException \InvalidArgumentException
122 */
123 public function setLimitRejectsIntegersLessThanOne() {
124 $this->query->setLimit(0);
125 }
126
127 /**
128 * @test
129 * @expectedException \InvalidArgumentException
130 */
131 public function setOffsetAcceptsOnlyIntegers() {
132 $this->query->setOffset(1.5);
133 }
134
135 /**
136 * @test
137 * @expectedException \InvalidArgumentException
138 */
139 public function setOffsetRejectsIntegersLessThanZero() {
140 $this->query->setOffset(-1);
141 }
142
143 /**
144 * @return array
145 */
146 public function equalsForCaseSensitiveFalseLowercasesOperandProvider() {
147 return array(
148 'Polish alphabet' => array('name', 'ĄĆĘŁŃÓŚŹŻABCDEFGHIJKLMNOPRSTUWYZQXVąćęłńóśźżabcdefghijklmnoprstuwyzqxv', 'ąćęłńóśźżabcdefghijklmnoprstuwyzqxvąćęłńóśźżabcdefghijklmnoprstuwyzqxv'),
149 'German alphabet' => array('name', 'ßÜÖÄüöä', 'ßüöäüöä'),
150 'Greek alphabet' => array('name', 'Τάχιστη αλώπηξ βαφής ψημένη γη', 'τάχιστη αλώπηξ βαφής ψημένη γη'),
151 'Russian alphabet' => array('name', 'АВСТРАЛИЯавстралия', 'австралияавстралия')
152 );
153 }
154
155 /**
156 * Checks if equals condition makes utf-8 argument lowercase correctly
157 *
158 * @test
159 * @dataProvider equalsForCaseSensitiveFalseLowercasesOperandProvider
160 * @param string $propertyName The name of the property to compare against
161 * @param mixed $operand The value to compare with
162 * @param string $expectedOperand
163 */
164 public function equalsForCaseSensitiveFalseLowercasesOperand($propertyName, $operand, $expectedOperand) {
165 /** @var $qomFactory \TYPO3\CMS\Extbase\Persistence\Generic\Qom\QueryObjectModelFactory */
166 $qomFactory = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Qom\\QueryObjectModelFactory', array('comparison'));
167 $qomFactory->_set('objectManager', \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager'));
168 $qomFactory->expects($this->once())->method('comparison')->with($this->anything(), $this->anything(), $expectedOperand);
169 $this->query->_set('qomFactory', $qomFactory);
170 $this->query->equals($propertyName, $operand, FALSE);
171 }
172 }