2 /***************************************************************
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
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.
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
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.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
28 class Tx_Extbase_Persistence_Storage_Typo3DbBackend_testcase
extends Tx_Extbase_BaseTestCase
{
31 * This is the data provider for the statement generation with a basic comparison
33 * @return array An array of data
35 public function providerForBasicComparison() {
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'"
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'"
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'"
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'"
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'"
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'));
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()));
74 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parseOrderings'), array(), '', FALSE);
75 $mockTypo3DbBackend->expects($this->any())->method('parseOrderings');
77 $parameters = array();
78 $resultingStatement = $mockTypo3DbBackend->getStatement($mockQueryObjectModel, $parameters);
79 $expectedStatement = 'SELECT selector_name.* FROM selector_name';
80 $this->assertEquals($expectedStatement, $resultingStatement);
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'));
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()));
96 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parseOrderings'), array(), '', FALSE);
97 $mockTypo3DbBackend->expects($this->any())->method('parseOrderings');
99 $parameters = array();
100 $resultingStatement = $mockTypo3DbBackend->getStatement($mockQueryObjectModel, $parameters);
101 $expectedStatement = 'SELECT selector_name.* FROM selector_name';
102 $this->assertEquals($expectedStatement, $resultingStatement);
107 * @expectedException Tx_Extbase_Persistence_Storage_Exception_BadConstraint
109 public function countRowsWithStatementConstraintResultsInAnException() {
110 $mockStatementConstraint = $this->getMock('Tx_Extbase_Persistence_QOM_Statement', array(), array(), '', FALSE);
112 $mockQueryObjectModel = $this->getMock('Tx_Extbase_Persistence_QOM_QueryObjectModel', array('getConstraint'), array(), '', FALSE);
113 $mockQueryObjectModel->expects($this->once())->method('getConstraint')->will($this->returnValue($mockStatementConstraint));
115 $mockTypo3DbBackend = $this->getMock('Tx_Extbase_Persistence_Storage_Typo3DbBackend', array('dummy'), array(), '', FALSE);
116 $mockTypo3DbBackend->countRows($mockQueryObjectModel);
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'));
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'));
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'));
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));
143 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
144 $mockDataMapper->expects($this->any())
145 ->method('convertPropertyNameToColumnName')
148 $this->equalTo('firstProperty'),
149 $this->equalTo('secondProperty')
152 ->will($this->returnValue('resulting_fieldname'));
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);
162 'fields' => array('left_selector_name.*', 'right_selector_name.*'),
164 'left_selector_name LEFT JOIN right_selector_name',
165 'ON first_selector.resulting_fieldname = second_selector.resulting_fieldname'
169 $this->assertEquals($expecedSql, $sql);
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'));
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);
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'));
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'));
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);
197 $expecedSql = array('orderings' => array('tx_myext_tablenamefromproperty.converted_fieldname ASC'));
198 $this->assertSame($expecedSql, $sql);
203 * @expectedException Tx_Extbase_Persistence_Exception_UnsupportedOrder
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');
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);
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'));
218 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
219 $mockDataMapper->expects($this->never())->method('convertPropertyNameToColumnName');
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);
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'));
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'));
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);
247 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
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'));
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);
257 $expecedSql = array('orderings' => array('tx_myext_bar.foo_field ASC', 'tx_myext_blub.foo_field DESC'));
258 $this->assertEquals($expecedSql, $sql);
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(''));
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);
274 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
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'));
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);
284 $expecedSql = array('orderings' => array('bar_property DESC'));
285 $this->assertEquals($expecedSql, $sql);
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(''));
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);
301 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
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'));
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);
311 $expecedSql = array('orderings' => array('bar_property ASC'));
312 $this->assertEquals($expecedSql, $sql);