TYPO3 CMS  TYPO3_7-6
RecordCollectionRepositoryTest.php
Go to the documentation of this file.
1 <?php
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 
21 {
25  protected $subject;
26 
30  protected $databaseMock;
31 
36 
41 
45  protected $testTableName;
46 
50  protected function setUp()
51  {
52  $this->databaseMock = $this->getMock(
53  \TYPO3\CMS\Core\Database\DatabaseConnection::class,
54  ['exec_UPDATEquery', 'exec_SELECTgetSingleRow', 'exec_SELECTgetRows', 'fullQuoteStr']
55  );
56  $this->subject = $this->getMock(\TYPO3\CMS\Core\Collection\RecordCollectionRepository::class, ['getDatabaseConnection']);
57  $this->subject->expects($this->any())->method('getDatabaseConnection')->will($this->returnValue($this->databaseMock));
58  $this->testTableName = $this->getUniqueId('tx_testtable');
59  }
60 
64  public function doesFindByUidReturnNull()
65  {
66  $testUid = rand(1, 1000);
67  $this->databaseMock->expects($this->once())->method('exec_SELECTgetSingleRow')->will($this->returnCallback([$this, 'getSingleRowCallback']));
68  $this->getSingleRowCallbackReturnValue = null;
69  $object = $this->subject->findByUid($testUid);
70  $this->assertNull($object);
71  }
72 
76  public function doesFindByUidReturnObject()
77  {
78  $testUid = rand(1, 1000);
79  $this->databaseMock->expects($this->once())->method('exec_SELECTgetSingleRow')->will($this->returnCallback([$this, 'getSingleRowCallback']));
80  $this->getSingleRowCallbackReturnValue = [
81  'uid' => $testUid,
83  'table_name' => $this->testTableName
84  ];
85  $object = $this->subject->findByUid($testUid);
86  $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $object);
87  }
88 
93  public function doesFindByUidThrowException()
94  {
95  $testUid = rand(1, 1000);
96  $this->databaseMock->expects($this->once())->method('exec_SELECTgetSingleRow')->will($this->returnCallback([$this, 'getSingleRowCallback']));
97  $this->getSingleRowCallbackReturnValue = [
98  'uid' => $testUid,
99  'type' => $this->getUniqueId('unknown')
100  ];
101  $object = $this->subject->findByUid($testUid);
102  }
103 
107  public function doesFindByTypeReturnNull()
108  {
110  $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback([$this, 'getRowsCallback']));
111  $this->getRowsCallbackReturnValue = null;
112  $objects = $this->subject->findByType($type);
113  $this->assertNull($objects);
114  }
115 
119  public function doesFindByTypeReturnObjects()
120  {
121  $testUid = rand(1, 1000);
123  $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback([$this, 'getRowsCallback']));
124  $this->getRowsCallbackReturnValue = [
125  ['uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName],
126  ['uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName]
127  ];
128  $objects = $this->subject->findByType($type);
129  $this->assertEquals(2, count($objects));
130  $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[0]);
131  $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[1]);
132  }
133 
138  {
139  $testTable = $this->getUniqueId('sys_collection_');
140  $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback([$this, 'getRowsCallback']));
141  $this->getRowsCallbackReturnValue = null;
142  $objects = $this->subject->findByTableName($testTable);
143  $this->assertNull($objects);
144  }
145 
150  {
151  $testUid = rand(1, 1000);
152  $testTable = $this->getUniqueId('sys_collection_');
154  $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback([$this, 'getRowsCallback']));
155  $this->getRowsCallbackReturnValue = [
156  ['uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName],
157  ['uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName]
158  ];
159  $objects = $this->subject->findByTableName($testTable);
160  $this->assertEquals(2, count($objects));
161  $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[0]);
162  $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[1]);
163  }
164 
169  {
170  $testTable = $this->getUniqueId('sys_collection_');
172  $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback([$this, 'getRowsCallback']));
173  $this->getRowsCallbackReturnValue = null;
174  $objects = $this->subject->findByTypeAndTableName($type, $testTable);
175  $this->assertNull($objects);
176  }
177 
182  {
183  $testUid = rand(1, 1000);
184  $testTable = $this->getUniqueId('sys_collection_');
186  $this->databaseMock->expects($this->once())->method('exec_SELECTgetRows')->will($this->returnCallback([$this, 'getRowsCallback']));
187  $this->getRowsCallbackReturnValue = [
188  ['uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName],
189  ['uid' => $testUid, 'type' => $type, 'table_name' => $this->testTableName]
190  ];
191  $objects = $this->subject->findByTypeAndTableName($type, $testTable);
192  $this->assertEquals(2, count($objects));
193  $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[0]);
194  $this->assertInstanceOf(\TYPO3\CMS\Core\Collection\StaticRecordCollection::class, $objects[1]);
195  }
196 
204  public function getSingleRowCallback($fields, $table)
205  {
206  if (!is_array($this->getSingleRowCallbackReturnValue) || $fields === '*') {
208  } else {
209  $returnValue = $this->limitRecordFields($fields, $this->getSingleRowCallbackReturnValue);
210  }
211  return $returnValue;
212  }
213 
221  public function getRowsCallback($fields, $table)
222  {
223  if (!is_array($this->getRowsCallbackReturnValue) || $fields === '*') {
224  $returnValue = $this->getRowsCallbackReturnValue;
225  } else {
226  $returnValue = [];
227  foreach ($this->getRowsCallbackReturnValue as $record) {
228  $returnValue[] = $this->limitRecordFields($fields, $record);
229  }
230  }
231  return $returnValue;
232  }
233 
241  protected function limitRecordFields($fields, array $record)
242  {
243  $result = [];
244  foreach ($record as $field => $value) {
245  if (\TYPO3\CMS\Core\Utility\GeneralUtility::inList($fields, $field)) {
246  $result[$field] = $value;
247  }
248  }
249  return $result;
250  }
251 }