TYPO3 CMS  TYPO3_7-6
PaginateControllerTest.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License, either version 3 *
9  * of the License, or (at your option) any later version. *
10  * *
11  * The TYPO3 project - inspiring people to share! *
12  * */
14 
19 {
23  protected $query;
24 
28  protected $controller;
29 
33  protected $querySettings;
34 
39 
43  protected $backend;
44 
48  protected $dataMapper;
49 
55  protected function setUp()
56  {
57  $this->query = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Query::class, ['dummy'], ['someType']);
58  $this->querySettings = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface::class);
59  $this->query->_set('querySettings', $this->querySettings);
60  $this->persistenceManager = $this->getMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
61  $this->backend = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class);
62  $this->backend->expects($this->any())->method('getQomFactory')->will($this->returnValue(null));
63  $this->persistenceManager->expects($this->any())->method('getBackend')->will($this->returnValue($this->backend));
64  $this->query->_set('persistenceManager', $this->persistenceManager);
65  $this->dataMapper = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class);
66  $this->query->_set('dataMapper', $this->dataMapper);
67  $this->controller = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController::class,
68  ['dummy'], [], '', false);
69  $this->controller->_set('view', $this->getMock(\TYPO3\CMS\Extbase\Mvc\View\ViewInterface::class));
70  }
71 
76  {
77  $this->controller->_set('maximumNumberOfLinks', 8);
78  $this->controller->_set('numberOfPages', 100);
79  $this->controller->_set('currentPage', 50);
80  $this->controller->_call('calculateDisplayRange');
81  $this->assertSame(46, $this->controller->_get('displayRangeStart'));
82  $this->assertSame(53, $this->controller->_get('displayRangeEnd'));
83  }
84 
89  {
90  $this->controller->_set('maximumNumberOfLinks', 7);
91  $this->controller->_set('numberOfPages', 100);
92  $this->controller->_set('currentPage', 50);
93  $this->controller->_call('calculateDisplayRange');
94  $this->assertSame(47, $this->controller->_get('displayRangeStart'));
95  $this->assertSame(53, $this->controller->_get('displayRangeEnd'));
96  }
97 
102  {
103  $this->controller->_set('maximumNumberOfLinks', 8);
104  $this->controller->_set('numberOfPages', 100);
105  $this->controller->_set('currentPage', 1);
106  $this->controller->_call('calculateDisplayRange');
107  $this->assertSame(1, $this->controller->_get('displayRangeStart'));
108  $this->assertSame(8, $this->controller->_get('displayRangeEnd'));
109  }
110 
115  {
116  $this->controller->_set('maximumNumberOfLinks', 7);
117  $this->controller->_set('numberOfPages', 100);
118  $this->controller->_set('currentPage', 1);
119  $this->controller->_call('calculateDisplayRange');
120  $this->assertSame(1, $this->controller->_get('displayRangeStart'));
121  $this->assertSame(7, $this->controller->_get('displayRangeEnd'));
122  }
123 
128  {
129  $this->controller->_set('maximumNumberOfLinks', 8);
130  $this->controller->_set('numberOfPages', 100);
131  $this->controller->_set('currentPage', 100);
132  $this->controller->_call('calculateDisplayRange');
133  $this->assertSame(93, $this->controller->_get('displayRangeStart'));
134  $this->assertSame(100, $this->controller->_get('displayRangeEnd'));
135  }
136 
141  {
142  $this->controller->_set('maximumNumberOfLinks', 7);
143  $this->controller->_set('numberOfPages', 100);
144  $this->controller->_set('currentPage', 100);
145  $this->controller->_call('calculateDisplayRange');
146  $this->assertSame(94, $this->controller->_get('displayRangeStart'));
147  $this->assertSame(100, $this->controller->_get('displayRangeEnd'));
148  }
149 
154  {
155  $mockQueryResult = $this->getMock(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class);
156  $mockQuery = $this->getMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
157  $mockQueryResult->expects($this->any())->method('getQuery')->will($this->returnValue($mockQuery));
158  $this->controller->_set('objects', $mockQueryResult);
159  $this->controller->indexAction();
160  $this->assertSame($mockQueryResult, $this->controller->_get('objects'));
161  }
162 
166  public function acceptArrayAsObjects()
167  {
168  $objects = [];
169  $this->controller->_set('objects', $objects);
170  $this->controller->indexAction();
171  $this->assertSame($objects, $this->controller->_get('objects'));
172  }
173 
178  {
179  $objects = new ObjectStorage();
180  $this->controller->_set('objects', $objects);
181  $this->controller->indexAction();
182  $this->assertSame($objects, $this->controller->_get('objects'));
183  }
184 
189  {
190  $objects = new ObjectStorage();
191  for ($i = 0; $i <= 25; $i++) {
192  $item = new \StdClass;
193  $objects->attach($item);
194  }
195  $this->controller->_set('objects', $objects);
196  $expectedPortion = [];
197  for ($j = 0; $j <= 9; $j++) {
198  $expectedPortion[] = $objects->toArray()[$j];
199  }
200  $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 0));
201  }
202 
207  {
208  $this->controller->_set('currentPage', 2);
209  $objects = new ObjectStorage();
210  for ($i = 0; $i <= 55; $i++) {
211  $item = new \StdClass;
212  $objects->attach($item);
213  }
214  $this->controller->_set('objects', $objects);
215  $expectedPortion = [];
216  for ($j = 10; $j <= 19; $j++) {
217  $expectedPortion[] = $objects->toArray()[$j];
218  }
219  $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 10));
220  }
221 
226  {
227  $this->controller->_set('currentPage', 3);
228  $objects = new ObjectStorage();
229  for ($i = 0; $i <= 25; $i++) {
230  $item = new \stdClass;
231  $objects->attach($item);
232  }
233  $this->controller->_set('objects', $objects);
234  $expectedPortion = [];
235  for ($j = 20; $j <= 25; $j++) {
236  $expectedPortion[] = $objects->toArray()[$j];
237  }
238  $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 20));
239  }
240 
245  {
246  $objects = [];
247  for ($i = 0; $i <= 25; $i++) {
248  $item = new \StdClass;
249  $objects[] = $item;
250  }
251  $this->controller->_set('objects', $objects);
252  $expectedPortion = [];
253  for ($j = 0; $j <= 9; $j++) {
254  $expectedPortion = array_slice($objects, 0, 10);
255  }
256  $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 0));
257  }
258 
263  {
264  $this->controller->_set('currentPage', 2);
265  $objects = [];
266  for ($i = 0; $i <= 55; $i++) {
267  $item = new \StdClass;
268  $objects[] = $item;
269  }
270  $this->controller->_set('objects', $objects);
271  $expectedPortion = [];
272  for ($j = 10; $j <= 19; $j++) {
273  $expectedPortion = array_slice($objects, 10, 10);
274  }
275  $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 10));
276  }
277 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)