Skip to content

Commit

Permalink
feature #12109 [API] placing order on multiple channels with api (art…
Browse files Browse the repository at this point in the history
…i0090)

This PR was merged into the 1.9-dev branch.

Discussion
----------

This is a continuation of this PR #12091 
After the comments there were too many changes to do or revert so task will be continued here

Commits
-------

8b8a629 placing order on multiple channels with api
c87076e fixed phpspec for PickupCartHandler
31229b1 fix psalm annotations
f7bb7fe Remove unnecessary context
db04349 add missing doc block
  • Loading branch information
GSadee committed Dec 2, 2020
2 parents c54d62e + db04349 commit 3464e8d
Show file tree
Hide file tree
Showing 18 changed files with 169 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Feature: Placing an order on multiple channels with different currency
And there is a customer account "customer@example.com" identified by "sylius"
And I am logged in as "customer@example.com"

@ui
@ui @api
Scenario: Placing an order in a channels base currency
Given I changed my current channel to "United States"
And I have product "PHP T-Shirt" in the cart
Expand All @@ -26,7 +26,7 @@ Feature: Placing an order on multiple channels with different currency
And I confirm my order
Then the administrator should see that order placed by "customer@example.com" has "USD" currency

@ui
@ui @api
Scenario: Placing an order on a different channel with same currency
Given I changed my current channel to "Colombia"
And I had product "PHP T-Shirt" in the cart
Expand Down
15 changes: 15 additions & 0 deletions src/Sylius/Behat/Context/Api/Admin/ManagingOrdersContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,19 @@ public function theCustomerServiceShouldKnowAboutThisAdditionalNotes(

Assert::same($notes, $orderNotes);
}

/**
* @Then /^(the administrator) should see that (order placed by "[^"]+") has "([^"]+)" currency$/
*/
public function theAdministratorShouldSeeThatThisOrderHasBeenPlacedIn(
AdminUserInterface $user,
OrderInterface $order,
string $currency
): void {
$this->adminSecurityService->logIn($user);

$currencyCode = $this->responseChecker->getValue($this->client->show($order->getTokenValue()), 'currencyCode');

Assert::same($currencyCode, $currency);
}
}
37 changes: 0 additions & 37 deletions src/Sylius/Behat/Context/Api/Shop/ChannelContext.php

This file was deleted.

12 changes: 10 additions & 2 deletions src/Sylius/Behat/Context/Setup/CartContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Bundle\ApiBundle\Command\Cart\AddItemToCart;
use Sylius\Bundle\ApiBundle\Command\Cart\PickupCart;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Product\Model\ProductOptionInterface;
Expand Down Expand Up @@ -61,7 +62,7 @@ public function theCustomerHasTheCart(): void

/**
* @Given /^I added (product "[^"]+") to the (cart)$/
* @Given /^I have (product "[^"]+") in the (cart)$/
* @Given /^I (?:have|had) (product "[^"]+") in the (cart)$/
* @Given /^I have (product "[^"]+") added to the (cart)$/
* @Given /^the (?:customer|visitor) has (product "[^"]+") in the (cart)$/
* @When /^the (?:customer|visitor) try to add (product "[^"]+") in the customer (cart)$/
Expand Down Expand Up @@ -132,7 +133,14 @@ private function pickupCart(): string
{
$tokenValue = $this->generator->generateUriSafeString(10);

$this->commandBus->dispatch(new PickupCart($tokenValue));
/** @var ChannelInterface $channel */
$channel = $this->sharedStorage->get('channel');
$channelCode = $channel->getCode();

$commandPickupCart = new PickupCart($tokenValue);
$commandPickupCart->setChannelCode($channelCode);

$this->commandBus->dispatch($commandPickupCart);

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

Expand Down
17 changes: 17 additions & 0 deletions src/Sylius/Behat/Context/Setup/ChannelContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Behat\Behat\Context\Context;
use Doctrine\Common\Persistence\ObjectManager;
use Sylius\Behat\Service\Setter\ChannelContextSetterInterface;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Addressing\Model\CountryInterface;
use Sylius\Component\Addressing\Model\ZoneInterface;
Expand All @@ -30,6 +31,9 @@ final class ChannelContext implements Context
/** @var SharedStorageInterface */
private $sharedStorage;

