[+FEATURE] Extbase (Persistence): Implemented (still experimental) feature to be...
[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 $this->markTestIncomplete();
66 }
67
68 /**
69 * @test
70 */
71 public function getStatementWorksWithBasicEqualsCondition() {
72 $this->markTestIncomplete();
73 }
74
75 /**
76 * @test
77 * @expectedException Tx_Extbase_Persistence_Storage_Exception_BadConstraint
78 */
79 public function countRowsWithStatementConstraintResultsInAnException() {
80 $this->markTestIncomplete();
81 }
82
83 /**
84 * @test
85 */
86 public function joinStatementGenerationWorks() {
87 $this->markTestIncomplete();
88 }
89
90 /**
91 * @test
92 */
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'));
97
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);
102
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'));
106
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'));
109
110 $sql = array();
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);
114
115 $expecedSql = array('orderings' => array('tx_myext_tablenamefromproperty.converted_fieldname ASC'));
116 $this->assertSame($expecedSql, $sql);
117 }
118
119 /**
120 * @test
121 * @expectedException Tx_Extbase_Persistence_Exception_UnsupportedOrder
122 */
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');
127
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);
132
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'));
135
136 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
137 $mockDataMapper->expects($this->never())->method('convertPropertyNameToColumnName');
138
139 $sql = array();
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);
143 }
144
145 /**
146 * @test
147 */
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'));
152
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'));
156
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);
164
165 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
166
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'));
169
170 $sql = array();
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);
174
175 $expecedSql = array('orderings' => array('tx_myext_bar.foo_field ASC', 'tx_myext_blub.foo_field DESC'));
176 $this->assertEquals($expecedSql, $sql);
177 }
178
179 /**
180 * @test
181 */
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(''));
186
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);
191
192 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
193
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'));
196
197 $sql = array();
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);
201
202 $expecedSql = array('orderings' => array('bar_property DESC'));
203 $this->assertEquals($expecedSql, $sql);
204 }
205
206 /**
207 * @test
208 */
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(''));
213
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);
218
219 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
220
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'));
223
224 $sql = array();
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);
228
229 $expecedSql = array('orderings' => array('bar_property ASC'));
230 $this->assertEquals($expecedSql, $sql);
231 }
232
233 }
234 ?>