Skip to content

Commit 8edf8c0

Browse files
authored
Merge branch 'master' into master
2 parents 43af360 + 417a524 commit 8edf8c0

12 files changed

+86
-38
lines changed

.phpunit.result.cache

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
C:37:"PHPUnit\Runner\DefaultTestResultCache":4952:{a:2:{s:7:"defects";a:7:{s:71:"CartConditionTest::test_add_cart_condition_without_condition_attributes";i:6;s:68:"CartConditionTest::test_add_cart_condition_with_condition_attributes";i:6;s:65:"CartTest::test_item_price_should_be_normalized_when_added_to_cart";i:6;s:79:"CartTest::test_should_throw_exception_when_provided_invalid_values_scenario_one";i:6;s:79:"CartTest::test_should_throw_exception_when_provided_invalid_values_scenario_two";i:6;s:81:"CartTest::test_should_throw_exception_when_provided_invalid_values_scenario_three";i:4;s:38:"CartTest::test_cart_get_total_quantity";i:6;}s:5:"times";a:61:{s:32:"CartConditionTest::test_subtotal";d:0.025;s:47:"CartConditionTest::test_total_without_condition";d:0.001;s:44:"CartConditionTest::test_total_with_condition";d:0.001;s:73:"CartConditionTest::test_total_with_multiple_conditions_added_scenario_one";d:0.001;s:73:"CartConditionTest::test_total_with_multiple_conditions_added_scenario_two";d:0.001;s:75:"CartConditionTest::test_total_with_multiple_conditions_added_scenario_three";d:0.001;s:75:"CartConditionTest::test_cart_multiple_conditions_can_be_added_once_by_array";d:0.001;s:74:"CartConditionTest::test_total_with_multiple_conditions_added_scenario_four";d:0.001;s:47:"CartConditionTest::test_add_item_with_condition";d:0;s:93:"CartConditionTest::test_add_item_with_multiple_item_conditions_in_multiple_condition_instance";d:0;s:82:"CartConditionTest::test_add_item_with_multiple_item_conditions_with_target_omitted";d:0;s:42:"CartConditionTest::test_add_item_condition";d:0.001;s:66:"CartConditionTest::test_add_item_condition_restrict_negative_price";d:0;s:60:"CartConditionTest::test_get_cart_condition_by_condition_name";d:0;s:63:"CartConditionTest::test_remove_cart_condition_by_condition_name";d:0;s:63:"CartConditionTest::test_remove_item_condition_by_condition_name";d:0;s:76:"CartConditionTest::test_remove_item_condition_by_condition_name_scenario_two";d:0.001;s:45:"CartConditionTest::test_clear_item_conditions";d:0;s:45:"CartConditionTest::test_clear_cart_conditions";d:0;s:59:"CartConditionTest::test_get_calculated_value_of_a_condition";d:0;s:46:"CartConditionTest::test_get_conditions_by_type";d:0;s:49:"CartConditionTest::test_remove_conditions_by_type";d:0;s:71:"CartConditionTest::test_add_cart_condition_without_condition_attributes";d:0.001;s:68:"CartConditionTest::test_add_cart_condition_with_condition_attributes";d:0.001;s:48:"CartConditionTest::test_get_order_from_condition";d:0;s:42:"CartConditionTest::test_condition_ordering";d:0.001;s:32:"CartTest::test_cart_can_add_item";d:0.001;s:42:"CartTest::test_cart_can_add_items_as_array";d:0;s:61:"CartTest::test_cart_can_add_items_with_multidimensional_array";d:0.001;s:51:"CartTest::test_cart_can_add_item_without_attributes";d:0;s:109:"CartTest::test_cart_update_with_attribute_then_attributes_should_be_still_instance_of_ItemAttributeCollection";d:0.001;s:36:"CartTest::test_cart_items_attributes";d:0;s:40:"CartTest::test_cart_update_existing_item";d:0;s:80:"CartTest::test_cart_update_existing_item_with_quantity_as_array_and_not_relative";d:0;s:65:"CartTest::test_item_price_should_be_normalized_when_added_to_cart";d:0;s:52:"CartTest::test_it_removes_an_item_on_cart_by_item_id";d:0.001;s:29:"CartTest::test_cart_sub_total";d:0.001;s:54:"CartTest::test_sub_total_when_item_quantity_is_updated";d:0.001;s:65:"CartTest::test_sub_total_when_item_quantity_is_updated_by_reduced";d:0;s:96:"CartTest::test_item_quantity_update_by_reduced_should_not_reduce_if_quantity_will_result_to_zero";d:0;s:79:"CartTest::test_should_throw_exception_when_provided_invalid_values_scenario_one";d:0.001;s:79:"CartTest::test_should_throw_exception_when_provided_invalid_values_scenario_two";d:0;s:81:"CartTest::test_should_throw_exception_when_provided_invalid_values_scenario_three";d:0;s:28:"CartTest::test_clearing_cart";d:0;s:38:"CartTest::test_cart_get_total_quantity";d:0;s:39:"CartTestEvents::test_event_cart_created";d:0.001;s:38:"CartTestEvents::test_event_cart_adding";d:0.002;s:53:"CartTestEvents::test_event_cart_adding_multiple_times";d:0.001;s:66:"CartTestEvents::test_event_cart_adding_multiple_times_scenario_two";d:0.001;s:43:"CartTestEvents::test_event_cart_remove_item";d:0.001;s:37:"CartTestEvents::test_event_cart_clear";d:0.001;s:55:"CartTestMultipleInstances::test_cart_multiple_instances";d:0.001;s:40:"CartTestOtherFormat::test_cart_sub_total";d:0.001;s:65:"CartTestOtherFormat::test_sub_total_when_item_quantity_is_updated";d:0;s:76:"CartTestOtherFormat::test_sub_total_when_item_quantity_is_updated_by_reduced";d:0;s:48:"ItemTest::test_item_get_sum_price_using_property";d:0;s:51:"ItemTest::test_item_get_sum_price_using_array_style";d:0;s:40:"ItemTest::test_item_get_conditions_empty";d:0;s:50:"ItemTest::test_item_get_conditions_with_conditions";d:0;s:59:"ItemTestOtherFormat::test_item_get_sum_price_using_property";d:0;s:62:"ItemTestOtherFormat::test_item_get_sum_price_using_array_style";d:0;}}}

