New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API] Pick up cart implementation #11524
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?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\Behat\Context\Api\Shop; | ||
|
||
use Behat\Behat\Context\Context; | ||
use Sylius\Behat\Client\ApiClientInterface; | ||
use Sylius\Behat\Client\Request; | ||
use Sylius\Behat\Client\ResponseCheckerInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class CartContext implements Context | ||
{ | ||
/** @var ApiClientInterface */ | ||
private $cartsClient; | ||
|
||
/** @var ResponseCheckerInterface */ | ||
private $responseChecker; | ||
|
||
public function __construct(ApiClientInterface $cartsClient, ResponseCheckerInterface $responseChecker) | ||
{ | ||
$this->cartsClient = $cartsClient; | ||
$this->responseChecker = $responseChecker; | ||
} | ||
|
||
/** | ||
* @When I see the summary of my cart | ||
*/ | ||
public function iSeeTheSummaryOfMyCart(): void | ||
{ | ||
$this->cartsClient->create(Request::create('orders')); | ||
} | ||
|
||
/** | ||
* @Then my cart should be empty | ||
*/ | ||
public function myCartShouldBeEmpty(): void | ||
{ | ||
$response = $this->cartsClient->getLastResponse(); | ||
Assert::true( | ||
$this->responseChecker->isCreationSuccessful($response), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering, cannot we really check is there nothing in the created cart? Of course, it needs to be created first, but the content is crucial for this step imo 🐴 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now, I would like to leave it as it is due to time constraints There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 but still should be improved 🚀 |
||
'Cart has not been created. Reason: ' . $response->getContent() | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# This file is part of the Sylius package. | ||
# (c) Paweł Jędrzejewski | ||
|
||
default: | ||
suites: | ||
api_shopping_cart: | ||
contexts: | ||
- sylius.behat.context.hook.doctrine_orm | ||
|
||
- sylius.behat.context.setup.channel | ||
|
||
- sylius.behat.context.api.shop.cart | ||
|
||
filters: | ||
tags: "@shopping_cart && @api" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?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; | ||
|
||
/** | ||
* @psalm-immutable | ||
*/ | ||
class PickupCart | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?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. | ||
*/ | ||
|
||
namespace Sylius\Bundle\ApiBundle\CommandHandler; | ||
lchrusciel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
use Doctrine\Persistence\ObjectManager; | ||
use Sylius\Bundle\ApiBundle\Command\PickupCart; | ||
use Sylius\Component\Channel\Context\ChannelContextInterface; | ||
use Sylius\Component\Core\Model\ChannelInterface; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Currency\Model\CurrencyInterface; | ||
use Sylius\Component\Locale\Model\LocaleInterface; | ||
use Sylius\Component\Resource\Factory\FactoryInterface; | ||
use Sylius\Component\Resource\Generator\RandomnessGeneratorInterface; | ||
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; | ||
|
||
final class PickupCartHandler implements MessageHandlerInterface | ||
{ | ||
/** @var FactoryInterface */ | ||
private $cartFactory; | ||
|
||
/** @var ChannelContextInterface */ | ||
private $channelContext; | ||
|
||
/** @var ObjectManager */ | ||
private $orderManager; | ||
|
||
/** @var RandomnessGeneratorInterface */ | ||
private $generator; | ||
|
||
public function __construct( | ||
FactoryInterface $cartFactory, | ||
ChannelContextInterface $channelContext, | ||
ObjectManager $orderManager, | ||
RandomnessGeneratorInterface $generator | ||
) { | ||
$this->cartFactory = $cartFactory; | ||
$this->channelContext = $channelContext; | ||
$this->orderManager = $orderManager; | ||
$this->generator = $generator; | ||
} | ||
|
||
public function __invoke(PickupCart $pickupCart) | ||
{ | ||
/** @var OrderInterface $cart */ | ||
$cart = $this->cartFactory->createNew(); | ||
|
||
/** @var ChannelInterface $channel */ | ||
$channel = $this->channelContext->getChannel(); | ||
/** @var LocaleInterface $locale */ | ||
$locale = $channel->getDefaultLocale(); | ||
/** @var CurrencyInterface $currency */ | ||
$currency = $channel->getBaseCurrency(); | ||
|
||
$cart->setChannel($channel); | ||
$cart->setLocaleCode($locale->getCode()); | ||
$cart->setCurrencyCode($currency->getCode()); | ||
$cart->setTokenValue($this->generator->generateUriSafeString(10)); | ||
|
||
$this->orderManager->persist($cart); | ||
|
||
return $cart; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How orthodox we want to be regarding command and handlers in API? 😄 I theory, the command handler should not return any value There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no other way ATM. At least without a custom data transformer at the end. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So probably we should implement a custom data transformer as fast as possible. The limits our used tool should no be an excuse for the wrong CQS pattern usage 🖖 |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason for extracting this method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to make a request without authorization, therefore the previous
create
method was no-go for me. However, the rest of the methods still needs to authorize so IMHO it is better to provide it as a part of Public API. BTW, I would like completely decouple Request creation from APIClient and I see these changes as a first steps in this direction.