[TASK] Doctrine: Migrate exec_SELECTgetSingleRow
[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 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use Prophecy\Argument;
17 use TYPO3\CMS\Core\Database\Connection;
18 use TYPO3\CMS\Core\Database\ConnectionPool;
19 use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
20 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * Test case for \TYPO3\CMS\Core\Collection\RecordCollectionRepository
25 */
26 class RecordCollectionRepositoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
27 {
28 /**
29 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Collection\RecordCollectionRepository
30 */
31 protected $subject;
32
33 /**
34 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Database\DatabaseConnection
35 */
36 protected $databaseMock;
37
38 /**
39 * @var NULL|array
40 */
41 protected $getSingleRowCallbackReturnValue;
42
43 /**
44 * @var NULL|array
45 */
46 protected $getRowsCallbackReturnValue;
47
48 /**
49 * @var string
50 */
51 protected $testTableName;
52
53 /**
54 * @var array
55 */
56 protected $singletonInstances;
57
58 /**
59 * Sets up this test case.
60 */
61 protected function setUp()
62 {
63 $this->singletonInstances = GeneralUtility::getSingletonInstances();
64
65 // Setup mocking for BackendUtility::deleteClause()
66 $connectionProphet = $this->prophesize(Connection::class);
67 $connectionProphet->quoteIdentifier(Argument::cetera())->willReturnArgument(0);
68
69 $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
70 $queryBuilderProphet->expr()->willReturn(
71 GeneralUtility::makeInstance(ExpressionBuilder::class, $connectionProphet->reveal())
72 );
73
74 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
75 $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
76 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
77
78 $this->databaseMock = $this->getMockBuilder(\TYPO3\CMS\Core\Database\DatabaseConnection::class)
79 ->setMethods(array('exec_UPDATEquery', 'exec_SELECTgetSingleRow', 'exec_SELECTgetRows', 'fullQuoteStr'))
80 ->getMock();
81 $this->subject = $this->getMockBuilder(\TYPO3\CMS\Core\Collection\RecordCollectionRepository::class)
82 ->setMethods(array('getDatabaseConnection'))
83 ->getMock();
84 $this->subject->expects($this->any())
85 ->method('getDatabaseConnection')
86 ->will($this->returnValue($this->databaseMock));
87 $this->testTableName = $this->getUniqueId('tx_testtable');
88 }
89
90 protected function tearDown()
91 {
92 GeneralUtility::purgeInstances();
93 GeneralUtility::resetSingletonInstances($this->singletonInstances);
94 parent::tearDown();
95 }
96
97 /**
98 * @test
99 */
100 public function doesFindByTypeReturnNull()
101 {
102 $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
103 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
104 $this->getRowsCallbackReturnValue = null;
105 $objects = $this->subject->findByType($type);
106 $this->assertNull($objects);
107 }
108
109 /**
110 * @test
111 */
112 public function doesFindByTypeReturnObjects()
113 {
114 $testUid = rand(1, 1000);
115 $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
116 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
117 $this->getRowsCallbackReturnValue = array(
118 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName),
119 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName)
120 );
121 $objects = $this->subject->findByType($type);
122 $this->assertEquals(2, count($objects));
123 $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[0]);
124 $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[1]);
125 }
126
127 /**
128 * @test
129 */
130 public function doesFindByTableNameReturnNull()
131 {
132 $testTable = $this->getUniqueId('sys_collection_');
133 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
134 $this->getRowsCallbackReturnValue = null;
135 $objects = $this->subject->findByTableName($testTable);
136 $this->assertNull($objects);
137 }
138
139 /**
140 * @test
141 */
142 public function doesFindByTableNameReturnObjects()
143 {
144 $testUid = rand(1, 1000);
145 $testTable = $this->getUniqueId('sys_collection_');
146 $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
147 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
148 $this->getRowsCallbackReturnValue = array(
149 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName),
150 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName)
151 );
152 $objects = $this->subject->findByTableName($testTable);
153 $this->assertEquals(2, count($objects));
154 $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[0]);
155 $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[1]);
156 }
157
158 /**
159 * @test
160 */
161 public function doesFindByTypeAndTableNameReturnNull()
162 {
163 $testTable = $this->getUniqueId('sys_collection_');
164 $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
165 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
166 $this->getRowsCallbackReturnValue = null;
167 $objects = $this->subject->findByTypeAndTableName($type, $testTable);
168 $this->assertNull($objects);
169 }
170
171 /**
172 * @test
173 */
174 public function doesFindByTypeAndTableNameReturnObjects()
175 {
176 $testUid = rand(1, 1000);
177 $testTable = $this->getUniqueId('sys_collection_');
178 $type = \TYPO3\CMS\Core\Collection\RecordCollectionRepository::TYPE_Static;
179 $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback(array($this, 'getRowsCallback')));
180 $this->getRowsCallbackReturnValue = array(
181 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName),
182 array('uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName)
183 );
184 $objects = $this->subject->findByTypeAndTableName($type, $testTable);
185 $this->assertEquals(2, count($objects));
186 $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[0]);
187 $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[1]);
188 }
189
190 /**
191 * Callback for exec_SELECTgetSingleRow
192 *
193 * @param string $fields
194 * @param string $table
195 * @return NULL|array
196 */
197 public function getSingleRowCallback($fields, $table)
198 {
199 if (!is_array($this->getSingleRowCallbackReturnValue) || $fields === '*') {
200 $returnValue = $this->getSingleRowCallbackReturnValue;
201 } else {
202 $returnValue = $this->limitRecordFields($fields, $this->getSingleRowCallbackReturnValue);
203 }
204 return $returnValue;
205 }
206
207 /**
208 * Callback for exec_SELECTgetRows
209 *
210 * @param string $fields
211 * @param string $table
212 * @return NULL|array
213 */
214 public function getRowsCallback($fields, $table)
215 {
216 if (!is_array($this->getRowsCallbackReturnValue) || $fields === '*') {
217 $returnValue = $this->getRowsCallbackReturnValue;
218 } else {
219 $returnValue = array();
220 foreach ($this->getRowsCallbackReturnValue as $record) {
221 $returnValue[] = $this->limitRecordFields($fields, $record);
222 }
223 }
224 return $returnValue;
225 }
226
227 /**
228 * Limits record fields to a given field list.
229 *
230 * @param string $fields List of fields
231 * @param array $record The database record (or the simulated one)
232 * @return array
233 */
234 protected function limitRecordFields($fields, array $record)
235 {
236 $result = array();
237 foreach ($record as $field => $value) {
238 if (\TYPO3\CMS\Core\Utility\GeneralUtility::inList($fields, $field)) {
239 $result[$field] = $value;
240 }
241 }
242 return $result;
243 }
244 }