Skip to content

Commit

Permalink
Add next iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomanhez committed Sep 23, 2020
1 parent c384acf commit 0045578
Show file tree
Hide file tree
Showing 17 changed files with 133 additions and 241 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Feature: Checking out as guest with a registered email
And the store allows paying offline
And there is a customer account "[email protected]"

@ui @api
@ui
Scenario: Successfully placing an order
Given I have product "PHP T-Shirt" in the cart
When I complete addressing step with email "[email protected]" and "United States" based billing address
Expand Down
10 changes: 5 additions & 5 deletions src/Sylius/Behat/Context/Api/Shop/CartContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function iRemoveProductFromTheCart(ProductInterface $product, string $tok
*/
public function iDoNotHaveAccessToSeeTheSummaryOfMyCart(): void
{
Assert::true($this->getCart()['code'] === 404);
Assert::same($this->getCart()['code'], 404);
}

/**
Expand Down Expand Up @@ -195,8 +195,8 @@ public function theVisitorHasNoAccessToCustomer(string $tokenValue): void
{
$response = $this->cartsClient->show($tokenValue);

Assert::true(
!$this->responseChecker->isShowSuccessful($response),
Assert::false(
$this->responseChecker->isShowSuccessful($response),
SprintfResponseEscaper::provideMessageWithEscapedResponseContent('Cart has not been created.', $response)
);
}
Expand Down Expand Up @@ -275,15 +275,15 @@ public function thisItemShouldHaveCode(array $item, string $variantCode): void

/**
* @Then I should see :productName with quantity :quantity in my cart
* @Then /^the (?:customer|visitor) should see (product "[^"]+") with quantity (\d+) in his cart$/
* @Then /^the (?:customer|visitor) should see product "([^"]+)" with quantity (\d+) in his cart$/
*/
public function iShouldSeeWithQuantityInMyCart(string $productName, int $quantity): void
{
$this->checkProductQuantity($this->cartsClient->getLastResponse(), $productName, $quantity);
}

/**
* @Then /^the administrator should see ("[^"]+" product) with quantity ([^"]+) in the (?:customer|visitor) cart$/
* @Then /^the administrator should see "([^"]+)" product with quantity (\d+) in the (?:customer|visitor) cart$/
*/
public function theAdministratorShouldSeeProductWithQuantityInTheCart(string $productName, int $quantity): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/Sylius/Behat/Context/Api/Shop/CheckoutContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public function iProceededWithShippingMethod(ShippingMethodInterface $shippingMe
*/
public function theVisitorHasNoProceedWithShippingMethodInTheCustomerCart(): void
{
Assert::true($this->getCart()['code'] === 404);
Assert::same($this->getCart()['code'], 404);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Doctrine\Persistence\ObjectManager;
use SM\Factory\FactoryInterface as StateMachineFactoryInterface;
use Sylius\Bundle\ApiBundle\Command\Checkout\AddressOrder;
use Sylius\Bundle\ApiBundle\Context\CartVisitorsCustomerContextInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderCheckoutTransitions;
Expand All @@ -43,23 +42,18 @@ final class AddressOrderHandler
/** @var StateMachineFactoryInterface */
private $stateMachineFactory;

/** @var CartVisitorsCustomerContextInterface */
private $cartVisitorsCustomerContext;

public function __construct(
OrderRepositoryInterface $orderRepository,
CustomerRepositoryInterface $customerRepository,
FactoryInterface $customerFactory,
ObjectManager $manager,
StateMachineFactoryInterface $stateMachineFactory,
CartVisitorsCustomerContextInterface $cartVisitorsCustomerContext
StateMachineFactoryInterface $stateMachineFactory
) {
$this->orderRepository = $orderRepository;
$this->customerRepository = $customerRepository;
$this->customerFactory = $customerFactory;
$this->manager = $manager;
$this->stateMachineFactory = $stateMachineFactory;
$this->cartVisitorsCustomerContext = $cartVisitorsCustomerContext;
}

public function __invoke(AddressOrder $addressOrder): OrderInterface
Expand All @@ -78,12 +72,7 @@ public function __invoke(AddressOrder $addressOrder): OrderInterface
);

if (null === $order->getCustomer()) {
/** @var CustomerInterface $customer */
$customer = $this->provideCustomerByEmail($addressOrder->email);

$order->setCustomer($customer);

$this->cartVisitorsCustomerContext->setCartCustomerId((string) $customer->getId());
$order->setCustomer($this->provideCustomerByEmail($addressOrder->email));
}

$order->setBillingAddress($addressOrder->billingAddress);
Expand All @@ -106,7 +95,6 @@ private function provideCustomerByEmail(?string $email): CustomerInterface
$customer = $this->customerFactory->createNew();
$customer->setEmail($email);
$this->manager->persist($customer);
$this->manager->flush();
}

return $customer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Doctrine\Persistence\ObjectManager;
use Sylius\Bundle\AdminApiBundle\Model\UserInterface;
use Sylius\Bundle\ApiBundle\Command\Cart\PickupCart;
use Sylius\Bundle\ApiBundle\Context\CartVisitorsCustomerContextInterface;
use Sylius\Bundle\ApiBundle\Context\UserContextInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
Expand Down Expand Up @@ -46,23 +45,18 @@ final class PickupCartHandler implements MessageHandlerInterface
/** @var RandomnessGeneratorInterface */
private $generator;

/** @var CartVisitorsCustomerContextInterface */
private $cartVisitorsCustomerContext;

public function __construct(
FactoryInterface $cartFactory,
ChannelContextInterface $channelContext,
UserContextInterface $userContext,
ObjectManager $orderManager,
RandomnessGeneratorInterface $generator,
CartVisitorsCustomerContextInterface $cartVisitorsCustomerContext
RandomnessGeneratorInterface $generator
) {
$this->cartFactory = $cartFactory;
$this->channelContext = $channelContext;
$this->userContext = $userContext;
$this->orderManager = $orderManager;
$this->generator = $generator;
$this->cartVisitorsCustomerContext = $cartVisitorsCustomerContext;
}

public function __invoke(PickupCart $pickupCart)
Expand All @@ -83,8 +77,6 @@ public function __invoke(PickupCart $pickupCart)
$cart->setCustomer($customer);
}

