Revert "[TASK] Namespace and move t3lib unit tests - 3"
[Packages/TYPO3.CMS.git] / tests / Unit / 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 * Test case for t3lib_collection_RecordCollectionRepository
29 *
30 * @author Oliver Hader <oliver.hader@typo3.org>
31 * @package TYPO3
32 * @subpackage t3lib
33 */
34 class t3lib_collection_RecordCollectionRepositoryTest extends Tx_Phpunit_TestCase {
35
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 * @var string
58 */
59 protected $testTableName;
60
61 /**
62 * Sets up this test case.
63 */
64 protected function setUp() {
65 $this->databaseMock = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('exec_UPDATEquery', 'exec_SELECTgetSingleRow', 'exec_SELECTgetRows'));
66 $this->fixture = $this->getMock('TYPO3\\CMS\\Core\\Collection\\RecordCollectionRepository', array('getDatabase'));
67 $this->fixture->expects($this->any())->method('getDatabase')->will($this->returnValue($this->databaseMock));
68 $this->testTableName = uniqid('tx_testtable');
69 }
70
71 /**
72 * Cleans up this test case.
73 */
74 protected function tearDown() {
75 unset($this->databaseMock);
76 unset($this->fixture);
77 unset($this->getSingleRowCallbackReturnValue);
78 unset($this->getRowsCallbackReturnValue);
79 unset($this->testTableName);
80 }
81
82 /**
83 * @test
84 */
85 public function doesFindByUidReturnNull() {
86 $testUid = rand(1, 1000);
87 $this->databaseMock->expects($this->once())->method('exec_SELECTgetSingleRow')->will($this->returnCallback(array($this, 'getSingleRowCallback')));
88 $this->getSingleRowCallbackReturnValue = NULL;
89 $object = $this->fixture->findByUid($testUid);
90 $this->assertNull($object);
91 }
92
93 /**
94 * @test
95 */
96 public function doesFindByUidReturnObject() {
97 $testUid = rand(1, 1000);
98 $this->databaseMock->expects($this->once())->method('exec_SELECTgetSingleRow')->will($this->returnCallback(array($this, 'getSingleRowCallback')));
99 $this->getSingleRowCallbackReturnValue = array(
100 'uid' => $testUid,
101 'type' => \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static,
102 'table_name' => $this->testTableName
103 );
104 $object = $this->fixture->findByUid($testUid);
105 $this->assertInstanceOf('TYPO3\\CMS\\Core\\Collection\\StaticRecordCollection', $object);
106 }
107
108 /**
109 * @test
110 * @expectedException RuntimeException
111 */
112 public function doesFindByUidThrowException() {
113 $testUid = rand(1, 1000);
114 $this->databaseMock->expects($this->once())->method('exec_SELECTgetSingleRow')->will($this->returnCallback(array($this, 'getSingleRowCallback')));
115 $this->getSingleRowCallbackReturnValue = array(
116 'uid' => $testUid,
117 'type' => uniqid('unknown')
118 );
119 $object = $this->fixture->findByUid($testUid);
120 }
121
122 /**
123 * @test
124 */
125 public function doesFindByTypeReturnNull() {
126 $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
127 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
128 $this->getRowsCallbackReturnValue = NULL;
129 $objects = $this->fixture->findByType($type);
130 $this->assertNull($objects);
131 }
132
133 /**
134 * @test
135 */
136 public function doesFindByTypeReturnObjects() {
137 $testUid = rand(1, 1000);
138 $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
139 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
140 $this->getRowsCallbackReturnValue = array(
141 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName),
142 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName)
143 );
144 $objects = $this->fixture->findByType($type);
145 $this->assertEquals(2, count($objects));
146 $this->assertInstanceOf('TYPO3\\CMS\\Core\\Collection\\StaticRecordCollection', $objects[0]);
147 $this->assertInstanceOf('TYPO3\\CMS\\Core\\Collection\\StaticRecordCollection', $objects[1]);
148 }
149
150 /**
151 * @test
152 */
153 public function doesFindByTableNameReturnNull() {
154 $testTable = uniqid('sys_collection_');
155 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
156 $this->getRowsCallbackReturnValue = NULL;
157 $objects = $this->fixture->findByTableName($testTable);
158 $this->assertNull($objects);
159 }
160
161 /**
162 * @test
163 */
164 public function doesFindByTableNameReturnObjects() {
165 $testUid = rand(1, 1000);
166 $testTable = uniqid('sys_collection_');
167 $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
168 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
169 $this->getRowsCallbackReturnValue = array(
170 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName),
171 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName)
172 );
173 $objects = $this->fixture->findByTableName($testTable);
174 $this->assertEquals(2, count($objects));
175 $this->assertInstanceOf('TYPO3\\CMS\\Core\\Collection\\StaticRecordCollection', $objects[0]);
176 $this->assertInstanceOf('TYPO3\\CMS\\Core\\Collection\\StaticRecordCollection', $objects[1]);
177 }
178
179 /**
180 * @test
181 */
182 public function doesFindByTypeAndTableNameReturnNull() {
183 $testTable = uniqid('sys_collection_');
184 $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
185 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
186 $this->getRowsCallbackReturnValue = NULL;
187 $objects = $this->fixture->findByTypeAndTableName($type, $testTable);
188 $this->assertNull($objects);
189 }
190
191 /**
192 * @test
193 */
194 public function doesFindByTypeAndTableNameReturnObjects() {
195 $testUid = rand(1, 1000);
196 $testTable = uniqid('sys_collection_');
197 $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
198 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
199 $this->getRowsCallbackReturnValue = array(
200 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName),
201 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName)
202 );
203 $objects = $this->fixture->findByTypeAndTableName($type, $testTable);
204 $this->assertEquals(2, count($objects));
205 $this->assertInstanceOf('TYPO3\\CMS\\Core\\Collection\\StaticRecordCollection', $objects[0]);
206 $this->assertInstanceOf('TYPO3\\CMS\\Core\\Collection\\StaticRecordCollection', $objects[1]);
207 }
208
209 /**
210 * Callback for exec_SELECTgetSingleRow
211 *
212 * @param string $fields
213 * @param string $table
214 * @return NULL|array
215 */
216 public function getSingleRowCallback($fields, $table) {
217 if (!is_array($this->getSingleRowCallbackReturnValue) || $fields === '*') {
218 $returnValue = $this->getSingleRowCallbackReturnValue;
219 } else {
220 $returnValue = $this->limitRecordFields($fields, $this->getSingleRowCallbackReturnValue);
221 }
222 return $returnValue;
223 }
224
225 /**
226 * Callback for exec_SELECTgetRows
227 *
228 * @param string $fields
229 * @param string $table
230 * @return NULL|array
231 */
232 public function getRowsCallback($fields, $table) {
233 if (!is_array($this->getRowsCallbackReturnValue) || $fields === '*') {
234 $returnValue = $this->getRowsCallbackReturnValue;
235 } else {
236 $returnValue = array();
237 foreach ($this->getRowsCallbackReturnValue as $record) {
238 $returnValue[] = $this->limitRecordFields($fields, $record);
239 }
240 }
241 return $returnValue;
242 }
243
244 /**
245 * Limits record fields to a given field list.
246 *
247 * @param string $fields List of fields
248 * @param array $record The database record (or the simulated one)
249 * @return array
250 */
251 protected function limitRecordFields($fields, array $record) {
252 $result = array();
253 foreach ($record as $field => $value) {
254 if (\TYPO3\CMS\Core\Utility\GeneralUtility::inList($fields, $field)) {
255 $result[$field] = $value;
256 }
257 }
258 return $result;
259 }
260
261 }
262
263 ?>