Skip to content

Commit

Permalink
added two new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
darryldecode committed Mar 19, 2015
1 parent cdcc82c commit 4369ebe
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 15 deletions.
4 changes: 1 addition & 3 deletions src/Darryldecode/Cart/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ public function getInstanceName()
*/
public function get($itemId)
{
$contents = $this->getContent();

return $contents->get($itemId);
return $this->getContent()->get($itemId);
}

/**
Expand Down
46 changes: 34 additions & 12 deletions src/Darryldecode/Cart/CartCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class CartCondition {
*/
private $args;

/**
* the parsed raw value of the condition
*
* @var
*/
private $parsedRawValue;

/**
* @param array $args (name, type, target, value)
* @throws InvalidConditionException
Expand Down Expand Up @@ -86,6 +93,19 @@ public function applyCondition($totalOrSubTotalOrPrice)
return $this->apply($totalOrSubTotalOrPrice, $this->getValue());
}

/**
* get the calculated value of this condition supplied by the subtotal|price
*
* @param $totalOrSubTotalOrPrice
* @return mixed
*/
public function getCalculatedValue($totalOrSubTotalOrPrice)
{
$this->apply($totalOrSubTotalOrPrice, $this->getValue());

return $this->parsedRawValue;
}

/**
* apply condition
*
Expand All @@ -106,25 +126,25 @@ protected function apply($totalOrSubTotalOrPrice, $conditionValue)
{
$value = Helpers::normalizePrice( $this->cleanValue($conditionValue) );

$valueToBeSubtracted = $totalOrSubTotalOrPrice * ($value / 100);
$this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100);

return floatval($totalOrSubTotalOrPrice - $valueToBeSubtracted);
$result = floatval($totalOrSubTotalOrPrice - $this->parsedRawValue);
}
else if ( $this->valueIsToBeAdded($conditionValue) )
{
$value = Helpers::normalizePrice( $this->cleanValue($conditionValue) );

$valueToBeSubtracted = $totalOrSubTotalOrPrice * ($value / 100);
$this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100);

return floatval($totalOrSubTotalOrPrice - $valueToBeSubtracted);
$result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue);
}
else
{
$value = Helpers::normalizePrice($conditionValue);

$valueToBeSubtracted = $totalOrSubTotalOrPrice * ($value / 100);
$this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100);

return floatval($totalOrSubTotalOrPrice + $valueToBeSubtracted);
$result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue);
}
}

Expand All @@ -134,23 +154,25 @@ protected function apply($totalOrSubTotalOrPrice, $conditionValue)
{
if( $this->valueIsToBeSubtracted($conditionValue) )
{
$value = Helpers::normalizePrice( $this->cleanValue($conditionValue) );
$this->parsedRawValue = Helpers::normalizePrice( $this->cleanValue($conditionValue) );

return floatval($totalOrSubTotalOrPrice - $value);
$result = floatval($totalOrSubTotalOrPrice - $this->parsedRawValue);
}
else if ( $this->valueIsToBeAdded($conditionValue) )
{
$value = Helpers::normalizePrice( $this->cleanValue($conditionValue) );
$this->parsedRawValue = Helpers::normalizePrice( $this->cleanValue($conditionValue) );

return floatval($totalOrSubTotalOrPrice + $value);
$result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue);
}
else
{
$value = Helpers::normalizePrice($conditionValue);
$this->parsedRawValue = Helpers::normalizePrice($conditionValue);

return floatval($totalOrSubTotalOrPrice + $value);
$result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue);
}
}

return $result;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Darryldecode/Cart/ItemCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@

class ItemCollection extends Collection {

/**
* get the sum of price
*
* @return mixed|null
*/
public function getPriceSum()
{
return $this->price * $this->quantity;
}

public function __get($name)
{
if( $this->has($name) ) return $this->get($name);
Expand Down
43 changes: 43 additions & 0 deletions tests/CartConditionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,49 @@ public function test_clear_cart_conditions()
$this->assertEquals(0, $this->cart->getConditions()->count(), 'Cart should have no conditions now');
}

public function test_get_calculated_value_of_a_condition()
{
$cartCondition1 = new CartCondition(array(
'name' => 'SALE 5%',
'type' => 'sale',
'target' => 'subtotal',
'value' => '-5%',
));
$cartCondition2 = new CartCondition(array(
'name' => 'Item Gift Pack 25.00',
'type' => 'promo',
'target' => 'subtotal',
'value' => '-25',
));

$item = array(
'id' => 456,
'name' => 'Sample Item 1',
'price' => 100,
'quantity' => 1,
'attributes' => array(),
);

$this->cart->add($item);

$this->cart->condition([$cartCondition1, $cartCondition2]);

$subTotal = $this->cart->getSubTotal();

$this->assertEquals(100, $subTotal, 'Subtotal should be 100');

// way 1
// now we will get the calculated value of the condition 1
$cond1 = $this->cart->getCondition('SALE 5%');
$this->assertEquals(5,$cond1->getCalculatedValue($subTotal), 'The calculated value must be 5');

// way 2
// get all cart conditions and get their calculated values
$conditions = $this->cart->getConditions();
$this->assertEquals(5, $conditions['SALE 5%']->getCalculatedValue($subTotal),'First condition calculated value must be 5');
$this->assertEquals(25, $conditions['Item Gift Pack 25.00']->getCalculatedValue($subTotal),'First condition calculated value must be 5');
}

protected function fillCart()
{
$items = array(
Expand Down
57 changes: 57 additions & 0 deletions tests/ItemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Created by PhpStorm.
* User: darryl
* Date: 3/18/2015
* Time: 6:17 PM
*/

use Darryldecode\Cart\Cart;
use Mockery as m;

require_once __DIR__.'/helpers/SessionMock.php';

class ItemTest extends PHPUnit_Framework_TestCase
{

/**
* @var Darryldecode\Cart\Cart
*/
protected $cart;

public function setUp()
{
$events = m::mock('Illuminate\Contracts\Events\Dispatcher');
$events->shouldReceive('fire');

$this->cart = new Cart(
new SessionMock(),
$events,
'shopping',
'SAMPLESESSIONKEY'
);
}

public function tearDown()
{
m::close();
}

public function test_item_get_sum_price_using_property()
{
$this->cart->add(455, 'Sample Item', 100.99, 2, array());

$item = $this->cart->get(455);

$this->assertEquals(201.98, $item->getPriceSum(), 'Item summed price should be 201.98');
}

public function test_item_get_sum_price_using_array_style()
{
$this->cart->add(455, 'Sample Item', 100.99, 2, array());

$item = $this->cart->get(455);

$this->assertEquals(201.98, $item->getPriceSum(), 'Item summed price should be 201.98');
}
}

0 comments on commit 4369ebe

Please sign in to comment.