mirror of
https://github.com/Sylius/Sylius.git
synced 2026-07-15 09:30:58 +00:00
[Behat][API] Allow setting no accept-language in header
[Locale] Prepend default channel locale when resolving by header
This commit is contained in:
parent
d896de175d
commit
0ceb7db697
4 changed files with 20 additions and 5 deletions
|
|
@ -18,7 +18,7 @@ Feature: Picking up the cart with the locale other than the default
|
|||
|
||||
@api
|
||||
Scenario: Picking up the cart without specified locale
|
||||
When I pick up cart
|
||||
When I pick up cart without specifying locale
|
||||
And I check details of my cart
|
||||
Then my cart's locale should be "French (France)"
|
||||
|
||||
|
|
|
|||
|
|
@ -218,6 +218,7 @@ final class CartContext implements Context
|
|||
/**
|
||||
* @When I pick up (my )cart (again)
|
||||
* @When I pick up cart in the :localeCode locale
|
||||
* @When I pick up cart without specifying locale
|
||||
* @When the visitor picks up the cart
|
||||
*/
|
||||
public function iPickUpMyCart(?string $localeCode = null): void
|
||||
|
|
@ -776,8 +777,9 @@ final class CartContext implements Context
|
|||
$request = $this->requestFactory->custom(
|
||||
sprintf('%s/shop/orders', $this->apiUrlPrefix),
|
||||
HttpRequest::METHOD_POST,
|
||||
$localeCode ? ['HTTP_ACCEPT_LANGUAGE' => $localeCode] : [],
|
||||
['HTTP_ACCEPT_LANGUAGE' => $localeCode ?? ''],
|
||||
);
|
||||
|
||||
$this->shopClient->executeCustomRequest($request);
|
||||
|
||||
$tokenValue = $this->responseChecker->getValue($this->shopClient->getLastResponse(), 'tokenValue');
|
||||
|
|
|
|||
|
|
@ -27,6 +27,9 @@ final class RequestHeaderBasedLocaleContext implements LocaleContextInterface
|
|||
{
|
||||
private const NO_CODE_VALID_STUB = 'NO_CODE_VALID_STUB';
|
||||
|
||||
/** @var array<array-key, string> $availableLocalesCodes */
|
||||
private array $availableLocalesCodes = [];
|
||||
|
||||
public function __construct(private RequestStack $requestStack, private LocaleProviderInterface $localeProvider)
|
||||
{
|
||||
}
|
||||
|
|
@ -38,17 +41,22 @@ final class RequestHeaderBasedLocaleContext implements LocaleContextInterface
|
|||
throw new LocaleNotFoundException('No main request available.');
|
||||
}
|
||||
|
||||
$availableLocalesCodes = $this->localeProvider->getAvailableLocalesCodes();
|
||||
if ([] === $this->availableLocalesCodes) {
|
||||
$this->availableLocalesCodes = array_unique(array_merge(
|
||||
[$this->localeProvider->getDefaultLocaleCode()],
|
||||
$this->localeProvider->getAvailableLocalesCodes()
|
||||
));
|
||||
}
|
||||
|
||||
// Request::getPreferredLanguage() returns first available locale code if none matches. To allow detection of
|
||||
// this unwanted behavior, we will prepend special locale code to the list of available locale codes.
|
||||
$prependedAvailableLocalesCodes = array_merge([self::NO_CODE_VALID_STUB], $availableLocalesCodes);
|
||||
$prependedAvailableLocalesCodes = array_merge([self::NO_CODE_VALID_STUB], $this->availableLocalesCodes);
|
||||
|
||||
$bestLocaleCode = $request->getPreferredLanguage($prependedAvailableLocalesCodes);
|
||||
if (self::NO_CODE_VALID_STUB === $bestLocaleCode) {
|
||||
throw new LocaleNotFoundException(sprintf(
|
||||
'None of the available locales is acceptable: "%s".',
|
||||
implode('", "', $availableLocalesCodes),
|
||||
implode('", "', $this->availableLocalesCodes),
|
||||
));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -48,6 +48,7 @@ final class RequestHeaderBasedLocaleContextSpec extends ObjectBehavior
|
|||
|
||||
$requestStack->getMainRequest()->willReturn($request);
|
||||
|
||||
$localeProvider->getDefaultLocaleCode()->willReturn('pl_PL');
|
||||
$localeProvider->getAvailableLocalesCodes()->willReturn(['pl_PL', 'de_DE']);
|
||||
|
||||
$this->shouldThrow(LocaleNotFoundException::class)->during('getLocaleCode');
|
||||
|
|
@ -62,6 +63,7 @@ final class RequestHeaderBasedLocaleContextSpec extends ObjectBehavior
|
|||
|
||||
$requestStack->getMainRequest()->willReturn($request);
|
||||
|
||||
$localeProvider->getDefaultLocaleCode()->willReturn('pl_PL');
|
||||
$localeProvider->getAvailableLocalesCodes()->willReturn(['pl_PL', 'de_DE']);
|
||||
|
||||
$this->getLocaleCode()->shouldReturn('de_DE');
|
||||
|
|
@ -76,6 +78,7 @@ final class RequestHeaderBasedLocaleContextSpec extends ObjectBehavior
|
|||
|
||||
$requestStack->getMainRequest()->willReturn($request);
|
||||
|
||||
$localeProvider->getDefaultLocaleCode()->willReturn('pl_PL');
|
||||
$localeProvider->getAvailableLocalesCodes()->willReturn(['pl_PL', 'de_DE']);
|
||||
|
||||
$this->getLocaleCode()->shouldReturn('de_DE');
|
||||
|
|
@ -90,6 +93,7 @@ final class RequestHeaderBasedLocaleContextSpec extends ObjectBehavior
|
|||
|
||||
$requestStack->getMainRequest()->willReturn($request);
|
||||
|
||||
$localeProvider->getDefaultLocaleCode()->willReturn('pl_PL');
|
||||
$localeProvider->getAvailableLocalesCodes()->willReturn(['pl_PL', 'de_DE']);
|
||||
|
||||
$this->getLocaleCode()->shouldReturn('de_DE');
|
||||
|
|
@ -104,6 +108,7 @@ final class RequestHeaderBasedLocaleContextSpec extends ObjectBehavior
|
|||
|
||||
$requestStack->getMainRequest()->willReturn($request);
|
||||
|
||||
$localeProvider->getDefaultLocaleCode()->willReturn('pl_PL');
|
||||
$localeProvider->getAvailableLocalesCodes()->willReturn(['pl_PL', 'de_DE']);
|
||||
|
||||
$this->getLocaleCode()->shouldReturn('de_DE');
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue