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

Add storage for filters #342

Merged
merged 5 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions src/Bundle/Resources/config/services/filters.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
<services>
<defaults public="true" />

<service id="sylius.grid.filter_storage.session" class="Sylius\Bundle\GridBundle\Storage\SessionFilterStorage" public="false">
<argument type="service" id="request_stack" />
</service>
<service id="sylius.grid.filter_storage" alias="sylius.grid.filter_storage.session" />
<service id="Sylius\Component\Grid\Filter\Storage\FilterStorageInterface" alias="sylius.grid.filter_storage" />

<service id="sylius.form_registry.grid_filter" class="Sylius\Bundle\GridBundle\Form\Registry\FormTypeRegistry" public="false" />

<service id="Sylius\Component\Grid\Filter\StringFilter">
Expand Down
23 changes: 23 additions & 0 deletions src/Component/Filter/Storage/FilterStorageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Component\Grid\Filter\Storage;

interface FilterStorageInterface
{
public function set(array $filters): void;

public function all(): array;

public function hasFilters(): bool;
}
GSadee marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Component\Grid\Symfony\Session\Filter\Storage;
GSadee marked this conversation as resolved.
Show resolved Hide resolved

use Sylius\Component\Grid\Filter\Storage\FilterStorageInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

final class SessionFilterStorage implements FilterStorageInterface
{
public function __construct(private readonly RequestStack $requestStack)
{
}

public function set(array $filters): void
{
$this->getSession()->set('filters', $filters);
}

public function all(): array
{
return $this->getSession()->all()['filters'] ?? [];
}

public function hasFilters(): bool
{
return [] !== $this->getSession()->get('filters', []);
}

private function getSession(): SessionInterface
{
return $this->requestStack->getSession();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Component\Grid\Tests\Integration\Filter\Storage;

use Sylius\Component\Grid\Filter\Storage\FilterStorageInterface;
use Sylius\Component\Grid\Symfony\Session\Filter\Storage\SessionFilterStorage;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

final class SessionFilterStorageTest extends KernelTestCase
GSadee marked this conversation as resolved.
Show resolved Hide resolved
{
public function testTheContainerContainsTheServiceAndAliases(): void
{
$this->bootKernel();

$container = $this->getContainer();

$this->assertTrue($container->has('sylius_grid.symfony.session.filter_storage'));
$this->assertInstanceOf(SessionFilterStorage::class, $container->get('sylius_grid.symfony.session.filter_storage'));

$this->assertTrue($container->has('sylius_grid.filter_storage'));
$this->assertInstanceOf(SessionFilterStorage::class, $container->get('sylius_grid.symfony.session.filter_storage'));

$this->assertTrue($container->has(FilterStorageInterface::class));
$this->assertInstanceOf(SessionFilterStorage::class, $container->get(FilterStorageInterface::class));
GSadee marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Component\Grid\Tests\Unit\Symfony\Session\Filter\Storage;

use PHPUnit\Framework\TestCase;
use Sylius\Component\Grid\Filter\Storage\FilterStorageInterface;
use Sylius\Component\Grid\Symfony\Session\Filter\Storage\SessionFilterStorage;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;

final class SessionFilterStorageTest extends TestCase
{
public function testItImplementsFilterStorageInterface(): void
{
$this->assertInstanceOf(FilterStorageInterface::class, new SessionFilterStorage(new RequestStack()));
}

public function testItSetsFiltersInASession(): void
{
$filters = [
'filter' => 'value',
];

$requestStack = new RequestStack();
$request = new Request();
$session = new Session(new MockArraySessionStorage());

$request->setSession($session);
$requestStack->push($request);

$filterStorage = new SessionFilterStorage($requestStack);
$filterStorage->set($filters);

$this->assertEquals($filters, $session->get('filters'));
}

public function testItReturnsAllFiltersFromASession(): void
{
$filters = [
'filter' => 'value',
];

$requestStack = new RequestStack();
$request = new Request();
$session = new Session(new MockArraySessionStorage());

$session->set('filters', $filters);
$request->setSession($session);
$requestStack->push($request);

$filterStorage = new SessionFilterStorage($requestStack);

$this->assertEquals($filters, $filterStorage->all());
}

public function testItReturnsTrueIfFiltersAreSet(): void
{
$filters = [
'filter' => 'value',
];

$requestStack = new RequestStack();
$request = new Request();
$session = new Session(new MockArraySessionStorage());

$session->set('filters', $filters);
$request->setSession($session);
$requestStack->push($request);

$filterStorage = new SessionFilterStorage($requestStack);

$this->assertTrue($filterStorage->hasFilters());
}

public function testItReturnsFalseIfFiltersAreSet(): void
{
$filters = [];

$requestStack = new RequestStack();
$request = new Request();
$session = new Session(new MockArraySessionStorage());

$session->set('filters', $filters);
$request->setSession($session);
$requestStack->push($request);

$filterStorage = new SessionFilterStorage($requestStack);

$this->assertFalse($filterStorage->hasFilters());
}
}
Loading