mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 13:51:44 +00:00
init commit :rocket
This commit is contained in:
@@ -0,0 +1,862 @@
|
||||
<?php
|
||||
|
||||
use Wearepixel\Cart\Cart;
|
||||
use Wearepixel\Cart\CartCondition;
|
||||
use Wearepixel\Cart\Tests\Helpers\SessionMock;
|
||||
|
||||
beforeEach(function () {
|
||||
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
||||
$events->shouldReceive('dispatch');
|
||||
|
||||
$this->cart = new Cart(
|
||||
new SessionMock,
|
||||
$events,
|
||||
'cart',
|
||||
'SAMPLESESSIONKEY',
|
||||
require (__DIR__ . '/../Helpers/ConfigMock.php')
|
||||
);
|
||||
|
||||
$items = [
|
||||
[
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 568,
|
||||
'name' => 'Sample Item 2',
|
||||
'price' => 69.25,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 856,
|
||||
'name' => 'Sample Item 3',
|
||||
'price' => 50.25,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
],
|
||||
];
|
||||
|
||||
$this->cart->add($items);
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
Mockery::close();
|
||||
});
|
||||
|
||||
test('subtotal', function () {
|
||||
// add condition to subtotal
|
||||
$condition = new CartCondition([
|
||||
'name' => 'VAT 12.5%',
|
||||
'type' => 'tax',
|
||||
'target' => 'subtotal',
|
||||
'value' => '-5',
|
||||
]);
|
||||
|
||||
$this->cart->condition($condition);
|
||||
|
||||
expect($this->cart->getSubTotal())->toEqual(182.49);
|
||||
|
||||
// the total is also should be the same with sub total since our getTotal
|
||||
// also depends on what is the value of subtotal
|
||||
expect($this->cart->getTotal())->toEqual(182.49);
|
||||
});
|
||||
|
||||
test('total without condition', function () {
|
||||
expect($this->cart->getSubTotal())->toEqual(187.49, 'Cart should have sub total of 187.49');
|
||||
|
||||
// no changes in subtotal as the condition's target added was for total
|
||||
expect($this->cart->getSubTotal())->toEqual(187.49, 'Cart should have sub total of 187.49');
|
||||
|
||||
// total should be the same as subtotal
|
||||
expect($this->cart->getTotal())->toEqual(187.49, 'Cart should have a total of 187.49');
|
||||
});
|
||||
|
||||
test('total with condition', function () {
|
||||
expect($this->cart->getSubTotal())->toEqual(187.49, 'Cart should have sub total of 187.49');
|
||||
|
||||
// add condition
|
||||
$condition = new CartCondition([
|
||||
'name' => 'VAT 12.5%',
|
||||
'type' => 'tax',
|
||||
'target' => 'total',
|
||||
'value' => '12.5%',
|
||||
]);
|
||||
|
||||
$this->cart->condition($condition);
|
||||
|
||||
// no changes in subtotal as the condition's target added was for total
|
||||
expect($this->cart->getSubTotal())->toEqual(187.49, 'Cart should have sub total of 187.49');
|
||||
|
||||
// total should be changed
|
||||
$this->cart->setDecimals(5);
|
||||
|
||||
expect($this->cart->getTotal())->toEqual(210.92625, 'Cart should have a total of 210.92625');
|
||||
});
|
||||
|
||||
test('total with multiple conditions added scenario one', function () {
|
||||
expect($this->cart->getSubTotal())->toEqual(187.49, 'Cart should have sub total of 187.49');
|
||||
|
||||
// add condition
|
||||
$condition1 = new CartCondition([
|
||||
'name' => 'VAT 12.5%',
|
||||
'type' => 'tax',
|
||||
'target' => 'total',
|
||||
'value' => '12.5%',
|
||||
]);
|
||||
|
||||
$condition2 = new CartCondition([
|
||||
'name' => 'Express Shipping $15',
|
||||
'type' => 'shipping',
|
||||
'target' => 'total',
|
||||
'value' => '+15',
|
||||
]);
|
||||
|
||||
$this->cart->condition($condition1);
|
||||
$this->cart->condition($condition2);
|
||||
|
||||
// no changes in subtotal as the condition's target added was for subtotal
|
||||
expect($this->cart->getSubTotal())->toEqual(187.49, 'Cart should have sub total of 187.49');
|
||||
|
||||
// total should be changed
|
||||
$this->cart->setDecimals(5);
|
||||
expect($this->cart->getTotal())->toEqual(225.92625, 'Cart should have a total of 225.92625');
|
||||
});
|
||||
|
||||
test('total with multiple conditions added scenario two', function () {
|
||||
expect($this->cart->getSubTotal())->toEqual(187.49, 'Cart should have sub total of 187.49');
|
||||
|
||||
// add condition
|
||||
$condition1 = new CartCondition([
|
||||
'name' => 'VAT 12.5%',
|
||||
'type' => 'tax',
|
||||
'target' => 'total',
|
||||
'value' => '12.5%',
|
||||
]);
|
||||
$condition2 = new CartCondition([
|
||||
'name' => 'Express Shipping $15',
|
||||
'type' => 'shipping',
|
||||
'target' => 'total',
|
||||
'value' => '-15',
|
||||
]);
|
||||
|
||||
$this->cart->condition($condition1);
|
||||
$this->cart->condition($condition2);
|
||||
|
||||
// no changes in subtotal as the condition's target added was for subtotal
|
||||
expect($this->cart->getSubTotal())->toEqual(187.49, 'Cart should have sub total of 187.49');
|
||||
|
||||
// total should be changed
|
||||
$this->cart->setDecimals(5);
|
||||
expect($this->cart->getTotal())->toEqual(195.92625, 'Cart should have a total of 195.92625');
|
||||
});
|
||||
|
||||
test('total with multiple conditions added scenario three', function () {
|
||||
expect($this->cart->getSubTotal())->toEqual(187.49, 'Cart should have sub total of 187.49');
|
||||
|
||||
// add condition
|
||||
$condition1 = new CartCondition([
|
||||
'name' => 'VAT 12.5%',
|
||||
'type' => 'tax',
|
||||
'target' => 'total',
|
||||
'value' => '-12.5%',
|
||||
]);
|
||||
$condition2 = new CartCondition([
|
||||
'name' => 'Express Shipping $15',
|
||||
'type' => 'shipping',
|
||||
'target' => 'total',
|
||||
'value' => '-15',
|
||||
]);
|
||||
|
||||
$this->cart->condition($condition1);
|
||||
$this->cart->condition($condition2);
|
||||
|
||||
// no changes in subtotal as the condition's target added was for total
|
||||
expect($this->cart->getSubTotal())->toEqual(187.49, 'Cart should have sub total of 187.49');
|
||||
|
||||
// total should be changed
|
||||
$this->cart->setDecimals(5);
|
||||
expect($this->cart->getTotal())->toEqual(149.05375, 'Cart should have a total of 149.05375');
|
||||
});
|
||||
|
||||
test('cart multiple conditions can be added once by array', function () {
|
||||
expect($this->cart->getSubTotal())->toEqual(187.49, 'Cart should have sub total of 187.49');
|
||||
|
||||
// add condition
|
||||
$condition1 = new CartCondition([
|
||||
'name' => 'VAT 12.5%',
|
||||
'type' => 'tax',
|
||||
'target' => 'total',
|
||||
'value' => '-12.5%',
|
||||
]);
|
||||
$condition2 = new CartCondition([
|
||||
'name' => 'Express Shipping $15',
|
||||
'type' => 'shipping',
|
||||
'target' => 'total',
|
||||
'value' => '-15',
|
||||
]);
|
||||
|
||||
$this->cart->condition([$condition1, $condition2]);
|
||||
|
||||
// no changes in subtotal as the condition's target added was for total
|
||||
expect($this->cart->getSubTotal())->toEqual(187.49, 'Cart should have sub total of 187.49');
|
||||
|
||||
// total should be changed
|
||||
$this->cart->setDecimals(5);
|
||||
expect($this->cart->getTotal())->toEqual(149.05375, 'Cart should have a total of 149.05375');
|
||||
});
|
||||
|
||||
test('total with multiple conditions added scenario four', function () {
|
||||
expect($this->cart->getSubTotal())->toEqual(187.49, 'Cart should have sub total of 187.49');
|
||||
|
||||
// add condition
|
||||
$condition1 = new CartCondition([
|
||||
'name' => 'COUPON LESS 12.5%',
|
||||
'type' => 'tax',
|
||||
'target' => 'total',
|
||||
'value' => '-12.5%',
|
||||
]);
|
||||
$condition2 = new CartCondition([
|
||||
'name' => 'Express Shipping $15',
|
||||
'type' => 'shipping',
|
||||
'target' => 'total',
|
||||
'value' => '+15',
|
||||
]);
|
||||
|
||||
$this->cart->condition($condition1);
|
||||
$this->cart->condition($condition2);
|
||||
|
||||
// no changes in subtotal as the condition's target added was for total
|
||||
expect($this->cart->getSubTotal())->toEqual(187.49, 'Cart should have sub total of 187.49');
|
||||
|
||||
// total should be changed
|
||||
$this->cart->setDecimals(5);
|
||||
expect($this->cart->getTotal())->toEqual(179.05375, 'Cart should have a total of 179.05375');
|
||||
});
|
||||
|
||||
test('add item with condition', function () {
|
||||
$this->cart->clear();
|
||||
|
||||
$condition1 = new CartCondition([
|
||||
'name' => 'SALE 5%',
|
||||
'type' => 'tax',
|
||||
'value' => '-5%',
|
||||
]);
|
||||
|
||||
$item = [
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 100,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
'conditions' => $condition1,
|
||||
];
|
||||
|
||||
$this->cart->add($item);
|
||||
|
||||
expect($this->cart->get(456)->getPriceSumWithConditions())->toEqual(95);
|
||||
expect($this->cart->getSubTotal())->toEqual(95);
|
||||
});
|
||||
|
||||
test('add item with multiple item conditions in multiple condition instance', function () {
|
||||
$this->cart->clear();
|
||||
|
||||
$itemCondition1 = new CartCondition([
|
||||
'name' => 'SALE 5%',
|
||||
'type' => 'sale',
|
||||
'value' => '-5%',
|
||||
]);
|
||||
$itemCondition2 = new CartCondition([
|
||||
'name' => 'Item Gift Pack 25.00',
|
||||
'type' => 'promo',
|
||||
'value' => '-25',
|
||||
]);
|
||||
$itemCondition3 = new CartCondition([
|
||||
'name' => 'MISC',
|
||||
'type' => 'misc',
|
||||
'value' => '+10',
|
||||
]);
|
||||
|
||||
$item = [
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 100,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
'conditions' => [$itemCondition1, $itemCondition2, $itemCondition3],
|
||||
];
|
||||
|
||||
$this->cart->add($item);
|
||||
|
||||
expect($this->cart->get(456)->getPriceSumWithConditions())->toEqual(80.00, 'Item subtotal with 1 item should be 80');
|
||||
expect($this->cart->getSubTotal())->toEqual(80.00, 'Cart subtotal with 1 item should be 80');
|
||||
});
|
||||
|
||||
test('add item with multiple item conditions with target omitted', function () {
|
||||
$this->cart->clear();
|
||||
|
||||
// NOTE:
|
||||
// $condition1 and $condition4 should not be included in calculation
|
||||
// as the target is not for item, remember that when adding
|
||||
// conditions in per-item bases, the condition's target should
|
||||
// have a value of item
|
||||
$itemCondition2 = new CartCondition([
|
||||
'name' => 'Item Gift Pack 25.00',
|
||||
'type' => 'promo',
|
||||
'value' => '-25',
|
||||
]);
|
||||
$itemCondition3 = new CartCondition([
|
||||
'name' => 'MISC',
|
||||
'type' => 'misc',
|
||||
'value' => '+10',
|
||||
]);
|
||||
|
||||
$item = [
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 100,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
'conditions' => [$itemCondition2, $itemCondition3],
|
||||
];
|
||||
|
||||
$this->cart->add($item);
|
||||
|
||||
expect($this->cart->get(456)->getPriceSumWithConditions())->toEqual(85.00, 'Cart subtotal with 1 item should be 85');
|
||||
expect($this->cart->getSubTotal())->toEqual(85.00, 'Cart subtotal with 1 item should be 85');
|
||||
});
|
||||
|
||||
test('add item condition', function () {
|
||||
$itemCondition2 = new CartCondition([
|
||||
'name' => 'Item Gift Pack 25.00',
|
||||
'type' => 'promo',
|
||||
'value' => '-25',
|
||||
]);
|
||||
$coupon101 = new CartCondition([
|
||||
'name' => 'COUPON 101',
|
||||
'type' => 'coupon',
|
||||
'value' => '-5%',
|
||||
]);
|
||||
|
||||
$item = [
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 100,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
'conditions' => [$itemCondition2],
|
||||
];
|
||||
|
||||
$this->cart->add($item);
|
||||
|
||||
// let's prove first we have 1 condition on this item
|
||||
expect($this->cart->get($item['id'])['conditions'])->toHaveCount(1, 'Item should have 1 condition');
|
||||
|
||||
// now let's insert a condition on an existing item on the cart
|
||||
$this->cart->addItemCondition($item['id'], $coupon101);
|
||||
|
||||
expect($this->cart->get($item['id'])['conditions'])->toHaveCount(2, 'Item should have 2 conditions');
|
||||
});
|
||||
|
||||
test('add item condition restrict negative price', function () {
|
||||
$condition = new CartCondition([
|
||||
'name' => 'Substract amount but prevent negative value',
|
||||
'type' => 'promo',
|
||||
'value' => '-25',
|
||||
]);
|
||||
|
||||
$item = [
|
||||
'id' => 789,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 20,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
'conditions' => [
|
||||
$condition,
|
||||
],
|
||||
];
|
||||
|
||||
$this->cart->add($item);
|
||||
|
||||
// Since the product price is 20 and the condition reduces it by 25,
|
||||
// check that the item's price has been prevented from dropping below zero.
|
||||
expect($this->cart->get($item['id'])->getPriceSumWithConditions())->toEqual(0.00, "The item's price should be prevented from going below zero.");
|
||||
});
|
||||
|
||||
test('get cart condition by condition name', function () {
|
||||
$itemCondition1 = new CartCondition([
|
||||
'name' => 'SALE 5%',
|
||||
'type' => 'sale',
|
||||
'target' => 'total',
|
||||
'value' => '-5%',
|
||||
]);
|
||||
$itemCondition2 = new CartCondition([
|
||||
'name' => 'Item Gift Pack 25.00',
|
||||
'type' => 'promo',
|
||||
'target' => 'total',
|
||||
'value' => '-25',
|
||||
]);
|
||||
|
||||
$item = [
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 100,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
];
|
||||
|
||||
$this->cart->add($item);
|
||||
|
||||
$this->cart->condition([$itemCondition1, $itemCondition2]);
|
||||
|
||||
// get a condition applied on cart by condition name
|
||||
$condition = $this->cart->getCondition($itemCondition1->getName());
|
||||
|
||||
expect('SALE 5%')->toEqual($condition->getName());
|
||||
expect('total')->toEqual($condition->getTarget());
|
||||
expect('sale')->toEqual($condition->getType());
|
||||
expect('-5%')->toEqual($condition->getValue());
|
||||
});
|
||||
|
||||
test('remove cart condition by condition name', function () {
|
||||
$itemCondition1 = new CartCondition([
|
||||
'name' => 'SALE 5%',
|
||||
'type' => 'sale',
|
||||
'target' => 'total',
|
||||
'value' => '-5%',
|
||||
]);
|
||||
$itemCondition2 = new CartCondition([
|
||||
'name' => 'Item Gift Pack 25.00',
|
||||
'type' => 'promo',
|
||||
'target' => 'total',
|
||||
'value' => '-25',
|
||||
]);
|
||||
|
||||
$item = [
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 100,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
];
|
||||
|
||||
$this->cart->add($item);
|
||||
|
||||
$this->cart->condition([$itemCondition1, $itemCondition2]);
|
||||
|
||||
// let's prove first we have now two conditions in the cart
|
||||
expect($this->cart->getConditions()->count())->toEqual(2, 'Cart should have two conditions');
|
||||
|
||||
// now let's remove a specific condition by condition name
|
||||
$this->cart->removeCartCondition('SALE 5%');
|
||||
|
||||
// cart should have now only 1 condition
|
||||
expect($this->cart->getConditions()->count())->toEqual(1, 'Cart should have one condition');
|
||||
expect($this->cart->getConditions()->first()->getName())->toEqual('Item Gift Pack 25.00');
|
||||
});
|
||||
|
||||
test('remove item condition by condition name', function () {
|
||||
$itemCondition1 = new CartCondition([
|
||||
'name' => 'SALE 5%',
|
||||
'type' => 'sale',
|
||||
'value' => '-5%',
|
||||
]);
|
||||
$itemCondition2 = new CartCondition([
|
||||
'name' => 'Item Gift Pack 25.00',
|
||||
'type' => 'promo',
|
||||
'value' => '-25',
|
||||
]);
|
||||
|
||||
$item = [
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 100,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
'conditions' => [$itemCondition1, $itemCondition2],
|
||||
];
|
||||
|
||||
$this->cart->add($item);
|
||||
|
||||
// let's very first the item has 2 conditions in it
|
||||
expect($this->cart->get(456)['conditions'])->toHaveCount(2, 'Item should have two conditions');
|
||||
|
||||
// now let's remove a condition on that item using the condition name
|
||||
$this->cart->removeItemCondition(456, 'SALE 5%');
|
||||
|
||||
// now we should have only 1 condition left on that item
|
||||
expect($this->cart->get(456)['conditions'])->toHaveCount(1, 'Item should have one condition left');
|
||||
});
|
||||
|
||||
test('remove item condition by condition name scenario two', function () {
|
||||
// NOTE: in this scenario, we will add the conditions not in array format
|
||||
$itemCondition = new CartCondition([
|
||||
'name' => 'SALE 5%',
|
||||
'type' => 'sale',
|
||||
'value' => '-5%',
|
||||
]);
|
||||
|
||||
$item = [
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 100,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
'conditions' => $itemCondition, // <--not in array format
|
||||
];
|
||||
|
||||
$this->cart->add($item);
|
||||
|
||||
// let's very first the item has 2 conditions in it
|
||||
expect($this->cart->get(456)['conditions'])->not->toBeEmpty('Item should have one condition in it.');
|
||||
|
||||
// now let's remove a condition on that item using the condition name
|
||||
$this->cart->removeItemCondition(456, 'SALE 5%');
|
||||
|
||||
// now we should have only 1 condition left on that item
|
||||
expect($this->cart->get(456)['conditions'])->toBeEmpty('Item should have no condition now');
|
||||
});
|
||||
|
||||
test('clear item conditions', function () {
|
||||
$itemCondition1 = new CartCondition([
|
||||
'name' => 'SALE 5%',
|
||||
'type' => 'sale',
|
||||
'value' => '-5%',
|
||||
]);
|
||||
$itemCondition2 = new CartCondition([
|
||||
'name' => 'Item Gift Pack 25.00',
|
||||
'type' => 'promo',
|
||||
'value' => '-25',
|
||||
]);
|
||||
|
||||
$item = [
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 100,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
'conditions' => [$itemCondition1, $itemCondition2],
|
||||
];
|
||||
|
||||
$this->cart->add($item);
|
||||
|
||||
// let's very first the item has 2 conditions in it
|
||||
expect($this->cart->get(456)['conditions'])->toHaveCount(2, 'Item should have two conditions');
|
||||
|
||||
// now let's remove all condition on that item
|
||||
$this->cart->clearItemConditions(456);
|
||||
|
||||
// now we should have only 0 condition left on that item
|
||||
expect($this->cart->get(456)['conditions'])->toHaveCount(0, 'Item should have no conditions now');
|
||||
});
|
||||
|
||||
test('clear cart conditions', function () {
|
||||
// NOTE:
|
||||
// This only clears all conditions that has been added in a cart bases
|
||||
// this does not remove conditions on per item bases
|
||||
$itemCondition1 = new CartCondition([
|
||||
'name' => 'SALE 5%',
|
||||
'type' => 'sale',
|
||||
'target' => 'total',
|
||||
'value' => '-5%',
|
||||
]);
|
||||
$itemCondition2 = new CartCondition([
|
||||
'name' => 'Item Gift Pack 25.00',
|
||||
'type' => 'promo',
|
||||
'target' => 'total',
|
||||
'value' => '-25',
|
||||
]);
|
||||
|
||||
$item = [
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 100,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
];
|
||||
|
||||
$this->cart->add($item);
|
||||
|
||||
$this->cart->condition([$itemCondition1, $itemCondition2]);
|
||||
|
||||
// let's prove first we have now two conditions in the cart
|
||||
expect($this->cart->getConditions()->count())->toEqual(2, 'Cart should have two conditions');
|
||||
|
||||
// now let's clear cart conditions
|
||||
$this->cart->clearCartConditions();
|
||||
|
||||
// cart should have now only 1 condition
|
||||
expect($this->cart->getConditions()->count())->toEqual(0, 'Cart should have no conditions now');
|
||||
});
|
||||
|
||||
test('get calculated value of a condition', function () {
|
||||
$this->cart->clear();
|
||||
|
||||
$cartCondition1 = new CartCondition([
|
||||
'name' => 'SALE 5%',
|
||||
'type' => 'sale',
|
||||
'target' => 'total',
|
||||
'value' => '-5%',
|
||||
]);
|
||||
$cartCondition2 = new CartCondition([
|
||||
'name' => 'Item Gift Pack 25.00',
|
||||
'type' => 'promo',
|
||||
'target' => 'total',
|
||||
'value' => '-25',
|
||||
]);
|
||||
|
||||
$item = [
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 100,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
];
|
||||
|
||||
$this->cart->add($item);
|
||||
|
||||
$this->cart->condition([$cartCondition1, $cartCondition2]);
|
||||
|
||||
$subTotal = $this->cart->getSubTotal();
|
||||
|
||||
expect($subTotal)->toEqual(100, 'Subtotal should be 100');
|
||||
|
||||
// way 1
|
||||
// now we will get the calculated value of the condition 1
|
||||
$cond1 = $this->cart->getCondition('SALE 5%');
|
||||
expect($cond1->getCalculatedValue($subTotal))->toEqual(5, 'The calculated value must be 5');
|
||||
|
||||
// way 2
|
||||
// get all cart conditions and get their calculated values
|
||||
$conditions = $this->cart->getConditions();
|
||||
expect($conditions['SALE 5%']->getCalculatedValue($subTotal))->toEqual(5, 'First condition calculated value must be 5');
|
||||
expect($conditions['Item Gift Pack 25.00']->getCalculatedValue($subTotal))->toEqual(25, 'First condition calculated value must be 5');
|
||||
});
|
||||
|
||||
test('get conditions by type', function () {
|
||||
$cartCondition1 = new CartCondition([
|
||||
'name' => 'SALE 5%',
|
||||
'type' => 'sale',
|
||||
'target' => 'total',
|
||||
'value' => '-5%',
|
||||
]);
|
||||
$cartCondition2 = new CartCondition([
|
||||
'name' => 'Item Gift Pack 25.00',
|
||||
'type' => 'promo',
|
||||
'target' => 'total',
|
||||
'value' => '-25',
|
||||
]);
|
||||
$cartCondition3 = new CartCondition([
|
||||
'name' => 'Item Less 8%',
|
||||
'type' => 'promo',
|
||||
'target' => 'total',
|
||||
'value' => '-8%',
|
||||
]);
|
||||
|
||||
$item = [
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 100,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
];
|
||||
|
||||
$this->cart->add($item);
|
||||
|
||||
$this->cart->condition([$cartCondition1, $cartCondition2, $cartCondition3]);
|
||||
|
||||
// now lets get all conditions added in the cart with the type "promo"
|
||||
$promoConditions = $this->cart->getConditionsByType('promo');
|
||||
|
||||
expect($promoConditions->count())->toEqual(2, 'We should have 2 items as promo condition type.');
|
||||
});
|
||||
|
||||
test('remove conditions by type', function () {
|
||||
// NOTE:
|
||||
// when add a new condition, the condition's name will be the key to be use
|
||||
// to access the condition. For some reasons, if the condition name contains
|
||||
// a "dot" on it ("."), for example adding a condition with name "SALE 35.00"
|
||||
// this will cause issues when removing this condition by name, this will not be removed
|
||||
// so when adding a condition, the condition name should not contain any "period" (.)
|
||||
// to avoid any issues removing it using remove method: removeCartCondition($conditionName);
|
||||
$cartCondition1 = new CartCondition([
|
||||
'name' => 'SALE 5%',
|
||||
'type' => 'sale',
|
||||
'target' => 'total',
|
||||
'value' => '-5%',
|
||||
]);
|
||||
$cartCondition2 = new CartCondition([
|
||||
'name' => 'Item Gift Pack 20',
|
||||
'type' => 'promo',
|
||||
'target' => 'total',
|
||||
'value' => '-25',
|
||||
]);
|
||||
$cartCondition3 = new CartCondition([
|
||||
'name' => 'Item Less 8%',
|
||||
'type' => 'promo',
|
||||
'target' => 'total',
|
||||
'value' => '-8%',
|
||||
]);
|
||||
|
||||
$item = [
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 100,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
];
|
||||
|
||||
$this->cart->add($item);
|
||||
|
||||
$this->cart->condition([$cartCondition1, $cartCondition2, $cartCondition3]);
|
||||
|
||||
// now lets remove all conditions added in the cart with the type "promo"
|
||||
$this->cart->removeConditionsByType('promo');
|
||||
|
||||
expect($this->cart->getConditions()->count())->toEqual(1, 'We should have 1 condition remaining as promo conditions type has been removed.');
|
||||
});
|
||||
|
||||
test('add cart condition without condition attributes', function () {
|
||||
$cartCondition1 = new CartCondition([
|
||||
'name' => 'SALE 5%',
|
||||
'type' => 'sale',
|
||||
'target' => 'total',
|
||||
'value' => '-5%',
|
||||
]);
|
||||
|
||||
$item = [
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 100,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
];
|
||||
|
||||
$this->cart->add($item);
|
||||
|
||||
$this->cart->condition([$cartCondition1]);
|
||||
|
||||
// prove first we have now the condition on the cart
|
||||
$contition = $this->cart->getCondition('SALE 5%');
|
||||
expect($contition->getName())->toEqual('SALE 5%');
|
||||
|
||||
// when get attribute is called and there is no attributes added,
|
||||
// it should return an empty array
|
||||
$conditionAttribute = $contition->getAttributes();
|
||||
expect($conditionAttribute)->toBeArray();
|
||||
});
|
||||
|
||||
test('add cart condition with condition attributes', function () {
|
||||
$cartCondition1 = new CartCondition([
|
||||
'name' => 'SALE 5%',
|
||||
'type' => 'sale',
|
||||
'target' => 'total',
|
||||
'value' => '-5%',
|
||||
'attributes' => [
|
||||
'description' => 'october fest promo sale',
|
||||
'sale_start_date' => '2015-01-20',
|
||||
'sale_end_date' => '2015-01-30',
|
||||
],
|
||||
]);
|
||||
|
||||
$item = [
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 100,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
];
|
||||
|
||||
$this->cart->add($item);
|
||||
|
||||
$this->cart->condition([$cartCondition1]);
|
||||
|
||||
// prove first we have now the condition on the cart
|
||||
$contition = $this->cart->getCondition('SALE 5%');
|
||||
expect($contition->getName())->toEqual('SALE 5%');
|
||||
|
||||
// when get attribute is called and there is no attributes added,
|
||||
// it should return an empty array
|
||||
$conditionAttributes = $contition->getAttributes();
|
||||
expect($conditionAttributes)->toBeArray();
|
||||
expect($conditionAttributes)->toHaveKey('description');
|
||||
expect($conditionAttributes)->toHaveKey('sale_start_date');
|
||||
expect($conditionAttributes)->toHaveKey('sale_end_date');
|
||||
expect($conditionAttributes['description'])->toEqual('october fest promo sale');
|
||||
expect($conditionAttributes['sale_start_date'])->toEqual('2015-01-20');
|
||||
expect($conditionAttributes['sale_end_date'])->toEqual('2015-01-30');
|
||||
});
|
||||
|
||||
test('get order from condition', function () {
|
||||
$cartCondition1 = new CartCondition([
|
||||
'name' => 'SALE 5%',
|
||||
'type' => 'sale',
|
||||
'target' => 'total',
|
||||
'value' => '-5%',
|
||||
'order' => 2,
|
||||
]);
|
||||
$cartCondition2 = new CartCondition([
|
||||
'name' => 'Item Gift Pack 20',
|
||||
'type' => 'promo',
|
||||
'target' => 'total',
|
||||
'value' => '-25',
|
||||
'order' => '3',
|
||||
]);
|
||||
$cartCondition3 = new CartCondition([
|
||||
'name' => 'Item Less 8%',
|
||||
'type' => 'tax',
|
||||
'target' => 'total',
|
||||
'value' => '-8%',
|
||||
'order' => 'first',
|
||||
]);
|
||||
|
||||
expect($cartCondition1->getOrder())->toEqual(2);
|
||||
expect($cartCondition2->getOrder())->toEqual(3);
|
||||
// numeric string is converted to integer
|
||||
expect($cartCondition3->getOrder())->toEqual(0);
|
||||
|
||||
// no numeric string is converted to 0
|
||||
$this->cart->condition($cartCondition1);
|
||||
$this->cart->condition($cartCondition2);
|
||||
$this->cart->condition($cartCondition3);
|
||||
|
||||
$conditions = $this->cart->getConditions();
|
||||
|
||||
expect($conditions->shift()->getType())->toEqual('sale');
|
||||
expect($conditions->shift()->getType())->toEqual('promo');
|
||||
expect($conditions->shift()->getType())->toEqual('tax');
|
||||
});
|
||||
|
||||
test('condition ordering', function () {
|
||||
$cartCondition1 = new CartCondition([
|
||||
'name' => 'TAX',
|
||||
'type' => 'tax',
|
||||
'target' => 'total',
|
||||
'value' => '-8%',
|
||||
'order' => 5,
|
||||
]);
|
||||
$cartCondition2 = new CartCondition([
|
||||
'name' => 'SALE 5%',
|
||||
'type' => 'sale',
|
||||
'target' => 'total',
|
||||
'value' => '-5%',
|
||||
'order' => 2,
|
||||
]);
|
||||
$cartCondition3 = new CartCondition([
|
||||
'name' => 'Item Gift Pack 20',
|
||||
'type' => 'promo',
|
||||
'target' => 'total',
|
||||
'value' => '-25',
|
||||
'order' => 1,
|
||||
]);
|
||||
|
||||
$this->cart->condition($cartCondition1);
|
||||
$this->cart->condition($cartCondition2);
|
||||
$this->cart->condition($cartCondition3);
|
||||
|
||||
expect($this->cart->getConditions()->first()->getName())->toEqual('Item Gift Pack 20');
|
||||
expect($this->cart->getConditions()->last()->getName())->toEqual('TAX');
|
||||
});
|
||||
@@ -0,0 +1,531 @@
|
||||
<?php
|
||||
|
||||
use Wearepixel\Cart\Cart;
|
||||
use Wearepixel\Cart\Tests\Helpers\MockProduct;
|
||||
use Wearepixel\Cart\Tests\Helpers\SessionMock;
|
||||
|
||||
beforeEach(function () {
|
||||
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
||||
$events->shouldReceive('dispatch');
|
||||
|
||||
$this->cart = new Cart(
|
||||
new SessionMock,
|
||||
$events,
|
||||
'shopping',
|
||||
'SAMPLESESSIONKEY',
|
||||
require (__DIR__ . '/../Helpers/ConfigMock.php')
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
Mockery::close();
|
||||
});
|
||||
|
||||
test('cart can add item', function () {
|
||||
$this->cart->add(455, 'Sample Item', 100.99, 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->getContent()->first()['price'])->toEqual(100.99, 'Item added has price of 100.99 so first content price should be 100.99');
|
||||
});
|
||||
|
||||
test('cart can add item with no price', function () {
|
||||
$this->cart->add(455, 'Sample Item', 0, 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('cart can add items as array', function () {
|
||||
$item = [
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item',
|
||||
'price' => 67.99,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
];
|
||||
|
||||
$this->cart->add($item);
|
||||
|
||||
expect($this->cart->isEmpty())->toBeFalse('Cart should not be empty');
|
||||
expect($this->cart->getContent()->count())->toEqual(1, 'Cart should have 1 item on it');
|
||||
expect($this->cart->getContent()->first()['id'])->toEqual(456, 'The first content must have ID of 456');
|
||||
expect($this->cart->getContent()->first()['name'])->toEqual('Sample Item', 'The first content must have name of "Sample Item"');
|
||||
});
|
||||
|
||||
test('cart can add items with multidimensional array', function () {
|
||||
$items = [
|
||||
[
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 568,
|
||||
'name' => 'Sample Item 2',
|
||||
'price' => 69.25,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 856,
|
||||
'name' => 'Sample Item 3',
|
||||
'price' => 50.25,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
],
|
||||
];
|
||||
|
||||
$this->cart->add($items);
|
||||
|
||||
expect($this->cart->isEmpty())->toBeFalse('Cart should not be empty');
|
||||
expect($this->cart->getContent()->toArray())->toHaveCount(3, 'Cart should have 3 items');
|
||||
});
|
||||
|
||||
test('cart can add item without attributes', function () {
|
||||
$item = [
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 4,
|
||||
];
|
||||
|
||||
$this->cart->add($item);
|
||||
|
||||
expect($this->cart->isEmpty())->toBeFalse('Cart should not be empty');
|
||||
});
|
||||
|
||||
test('cart update with attribute then attributes should be still instance of item attribute collection', function () {
|
||||
$item = [
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 4,
|
||||
'attributes' => [
|
||||
'product_id' => '145',
|
||||
'color' => 'red',
|
||||
],
|
||||
];
|
||||
$this->cart->add($item);
|
||||
|
||||
// lets get the attribute and prove first its an instance of
|
||||
// ItemAttributeCollection
|
||||
$item = $this->cart->get(456);
|
||||
|
||||
expect($item->attributes)->toBeInstanceOf('Wearepixel\Cart\ItemAttributeCollection');
|
||||
|
||||
// now lets update the item with its new attributes
|
||||
// when we get that item from cart, it should still be an instance of ItemAttributeCollection
|
||||
$updatedItem = [
|
||||
'attributes' => [
|
||||
'product_id' => '145',
|
||||
'color' => 'red',
|
||||
],
|
||||
];
|
||||
$this->cart->update(456, $updatedItem);
|
||||
|
||||
expect($item->attributes)->toBeInstanceOf('Wearepixel\Cart\ItemAttributeCollection');
|
||||
});
|
||||
|
||||
test('cart items attributes', function () {
|
||||
$item = [
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 4,
|
||||
'attributes' => [
|
||||
'size' => 'L',
|
||||
'color' => 'blue',
|
||||
],
|
||||
];
|
||||
|
||||
$this->cart->add($item);
|
||||
|
||||
expect($this->cart->isEmpty())->toBeFalse('Cart should not be empty');
|
||||
expect($this->cart->getContent()->first()['attributes'])->toHaveCount(2, 'Item\'s attribute should have two');
|
||||
expect($this->cart->getContent()->first()->attributes->size)->toEqual('L', 'Item should have attribute size of L');
|
||||
expect($this->cart->getContent()->first()->attributes->color)->toEqual('blue', 'Item should have attribute color of blue');
|
||||
expect($this->cart->get(456)->has('attributes'))->toBeTrue('Item should have attributes');
|
||||
expect($this->cart->get(456)->get('attributes')->size)->toEqual('L');
|
||||
});
|
||||
|
||||
test('cart update existing item', function () {
|
||||
$items = [
|
||||
[
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 3,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 568,
|
||||
'name' => 'Sample Item 2',
|
||||
'price' => 69.25,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
],
|
||||
];
|
||||
|
||||
$this->cart->add($items);
|
||||
|
||||
$itemIdToEvaluate = 456;
|
||||
|
||||
$item = $this->cart->get($itemIdToEvaluate);
|
||||
expect($item['name'])->toEqual('Sample Item 1', 'Item name should be "Sample Item 1"');
|
||||
expect($item['price'])->toEqual(67.99, 'Item price should be "67.99"');
|
||||
expect($item['quantity'])->toEqual(3, 'Item quantity should be 3');
|
||||
|
||||
// when cart's item quantity is updated, the subtotal should be updated as well
|
||||
$this->cart->update(456, [
|
||||
'name' => 'Renamed',
|
||||
'quantity' => 2,
|
||||
'price' => 105,
|
||||
]);
|
||||
|
||||
$item = $this->cart->get($itemIdToEvaluate);
|
||||
expect($item['name'])->toEqual('Renamed', 'Item name should be "Renamed"');
|
||||
expect($item['price'])->toEqual(105, 'Item price should be 105');
|
||||
expect($item['quantity'])->toEqual(5, 'Item quantity should be 2');
|
||||
});
|
||||
|
||||
test('cart update existing item with quantity as array and not relative', function () {
|
||||
$items = [
|
||||
[
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 3,
|
||||
'attributes' => [],
|
||||
],
|
||||
];
|
||||
|
||||
$this->cart->add($items);
|
||||
|
||||
$itemIdToEvaluate = 456;
|
||||
$item = $this->cart->get($itemIdToEvaluate);
|
||||
expect($item['quantity'])->toEqual(3, 'Item quantity should be 3');
|
||||
|
||||
// now by default when an update takes place and the quantity attribute
|
||||
// is present, it will evaluate for arithmetic operation if the quantity
|
||||
// should be incremented or decremented, we should also allow the quantity
|
||||
// value to be in array format and provide a field if the quantity should not be
|
||||
// treated as relative to Item quantity current value
|
||||
$this->cart->update($itemIdToEvaluate, ['quantity' => ['relative' => false, 'value' => 5]]);
|
||||
|
||||
$item = $this->cart->get($itemIdToEvaluate);
|
||||
expect($item['quantity'])->toEqual(5, 'Item quantity should be 5');
|
||||
});
|
||||
|
||||
test('item price should be normalized when added to cart', function () {
|
||||
// add a price in a string format should be converted to float
|
||||
$this->cart->add(455, 'Sample Item', '100.99', 2, []);
|
||||
|
||||
expect($this->cart->getContent()->first()['price'])->toBeFloat('Cart price should be a float');
|
||||
});
|
||||
|
||||
test('it removes an item on cart by item id', function () {
|
||||
$items = [
|
||||
[
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 568,
|
||||
'name' => 'Sample Item 2',
|
||||
'price' => 69.25,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 856,
|
||||
'name' => 'Sample Item 3',
|
||||
'price' => 50.25,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
],
|
||||
];
|
||||
|
||||
$this->cart->add($items);
|
||||
|
||||
$removeItemId = 456;
|
||||
|
||||
$this->cart->remove($removeItemId);
|
||||
|
||||
expect($this->cart->getContent()->toArray())->toHaveCount(2, 'Cart must have 2 items left');
|
||||
expect($this->cart->getContent()->has($removeItemId))->toBeFalse('Cart must have not contain the remove item anymore');
|
||||
});
|
||||
|
||||
test('cart sub total', function () {
|
||||
$items = [
|
||||
[
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 568,
|
||||
'name' => 'Sample Item 2',
|
||||
'price' => 69.25,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 856,
|
||||
'name' => 'Sample Item 3',
|
||||
'price' => 50.25,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
],
|
||||
];
|
||||
|
||||
$this->cart->add($items);
|
||||
|
||||
expect($this->cart->getSubTotal())->toEqual(187.49, 'Cart should have sub total of 187.49');
|
||||
|
||||
// if we remove an item, the sub total should be updated as well
|
||||
$this->cart->remove(456);
|
||||
|
||||
expect($this->cart->getSubTotal())->toEqual(119.5, 'Cart should have sub total of 119.5');
|
||||
});
|
||||
|
||||
test('sub total when item quantity is updated', function () {
|
||||
$this->cart->clear();
|
||||
|
||||
$items = [
|
||||
[
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 3,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 568,
|
||||
'name' => 'Sample Item 2',
|
||||
'price' => 69.25,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
],
|
||||
];
|
||||
|
||||
$this->cart->add($items);
|
||||
|
||||
expect($this->cart->getSubTotal())
|
||||
->toBe(273.22, 'Cart should have sub total of 273.22');
|
||||
|
||||
// when cart's item quantity is updated, the subtotal should be updated as well
|
||||
$this->cart->update(456, ['quantity' => 2]);
|
||||
|
||||
expect($this->cart->getSubTotal())->toEqual(409.2, 'Cart should have sub total of 409.2');
|
||||
});
|
||||
|
||||
test('sub total when item quantity is updated by reduced', function () {
|
||||
// $this->cart->setConfig(['format_numbers' => true, 'decimals' => 2, 'dec_point' => '.', 'thousands_sep' => ',']);
|
||||
|
||||
$items = [
|
||||
[
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 3,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 568,
|
||||
'name' => 'Sample Item 2',
|
||||
'price' => 69.25,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
],
|
||||
];
|
||||
|
||||
$this->cart->add($items);
|
||||
|
||||
expect($this->cart->getSubTotal())
|
||||
->toBe(273.22, 'Cart should have sub total of 273.22');
|
||||
|
||||
// when cart's item quantity is updated, the subtotal should be updated as well
|
||||
$this->cart->update(456, ['quantity' => -1]);
|
||||
|
||||
// get the item to be evaluated
|
||||
$item = $this->cart->get(456);
|
||||
|
||||
expect($item['quantity'])->toEqual(2, 'Item quantity of with item ID of 456 should now be reduced to 2');
|
||||
expect($this->cart->getSubTotal())->toEqual(205.23, 'Cart should have sub total of 205.23');
|
||||
});
|
||||
|
||||
test('item quantity update by reduced should not reduce if quantity will result to zero', function () {
|
||||
$items = [
|
||||
[
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 3,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 568,
|
||||
'name' => 'Sample Item 2',
|
||||
'price' => 69.25,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
],
|
||||
];
|
||||
|
||||
$this->cart->add($items);
|
||||
|
||||
// get the item to be evaluated
|
||||
$item = $this->cart->get(456);
|
||||
|
||||
// prove first we have quantity of 3
|
||||
expect($item['quantity'])->toEqual(3, 'Item quantity of with item ID of 456 should be reduced to 3');
|
||||
|
||||
// when cart's item quantity is updated, and reduced to more than the current quantity
|
||||
// this should not work
|
||||
$this->cart->update(456, ['quantity' => -3]);
|
||||
|
||||
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 = [
|
||||
[
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 3,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 568,
|
||||
'name' => 'Sample Item 2',
|
||||
'price' => 69.25,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
],
|
||||
];
|
||||
|
||||
$this->cart->add($items);
|
||||
|
||||
expect($this->cart->isEmpty())->toBeFalse('prove first cart is not empty');
|
||||
|
||||
// now let's clear cart
|
||||
$this->cart->clear();
|
||||
|
||||
expect($this->cart->isEmpty())->toBeTrue('cart should now be empty');
|
||||
});
|
||||
|
||||
test('cart get total quantity', function () {
|
||||
$items = [
|
||||
[
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 3,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 568,
|
||||
'name' => 'Sample Item 2',
|
||||
'price' => 69.25,
|
||||
'quantity' => 1,
|
||||
'attributes' => [],
|
||||
],
|
||||
];
|
||||
|
||||
$this->cart->add($items);
|
||||
|
||||
expect($this->cart->isEmpty())->toBeFalse('prove first cart is not empty');
|
||||
|
||||
// now let's count the cart's quantity
|
||||
expect($this->cart->getTotalQuantity())->toBeInt('Return type should be INT');
|
||||
expect($this->cart->getTotalQuantity())->toEqual(4, 'Cart\'s quantity should be 4.');
|
||||
});
|
||||
|
||||
test('cart can add items as array with associated model', function () {
|
||||
$item = [
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item',
|
||||
'price' => 67.99,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
'associatedModel' => MockProduct::class,
|
||||
];
|
||||
|
||||
$this->cart->add($item);
|
||||
|
||||
$addedItem = $this->cart->get($item['id']);
|
||||
|
||||
expect($this->cart->isEmpty())->toBeFalse('Cart should not be empty');
|
||||
expect($this->cart->getContent()->count())->toEqual(1, 'Cart should have 1 item on it');
|
||||
expect($this->cart->getContent()->first()['id'])->toEqual(456, 'The first content must have ID of 456');
|
||||
expect($this->cart->getContent()->first()['name'])->toEqual('Sample Item', 'The first content must have name of "Sample Item"');
|
||||
expect($addedItem->model)->toBeInstanceOf('Wearepixel\Cart\Tests\Helpers\MockProduct');
|
||||
});
|
||||
|
||||
test('cart can add items with multidimensional array with associated model', function () {
|
||||
$items = [
|
||||
[
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
'associatedModel' => MockProduct::class,
|
||||
],
|
||||
[
|
||||
'id' => 568,
|
||||
'name' => 'Sample Item 2',
|
||||
'price' => 69.25,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
'associatedModel' => MockProduct::class,
|
||||
],
|
||||
[
|
||||
'id' => 856,
|
||||
'name' => 'Sample Item 3',
|
||||
'price' => 50.25,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
'associatedModel' => MockProduct::class,
|
||||
],
|
||||
];
|
||||
|
||||
$this->cart->add($items);
|
||||
|
||||
$content = $this->cart->getContent();
|
||||
foreach ($content as $item) {
|
||||
expect($item->model)->toBeInstanceOf('Wearepixel\Cart\Tests\Helpers\MockProduct');
|
||||
}
|
||||
|
||||
expect($this->cart->isEmpty())->toBeFalse('Cart should not be empty');
|
||||
expect($this->cart->getContent()->toArray())->toHaveCount(3, 'Cart should have 3 items');
|
||||
expect($this->cart->getTotalQuantity())->toBeInt('Return type should be INT');
|
||||
expect($this->cart->getTotalQuantity())->toEqual(12, 'Cart\'s quantity should be 4.');
|
||||
});
|
||||
@@ -0,0 +1,201 @@
|
||||
<?php
|
||||
|
||||
use Wearepixel\Cart\Cart;
|
||||
use Wearepixel\Cart\Tests\Helpers\SessionMock;
|
||||
|
||||
beforeEach(function () {});
|
||||
|
||||
afterEach(function () {
|
||||
Mockery::close();
|
||||
});
|
||||
|
||||
test('event cart created', function () {
|
||||
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
||||
$events->shouldReceive('dispatch')->once()->with('cart.created', Mockery::type('array'), true);
|
||||
|
||||
$cart = new Cart(
|
||||
new SessionMock,
|
||||
$events,
|
||||
'cart',
|
||||
'SAMPLESESSIONKEY',
|
||||
require (__DIR__ . '/../Helpers/ConfigMock.php')
|
||||
);
|
||||
|
||||
expect(true)->toBeTrue();
|
||||
});
|
||||
|
||||
test('event cart adding', function () {
|
||||
$events = Mockery::mock('Illuminate\Events\Dispatcher');
|
||||
$events->shouldReceive('dispatch')->once()->with('cart.created', Mockery::type('array'), true);
|
||||
$events->shouldReceive('dispatch')->once()->with('cart.adding', Mockery::type('array'), true);
|
||||
$events->shouldReceive('dispatch')->once()->with('cart.added', Mockery::type('array'), true);
|
||||
|
||||
$cart = new Cart(
|
||||
new SessionMock,
|
||||
$events,
|
||||
'cart',
|
||||
'SAMPLESESSIONKEY',
|
||||
require (__DIR__ . '/../Helpers/ConfigMock.php')
|
||||
);
|
||||
|
||||
$cart->add(455, 'Sample Item', 100.99, 2, []);
|
||||
|
||||
expect(true)->toBeTrue();
|
||||
});
|
||||
|
||||
test('event cart adding multiple times', function () {
|
||||
$events = Mockery::mock('Illuminate\Events\Dispatcher');
|
||||
$events->shouldReceive('dispatch')->once()->with('cart.created', Mockery::type('array'), true);
|
||||
$events->shouldReceive('dispatch')->times(2)->with('cart.adding', Mockery::type('array'), true);
|
||||
$events->shouldReceive('dispatch')->times(2)->with('cart.added', Mockery::type('array'), true);
|
||||
|
||||
$cart = new Cart(
|
||||
new SessionMock,
|
||||
$events,
|
||||
'cart',
|
||||
'SAMPLESESSIONKEY',
|
||||
require (__DIR__ . '/../Helpers/ConfigMock.php')
|
||||
);
|
||||
|
||||
$cart->add(455, 'Sample Item 1', 100.99, 2, []);
|
||||
$cart->add(562, 'Sample Item 2', 100.99, 2, []);
|
||||
|
||||
expect(true)->toBeTrue();
|
||||
});
|
||||
|
||||
test('event cart adding multiple times scenario two', function () {
|
||||
$events = Mockery::mock('Illuminate\Events\Dispatcher');
|
||||
$events->shouldReceive('dispatch')->once()->with('cart.created', Mockery::type('array'), true);
|
||||
$events->shouldReceive('dispatch')->times(3)->with('cart.adding', Mockery::type('array'), true);
|
||||
$events->shouldReceive('dispatch')->times(3)->with('cart.added', Mockery::type('array'), true);
|
||||
|
||||
$items = [
|
||||
[
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 568,
|
||||
'name' => 'Sample Item 2',
|
||||
'price' => 69.25,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 856,
|
||||
'name' => 'Sample Item 3',
|
||||
'price' => 50.25,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
],
|
||||
];
|
||||
|
||||
$cart = new Cart(
|
||||
new SessionMock,
|
||||
$events,
|
||||
'cart',
|
||||
'SAMPLESESSIONKEY',
|
||||
require (__DIR__ . '/../Helpers/ConfigMock.php')
|
||||
);
|
||||
|
||||
$cart->add($items);
|
||||
|
||||
expect(true)->toBeTrue();
|
||||
});
|
||||
|
||||
test('event cart remove item', function () {
|
||||
$events = Mockery::mock('Illuminate\Events\Dispatcher');
|
||||
$events->shouldReceive('dispatch')->once()->with('cart.created', Mockery::type('array'), true);
|
||||
$events->shouldReceive('dispatch')->times(3)->with('cart.adding', Mockery::type('array'), true);
|
||||
$events->shouldReceive('dispatch')->times(3)->with('cart.added', Mockery::type('array'), true);
|
||||
$events->shouldReceive('dispatch')->times(1)->with('cart.removing', Mockery::type('array'), true);
|
||||
$events->shouldReceive('dispatch')->times(1)->with('cart.removed', Mockery::type('array'), true);
|
||||
|
||||
$items = [
|
||||
[
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 568,
|
||||
'name' => 'Sample Item 2',
|
||||
'price' => 69.25,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 856,
|
||||
'name' => 'Sample Item 3',
|
||||
'price' => 50.25,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
],
|
||||
];
|
||||
|
||||
$cart = new Cart(
|
||||
new SessionMock,
|
||||
$events,
|
||||
'cart',
|
||||
'SAMPLESESSIONKEY',
|
||||
require (__DIR__ . '/../Helpers/ConfigMock.php')
|
||||
);
|
||||
|
||||
$cart->add($items);
|
||||
|
||||
$cart->remove(456);
|
||||
|
||||
expect(true)->toBeTrue();
|
||||
});
|
||||
|
||||
test('event cart clear', function () {
|
||||
$events = Mockery::mock('Illuminate\Events\Dispatcher');
|
||||
$events->shouldReceive('dispatch')->once()->with('cart.created', Mockery::type('array'), true);
|
||||
$events->shouldReceive('dispatch')->times(3)->with('cart.adding', Mockery::type('array'), true);
|
||||
$events->shouldReceive('dispatch')->times(3)->with('cart.added', Mockery::type('array'), true);
|
||||
$events->shouldReceive('dispatch')->once()->with('cart.clearing', Mockery::type('array'), true);
|
||||
$events->shouldReceive('dispatch')->once()->with('cart.cleared', Mockery::type('array'), true);
|
||||
|
||||
$items = [
|
||||
[
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 568,
|
||||
'name' => 'Sample Item 2',
|
||||
'price' => 69.25,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 856,
|
||||
'name' => 'Sample Item 3',
|
||||
'price' => 50.25,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
],
|
||||
];
|
||||
|
||||
$cart = new Cart(
|
||||
new SessionMock,
|
||||
$events,
|
||||
'cart',
|
||||
'SAMPLESESSIONKEY',
|
||||
require (__DIR__ . '/../Helpers/ConfigMock.php')
|
||||
);
|
||||
|
||||
$cart->add($items);
|
||||
|
||||
$cart->clear();
|
||||
|
||||
expect(true)->toBeTrue();
|
||||
});
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
use Wearepixel\Cart\Cart;
|
||||
use Wearepixel\Cart\Tests\Helpers\SessionMock;
|
||||
|
||||
beforeEach(function () {
|
||||
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
||||
$events->shouldReceive('dispatch');
|
||||
|
||||
$this->cart1 = new Cart(
|
||||
new SessionMock,
|
||||
$events,
|
||||
'shopping',
|
||||
'uniquesessionkey123',
|
||||
require (__DIR__ . '/../Helpers/ConfigMock.php')
|
||||
);
|
||||
|
||||
$this->cart2 = new Cart(
|
||||
new SessionMock,
|
||||
$events,
|
||||
'wishlist',
|
||||
'uniquesessionkey456',
|
||||
require (__DIR__ . '/../Helpers/ConfigMock.php')
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
Mockery::close();
|
||||
});
|
||||
|
||||
test('cart multiple instances', function () {
|
||||
// add 3 items on cart 1
|
||||
$itemsForCart1 = [
|
||||
[
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 568,
|
||||
'name' => 'Sample Item 2',
|
||||
'price' => 69.25,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
],
|
||||
[
|
||||
'id' => 856,
|
||||
'name' => 'Sample Item 3',
|
||||
'price' => 50.25,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
],
|
||||
];
|
||||
|
||||
$this->cart1->add($itemsForCart1);
|
||||
|
||||
expect($this->cart1->isEmpty())->toBeFalse('Cart should not be empty');
|
||||
expect($this->cart1->getContent()->toArray())->toHaveCount(3, 'Cart should have 3 items');
|
||||
expect($this->cart1->getInstanceName())->toEqual('shopping', 'Cart 1 should have instance name of "shopping"');
|
||||
|
||||
// add 1 item on cart 2
|
||||
$itemsForCart2 = [
|
||||
[
|
||||
'id' => 456,
|
||||
'name' => 'Sample Item 1',
|
||||
'price' => 67.99,
|
||||
'quantity' => 4,
|
||||
'attributes' => [],
|
||||
],
|
||||
];
|
||||
|
||||
$this->cart2->add($itemsForCart2);
|
||||
|
||||
expect($this->cart2->isEmpty())->toBeFalse('Cart should not be empty');
|
||||
expect($this->cart2->getContent()->toArray())->toHaveCount(1, 'Cart should have 3 items');
|
||||
expect($this->cart2->getInstanceName())->toEqual('wishlist', 'Cart 2 should have instance name of "wishlist"');
|
||||
});
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
use Wearepixel\Cart\Cart;
|
||||
use Wearepixel\Cart\CartCondition;
|
||||
use Wearepixel\Cart\Tests\Helpers\MockProduct;
|
||||
use Wearepixel\Cart\Tests\Helpers\SessionMock;
|
||||
|
||||
beforeEach(function () {
|
||||
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
||||
$events->shouldReceive('dispatch');
|
||||
|
||||
$this->cart = new Cart(
|
||||
new SessionMock,
|
||||
$events,
|
||||
'shopping',
|
||||
'SAMPLESESSIONKEY',
|
||||
require (__DIR__ . '/../Helpers/ConfigMock.php')
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
Mockery::close();
|
||||
});
|
||||
|
||||
test('item get sum price using property', function () {
|
||||
$this->cart->add(455, 'Sample Item', 100.99, 2, []);
|
||||
|
||||
$item = $this->cart->get(455);
|
||||
|
||||
expect($item->getPriceSum())->toEqual(201.98, 'Item summed price should be 201.98');
|
||||
});
|
||||
|
||||
test('item get sum price using array style', function () {
|
||||
$this->cart->add(455, 'Sample Item', 100.99, 2, []);
|
||||
|
||||
$item = $this->cart->get(455);
|
||||
|
||||
expect($item->getPriceSum())->toEqual(201.98, 'Item summed price should be 201.98');
|
||||
});
|
||||
|
||||
test('item get conditions empty', function () {
|
||||
$this->cart->add(455, 'Sample Item', 100.99, 2, []);
|
||||
|
||||
$item = $this->cart->get(455);
|
||||
|
||||
expect($item->getConditions())->toBeEmpty('Item should have no conditions');
|
||||
});
|
||||
|
||||
test('item get conditions with conditions', function () {
|
||||
$itemCondition1 = new \Wearepixel\Cart\CartCondition([
|
||||
'name' => 'SALE 5%',
|
||||
'type' => 'sale',
|
||||
'target' => 'item',
|
||||
'value' => '-5%',
|
||||
]);
|
||||
|
||||
$itemCondition2 = new CartCondition([
|
||||
'name' => 'Item Gift Pack 25.00',
|
||||
'type' => 'promo',
|
||||
'target' => 'item',
|
||||
'value' => '-25',
|
||||
]);
|
||||
|
||||
$this->cart->add(455, 'Sample Item', 100.99, 2, [], [$itemCondition1, $itemCondition2]);
|
||||
|
||||
$item = $this->cart->get(455);
|
||||
|
||||
expect($item->getConditions())->toHaveCount(2, 'Item should have two conditions');
|
||||
});
|
||||
|
||||
test('item associate model', function () {
|
||||
$this->cart->add(455, 'Sample Item', 100.99, 2, [])->associate(MockProduct::class);
|
||||
|
||||
$item = $this->cart->get(455);
|
||||
|
||||
expect($item->associatedModel)->toEqual(MockProduct::class, 'Item assocaited model should be ' . MockProduct::class);
|
||||
});
|
||||
|
||||
test('it will throw an exception when a non existing model is being associated', function () {
|
||||
$this->expectException(\Wearepixel\Cart\Exceptions\UnknownModelException::class);
|
||||
$this->expectExceptionMessage('The supplied model SomeModel does not exist.');
|
||||
|
||||
$this->cart->add(1, 'Test item', 1, 10.00)->associate('SomeModel');
|
||||
});
|
||||
|
||||
test('item get model', function () {
|
||||
$this->cart->add(455, 'Sample Item', 100.99, 2, [])->associate(MockProduct::class);
|
||||
|
||||
$item = $this->cart->get(455);
|
||||
|
||||
expect($item->model)->toBeInstanceOf(MockProduct::class);
|
||||
expect($item->model->name)->toEqual('Sample Item');
|
||||
expect($item->model->id)->toEqual(455);
|
||||
});
|
||||
|
||||
test('item get model will return null if it has no model', function () {
|
||||
$this->cart->add(455, 'Sample Item', 100.99, 2, []);
|
||||
|
||||
$item = $this->cart->get(455);
|
||||
|
||||
expect($item->model)->toEqual(null);
|
||||
});
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
use Wearepixel\Cart\Cart;
|
||||
use Wearepixel\Cart\Tests\Helpers\SessionMock;
|
||||
|
||||
beforeEach(function () {
|
||||
$events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
|
||||
$events->shouldReceive('dispatch');
|
||||
|
||||
$this->cart = new Cart(
|
||||
new SessionMock,
|
||||
$events,
|
||||
'shopping',
|
||||
'SAMPLESESSIONKEY',
|
||||
require (__DIR__ . '/../Helpers/ConfigMockOtherFormat.php')
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
Mockery::close();
|
||||
});
|
||||
|
||||
test('item get sum price using property', function () {
|
||||
$this->cart->add(455, 'Sample Item', 100.99, 2, []);
|
||||
|
||||
$item = $this->cart->get(455);
|
||||
|
||||
expect($item->getPriceSum())->toEqual('201.98', 'Item summed price should be 201.98');
|
||||
});
|
||||
|
||||
test('item get sum price using array style', function () {
|
||||
$this->cart->add(455, 'Sample Item', 100.99, 2, []);
|
||||
|
||||
$item = $this->cart->get(455);
|
||||
|
||||
expect($item->getPriceSum())->toEqual('201.98', 'Item summed price should be 201.98');
|
||||
});
|
||||
Reference in New Issue
Block a user