Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 364322f

Browse files
committed
Fixes for tests
1 parent 1f7c6f0 commit 364322f

10 files changed

Lines changed: 90 additions & 33 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@checkout
2+
Feature: Seeing cart locale on cart details
3+
In order be certain what is the cart locale
4+
As a Customer
5+
I want to be able to see cart locale on the cart details
6+
7+
Background:
8+
Given the store operates on a single channel in "United States"
9+
And that channel allows to shop using "English (United States)" and "French (France)" locales
10+
And I am a logged in customer
11+
12+
@api
13+
Scenario: Seeing cart locale on the cart summary page after choosing channel locale
14+
When I pick up cart in the "French (France)" locale
15+
And I check details of my cart
16+
Then my cart's locale should be "French (France)"

features/checkout/seeing_order_summary/seeing_order_locale.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ Feature: Seeing order locale on order summary page
2424
Given I have product "Stark T-Shirt" in the cart
2525
When I proceed through checkout process in the "French (France)" locale
2626
Then I should be on the checkout summary step
27-
And my order's locale should be "français (France)"
27+
And my order's locale should be "French (France)"

src/Sylius/Behat/Context/Api/Shop/CartContext.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Sylius\Component\Core\Model\ChannelPricingInterface;
2323
use Sylius\Component\Core\Model\ProductInterface;
2424
use Sylius\Component\Core\Model\ProductVariantInterface;
25+
use Sylius\Component\Locale\Model\LocaleInterface;
2526
use Sylius\Component\Product\Resolver\ProductVariantResolverInterface;
2627
use Symfony\Component\HttpFoundation\Request as HttpRequest;
2728
use Symfony\Component\HttpFoundation\Response;
@@ -144,10 +145,11 @@ public function iRemoveProductFromTheCart(ProductInterface $product, string $tok
144145

145146
/**
146147
* @When I pick up my cart (again)
148+
* @When I pick up cart in the :localeCode locale
147149
*/
148-
public function iPickUpMyCart(): void
150+
public function iPickUpMyCart(?string $localeCode = null): void
149151
{
150-
$this->pickupCart();
152+
$this->pickupCart($localeCode);
151153
}
152154

153155
/**
@@ -158,6 +160,17 @@ public function iCheckDetailsOfMyCart(string $tokenValue): void
158160
$this->cartsClient->show($tokenValue);
159161
}
160162

163+
/**
164+
* @Then my cart's locale should be :locale
165+
*/
166+
public function myCartLocaleShouldBe(LocaleInterface $locale): void
167+
{
168+
Assert::same($this->responseChecker->getValue(
169+
$this->cartsClient->getLastResponse(), 'localeCode'),
170+
$locale->getCode()
171+
);
172+
}
173+
161174
/**
162175
* @Then /^I don't have access to see the summary of my (previous cart)$/
163176
*/
@@ -426,9 +439,10 @@ public function iShouldHaveEmptyCart(string $tokenValue): void
426439
Assert::same(count($items), 0, 'There should be an empty cart');
427440
}
428441

429-
private function pickupCart(): string
442+
private function pickupCart(?string $localeCode = null): string
430443
{
431444
$this->cartsClient->buildCreateRequest();
445+
$this->cartsClient->addRequestData('localeCode', $localeCode);
432446
$tokenValue = $this->responseChecker->getValue($this->cartsClient->create(), 'tokenValue');
433447

434448
$this->sharedStorage->set('cart_token', $tokenValue);

src/Sylius/Bundle/ApiBundle/CommandHandler/PickupCartHandler.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,11 @@ public function __invoke(PickupCart $pickupCart)
8383
/** @var OrderInterface $cart */
8484
$cart = $this->cartFactory->createNew();
8585

86-
/** @var LocaleInterface $locale */
87-
$locale = $channel->getDefaultLocale();
88-
89-
/** @var string $localeCode */
90-
$localeCode = $locale->getCode();
91-
92-
/** @var string|null $commandLocaleCode */
93-
$commandLocaleCode = $pickupCart->localeCode;
94-
95-
if ($commandLocaleCode !== null && $channel->hasLocaleWithLocaleCode($commandLocaleCode)) {
96-
$localeCode = $commandLocaleCode;
97-
}
98-
9986
/** @var CurrencyInterface $currency */
10087
$currency = $channel->getBaseCurrency();
10188

10289
$cart->setChannel($channel);
103-
$cart->setLocaleCode($localeCode);
90+
$cart->setLocaleCode($this->getLocaleCode($pickupCart->localeCode, $channel));
10491
$cart->setCurrencyCode($currency->getCode());
10592
$cart->setTokenValue($pickupCart->tokenValue ?? $this->generator->generateUriSafeString(10));
10693
if ($customer !== null) {
@@ -122,4 +109,27 @@ private function provideCustomer(): ?CustomerInterface
122109

123110
return null;
124111
}
112+
113+
private function hasLocaleWithLocaleCode(ChannelInterface $channel, ?string $localeCode): bool
114+
{
115+
$locales = $channel->getLocales();
116+
117+
$localeWithExpectedCode = $locales->filter(function (LocaleInterface $locale) use ($localeCode): bool {
118+
return $locale->getCode() === $localeCode;
119+
});
120+
121+
return !$localeWithExpectedCode->isEmpty();
122+
}
123+
124+
private function getLocaleCode(?string $localeCode, ChannelInterface $channel): string
125+
{
126+
if ($localeCode === null || !$this->hasLocaleWithLocaleCode($channel, $localeCode)) {
127+
/** @var LocaleInterface $locale */
128+
$locale = $channel->getDefaultLocale();
129+
130+
$localeCode = $locale->getCode();
131+
}
132+
133+
return $localeCode;
134+
}
125135
}

