[BUGFIX] Unknown record collection type in RecordCollectionRepository
[Packages/TYPO3.CMS.git] / tests / t3lib / collection / RecordCollectionRepositoryTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2012 Oliver Hader <oliver.hader@typo3.org>
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 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
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 /**
29 * Test case for t3lib_collection_RecordCollectionRepository
30 *
31 * @author Oliver Hader <oliver.hader@typo3.org>
32 * @package TYPO3
33 * @subpackage t3lib
34 */
35 class t3lib_collection_RecordCollectionRepositoryTest extends Tx_Phpunit_TestCase {
36 /**
37 * @var PHPUnit_Framework_MockObject_MockObject|t3lib_collection_RecordCollectionRepository
38 */
39 protected $fixture;
40
41 /**
42 * @var PHPUnit_Framework_MockObject_MockObject|t3lib_DB
43 */
44 protected $databaseMock;
45
46 /**
47 * @var NULL|array
48 */
49 protected $getSingleRowCallbackReturnValue;
50
51 /**
52 * @var NULL|array
53 */
54 protected $getRowsCallbackReturnValue;
55
56 /**
57 * Sets up this test case.
58 */
59 protected function setUp() {
60 $this->databaseMock = $this->getMock(
61 't3lib_DB',
62 array('exec_UPDATEquery', 'exec_SELECTgetSingleRow', 'exec_SELECTgetRows')
63 );
64
65 $this->fixture = $this->getMock(
66 't3lib_collection_RecordCollectionRepository',
67 array('getDatabase')
68 );
69 $this->fixture
70 ->expects($this->any())
71 ->method('getDatabase')
72 ->will($this->returnValue($this->databaseMock));
73 }
74
75 /**
76 * Cleans up this test case.
77 */
78 protected function tearDown() {
79 unset($this->databaseMock);
80 unset($this->fixture);
81 unset($this->getSingleRowCallbackReturnValue);
82 unset($this->getRowsCallbackReturnValue);
83 }
84
85 /**
86 * @test
87 */
88 public function doesFindByUidReturnNull() {
89 $testUid = rand(1, 1000);
90
91 $this->databaseMock
92 ->expects($this->once())
93 ->method('exec_SELECTgetSingleRow')
94 ->will($this->returnCallback(array($this, 'getSingleRowCallback')));
95 $this->getSingleRowCallbackReturnValue = NULL;
96
97 $object = $this->fixture->findByUid($testUid);
98 $this->assertNull($object);
99 }
100
101 /**
102 * @test
103 */
104 public function doesFindByUidReturnObject() {
105 $testUid = rand(1, 1000);
106
107 $this->databaseMock
108 ->expects($this->once())
109 ->method('exec_SELECTgetSingleRow')
110 ->will($this->returnCallback(array($this, 'getSingleRowCallback')));
111 $this->getSingleRowCallbackReturnValue = array(
112 'uid' => $testUid,
113 'type' => t3lib_collection_RecordCollectionRepository::TYPE_Static,
114 );
115
116 $object = $this->fixture->findByUid($testUid);
117 $this->assertInstanceOf('t3lib_collection_StaticRecordCollection', $object);
118 }
119
120 /**
121 * @test
122 * @expectedException RuntimeException
123 */
124 public function doesFindByUidThrowException() {
125 $testUid = rand(1, 1000);
126
127 $this->databaseMock
128 ->expects($this->once())
129 ->method('exec_SELECTgetSingleRow')
130 ->will($this->returnCallback(array($this, 'getSingleRowCallback')));
131 $this->getSingleRowCallbackReturnValue = array(
132 'uid' => $testUid,
133 'type' => uniqid('unknown'),
134 );
135
136 $object = $this->fixture->findByUid($testUid);
137 }
138
139 /**
140 * @test
141 */
142 public function doesFindByTypeReturnNull() {
143 $type = t3lib_collection_RecordCollectionRepository::TYPE_Static;
144
145 $this->databaseMock
146 ->expects($this->once())
147 ->method('exec_SELECTgetRows')
148 ->will($this->returnCallback(array($this, 'getRowsCallback')));
149 $this->getRowsCallbackReturnValue = NULL;
150
151 $objects = $this->fixture->findByType($type);
152
153 $this->assertNull($objects);
154 }
155
156 /**
157 * @test
158 */
159 public function doesFindByTypeReturnObjects() {
160 $testUid = rand(1, 1000);
161 $type = t3lib_collection_RecordCollectionRepository::TYPE_Static;
162
163 $this->databaseMock
164 ->expects($this->once())
165 ->method('exec_SELECTgetRows')
166 ->will($this->returnCallback(array($this, 'getRowsCallback')));
167 $this->getRowsCallbackReturnValue = array(
168 array('uid' => $testUid, 'type' => $type),
169 array('uid' => $testUid, 'type' => $type),
170 );
171
172 $objects = $this->fixture->findByType($type);
173
174 $this->assertEquals(2, count($objects));
175 $this->assertInstanceOf('t3lib_collection_StaticRecordCollection', $objects[0]);
176 $this->assertInstanceOf('t3lib_collection_StaticRecordCollection', $objects[1]);
177 }
178
179 /**
180 * @test
181 */
182 public function doesFindByTableNameReturnNull() {
183 $testTable = uniqid('sys_collection_');
184
185 $this->databaseMock
186 ->expects($this->once())
187 ->method('exec_SELECTgetRows')
188 ->will($this->returnCallback(array($this, 'getRowsCallback')));
189 $this->getRowsCallbackReturnValue = NULL;
190
191 $objects = $this->fixture->findByTableName($testTable);
192
193 $this->assertNull($objects);
194 }
195
196 /**
197 * @test
198 */
199 public function doesFindByTableNameReturnObjects() {
200 $testUid = rand(1, 1000);
201 $testTable = uniqid('sys_collection_');
202 $type = t3lib_collection_RecordCollectionRepository::TYPE_Static;
203
204 $this->databaseMock
205 ->expects($this->once())
206 ->method('exec_SELECTgetRows')
207 ->will($this->returnCallback(array($this, 'getRowsCallback')));
208 $this->getRowsCallbackReturnValue = array(
209 array('uid' => $testUid, 'type' => $type),
210 array('uid' => $testUid, 'type' => $type),
211 );
212
213 $objects = $this->fixture->findByTableName($testTable);
214
215 $this->assertEquals(2, count($objects));
216 $this->assertInstanceOf('t3lib_collection_StaticRecordCollection', $objects[0]);
217 $this->assertInstanceOf('t3lib_collection_StaticRecordCollection', $objects[1]);
218 }
219
220 /**
221 * @test
222 */
223 public function doesFindByTypeAndTableNameReturnNull() {
224 $testTable = uniqid('sys_collection_');
225 $type = t3lib_collection_RecordCollectionRepository::TYPE_Static;
226
227 $this->databaseMock
228 ->expects($this->once())
229 ->method('exec_SELECTgetRows')
230 ->will($this->returnCallback(array($this, 'getRowsCallback')));
231 $this->getRowsCallbackReturnValue = NULL;
232
233 $objects = $this->fixture->findByTypeAndTableName($type, $testTable);
234
235 $this->assertNull($objects);
236 }
237
238 /**
239 * @test
240 */
241 public function doesFindByTypeAndTableNameReturnObjects() {
242 $testUid = rand(1, 1000);
243 $testTable = uniqid('sys_collection_');
244 $type = t3lib_collection_RecordCollectionRepository::TYPE_Static;
245
246 $this->databaseMock
247 ->expects($this->once())
248 ->method('exec_SELECTgetRows')
249 ->will($this->returnCallback(array($this, 'getRowsCallback')));
250 $this->getRowsCallbackReturnValue = array(
251 array('uid' => $testUid, 'type' => $type),
252 array('uid' => $testUid, 'type' => $type),
253 );
254
255 $objects = $this->fixture->findByTypeAndTableName($type, $testTable);
256
257 $this->assertEquals(2, count($objects));
258 $this->assertInstanceOf('t3lib_collection_StaticRecordCollection', $objects[0]);
259 $this->assertInstanceOf('t3lib_collection_StaticRecordCollection', $objects[1]);
260 }
261
262 /**
263 * Callback for exec_SELECTgetSingleRow
264 *
265 * @param string $fields
266 * @param string $table
267 * @return NULL|array
268 */
269 public function getSingleRowCallback($fields, $table) {
270 if (!is_array($this->getSingleRowCallbackReturnValue) || $fields === '*') {
271 $returnValue = $this->getSingleRowCallbackReturnValue;
272 } else {
273 $returnValue = $this->limitRecordFields(
274 $fields,
275 $this->getSingleRowCallbackReturnValue
276 );
277 }
278
279 return $returnValue;
280 }
281
282 /**
283 * Callback for exec_SELECTgetRows
284 *
285 * @param string $fields
286 * @param string $table
287 * @return NULL|array
288 */
289 public function getRowsCallback($fields, $table) {
290 if (!is_array($this->getRowsCallbackReturnValue) || $fields === '*') {
291 $returnValue = $this->getRowsCallbackReturnValue;
292 } else {
293 $returnValue = array();
294
295 foreach ($this->getRowsCallbackReturnValue as $record) {
296 $returnValue[] = $this->limitRecordFields($fields, $record);
297 }
298 }
299
300 return $returnValue;
301 }
302
303 /**
304 * Limits record fields to a given field list.
305 *
306 * @param string $fields List of fields
307 * @param array $record The database record (or the simulated one)
308 * @return array
309 */
310 protected function limitRecordFields($fields, array $record) {
311 $result = array();
312
313 foreach ($record as $field => $value) {
314 if (t3lib_div::inList($fields, $field)) {
315 $result[$field] = $value;
316 }
317 }
318
319 return $result;
320 }
321 }
322 ?>