Skip to content

Commit

Permalink
[Api] Add validation to add to cart command
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKasp committed Oct 5, 2020
1 parent ba82588 commit a7c9bf7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
7 changes: 7 additions & 0 deletions features/cart/shopping_cart/adding_product_to_cart.feature
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ Feature: Adding a simple product to the cart
And I should be notified that the product has been successfully added
And there should be one item in my cart
And this item should have name "Oathkeeper"

@api
Scenario: Preventing adding to cart item with 0 quantity
Given the store has a product "T-shirt banana" priced at "$12.54"
When I try to add 0 products "T-shirt banana" to the cart
Then I should be notified that quantity of added product should be greater than 0
And there should be 0 item in my cart
24 changes: 24 additions & 0 deletions src/Sylius/Behat/Context/Api/Shop/CartContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public function iAddThisProductToTheCart(ProductInterface $product, string $toke
/**
* @When /^I add (\d+) of (them) to (?:the|my) (cart)$/
* @When /^I add (\d+) (products "[^"]+") to the (cart)$/
* @When /^I try to add (\d+) (products "[^"]+") to the (cart)$/
*/
public function iAddOfThemToMyCart(int $quantity, ProductInterface $product, string $tokenValue): void
{
Expand Down Expand Up @@ -221,6 +222,18 @@ public function iShouldBeNotifiedThatTheProductHasBeenSuccessfullyAdded(): void
);
}

/**
* @Then I should be notified that quantity of added product should be greater than 0
*/
public function iShouldBeNotifiedThatQuantityOfAddedProductShouldBeMoreThan0(): void
{
$response = $this->cartsClient->getLastResponse();
Assert::false(
$this->responseChecker->isUpdateSuccessful($response),
SprintfResponseEscaper::provideMessageWithEscapedResponseContent('Quantity of added item should by grater than 0', $response)
);
}

/**
* @Then there should be one item in my cart
*/
Expand All @@ -234,6 +247,17 @@ public function thereShouldBeOneItemInMyCart(): void
$this->sharedStorage->set('item', $items[0]);
}

/**
* @Then /^there should be (\d+) item in my (cart)$/
*/
public function thereShouldCountItemsInMyCart(int $count, string $cartToken): void
{
$response = $this->cartsClient->show($cartToken);
$items = $this->responseChecker->getValue($response, 'items');

Assert::count($items, $count);
}

/**
* @Then /^(this item) should have name "([^"]+)"$/
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
This file is part of the Sylius package.
(c) Paweł Jędrzejewski
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
-->

<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Sylius\Bundle\ApiBundle\Command\Cart\AddItemToCart">
<property name="quantity">
<constraint name="NotNull" />
<constraint name="Positive">
<option name="message">sylius.cart_item.greater_than_0</option>
<option name="groups">
<value>sylius</value>
</option>
</constraint>
</property>
</class>
</constraint-mapping>
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ sylius:
not_valid: The currency code you entered is invalid.
cart_item:
not_available: '%itemName% does not have sufficient stock.'
greater_than_0: 'Quantity of added item should by grater than 0'
order:
currency_code:
not_valid: The currency code you entered is invalid.
Expand Down

0 comments on commit a7c9bf7

Please sign in to comment.