.travis.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
language: php
22

33
php:
4-
- 5.6
5-
- 7.0
6-
- 7.1
4+
- 7.2
5+
- 7.3
6+
- 7.4
77

88
before_script:
99
- travis_retry composer self-update

README.md

+54-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# Laravel 5 Shopping Cart
2-
1+
# Laravel 5 & 6 Shopping Cart
32
[![Build Status](https://travis-ci.org/darryldecode/laravelshoppingcart.svg?branch=master)](https://travis-ci.org/darryldecode/laravelshoppingcart)
43
[![Total Downloads](https://poser.pugx.org/darryldecode/cart/d/total.svg)](https://packagist.org/packages/darryldecode/cart)
54
[![License](https://poser.pugx.org/darryldecode/cart/license.svg)](https://packagist.org/packages/darryldecode/cart)
@@ -20,7 +19,9 @@ For Laravel 5.1~:
2019
`composer require "darryldecode/cart:~2.0"`
2120

2221
For Laravel 5.5, 5.6 or 5.7~:
23-
`composer require "darryldecode/cart:~4.0"`
22+
23+
```composer require "darryldecode/cart:~4.0"``` or
24+
```composer require "darryldecode/cart"```
2425

2526
## CONFIGURATION
2627

@@ -1057,6 +1058,56 @@ class DBStorage {
10571058
}
10581059
```
10591060

1061+
For example you can also leverage Laravel's Caching (redis, memcached, file, dynamo, etc) using the example below. Exmaple also includes cookie persistance, so that cart would be still available for 30 days. Sessions by default persists only 20 minutes.
1062+
1063+
```
1064+
<?php
1065+
1066+
namespace App\Cart;
1067+
1068+
use Carbon\Carbon;
1069+
use Cookie;
1070+
use Darryldecode\Cart\CartCollection;
1071+
1072+
class CacheStorage
1073+
{
1074+
private $data = [];
1075+
private $cart_id;
1076+
1077+
public function __construct()
1078+
{
1079+
$this->cart_id = \Cookie::get('cart');
1080+
if ($this->cart_id) {
1081+
$this->data = \Cache::get('cart_' . $this->cart_id, []);
1082+
} else {
1083+
$this->cart_id = uniqid();
1084+
}
1085+
}
1086+
1087+
public function has($key)
1088+
{
1089+
return isset($this->data[$key]);
1090+
}
1091+
1092+
public function get($key)
1093+
{
1094+
return new CartCollection($this->data[$key] ?? []);
1095+
}
1096+
1097+
public function put($key, $value)
1098+
{
1099+
$this->data[$key] = $value;
1100+
\Cache::put('cart_' . $this->cart_id, $this->data, Carbon::now()->addDays(30));
1101+
1102+
if (!Cookie::hasQueued('cart')) {
1103+
Cookie::queue(
1104+
Cookie::make('cart', $this->cart_id, 60 * 24 * 30)
1105+
);
1106+
}
1107+
}
1108+
}
1109+
```
1110+
10601111
To make this the cart's default storage, let's update the cart's configuration file.
10611112
First, let us publish first the cart config file for us to enable to override it.
10621113
`php artisan vendor:publish --provider="Darryldecode\Cart\CartServiceProvider" --tag="config"`

composer.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
}
1111
],
1212
"require": {
13-
"php": ">=5.4.0",
14-
"illuminate/support": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*",
15-
"illuminate/validation": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*",
16-
"illuminate/translation": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*"
13+
"php": ">=7.1",
14+
"illuminate/support": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0",
15+
"illuminate/validation": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0",
16+
"illuminate/translation": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0"
1717
},
1818
"require-dev": {
1919
"mockery/mockery": "^1.0",
20-
"phpunit/phpunit": "^5.0",
20+
"phpunit/phpunit": "^8.0.0",
2121
"symfony/var-dumper": "2.7.*@dev"
2222
},
2323
"autoload": {

src/Darryldecode/Cart/ItemCollection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ItemCollection extends Collection
2525
* @param array|mixed $items
2626
* @param $config
2727
*/
28-
public function __construct($items, $config)
28+
public function __construct($items, $config = [])
2929
{
3030
parent::__construct($items);
3131

tests/CartConditionsTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class CartConditionTest extends PHPUnit\Framework\TestCase {
1919
*/
2020
protected $cart;
2121

22-
public function setUp()
22+
public function setUp(): void
2323
{
2424
$events = m::mock('Illuminate\Contracts\Events\Dispatcher');
2525
$events->shouldReceive('dispatch');
@@ -33,7 +33,7 @@ public function setUp()
3333
);
3434
}
3535

36-
public function tearDown()
36+
public function tearDown(): void
3737
{
3838
m::close();
3939
}
@@ -770,7 +770,7 @@ public function test_add_cart_condition_without_condition_attributes()
770770
// when get attribute is called and there is no attributes added,
771771
// it should return an empty array
772772
$conditionAttribute = $contition->getAttributes();
773-
$this->assertInternalType('array', $conditionAttribute);
773+
$this->assertIsArray($conditionAttribute);
774774
}
775775

776776
public function test_add_cart_condition_with_condition_attributes()
@@ -806,7 +806,7 @@ public function test_add_cart_condition_with_condition_attributes()
806806
// when get attribute is called and there is no attributes added,
807807
// it should return an empty array
808808
$conditionAttributes = $contition->getAttributes();
809-
$this->assertInternalType('array', $conditionAttributes);
809+
$this->assertIsArray($conditionAttributes);
810810
$this->assertArrayHasKey('description',$conditionAttributes);
811811
$this->assertArrayHasKey('sale_start_date',$conditionAttributes);
812812
$this->assertArrayHasKey('sale_end_date',$conditionAttributes);

tests/CartTest.php

+8-12
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class CartTest extends PHPUnit\Framework\TestCase
2020
*/
2121
protected $cart;
2222

23-
public function setUp()
23+
public function setUp(): void
2424
{
2525
$events = m::mock('Illuminate\Contracts\Events\Dispatcher');
2626
$events->shouldReceive('dispatch');
@@ -34,7 +34,7 @@ public function setUp()
3434
);
3535
}
3636

37-
public function tearDown()
37+
public function tearDown(): void
3838
{
3939
m::close();
4040
}
@@ -244,7 +244,7 @@ public function test_item_price_should_be_normalized_when_added_to_cart()
244244
// add a price in a string format should be converted to float
245245
$this->cart->add(455, 'Sample Item', '100.99', 2, array());
246246

247-
$this->assertInternalType('float', $this->cart->getContent()->first()['price'], 'Cart price should be a float');
247+
$this->assertIsFloat($this->cart->getContent()->first()['price'], 'Cart price should be a float');
248248
}
249249

250250
public function test_it_removes_an_item_on_cart_by_item_id()
@@ -415,27 +415,21 @@ public function test_item_quantity_update_by_reduced_should_not_reduce_if_quanti
415415
$this->assertEquals(3, $item['quantity'], 'Item quantity of with item ID of 456 should now be reduced to 2');
416416
}
417417

418-
/**
419-
* @expectedException Darryldecode\Cart\Exceptions\InvalidItemException
420-
*/
421418
public function test_should_throw_exception_when_provided_invalid_values_scenario_one()
422419
{
420+
$this->expectException('Darryldecode\Cart\Exceptions\InvalidItemException');
423421
$this->cart->add(455, 'Sample Item', 100.99, 0, array());
424422
}
425423

426-
/**
427-
* @expectedException Darryldecode\Cart\Exceptions\InvalidItemException
428-
*/
429424
public function test_should_throw_exception_when_provided_invalid_values_scenario_two()
430425
{
426+
$this->expectException('Darryldecode\Cart\Exceptions\InvalidItemException');
431427
$this->cart->add('', 'Sample Item', 100.99, 2, array());
432428
}
433429

434-
/**
435-
* @expectedException Darryldecode\Cart\Exceptions\InvalidItemException
436-
*/
437430
public function test_should_throw_exception_when_provided_invalid_values_scenario_three()
438431
{
432+
$this->expectException('Darryldecode\Cart\Exceptions\InvalidItemException');
439433
$this->cart->add(523, '', 100.99, 2, array());
440434
}
441435

@@ -556,5 +550,7 @@ public function test_cart_can_add_items_with_multidimensional_array_with_associa
556550

557551
$this->assertFalse($this->cart->isEmpty(), 'Cart should not be empty');
558552
$this->assertCount(3, $this->cart->getContent()->toArray(), 'Cart should have 3 items');
553+
$this->assertIsInt($this->cart->getTotalQuantity(), 'Return type should be INT');
554+
$this->assertEquals(4, $this->cart->getTotalQuantity(),'Cart\'s quantity should be 4.');
559555
}
560556
}

tests/CartTestEvents.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ class CartTestEvents extends PHPUnit\Framework\TestCase {
1515

1616
const CART_INSTANCE_NAME = 'shopping';
1717

18-
public function setUp()
18+
public function setUp(): void
1919
{
2020
}
2121

22-
public function tearDown()
22+
public function tearDown(): void
2323
{
2424
m::close();
2525
}

tests/CartTestMultipleInstances.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CartTestMultipleInstances extends PHPUnit\Framework\TestCase {
2323
*/
2424
protected $cart2;
2525

26-
public function setUp()
26+
public function setUp(): void
2727
{
2828
$events = m::mock('Illuminate\Contracts\Events\Dispatcher');
2929
$events->shouldReceive('dispatch');
@@ -45,7 +45,7 @@ public function setUp()
4545
);
4646
}
4747

48-
public function tearDown()
48+
public function tearDown(): void
4949
{
5050
m::close();
5151
}

tests/CartTestOtherFormat.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class CartTestOtherFormat extends PHPUnit\Framework\TestCase {
1818
*/
1919
protected $cart;
2020

21-
public function setUp()
21+
public function setUp(): void
2222
{
2323
$events = m::mock('Illuminate\Contracts\Events\Dispatcher');
2424
$events->shouldReceive('dispatch');
@@ -32,7 +32,7 @@ public function setUp()
3232
);
3333
}
3434

35-
public function tearDown()
35+
public function tearDown(): void
3636
{
3737
m::close();
3838
}

tests/ItemTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ItemTest extends PHPUnit\Framework\TestCase
2121
*/
2222
protected $cart;
2323

24-
public function setUp()
24+
public function setUp(): void
2525
{
2626
$events = m::mock('Illuminate\Contracts\Events\Dispatcher');
2727
$events->shouldReceive('dispatch');
@@ -35,7 +35,7 @@ public function setUp()
3535
);
3636
}
3737

38-
public function tearDown()
38+
public function tearDown(): void
3939
{
4040
m::close();
4141
}

tests/ItemTestOtherFormat.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ItemTestOtherFormat extends PHPUnit\Framework\TestCase
1919
*/
2020
protected $cart;
2121

22-
public function setUp()
22+
public function setUp(): void
2323
{
2424
$events = m::mock('Illuminate\Contracts\Events\Dispatcher');
2525
$events->shouldReceive('dispatch');
@@ -33,7 +33,7 @@ public function setUp()
3333
);
3434
}
3535

36-
public function tearDown()
36+
public function tearDown(): void
3737
{
3838
m::close();
3939
}

0 commit comments

Comments
 (0)