-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #342 from loic425/feat/filter-storage
Add storage for filters
- Loading branch information
Showing
5 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
src/Bundle/Tests/Integration/Storage/SessionFilterStorageTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
104
src/Bundle/Tests/Unit/Storage/SessionFilterStorageTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |