Enable scenarios, add component

# Conflicts:
#	src/Sylius/Bundle/ShopBundle/Resources/config/services/twig/component.xml
This commit is contained in:
Michał Pysiak 2024-08-23 13:41:12 +02:00 committed by Grzegorz Sadowski
parent 090506a180
commit 787cde8fb3
14 changed files with 85 additions and 47 deletions

View file

@ -12,18 +12,18 @@ Feature: Browsing shop in different than default locale
And this product is named "Koszulka bananowa" in the "Polish (Poland)" locale
And this product is named "T" in the "Chinese (Simplified, China)" locale
@todo @ui @api
@api @ui
Scenario: Browsing product details in non-default locale
When I browse that channel
And I check this product's details in the "Polish (Poland)" locale
Then I should see the product name "Koszulka bananowa"
@todo @ui @api
@api @ui
Scenario: Browsing product details in non-default locale
When I check this product's details in the "Chinese (Simplified, China)" locale
Then I should see the product name "T"
@todo @ui @api
@api @ui
Scenario: Not being able to shop using a locale non-existent in the channel
When I browse that channel
And I try to check this product's details in the "Irish (Ireland)" locale

View file

@ -10,7 +10,7 @@ Feature: Getting available locales in the current channel
And it uses the "English (United States)" locale by default
And the store has locale "German (Germany)"
@todo @api
@api @no-ui
Scenario: Getting only available locales in channel
When I get available locales
Then I should have 2 locales
@ -18,7 +18,7 @@ Feature: Getting available locales in the current channel
And the "Polish (Poland)" locale with code "pl_PL" should be available
But the "German (Germany)" locale with code "de_DE" should not be available
@todo @api
@api @no-ui
Scenario: Get locales details
When I get "English (United States)" locale
Then I should have "English (United States)" with code "en_US"

View file

@ -12,20 +12,20 @@ Feature: Handling different locales on multiple channels
And that channel allows to shop using "Polish (Poland)" and "Norwegian (Norway)" locales
And it uses the "Polish (Poland)" locale by default
@todo @ui @api
@api @ui
Scenario: Showing locales only from the current channel
When I browse the "Mobile" channel
Then I should shop using the "Polish (Poland)" locale
And I should be able to shop using the "Norwegian (Norway)" locale
And I should not be able to shop using the "English (United States)" locale
@todo @ui @api
@api @ui
Scenario: Browsing channels using their default locales
When I browse the "Web" channel
And I start browsing the "Mobile" channel
Then I should shop using the "Polish (Poland)" locale
@todo @ui @api
@api @ui
Scenario: Switching a locale applies only to the current channel
When I browse the "Web" channel
And I switch to the "Norwegian (Norway)" locale

View file

@ -10,13 +10,13 @@ Feature: Redirecting to the default locale
And it uses the "English (United States)" locale by default
And the store has a product "PHP T-Shirt"
@todo @ui @api
@api @ui
Scenario: Staying on the current locale if it is available
When I browse that channel
And I use the locale "French (France)"
Then I should shop using the "French (France)" locale
@todo @ui @api
@api @ui
Scenario: Redirecting to default locale if it is not available
When I browse that channel
And I use the locale "Polish (Poland)"

View file

@ -9,17 +9,17 @@ Feature: Switching the current locale
And that channel allows to shop using "English (United States)" and "Polish (Poland)" locales
And it uses the "English (United States)" locale by default
@todo @ui @api
@api @ui
Scenario: Showing the current locale
When I browse that channel
Then I should shop using the "English (United States)" locale
@todo @ui @api
@api @ui
Scenario: Showing available locales
When I browse that channel
Then I should be able to shop using the "Polish (Poland)" locale
@todo @ui @api
@api @ui
Scenario: Switching the current locale
When I browse that channel
And I switch to the "Polish (Poland)" locale

View file

