Skip to content

Commit 4369ebe

Browse files
committed
added two new methods
1 parent cdcc82c commit 4369ebe

File tree

5 files changed

+145
-15
lines changed

5 files changed

+145
-15
lines changed

src/Darryldecode/Cart/Cart.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ public function getInstanceName()
8282
*/
8383
public function get($itemId)
8484
{
85-
$contents = $this->getContent();
86-
87-
return $contents->get($itemId);
85+
return $this->getContent()->get($itemId);
8886
}
8987

9088
/**

src/Darryldecode/Cart/CartCondition.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ class CartCondition {
1717
*/
1818
private $args;
1919

20+
/**
21+
* the parsed raw value of the condition
22+
*
23+
* @var
24+
*/
25+
private $parsedRawValue;
26+
2027
/**
2128
* @param array $args (name, type, target, value)
2229
* @throws InvalidConditionException
@@ -86,6 +93,19 @@ public function applyCondition($totalOrSubTotalOrPrice)
8693
return $this->apply($totalOrSubTotalOrPrice, $this->getValue());
8794
}
8895

96+
/**
97+
* get the calculated value of this condition supplied by the subtotal|price
98+
*
99+
* @param $totalOrSubTotalOrPrice
100+
* @return mixed
101+
*/
102+
public function getCalculatedValue($totalOrSubTotalOrPrice)
103+
{
104+
$this->apply($totalOrSubTotalOrPrice, $this->getValue());
105+
106+
return $this->parsedRawValue;
107+
}
108+
89109
/**
90110
* apply condition
91111
*
@@ -106,25 +126,25 @@ protected function apply($totalOrSubTotalOrPrice, $conditionValue)
106126
{
107127
$value = Helpers::normalizePrice( $this->cleanValue($conditionValue) );
108128

109-
$valueToBeSubtracted = $totalOrSubTotalOrPrice * ($value / 100);
129+
$this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100);
110130

111-
return floatval($totalOrSubTotalOrPrice - $valueToBeSubtracted);
131+
$result = floatval($totalOrSubTotalOrPrice - $this->parsedRawValue);
112132
}
113133
else if ( $this->valueIsToBeAdded($conditionValue) )
114134
{
115135
$value = Helpers::normalizePrice( $this->cleanValue($conditionValue) );
116136

117-
$valueToBeSubtracted = $totalOrSubTotalOrPrice * ($value / 100);
137+
$this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100);
118138

119-
return floatval($totalOrSubTotalOrPrice - $valueToBeSubtracted);
139+
$result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue);
120140
}
121141
else
122142
{
123143
$value = Helpers::normalizePrice($conditionValue);
124144

125-
$valueToBeSubtracted = $totalOrSubTotalOrPrice * ($value / 100);
145+
$this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100);
126146

127-
return floatval($totalOrSubTotalOrPrice + $valueToBeSubtracted);
147+
$result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue);
128148
}
129149
}
130150

@@ -134,23 +154,25 @@ protected function apply($totalOrSubTotalOrPrice, $conditionValue)
134154
{
135155
if( $this->valueIsToBeSubtracted($conditionValue) )
136156
{
137-
$value = Helpers::normalizePrice( $this->cleanValue($conditionValue) );
157+
$this->parsedRawValue = Helpers::normalizePrice( $this->cleanValue($conditionValue) );
138158

139-
return floatval($totalOrSubTotalOrPrice - $value);
159+
$result = floatval($totalOrSubTotalOrPrice - $this->parsedRawValue);
140160
}
141161
else if ( $this->valueIsToBeAdded($conditionValue) )
142162
{
143-
$value = Helpers::normalizePrice( $this->cleanValue($conditionValue) );
163+
$this->parsedRawValue = Helpers::normalizePrice( $this->cleanValue($conditionValue) );
144164

145-
return floatval($totalOrSubTotalOrPrice + $value);
165+
$result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue);
146166
}
147167
else
148168
{
149-
$value = Helpers::normalizePrice($conditionValue);
169+
$this->parsedRawValue = Helpers::normalizePrice($conditionValue);
150170

151-
return floatval($totalOrSubTotalOrPrice + $value);
171+
$result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue);
152172
}
153173
}
174+
175+
return $result;
154176
}
155177

156178
/**

src/Darryldecode/Cart/ItemCollection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111

1212
class ItemCollection extends Collection {
1313

14+
/**
15+
* get the sum of price
16+
*
17+
* @return mixed|null
18+
*/
19+
public function getPriceSum()
20+
{
21+
return $this->price * $this->quantity;
22+
}
23+
1424
public function __get($name)
1525
{
1626
if( $this->has($name) ) return $this->get($name);

tests/CartConditionsTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,49 @@ public function test_clear_cart_conditions()
478478
$this->assertEquals(0, $this->cart->getConditions()->count(), 'Cart should have no conditions now');
479479
}
480480