src/Sylius/Bundle/ApiBundle/Resources/config/api_resources/Order.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<attribute name="groups">order:create</attribute>
4040
</attribute>
4141
<attribute name="openapi_context">
42-
<attribute name="summary">Pickups a new cart</attribute>
42+
<attribute name="summary">Pickups a new cart. Provided locale code has to be one of available for a particular channel. Setting different local code will result in the assignment of the default locale from the channel.</attribute>
4343
</attribute>
4444
</collectionOperation>
4545

@@ -313,6 +313,9 @@
313313
<attribute name="denormalization_context">
314314
<attribute name="groups">cart:update</attribute>
315315
</attribute>
316+
<attribute name="openapi_context">
317+
<attribute name="summary">Change locale code on cart</attribute>
318+
</attribute>
316319
</itemOperation>
317320
</itemOperations>
318321

@@ -379,5 +382,6 @@
379382
</property>
380383
<property name="billingAddress" writable="false" />
381384
<property name="shippingAddress" writable="false" />
385+
<property name="localeCode" writable="true" />
382386
</resource>
383387
</resources>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" ?>
2+
3+
<!--
4+
5+
This file is part of the Sylius package.
6+
7+
(c) Paweł Jędrzejewski
8+
9+
For the full copyright and license information, please view the LICENSE
10+
file that was distributed with this source code.
11+
12+
-->
13+
14+
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
15+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
16+
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
17+
>
18+
<class name="Sylius\Bundle\ApiBundle\Command\Cart\PickupCart">
19+
<attribute name="localeCode">
20+
<group>order:create</group>
21+
</attribute>
22+
</class>
23+
</serializer>

src/Sylius/Bundle/ApiBundle/Resources/config/serialization/Order.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
<group>order:read</group>
114114
<group>checkout:read</group>
115115
<group>cart:update</group>
116+
<group>order:read</group>
116117
</attribute>
117118

118119
<attribute name="shippingTotal">

src/Sylius/Bundle/ApiBundle/spec/CommandHandler/PickupCartHandlerSpec.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace spec\Sylius\Bundle\ApiBundle\CommandHandler;
1515

16+
use Doctrine\Common\Collections\ArrayCollection;
1617
use Doctrine\Persistence\ObjectManager;
1718
use PhpSpec\ObjectBehavior;
1819
use Prophecy\Argument;
@@ -185,7 +186,8 @@ function it_picks_up_a_cart_with_locale_code_for_visitor(
185186
$channelRepository->findOneByCode('code')->willReturn($channel);
186187
$channel->getBaseCurrency()->willReturn($currency);
187188
$channel->getDefaultLocale()->willReturn($locale);
188-
$channel->hasLocaleWithLocaleCode('en_US')->willReturn(true);
189+
$locale->getCode()->willReturn('en_US');
190+
$channel->getLocales()->willReturn(new ArrayCollection([$locale->getWrappedObject()]));
189191

190192
$userContext->getUser()->willReturn(null);
191193

src/Sylius/Component/Core/Model/Channel.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,6 @@ public function hasLocale(LocaleInterface $locale): bool
180180
return $this->locales->contains($locale);
181181
}
182182

183-
public function hasLocaleWithLocaleCode(string $localeCode): bool
184-
{
185-
return !$this
186-
->locales
187-
->filter(function (LocaleInterface $locale) use ($localeCode): bool {
188-
return $locale->getCode() === $localeCode;
189-
})
190-
->isEmpty()
191-
;
192-
}
193-
194183
public function getCountries(): Collection
195184
{
196185
return $this->countries;

src/Sylius/Component/Locale/Model/LocalesAwareInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ public function getLocales(): Collection;
2626

2727
public function hasLocale(LocaleInterface $locale): bool;
2828

29-
public function hasLocaleWithLocaleCode(string $localeCode): bool;
30-
3129
public function addLocale(LocaleInterface $locale): void;
3230

3331
public function removeLocale(LocaleInterface $locale): void;

0 commit comments

Comments
 (0)