fix tests

This commit is contained in:
Joel Male
2024-09-04 09:16:15 +10:00
parent 7ee26add8d
commit ab9eb6c873
2 changed files with 29 additions and 3 deletions
+27 -1
View File
@@ -2,6 +2,7 @@
namespace Wearepixel\Cart; namespace Wearepixel\Cart;
use Wearepixel\Cart\Exceptions\InvalidConditionException;
use Wearepixel\Cart\Helpers\Helpers; use Wearepixel\Cart\Helpers\Helpers;
use Wearepixel\Cart\Validators\CartItemValidator; use Wearepixel\Cart\Validators\CartItemValidator;
use Wearepixel\Cart\Exceptions\InvalidItemException; use Wearepixel\Cart\Exceptions\InvalidItemException;
@@ -296,6 +297,11 @@ public function clear()
/** /**
* Adds a condition to the cart * Adds a condition to the cart
*/ */
/**
* Adds a condition to the cart
*
* @throws InvalidConditionException
*/
public function condition(array|CartCondition $condition): self public function condition(array|CartCondition $condition): self
{ {
if (is_array($condition)) { if (is_array($condition)) {
@@ -306,7 +312,27 @@ public function condition(array|CartCondition $condition): self
return $this; return $this;
} }
return $this->condition($condition); if (! $condition instanceof CartCondition) {
throw new InvalidConditionException('Argument 1 must be an instance of \'Wearepixel\Cart\CartCondition\'');
}
$conditions = $this->getConditions();
// Check if order has been applied
if ($condition->getOrder() == 0) {
$last = $conditions->last();
$condition->setOrder(! is_null($last) ? $last->getOrder() + 1 : 1);
}
$conditions->put($condition->getName(), $condition);
$conditions = $conditions->sortBy(function ($condition, $key) {
return $condition->getOrder();
});
$this->saveConditions($conditions);
return $this;
} }
/** /**
+2 -2
View File
@@ -1077,13 +1077,13 @@
$this->cart->add($item); $this->cart->add($item);
$this->cart->addConditions([$shipping, $tax]); $this->cart->condition([$shipping, $tax]);
// let's prove first we have now two conditions in the cart // let's prove first we have now two conditions in the cart
expect($this->cart->getConditions()->count())->toEqual(2, 'Cart should have two conditions'); expect($this->cart->getConditions()->count())->toEqual(2, 'Cart should have two conditions');
// now let's remove a specific condition by condition name // now let's remove a specific condition by condition name
$this->cart->removeCondition('Tax'); $this->cart->removeCartCondition('Tax');
// cart should have now only 1 condition // cart should have now only 1 condition
expect($this->cart->getConditions()->count())->toEqual(1, 'Cart should have one condition'); expect($this->cart->getConditions()->count())->toEqual(1, 'Cart should have one condition');