Skip to content

Commit

Permalink
Merge pull request #342 from loic425/feat/filter-storage
Browse files Browse the repository at this point in the history
Add storage for filters
  • Loading branch information
GSadee authored Oct 1, 2024
2 parents 4a23aee + 543594f commit ac18698
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 0 deletions.
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\Bundle\GridBundle\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/Bundle/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\Bundle\GridBundle\Storage;

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

public function all(): array;

public function hasFilters(): bool;
}
44 changes: 44 additions & 0 deletions src/Bundle/Storage/SessionFilterStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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\Bundle\GridBundle\Storage;

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();
}
}
37 changes: 37 additions & 0 deletions src/Bundle/Tests/Integration/Storage/SessionFilterStorageTest.php
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 Integration\Storage;

use Sylius\Bundle\GridBundle\Storage\FilterStorageInterface;
use Sylius\Bundle\GridBundle\Storage\SessionFilterStorage;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

final class SessionFilterStorageTest extends KernelTestCase
{
public function testTheContainerContainsTheServiceAndAliases(): void
{
$this->bootKernel();

$container = $this->getContainer();

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

$this->assertTrue($container->has('sylius.grid.filter_storage'));
$this->assertInstanceOf(SessionFilterStorage::class, $container->get('sylius.grid.filter_storage'));

$this->assertTrue($container->has(FilterStorageInterface::class));
$this->assertInstanceOf(SessionFilterStorage::class, $container->get(FilterStorageInterface::class));
}
}
104 changes: 104 additions & 0 deletions src/Bundle/Tests/Unit/Storage/SessionFilterStorageTest.php
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\Bundle\GridBundle\Tests\Unit\Storage;

use PHPUnit\Framework\TestCase;
use Sylius\Bundle\GridBundle\Storage\FilterStorageInterface;
use Sylius\Bundle\GridBundle\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());
}
}

0 comments on commit ac18698

Please sign in to comment.