mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 13:51:44 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
61475a7060 | ||
|
|
6604cf984d | ||
|
|
7d955d722c | ||
|
|
54d94d8d12 | ||
|
|
e49ebe02e0 | ||
|
|
28b451583d | ||
|
|
3673f360b7 | ||
|
|
f6447ad4d9 |
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "wearepixel/laravel-cart",
|
||||
"version": "1.0.3",
|
||||
"version": "1.0.6",
|
||||
"description": "A cart implementation for Laravel",
|
||||
"keywords": [
|
||||
"laravel",
|
||||
|
||||
+14
-5
@@ -153,7 +153,7 @@ public function add(
|
||||
$this->add(
|
||||
$item['id'],
|
||||
$item['name'],
|
||||
$item['price'],
|
||||
isset($item['price']) ? $item['price'] : null,
|
||||
$item['quantity'],
|
||||
Helpers::issetAndHasValueOrAssignDefault($item['attributes'], []),
|
||||
Helpers::issetAndHasValueOrAssignDefault($item['conditions'], []),
|
||||
@@ -164,7 +164,7 @@ public function add(
|
||||
$this->add(
|
||||
$id['id'],
|
||||
$id['name'],
|
||||
$id['price'],
|
||||
isset($id['price']) ? $id['price'] : null,
|
||||
$id['quantity'],
|
||||
Helpers::issetAndHasValueOrAssignDefault($id['attributes'], []),
|
||||
Helpers::issetAndHasValueOrAssignDefault($id['conditions'], []),
|
||||
@@ -698,15 +698,24 @@ protected function validate($item)
|
||||
{
|
||||
$rules = [
|
||||
'id' => 'required',
|
||||
'price' => 'required|numeric',
|
||||
'quantity' => 'required|numeric|min:0.1',
|
||||
'name' => 'required',
|
||||
'quantity' => 'required|numeric|min:0.1',
|
||||
];
|
||||
|
||||
$validator = CartItemValidator::make($item, $rules);
|
||||
|
||||
if ($validator->fails()) {
|
||||
throw new InvalidItemException($validator->messages()->first());
|
||||
if ($validator->errors()->first('id') === 'validation.required') {
|
||||
throw new InvalidItemException('The id field is required.');
|
||||
}
|
||||
|
||||
if ($validator->errors()->first('name') === 'validation.required') {
|
||||
throw new InvalidItemException('The name field is required.');
|
||||
}
|
||||
|
||||
if ($validator->errors()->first('quantity') === 'validation.min.numeric') {
|
||||
throw new InvalidItemException('The quantity must be at least 0.1.');
|
||||
}
|
||||
}
|
||||
|
||||
return $item;
|
||||
|
||||
@@ -145,12 +145,6 @@ public function getCalculatedValue($totalOrSubTotalOrPrice = null): int|float
|
||||
*/
|
||||
protected function apply($totalOrSubTotalOrPrice, $conditionValue)
|
||||
{
|
||||
if ($totalOrSubTotalOrPrice <= 0) {
|
||||
$this->parsedRawValue = 0;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// if value has a percentage sign on it, we will get first
|
||||
// its percentage then we will evaluate again if the value
|
||||
// has a minus or plus sign so we can decide what to do with the
|
||||
|
||||
@@ -7,7 +7,7 @@ class Helpers
|
||||
/**
|
||||
* Normalize prices
|
||||
*/
|
||||
public static function normalizePrice($price): float
|
||||
public static function normalizePrice($price): ?float
|
||||
{
|
||||
return (is_string($price)) ? floatval($price) : $price;
|
||||
}
|
||||
|
||||
@@ -825,6 +825,96 @@
|
||||
|
||||
expect($this->cart->getConditions()->count())->toEqual(0, 'Cart should have no conditions now');
|
||||
});
|
||||
|
||||
test('calculate the subtotal correctly when one condition makes it $0, and the other adds $10', function () {
|
||||
$this->cart->add([
|
||||
[
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 568,
|
||||
'name' => 'Sample Item 2',
|
||||
'price' => 69.25,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
],
|
||||
]);
|
||||
|
||||
expect($this->cart->getSubTotal())->toEqual(137.24, 'Cart should have sub total of 137.24');
|
||||
expect($this->cart->getTotal())->toEqual(137.24, 'Cart should have total of 137.24');
|
||||
|
||||
// add conditions
|
||||
$couponCode = new CartCondition([
|
||||
'name' => 'Coupon code',
|
||||
'type' => 'discount',
|
||||
'target' => 'subtotal',
|
||||
'order' => '1',
|
||||
'value' => '-100%',
|
||||
]);
|
||||
|
||||
$shipping = new CartCondition([
|
||||
'name' => 'Shipping',
|
||||
'type' => 'shipping',
|
||||
'target' => 'subtotal',
|
||||
'order' => '2',
|
||||
'value' => '10',
|
||||
]);
|
||||
|
||||
$this->cart->condition($couponCode);
|
||||
$this->cart->condition($shipping);
|
||||
|
||||
expect($this->cart->getSubTotal())->toEqual(10.00, 'Cart should have sub total of 10.00');
|
||||
expect($this->cart->getTotal())->toEqual(10.00, 'Cart should have a total of 10.00');
|
||||
});
|
||||
|
||||
test('calculate the subtotal correctly when one condition makes it $0 (with dollars), and the other adds $10', function () {
|
||||
$this->cart->add([
|
||||
[
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 568,
|
||||
'name' => 'Sample Item 2',
|
||||
'price' => 69.25,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
],
|
||||
]);
|
||||
|
||||
expect($this->cart->getSubTotal())->toEqual(137.24, 'Cart should have sub total of 137.24');
|
||||
expect($this->cart->getTotal())->toEqual(137.24, 'Cart should have total of 137.24');
|
||||
|
||||
// add conditions
|
||||
$couponCode = new CartCondition([
|
||||
'name' => 'Coupon code',
|
||||
'type' => 'discount',
|
||||
'target' => 'subtotal',
|
||||
'order' => '1',
|
||||
'value' => '-137.24',
|
||||
]);
|
||||
|
||||
$shipping = new CartCondition([
|
||||
'name' => 'Shipping',
|
||||
'type' => 'shipping',
|
||||
'target' => 'subtotal',
|
||||
'order' => '2',
|
||||
'value' => '10',
|
||||
]);
|
||||
|
||||
$this->cart->condition($couponCode);
|
||||
$this->cart->condition($shipping);
|
||||
|
||||
expect($this->cart->getSubTotal())->toEqual(10.00, 'Cart should have sub total of 10.00');
|
||||
expect($this->cart->getTotal())->toEqual(10.00, 'Cart should have a total of 10.00');
|
||||
});
|
||||
});
|
||||
|
||||
describe('item level conditions', function () {
|
||||
|
||||
+31
-15
@@ -49,6 +49,19 @@
|
||||
expect($this->cart->getTotal())->toEqual(0, 'Total should be 0');
|
||||
});
|
||||
|
||||
test('can add an item an array with no price', function () {
|
||||
$this->cart->add([
|
||||
'id' => 455,
|
||||
'name' => 'Sample Item',
|
||||
'quantity' => 2,
|
||||
]);
|
||||
|
||||
expect($this->cart->isEmpty())->toBeFalse('Cart should not be empty');
|
||||
expect($this->cart->getContent()->count())->toEqual(1, 'Cart content should be 1');
|
||||
expect($this->cart->getContent()->first()['id'])->toEqual(455, 'Item added has ID of 455 so first content ID should be 455');
|
||||
expect($this->cart->getTotal())->toEqual(0, 'Total should be 0');
|
||||
});
|
||||
|
||||
test('can add an item without attributes', function () {
|
||||
$item = [
|
||||
'id' => 1,
|
||||
@@ -283,6 +296,24 @@
|
||||
|
||||
expect($item['quantity'])->toEqual(9.9, 'Item quantity should be 9.9');
|
||||
});
|
||||
|
||||
test('should throw an exception when no quantity is provided', function () {
|
||||
$this->expectException('Wearepixel\Cart\Exceptions\InvalidItemException');
|
||||
$this->expectExceptionMessage('The quantity must be at least 0.1.');
|
||||
$this->cart->add(455, 'Sample Item', 100.99, 0, []);
|
||||
});
|
||||
|
||||
test('should throw an exception when no id is provided', function () {
|
||||
$this->expectException('Wearepixel\Cart\Exceptions\InvalidItemException');
|
||||
$this->expectExceptionMessage('The id field is required.');
|
||||
$this->cart->add('', 'Sample Item', 100.99, 2, []);
|
||||
});
|
||||
|
||||
test('should throw an exception when no name is provided', function () {
|
||||
$this->expectException('Wearepixel\Cart\Exceptions\InvalidItemException');
|
||||
$this->expectExceptionMessage('The name field is required.');
|
||||
$this->cart->add(523, '', 100.99, 2, []);
|
||||
});
|
||||
});
|
||||
|
||||
test('cart items attributes', function () {
|
||||
@@ -542,21 +573,6 @@
|
||||
expect($item['quantity'])->toEqual(3, 'Item quantity of with item ID of 456 should now be reduced to 2');
|
||||
});
|
||||
|
||||
test('should throw exception when provided invalid values scenario one', function () {
|
||||
$this->expectException('Wearepixel\Cart\Exceptions\InvalidItemException');
|
||||
$this->cart->add(455, 'Sample Item', 100.99, 0, []);
|
||||
});
|
||||
|
||||
test('should throw exception when provided invalid values scenario two', function () {
|
||||
$this->expectException('Wearepixel\Cart\Exceptions\InvalidItemException');
|
||||
$this->cart->add('', 'Sample Item', 100.99, 2, []);
|
||||
});
|
||||
|
||||
test('should throw exception when provided invalid values scenario three', function () {
|
||||
$this->expectException('Wearepixel\Cart\Exceptions\InvalidItemException');
|
||||
$this->cart->add(523, '', 100.99, 2, []);
|
||||
});
|
||||
|
||||
test('clearing cart', function () {
|
||||
$items = [
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user