diff --git a/features/shop/cart/shopping_cart/changing_quantity_of_product_in_cart.feature b/features/shop/cart/shopping_cart/changing_quantity_of_product_in_cart.feature index e377e799e6..29306228cb 100644 --- a/features/shop/cart/shopping_cart/changing_quantity_of_product_in_cart.feature +++ b/features/shop/cart/shopping_cart/changing_quantity_of_product_in_cart.feature @@ -16,7 +16,7 @@ Feature: Changing quantity of a product in cart Then I should see "T-Shirt banana" with quantity 2 in my cart And my cart items total should be "$25.08" - @todo-api @ui @javascript + @api @ui @javascript Scenario: Increasing quantity of an item in cart beyond the threshold Given I see the summary of my cart When I change product "T-Shirt banana" quantity to 20000 in my cart diff --git a/src/Sylius/Behat/Context/Api/Shop/CartContext.php b/src/Sylius/Behat/Context/Api/Shop/CartContext.php index f581372f3b..ea174d2a04 100644 --- a/src/Sylius/Behat/Context/Api/Shop/CartContext.php +++ b/src/Sylius/Behat/Context/Api/Shop/CartContext.php @@ -291,7 +291,7 @@ final class CartContext implements Context */ public function iShouldBeNotifiedThatTheQuantityOfThisProductMustBeBetween(ProductInterface $product): void { - Assert::false($this->responseChecker->hasViolationWithMessage( + Assert::true($this->responseChecker->hasViolationWithMessage( $this->shopClient->getLastResponse(), 'Quantity must be between 1 and 9999.', )); diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/validation/ChangeItemQuantityInCart.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/validation/ChangeItemQuantityInCart.xml index e3b4dacc80..2fa44a4dd4 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/validation/ChangeItemQuantityInCart.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/validation/ChangeItemQuantityInCart.xml @@ -19,10 +19,11 @@ - + - - + diff --git a/src/Sylius/Bundle/CoreBundle/Resources/config/services/validators.xml b/src/Sylius/Bundle/CoreBundle/Resources/config/services/validators.xml index 7072517450..4021576ff4 100644 --- a/src/Sylius/Bundle/CoreBundle/Resources/config/services/validators.xml +++ b/src/Sylius/Bundle/CoreBundle/Resources/config/services/validators.xml @@ -136,5 +136,11 @@ + + + + %sylius.order_item_quantity_modifier.limit% + + diff --git a/src/Sylius/Bundle/CoreBundle/Resources/config/validation/OrderItem.xml b/src/Sylius/Bundle/CoreBundle/Resources/config/validation/OrderItem.xml index 694e0734b9..542df2023a 100644 --- a/src/Sylius/Bundle/CoreBundle/Resources/config/validation/OrderItem.xml +++ b/src/Sylius/Bundle/CoreBundle/Resources/config/validation/OrderItem.xml @@ -27,5 +27,13 @@ sylius_checkout_complete + + + + + + diff --git a/src/Sylius/Bundle/CoreBundle/Validator/Constraints/CartItemQuantityRange.php b/src/Sylius/Bundle/CoreBundle/Validator/Constraints/CartItemQuantityRange.php new file mode 100644 index 0000000000..08d472f0bf --- /dev/null +++ b/src/Sylius/Bundle/CoreBundle/Validator/Constraints/CartItemQuantityRange.php @@ -0,0 +1,32 @@ +max = $this->orderItemQuantityModifierLimit; + + parent::validate($value, $constraint); + } +} diff --git a/src/Sylius/Bundle/CoreBundle/spec/Validator/Constraints/CartItemQuantityRangeValidatorSpec.php b/src/Sylius/Bundle/CoreBundle/spec/Validator/Constraints/CartItemQuantityRangeValidatorSpec.php new file mode 100644 index 0000000000..8006f64965 --- /dev/null +++ b/src/Sylius/Bundle/CoreBundle/spec/Validator/Constraints/CartItemQuantityRangeValidatorSpec.php @@ -0,0 +1,79 @@ +beConstructedWith($propertyAccessor, 17); + + $this->initialize($context); + } + + function it_throws_an_exception_if_constraint_is_not_an_instance_of_cart_item_quantity_range( + Constraint $constraint, + ): void { + $this + ->shouldThrow(UnexpectedTypeException::class) + ->during('validate', [9, $constraint]) + ; + } + + function it_does_nothing_if_value_is_empty( + ExecutionContextInterface $context, + ): void { + $this->validate(null, new CartItemQuantityRange(min: 1)); + + $context->buildViolation(self::MESSAGE)->shouldNotHaveBeenCalled(); + } + + function it_does_nothing_if_value_is_in_range( + RepositoryInterface $countryRepository, + ExecutionContextInterface $context, + CountryInterface $country, + ): void { + $this->validate(5, new CartItemQuantityRange(min: 1)); + + $context->buildViolation(self::MESSAGE)->shouldNotHaveBeenCalled(); + } + + function it_adds_a_violation_if_value_is_not_in_range( + ExecutionContextInterface $context, + ConstraintViolationBuilderInterface $constraintViolationBuilder, + ): void { + $constraintViolationBuilder->addViolation()->shouldBeCalled(); + $constraintViolationBuilder->setCode(CartItemQuantityRange::NOT_IN_RANGE_ERROR)->willReturn($constraintViolationBuilder); + $constraintViolationBuilder->atPath(Argument::any())->willReturn($constraintViolationBuilder); + $constraintViolationBuilder->setParameter(Argument::cetera())->willReturn($constraintViolationBuilder); + + $context->buildViolation(self::MESSAGE)->shouldBeCalled()->willReturn($constraintViolationBuilder); + + $this->validate(18, new CartItemQuantityRange(min: 1)); + } +}