481+
public function test_get_calculated_value_of_a_condition()
482+
{
483+
$cartCondition1 = new CartCondition(array(
484+
'name' => 'SALE 5%',
485+
'type' => 'sale',
486+
'target' => 'subtotal',
487+
'value' => '-5%',
488+
));
489+
$cartCondition2 = new CartCondition(array(
490+
'name' => 'Item Gift Pack 25.00',
491+
'type' => 'promo',
492+
'target' => 'subtotal',
493+
'value' => '-25',
494+
));
495+
496+
$item = array(
497+
'id' => 456,
498+
'name' => 'Sample Item 1',
499+
'price' => 100,
500+
'quantity' => 1,
501+
'attributes' => array(),
502+
);
503+
504+
$this->cart->add($item);
505+
506+
$this->cart->condition([$cartCondition1, $cartCondition2]);
507+
508+
$subTotal = $this->cart->getSubTotal();
509+
510+
$this->assertEquals(100, $subTotal, 'Subtotal should be 100');
511+
512+
// way 1
513+
// now we will get the calculated value of the condition 1
514+
$cond1 = $this->cart->getCondition('SALE 5%');
515+
$this->assertEquals(5,$cond1->getCalculatedValue($subTotal), 'The calculated value must be 5');
516+
517+
// way 2
518+
// get all cart conditions and get their calculated values
519+
$conditions = $this->cart->getConditions();
520+
$this->assertEquals(5, $conditions['SALE 5%']->getCalculatedValue($subTotal),'First condition calculated value must be 5');
521+
$this->assertEquals(25, $conditions['Item Gift Pack 25.00']->getCalculatedValue($subTotal),'First condition calculated value must be 5');
522+
}
523+
481524
protected function fillCart()
482525
{
483526
$items = array(

tests/ItemTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: darryl
5+
* Date: 3/18/2015
6+
* Time: 6:17 PM
7+
*/
8+
9+
use Darryldecode\Cart\Cart;
10+
use Mockery as m;
11+
12+
require_once __DIR__.'/helpers/SessionMock.php';
13+
14+
class ItemTest extends PHPUnit_Framework_TestCase
15+
{
16+
17+
/**
18+
* @var Darryldecode\Cart\Cart
19+
*/
20+
protected $cart;
21+
22+
public function setUp()
23+
{
24+
$events = m::mock('Illuminate\Contracts\Events\Dispatcher');
25+
$events->shouldReceive('fire');
26+
27+
$this->cart = new Cart(
28+
new SessionMock(),
29+
$events,
30+
'shopping',
31+
'SAMPLESESSIONKEY'
32+
);
33+
}
34+
35+
public function tearDown()
36+
{
37+
m::close();
38+
}
39+
40+
public function test_item_get_sum_price_using_property()
41+
{
42+
$this->cart->add(455, 'Sample Item', 100.99, 2, array());
43+
44+
$item = $this->cart->get(455);
45+
46+
$this->assertEquals(201.98, $item->getPriceSum(), 'Item summed price should be 201.98');
47+
}
48+
49+
public function test_item_get_sum_price_using_array_style()
50+
{
51+
$this->cart->add(455, 'Sample Item', 100.99, 2, array());
52+
53+
$item = $this->cart->get(455);
54+
55+
$this->assertEquals(201.98, $item->getPriceSum(), 'Item summed price should be 201.98');
56+
}
57+
}

0 commit comments

Comments
 (0)