[~TASK] Extbase: Refactored Dispatcher. Moved initialization of cache and reflection...
[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_QOM_QueryObjectModelConstantsInterface::JCR_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_QOM_QueryObjectModelConstantsInterface::JCR_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_QOM_QueryObjectModelConstantsInterface::JCR_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_QOM_QueryObjectModelConstantsInterface::JCR_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_QOM_QueryObjectModelConstantsInterface::JCR_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 parseQueryWorksWithMinimalisticQueryObjectModel() {
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
75 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parseOrderings'), array(), '', FALSE);
76 $mockTypo3DbBackend->expects($this->any())->method('parseOrderings');
77
78 $resultingStatement = $mockTypo3DbBackend->parseQuery($mockQueryObjectModel);
79 $expectedStatement = 'SELECT selector_name.* FROM selector_name';
80 $this->assertEquals($expectedStatement, $resultingStatement);
81 }
82
83 /**
84 * @test
85 */
86 public function parseQueryWorksWithBasicEqualsCondition() {
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
97 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parseOrderings'), array(), '', FALSE);
98 $mockTypo3DbBackend->expects($this->any())->method('parseOrderings');
99
100 $resultingStatement = $mockTypo3DbBackend->parseQuery($mockQueryObjectModel);
101 $expectedStatement = 'SELECT selector_name.* FROM selector_name';
102 $this->assertEquals($expectedStatement, $resultingStatement);
103 }
104
105 /**
106 * @test
107 */
108 public function joinStatementGenerationWorks() {
109 $mockLeftSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
110 $mockLeftSource->expects($this->any())->method('getSelectorName')->will($this->returnValue('left_selector_name'));
111 $mockLeftSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('left_nodetype_name'));
112
113 $mockRightSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
114 $mockRightSource->expects($this->any())->method('getSelectorName')->will($this->returnValue('right_selector_name'));
115 $mockRightSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('right_nodetype_name'));
116
117 $mockJoinCondition = $this->getMock('Tx_Extbase_Persistence_QOM_EquiJoinCondition', array('getSelector1Name', 'getSelector2Name', 'getProperty1Name', 'getProperty2Name'), array(), '', FALSE);
118 $mockJoinCondition->expects($this->any())->method('getSelector1Name')->will($this->returnValue('first_selector'));
119 $mockJoinCondition->expects($this->any())->method('getSelector2Name')->will($this->returnValue('second_selector'));
120 $mockJoinCondition->expects($this->any())->method('getProperty1Name')->will($this->returnValue('firstProperty'));
121 $mockJoinCondition->expects($this->any())->method('getProperty2Name')->will($this->returnValue('secondProperty'));
122
123 $mockJoin = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Join'), array('getLeft', 'getRight'), array(), '', FALSE);
124 $mockJoin->_set('joinCondition', $mockJoinCondition);
125 $mockJoin->_set('joinType', $mockJoinCondition);
126 $mockJoin->expects($this->any())->method('getLeft')->will($this->returnValue($mockLeftSource));
127 $mockJoin->expects($this->any())->method('getRight')->will($this->returnValue($mockRightSource));
128
129 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
130 $mockDataMapper->expects($this->any())
131 ->method('convertPropertyNameToColumnName')
132 ->with(
133 $this->logicalOr(
134 $this->equalTo('firstProperty'),
135 $this->equalTo('secondProperty')
136 )
137 )
138 ->will($this->returnValue('resulting_fieldname'));
139
140 $sql = array();
141 $parameters = array();
142 $mockQueryObjectModel = $this->getMock('Tx_Extbase_Persistence_QOM_QueryObjectModelInterface');
143 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserJoin'), array(), '', FALSE);
144 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
145 $mockTypo3DbBackend->_callRef('parseJoin', $mockQueryObjectModel, $mockJoin, $sql, $parameters);
146
147 $expecedSql = array(
148 'fields' => array('left_selector_name.*', 'right_selector_name.*'),
149 'tables' => array(
150 'left_selector_name LEFT JOIN right_selector_name',
151 'ON first_selector.resulting_fieldname = second_selector.resulting_fieldname'
152 )
153 );
154
155 $this->assertEquals($expecedSql, $sql);
156 }
157
158 /**
159 * @test
160 */
161 public function orderStatementGenerationWorks() {
162 $mockPropertyValue = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
163 $mockPropertyValue->expects($this->once())->method('getPropertyName')->will($this->returnValue('fooProperty'));
164 $mockPropertyValue->expects($this->once())->method('getSelectorName')->will($this->returnValue('tx_myext_tablenamefromproperty'));
165
166 $mockOrdering1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
167 $mockOrdering1->expects($this->once())->method('getOrder')->will($this->returnValue(Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_ORDER_ASCENDING));
168 $mockOrdering1->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue));
169 $orderings = array($mockOrdering1);
170
171 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array('getSelectorName', 'getNodeTypeName'), array(), '', FALSE);
172 $mockSource->expects($this->any())->method('getSelectorName')->will($this->returnValue('tx_myext_tablename'));
173 $mockSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('Tx_MyExt_ClassName'));
174
175 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
176 $mockDataMapper->expects($this->once())->method('convertPropertyNameToColumnName')->with('fooProperty', 'Tx_MyExt_ClassName')->will($this->returnValue('converted_fieldname'));
177
178 $sql = array();
179 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
180 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
181 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
182
183 $expecedSql = array('orderings' => array('tx_myext_tablenamefromproperty.converted_fieldname ASC'));
184 $this->assertSame($expecedSql, $sql);
185 }
186
187 /**
188 * @test
189 * @expectedException Tx_Extbase_Persistence_Exception_UnsupportedOrder
190 */
191 public function orderStatementGenerationThrowsExceptionOnUnsupportedOrder() {
192 $mockPropertyValue = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
193 $mockPropertyValue->expects($this->never())->method('getPropertyName');
194 $mockPropertyValue->expects($this->never())->method('getSelectorName');
195
196 $mockOrdering1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
197 $mockOrdering1->expects($this->once())->method('getOrder')->will($this->returnValue('unsupported_order'));
198 $mockOrdering1->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue));
199 $orderings = array($mockOrdering1);
200
201 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array('getSelectorName', 'getNodeTypeName'), array(), '', FALSE);
202 $mockSource->expects($this->any())->method('getSelectorName')->will($this->returnValue('tx_myext_tablename'));
203
204 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
205 $mockDataMapper->expects($this->never())->method('convertPropertyNameToColumnName');
206
207 $sql = array();
208 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
209 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
210 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
211 }
212
213 /**
214 * @test
215 */
216 public function orderStatementGenerationWorksWithMultipleOrderings() {
217 $mockPropertyValue1 = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
218 $mockPropertyValue1->expects($this->atLeastOnce())->method('getPropertyName')->will($this->returnValue('fooProperty'));
219 $mockPropertyValue1->expects($this->atLeastOnce())->method('getSelectorName')->will($this->returnValue('tx_myext_bar'));
220
221 $mockPropertyValue2 = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
222 $mockPropertyValue2->expects($this->atLeastOnce())->method('getPropertyName')->will($this->returnValue('barProperty'));
223 $mockPropertyValue2->expects($this->atLeastOnce())->method('getSelectorName')->will($this->returnValue('tx_myext_blub'));
224
225 $mockOrdering1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
226 $mockOrdering1->expects($this->once())->method('getOrder')->will($this->returnValue(Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_ORDER_ASCENDING));
227 $mockOrdering1->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue1));
228 $mockOrdering2 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
229 $mockOrdering2->expects($this->once())->method('getOrder')->will($this->returnValue(Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_ORDER_DESCENDING));
230 $mockOrdering2->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue2));
231 $orderings = array($mockOrdering1, $mockOrdering2);
232
233 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
234
235 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
236 $mockDataMapper->expects($this->atLeastOnce())->method('convertPropertyNameToColumnName')->will($this->returnValue('foo_field'));
237
238 $sql = array();
239 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
240 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
241 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
242
243 $expecedSql = array('orderings' => array('tx_myext_bar.foo_field ASC', 'tx_myext_blub.foo_field DESC'));
244 $this->assertEquals($expecedSql, $sql);
245 }
246
247 /**
248 * @test
249 */
250 public function orderStatementGenerationWorksWithDescendingOrder() {
251 $mockPropertyValue = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
252 $mockPropertyValue->expects($this->once())->method('getPropertyName')->will($this->returnValue('fooProperty'));
253 $mockPropertyValue->expects($this->once())->method('getSelectorName')->will($this->returnValue(''));
254
255 $mockOrdering1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
256 $mockOrdering1->expects($this->once())->method('getOrder')->will($this->returnValue(Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_ORDER_DESCENDING));
257 $mockOrdering1->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue));
258 $orderings = array($mockOrdering1);
259
260 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
261
262 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
263 $mockDataMapper->expects($this->once())->method('convertPropertyNameToColumnName')->with('fooProperty', '')->will($this->returnValue('bar_property'));
264
265 $sql = array();
266 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
267 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
268 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
269
270 $expecedSql = array('orderings' => array('bar_property DESC'));
271 $this->assertEquals($expecedSql, $sql);
272 }
273
274 /**
275 * @test
276 */
277 public function orderStatementGenerationWorksWithTheSourceSelectorNameIfNotSpecifiedInThePropertyValue() {
278 $mockPropertyValue = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
279 $mockPropertyValue->expects($this->once())->method('getPropertyName')->will($this->returnValue('fooProperty'));
280 $mockPropertyValue->expects($this->once())->method('getSelectorName')->will($this->returnValue(''));
281
282 $mockOrdering1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
283 $mockOrdering1->expects($this->once())->method('getOrder')->will($this->returnValue(Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_ORDER_ASCENDING));
284 $mockOrdering1->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue));
285 $orderings = array($mockOrdering1);
286
287 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
288
289 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
290 $mockDataMapper->expects($this->once())->method('convertPropertyNameToColumnName')->with('fooProperty', '')->will($this->returnValue('bar_property'));
291
292 $sql = array();
293 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
294 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
295 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
296
297 $expecedSql = array('orderings' => array('bar_property ASC'));
298 $this->assertEquals($expecedSql, $sql);
299 }
300
301 }
302 ?>