Skip to content

Commit

Permalink
Merge pull request #191 from lchrusciel/fix-request-locale
Browse files Browse the repository at this point in the history
Fix request locale handling
  • Loading branch information
Arminek authored Jul 18, 2017
2 parents 5ce3937 + 6b9b1ea commit bc473e8
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 2 deletions.
78 changes: 78 additions & 0 deletions spec/EventListener/RequestLocaleSetterSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types = 1);

namespace spec\Sylius\ShopApiPlugin\EventListener;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Component\Locale\Provider\LocaleProviderInterface;
use Sylius\ShopApiPlugin\EventListener\RequestLocaleSetter;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

final class RequestLocaleSetterSpec extends ObjectBehavior
{
function let(LocaleProviderInterface $localeProvider)
{
$this->beConstructedWith($localeProvider);
}

function it_is_initializable()
{
$this->shouldHaveType(RequestLocaleSetter::class);
}

function it_sets_default_locale_on_request_if_locale_is_not_set(
LocaleProviderInterface $localeProvider,
GetResponseEvent $event,
Request $request
) {
$event->getRequest()->willReturn($request);

$request->get('_locale')->willReturn(null);

$localeProvider->getDefaultLocaleCode()->willReturn('en_US');

$request->setLocale('pl_PL')->shouldNotBeCalled();
$request->setDefaultLocale('en_US')->shouldBeCalled();

$this->onKernelRequest($event);
}

function it_sets_default_locale_on_request_if_provided_locale_is_not_available(
LocaleProviderInterface $localeProvider,
GetResponseEvent $event,
Request $request
) {
$event->getRequest()->willReturn($request);

$request->get('_locale')->willReturn('pl_PL');

$localeProvider->getDefaultLocaleCode()->willReturn('en_US');
$localeProvider->getAvailableLocalesCodes()->willReturn(['en_US']);

$request->setLocale('pl_PL')->shouldNotBeCalled();
$request->setDefaultLocale('en_US')->shouldBeCalled();

$this->onKernelRequest($event);
}

function it_sets_locale_and_default_locale_on_request(
LocaleProviderInterface $localeProvider,
GetResponseEvent $event,
Request $request
) {
$event->getRequest()->willReturn($request);

$request->get('_locale')->willReturn('pl_PL');

$localeProvider->getDefaultLocaleCode()->willReturn('en_US');
$localeProvider->getAvailableLocalesCodes()->willReturn(['en_US', 'pl_PL']);

$request->setLocale('pl_PL')->shouldBeCalled();
$request->setDefaultLocale('en_US')->shouldBeCalled();

$this->onKernelRequest($event);
}
}
36 changes: 36 additions & 0 deletions src/EventListener/RequestLocaleSetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Sylius\ShopApiPlugin\EventListener;

use Sylius\Component\Locale\Provider\LocaleProviderInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

final class RequestLocaleSetter
{
/** @var LocaleProviderInterface */
private $localeProvider;

public function __construct(LocaleProviderInterface $localeProvider)
{
$this->localeProvider = $localeProvider;
}

public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();

$request->setDefaultLocale($this->localeProvider->getDefaultLocaleCode());

$localeCode = $request->get('_locale');
if (null === $localeCode) {
return;
}

$availableLocalesCodes = $this->localeProvider->getAvailableLocalesCodes();
if (in_array($localeCode, $availableLocalesCodes, true)) {
$request->setLocale($localeCode);
}
}
}
5 changes: 5 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,10 @@
</service>

<service id="sylius.shop_api_plugin.generator.product_breadcrumb_generator" class="Sylius\ShopApiPlugin\Generator\ProductBreadcrumbGenerator" />

<service id="sylius.shop_api_plugin.event_listener.request_locale_setter" class="Sylius\ShopApiPlugin\EventListener\RequestLocaleSetter" >
<argument type="service" id="sylius.locale_provider" />
<tag name="kernel.event_listener" event="kernel.request" priority="3" />
</service>
</services>
</container>
4 changes: 4 additions & 0 deletions src/Resources/translations/validators.de.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sylius:
shop_api:
cart:
not_exists: Warenkorb existiert nicht.
4 changes: 4 additions & 0 deletions src/Resources/translations/validators.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sylius:
shop_api:
cart:
not_exists: Cart does not exists.
5 changes: 5 additions & 0 deletions tests/Controller/CartDropCartApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public function it_returns_not_found_exception_if_cart_has_not_been_found()
$response = $this->client->getResponse();

$this->assertResponse($response, 'cart/validation_cart_not_exists_response', Response::HTTP_BAD_REQUEST);

$this->client->request('DELETE', '/shop-api/carts/SDAOSLEFNWU35H3QLI5325?_locale=de_DE', [], [], ['ACCEPT' => 'application/json']);
$response = $this->client->getResponse();

$this->assertResponse($response, 'cart/validation_cart_not_exists_in_german_response', Response::HTTP_BAD_REQUEST);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"message": "Validation failed",
"errors": {
"token": [
"sylius.shop_api.cart.not_exists"
"Cart does not exists."
],
"id": [
"sylius.shop_api.cart_item.not_exists"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"code": 400,
"message": "Validation failed",
"errors": {
"token": [
"Warenkorb existiert nicht."
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"message": "Validation failed",
"errors": {
"token": [
"sylius.shop_api.cart.not_exists"
"Cart does not exists."
]
}
}

0 comments on commit bc473e8

Please sign in to comment.