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