-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
379 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?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 Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use Sylius\TwigExtra\Storage\FiltersStorageInterface; | ||
use Sylius\TwigExtra\Symfony\Session\Storage\SessionFiltersStorage; | ||
|
||
return function (ContainerConfigurator $configurator): void { | ||
$services = $configurator->services(); | ||
|
||
$services->set('sylius_twig_extra.symfony.session.storage.filters', SessionFiltersStorage::class) | ||
->args([ | ||
service('request_stack'), | ||
]) | ||
; | ||
$services->alias(FiltersStorageInterface::class, 'sylius_twig_extra.symfony.session.storage.filters'); | ||
}; |
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,29 @@ | ||
<?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\TwigExtra\Storage; | ||
|
||
interface FiltersStorageInterface | ||
{ | ||
/** | ||
* @param mixed[] $filters | ||
*/ | ||
public function set(array $filters): void; | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
public function all(): array; | ||
|
||
public function hasFilters(): bool; | ||
} |
45 changes: 45 additions & 0 deletions
45
src/TwigExtra/src/Symfony/Session/Storage/SessionFiltersStorage.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,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\TwigExtra\Symfony\Session\Storage; | ||
|
||
use Sylius\TwigExtra\Storage\FiltersStorageInterface; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\HttpFoundation\Session\SessionInterface; | ||
|
||
final class SessionFiltersStorage implements FiltersStorageInterface | ||
{ | ||
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()->get('filters', []); | ||
} | ||
|
||
public function hasFilters(): bool | ||
{ | ||
return [] !== $this->getSession()->get('filters', []); | ||
} | ||
|
||
private function getSession(): SessionInterface | ||
{ | ||
return $this->requestStack->getSession(); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/TwigExtra/src/Twig/Extension/RedirectPathExtension.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,69 @@ | ||
<?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\TwigExtra\Twig\Extension; | ||
|
||
use Sylius\TwigExtra\Storage\FiltersStorageInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
final class RedirectPathExtension extends AbstractExtension | ||
{ | ||
private const NUMBER_OF_ROUTE_PROPERTIES = 3; | ||
|
||
public function __construct( | ||
private readonly FiltersStorageInterface $filterStorage, | ||
private readonly RouterInterface $router, | ||
) { | ||
} | ||
|
||
public function getFunctions(): array | ||
{ | ||
return [ | ||
new TwigFunction('sylius_generate_redirect_path', [$this, 'generateRedirectPath']), | ||
]; | ||
} | ||
|
||
public function generateRedirectPath(?string $path): ?string | ||
{ | ||
if (null === $path) { | ||
return null; | ||
} | ||
|
||
$request = Request::create($path); | ||
|
||
try { | ||
$routeInfo = $this->router->match($request->getPathInfo()); | ||
} catch (\Throwable) { | ||
return $path; | ||
} | ||
|
||
if ([] !== $request->query->all() || $this->hasAdditionalParameters($routeInfo)) { | ||
return $path; | ||
} | ||
|
||
$route = $routeInfo['_route']; | ||
|
||
return $this->router->generate($route, $this->filterStorage->all()); | ||
} | ||
|
||
/** | ||
* @param mixed[] $routeInfo | ||
*/ | ||
private function hasAdditionalParameters(array $routeInfo): bool | ||
{ | ||
return count($routeInfo) > self::NUMBER_OF_ROUTE_PROPERTIES; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/TwigExtra/tests/Functional/Symfony/HttpFoundation/Storage/FilterStorageTest.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,30 @@ | ||
<?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 Tests\Sylius\TwigExtra\Functional\Symfony\HttpFoundation\Storage; | ||
|
||
use Sylius\TwigExtra\Symfony\HttpFoundation\Storage\SessionFiltersStorage; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
|
||
final class FilterStorageTest extends KernelTestCase | ||
{ | ||
public function testTheContainerContainsTheService(): void | ||
{ | ||
$this->bootKernel(); | ||
|
||
$container = $this->getContainer(); | ||
|
||
$this->assertTrue($container->has('sylius_twig_extra.symfony.http_foundation.storage.filter')); | ||
$this->assertInstanceOf(SessionFiltersStorage::class, $container->get('sylius_twig_extra.symfony.http_foundation.storage.filter')); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/TwigExtra/tests/Functional/Twig/Extension/RedirectPathExtensionTest.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,65 @@ | ||
<?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 Tests\Sylius\TwigExtra\Functional\Twig\Extension; | ||
|
||
use Sylius\TwigExtra\Twig\Extension\RedirectPathExtension; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
final class RedirectPathExtensionTest extends KernelTestCase | ||
{ | ||
private RedirectPathExtension $redirectPathExtension; | ||
|
||
protected function setUp(): void | ||
{ | ||
$container = self::getContainer(); | ||
|
||
$session = $container->get('session.factory')->createSession(); | ||
$request = new Request(); | ||
$request->setSession($session); | ||
$container->get('request_stack')->push($request); | ||
|
||
$this->redirectPathExtension = $container->get('sylius_twig_extra.twig.extension.redirect_path'); | ||
$container->get('sylius_twig_extra.symfony.session.storage.filters')->set(['criteria' => ['enabled' => true]]); | ||
} | ||
|
||
public function testItReturnsRedirectPathWithFiltersFromStorageApplied(): void | ||
{ | ||
// Note, it requires an existing route path. | ||
$redirectPath = $this->redirectPathExtension->generateRedirectPath('/admin/books'); | ||
|
||
$this->assertSame('/admin/books?criteria%5Benabled%5D=1', $redirectPath); | ||
} | ||
|
||
public function testItReturnsGivenPathIfRouteHasSomeMoreConfiguration(): void | ||
{ | ||
$redirectPath = $this->redirectPathExtension->generateRedirectPath('/admin/ajax/products/search'); | ||
|
||
$this->assertSame('/admin/ajax/products/search', $redirectPath); | ||
} | ||
|
||
public function testItReturnsGivenPathIfRouteIsNotMatched(): void | ||
{ | ||
$redirectPath = $this->redirectPathExtension->generateRedirectPath('/admin/invalid-path'); | ||
|
||
$this->assertSame('/admin/invalid-path', $redirectPath); | ||
} | ||
|
||
public function testItReturnsNullIfThePathIsNullAsWell(): void | ||
{ | ||
$redirectPath = $this->redirectPathExtension->generateRedirectPath(null); | ||
|
||
$this->assertNull($redirectPath); | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
src/TwigExtra/tests/Unit/Symfony/Session/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 Tests\Sylius\TwigExtra\Unit\Symfony\Session; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Sylius\TwigExtra\Storage\FiltersStorageInterface; | ||
use Sylius\TwigExtra\Symfony\Session\Storage\SessionFiltersStorage; | ||
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(FiltersStorageInterface::class, new SessionFiltersStorage(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 SessionFiltersStorage($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 SessionFiltersStorage($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 SessionFiltersStorage($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 SessionFiltersStorage($requestStack); | ||
|
||
$this->assertFalse($filterStorage->hasFilters()); | ||
} | ||
} |