ExtBase:
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Persistence / Mapper / ObjectRelationalMapper_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Christopher Hlubek <hlubek@networkteam.com>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 class Tx_ExtBase_Persistence_Mapper_ObjectRelationalMapper_testcase extends Tx_ExtBase_Base_testcase {
26
27 public function setUp() {
28 $GLOBALS['TSFE'] = $this->getMock('tslib_fe', array('includeTCA'));
29 $GLOBALS['TSFE']->expects($this->any())
30 ->method('includeTCA')
31 ->will($this->returnValue(NULL));
32
33 $this->typo3Db = $GLOBALS['TYPO3_DB'];
34 $GLOBALS['TYPO3_DB'] = $this->getMock('tslib_DB', array('fullQuoteStr'));
35 }
36
37 public function tearDown() {
38 $GLOBALS['TYPO3_DB'] = $this->typo3Db;
39 }
40
41 public function test_QueryWithPlaceholdersCanBeBuild() {
42 $mapper = new Tx_ExtBase_Persistence_Mapper_ObjectRelationalMapper();
43
44 $GLOBALS['TYPO3_DB']->expects($this->at(0))
45 ->method('fullQuoteStr')
46 ->with($this->equalTo('foo'))
47 ->will($this->returnValue('"foo"'));
48
49 $GLOBALS['TYPO3_DB']->expects($this->at(1))
50 ->method('fullQuoteStr')
51 ->with($this->equalTo('bar'))
52 ->will($this->returnValue('"bar"'));
53
54 $query = $mapper->buildQuery('Tx_BlogExample_Domain_Blog',
55 array(
56 array('name LIKE ? OR name LIKE ?', 'foo', 'bar'),
57 array('hidden = ?', FALSE)
58 ));
59
60 $this->assertEquals('(name LIKE "foo" OR name LIKE "bar") AND (hidden = 0)', $query);
61 }
62
63 public function test_QueryWithExampleCanBeBuild() {
64 $mapper = $this->getMock('Tx_ExtBase_Persistence_Mapper_ObjectRelationalMapper', array('getDataMap'));
65
66 $columnMap1 = $this->getMock('Tx_ExtBase_Persistence_Mapper_ColumnMap', array('getColumnName'), array(), '', FALSE);
67 $columnMap1->expects($this->once())
68 ->method('getColumnName')
69 ->will($this->returnValue('blog_name'));
70
71 $columnMap2 = $this->getMock('Tx_ExtBase_Persistence_Mapper_ColumnMap', array('getColumnName'), array(), '', FALSE);
72 $columnMap2->expects($this->once())
73 ->method('getColumnName')
74 ->will($this->returnValue('hidden'));
75
76 $dataMap = $this->getMock('Tx_ExtBase_Persistence_Mapper_DataMap', array('getColumnMap', 'getTableName'), array(), '', FALSE);
77
78 $dataMap->expects($this->at(0))
79 ->method('getColumnMap')
80 ->with($this->equalTo('blogName'))
81 ->will($this->returnValue($columnMap1));
82
83 $dataMap->expects($this->at(1))
84 ->method('getTableName')
85 ->will($this->returnValue('tx_blogexample_domain_model_blog'));
86
87 $dataMap->expects($this->at(2))
88 ->method('getColumnMap')
89 ->with($this->equalTo('hidden'))
90 ->will($this->returnValue($columnMap2));
91
92 $dataMap->expects($this->at(3))
93 ->method('getTableName')
94 ->will($this->returnValue('tx_blogexample_domain_model_blog'));
95
96 $mapper->expects($this->any())
97 ->method('getDataMap')
98 ->with($this->equalTo('Tx_BlogExample_Domain_Model_Blog'))
99 ->will($this->returnValue($dataMap));
100
101 $GLOBALS['TYPO3_DB']->expects($this->at(0))
102 ->method('fullQuoteStr')
103 ->with($this->equalTo('foo'))
104 ->will($this->returnValue('"foo"'));
105
106 $query = $mapper->buildQuery('Tx_BlogExample_Domain_Model_Blog',
107 array(
108 'blogName' => 'foo',
109 'hidden' => FALSE
110 ));
111
112 $this->assertEquals('(tx_blogexample_domain_model_blog.blog_name = "foo") AND (tx_blogexample_domain_model_blog.hidden = 0)', $query);
113 }
114
115 public function test_QueryWithNestedExampleCanBeBuild() {
116 $mapper = $this->getMock('Tx_ExtBase_Persistence_Mapper_ObjectRelationalMapper', array('getDataMap'));
117
118 $columnMap1 = $this->getMock('Tx_ExtBase_Persistence_Mapper_ColumnMap', array('getColumnName'), array(), '', FALSE);
119 $columnMap1->expects($this->once())
120 ->method('getColumnName')
121 ->will($this->returnValue('hidden'));
122
123 $columnMap3 = $this->getMock('Tx_ExtBase_Persistence_Mapper_ColumnMap', array('getChildClassName'), array(), '', FALSE);
124 $columnMap3->expects($this->once())
125 ->method('getChildClassName')
126 ->will($this->returnValue('Tx_BlogExample_Domain_Model_Author'));
127
128 $dataMap1 = $this->getMock('Tx_ExtBase_Persistence_Mapper_DataMap', array('getColumnMap', 'getTableName'), array(), '', FALSE);
129 $dataMap1->expects($this->at(0))
130 ->method('getColumnMap')
131 ->with($this->equalTo('hidden'))
132 ->will($this->returnValue($columnMap1));
133 $dataMap1->expects($this->at(1))
134 ->method('getTableName')
135 ->will($this->returnValue('tx_blogexample_domain_model_blog'));
136 $dataMap1->expects($this->at(2))
137 ->method('getColumnMap')
138 ->with($this->equalTo('author'))
139 ->will($this->returnValue($columnMap3));
140
141 $columnMap2 = $this->getMock('Tx_ExtBase_Persistence_Mapper_ColumnMap', array('getColumnName'), array(), '', FALSE);
142 $columnMap2->expects($this->once())
143 ->method('getColumnName')
144 ->will($this->returnValue('name'));
145
146 $dataMap2 = $this->getMock('Tx_ExtBase_Persistence_Mapper_DataMap', array('getColumnMap', 'getTableName'), array(), '', FALSE);
147 $dataMap2->expects($this->at(0))
148 ->method('getColumnMap')
149 ->with($this->equalTo('name'))
150 ->will($this->returnValue($columnMap2));
151 $dataMap2->expects($this->at(1))
152 ->method('getTableName')
153 ->will($this->returnValue('tx_blogexample_domain_model_author'));
154
155 $mapper->expects($this->at(0))
156 ->method('getDataMap')
157 ->with($this->equalTo('Tx_BlogExample_Domain_Model_Blog'))
158 ->will($this->returnValue($dataMap1));
159
160 $mapper->expects($this->at(1))
161 ->method('getDataMap')
162 ->with($this->equalTo('Tx_BlogExample_Domain_Model_Author'))
163 ->will($this->returnValue($dataMap2));
164
165 $GLOBALS['TYPO3_DB']->expects($this->any())
166 ->method('fullQuoteStr')
167 ->with($this->equalTo('Christopher'))
168 ->will($this->returnValue('"Christopher"'));
169
170 $query = $mapper->buildQuery('Tx_BlogExample_Domain_Model_Blog',
171 array(
172 'hidden' => FALSE,
173 'author' => array(
174 'name' => 'Christopher'
175 )
176 ));
177
178 $this->assertEquals('(tx_blogexample_domain_model_blog.hidden = 0) AND ((tx_blogexample_domain_model_author.name = "Christopher"))', $query);
179 }
180
181 public function test_QueryWithExampleCanBeBuild() {
182 $mapper = $this->getMock('Tx_ExtBase_Persistence_Mapper_ObjectRelationalMapper', array('getDataMap'));
183
184 $columnMap1 = $this->getMock('Tx_ExtBase_Persistence_Mapper_ColumnMap', array('getColumnName'), array(), '', FALSE);
185 $columnMap1->expects($this->once())
186 ->method('getColumnName')
187 ->will($this->returnValue('blog_name'));
188
189 $columnMap2 = $this->getMock('Tx_ExtBase_Persistence_Mapper_ColumnMap', array('getColumnName'), array(), '', FALSE);
190 $columnMap2->expects($this->once())
191 ->method('getColumnName')
192 ->will($this->returnValue('hidden'));
193
194 $dataMap = $this->getMock('Tx_ExtBase_Persistence_Mapper_DataMap', array('getColumnMap', 'getTableName'), array(), '', FALSE);
195
196 $dataMap->expects($this->at(0))
197 ->method('getColumnMap')
198 ->with($this->equalTo('blogName'))
199 ->will($this->returnValue($columnMap1));
200
201 $dataMap->expects($this->at(1))
202 ->method('getTableName')
203 ->will($this->returnValue('tx_blogexample_domain_model_blog'));
204
205 $dataMap->expects($this->at(2))
206 ->method('getColumnMap')
207 ->with($this->equalTo('hidden'))
208 ->will($this->returnValue($columnMap2));
209
210 $dataMap->expects($this->at(3))
211 ->method('getTableName')
212 ->will($this->returnValue('tx_blogexample_domain_model_blog'));
213
214 $mapper->expects($this->any())
215 ->method('getDataMap')
216 ->with($this->equalTo('Tx_BlogExample_Domain_Model_Blog'))
217 ->will($this->returnValue($dataMap));
218
219 $GLOBALS['TYPO3_DB']->expects($this->at(0))
220 ->method('fullQuoteStr')
221 ->with($this->equalTo('foo'))
222 ->will($this->returnValue('"foo"'));
223
224 $query = $mapper->buildQuery('Tx_BlogExample_Domain_Model_Blog',
225 array(
226 'blogName' => 'foo',
227 'hidden' => FALSE
228 ));
229
230 $this->assertEquals('(tx_blogexample_domain_model_blog.blog_name = "foo") AND (tx_blogexample_domain_model_blog.hidden = 0)', $query);
231 }
232
233
234 }
235 ?>