Skip to content

Commit

Permalink
Fix for validator names
Browse files Browse the repository at this point in the history
  • Loading branch information
Nenad Stefanovic authored and Nenad Stefanovic committed Nov 24, 2020
1 parent ec87c8c commit 4f51cf3
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 122 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,3 @@

.phpunit.result.cache
/phpunit.xml

/.idea/
/docker/
Original file line number Diff line number Diff line change
Expand Up @@ -4,90 +4,119 @@

namespace spec\Sylius\ShopApiPlugin\Validator\Cart;

use ArrayIterator;
use Doctrine\Common\Collections\ArrayCollection;
use PhpSpec\ObjectBehavior;
use Prophecy\Promise\ReturnPromise;
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 Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\ShopApiPlugin\Validator\Constraints\CartEligibility;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

final class CartCheckoutItemEligibilityValidatorSpec extends ObjectBehavior
final class CartEligibilityValidatorSpec extends ObjectBehavior
{
function let(
RepositoryInterface $repository,
OrderRepositoryInterface $repository,
ExecutionContextInterface $context
): void {
$this->beConstructedWith($repository);
$this->initialize($context);
}

function it_add_no_violation_if_cart_has_eligible_item(
RepositoryInterface $repository,
OrderRepositoryInterface $repository,
OrderInterface $order,
OrderItemInterface $orderItem,
ProductVariantInterface $productVariant,
ProductInterface $product,
ArrayCollection $collection,
ArrayIterator $arrayIterator,
ExecutionContextInterface $context
): void {
$repository->findOneBy(['tokenValue' => 'CART_TOKEN'])->willReturn($order);
$repository->findOneBy(['tokenValue' => 'CART_TOKEN', 'state' => OrderInterface::STATE_CART])->willReturn($order);

$arrayIterator->valid()->will(new ReturnPromise(array_merge(array_fill(0, count([$orderItem]), true), [false])));
$arrayIterator->current()->will(new ReturnPromise([$orderItem]));
$arrayIterator->count()->willReturn(count([$orderItem]));
$arrayIterator->next()->willReturn();
$arrayIterator->rewind()->willReturn();

$order->getItems()->willReturn($collection);
$collection->toArray()->willReturn([$orderItem]);
$collection->getIterator()->willReturn($arrayIterator);

$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());
$this->validate('CART_TOKEN', new CartEligibility());
}

function it_add_violation_if_cart_has_non_eligible_item_variant(
RepositoryInterface $repository,
OrderRepositoryInterface $repository,
OrderInterface $order,
OrderItemInterface $orderItem,
ProductVariantInterface $productVariant,
ArrayCollection $collection,
ArrayIterator $arrayIterator,
ExecutionContextInterface $context
): void {
$repository->findOneBy(['tokenValue' => 'CART_TOKEN'])->willReturn($order);
$repository->findOneBy(['tokenValue' => 'CART_TOKEN', 'state' => OrderInterface::STATE_CART])->willReturn($order);

$arrayIterator->valid()->will(new ReturnPromise(array_merge(array_fill(0, count([$orderItem]), true), [false])));
$arrayIterator->current()->will(new ReturnPromise([$orderItem]));
$arrayIterator->count()->willReturn(count([$orderItem]));
$arrayIterator->next()->willReturn();
$arrayIterator->rewind()->willReturn();

$order->getItems()->willReturn($collection);
$collection->toArray()->willReturn([$orderItem]);
$collection->getIterator()->willReturn($arrayIterator);

$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());
$this->validate('CART_TOKEN', new CartEligibility());
}

function it_add_violation_if_cart_has_non_eligible_item(
RepositoryInterface $repository,
OrderRepositoryInterface $repository,
OrderInterface $order,
OrderItemInterface $orderItem,
ProductVariantInterface $productVariant,
ProductInterface $product,
ArrayCollection $collection,
ArrayIterator $arrayIterator,
ExecutionContextInterface $context
): void {
$repository->findOneBy(['tokenValue' => 'CART_TOKEN'])->willReturn($order);
$repository->findOneBy(['tokenValue' => 'CART_TOKEN', 'state' => OrderInterface::STATE_CART])->willReturn($order);

$arrayIterator->valid()->will(new ReturnPromise(array_merge(array_fill(0, count([$orderItem]), true), [false])));
$arrayIterator->current()->will(new ReturnPromise([$orderItem]));
$arrayIterator->count()->willReturn(count([$orderItem]));
$arrayIterator->next()->willReturn();
$arrayIterator->rewind()->willReturn();

$order->getItems()->willReturn($collection);
$collection->toArray()->willReturn([$orderItem]);
$orderItem->getVariant()->willReturn($productVariant);
$productVariant->isEnabled()->willReturn(true);
$collection->getIterator()->willReturn($arrayIterator);

$orderItem->getVariant()->willReturn($productVariant);
$productVariant->getProduct()->willReturn($product);

$productVariant->isEnabled()->willReturn(true);
$product->isEnabled()->willReturn(false);

$context->addViolation('sylius.shop_api.checkout.cart_item.non_eligible')->shouldBeCalled();

$this->validate('CART_TOKEN', new CartCheckoutItemEligibility());
$this->validate('CART_TOKEN', new CartEligibility());
}
}
23 changes: 23 additions & 0 deletions spec/Validator/Cart/CartItemEligibilityValidatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,30 @@ function it_adds_constraint_if_order_item_variant_is_not_eligible(
$orderItem->getVariant()->willReturn($productVariant);
$productVariant->isEnabled()->willReturn(false);

$productVariant->getProduct()->willReturn($product);
$product->isEnabled()->willReturn(true);

$executionContext->addViolation('sylius.shop_api.cart_item.product_variant.non_eligible')->shouldBeCalled();

$this->validate(1, new CartItemEligibility());
}

