[TASK] Shorten the extend class in all unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / Widget / Controller / PaginateControllerTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Widget\Controller;
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 TYPO3\CMS\Extbase\Persistence\ObjectStorage;
17 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
18
19 /**
20 * Test case
21 */
22 class PaginateControllerTest extends UnitTestCase
23 {
24 /**
25 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Query
26 */
27 protected $query;
28
29 /**
30 * @var \TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface
31 */
32 protected $controller;
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
36 */
37 protected $querySettings;
38
39 /**
40 * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
41 */
42 protected $persistenceManager;
43
44 /**
45 * @var \TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface
46 */
47 protected $backend;
48
49 /**
50 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
51 */
52 protected $dataMapper;
53
54 /**
55 * Sets up this test case
56 */
57 protected function setUp()
58 {
59 $this->query = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Query::class, ['dummy'], ['someType']);
60 $this->querySettings = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface::class);
61 $this->query->_set('querySettings', $this->querySettings);
62 $this->persistenceManager = $this->createMock(\TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface::class);
63 $this->backend = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\BackendInterface::class);
64 $this->query->_set('persistenceManager', $this->persistenceManager);
65 $this->dataMapper = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class);
66 $this->query->_set('dataMapper', $this->dataMapper);
67 $this->controller = $this->getAccessibleMock(
68 \TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller\PaginateController::class,
69 ['dummy'],
70 [],
71 '',
72 false
73 );
74 $this->controller->_set('view', $this->createMock(\TYPO3\CMS\Extbase\Mvc\View\ViewInterface::class));
75 }
76
77 /**
78 * @test
79 */
80 public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForEvenMaximumNumberOfLinks()
81 {
82 $this->controller->_set('maximumNumberOfLinks', 8);
83 $this->controller->_set('numberOfPages', 100);
84 $this->controller->_set('currentPage', 50);
85 $this->controller->_call('calculateDisplayRange');
86 $this->assertSame(46, $this->controller->_get('displayRangeStart'));
87 $this->assertSame(53, $this->controller->_get('displayRangeEnd'));
88 }
89
90 /**
91 * @test
92 */
93 public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForOddMaximumNumberOfLinks()
94 {
95 $this->controller->_set('maximumNumberOfLinks', 7);
96 $this->controller->_set('numberOfPages', 100);
97 $this->controller->_set('currentPage', 50);
98 $this->controller->_call('calculateDisplayRange');
99 $this->assertSame(47, $this->controller->_get('displayRangeStart'));
100 $this->assertSame(53, $this->controller->_get('displayRangeEnd'));
101 }
102
103 /**
104 * @test
105 */
106 public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForEvenMaximumNumberOfLinksWhenOnFirstPage()
107 {
108 $this->controller->_set('maximumNumberOfLinks', 8);
109 $this->controller->_set('numberOfPages', 100);
110 $this->controller->_set('currentPage', 1);
111 $this->controller->_call('calculateDisplayRange');
112 $this->assertSame(1, $this->controller->_get('displayRangeStart'));
113 $this->assertSame(8, $this->controller->_get('displayRangeEnd'));
114 }
115
116 /**
117 * @test
118 */
119 public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForOddMaximumNumberOfLinksWhenOnFirstPage()
120 {
121 $this->controller->_set('maximumNumberOfLinks', 7);
122 $this->controller->_set('numberOfPages', 100);
123 $this->controller->_set('currentPage', 1);
124 $this->controller->_call('calculateDisplayRange');
125 $this->assertSame(1, $this->controller->_get('displayRangeStart'));
126 $this->assertSame(7, $this->controller->_get('displayRangeEnd'));
127 }
128
129 /**
130 * @test
131 */
132 public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForEvenMaximumNumberOfLinksWhenOnLastPage()
133 {
134 $this->controller->_set('maximumNumberOfLinks', 8);
135 $this->controller->_set('numberOfPages', 100);
136 $this->controller->_set('currentPage', 100);
137 $this->controller->_call('calculateDisplayRange');
138 $this->assertSame(93, $this->controller->_get('displayRangeStart'));
139 $this->assertSame(100, $this->controller->_get('displayRangeEnd'));
140 }
141
142 /**
143 * @test
144 */
145 public function calculateDisplayRangeDeterminesCorrectDisplayRangeStartAndEndForOddMaximumNumberOfLinksWhenOnLastPage()
146 {
147 $this->controller->_set('maximumNumberOfLinks', 7);
148 $this->controller->_set('numberOfPages', 100);
149 $this->controller->_set('currentPage', 100);
150 $this->controller->_call('calculateDisplayRange');
151 $this->assertSame(94, $this->controller->_get('displayRangeStart'));
152 $this->assertSame(100, $this->controller->_get('displayRangeEnd'));
153 }
154
155 /**
156 * @test
157 */
158 public function acceptQueryResultInterfaceAsObjects()
159 {
160 $mockQueryResult = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryResultInterface::class);
161 $mockQuery = $this->createMock(\TYPO3\CMS\Extbase\Persistence\QueryInterface::class);
162 $mockQueryResult->expects($this->any())->method('getQuery')->will($this->returnValue($mockQuery));
163 $this->controller->_set('objects', $mockQueryResult);
164 $this->controller->indexAction();
165 $this->assertSame($mockQueryResult, $this->controller->_get('objects'));
166 }
167
168 /**
169 * @test
170 */
171 public function acceptArrayAsObjects()
172 {
173 $objects = [];
174 $this->controller->_set('objects', $objects);
175 $this->controller->indexAction();
176 $this->assertSame($objects, $this->controller->_get('objects'));
177 }
178
179 /**
180 * @test
181 */
182 public function acceptObjectStorageAsObjects()
183 {
184 $objects = new ObjectStorage();
185 $this->controller->_set('objects', $objects);
186 $this->controller->indexAction();
187 $this->assertSame($objects, $this->controller->_get('objects'));
188 }
189
190 /**
191 * @test
192 */
193 public function prepareObjectsSliceReturnsCorrectPortionForObjectStorageAndFirstPage()
194 {
195 $objects = new ObjectStorage();
196 for ($i = 0; $i <= 25; $i++) {
197 $item = new \stdClass;
198 $objects->attach($item);
199 }
200 $this->controller->_set('objects', $objects);
201 $expectedPortion = [];
202 for ($j = 0; $j <= 9; $j++) {
203 $expectedPortion[] = $objects->toArray()[$j];
204 }
205 $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 0));
206 }
207
208 /**
209 * @test
210 */
211 public function prepareObjectsSliceReturnsCorrectPortionForObjectStorageAndSecondPage()
212 {
213 $this->controller->_set('currentPage', 2);
214 $objects = new ObjectStorage();
215 for ($i = 0; $i <= 55; $i++) {
216 $item = new \stdClass;
217 $objects->attach($item);
218 }
219 $this->controller->_set('objects', $objects);
220 $expectedPortion = [];
221 for ($j = 10; $j <= 19; $j++) {
222 $expectedPortion[] = $objects->toArray()[$j];
223 }
224 $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 10));
225 }
226
227 /**
228 * @test
229 */
230 public function prepareObjectsSliceReturnsCorrectPortionForObjectStorageAndLastPage()
231 {
232 $this->controller->_set('currentPage', 3);
233 $objects = new ObjectStorage();
234 for ($i = 0; $i <= 25; $i++) {
235 $item = new \stdClass;
236 $objects->attach($item);
237 }
238 $this->controller->_set('objects', $objects);
239 $expectedPortion = [];
240 for ($j = 20; $j <= 25; $j++) {
241 $expectedPortion[] = $objects->toArray()[$j];
242 }
243 $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 20));
244 }
245
246 /**
247 * @test
248 */
249 public function prepareObjectsSliceReturnsCorrectPortionForArrayAndFirstPage()
250 {
251 $objects = [];
252 for ($i = 0; $i <= 25; $i++) {
253 $item = new \stdClass;
254 $objects[] = $item;
255 }
256 $this->controller->_set('objects', $objects);
257 $expectedPortion = [];
258 for ($j = 0; $j <= 9; $j++) {
259 $expectedPortion = array_slice($objects, 0, 10);
260 }
261 $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 0));
262 }
263
264 /**
265 * @test
266 */
267 public function prepareObjectsSliceReturnsCorrectPortionForArrayAndSecondPage()
268 {
269 $this->controller->_set('currentPage', 2);
270 $objects = [];
271 for ($i = 0; $i <= 55; $i++) {
272 $item = new \stdClass;
273 $objects[] = $item;
274 }
275 $this->controller->_set('objects', $objects);
276 $expectedPortion = [];
277 for ($j = 10; $j <= 19; $j++) {
278 $expectedPortion = array_slice($objects, 10, 10);
279 }
280 $this->assertSame($expectedPortion, $this->controller->_call('prepareObjectsSlice', 10, 10));
281 }
282 }