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 $this->markTestIncomplete();
71 public function getStatementWorksWithBasicEqualsCondition() {
72 $this->markTestIncomplete();
77 * @expectedException Tx_Extbase_Persistence_Storage_Exception_BadConstraint
79 public function countRowsWithStatementConstraintResultsInAnException() {
80 $this->markTestIncomplete();
86 public function joinStatementGenerationWorks() {
87 $this->markTestIncomplete();
93 public function orderStatementGenerationWorks() {
94 $mockPropertyValue = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
95 $mockPropertyValue->expects($this->once())->method('getPropertyName')->will($this->returnValue('fooProperty'));
96 $mockPropertyValue->expects($this->once())->method('getSelectorName')->will($this->returnValue('tx_myext_tablenamefromproperty'));
98 $mockOrdering1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
99 $mockOrdering1->expects($this->once())->method('getOrder')->will($this->returnValue(Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface
::JCR_ORDER_ASCENDING
));
100 $mockOrdering1->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue));
101 $orderings = array($mockOrdering1);
103 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array('getSelectorName', 'getNodeTypeName'), array(), '', FALSE);
104 $mockSource->expects($this->any())->method('getSelectorName')->will($this->returnValue('tx_myext_tablename'));
105 $mockSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('Tx_MyExt_ClassName'));
107 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
108 $mockDataMapper->expects($this->once())->method('convertPropertyNameToColumnName')->with('fooProperty', 'Tx_MyExt_ClassName')->will($this->returnValue('converted_fieldname'));
111 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
112 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
113 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
115 $expecedSql = array('orderings' => array('tx_myext_tablenamefromproperty.converted_fieldname ASC'));
116 $this->assertSame($expecedSql, $sql);
121 * @expectedException Tx_Extbase_Persistence_Exception_UnsupportedOrder
123 public function orderStatementGenerationThrowsExceptionOnUnsupportedOrder() {
124 $mockPropertyValue = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
125 $mockPropertyValue->expects($this->never())->method('getPropertyName');
126 $mockPropertyValue->expects($this->never())->method('getSelectorName');
128 $mockOrdering1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
129 $mockOrdering1->expects($this->once())->method('getOrder')->will($this->returnValue('unsupported_order'));
130 $mockOrdering1->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue));
131 $orderings = array($mockOrdering1);
133 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array('getSelectorName', 'getNodeTypeName'), array(), '', FALSE);
134 $mockSource->expects($this->any())->method('getSelectorName')->will($this->returnValue('tx_myext_tablename'));
136 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
137 $mockDataMapper->expects($this->never())->method('convertPropertyNameToColumnName');
140 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
141 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
142 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
148 public function orderStatementGenerationWorksWithMultipleOrderings() {
149 $mockPropertyValue1 = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
150 $mockPropertyValue1->expects($this->atLeastOnce())->method('getPropertyName')->will($this->returnValue('fooProperty'));
151 $mockPropertyValue1->expects($this->atLeastOnce())->method('getSelectorName')->will($this->returnValue('tx_myext_bar'));
153 $mockPropertyValue2 = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
154 $mockPropertyValue2->expects($this->atLeastOnce())->method('getPropertyName')->will($this->returnValue('barProperty'));
155 $mockPropertyValue2->expects($this->atLeastOnce())->method('getSelectorName')->will($this->returnValue('tx_myext_blub'));
157 $mockOrdering1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
158 $mockOrdering1->expects($this->once())->method('getOrder')->will($this->returnValue(Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface
::JCR_ORDER_ASCENDING
));
159 $mockOrdering1->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue1));
160 $mockOrdering2 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
161 $mockOrdering2->expects($this->once())->method('getOrder')->will($this->returnValue(Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface
::JCR_ORDER_DESCENDING
));
162 $mockOrdering2->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue2));
163 $orderings = array($mockOrdering1, $mockOrdering2);
165 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
167 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
168 $mockDataMapper->expects($this->atLeastOnce())->method('convertPropertyNameToColumnName')->will($this->returnValue('foo_field'));
171 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
172 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
173 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
175 $expecedSql = array('orderings' => array('tx_myext_bar.foo_field ASC', 'tx_myext_blub.foo_field DESC'));
176 $this->assertEquals($expecedSql, $sql);
182 public function orderStatementGenerationWorksWithDescendingOrder() {
183 $mockPropertyValue = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
184 $mockPropertyValue->expects($this->once())->method('getPropertyName')->will($this->returnValue('fooProperty'));
185 $mockPropertyValue->expects($this->once())->method('getSelectorName')->will($this->returnValue(''));
187 $mockOrdering1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
188 $mockOrdering1->expects($this->once())->method('getOrder')->will($this->returnValue(Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface
::JCR_ORDER_DESCENDING
));
189 $mockOrdering1->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue));
190 $orderings = array($mockOrdering1);
192 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
194 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
195 $mockDataMapper->expects($this->once())->method('convertPropertyNameToColumnName')->with('fooProperty', '')->will($this->returnValue('bar_property'));
198 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
199 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
200 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
202 $expecedSql = array('orderings' => array('bar_property DESC'));
203 $this->assertEquals($expecedSql, $sql);
209 public function orderStatementGenerationWorksWithTheSourceSelectorNameIfNotSpecifiedInThePropertyValue() {
210 $mockPropertyValue = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
211 $mockPropertyValue->expects($this->once())->method('getPropertyName')->will($this->returnValue('fooProperty'));
212 $mockPropertyValue->expects($this->once())->method('getSelectorName')->will($this->returnValue(''));
214 $mockOrdering1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
215 $mockOrdering1->expects($this->once())->method('getOrder')->will($this->returnValue(Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface
::JCR_ORDER_ASCENDING
));
216 $mockOrdering1->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue));
217 $orderings = array($mockOrdering1);
219 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
221 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
222 $mockDataMapper->expects($this->once())->method('convertPropertyNameToColumnName')->with('fooProperty', '')->will($this->returnValue('bar_property'));
225 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
226 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
227 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
229 $expecedSql = array('orderings' => array('bar_property ASC'));
230 $this->assertEquals($expecedSql, $sql);