Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass $options in knp_pager.before event #342

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']);
}
}