/** @var ChannelContextSetterInterface */
private $channelContextSetter;

/** @var DefaultChannelFactoryInterface */
private $unitedStatesChannelFactory;

Expand All @@ -44,12 +48,14 @@ final class ChannelContext implements Context

public function __construct(
SharedStorageInterface $sharedStorage,
ChannelContextSetterInterface $channelContextSetter,
DefaultChannelFactoryInterface $unitedStatesChannelFactory,
DefaultChannelFactoryInterface $defaultChannelFactory,
ChannelRepositoryInterface $channelRepository,
ObjectManager $channelManager
) {
$this->sharedStorage = $sharedStorage;
$this->channelContextSetter = $channelContextSetter;
$this->unitedStatesChannelFactory = $unitedStatesChannelFactory;
$this->defaultChannelFactory = $defaultChannelFactory;
$this->channelRepository = $channelRepository;
Expand Down Expand Up @@ -251,6 +257,17 @@ public function channelDoesNotDefineOperatingCountries(ChannelInterface $channel
$this->channelManager->flush();
}

/**
* @Given /^I changed (?:|back )my current (channel to "([^"]+)")$/
* @When /^I change (?:|back )my current (channel to "([^"]+)")$/
*/
public function iChangeMyCurrentChannelTo(ChannelInterface $channel): void
{
$this->sharedStorage->set('channel', $channel);
$this->sharedStorage->set('hostname', $channel->getHostname());
$this->channelContextSetter->setChannel($channel);
}

/**
* @param bool $state
*/
Expand Down
9 changes: 0 additions & 9 deletions src/Sylius/Behat/Context/Ui/ChannelContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,6 @@ public function __construct(
$this->pluginMainPage = $pluginMainPage;
}

/**
* @Given /^I changed (?:|back )my current (channel to "([^"]+)")$/
* @When /^I change (?:|back )my current (channel to "([^"]+)")$/
*/
public function iChangeMyCurrentChannelTo(ChannelInterface $channel): void
{
$this->channelContextSetter->setChannel($channel);
}

/**
* @When I create a new channel :channelName
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />
</service>

<service id="sylius.behat.context.api.shop.channel" class="Sylius\Behat\Context\Api\Shop\ChannelContext">
<argument type="service" id="sylius.behat.shared_storage" />
</service>

<service id="sylius.behat.context.api.shop.checkout" class="Sylius\Behat\Context\Api\Shop\CheckoutContext">
<argument type="service" id="test.client" />
<argument type="service" id="sylius.behat.api_platform_client.shop.order" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

<service id="sylius.behat.context.setup.channel" class="Sylius\Behat\Context\Setup\ChannelContext">
<argument type="service" id="sylius.behat.shared_storage" />
<argument type="service" id="sylius.behat.channel_context_setter" />
<argument type="service" id="sylius.behat.factory.default_united_states_channel" />
<argument type="service" id="sylius.behat.factory.default_channel" />
<argument type="service" id="sylius.repository.channel" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ default:
- sylius.behat.context.transform.address
- sylius.behat.context.transform.cart
- sylius.behat.context.transform.country
- sylius.behat.context.transform.channel
- sylius.behat.context.transform.lexical
- sylius.behat.context.transform.locale
- sylius.behat.context.transform.order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ default:
- sylius.behat.context.setup.product
- sylius.behat.context.setup.taxonomy

- sylius.behat.context.api.shop.channel
- sylius.behat.context.api.shop.homepage

filters:
Expand Down
26 changes: 20 additions & 6 deletions src/Sylius/Bundle/ApiBundle/Command/Cart/PickupCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,31 @@

namespace Sylius\Bundle\ApiBundle\Command\Cart;

/**
* @experimental
* @psalm-immutable
*/
class PickupCart
use Sylius\Bundle\ApiBundle\Command\ChannelCodeAwareInterface;

/** @experimental */
class PickupCart implements ChannelCodeAwareInterface
{
/** @var string|null */
/** @var string|null
* @psalm-immutable
*/
public $tokenValue;

/** @var string|null */
private $channelCode;

public function __construct(?string $tokenValue = null)
{
$this->tokenValue = $tokenValue;
}

public function getChannelCode(): ?string
{
return $this->channelCode;
}

public function setChannelCode(?string $channelCode): void
{
$this->channelCode = $channelCode;
}
}
22 changes: 22 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Command/ChannelCodeAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\Command;

/** @experimental */
interface ChannelCodeAwareInterface extends CommandAwareDataTransformerInterface
{
public function getChannelCode(): ?string;

public function setChannelCode(?string $channelCode): void;
}
12 changes: 6 additions & 6 deletions src/Sylius/Bundle/ApiBundle/CommandHandler/PickupCartHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Sylius\Bundle\AdminApiBundle\Model\UserInterface;
use Sylius\Bundle\ApiBundle\Command\Cart\PickupCart;
use Sylius\Bundle\ApiBundle\Context\UserContextInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
Expand All @@ -38,8 +38,8 @@ final class PickupCartHandler implements MessageHandlerInterface
/** @var OrderRepositoryInterface */
private $cartRepository;

/** @var ChannelContextInterface */
private $channelContext;
/** @var ChannelRepositoryInterface */
private $channelRepository;

/** @var UserContextInterface */
private $userContext;
Expand All @@ -53,14 +53,14 @@ final class PickupCartHandler implements MessageHandlerInterface
public function __construct(
FactoryInterface $cartFactory,
OrderRepositoryInterface $cartRepository,
ChannelContextInterface $channelContext,
ChannelRepositoryInterface $channelRepository,
UserContextInterface $userContext,
ObjectManager $orderManager,
RandomnessGeneratorInterface $generator
) {
$this->cartFactory = $cartFactory;
$this->cartRepository = $cartRepository;
$this->channelContext = $channelContext;
$this->channelRepository = $channelRepository;
$this->userContext = $userContext;
$this->orderManager = $orderManager;
$this->generator = $generator;
Expand All @@ -69,7 +69,7 @@ public function __construct(
public function __invoke(PickupCart $pickupCart)
{
/** @var ChannelInterface $channel */
$channel = $this->channelContext->getChannel();
$channel = $this->channelRepository->findOneByCode($pickupCart->getChannelCode());

$customer = $this->provideCustomer();
if ($customer !== null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\DataTransformer;

use Sylius\Bundle\ApiBundle\Command\ChannelCodeAwareInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;

/** @experimental */
final class ChannelCodeAwareInputCommandDataTransformer implements CommandDataTransformerInterface
{
/** @var ChannelContextInterface */
private $channelContext;

public function __construct(ChannelContextInterface $channelContext)
{
$this->channelContext = $channelContext;
}

public function transform($object, string $to, array $context = [])
{
$channel = $this->channelContext->getChannel();

$object->setChannelCode($channel->getCode());

return $object;
}

public function supportsTransformation($object): bool
{
return $object instanceof ChannelCodeAwareInterface;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<group>shop:order:read</group>
</attribute>

<attribute name="currencyCode">
<group>order:read</group>
<group>checkout:read</group>
</attribute>

<attribute name="channel">
<group>order:read</group>
</attribute>
Expand Down
5 changes: 5 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
<tag name="sylius.api.command_data_transformer" />
</service>

<service id="sylius.api.data_transformer.channel_code_aware_input_data_transformer" class="Sylius\Bundle\ApiBundle\DataTransformer\ChannelCodeAwareInputCommandDataTransformer">
<argument type="service" id="sylius.context.channel" />
<tag name="sylius.api.command_data_transformer" />
</service>

<service id="sylius.api.data_transformer.subresource_id_aware_data_transformer" class="Sylius\Bundle\ApiBundle\DataTransformer\SubresourceIdAwareCommandDataTransformer">
<argument type="service" id="request_stack" />
<tag name="sylius.api.command_data_transformer" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<service id="Sylius\Bundle\ApiBundle\CommandHandler\PickupCartHandler">
<argument type="service" id="sylius.factory.order" />
<argument type="service" id="sylius.repository.order" />
<argument type="service" id="sylius.context.channel" />
<argument type="service" id="sylius.repository.channel" />
<argument type="service" id="sylius.api.context.user" />
<argument type="service" id="sylius.manager.order" />
<argument type="service" id="sylius.random_generator" />
Expand Down

0 comments on commit 3464e8d

Please sign in to comment.