Skip to content

Commit 3d73145

Browse files
Adjust applyViewModeLimit to use the tracked ViewModeSwitcher
1 parent c35e621 commit 3d73145

2 files changed

Lines changed: 78 additions & 3 deletions

File tree

src/Common/Controls.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,17 @@ public function createViewModeSwitcher(
7171
/**
7272
* Double the default item limit and page size for `minimal` view mode
7373
*
74-
* @param ViewModeSwitcher $viewModeSwitcher
7574
* @param LimitControl $limitControl
7675
* @param ?PaginationControl $paginationControl
7776
*
7877
* @return void
7978
*/
8079
protected function applyViewModeLimit(
81-
ViewModeSwitcher $viewModeSwitcher,
8280
LimitControl $limitControl,
8381
?PaginationControl $paginationControl = null
8482
): void {
85-
if ($viewModeSwitcher->getViewMode() === 'minimal') {
83+
$viewModeSwitcher = $this->getTrackedControl(ViewModeSwitcher::class);
84+
if ($viewModeSwitcher?->getViewMode() === 'minimal') {
8685
$limitControl->setDefaultLimit($limitControl->getDefaultLimit() * 2);
8786

8887
$paginationControl
@@ -105,6 +104,26 @@ protected function trackControl(Form $control): static
105104
return $this;
106105
}
107106

107+
/**
108+
* Get the tracked control of the given type
109+
*
110+
* @template TForm of Form
111+
*
112+
* @param class-string<TForm> $type
113+
*
114+
* @return ?TForm
115+
*/
116+
protected function getTrackedControl(string $type): ?Form
117+
{
118+
foreach ($this->trackedControls as $control) {
119+
if ($control instanceof $type) {
120+
return $control;
121+
}
122+
}
123+
124+
return null;
125+
}
126+
108127
/**
109128
* Get the Url {@see self::handleControls()} redirects to
110129
*

tests/Common/ControlsTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
namespace ipl\Tests\Web\Common;
44

55
use Icinga\Web\UrlParams;
6+
use InvalidArgumentException;
67
use ipl\Html\Form;
78
use ipl\I18n\NoopTranslator;
89
use ipl\I18n\StaticTranslator;
910
use ipl\Web\Common\Controls;
11+
use ipl\Web\Control\LimitControl;
12+
use ipl\Web\Control\PaginationControl;
1013
use ipl\Web\Control\ViewModeSwitcher;
1114
use ipl\Tests\Web\TestCase;
1215
use Psr\Http\Message\ServerRequestInterface;
@@ -111,6 +114,52 @@ public function testCreateViewModeSwitcherThrowsOnClassThatIsNoViewModeSwitcher(
111114
);
112115
}
113116

117+
public function testApplyViewModeLimitDoublesLimitsInMinimalMode(): void
118+
{
119+
$switcher = $this->createMock(ViewModeSwitcher::class);
120+
$switcher->method('getViewMode')->willReturn('minimal');
121+
122+
$limitControl = $this->createMock(LimitControl::class);
123+
$limitControl->method('getDefaultLimit')->willReturn(25);
124+
$limitControl->expects($this->once())->method('setDefaultLimit')->with(50);
125+
126+
$paginationControl = $this->createMock(PaginationControl::class);
127+
$paginationControl->method('getDefaultPageSize')->willReturn(25);
128+
$paginationControl->expects($this->once())
129+
->method('setDefaultPageSize')
130+
->with(50)
131+
->willReturnSelf();
132+
$paginationControl->expects($this->once())->method('apply')->willReturnSelf();
133+
134+
$controller = $this->controls();
135+
$controller->track($switcher);
136+
$controller->applyLimit($limitControl, $paginationControl);
137+
}
138+
139+
public function testApplyViewModeLimitLeavesLimitsUntouchedInNonMinimalMode(): void
140+
{
141+
$switcher = $this->createMock(ViewModeSwitcher::class);
142+
$switcher->method('getViewMode')->willReturn('common');
143+
144+
$limitControl = $this->createMock(LimitControl::class);
145+
$limitControl->expects($this->never())->method('setDefaultLimit');
146+
147+
$paginationControl = $this->createMock(PaginationControl::class);
148+
$paginationControl->expects($this->never())->method('setDefaultPageSize');
149+
150+
$controller = $this->controls();
151+
$controller->track($switcher);
152+
$controller->applyLimit($limitControl, $paginationControl);
153+
}
154+
155+
public function testApplyViewModeLimitDoesNothingWithoutTrackedSwitcher(): void
156+
{
157+
$limitControl = $this->createMock(LimitControl::class);
158+
$limitControl->expects($this->never())->method('setDefaultLimit');
159+
160+
$this->controls()->applyLimit($limitControl);
161+
}
162+
114163
public function testHandleControls(): void
115164
{
116165
$request = $this->createMock(ServerRequestInterface::class);
@@ -157,6 +206,13 @@ public function handle(ServerRequestInterface $request): void
157206
{
158207
$this->handleControls($request);
159208
}
209+
210+
public function applyLimit(
211+
LimitControl $limitControl,
212+
?PaginationControl $paginationControl = null
213+
): void {
214+
$this->applyViewModeLimit($limitControl, $paginationControl);
215+
}
160216
};
161217
}
162218
}

0 commit comments

Comments
 (0)