@ -20,7 +20,7 @@ use Sylius\Component\Locale\Context\LocaleNotFoundException;
use Sylius\Component\Locale\Model\LocaleInterface;
use Webmozart\Assert\Assert;
final class LocaleContext implements Context
final readonly class LocaleContext implements Context
{
public function __construct(
private HomePageInterface $homePage,
@ -63,7 +63,7 @@ final class LocaleContext implements Context
* @Then I should be able to shop using the :localeNameInCurrentLocale locale
* @Then the store should be available in the :localeNameInCurrentLocale locale
*/
public function iShouldBeAbleToShopUsingTheLocale(string $localeNameInCurrentLocale)
public function iShouldBeAbleToShopUsingTheLocale(string $localeNameInCurrentLocale): void
{
Assert::oneOf($localeNameInCurrentLocale, $this->homePage->getAvailableLocales());
}
@ -72,7 +72,7 @@ final class LocaleContext implements Context
* @Then I should not be able to shop using the :locale locale
* @Then the store should not be available in the :locale locale
*/
public function iShouldNotBeAbleToShopUsingTheLocale($locale)
public function iShouldNotBeAbleToShopUsingTheLocale(string $locale): void
{
if (in_array($locale, $this->homePage->getAvailableLocales(), true)) {
throw new \InvalidArgumentException(sprintf(
@ -86,7 +86,7 @@ final class LocaleContext implements Context
/**
* @Then I should not be able to shop without default locale
*/
public function iShouldNotBeAbleToShop()
public function iShouldNotBeAbleToShop(): void
{
try {
$this->homePage->tryToOpen();

View file

@ -14,31 +14,19 @@ declare(strict_types=1);
namespace Sylius\Bundle\ShopBundle\Controller;
use Sylius\Bundle\ShopBundle\Locale\LocaleSwitcherInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Sylius\Component\Locale\Provider\LocaleProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Twig\Environment;
final class LocaleSwitchController
final readonly class LocaleSwitchController
{
public function __construct(
private Environment $templatingEngine,
private LocaleContextInterface $localeContext,
private LocaleProviderInterface $localeProvider,
private LocaleSwitcherInterface $localeSwitcher,
) {
}
public function renderAction(): Response
{
return new Response($this->templatingEngine->render('@SyliusShop/shared/layout/base/header/top_bar/locale_switch.html.twig', [
'active' => $this->localeContext->getLocaleCode(),
'locales' => $this->localeProvider->getAvailableLocalesCodes(),
]));
}
public function switchAction(Request $request, ?string $code = null): Response
{
if (null === $code) {

View file

@ -24,7 +24,7 @@ twig_hooks:
currency_switcher:
template: '@SyliusShop/shared/layout/base/header/top_bar/currency_switcher.html.twig'
locale_switcher:
template: '@SyliusShop/shared/layout/base/header/top_bar/locale_switcher.html.twig'
component: 'sylius_shop:common:locale_switcher'
'sylius_shop.base.header.content':
logo:

View file

@ -16,7 +16,6 @@ sylius_shop_homepage:
defaults:
template: "@SyliusShop/homepage/index.html.twig"
sylius_shop_security:
resource: "@SyliusShopBundle/Resources/config/routing/security.yml"

View file

@ -33,8 +33,6 @@
</service>
<service id="sylius.controller.shop.locale_switch" class="Sylius\Bundle\ShopBundle\Controller\LocaleSwitchController">
<argument type="service" id="twig" />
<argument type="service" id="sylius.context.locale" />
<argument type="service" id="sylius.locale_provider" />
<argument type="service" id="sylius.shop.locale_switcher" />
</service>

View file

@ -227,5 +227,19 @@
template="@SyliusShop/account/register/form.html.twig"
/>
</service>
<service
id="sylius_shop.twig.component.locale_switcher"
class="Sylius\Bundle\ShopBundle\Twig\Component\Common\LocaleSwitcher"
>
<argument type="service" id="sylius.context.locale" />
<argument type="service" id="sylius.locale_provider" />
<tag
name="twig.component"
key="sylius_shop:common:locale_switcher"
template="@SyliusShop/shared/layout/base/header/top_bar/locale_switch.html.twig"
/>
</service>
</services>
</container>

View file

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace Sylius\Bundle\ShopBundle\Twig\Component\Common;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Sylius\Component\Locale\Provider\LocaleProviderInterface;
use Sylius\TwigHooks\LiveComponent\HookableLiveComponentTrait;
use Symfony\UX\LiveComponent\DefaultActionTrait;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;
#[AsTwigComponent]
class LocaleSwitcher
{
use DefaultActionTrait;
use HookableLiveComponentTrait;
public function __construct(
private readonly LocaleContextInterface $localeContext,
private readonly LocaleProviderInterface $localeProvider,
) {
}
#[ExposeInTemplate('active_locale')]
public function activeLocale(): string
{
return $this->localeContext->getLocaleCode();
}
/**
* @return array<string>
*/
#[ExposeInTemplate('available_locales')]
public function availableLocales(): array
{
return $this->localeProvider->getAvailableLocalesCodes();
}
}

View file

@ -1,12 +1,14 @@
{% if locales|length > 1 %}
<div {{ sylius_test_html_attribute('locale-selector') }}>
<span {{ sylius_test_html_attribute('active-locale') }}>{{ active|sylius_locale_name }}</span>
{% for code in locales %}
{% if code is not same as(active) %}
<a href="{{ path('sylius_shop_switch_locale', {'code': code}) }}" {{ sylius_test_html_attribute('available-locale') }}>
{{ code|sylius_locale_name }}
</a>
{% endif %}
{% endfor %}
{% if available_locales|length > 1 %}
<div class="col-auto">
<div {{ sylius_test_html_attribute('locale-selector') }}>
<span {{ sylius_test_html_attribute('active-locale') }}>{{ active_locale|sylius_locale_name }}</span>
{% for code in available_locales %}
{% if code is not same as(active_locale) %}
<a href="{{ path('sylius_shop_switch_locale', {'code': code}) }}" {{ sylius_test_html_attribute('available-locale') }}>
{{ code|sylius_locale_name }}
</a>
{% endif %}
{% endfor %}
</div>
</div>
{% endif %}

View file

@ -1,3 +0,0 @@
<div class="col-auto">
{{ render(controller('sylius.controller.shop.locale_switch::renderAction')) }}
</div>