Merge "[BUGFIX] PropertyMapper throws exceptions instead of returning error objects."
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Persistence / Storage / Typo3DbBackendTest.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_Tests_Unit_Persistence_Storage_Typo3DbBackendTest extends Tx_Extbase_Tests_Unit_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 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array('getNodeTypeName'), array(), '', FALSE);
95 $mockSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('Tx_MyExt_ClassName'));
96
97 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName', 'convertClassNameToTableName'), array(), '', FALSE);
98 $mockDataMapper->expects($this->once())->method('convertClassNameToTableName')->with('Tx_MyExt_ClassName')->will($this->returnValue('tx_myext_tablename'));
99 $mockDataMapper->expects($this->once())->method('convertPropertyNameToColumnName')->with('fooProperty', 'Tx_MyExt_ClassName')->will($this->returnValue('converted_fieldname'));
100
101 $sql = array();
102 $orderings = array('fooProperty' => Tx_Extbase_Persistence_QueryInterface::ORDER_ASCENDING);
103 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
104 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
105 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
106
107 $expecedSql = array('orderings' => array('tx_myext_tablename.converted_fieldname ASC'));
108 $this->assertSame($expecedSql, $sql);
109 }
110
111 /**
112 * @test
113 * @expectedException Tx_Extbase_Persistence_Exception_UnsupportedOrder
114 */
115 public function orderStatementGenerationThrowsExceptionOnUnsupportedOrder() {
116 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array('getNodeTypeName'), array(), '', FALSE);
117 $mockSource->expects($this->never())->method('getNodeTypeName');
118
119 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName', 'convertClassNameToTableName'), array(), '', FALSE);
120 $mockDataMapper->expects($this->never())->method('convertClassNameToTableName');
121 $mockDataMapper->expects($this->never())->method('convertPropertyNameToColumnName');
122
123 $sql = array();
124 $orderings = array('fooProperty' => 'unsupported_order');
125 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
126 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
127 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
128 }
129
130 /**
131 * @test
132 */
133 public function orderStatementGenerationWorksWithMultipleOrderings() {
134 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array('getNodeTypeName'), array(), '', FALSE);
135 $mockSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('Tx_MyExt_ClassName'));
136
137 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName', 'convertClassNameToTableName'), array(), '', FALSE);
138 $mockDataMapper->expects($this->any())->method('convertClassNameToTableName')->with('Tx_MyExt_ClassName')->will($this->returnValue('tx_myext_tablename'));
139 $mockDataMapper->expects($this->any())->method('convertPropertyNameToColumnName')->will($this->returnValue('converted_fieldname'));
140
141 $sql = array();
142 $orderings = array(
143 'fooProperty' => Tx_Extbase_Persistence_QueryInterface::ORDER_ASCENDING,
144 'barProperty' => Tx_Extbase_Persistence_QueryInterface::ORDER_DESCENDING
145 );
146 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
147 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
148 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
149
150 $expecedSql = array('orderings' => array('tx_myext_tablename.converted_fieldname ASC', 'tx_myext_tablename.converted_fieldname DESC'));
151 $this->assertSame($expecedSql, $sql);
152 }
153
154 /**
155 * @test
156 */
157 public function doLanguageAndWorkspaceOverlayChangesUidIfInPreview() {
158 $comparisonRow = array(
159 'uid' => '43',
160 'pid' => '42',
161 '_ORIG_pid' => '-1',
162 '_ORIG_uid' => '43'
163 );
164
165 $row = array(
166 'uid' => '42',
167 'pid' => '42',
168 );
169
170 $workspaceVersion = array(
171 'uid' => '43',
172 'pid' => '-1',
173 );
174
175 $languageUid = 2;
176 $workspaceUid = 2;
177
178 $sourceMock = new Tx_Extbase_Persistence_QOM_Selector('tx_foo', 'Tx_Foo');
179
180 $pageSelectMock = $this->getMock('t3lib_pageSelect', array('movePlhOL', 'getWorkspaceVersionOfRecord'));
181 $pageSelectMock->versioningPreview = 1;
182
183 $pageSelectMock->expects($this->once())
184 ->method('getWorkspaceVersionOfRecord')
185 ->with($workspaceUid, 'tx_foo', '42')
186 ->will($this->returnValue($workspaceVersion));
187
188 $mockTypo3DbBackend = $this->getAccessibleMock(
189 'Tx_Extbase_Persistence_Storage_Typo3DbBackend',
190 array('dummy'),
191 array(), '', FALSE);
192
193
194 $mockTypo3DbBackend->_set('pageSelectObject', $pageSelectMock);
195
196 $this->assertSame(
197 array($comparisonRow),
198 $mockTypo3DbBackend->_call('doLanguageAndWorkspaceOverlay', $sourceMock, array($row), $languageUid, $workspaceUid)
199 );
200 }
201 }
202 ?>