[TASK] Namespace and move t3lib unit tests - 3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Collection / RecordCollectionRepositoryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Collection;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012 Oliver Hader <oliver.hader@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 /**
31 * Test case for \TYPO3\CMS\Core\Collection\RecordCollectionRepository
32 *
33 * @author Oliver Hader <oliver.hader@typo3.org>
34 * @package TYPO3
35 * @subpackage t3lib
36 */
37 class RecordCollectionRepositoryTest extends \Tx_Phpunit_TestCase {
38
39 /**
40 * @var PHPUnit_Framework_MockObject_MockObject|t3lib_collection_RecordCollectionRepository
41 */
42 protected $fixture;
43
44 /**
45 * @var PHPUnit_Framework_MockObject_MockObject|t3lib_DB
46 */
47 protected $databaseMock;
48
49 /**
50 * @var NULL|array
51 */
52 protected $getSingleRowCallbackReturnValue;
53
54 /**
55 * @var NULL|array
56 */
57 protected $getRowsCallbackReturnValue;
58
59 /**
60 * @var string
61 */
62 protected $testTableName;
63
64 /**
65 * Sets up this test case.
66 */
67 protected function setUp() {
68 $this->databaseMock = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('exec_UPDATEquery', 'exec_SELECTgetSingleRow', 'exec_SELECTgetRows'));
69 $this->fixture = $this->getMock('TYPO3\\CMS\\Core\\Collection\\RecordCollectionRepository', array('getDatabase'));
70 $this->fixture->expects($this->any())->method('getDatabase')->will($this->returnValue($this->databaseMock));
71 $this->testTableName = uniqid('tx_testtable');
72 }
73
74 /**
75 * Cleans up this test case.
76 */
77 protected function tearDown() {
78 unset($this->databaseMock);
79 unset($this->fixture);
80 unset($this->getSingleRowCallbackReturnValue);
81 unset($this->getRowsCallbackReturnValue);
82 unset($this->testTableName);
83 }
84
85 /**
86 * @test
87 */
88 public function doesFindByUidReturnNull() {
89 $testUid = rand(1, 1000);
90 $this->databaseMock->expects($this->once())->method('exec_SELECTgetSingleRow')->will($this->returnCallback(array($this, 'getSingleRowCallback')));
91 $this->getSingleRowCallbackReturnValue = NULL;
92 $object = $this->fixture->findByUid($testUid);
93 $this->assertNull($object);
94 }
95
96 /**
97 * @test
98 */
99 public function doesFindByUidReturnObject() {
100 $testUid = rand(1, 1000);
101 $this->databaseMock->expects($this->once())->method('exec_SELECTgetSingleRow')->will($this->returnCallback(array($this, 'getSingleRowCallback')));
102 $this->getSingleRowCallbackReturnValue = array(
103 'uid' => $testUid,
104 'type' => \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static,
105 'table_name' => $this->testTableName
106 );
107 $object = $this->fixture->findByUid($testUid);
108 $this->assertInstanceOf('TYPO3\\CMS\\Core\\Collection\\StaticRecordCollection', $object);
109 }
110
111 /**
112 * @test
113 * @expectedException RuntimeException
114 */
115 public function doesFindByUidThrowException() {
116 $testUid = rand(1, 1000);
117 $this->databaseMock->expects($this->once())->method('exec_SELECTgetSingleRow')->will($this->returnCallback(array($this, 'getSingleRowCallback')));
118 $this->getSingleRowCallbackReturnValue = array(
119 'uid' => $testUid,
120 'type' => uniqid('unknown')
121 );
122 $object = $this->fixture->findByUid($testUid);
123 }
124
125 /**
126 * @test
127 */
128 public function doesFindByTypeReturnNull() {
129 $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
130 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
131 $this->getRowsCallbackReturnValue = NULL;
132 $objects = $this->fixture->findByType($type);
133 $this->assertNull($objects);
134 }
135
136 /**
137 * @test
138 */
139 public function doesFindByTypeReturnObjects() {
140 $testUid = rand(1, 1000);
141 $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
142 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
143 $this->getRowsCallbackReturnValue = array(
144 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName),
145 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName)
146 );
147 $objects = $this->fixture->findByType($type);
148 $this->assertEquals(2, count($objects));
149 $this->assertInstanceOf('TYPO3\\CMS\\Core\\Collection\\StaticRecordCollection', $objects[0]);
150 $this->assertInstanceOf('TYPO3\\CMS\\Core\\Collection\\StaticRecordCollection', $objects[1]);
151 }
152
153 /**
154 * @test
155 */
156 public function doesFindByTableNameReturnNull() {
157 $testTable = uniqid('sys_collection_');
158 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
159 $this->getRowsCallbackReturnValue = NULL;
160 $objects = $this->fixture->findByTableName($testTable);
161 $this->assertNull($objects);
162 }
163
164 /**
165 * @test
166 */
167 public function doesFindByTableNameReturnObjects() {
168 $testUid = rand(1, 1000);
169 $testTable = uniqid('sys_collection_');
170 $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
171 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
172 $this->getRowsCallbackReturnValue = array(
173 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName),
174 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName)
175 );
176 $objects = $this->fixture->findByTableName($testTable);
177 $this->assertEquals(2, count($objects));
178 $this->assertInstanceOf('TYPO3\\CMS\\Core\\Collection\\StaticRecordCollection', $objects[0]);
179 $this->assertInstanceOf('TYPO3\\CMS\\Core\\Collection\\StaticRecordCollection', $objects[1]);
180 }
181
182 /**
183 * @test
184 */
185 public function doesFindByTypeAndTableNameReturnNull() {
186 $testTable = uniqid('sys_collection_');
187 $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
188 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
189 $this->getRowsCallbackReturnValue = NULL;
190 $objects = $this->fixture->findByTypeAndTableName($type, $testTable);
191 $this->assertNull($objects);
192 }
193
194 /**
195 * @test
196 */
197 public function doesFindByTypeAndTableNameReturnObjects() {
198 $testUid = rand(1, 1000);
199 $testTable = uniqid('sys_collection_');
200 $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
201 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
202 $this->getRowsCallbackReturnValue = array(
203 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName),
204 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName)
205 );
206 $objects = $this->fixture->findByTypeAndTableName($type, $testTable);
207 $this->assertEquals(2, count($objects));
208 $this->assertInstanceOf('TYPO3\\CMS\\Core\\Collection\\StaticRecordCollection', $objects[0]);
209 $this->assertInstanceOf('TYPO3\\CMS\\Core\\Collection\\StaticRecordCollection', $objects[1]);
210 }
211
212 /**
213 * Callback for exec_SELECTgetSingleRow
214 *
215 * @param string $fields
216 * @param string $table
217 * @return NULL|array
218 */
219 public function getSingleRowCallback($fields, $table) {
220 if (!is_array($this->getSingleRowCallbackReturnValue) || $fields === '*') {
221 $returnValue = $this->getSingleRowCallbackReturnValue;
222 } else {
223 $returnValue = $this->limitRecordFields($fields, $this->getSingleRowCallbackReturnValue);
224 }
225 return $returnValue;
226 }
227
228 /**
229 * Callback for exec_SELECTgetRows
230 *
231 * @param string $fields
232 * @param string $table
233 * @return NULL|array
234 */
235 public function getRowsCallback($fields, $table) {
236 if (!is_array($this->getRowsCallbackReturnValue) || $fields === '*') {
237 $returnValue = $this->getRowsCallbackReturnValue;
238 } else {
239 $returnValue = array();
240 foreach ($this->getRowsCallbackReturnValue as $record) {
241 $returnValue[] = $this->limitRecordFields($fields, $record);
242 }
243 }
244 return $returnValue;
245 }
246
247 /**
248 * Limits record fields to a given field list.
249 *
250 * @param string $fields List of fields
251 * @param array $record The database record (or the simulated one)
252 * @return array
253 */
254 protected function limitRecordFields($fields, array $record) {
255 $result = array();
256 foreach ($record as $field => $value) {
257 if (\TYPO3\CMS\Core\Utility\GeneralUtility::inList($fields, $field)) {
258 $result[$field] = $value;
259 }
260 }
261 return $result;
262 }
263
264 }
265
266 ?>