657efce77c79f8e50d4b49d0442c79c1cc9b4f53
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Persistence / Storage / Typo3DbBackend_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
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 class Tx_Extbase_Persistence_Storage_Typo3DbBackend_testcase extends Tx_Extbase_BaseTestCase {
29
30 /**
31 * This is the data provider for the statement generation with a basic comparison
32 *
33 * @return array An array of data
34 */
35 public function providerForBasicComparison() {
36 return array(
37 'equal' => array(
38 Tx_Extbase_Persistence_QueryInterface::OPERATOR_EQUAL_TO,
39 "SELECT table_name_from_selector.* FROM table_name_from_selector WHERE table_name_from_property.foo = 'baz'"
40 ),
41 'less' => array(
42 Tx_Extbase_Persistence_QueryInterface::OPERATOR_LESS_THAN,
43 "SELECT table_name_from_selector.* FROM table_name_from_selector WHERE table_name_from_property.foo < 'baz'"
44 ),
45 'less or equal' => array(
46 Tx_Extbase_Persistence_QueryInterface::OPERATOR_LESS_THAN_OR_EQUAL_TO,
47 "SELECT table_name_from_selector.* FROM table_name_from_selector WHERE table_name_from_property.foo <= 'baz'"
48 ),
49 'greater' => array(
50 Tx_Extbase_Persistence_QueryInterface::OPERATOR_GREATER_THAN,
51 "SELECT table_name_from_selector.* FROM table_name_from_selector WHERE table_name_from_property.foo > 'baz'"
52 ),
53 'greater or equal' => array(
54 Tx_Extbase_Persistence_QueryInterface::OPERATOR_GREATER_THAN_OR_EQUAL_TO,
55 "SELECT table_name_from_selector.* FROM table_name_from_selector WHERE table_name_from_property.foo >= 'baz'"
56 ),
57
58 );
59 }
60
61 /**
62 * @test
63 */
64 public function getStatementWorksWithMinimalisticQueryObjectModel() {
65 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
66 $mockSource->expects($this->any())->method('getSelectorName')->will($this->returnValue('selector_name'));
67 $mockSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('nodetype_name'));
68
69 $mockQueryObjectModel = $this->getMock('Tx_Extbase_Persistence_QOM_QueryObjectModel', array(), array(), '', FALSE);
70 $mockQueryObjectModel->expects($this->any())->method('getSource')->will($this->returnValue($mockSource));
71 $mockQueryObjectModel->expects($this->any())->method('getBoundVariableValues')->will($this->returnValue(array()));
72 $mockQueryObjectModel->expects($this->any())->method('getOrderings')->will($this->returnValue(array()));
73
74 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parseOrderings'), array(), '', FALSE);
75 $mockTypo3DbBackend->expects($this->any())->method('parseOrderings');
76
77 $parameters = array();
78 $resultingStatement = $mockTypo3DbBackend->getStatement($mockQueryObjectModel, $parameters);
79 $expectedStatement = 'SELECT selector_name.* FROM selector_name';
80 $this->assertEquals($expectedStatement, $resultingStatement);
81 }
82
83 /**
84 * @test
85 */
86 public function getStatementWorksWithBasicEqualsCondition() {
87 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
88 $mockSource->expects($this->any())->method('getSelectorName')->will($this->returnValue('selector_name'));
89 $mockSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('nodetype_name'));
90
91 $mockQueryObjectModel = $this->getMock('Tx_Extbase_Persistence_QOM_QueryObjectModel', array(), array(), '', FALSE);
92 $mockQueryObjectModel->expects($this->any())->method('getSource')->will($this->returnValue($mockSource));
93 $mockQueryObjectModel->expects($this->any())->method('getBoundVariableValues')->will($this->returnValue(array()));
94 $mockQueryObjectModel->expects($this->any())->method('getOrderings')->will($this->returnValue(array()));
95
96 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parseOrderings'), array(), '', FALSE);
97 $mockTypo3DbBackend->expects($this->any())->method('parseOrderings');
98
99 $parameters = array();
100 $resultingStatement = $mockTypo3DbBackend->getStatement($mockQueryObjectModel, $parameters);
101 $expectedStatement = 'SELECT selector_name.* FROM selector_name';
102 $this->assertEquals($expectedStatement, $resultingStatement);
103 }
104
105 /**
106 * @test
107 * @expectedException Tx_Extbase_Persistence_Storage_Exception_BadConstraint
108 */
109 public function countRowsWithStatementConstraintResultsInAnException() {
110 $mockStatementConstraint = $this->getMock('Tx_Extbase_Persistence_QOM_Statement', array(), array(), '', FALSE);
111
112 $mockQueryObjectModel = $this->getMock('Tx_Extbase_Persistence_QOM_QueryObjectModel', array('getConstraint'), array(), '', FALSE);
113 $mockQueryObjectModel->expects($this->once())->method('getConstraint')->will($this->returnValue($mockStatementConstraint));
114
115 $mockTypo3DbBackend = $this->getMock('Tx_Extbase_Persistence_Storage_Typo3DbBackend', array('dummy'), array(), '', FALSE);
116 $mockTypo3DbBackend->countRows($mockQueryObjectModel);
117 }
118
119 /**
120 * @test
121 */
122 public function joinStatementGenerationWorks() {
123 $mockLeftSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
124 $mockLeftSource->expects($this->any())->method('getSelectorName')->will($this->returnValue('left_selector_name'));
125 $mockLeftSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('left_nodetype_name'));
126
127 $mockRightSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
128 $mockRightSource->expects($this->any())->method('getSelectorName')->will($this->returnValue('right_selector_name'));
129 $mockRightSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('right_nodetype_name'));
130
131 $mockJoinCondition = $this->getMock('Tx_Extbase_Persistence_QOM_EquiJoinCondition', array('getSelector1Name', 'getSelector2Name', 'getProperty1Name', 'getProperty2Name'), array(), '', FALSE);
132 $mockJoinCondition->expects($this->any())->method('getSelector1Name')->will($this->returnValue('first_selector'));
133 $mockJoinCondition->expects($this->any())->method('getSelector2Name')->will($this->returnValue('second_selector'));
134 $mockJoinCondition->expects($this->any())->method('getProperty1Name')->will($this->returnValue('firstProperty'));
135 $mockJoinCondition->expects($this->any())->method('getProperty2Name')->will($this->returnValue('secondProperty'));
136
137 $mockJoin = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Join'), array('getLeft', 'getRight'), array(), '', FALSE);
138 $mockJoin->_set('joinCondition', $mockJoinCondition);
139 $mockJoin->_set('joinType', $mockJoinCondition);
140 $mockJoin->expects($this->any())->method('getLeft')->will($this->returnValue($mockLeftSource));
141 $mockJoin->expects($this->any())->method('getRight')->will($this->returnValue($mockRightSource));
142
143 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
144 $mockDataMapper->expects($this->any())
145 ->method('convertPropertyNameToColumnName')
146 ->with(
147 $this->logicalOr(
148 $this->equalTo('firstProperty'),
149 $this->equalTo('secondProperty')
150 )
151 )
152 ->will($this->returnValue('resulting_fieldname'));
153
154 $sql = array();
155 $parameters = array();
156 $mockQueryObjectModel = $this->getMock('Tx_Extbase_Persistence_QOM_QueryObjectModelInterface');
157 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserJoin'), array(), '', FALSE);
158 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
159 $mockTypo3DbBackend->_callRef('parseJoin', $mockQueryObjectModel, $mockJoin, $sql, $parameters);
160
161 $expecedSql = array(
162 'fields' => array('left_selector_name.*', 'right_selector_name.*'),
163 'tables' => array(
164 'left_selector_name LEFT JOIN right_selector_name',
165 'ON first_selector.resulting_fieldname = second_selector.resulting_fieldname'
166 )
167 );
168
169 $this->assertEquals($expecedSql, $sql);
170 }
171
172 /**
173 * @test
174 */
175 public function orderStatementGenerationWorks() {
176 $mockPropertyValue = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
177 $mockPropertyValue->expects($this->once())->method('getPropertyName')->will($this->returnValue('fooProperty'));
178 $mockPropertyValue->expects($this->once())->method('getSelectorName')->will($this->returnValue('tx_myext_tablenamefromproperty'));
179
180 $mockOrdering1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
181 $mockOrdering1->expects($this->once())->method('getOrder')->will($this->returnValue(Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_ORDER_ASCENDING));
182 $mockOrdering1->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue));
183 $orderings = array($mockOrdering1);
184
185 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array('getSelectorName', 'getNodeTypeName'), array(), '', FALSE);
186 $mockSource->expects($this->any())->method('getSelectorName')->will($this->returnValue('tx_myext_tablename'));
187 $mockSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('Tx_MyExt_ClassName'));
188
189 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
190 $mockDataMapper->expects($this->once())->method('convertPropertyNameToColumnName')->with('fooProperty', 'Tx_MyExt_ClassName')->will($this->returnValue('converted_fieldname'));
191
192 $sql = array();
193 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
194 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
195 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
196
197 $expecedSql = array('orderings' => array('tx_myext_tablenamefromproperty.converted_fieldname ASC'));
198 $this->assertSame($expecedSql, $sql);
199 }
200
201 /**
202 * @test
203 * @expectedException Tx_Extbase_Persistence_Exception_UnsupportedOrder
204 */
205 public function orderStatementGenerationThrowsExceptionOnUnsupportedOrder() {
206 $mockPropertyValue = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
207 $mockPropertyValue->expects($this->never())->method('getPropertyName');
208 $mockPropertyValue->expects($this->never())->method('getSelectorName');
209
210 $mockOrdering1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
211 $mockOrdering1->expects($this->once())->method('getOrder')->will($this->returnValue('unsupported_order'));
212 $mockOrdering1->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue));
213 $orderings = array($mockOrdering1);
214
215 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array('getSelectorName', 'getNodeTypeName'), array(), '', FALSE);
216 $mockSource->expects($this->any())->method('getSelectorName')->will($this->returnValue('tx_myext_tablename'));
217
218 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
219 $mockDataMapper->expects($this->never())->method('convertPropertyNameToColumnName');
220
221 $sql = array();
222 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
223 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
224 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
225 }
226
227 /**
228 * @test
229 */
230 public function orderStatementGenerationWorksWithMultipleOrderings() {
231 $mockPropertyValue1 = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
232 $mockPropertyValue1->expects($this->atLeastOnce())->method('getPropertyName')->will($this->returnValue('fooProperty'));
233 $mockPropertyValue1->expects($this->atLeastOnce())->method('getSelectorName')->will($this->returnValue('tx_myext_bar'));
234
235 $mockPropertyValue2 = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
236 $mockPropertyValue2->expects($this->atLeastOnce())->method('getPropertyName')->will($this->returnValue('barProperty'));
237 $mockPropertyValue2->expects($this->atLeastOnce())->method('getSelectorName')->will($this->returnValue('tx_myext_blub'));
238
239 $mockOrdering1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
240 $mockOrdering1->expects($this->once())->method('getOrder')->will($this->returnValue(Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_ORDER_ASCENDING));
241 $mockOrdering1->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue1));
242 $mockOrdering2 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
243 $mockOrdering2->expects($this->once())->method('getOrder')->will($this->returnValue(Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_ORDER_DESCENDING));
244 $mockOrdering2->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue2));
245 $orderings = array($mockOrdering1, $mockOrdering2);
246
247 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
248
249 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
250 $mockDataMapper->expects($this->atLeastOnce())->method('convertPropertyNameToColumnName')->will($this->returnValue('foo_field'));
251
252 $sql = array();
253 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
254 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
255 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
256
257 $expecedSql = array('orderings' => array('tx_myext_bar.foo_field ASC', 'tx_myext_blub.foo_field DESC'));
258 $this->assertEquals($expecedSql, $sql);
259 }
260
261 /**
262 * @test
263 */
264 public function orderStatementGenerationWorksWithDescendingOrder() {
265 $mockPropertyValue = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
266 $mockPropertyValue->expects($this->once())->method('getPropertyName')->will($this->returnValue('fooProperty'));
267 $mockPropertyValue->expects($this->once())->method('getSelectorName')->will($this->returnValue(''));
268
269 $mockOrdering1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
270 $mockOrdering1->expects($this->once())->method('getOrder')->will($this->returnValue(Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_ORDER_DESCENDING));
271 $mockOrdering1->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue));
272 $orderings = array($mockOrdering1);
273
274 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
275
276 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
277 $mockDataMapper->expects($this->once())->method('convertPropertyNameToColumnName')->with('fooProperty', '')->will($this->returnValue('bar_property'));
278
279 $sql = array();
280 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
281 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
282 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
283
284 $expecedSql = array('orderings' => array('bar_property DESC'));
285 $this->assertEquals($expecedSql, $sql);
286 }
287
288 /**
289 * @test
290 */
291 public function orderStatementGenerationWorksWithTheSourceSelectorNameIfNotSpecifiedInThePropertyValue() {
292 $mockPropertyValue = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
293 $mockPropertyValue->expects($this->once())->method('getPropertyName')->will($this->returnValue('fooProperty'));
294 $mockPropertyValue->expects($this->once())->method('getSelectorName')->will($this->returnValue(''));
295
296 $mockOrdering1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
297 $mockOrdering1->expects($this->once())->method('getOrder')->will($this->returnValue(Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_ORDER_ASCENDING));
298 $mockOrdering1->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue));
299 $orderings = array($mockOrdering1);
300
301 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
302
303 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
304 $mockDataMapper->expects($this->once())->method('convertPropertyNameToColumnName')->with('fooProperty', '')->will($this->returnValue('bar_property'));
305
306 $sql = array();
307 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
308 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
309 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
310
311 $expecedSql = array('orderings' => array('bar_property ASC'));
312 $this->assertEquals($expecedSql, $sql);
313 }
314
315 }
316 ?>