Skip to content

Commit

Permalink
Pass $options in knp_pager.before event (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpdude authored Sep 20, 2024
1 parent 1dbd333 commit a1ccc00
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Knp/Component/Pager/Event/BeforeEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
*/
final class BeforeEvent extends Event
{
/**
* @var array<string, mixed>
*/
public array $options = [];

public function __construct(
private readonly EventDispatcherInterface $eventDispatcher,
private readonly ArgumentAccessInterface $argumentAccess,
Expand Down
1 change: 1 addition & 0 deletions src/Knp/Component/Pager/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public function paginate($target, int $page = 1, ?int $limit = null, array $opti

// before pagination start
$beforeEvent = new Event\BeforeEvent($this->eventDispatcher, $this->argumentAccess, $this->connection);
$beforeEvent->options = &$options;
$this->eventDispatcher->dispatch($beforeEvent, 'knp_pager.before');
// items
$itemsEvent = new Event\ItemsEvent($offset, $limit);
Expand Down
45 changes: 45 additions & 0 deletions tests/Test/Pager/PaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

namespace Test\Pager;

use Knp\Component\Pager\Event\BeforeEvent;
use Knp\Component\Pager\Event\ItemsEvent;
use Knp\Component\Pager\Event\Subscriber\Paginate\PaginationSubscriber;
use Knp\Component\Pager\Pagination\SlidingPagination;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Test\Tool\BaseTestCase;

final class PaginatorTest extends BaseTestCase
Expand All @@ -29,4 +33,45 @@ public function shouldFailToPaginateUnsupportedValue(): void
$paginator = $this->getPaginatorInstance(null, $dispatcher);
$paginator->paginate(null, 1, 10);
}

#[Test]
public function shouldPassOptionsToBeforeEventSubscriber(): void
{
$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new class implements EventSubscriberInterface {
public static function getSubscribedEvents(): array
{
return [
'knp_pager.before' => ['before', 1],
];
}
public function before(BeforeEvent $event): void
{
BaseTestCase::assertArrayHasKey('some_option', $event->options);
BaseTestCase::assertEquals('value', $event->options['some_option']);

$event->options['some_option'] = 'changed';
$event->options['extra_option'] = 'added';
}
});
$dispatcher->addSubscriber(new class implements EventSubscriberInterface {
public static function getSubscribedEvents(): array
{
return [
'knp_pager.items' => ['items', 1],
];
}
public function items(ItemsEvent $event): void
{
BaseTestCase::assertArrayHasKey('some_option', $event->options);
BaseTestCase::assertEquals('changed', $event->options['some_option']);
BaseTestCase::assertArrayHasKey('extra_option', $event->options);
BaseTestCase::assertEquals('added', $event->options['extra_option']);
}
});
$dispatcher->addSubscriber(new PaginationSubscriber());
$paginator = $this->getPaginatorInstance(null, $dispatcher);

$paginator->paginate([], 1, 10, ['some_option' => 'value']);
}
}

0 comments on commit a1ccc00

Please sign in to comment.