Skip to content

Commit

Permalink
Merge pull request #5845 from GSadee/remove-item-from-cart
Browse files Browse the repository at this point in the history
[Cart] Refactor removing cart item from cart
  • Loading branch information
michalmarcinkowski committed Aug 31, 2016
2 parents 7c7881b + 625a526 commit 78efcec
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 4 deletions.
19 changes: 17 additions & 2 deletions features/cart/shopping_cart/removing_cart_item.feature
Expand Up @@ -7,10 +7,25 @@ Feature: Removing cart item from cart
Background:
Given the store operates on a single channel in "United States"
And the store has a product "T-shirt banana" priced at "$12.54"
And I add this product to the cart
And I added product "T-shirt banana" to the cart

@ui
Scenario: Removing cart item
Given I see the summary of my cart
When I see the summary of my cart
And I remove product "T-shirt banana" from the cart
Then my cart should be empty
And my cart's total should be "$0.00"

@ui
Scenario: Removing cart item when the store has defined default shipping method
Given the store has "UPS" shipping method with "$20.00" fee
When I remove product "T-shirt banana" from the cart
Then my cart should be empty
And my cart's total should be "$0.00"

@ui
Scenario: Checking cart's total after removing one item
Given the store has a product "T-shirt strawberry" priced at "$17.22"
And I add this product to the cart
When I remove product "T-shirt banana" from the cart
Then my cart's total should be "$17.22"
14 changes: 13 additions & 1 deletion src/Sylius/Behat/Context/Ui/Shop/CartContext.php
Expand Up @@ -252,7 +252,7 @@ public function productPriceShouldNotBeDecreased(ProductInterface $product)
}

/**
* @Given /^I add (this product) to the cart$/
* @Given /^I (?:add|added) (this product) to the cart$/
* @Given I added product :product to the cart
* @Given /^I (?:have|had) (product "[^"]+") in the cart$/
* @When I add product :product to the cart
Expand Down Expand Up @@ -480,4 +480,16 @@ public function iShouldNotBeNotifiedThatThisProductCannotBeUpdated(ProductInterf
sprintf('I should see validation message for %s product', $product->getName())
);
}

/**
* @Then my cart's total should be :total
*/
public function myCartSTotalShouldBe($total)
{
Assert::same(
$total,
$this->summaryPage->getCartTotal(),
'Cart should have %s total, but it has %2$s.'
);
}
}
15 changes: 15 additions & 0 deletions src/Sylius/Behat/Page/Shop/Cart/SummaryPage.php
Expand Up @@ -212,6 +212,20 @@ public function getQuantity($productName)
return (int) $itemElement->find('css', 'input[type=number]')->getValue();
}

/**
* {@inheritdoc}
*/
public function getCartTotal()
{
$cartTotalText = $this->getElement('cart_total')->getText();

if (strpos($cartTotalText, ',') !== false ) {
return strstr($cartTotalText, ',', true);
}

return $cartTotalText;
}

public function clearCart()
{
$this->getElement('clear_button')->click();
Expand All @@ -230,6 +244,7 @@ protected function getDefinedElements()
return array_merge(parent::getDefinedElements(), [
'apply_coupon_button' => 'button:contains("Apply coupon")',
'cart_items' => '#sylius-cart-items',
'cart_total' => '#sylius-cart-button',
'clear_button' => '#sylius-cart-clear',
'coupon_field' => '#sylius_cart_promotionCoupon',
'grand_total' => '#sylius-cart-grand-total',
Expand Down
5 changes: 5 additions & 0 deletions src/Sylius/Behat/Page/Shop/Cart/SummaryPageInterface.php
Expand Up @@ -131,6 +131,11 @@ public function isEmpty();
*/
public function getQuantity($productName);

/**
* @return string
*/
public function getCartTotal();

public function clearCart();

public function updateCart();
Expand Down
Expand Up @@ -16,6 +16,7 @@ default:
- sylius.behat.context.setup.shop_security
- sylius.behat.context.setup.channel
- sylius.behat.context.setup.product
- sylius.behat.context.setup.shipping
- sylius.behat.context.setup.user

- sylius.behat.context.ui.channel
Expand Down
@@ -1,6 +1,6 @@
{% set cart = sylius_cart_get() %}

<div class="ui circular cart button">
<div id="sylius-cart-button" class="ui circular cart button">
<i class="cart icon"></i> {{ cart.total|sylius_price }}{% if not cart.empty %}, {{ cart.items|length }} {{ 'sylius.ui.items'|trans|lower }}{% endif %}
</div>
<div class="ui large flowing cart hidden popup">
Expand Down
8 changes: 8 additions & 0 deletions src/Sylius/Component/Core/Model/Order.php
Expand Up @@ -343,6 +343,14 @@ public function removeShipment(ShipmentInterface $shipment)
}
}

/**
* {@inheritdoc}
*/
public function removeShipments()
{
$this->shipments->clear();
}

/**
* {@inheritdoc}
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Sylius/Component/Core/Model/OrderInterface.php
Expand Up @@ -108,6 +108,8 @@ public function addShipment(ShipmentInterface $shipment);
*/
public function removeShipment(ShipmentInterface $shipment);

public function removeShipments();

/**
* @param ShipmentInterface $shipment
*
Expand Down
Expand Up @@ -49,6 +49,12 @@ public function __construct(
*/
public function process(OrderInterface $order)
{
if ($order->isEmpty()) {
$order->removeShipments();

return;
}

$shipment = $this->getOrderShipment($order);

if (null === $shipment) {
Expand Down
10 changes: 10 additions & 0 deletions src/Sylius/Component/Core/spec/Model/OrderSpec.php
Expand Up @@ -134,6 +134,16 @@ function it_should_remove_shipment_properly(ShipmentInterface $shipment)
$this->shouldNotHaveShipment($shipment);
}

function it_removes_shipments(ShipmentInterface $shipment)
{
$this->addShipment($shipment);
$this->hasShipment($shipment)->shouldReturn(true);

$this->removeShipments();

$this->hasShipment($shipment)->shouldReturn(false);
}

function it_should_return_shipping_adjustments(
AdjustmentInterface $shippingAdjustment,
AdjustmentInterface $taxAdjustment
Expand Down
Expand Up @@ -59,6 +59,7 @@ function it_creates_a_single_shipment_with_default_shipping_method_and_assigns_a

$shipmentFactory->createNew()->willReturn($shipment);

$order->isEmpty()->willReturn(false);
$order->hasShipments()->willReturn(false);
$order->getItemUnits()->willReturn([$itemUnit1, $itemUnit2]);

Expand All @@ -81,6 +82,7 @@ function it_adds_new_item_units_to_existing_shipment(
) {
$shipments->first()->willReturn($shipment);

$order->isEmpty()->willReturn(false);
$order->hasShipments()->willReturn(true);
$order->getItemUnits()->willReturn([$itemUnit, $itemUnitWithoutShipment]);
$order->getShipments()->willReturn($shipments);
Expand Down

0 comments on commit 78efcec

Please sign in to comment.