Skip to content
Open
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
95 changes: 95 additions & 0 deletions spec/Context/FakeTheme/FakeThemeContextSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\Bundle\ThemeBundle\Context\FakeTheme;

use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ThemeBundle\Context\FakeTheme\FakeThemeNameProviderInterface;
use Sylius\Bundle\ThemeBundle\Context\ThemeContextInterface;
use Sylius\Bundle\ThemeBundle\Model\Theme;
use Sylius\Bundle\ThemeBundle\Repository\ThemeRepositoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

final class FakeThemeContextSpec extends ObjectBehavior
{
public function let(
ThemeContextInterface $decoratedContext,
FakeThemeNameProviderInterface $fakeThemeNameProvider,
RequestStack $requestStack,
ThemeRepositoryInterface $themeRepository
): void {
$this->beConstructedWith($decoratedContext, $fakeThemeNameProvider, $requestStack, $themeRepository);
}

public function it_implements_theme_context(): void
{
$this->shouldImplement(ThemeContextInterface::class);
}

public function it_falls_back_to_decorated_context_if_request_is_null(ThemeContextInterface $decoratedContext): void
{
$this->getTheme();

$decoratedContext->getTheme()->shouldHaveBeenCalled();
}

public function it_falls_back_to_decorated_if_theme_name_is_null(
ThemeContextInterface $decoratedContext,
RequestStack $requestStack,
): void {
$request = new Request();
if (method_exists(RequestStack::class, 'getMainRequest')) {
$requestStack->getMainRequest()->willReturn($request);
} else {
$requestStack->getMasterRequest()->willReturn($request);
}

$this->getTheme();

$decoratedContext->getTheme()->shouldHaveBeenCalled();
}

public function it_returns_null_if_theme_does_not_exist(
RequestStack $requestStack,
FakeThemeNameProviderInterface $fakeThemeNameProvider
): void {
$request = new Request();
if (method_exists(RequestStack::class, 'getMainRequest')) {
$requestStack->getMainRequest()->willReturn($request);
} else {
$requestStack->getMasterRequest()->willReturn($request);
}
$fakeThemeNameProvider->getName($request)->willReturn('fake/theme');

$this->getTheme()->shouldBeNull();
}

public function it_returns_theme_if_it_exists(
RequestStack $requestStack,
FakeThemeNameProviderInterface $fakeThemeNameProvider,
ThemeRepositoryInterface $themeRepository,
): void {
$theme = new Theme('fake/theme', 'fake/path');
$request = new Request();
if (method_exists(RequestStack::class, 'getMainRequest')) {
$requestStack->getMainRequest()->willReturn($request);
} else {
$requestStack->getMasterRequest()->willReturn($request);
}
$fakeThemeNameProvider->getName($request)->willReturn('fake/theme');
$themeRepository->findOneByName('fake/theme')->willReturn($theme);

$this->getTheme()->shouldReturn($theme);
}
}
43 changes: 43 additions & 0 deletions spec/Context/FakeTheme/FakeThemeNameProviderSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\Bundle\ThemeBundle\Context\FakeTheme;

use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ThemeBundle\Context\FakeTheme\FakeThemeNameProviderInterface;
use Symfony\Component\HttpFoundation\Request;

final class FakeThemeNameProviderSpec extends ObjectBehavior
{
public function it_implements_fake_theme_name_provider_interface(): void
{
$this->shouldImplement(FakeThemeNameProviderInterface::class);
}

public function it_returns_null_if_request_has_no_query_nor_cookie(): void
{
$this->getName(new Request())->shouldReturn(null);
}

public function it_returns_theme_name_from_query(): void
{
$request = new Request([FakeThemeNameProviderInterface::PARAMETER_NAME => 'test/theme']);
$this->getName($request)->shouldReturn('test/theme');
}

public function it_returns_theme_name_from_cookie(): void
{
$request = new Request(cookies: [FakeThemeNameProviderInterface::PARAMETER_NAME => 'test/cookie-theme']);
$this->getName($request)->shouldReturn('test/cookie-theme');
}
}
90 changes: 90 additions & 0 deletions spec/Context/FakeTheme/FakeThemePersisterSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\Bundle\ThemeBundle\Context\FakeTheme;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Bundle\ThemeBundle\Context\FakeTheme\FakeThemeNameProviderInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

final class FakeThemePersisterSpec extends ObjectBehavior
{
public function let(FakeThemeNameProviderInterface $fakeThemeNameProvider): void
{
$this->beConstructedWith($fakeThemeNameProvider);
}

function it_applies_only_to_main_requests(
HttpKernelInterface $kernel,
Request $request,
Response $response
): void {
$this->onKernelResponse(new ResponseEvent(
$kernel->getWrappedObject(),
$request->getWrappedObject(),
HttpKernelInterface::SUB_REQUEST,
$response->getWrappedObject()
));
}

function it_applies_only_for_request_with_fake_theme_name(
FakeThemeNameProviderInterface $fakeThemeNameProvider,
HttpKernelInterface $kernel,
Request $request,
Response $response
): void {
$fakeThemeNameProvider->getName($request)->willReturn(null);

$mainRequest = defined('Symfony\\Component\\HttpKernel\\HttpKernelInterface::MAIN_REQUEST')
? HttpKernelInterface::MAIN_REQUEST
: HttpKernelInterface::MASTER_REQUEST;
$this->onKernelResponse(new ResponseEvent(
$kernel->getWrappedObject(),
$request->getWrappedObject(),
$mainRequest,
$response->getWrappedObject()
));
}

function it_persists_fake_channel_codes_in_a_cookie(
FakeThemeNameProviderInterface $fakeThemeNameProvider,
HttpKernelInterface $kernel,
Request $request,
Response $response,
ResponseHeaderBag $responseHeaderBag
): void {
$fakeThemeNameProvider->getName($request)->willReturn('fake_theme_name');

$response->headers = $responseHeaderBag;
$responseHeaderBag
->setCookie(Argument::that(static fn (Cookie $cookie): bool => $cookie->getName() === '_theme_name' && $cookie->getValue() === 'fake_theme_name'))
->shouldBeCalled()
;

$mainRequest = defined('Symfony\\Component\\HttpKernel\\HttpKernelInterface::MAIN_REQUEST')
? HttpKernelInterface::MAIN_REQUEST
: HttpKernelInterface::MASTER_REQUEST;
$this->onKernelResponse(new ResponseEvent(
$kernel->getWrappedObject(),
$request->getWrappedObject(),
$mainRequest,
$response->getWrappedObject()
));
}
}
55 changes: 55 additions & 0 deletions src/Context/FakeTheme/FakeThemeContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* 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\ThemeBundle\Context\FakeTheme;

use Sylius\Bundle\ThemeBundle\Context\ThemeContextInterface;
use Sylius\Bundle\ThemeBundle\Model\ThemeInterface;
use Sylius\Bundle\ThemeBundle\Repository\ThemeRepositoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

final class FakeThemeContext implements ThemeContextInterface
{
public function __construct(
private ThemeContextInterface $decoratedContext,
private FakeThemeNameProviderInterface $fakeThemeNameProvider,
private RequestStack $requestStack,
private ThemeRepositoryInterface $themeRepository,
) {
}

public function getTheme(): ?ThemeInterface
{
$request = $this->getRequest();
if (null === $request) {
return $this->decoratedContext->getTheme();
}
$themeName = $this->fakeThemeNameProvider->getName($request);
if (null === $themeName) {
return $this->decoratedContext->getTheme();
}

return $this->themeRepository->findOneByName($themeName);
}

private function getRequest(): ?Request
{
if (\method_exists($this->requestStack, 'getMainRequest')) {
return $this->requestStack->getMainRequest();
}

/** @psalm-suppress DeprecatedMethod */
return $this->requestStack->getMasterRequest();
}
}
34 changes: 34 additions & 0 deletions src/Context/FakeTheme/FakeThemeNameProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* 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\ThemeBundle\Context\FakeTheme;

use Symfony\Component\HttpFoundation\Request;

final class FakeThemeNameProvider implements FakeThemeNameProviderInterface
{
public function getName(Request $request): ?string
{
$themeName = $request->query->get(self::PARAMETER_NAME);
if (\is_string($themeName) && '' !== $themeName) {
return $themeName;
}

$themeName = $request->cookies->get(self::PARAMETER_NAME);
if (\is_string($themeName) && '' !== $themeName) {
return $themeName;
}

return null;
}
}
23 changes: 23 additions & 0 deletions src/Context/FakeTheme/FakeThemeNameProviderInterface.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) Paweł Jędrzejewski
*
* 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\ThemeBundle\Context\FakeTheme;

use Symfony\Component\HttpFoundation\Request;

interface FakeThemeNameProviderInterface
{
public const PARAMETER_NAME = '_theme_name';

public function getName(Request $request): ?string;
}
40 changes: 40 additions & 0 deletions src/Context/FakeTheme/FakeThemePersister.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* 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\ThemeBundle\Context\FakeTheme;

use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

final class FakeThemePersister
{
public function __construct(private FakeThemeNameProviderInterface $fakeThemeNameProvider)
{
}

public function onKernelResponse(ResponseEvent $event): void
{
if (HttpKernelInterface::SUB_REQUEST === $event->getRequestType()) {
return;
}

$fakeThemeName = $this->fakeThemeNameProvider->getName($event->getRequest());
if (null === $fakeThemeName) {
return;
}

$response = $event->getResponse();
$response->headers->setCookie(new Cookie(FakeThemeNameProviderInterface::PARAMETER_NAME, $fakeThemeName));
}
}
4 changes: 4 additions & 0 deletions src/DependencyInjection/SyliusThemeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public function load(array $configs, ContainerBuilder $container): void
}
}

if ($container->getParameter('kernel.debug')) {
$loader->load('services/integrations/debug.xml');
}

$this->resolveConfigurationSources($container, $config);

$container->setAlias(ThemeContextInterface::class, $config['context']);
Expand Down
Loading