‪TYPO3CMS  10.4
PaginateControllerTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
33 class ‪PaginateControllerTest extends UnitTestCase
34 {
38  protected ‪$query;
39 
43  protected ‪$controller;
44 
48  protected ‪$querySettings;
49 
54 
58  protected ‪$backend;
59 
63  protected ‪$dataMapper;
64 
68  protected function ‪setUp(): void
69  {
70  parent::setUp();
71  $this->query = $this->getAccessibleMock(Query::class, ['dummy'], ['someType']);
72  $this->querySettings = $this->createMock(QuerySettingsInterface::class);
73  $this->query->_set('querySettings', $this->querySettings);
74  $this->persistenceManager = $this->createMock(PersistenceManagerInterface::class);
75  $this->backend = $this->createMock(BackendInterface::class);
76  $this->query->_set('persistenceManager', $this->persistenceManager);
77  $this->dataMapper = $this->createMock(DataMapper::class);
78  $this->query->_set('dataMapper', $this->dataMapper);
79  $this->controller = $this->getAccessibleMock(
80  PaginateController::class,
81  ['dummy'],
82  [],
83  '',
84  false
85  );
86  $this->controller->_set('view', $this->createMock(ViewInterface::class));
87  }
88 
93  {
94  $this->controller->_set('maximumNumberOfLinks', 8);
95  $this->controller->_set('numberOfPages', 100);
96  $this->controller->_set('currentPage', 50);
97  $this->controller->_call('calculateDisplayRange');
98  self::assertSame(46, $this->controller->_get('displayRangeStart'));
99  self::assertSame(53, $this->controller->_get('displayRangeEnd'));
100  }
101 
106  {
107  $this->controller->_set('maximumNumberOfLinks', 7);
108  $this->controller->_set('numberOfPages', 100);
109  $this->controller->_set('currentPage', 50);
110  $this->controller->_call('calculateDisplayRange');
111  self::assertSame(47, $this->controller->_get('displayRangeStart'));
112  self::assertSame(53, $this->controller->_get('displayRangeEnd'));
113  }
114 
119  {
120  $this->controller->_set('maximumNumberOfLinks', 8);
121  $this->controller->_set('numberOfPages', 100);
122  $this->controller->_set('currentPage', 1);
123  $this->controller->_call('calculateDisplayRange');
124  self::assertSame(1, $this->controller->_get('displayRangeStart'));
125  self::assertSame(8, $this->controller->_get('displayRangeEnd'));
126  }
127 
132  {
133  $this->controller->_set('maximumNumberOfLinks', 7);
134  $this->controller->_set('numberOfPages', 100);
135  $this->controller->_set('currentPage', 1);
136  $this->controller->_call('calculateDisplayRange');
137  self::assertSame(1, $this->controller->_get('displayRangeStart'));
138  self::assertSame(7, $this->controller->_get('displayRangeEnd'));
139  }
140 
145  {
146  $this->controller->_set('maximumNumberOfLinks', 8);
147  $this->controller->_set('numberOfPages', 100);
148  $this->controller->_set('currentPage', 100);
149  $this->controller->_call('calculateDisplayRange');
150  self::assertSame(93, $this->controller->_get('displayRangeStart'));
151  self::assertSame(100, $this->controller->_get('displayRangeEnd'));
152  }
153 
158  {
159  $this->controller->_set('maximumNumberOfLinks', 7);
160  $this->controller->_set('numberOfPages', 100);
161  $this->controller->_set('currentPage', 100);
162  $this->controller->_call('calculateDisplayRange');
163  self::assertSame(94, $this->controller->_get('displayRangeStart'));
164  self::assertSame(100, $this->controller->_get('displayRangeEnd'));
165  }
166 
171  {
172  $mockQueryResult = $this->createMock(QueryResultInterface::class);
173  $mockQuery = $this->createMock(QueryInterface::class);
174  $mockQueryResult->expects(self::any())->method('getQuery')->willReturn($mockQuery);
175  $this->controller->_set('objects', $mockQueryResult);
176  $this->controller->_set('widgetConfiguration', ['as' => 'paginatedObjects']);
177  $this->controller->indexAction();
178  self::assertSame($mockQueryResult, $this->controller->_get('objects'));
179  }
180 
184  public function ‪acceptArrayAsObjects()
185  {
186  $objects = [];
187  $this->controller->_set('objects', $objects);
188  $this->controller->_set('widgetConfiguration', ['as' => 'paginatedObjects']);
189  $this->controller->indexAction();
190  self::assertSame($objects, $this->controller->_get('objects'));
191  }
192 
196  public function ‪acceptObjectStorageAsObjects()
197  {
198  $objects = new ‪ObjectStorage();
199  $this->controller->_set('objects', $objects);
200  $this->controller->_set('widgetConfiguration', ['as' => 'paginatedObjects']);
201  $this->controller->indexAction();
202  self::assertSame($objects, $this->controller->_get('objects'));
203  }
204 
209  {
210  $objects = new ‪ObjectStorage();
211  for ($i = 0; $i <= 25; $i++) {
212  $item = new \stdClass();
213  $objects->attach($item);
214  }
215  $this->controller->_set('objects', $objects);
216  $expectedPortion = [];
217  for ($j = 0; $j <= 9; $j++) {
218  $expectedPortion[] = $objects->toArray()[$j];
219  }
220  self::assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 0));
221  }
222 
227  {
228  $this->controller->_set('currentPage', 2);
229  $objects = new ‪ObjectStorage();
230  for ($i = 0; $i <= 55; $i++) {
231  $item = new \stdClass();
232  $objects->attach($item);
233  }
234  $this->controller->_set('objects', $objects);
235  $expectedPortion = [];
236  for ($j = 10; $j <= 19; $j++) {
237  $expectedPortion[] = $objects->toArray()[$j];
238  }
239  self::assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 10));
240  }
241 
246  {
247  $this->controller->_set('currentPage', 3);
248  $objects = new ‪ObjectStorage();
249  for ($i = 0; $i <= 25; $i++) {
250  $item = new \stdClass();
251  $objects->attach($item);
252  }
253  $this->controller->_set('objects', $objects);
254  $expectedPortion = [];
255  for ($j = 20; $j <= 25; $j++) {
256  $expectedPortion[] = $objects->toArray()[$j];
257  }
258  self::assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 20));
259  }
260 
265  {
266  $objects = [];
267  for ($i = 0; $i <= 25; $i++) {
268  $item = new \stdClass();
269  $objects[] = $item;
270  }
271  $this->controller->_set('objects', $objects);
272  $expectedPortion = [];
273  for ($j = 0; $j <= 9; $j++) {
274  $expectedPortion = array_slice($objects, 0, 10);
275  }
276  self::assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 0));
277  }
278 
283  {
284  $this->controller->_set('currentPage', 2);
285  $objects = [];
286  for ($i = 0; $i <= 55; $i++) {
287  $item = new \stdClass();
288  $objects[] = $item;
289  }
290  $this->controller->_set('objects', $objects);
291  $expectedPortion = [];
292  for ($j = 10; $j <= 19; $j++) {
293  $expectedPortion = array_slice($objects, 10, 10);
294  }
295  self::assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 10));
296  }
297 }
‪TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
Definition: PersistenceManagerInterface.php:22
‪TYPO3\CMS\Extbase\Persistence\QueryInterface
Definition: QueryInterface.php:29
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest\calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForOddMaximumNumberOfLinksWhenOnFirstPage
‪calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForOddMaximumNumberOfLinksWhenOnFirstPage()
Definition: PaginateControllerTest.php:125
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest\calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForEvenMaximumNumberOfLinksWhenOnFirstPage
‪calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForEvenMaximumNumberOfLinksWhenOnFirstPage()
Definition: PaginateControllerTest.php:112
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest\prepareObjectsSliceReturnsCorrectPortionForArrayAndFirstPage
‪prepareObjectsSliceReturnsCorrectPortionForArrayAndFirstPage()
Definition: PaginateControllerTest.php:258
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest\calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForEvenMaximumNumberOfLinksWhenOnLastPage
‪calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForEvenMaximumNumberOfLinksWhenOnLastPage()
Definition: PaginateControllerTest.php:138
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest\$backend
‪TYPO3 CMS Extbase Persistence Generic BackendInterface $backend
Definition: PaginateControllerTest.php:53
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
Definition: DataMapper.php:52
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller
Definition: PaginateControllerTest.php:16
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest\calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForEvenMaximumNumberOfLinks
‪calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForEvenMaximumNumberOfLinks()
Definition: PaginateControllerTest.php:86
‪TYPO3\CMS\Extbase\Persistence\ObjectStorage
Definition: ObjectStorage.php:28
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest\$controller
‪TYPO3 CMS Fluid ViewHelpers Widget Controller PaginateController PHPUnit Framework MockObject MockObject TYPO3 TestingFramework Core AccessibleObjectInterface $controller
Definition: PaginateControllerTest.php:41
‪TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
Definition: BackendInterface.php:26
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest\prepareObjectsSliceReturnsCorrectPortionForArrayAndSecondPage
‪prepareObjectsSliceReturnsCorrectPortionForArrayAndSecondPage()
Definition: PaginateControllerTest.php:276
‪TYPO3\CMS\Extbase\Persistence\QueryResultInterface
Definition: QueryResultInterface.php:22
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest\acceptArrayAsObjects
‪acceptArrayAsObjects()
Definition: PaginateControllerTest.php:178
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest\$persistenceManager
‪TYPO3 CMS Extbase Persistence PersistenceManagerInterface $persistenceManager
Definition: PaginateControllerTest.php:49
‪TYPO3\CMS\Extbase\Mvc\View\ViewInterface
Definition: ViewInterface.php:24
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest\calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForOddMaximumNumberOfLinks
‪calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForOddMaximumNumberOfLinks()
Definition: PaginateControllerTest.php:99
‪TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
Definition: QuerySettingsInterface.php:22
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest\prepareObjectsSliceReturnsCorrectPortionForObjectStorageAndSecondPage
‪prepareObjectsSliceReturnsCorrectPortionForObjectStorageAndSecondPage()
Definition: PaginateControllerTest.php:220
‪TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController
Definition: PaginateController.php:27
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest\prepareObjectsSliceReturnsCorrectPortionForObjectStorageAndLastPage
‪prepareObjectsSliceReturnsCorrectPortionForObjectStorageAndLastPage()
Definition: PaginateControllerTest.php:239
‪TYPO3\CMS\Extbase\Persistence\Generic\Query
Definition: Query.php:38
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest\acceptObjectStorageAsObjects
‪acceptObjectStorageAsObjects()
Definition: PaginateControllerTest.php:190
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest\prepareObjectsSliceReturnsCorrectPortionForObjectStorageAndFirstPage
‪prepareObjectsSliceReturnsCorrectPortionForObjectStorageAndFirstPage()
Definition: PaginateControllerTest.php:202
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest\setUp
‪setUp()
Definition: PaginateControllerTest.php:62
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest\$query
‪TYPO3 CMS Extbase Persistence Generic Query $query
Definition: PaginateControllerTest.php:37
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest\$querySettings
‪TYPO3 CMS Extbase Persistence Generic QuerySettingsInterface $querySettings
Definition: PaginateControllerTest.php:45
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest\$dataMapper
‪TYPO3 CMS Extbase Persistence Generic Mapper DataMapper $dataMapper
Definition: PaginateControllerTest.php:57
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest\acceptQueryResultInterfaceAsObjects
‪acceptQueryResultInterfaceAsObjects()
Definition: PaginateControllerTest.php:164
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest\calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForOddMaximumNumberOfLinksWhenOnLastPage
‪calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForOddMaximumNumberOfLinksWhenOnLastPage()
Definition: PaginateControllerTest.php:151
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller\PaginateControllerTest
Definition: PaginateControllerTest.php:34