function it_adds_constraint_if_order_item_and_order_item_variant_are_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);

$productVariant->getProduct()->willReturn($product);
$product->isEnabled()->willReturn(false);

$executionContext->addViolation('sylius.shop_api.cart_item.product_variant.non_eligible')->shouldBeCalled();
$executionContext->addViolation('sylius.shop_api.cart_item.product.non_eligible')->shouldBeCalled();

$this->validate(1, new CartItemEligibility());
}
Expand Down
6 changes: 3 additions & 3 deletions src/Resources/config/services/validators/cart.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@
<tag name="validator.constraint_validator" alias="sylius_shop_api_cart_item_eligibility_validator" />
</service>

<service id="sylius.shop_api_plugin.validator.cart.cart_checkout_item_eligibility_validator"
class="Sylius\ShopApiPlugin\Validator\Cart\CartCheckoutItemEligibilityValidator">
<service id="sylius.shop_api_plugin.validator.cart.cart_eligibility_validator"
class="Sylius\ShopApiPlugin\Validator\Cart\CartEligibilityValidator">
<argument type="service" id="sylius.repository.order" />
<tag name="validator.constraint_validator" alias="sylius_shop_api_cart_checkout_item_eligibility_validator" />
<tag name="validator.constraint_validator" alias="sylius_shop_api_cart_eligibility_validator" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</constraint>
<constraint name="Sylius\ShopApiPlugin\Validator\Constraints\CartExists" />
<constraint name="Sylius\ShopApiPlugin\Validator\Constraints\CartReadyForCheckout" />
<constraint name="Sylius\ShopApiPlugin\Validator\Constraints\CartCheckoutItemEligibility" />
<constraint name="Sylius\ShopApiPlugin\Validator\Constraints\CartEligibility" />
</property>
</class>
</constraint-mapping>
62 changes: 0 additions & 62 deletions src/Validator/Cart/CartCheckoutItemEligibilityValidator.php

This file was deleted.

84 changes: 84 additions & 0 deletions src/Validator/Cart/CartEligibilityValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?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\Core\Repository\OrderRepositoryInterface;
use Sylius\ShopApiPlugin\Validator\Constraints\CartEligibility;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Webmozart\Assert\Assert;

final class CartEligibilityValidator extends ConstraintValidator
{
/**
* @var OrderRepositoryInterface
*/
private $_cartRepository;

/**
* CartEligibilityValidator constructor.
*
* @param OrderRepositoryInterface $cartRepository
*/
public function __construct(OrderRepositoryInterface $cartRepository)
{
$this->_cartRepository = $cartRepository;
}

/**
* @param mixed $token
* @param Constraint $constraint
*
* @return void
*/
public function validate($token, Constraint $constraint): void
{
Assert::isInstanceOf($constraint, CartEligibility::class);

$cart = $this->_cartRepository->findOneBy(
[
'tokenValue' => $token,
'state' => OrderInterface::STATE_CART
]
);

if ($cart === null) {
return;
}

Assert::isInstanceOf($cart, OrderInterface::class);

$cartItems = $cart->getItems();
foreach ($cartItems as $item) {
Assert::isInstanceOf($item, OrderItemInterface::class);

$variant = $item->getVariant();

Assert::isInstanceOf($variant, ProductVariantInterface::class);
if (!$variant->isEnabled()) {
$this->context->addViolation(
$constraint->nonEligibleCartItemVariantMessage
);

break;
}

$product = $variant->getProduct();

Assert::isInstanceOf($product, ProductInterface::class);
if (!$product->isEnabled()) {
$this->context->addViolation(
$constraint->nonEligibleCartItemMessage
);

break;
}
}
}
}
Loading

0 comments on commit 4f51cf3

Please sign in to comment.