-
Notifications
You must be signed in to change notification settings - Fork 89
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 #191 from lchrusciel/fix-request-locale
Fix request locale handling
- Loading branch information
Showing
9 changed files
with
143 additions
and
2 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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
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,4 @@ | ||
sylius: | ||
shop_api: | ||
cart: | ||
not_exists: Warenkorb existiert nicht. |
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,4 @@ | ||
sylius: | ||
shop_api: | ||
cart: | ||
not_exists: Cart does not exists. |
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
9 changes: 9 additions & 0 deletions
9
tests/Responses/Expected/cart/validation_cart_not_exists_in_german_response.json
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,9 @@ | ||
{ | ||
"code": 400, | ||
"message": "Validation failed", | ||
"errors": { | ||
"token": [ | ||
"Warenkorb existiert nicht." | ||
] | ||
} | ||
} |
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