-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented compatibility with sylius 1.8 and added integrations test…
…s for product eligibility
- Loading branch information
Nenad Stefanovic
authored and
Nenad Stefanovic
committed
Nov 18, 2020
1 parent
775e657
commit ec87c8c
Showing
38 changed files
with
824 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ | |
/phpunit.xml | ||
|
||
/.idea/ | ||
/docker/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
spec/Validator/Cart/CartCheckoutItemEligibilityValidatorSpec.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\ShopApiPlugin\Validator\Cart; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Component\Core\Model\OrderItemInterface; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\ProductInterface; | ||
use Sylius\Component\Core\Model\ProductVariantInterface; | ||
use Sylius\Component\Resource\Repository\RepositoryInterface; | ||
use Sylius\ShopApiPlugin\Validator\Constraints\CartCheckoutItemEligibility; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
|
||
final class CartCheckoutItemEligibilityValidatorSpec extends ObjectBehavior | ||
{ | ||
function let( | ||
RepositoryInterface $repository, | ||
ExecutionContextInterface $context | ||
): void { | ||
$this->beConstructedWith($repository); | ||
$this->initialize($context); | ||
} | ||
|
||
function it_add_no_violation_if_cart_has_eligible_item( | ||
RepositoryInterface $repository, | ||
OrderInterface $order, | ||
OrderItemInterface $orderItem, | ||
ProductVariantInterface $productVariant, | ||
ProductInterface $product, | ||
ArrayCollection $collection, | ||
ExecutionContextInterface $context | ||
): void { | ||
$repository->findOneBy(['tokenValue' => 'CART_TOKEN'])->willReturn($order); | ||
$order->getItems()->willReturn($collection); | ||
$collection->toArray()->willReturn([$orderItem]); | ||
$orderItem->getVariant()->willReturn($productVariant); | ||
$productVariant->getProduct()->willReturn($product); | ||
|
||
$productVariant->isEnabled()->willReturn(true); | ||
|
||
$product->isEnabled()->willReturn(true); | ||
|
||
$context->addViolation('sylius.shop_api.checkout.cart_item.non_eligible')->shouldNotBeCalled(); | ||
$context->addViolation('sylius.shop_api.checkout.cart_item_variant.non_eligible')->shouldNotBeCalled(); | ||
|
||
$this->validate('CART_TOKEN', new CartCheckoutItemEligibility()); | ||
} | ||
|
||
function it_add_violation_if_cart_has_non_eligible_item_variant( | ||
RepositoryInterface $repository, | ||
OrderInterface $order, | ||
OrderItemInterface $orderItem, | ||
ProductVariantInterface $productVariant, | ||
ArrayCollection $collection, | ||
ExecutionContextInterface $context | ||
): void { | ||
$repository->findOneBy(['tokenValue' => 'CART_TOKEN'])->willReturn($order); | ||
$order->getItems()->willReturn($collection); | ||
$collection->toArray()->willReturn([$orderItem]); | ||
$orderItem->getVariant()->willReturn($productVariant); | ||
$productVariant->isEnabled()->willReturn(false); | ||
|
||
$context->addViolation('sylius.shop_api.checkout.cart_item_variant.non_eligible')->shouldBeCalled(); | ||
|
||
$this->validate('CART_TOKEN', new CartCheckoutItemEligibility()); | ||
} | ||
|
||
function it_add_violation_if_cart_has_non_eligible_item( | ||
RepositoryInterface $repository, | ||
OrderInterface $order, | ||
OrderItemInterface $orderItem, | ||
ProductVariantInterface $productVariant, | ||
ProductInterface $product, | ||
ArrayCollection $collection, | ||
ExecutionContextInterface $context | ||
): void { | ||
$repository->findOneBy(['tokenValue' => 'CART_TOKEN'])->willReturn($order); | ||
$order->getItems()->willReturn($collection); | ||
$collection->toArray()->willReturn([$orderItem]); | ||
$orderItem->getVariant()->willReturn($productVariant); | ||
$productVariant->isEnabled()->willReturn(true); | ||
|
||
$productVariant->getProduct()->willReturn($product); | ||
$product->isEnabled()->willReturn(false); | ||
|
||
$context->addViolation('sylius.shop_api.checkout.cart_item.non_eligible')->shouldBeCalled(); | ||
|
||
$this->validate('CART_TOKEN', new CartCheckoutItemEligibility()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\ShopApiPlugin\Validator\Cart; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Component\Core\Model\OrderItemInterface; | ||
use Sylius\Component\Core\Model\ProductVariant; | ||
use Sylius\Component\Order\Repository\OrderItemRepositoryInterface; | ||
use Sylius\Component\Core\Model\Product; | ||
use Sylius\ShopApiPlugin\Validator\Constraints\CartItemEligibility; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
|
||
final class CartItemEligibilityValidatorSpec extends ObjectBehavior | ||
{ | ||
function let(ExecutionContextInterface $executionContext, OrderItemRepositoryInterface $orderItemRepository): void | ||
{ | ||
$this->beConstructedWith($orderItemRepository); | ||
|
||
$this->initialize($executionContext); | ||
} | ||
|
||
function it_does_not_add_constraint_if_order_item_and_order_item_variant_is_eligible( | ||
OrderItemRepositoryInterface $orderItemRepository, | ||
ExecutionContextInterface $executionContext, | ||
OrderItemInterface $orderItem, | ||
ProductVariant $productVariant, | ||
Product $product | ||
): void { | ||
$orderItemRepository->find(1)->willReturn($orderItem); | ||
$orderItem->getVariant()->willReturn($productVariant); | ||
$productVariant->isEnabled()->willReturn(true); | ||
|
||
$productVariant->getProduct()->willReturn($product); | ||
$product->isEnabled()->willReturn(true); | ||
|
||
$executionContext->addViolation('sylius.shop_api.cart_item.product.non_eligible')->shouldNotBeCalled(); | ||
$executionContext->addViolation('sylius.shop_api.cart_item.product_variant.non_eligible')->shouldNotBeCalled(); | ||
|
||
$this->validate(1, new CartItemEligibility()); | ||
} | ||
|
||
function it_adds_constraint_if_order_item_is_not_eligible( | ||
OrderItemRepositoryInterface $orderItemRepository, | ||
ExecutionContextInterface $executionContext, | ||
OrderItemInterface $orderItem, | ||
ProductVariant $productVariant, | ||
Product $product | ||
): void { | ||
$orderItemRepository->find(1)->willReturn($orderItem); | ||
$orderItem->getVariant()->willReturn($productVariant); | ||
$productVariant->isEnabled()->willReturn(true); | ||
|
||
$productVariant->getProduct()->willReturn($product); | ||
$product->isEnabled()->willReturn(false); | ||
|
||
$executionContext->addViolation('sylius.shop_api.cart_item.product.non_eligible')->shouldBeCalled(); | ||
|
||
$this->validate(1, new CartItemEligibility()); | ||
} | ||
|
||
function it_adds_constraint_if_order_item_variant_is_not_eligible( | ||
OrderItemRepositoryInterface $orderItemRepository, | ||
ExecutionContextInterface $executionContext, | ||
OrderItemInterface $orderItem, | ||
ProductVariant $productVariant, | ||
Product $product | ||
): void { | ||
$orderItemRepository->find(1)->willReturn($orderItem); | ||
$orderItem->getVariant()->willReturn($productVariant); | ||
$productVariant->isEnabled()->willReturn(false); | ||
|
||
$executionContext->addViolation('sylius.shop_api.cart_item.product_variant.non_eligible')->shouldBeCalled(); | ||
|
||
$this->validate(1, new CartItemEligibility()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/Validator/Cart/CartCheckoutItemEligibilityValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\ShopApiPlugin\Validator\Cart; | ||
|
||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\OrderItemInterface; | ||
use Sylius\Component\Core\Model\ProductInterface; | ||
use Sylius\Component\Core\Model\ProductVariantInterface; | ||
use Sylius\Component\Resource\Repository\RepositoryInterface; | ||
use Sylius\ShopApiPlugin\Validator\Constraints\CartCheckoutItemEligibility; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
|
||
final class CartCheckoutItemEligibilityValidator extends ConstraintValidator | ||
{ | ||
/** @var RepositoryInterface */ | ||
private $cartRepository; | ||
|
||
public function __construct(RepositoryInterface $cartRepository) | ||
{ | ||
$this->cartRepository = $cartRepository; | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function validate($value, Constraint $constraint): void | ||
{ | ||
/** @var OrderInterface|null $cart */ | ||
$cart = $this->cartRepository->findOneBy(['tokenValue' => $value]); | ||
if ($cart === null) { | ||
return; | ||
} | ||
|
||
$cartItems = $cart->getItems()->toArray(); | ||
|
||
/** @var OrderItemInterface $item */ | ||
foreach ($cartItems as $item) { | ||
|
||
/** @var ProductVariantInterface $variant */ | ||
$variant = $item->getVariant(); | ||
|
||
if ($variant->isEnabled()) { | ||
|
||
/** @var ProductInterface $product */ | ||
$product = $variant->getProduct(); | ||
|
||
if (!$product->isEnabled()) { | ||
|
||
/** @var CartCheckoutItemEligibility $constraint */ | ||
$this->context->addViolation($constraint->messageOnNonEligibleCartItem); | ||
break; | ||
} | ||
} else { | ||
|
||
/** @var CartCheckoutItemEligibility $constraint */ | ||
$this->context->addViolation($constraint->messageOnNonEligibleCartItemVariant); | ||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\ShopApiPlugin\Validator\Cart; | ||
|
||
use Sylius\Component\Core\Model\OrderItem; | ||
use Sylius\Component\Core\Model\Product; | ||
use Sylius\Component\Core\Model\ProductVariant; | ||
use Sylius\Component\Order\Repository\OrderItemRepositoryInterface; | ||
use Sylius\ShopApiPlugin\Validator\Constraints\CartItemEligibility; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
|
||
final class CartItemEligibilityValidator extends ConstraintValidator | ||
{ | ||
/** @var OrderItemRepositoryInterface */ | ||
private $orderItemRepository; | ||
|
||
public function __construct(OrderItemRepositoryInterface $orderItemRepository) | ||
{ | ||
$this->orderItemRepository = $orderItemRepository; | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function validate($id, Constraint $constraint): void | ||
{ | ||
if (null === $id) { | ||
return; | ||
} | ||
|
||
/** @var OrderItem|null $orderItem */ | ||
$orderItem = $this->orderItemRepository->find($id); | ||
|
||
if ($orderItem) { | ||
|
||
/** @var ProductVariant $variant */ | ||
$variant = $orderItem->getVariant(); | ||
|
||
if ($variant->isEnabled()) { | ||
|
||
/** @var Product $product */ | ||
$product = $variant->getProduct(); | ||
|
||
if (!$product->isEnabled()) { | ||
|
||
/** @var CartItemEligibility $constraint */ | ||
$this->context->addViolation($constraint->messageOnNonEligible); | ||
} | ||
} else { | ||
|
||
/** @var CartItemEligibility $constraint */ | ||
$this->context->addViolation($constraint->messageOnNonEligibleVariant); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.