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