$this->cartVisitorsCustomerContext->setCartCustomerId(null);

$cart->setChannel($channel);
$cart->setLocaleCode($locale->getCode());
$cart->setCurrencyCode($currency->getCode());
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use Doctrine\ORM\QueryBuilder;
use Sylius\Bundle\ApiBundle\Context\CartVisitorsCustomerContextInterface;
use Sylius\Bundle\ApiBundle\Context\UserContextInterface;
use Sylius\Bundle\ApiBundle\Doctrine\ApiShopRequestMethodsTypes;
use Sylius\Bundle\ApiBundle\Serializer\ContextKeys;
use Sylius\Component\Core\Model\AdminUserInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Security\Core\User\UserInterface;

Expand All @@ -35,15 +31,9 @@ final class OrderGetMethodItemExtension implements QueryItemExtensionInterface
/** @var UserContextInterface */
private $userContext;

/** @var CartVisitorsCustomerContextInterface */
private $cartVisitorsCustomerContext;

public function __construct(
UserContextInterface $userContext,
CartVisitorsCustomerContextInterface $cartVisitorsCustomerContext
) {
public function __construct(UserContextInterface $userContext)
{
$this->userContext = $userContext;
$this->cartVisitorsCustomerContext = $cartVisitorsCustomerContext;
}

public function applyToItem(
Expand Down Expand Up @@ -73,19 +63,12 @@ private function applyToItemForGetMethod(
QueryBuilder $queryBuilder,
string $rootAlias
): void {
/** @var string|null $cartCustomerId */
$cartCustomerId = $this->cartVisitorsCustomerContext->getCartCustomerId();

if ($user === null && $cartCustomerId === null) {
$queryBuilder->andWhere(sprintf('%s.customer IS NULL', $rootAlias));

return;
}

if ($user === null && $cartCustomerId !== null) {
if ($user === null) {
$queryBuilder
->andWhere(sprintf('%s.customer = :customer', $rootAlias))
->setParameter('customer', $cartCustomerId)
->leftJoin(sprintf('%s.customer', $rootAlias), 'customer')
->leftJoin('customer.user', 'user')
->andWhere('user IS NULL')
->orWhere(sprintf('%s.customer IS NULL', $rootAlias))
;

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use Doctrine\ORM\QueryBuilder;
use Sylius\Bundle\ApiBundle\Context\CartVisitorsCustomerContextInterface;
use Sylius\Bundle\ApiBundle\Context\UserContextInterface;
use Sylius\Bundle\ApiBundle\Serializer\ContextKeys;
use Sylius\Component\Core\Model\AdminUserInterface;
Expand All @@ -32,15 +31,9 @@ final class OrderMethodsItemExtension implements QueryItemExtensionInterface
/** @var UserContextInterface */
private $userContext;

/** @var CartVisitorsCustomerContextInterface */
private $cartVisitorsCustomerContext;

public function __construct(
UserContextInterface $userContext,
CartVisitorsCustomerContextInterface $cartVisitorsCustomerContext
) {
public function __construct(UserContextInterface $userContext)
{
$this->userContext = $userContext;
$this->cartVisitorsCustomerContext = $cartVisitorsCustomerContext;
}

public function applyToItem(
Expand Down Expand Up @@ -73,23 +66,12 @@ private function applyUserRulesToItem(
string $rootAlias,
string $httpRequestMethodType
): void {
/** @var string|null $cartCustomerId */
$cartCustomerId = $this->cartVisitorsCustomerContext->getCartCustomerId();

if ($user === null && $cartCustomerId === null) {
$queryBuilder
->andWhere(sprintf('%s.customer IS NULL', $rootAlias))
->andWhere(sprintf('%s.state = :state', $rootAlias))
->setParameter('state', OrderInterface::STATE_CART)
;

return;
}

if ($user === null && $cartCustomerId !== null) {
if ($user === null) {
$queryBuilder
->andWhere(sprintf('%s.customer = :customer', $rootAlias))
->setParameter('customer', $cartCustomerId)
->leftJoin(sprintf('%s.customer', $rootAlias), 'customer')
->leftJoin('customer.user', 'user')
->andWhere('user IS NULL')
->orWhere(sprintf('%s.customer IS NULL', $rootAlias))
->andWhere(sprintf('%s.state = :state', $rootAlias))
->setParameter('state', OrderInterface::STATE_CART)
;
Expand All @@ -108,9 +90,11 @@ private function applyUserRulesToItem(
return;
}

if ($user instanceof AdminUserInterface &&
if (
$user instanceof AdminUserInterface &&
in_array('ROLE_API_ACCESS', $user->getRoles(), true) &&
$httpRequestMethodType === Request::METHOD_DELETE) {
$httpRequestMethodType === Request::METHOD_DELETE
) {
$queryBuilder
->andWhere(sprintf('%s.state = :state', $rootAlias))
->setParameter('state', OrderInterface::STATE_CART)
Expand All @@ -119,9 +103,11 @@ private function applyUserRulesToItem(
return;
}

if ($user instanceof AdminUserInterface &&
if (
$user instanceof AdminUserInterface &&
in_array('ROLE_API_ACCESS', $user->getRoles(), true) &&
$httpRequestMethodType !== Request::METHOD_DELETE) {
$httpRequestMethodType !== Request::METHOD_DELETE
) {
//admin has also access to modified orders in states other than cart

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
<argument type="service" id="sylius.api.context.user" />
<argument type="service" id="sylius.manager.order" />
<argument type="service" id="sylius.random_generator" />
<argument type="service" id="sylius.api.context.cart.visitors.customer" />
<tag name="messenger.message_handler" />
</service>

Expand All @@ -56,7 +55,6 @@
<argument type="service" id="sylius.factory.customer"/>
<argument type="service" id="sylius.manager.customer"/>
<argument type="service" id="sm.factory" />
<argument type="service" id="sylius.api.context.cart.visitors.customer" />
<tag name="messenger.message_handler" />
</service>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,5 @@
<service id="sylius.api.context.user" class="Sylius\Bundle\ApiBundle\Context\TokenBasedUserContext">
<argument type="service" id="security.token_storage" />
</service>

<service id="sylius.api.context.cart.visitors.customer" class="Sylius\Bundle\ApiBundle\Context\CartVisitorsCustomerContext">
<argument type="service" id="session"/>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
public="true"
>
<argument type="service" id="sylius.api.context.user" />
<argument type="service" id="sylius.api.context.cart.visitors.customer" />
<tag name="api_platform.doctrine.orm.query_extension.item" />
</service>

Expand All @@ -50,7 +49,6 @@
public="true"
>
<argument type="service" id="sylius.api.context.user" />
<argument type="service" id="sylius.api.context.cart.visitors.customer" />
<tag name="api_platform.doctrine.orm.query_extension.item" />
</service>
</services>
Expand Down
Loading

0 comments on commit 0045578

Please sign in to comment.