Skip to content

Commit b40b3c7

Browse files
author
Benjamin Georgeault
committed
Fix paginate subscriber when request stack is empty (eg. console).
1 parent dbfd824 commit b40b3c7

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/Subscriber/PaginateElasticaQuerySubscriber.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static function getSubscribedEvents()
7474
protected function setSorting(ItemsEvent $event)
7575
{
7676
$options = $event->options;
77-
$sortField = $this->getRequest()->get($options['sortFieldParameterName']);
77+
$sortField = $this->getFromRequest($options['sortFieldParameterName']);
7878

7979
if (!$sortField && isset($options['defaultSortFieldName'])) {
8080
$sortField = $options['defaultSortFieldName'];
@@ -117,7 +117,7 @@ protected function getSort($sortField, array $options = [])
117117
protected function getSortDirection($sortField, array $options = [])
118118
{
119119
$dir = 'asc';
120-
$sortDirection = $this->getRequest()->get($options['sortDirectionParameterName']);
120+
$sortDirection = $this->getFromRequest($options['sortDirectionParameterName']);
121121

122122
if (empty($sortDirection) && isset($options['defaultSortDirection'])) {
123123
$sortDirection = $options['defaultSortDirection'];
@@ -142,4 +142,17 @@ private function getRequest()
142142
{
143143
return $this->requestStack->getCurrentRequest();
144144
}
145+
146+
/**
147+
* @param string|null $key
148+
* @return mixed|null
149+
*/
150+
private function getFromRequest(?string $key)
151+
{
152+
if (null !== $request = $this->getRequest()) {
153+
return $request->get($key);
154+
}
155+
156+
return null;
157+
}
145158
}

tests/Unit/Subscriber/PaginateElasticaQuerySubscriberTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,22 @@ public function testShouldInvokeNestedFilterCallable()
271271
], $query->toArray());
272272
}
273273

274+
public function testShouldDoNothingIfNoRequest()
275+
{
276+
$subscriber = new PaginateElasticaQuerySubscriber($this->getRequestStack());
277+
278+
$adapter = $this->getAdapterMock();
279+
$adapter->expects($this->never())
280+
->method('getQuery');
281+
$adapter->method('getResults')
282+
->willReturn($this->getResultSetMock());
283+
284+
$event = new ItemsEvent(0, 10);
285+
$event->target = $adapter;
286+
287+
$subscriber->items($event);
288+
}
289+
274290
protected function getAdapterMock()
275291
{
276292
return $this->createMock(RawPaginatorAdapter::class);

0 commit comments

Comments
 (0)