[Behat][API] Allow setting no accept-language in header

[Locale] Prepend default channel locale when resolving by header
This commit is contained in:
Jan Goralski 2023-12-22 11:26:31 +01:00
parent d896de175d
commit 0ceb7db697
No known key found for this signature in database
GPG key ID: 95D91BA380F31EDD
4 changed files with 20 additions and 5 deletions

View file

@ -18,7 +18,7 @@ Feature: Picking up the cart with the locale other than the default
@api @api
Scenario: Picking up the cart without specified locale 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 And I check details of my cart
Then my cart's locale should be "French (France)" Then my cart's locale should be "French (France)"

View file

@ -218,6 +218,7 @@ final class CartContext implements Context
/** /**
* @When I pick up (my )cart (again) * @When I pick up (my )cart (again)
* @When I pick up cart in the :localeCode locale * @When I pick up cart in the :localeCode locale
* @When I pick up cart without specifying locale
* @When the visitor picks up the cart * @When the visitor picks up the cart
*/ */
public function iPickUpMyCart(?string $localeCode = null): void public function iPickUpMyCart(?string $localeCode = null): void
@ -776,8 +777,9 @@ final class CartContext implements Context
$request = $this->requestFactory->custom( $request = $this->requestFactory->custom(
sprintf('%s/shop/orders', $this->apiUrlPrefix), sprintf('%s/shop/orders', $this->apiUrlPrefix),
HttpRequest::METHOD_POST, HttpRequest::METHOD_POST,
$localeCode ? ['HTTP_ACCEPT_LANGUAGE' => $localeCode] : [], ['HTTP_ACCEPT_LANGUAGE' => $localeCode ?? ''],
); );
$this->shopClient->executeCustomRequest($request); $this->shopClient->executeCustomRequest($request);
$tokenValue = $this->responseChecker->getValue($this->shopClient->getLastResponse(), 'tokenValue'); $tokenValue = $this->responseChecker->getValue($this->shopClient->getLastResponse(), 'tokenValue');

View file

@ -27,6 +27,9 @@ final class RequestHeaderBasedLocaleContext implements LocaleContextInterface
{ {
private const NO_CODE_VALID_STUB = 'NO_CODE_VALID_STUB'; 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) 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.'); 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 // 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. // 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); $bestLocaleCode = $request->getPreferredLanguage($prependedAvailableLocalesCodes);
if (self::NO_CODE_VALID_STUB === $bestLocaleCode) { if (self::NO_CODE_VALID_STUB === $bestLocaleCode) {
throw new LocaleNotFoundException(sprintf( throw new LocaleNotFoundException(sprintf(
'None of the available locales is acceptable: "%s".', 'None of the available locales is acceptable: "%s".',
implode('", "', $availableLocalesCodes), implode('", "', $this->availableLocalesCodes),
)); ));
} }

View file

@ -48,6 +48,7 @@ final class RequestHeaderBasedLocaleContextSpec extends ObjectBehavior
$requestStack->getMainRequest()->willReturn($request); $requestStack->getMainRequest()->willReturn($request);
$localeProvider->getDefaultLocaleCode()->willReturn('pl_PL');
$localeProvider->getAvailableLocalesCodes()->willReturn(['pl_PL', 'de_DE']); $localeProvider->getAvailableLocalesCodes()->willReturn(['pl_PL', 'de_DE']);
$this->shouldThrow(LocaleNotFoundException::class)->during('getLocaleCode'); $this->shouldThrow(LocaleNotFoundException::class)->during('getLocaleCode');
@ -62,6 +63,7 @@ final class RequestHeaderBasedLocaleContextSpec extends ObjectBehavior
$requestStack->getMainRequest()->willReturn($request); $requestStack->getMainRequest()->willReturn($request);
$localeProvider->getDefaultLocaleCode()->willReturn('pl_PL');
$localeProvider->getAvailableLocalesCodes()->willReturn(['pl_PL', 'de_DE']); $localeProvider->getAvailableLocalesCodes()->willReturn(['pl_PL', 'de_DE']);
$this->getLocaleCode()->shouldReturn('de_DE'); $this->getLocaleCode()->shouldReturn('de_DE');
@ -76,6 +78,7 @@ final class RequestHeaderBasedLocaleContextSpec extends ObjectBehavior
$requestStack->getMainRequest()->willReturn($request); $requestStack->getMainRequest()->willReturn($request);
$localeProvider->getDefaultLocaleCode()->willReturn('pl_PL');
$localeProvider->getAvailableLocalesCodes()->willReturn(['pl_PL', 'de_DE']); $localeProvider->getAvailableLocalesCodes()->willReturn(['pl_PL', 'de_DE']);
$this->getLocaleCode()->shouldReturn('de_DE'); $this->getLocaleCode()->shouldReturn('de_DE');
@ -90,6 +93,7 @@ final class RequestHeaderBasedLocaleContextSpec extends ObjectBehavior
$requestStack->getMainRequest()->willReturn($request); $requestStack->getMainRequest()->willReturn($request);
$localeProvider->getDefaultLocaleCode()->willReturn('pl_PL');
$localeProvider->getAvailableLocalesCodes()->willReturn(['pl_PL', 'de_DE']); $localeProvider->getAvailableLocalesCodes()->willReturn(['pl_PL', 'de_DE']);
$this->getLocaleCode()->shouldReturn('de_DE'); $this->getLocaleCode()->shouldReturn('de_DE');
@ -104,6 +108,7 @@ final class RequestHeaderBasedLocaleContextSpec extends ObjectBehavior
$requestStack->getMainRequest()->willReturn($request); $requestStack->getMainRequest()->willReturn($request);
$localeProvider->getDefaultLocaleCode()->willReturn('pl_PL');
$localeProvider->getAvailableLocalesCodes()->willReturn(['pl_PL', 'de_DE']); $localeProvider->getAvailableLocalesCodes()->willReturn(['pl_PL', 'de_DE']);
$this->getLocaleCode()->shouldReturn('de_DE'); $this->getLocaleCode()->shouldReturn('de_DE');