mirror of
https://github.com/Sylius/Sylius.git
synced 2026-07-14 17:10:53 +00:00
[Channel] Fake hostname replaced with fake channel
This commit is contained in:
parent
ec16dd55a6
commit
dfc2a8310c
20 changed files with 351 additions and 297 deletions
|
|
@ -33,4 +33,4 @@ swiftmailer:
|
|||
disable_delivery: true
|
||||
|
||||
sylius_channel:
|
||||
fake_hostname_support: true
|
||||
fake_channel_support: true
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ sylius_rbac:
|
|||
authorization_checker: sylius.authorization_checker.toggleable
|
||||
|
||||
sylius_channel:
|
||||
fake_hostname_support: true
|
||||
fake_channel_support: true
|
||||
|
||||
doctrine_cache:
|
||||
providers:
|
||||
|
|
|
|||
|
|
@ -9,20 +9,20 @@
|
|||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Sylius\Bundle\ChannelBundle\Development;
|
||||
namespace Sylius\Bundle\ChannelBundle\Context\FakeChannel;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* @author Kamil Kokot <kamil.kokot@lakion.com>
|
||||
*/
|
||||
final class FakeHostnameProvider implements FakeHostnameProviderInterface
|
||||
final class FakeChannelCodeProvider implements FakeChannelCodeProviderInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getHostname(Request $request)
|
||||
public function getCode(Request $request)
|
||||
{
|
||||
return $request->query->get('_hostname') ?: $request->cookies->get('_hostname');
|
||||
return $request->query->get('_channel_code') ?: $request->cookies->get('_channel_code');
|
||||
}
|
||||
}
|
||||
|
|
@ -9,19 +9,19 @@
|
|||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Sylius\Bundle\ChannelBundle\Development;
|
||||
namespace Sylius\Bundle\ChannelBundle\Context\FakeChannel;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* @author Kamil Kokot <kamil.kokot@lakion.com>
|
||||
*/
|
||||
interface FakeHostnameProviderInterface
|
||||
interface FakeChannelCodeProviderInterface
|
||||
{
|
||||
/**
|
||||
* @param Request $request
|
||||
*
|
||||
* @return string
|
||||
* @return string|null
|
||||
*/
|
||||
public function getHostname(Request $request);
|
||||
public function getCode(Request $request);
|
||||
}
|
||||
|
|
@ -9,7 +9,7 @@
|
|||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Sylius\Bundle\ChannelBundle\Development;
|
||||
namespace Sylius\Bundle\ChannelBundle\Context\FakeChannel;
|
||||
|
||||
use Sylius\Component\Channel\Context\ChannelContextInterface;
|
||||
use Sylius\Component\Channel\Model\ChannelInterface;
|
||||
|
|
@ -21,7 +21,7 @@ use Symfony\Component\HttpKernel\DataCollector\DataCollector;
|
|||
/**
|
||||
* @author Kamil Kokot <kamil.kokot@lakion.com>
|
||||
*/
|
||||
final class FakeHostnameCollector extends DataCollector
|
||||
final class FakeChannelCollector extends DataCollector
|
||||
{
|
||||
/**
|
||||
* @param ChannelRepositoryInterface $channelRepository
|
||||
|
|
@ -61,6 +61,6 @@ final class FakeHostnameCollector extends DataCollector
|
|||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'sylius.channel.fake_hostname.collector';
|
||||
return 'sylius.context.channel.fake_channel.collector';
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,92 @@
|
|||
<?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.
|
||||
*/
|
||||
|
||||
namespace Sylius\Bundle\ChannelBundle\Context\FakeChannel;
|
||||
|
||||
use Sylius\Component\Channel\Context\ChannelContextInterface;
|
||||
use Sylius\Component\Channel\Context\ChannelNotFoundException;
|
||||
use Sylius\Component\Channel\Model\ChannelInterface;
|
||||
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
|
||||
/**
|
||||
* @author Kamil Kokot <kamil.kokot@lakion.com>
|
||||
*/
|
||||
final class FakeChannelContext implements ChannelContextInterface
|
||||
{
|
||||
/**
|
||||
* @var FakeChannelCodeProviderInterface
|
||||
*/
|
||||
private $fakeChannelCodeProvider;
|
||||
|
||||
/**
|
||||
* @var ChannelRepositoryInterface
|
||||
*/
|
||||
private $channelRepository;
|
||||
|
||||
/**
|
||||
* @var RequestStack
|
||||
*/
|
||||
private $requestStack;
|
||||
|
||||
/**
|
||||
* @param FakeChannelCodeProviderInterface $fakeChannelCodeProvider
|
||||
* @param ChannelRepositoryInterface $channelRepository
|
||||
* @param RequestStack $requestStack
|
||||
*/
|
||||
public function __construct(
|
||||
FakeChannelCodeProviderInterface $fakeChannelCodeProvider,
|
||||
ChannelRepositoryInterface $channelRepository,
|
||||
RequestStack $requestStack
|
||||
) {
|
||||
$this->fakeChannelCodeProvider = $fakeChannelCodeProvider;
|
||||
$this->channelRepository = $channelRepository;
|
||||
$this->requestStack = $requestStack;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getChannel()
|
||||
{
|
||||
$fakeChannelCode = $this->fakeChannelCodeProvider->getCode($this->getMasterRequest());
|
||||
|
||||
$channel = $this->channelRepository->findOneByCode($fakeChannelCode);
|
||||
|
||||
$this->assertChannelWasFound($channel);
|
||||
|
||||
return $channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Request
|
||||
*/
|
||||
private function getMasterRequest()
|
||||
{
|
||||
$masterRequest = $this->requestStack->getMasterRequest();
|
||||
if (null === $masterRequest) {
|
||||
throw new ChannelNotFoundException();
|
||||
}
|
||||
|
||||
return $masterRequest;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ChannelInterface|null $channel
|
||||
*/
|
||||
private function assertChannelWasFound(ChannelInterface $channel = null)
|
||||
{
|
||||
if (null === $channel) {
|
||||
throw new ChannelNotFoundException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -9,7 +9,7 @@
|
|||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Sylius\Bundle\ChannelBundle\Development;
|
||||
namespace Sylius\Bundle\ChannelBundle\Context\FakeChannel;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Cookie;
|
||||
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
|
||||
|
|
@ -18,19 +18,19 @@ use Symfony\Component\HttpKernel\HttpKernelInterface;
|
|||
/**
|
||||
* @author Kamil Kokot <kamil.kokot@lakion.com>
|
||||
*/
|
||||
final class FakeHostnamePersister
|
||||
final class FakeChannelPersister
|
||||
{
|
||||
/**
|
||||
* @var FakeHostnameProviderInterface
|
||||
* @var FakeChannelCodeProviderInterface
|
||||
*/
|
||||
private $fakeHostnameProvider;
|
||||
private $fakeChannelCodeProvider;
|
||||
|
||||
/**
|
||||
* @param FakeHostnameProviderInterface $fakeHostnameProvider
|
||||
* @param FakeChannelCodeProviderInterface $fakeChannelCodeProvider
|
||||
*/
|
||||
public function __construct(FakeHostnameProviderInterface $fakeHostnameProvider)
|
||||
public function __construct(FakeChannelCodeProviderInterface $fakeChannelCodeProvider)
|
||||
{
|
||||
$this->fakeHostnameProvider = $fakeHostnameProvider;
|
||||
$this->fakeChannelCodeProvider = $fakeChannelCodeProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -42,13 +42,13 @@ final class FakeHostnamePersister
|
|||
return;
|
||||
}
|
||||
|
||||
$fakeHostname = $this->fakeHostnameProvider->getHostname($filterResponseEvent->getRequest());
|
||||
$fakeChannelCode = $this->fakeChannelCodeProvider->getCode($filterResponseEvent->getRequest());
|
||||
|
||||
if (null === $fakeHostname) {
|
||||
if (null === $fakeChannelCode) {
|
||||
return;
|
||||
}
|
||||
|
||||
$response = $filterResponseEvent->getResponse();
|
||||
$response->headers->setCookie(new Cookie('_hostname', $fakeHostname));
|
||||
$response->headers->setCookie(new Cookie('_channel_code', $fakeChannelCode));
|
||||
}
|
||||
}
|
||||
|
|
@ -43,7 +43,7 @@ class Configuration implements ConfigurationInterface
|
|||
$rootNode
|
||||
->children()
|
||||
->scalarNode('driver')->defaultValue(SyliusResourceBundle::DRIVER_DOCTRINE_ORM)->end()
|
||||
->booleanNode('fake_hostname_support')->defaultFalse()->cannotBeEmpty()->end()
|
||||
->booleanNode('fake_channel_support')->defaultFalse()->cannotBeEmpty()->end()
|
||||
->end()
|
||||
;
|
||||
|
||||
|
|
|
|||
|
|
@ -39,8 +39,8 @@ class SyliusChannelExtension extends AbstractResourceExtension
|
|||
'services.xml',
|
||||
];
|
||||
|
||||
if ($config['fake_hostname_support']) {
|
||||
$configFiles[] = 'fake_hostname.xml';
|
||||
if ($config['fake_channel_support']) {
|
||||
$configFiles[] = 'fake_channel.xml';
|
||||
}
|
||||
|
||||
foreach ($configFiles as $configFile) {
|
||||
|
|
|
|||
|
|
@ -1,58 +0,0 @@
|
|||
<?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.
|
||||
*/
|
||||
|
||||
namespace Sylius\Bundle\ChannelBundle\Development;
|
||||
|
||||
use Sylius\Component\Channel\Context\RequestBased\RequestResolverInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* @author Kamil Kokot <kamil.kokot@lakion.com>
|
||||
*/
|
||||
final class FakeRequestResolver implements RequestResolverInterface
|
||||
{
|
||||
/**
|
||||
* @var RequestResolverInterface
|
||||
*/
|
||||
private $decoratedRequestResolver;
|
||||
|
||||
/**
|
||||
* @var FakeHostnameProviderInterface
|
||||
*/
|
||||
private $fakeHostnameProvider;
|
||||
|
||||
/**
|
||||
* @param RequestResolverInterface $decoratedRequestResolver
|
||||
* @param FakeHostnameProviderInterface $fakeHostnameProvider
|
||||
*/
|
||||
public function __construct(
|
||||
RequestResolverInterface $decoratedRequestResolver,
|
||||
FakeHostnameProviderInterface $fakeHostnameProvider
|
||||
) {
|
||||
$this->decoratedRequestResolver = $decoratedRequestResolver;
|
||||
$this->fakeHostnameProvider = $fakeHostnameProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function findChannel(Request $request)
|
||||
{
|
||||
$fakeHostname = $this->fakeHostnameProvider->getHostname($request);
|
||||
|
||||
if (null !== $fakeHostname) {
|
||||
$request = clone $request;
|
||||
$request->headers->set('HOST', $fakeHostname);
|
||||
}
|
||||
|
||||
return $this->decoratedRequestResolver->findChannel($request);
|
||||
}
|
||||
}
|
||||
|
|
@ -26,4 +26,12 @@ class ChannelRepository extends EntityRepository implements ChannelRepositoryInt
|
|||
{
|
||||
return $this->findOneBy(['hostname' => $hostname]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function findOneByCode($code)
|
||||
{
|
||||
return $this->findOneBy(['code' => $code]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,43 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<!--
|
||||
|
||||
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.
|
||||
|
||||
-->
|
||||
|
||||
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
|
||||
<parameters>
|
||||
<parameter key="sylius.context.channel.fake_channel.persister.class">Sylius\Bundle\ChannelBundle\Context\FakeChannel\FakeChannelPersister</parameter>
|
||||
<parameter key="sylius.context.channel.fake_channel.code_provider.class">Sylius\Bundle\ChannelBundle\Context\FakeChannel\FakeChannelCodeProvider</parameter>
|
||||
<parameter key="sylius.context.channel.fake_channel.context.class">Sylius\Bundle\ChannelBundle\Context\FakeChannel\FakeChannelContext</parameter>
|
||||
<parameter key="sylius.context.channel.fake_channel.collector.class">Sylius\Bundle\ChannelBundle\Context\FakeChannel\FakeChannelCollector</parameter>
|
||||
</parameters>
|
||||
|
||||
<services>
|
||||
<service id="sylius.context.channel.fake_channel.persister" class="%sylius.context.channel.fake_channel.persister.class%">
|
||||
<argument type="service" id="sylius.context.channel.fake_channel.code_provider" />
|
||||
<tag name="kernel.event_listener" event="kernel.response" method="onKernelResponse" priority="-8192" />
|
||||
</service>
|
||||
|
||||
<service id="sylius.context.channel.fake_channel.code_provider" class="%sylius.context.channel.fake_channel.code_provider.class%" public="false" />
|
||||
|
||||
<service id="sylius.context.channel.fake_channel.context" class="%sylius.context.channel.fake_channel.context.class%" public="false">
|
||||
<argument type="service" id="sylius.context.channel.fake_channel.code_provider" />
|
||||
<argument type="service" id="sylius.repository.channel" />
|
||||
<argument type="service" id="request_stack" />
|
||||
<tag name="sylius.context.channel" priority="128" />
|
||||
</service>
|
||||
|
||||
<service id="sylius.context.channel.fake_channel.collector" class="%sylius.context.channel.fake_channel.collector.class%" public="false">
|
||||
<argument type="service" id="sylius.repository.channel" />
|
||||
<argument type="service" id="sylius.context.channel" />
|
||||
<tag name="data_collector" template="SyliusChannelBundle:FakeChannelCollector:layout.html.twig" id="sylius.context.channel.fake_channel.collector" />
|
||||
</service>
|
||||
</services>
|
||||
</container>
|
||||
|
|
@ -1,41 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<!--
|
||||
|
||||
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.
|
||||
|
||||
-->
|
||||
|
||||
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
|
||||
<parameters>
|
||||
<parameter key="sylius.channel.fake_hostname.persister.class">Sylius\Bundle\ChannelBundle\Development\FakeHostnamePersister</parameter>
|
||||
<parameter key="sylius.channel.fake_hostname.provider.class">Sylius\Bundle\ChannelBundle\Development\FakeHostnameProvider</parameter>
|
||||
<parameter key="sylius.channel.fake_hostname.request_resolver.class">Sylius\Bundle\ChannelBundle\Development\FakeRequestResolver</parameter>
|
||||
<parameter key="sylius.channel.fake_hostname.collector.class">Sylius\Bundle\ChannelBundle\Development\FakeHostnameCollector</parameter>
|
||||
</parameters>
|
||||
|
||||
<services>
|
||||
<service id="sylius.channel.fake_hostname.persister" class="%sylius.channel.fake_hostname.persister.class%">
|
||||
<argument type="service" id="sylius.channel.fake_hostname.provider" />
|
||||
<tag name="kernel.event_listener" event="kernel.response" method="onKernelResponse" priority="-8192" />
|
||||
</service>
|
||||
|
||||
<service id="sylius.channel.fake_hostname.provider" class="%sylius.channel.fake_hostname.provider.class%" public="false" />
|
||||
|
||||
<service id="sylius.channel.fake_hostname.request_resolver" class="%sylius.channel.fake_hostname.request_resolver.class%" decorates="sylius.context.channel.request_based.resolver" public="false">
|
||||
<argument type="service" id="sylius.channel.fake_hostname.request_resolver.inner" />
|
||||
<argument type="service" id="sylius.channel.fake_hostname.provider" />
|
||||
</service>
|
||||
|
||||
<service id="sylius.channel.fake_hostname.collector" class="%sylius.channel.fake_hostname.collector.class%" public="false">
|
||||
<argument type="service" id="sylius.repository.channel" />
|
||||
<argument type="service" id="sylius.context.channel" />
|
||||
<tag name="data_collector" template="SyliusChannelBundle:FakeHostnameCollector:layout.html.twig" id="sylius.channel.fake_hostname.collector" />
|
||||
</service>
|
||||
</services>
|
||||
</container>
|
||||
|
|
@ -4,7 +4,7 @@
|
|||
{% set icon %}
|
||||
<a href="#">
|
||||
{# fake image span #}<span style="width: 0; height: 28px; vertical-align: middle;"></span>
|
||||
<span class="sf-toolbar-status">{{ collector.channels|length }}</span> channels,
|
||||
<span class="sf-toolbar-status">{{ collector.channels|length }}</span> channels,
|
||||
<b>{{ collector.currentChannel.name }}</b>
|
||||
</a>
|
||||
{% endset %}
|
||||
|
|
@ -21,7 +21,7 @@
|
|||
<tr>
|
||||
<td><b>{{ channel.name }}</b></td>
|
||||
<td>{{ channel.hostname }}</td>
|
||||
<td><a href="?_hostname={{ channel.hostname }}">Change</a></td>
|
||||
<td><a href="?_channel_code={{ channel.code }}">Change</a></td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
<?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.
|
||||
*/
|
||||
|
||||
namespace spec\Sylius\Bundle\ChannelBundle\Context\FakeChannel;
|
||||
|
||||
use PhpSpec\ObjectBehavior;
|
||||
use Sylius\Bundle\ChannelBundle\Context\FakeChannel\FakeChannelCodeProvider;
|
||||
use Sylius\Bundle\ChannelBundle\Context\FakeChannel\FakeChannelCodeProviderInterface;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* @mixin FakeChannelCodeProvider
|
||||
*
|
||||
* @author Kamil Kokot <kamil.kokot@lakion.com>
|
||||
*/
|
||||
class FakeChannelCodeProviderSpec extends ObjectBehavior
|
||||
{
|
||||
function it_is_initializable()
|
||||
{
|
||||
$this->shouldHaveType('Sylius\Bundle\ChannelBundle\Context\FakeChannel\FakeChannelCodeProvider');
|
||||
}
|
||||
|
||||
function it_implements_channel_code_provider_interface()
|
||||
{
|
||||
$this->shouldImplement(FakeChannelCodeProviderInterface::class);
|
||||
}
|
||||
|
||||
function it_returns_fake_channel_code_from_query_string(Request $request, ParameterBag $queryBag)
|
||||
{
|
||||
$queryBag->get('_channel_code')->willReturn('channel_code_form_get');
|
||||
$request->query = $queryBag;
|
||||
|
||||
$this->getCode($request)->shouldReturn('channel_code_form_get');
|
||||
}
|
||||
|
||||
function it_returns_fake_channel_code_from_cookie_if_there_is_none_in_query_string(
|
||||
Request $request,
|
||||
ParameterBag $queryBag,
|
||||
ParameterBag $cookiesBag
|
||||
) {
|
||||
$queryBag->get('_channel_code')->willReturn(null);
|
||||
$request->query = $queryBag;
|
||||
|
||||
$cookiesBag->get('_channel_code')->willReturn('channel_code_form_cookie');
|
||||
$request->cookies = $cookiesBag;
|
||||
|
||||
$this->getCode($request)->shouldReturn('channel_code_form_cookie');
|
||||
}
|
||||
|
||||
function it_returns_null_channel_code_if_no_fake_channel_code_was_found(
|
||||
Request $request,
|
||||
ParameterBag $queryBag,
|
||||
ParameterBag $cookiesBag
|
||||
) {
|
||||
$queryBag->get('_channel_code')->willReturn(null);
|
||||
$request->query = $queryBag;
|
||||
|
||||
$cookiesBag->get('_channel_code')->willReturn(null);
|
||||
$request->cookies = $cookiesBag;
|
||||
|
||||
$this->getCode($request)->shouldReturn(null);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,86 @@
|
|||
<?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.
|
||||
*/
|
||||
|
||||
namespace spec\Sylius\Bundle\ChannelBundle\Context\FakeChannel;
|
||||
|
||||
use PhpSpec\ObjectBehavior;
|
||||
use Sylius\Bundle\ChannelBundle\Context\FakeChannel\FakeChannelCodeProviderInterface;
|
||||
use Sylius\Bundle\ChannelBundle\Context\FakeChannel\FakeChannelContext;
|
||||
use Sylius\Component\Channel\Context\ChannelContextInterface;
|
||||
use Sylius\Component\Channel\Context\ChannelNotFoundException;
|
||||
use Sylius\Component\Channel\Model\ChannelInterface;
|
||||
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
|
||||
/**
|
||||
* @mixin FakeChannelContext
|
||||
*
|
||||
* @author Kamil Kokot <kamil.kokot@lakion.com>
|
||||
*/
|
||||
class FakeChannelContextSpec extends ObjectBehavior
|
||||
{
|
||||
function let(
|
||||
FakeChannelCodeProviderInterface $fakeChannelCodeProvider,
|
||||
ChannelRepositoryInterface $channelRepository,
|
||||
RequestStack $requestStack
|
||||
) {
|
||||
$this->beConstructedWith($fakeChannelCodeProvider, $channelRepository, $requestStack);
|
||||
}
|
||||
|
||||
function it_is_initializable()
|
||||
{
|
||||
$this->shouldHaveType('Sylius\Bundle\ChannelBundle\Context\FakeChannel\FakeChannelContext');
|
||||
}
|
||||
|
||||
function it_implements_channel_context_interface()
|
||||
{
|
||||
$this->shouldImplement(ChannelContextInterface::class);
|
||||
}
|
||||
|
||||
function it_returns_a_fake_channel_with_the_given_code(
|
||||
FakeChannelCodeProviderInterface $fakeChannelCodeProvider,
|
||||
ChannelRepositoryInterface $channelRepository,
|
||||
RequestStack $requestStack,
|
||||
Request $request,
|
||||
ChannelInterface $channel
|
||||
) {
|
||||
$requestStack->getMasterRequest()->willReturn($request);
|
||||
|
||||
$fakeChannelCodeProvider->getCode($request)->willReturn('CHANNEL_CODE');
|
||||
|
||||
$channelRepository->findOneByCode('CHANNEL_CODE')->willReturn($channel);
|
||||
|
||||
$this->getChannel()->shouldReturn($channel);
|
||||
}
|
||||
|
||||
function it_throws_a_channel_not_found_exception_if_there_is_no_master_request(RequestStack $requestStack)
|
||||
{
|
||||
$requestStack->getMasterRequest()->willReturn(null);
|
||||
|
||||
$this->shouldThrow(ChannelNotFoundException::class)->during('getChannel');
|
||||
}
|
||||
|
||||
function it_throws_a_channel_not_found_exception_if_channel_with_given_code_was_not_found(
|
||||
FakeChannelCodeProviderInterface $fakeChannelCodeProvider,
|
||||
ChannelRepositoryInterface $channelRepository,
|
||||
RequestStack $requestStack,
|
||||
Request $request
|
||||
) {
|
||||
$requestStack->getMasterRequest()->willReturn($request);
|
||||
|
||||
$fakeChannelCodeProvider->getCode($request)->willReturn('CHANNEL_CODE');
|
||||
|
||||
$channelRepository->findOneByCode('CHANNEL_CODE')->willReturn(null);
|
||||
|
||||
$this->shouldThrow(ChannelNotFoundException::class)->during('getChannel');
|
||||
}
|
||||
}
|
||||
|
|
@ -9,12 +9,12 @@
|
|||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace spec\Sylius\Bundle\ChannelBundle\Development;
|
||||
namespace spec\Sylius\Bundle\ChannelBundle\Context\FakeChannel;
|
||||
|
||||
use PhpSpec\ObjectBehavior;
|
||||
use Prophecy\Argument;
|
||||
use Sylius\Bundle\ChannelBundle\Development\FakeHostnamePersister;
|
||||
use Sylius\Bundle\ChannelBundle\Development\FakeHostnameProviderInterface;
|
||||
use Sylius\Bundle\ChannelBundle\Context\FakeChannel\FakeChannelCodeProviderInterface;
|
||||
use Sylius\Bundle\ChannelBundle\Context\FakeChannel\FakeChannelPersister;
|
||||
use Symfony\Component\HttpFoundation\Cookie;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
|
@ -23,20 +23,20 @@ use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
|
|||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
|
||||
/**
|
||||
* @mixin FakeHostnamePersister
|
||||
* @mixin FakeChannelPersister
|
||||
*
|
||||
* @author Kamil Kokot <kamil.kokot@lakion.com>
|
||||
*/
|
||||
class FakeHostnamePersisterSpec extends ObjectBehavior
|
||||
class FakeChannelPersisterSpec extends ObjectBehavior
|
||||
{
|
||||
function let(FakeHostnameProviderInterface $fakeHostnameProvider)
|
||||
function let(FakeChannelCodeProviderInterface $fakeHostnameProvider)
|
||||
{
|
||||
$this->beConstructedWith($fakeHostnameProvider);
|
||||
}
|
||||
|
||||
function it_is_initializable()
|
||||
{
|
||||
$this->shouldHaveType('Sylius\Bundle\ChannelBundle\Development\FakeHostnamePersister');
|
||||
$this->shouldHaveType('Sylius\Bundle\ChannelBundle\Context\FakeChannel\FakeChannelPersister');
|
||||
}
|
||||
|
||||
function it_applies_only_to_master_requests(FilterResponseEvent $filterResponseEvent)
|
||||
|
|
@ -49,23 +49,23 @@ class FakeHostnamePersisterSpec extends ObjectBehavior
|
|||
$this->onKernelResponse($filterResponseEvent);
|
||||
}
|
||||
|
||||
function it_applies_only_for_request_with_fake_hostname(
|
||||
FakeHostnameProviderInterface $fakeHostnameProvider,
|
||||
function it_applies_only_for_request_with_fake_channel_code(
|
||||
FakeChannelCodeProviderInterface $fakeHostnameProvider,
|
||||
FilterResponseEvent $filterResponseEvent,
|
||||
Request $request
|
||||
) {
|
||||
$filterResponseEvent->getRequestType()->willReturn(HttpKernelInterface::MASTER_REQUEST);
|
||||
$filterResponseEvent->getRequest()->willReturn($request);
|
||||
|
||||
$fakeHostnameProvider->getHostname($request)->willReturn(null);
|
||||
$fakeHostnameProvider->getCode($request)->willReturn(null);
|
||||
|
||||
$filterResponseEvent->getResponse()->shouldNotBeCalled();
|
||||
|
||||
$this->onKernelResponse($filterResponseEvent);
|
||||
}
|
||||
|
||||
function it_persists_fake_hostnames_in_a_cookie(
|
||||
FakeHostnameProviderInterface $fakeHostnameProvider,
|
||||
function it_persists_fake_channel_codes_in_a_cookie(
|
||||
FakeChannelCodeProviderInterface $fakeHostnameProvider,
|
||||
FilterResponseEvent $filterResponseEvent,
|
||||
Request $request,
|
||||
Response $response,
|
||||
|
|
@ -74,17 +74,17 @@ class FakeHostnamePersisterSpec extends ObjectBehavior
|
|||
$filterResponseEvent->getRequestType()->willReturn(HttpKernelInterface::MASTER_REQUEST);
|
||||
$filterResponseEvent->getRequest()->willReturn($request);
|
||||
|
||||
$fakeHostnameProvider->getHostname($request)->willReturn('fake.hostname');
|
||||
$fakeHostnameProvider->getCode($request)->willReturn('fake_channel_code');
|
||||
|
||||
$filterResponseEvent->getResponse()->willReturn($response);
|
||||
|
||||
$response->headers = $responseHeaderBag;
|
||||
$responseHeaderBag->setCookie(Argument::that(function (Cookie $cookie) {
|
||||
if ($cookie->getName() !== '_hostname') {
|
||||
if ($cookie->getName() !== '_channel_code') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($cookie->getValue() !== 'fake.hostname') {
|
||||
if ($cookie->getValue() !== 'fake_channel_code') {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -1,73 +0,0 @@
|
|||
<?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.
|
||||
*/
|
||||
|
||||
namespace spec\Sylius\Bundle\ChannelBundle\Development;
|
||||
|
||||
use PhpSpec\ObjectBehavior;
|
||||
use Prophecy\Argument;
|
||||
use Sylius\Bundle\ChannelBundle\Development\FakeFakeHostnameProvider;
|
||||
use Sylius\Bundle\ChannelBundle\Development\FakeHostnameProviderInterface;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* @mixin FakeFakeHostnameProvider
|
||||
*
|
||||
* @author Kamil Kokot <kamil.kokot@lakion.com>
|
||||
*/
|
||||
class FakeHostnameProviderSpec extends ObjectBehavior
|
||||
{
|
||||
function it_is_initializable()
|
||||
{
|
||||
$this->shouldHaveType('Sylius\Bundle\ChannelBundle\Development\FakeHostnameProvider');
|
||||
}
|
||||
|
||||
function it_implements_hostname_provider_interface()
|
||||
{
|
||||
$this->shouldImplement(FakeHostnameProviderInterface::class);
|
||||
}
|
||||
|
||||
function it_returns_fake_hostname_from_query_string(Request $request, ParameterBag $queryBag)
|
||||
{
|
||||
$queryBag->get('_hostname')->willReturn('fake.hostname.from.get');
|
||||
$request->query = $queryBag;
|
||||
|
||||
$this->getHostname($request)->shouldReturn('fake.hostname.from.get');
|
||||
}
|
||||
|
||||
function it_returns_fake_hostname_from_cookie_if_there_is_none_in_query_string(
|
||||
Request $request,
|
||||
ParameterBag $queryBag,
|
||||
ParameterBag $cookiesBag
|
||||
) {
|
||||
$queryBag->get('_hostname')->willReturn(null);
|
||||
$request->query = $queryBag;
|
||||
|
||||
$cookiesBag->get('_hostname')->willReturn('fake.hostname.from.cookie');
|
||||
$request->cookies = $cookiesBag;
|
||||
|
||||
$this->getHostname($request)->shouldReturn('fake.hostname.from.cookie');
|
||||
}
|
||||
|
||||
function it_returns_null_hostname_if_no_fake_hostname_was_found(
|
||||
Request $request,
|
||||
ParameterBag $queryBag,
|
||||
ParameterBag $cookiesBag
|
||||
) {
|
||||
$queryBag->get('_hostname')->willReturn(null);
|
||||
$request->query = $queryBag;
|
||||
|
||||
$cookiesBag->get('_hostname')->willReturn(null);
|
||||
$request->cookies = $cookiesBag;
|
||||
|
||||
$this->getHostname($request)->shouldReturn(null);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,82 +0,0 @@
|
|||
<?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.
|
||||
*/
|
||||
|
||||
namespace spec\Sylius\Bundle\ChannelBundle\Development;
|
||||
|
||||
use PhpSpec\ObjectBehavior;
|
||||
use Prophecy\Argument;
|
||||
use Sylius\Bundle\ChannelBundle\Development\FakeHostnameProviderInterface;
|
||||
use Sylius\Bundle\ChannelBundle\Development\FakeRequestResolver;
|
||||
use Sylius\Component\Channel\Context\RequestBased\RequestResolverInterface;
|
||||
use Sylius\Component\Channel\Model\ChannelInterface;
|
||||
use Symfony\Component\HttpFoundation\HeaderBag;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||
|
||||
/**
|
||||
* @mixin FakeRequestResolver
|
||||
*
|
||||
* @author Kamil Kokot <kamil.kokot@lakion.com>
|
||||
*/
|
||||
class FakeRequestResolverSpec extends ObjectBehavior
|
||||
{
|
||||
function let(RequestResolverInterface $decoratedRequestResolver, FakeHostnameProviderInterface $fakeHostnameProvider)
|
||||
{
|
||||
$this->beConstructedWith($decoratedRequestResolver, $fakeHostnameProvider);
|
||||
}
|
||||
|
||||
function it_is_initializable()
|
||||
{
|
||||
$this->shouldHaveType('Sylius\Bundle\ChannelBundle\Development\FakeRequestResolver');
|
||||
}
|
||||
|
||||
function it_implements_request_resolver_interface()
|
||||
{
|
||||
$this->shouldImplement(RequestResolverInterface::class);
|
||||
}
|
||||
|
||||
function it_proxies_decorated_request_resolver_if_there_is_no_fake_hostname_found(
|
||||
RequestResolverInterface $decoratedRequestResolver,
|
||||
FakeHostnameProviderInterface $fakeHostnameProvider,
|
||||
Request $request,
|
||||
ChannelInterface $channel
|
||||
) {
|
||||
$fakeHostnameProvider->getHostname($request)->willReturn(null);
|
||||
|
||||
$decoratedRequestResolver->findChannel($request)->willReturn($channel);
|
||||
|
||||
$this->findChannel($request)->shouldReturn($channel);
|
||||
}
|
||||
|
||||
function it_proxies_decorated_request_resolver_with_cloned_request_if_fake_hostname_was_found(
|
||||
RequestResolverInterface $decoratedRequestResolver,
|
||||
FakeHostnameProviderInterface $fakeHostnameProvider,
|
||||
ChannelInterface $channel
|
||||
) {
|
||||
$request = new Request();
|
||||
|
||||
$fakeHostnameProvider->getHostname($request)->willReturn('fake.hostname');
|
||||
|
||||
$decoratedRequestResolver->findChannel(Argument::that(function (Request $clonedRequest) use ($request) {
|
||||
if ($request === $clonedRequest) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ('fake.hostname' !== $clonedRequest->headers->get('HOST')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}))->willReturn($channel);
|
||||
|
||||
$this->findChannel($request)->shouldReturn($channel);
|
||||
}
|
||||
}
|
||||
|
|
@ -29,4 +29,11 @@ interface ChannelRepositoryInterface
|
|||
* @return ChannelInterface|null
|
||||
*/
|
||||
public function findOneByHostname($hostname);
|
||||
|
||||
/**
|
||||
* @param string $code
|
||||
*
|
||||
* @return ChannelInterface|null
|
||||
*/
|
||||
public function findOneByCode($code);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue