Skip to content

Commit

Permalink
[API][Orders] Validate adding items to the cart
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafikooo committed Jul 3, 2023
1 parent 2876f9b commit 8b1dce2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Sylius/Behat/Context/Api/Shop/CheckoutContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ public function iShouldBeInformedThatProductVariantDoesNotExist(ProductVariantIn
{
Assert::true($this->isViolationWithMessageInResponse(
$this->client->getLastResponse(),
sprintf('The product variant with %s does not exist.', $productVariant->getCode()),
sprintf('The product variant %s does not exist.', $productVariant->getCode()),
));
}

Expand All @@ -1063,7 +1063,7 @@ public function iShouldBeInformedThatProductVariantWithCodeDoesNotExist(string $
{
Assert::true($this->isViolationWithMessageInResponse(
$this->client->getLastResponse(),
sprintf('The product variant with %s does not exist.', $code),
sprintf('The product variant %s does not exist.', $code),
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@
<value>sylius</value>
</option>
</constraint>
<property name="productVariantCode">
<constraint name="NotBlank">
<option name="message">sylius.order_item.product_variant.not_blank</option>
<option name="groups">sylius</option>
</constraint>
</property>
<property name="quantity">
<constraint name="NotBlank">
<option name="message">sylius.order_item.quantity.not_blank</option>
<option name="groups">sylius</option>
</constraint>
<constraint name="Range">
<option name="min">1</option>
<option name="minMessage">sylius.order_item.quantity.min</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ sylius:
product:
not_exist: 'The product %productName% does not exist.'
product_variant:
not_exist: 'The product variant with %productVariantCode% does not exist.'
not_exist: 'The product variant %productVariantCode% does not exist.'
not_longer_available: 'The product variant with name %productVariantName% does not exist.'
not_sufficient: 'The product variant with %productVariantCode% code does not have sufficient stock.'
product_variant_with_name_not_sufficient: 'The product variant with %productVariantName% name does not have sufficient stock.'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public function validate($value, Constraint $constraint)
/** @var AddingEligibleProductVariantToCart $constraint */
Assert::isInstanceOf($constraint, AddingEligibleProductVariantToCart::class);

if ($value->productVariantCode === null) {
return;
}

/** @var ProductVariantInterface|null $productVariant */
$productVariant = $this->productVariantRepository->findOneBy(['code' => $value->productVariantCode]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Doctrine\Common\Collections\Collection;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Bundle\ApiBundle\Command\Cart\AddItemToCart;
use Sylius\Bundle\ApiBundle\Command\Checkout\CompleteOrder;
use Sylius\Bundle\ApiBundle\Validator\Constraints\AddingEligibleProductVariantToCart;
Expand Down Expand Up @@ -135,6 +136,37 @@ function it_adds_violation_if_product_variant_is_disabled(
);
}

function it_does_nothing_if_product_variant_is_not_provided(
ProductVariantRepositoryInterface $productVariantRepository,
OrderRepositoryInterface $orderRepository,
AvailabilityCheckerInterface $availabilityChecker,
AddItemToCart $addItemToCart,
ExecutionContextInterface $executionContext,
ProductInterface $product,
ProductVariantInterface $productVariant,
): void {
$this->initialize($executionContext);

$productVariantRepository->findOneBy(['code' => null])->shouldNotBeCalled();
$productVariant->getProduct()->shouldNotBeCalled();
$product->isEnabled()->shouldNotBeCalled();

$addItemToCart->getOrderTokenValue()->willReturn('TOKEN');
$orderRepository->findCartByTokenValue('TOKEN')->shouldNotBeCalled();

$availabilityChecker->isStockSufficient($productVariant, 1)->shouldNotBeCalled();

$executionContext
->addViolation(Argument::any())
->shouldNotBeCalled()
;

$this->validate(
new AddItemToCart(null, 1),
new AddingEligibleProductVariantToCart(),
);
}

function it_adds_violation_if_product_variant_stock_is_not_sufficient_and_cart_has_same_units(
ProductVariantRepositoryInterface $productVariantRepository,
OrderRepositoryInterface $orderRepository,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# This file is part of the Sylius package.
# (c) Sylius Sp. z o.o.

sylius:
order_item:
product_variant:
not_blank: The product variant has not been provided.
quantity:
min: Quantity of an order item cannot be lower than 1.
not_blank: The quantity has not been provided.

0 comments on commit 8b1dce2

Please sign in to comment.