Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

updates to pricing set code

  • Loading branch information...
commit 5b309d8df37b4a54ea442b5499940a65c6d57d92 1 parent 9b6049f
@iampersistent iampersistent authored
View
17 lib/ImmersiveLabs/Pricing/Entity/Element/TotalValueElement.php
@@ -2,12 +2,27 @@
namespace ImmersiveLabs\Pricing\Entity\Element;
+<<<<<<< HEAD:lib/ImmersiveLabs/Pricing/Entity/Element/TotalValueElement.php
use ImmersiveLabs\Pricing\Entity\PricingElement;
+=======
+use Vespolina\Entity\Pricing\PricingElement;
+use Vespolina\Entity\Pricing\PricingElementValueInterface;
+>>>>>>> updates to pricing set code:lib/Vespolina/Entity/Pricing/Element/TotalValueElement.php
-class TotalValueElement extends PricingElement
+class TotalValueElement extends PricingElement implements PricingElementValueInterface
{
protected $position = 100000;
+ public function add($augend, $addend)
+ {
+ throw new FunctionNotSupportedException('add() has not been implemented in ' . get_class($this));
+ }
+
+ public function subtract($minuend, $subtrahend)
+ {
+ throw new FunctionNotSupportedException('subtract() has not been implemented in ' . get_class($this));
+ }
+
protected function doProcess($context, $processed)
{
$totalValue = $processed['netValue'];
View
13 lib/Vespolina/Entity/Pricing/Element/TotalDoughValueElement.php
@@ -3,11 +3,22 @@
namespace Vespolina\Entity\Pricing\Element;
use Vespolina\Entity\Pricing\PricingElement;
+use Vespolina\Entity\Pricing\PricingElementValueInterface;
-class TotalDoughValueElement extends PricingElement
+class TotalDoughValueElement extends PricingElement implements PricingElementValueInterface
{
protected $position = 100000;
+ public function add($augend, $addend)
+ {
+ return $augend->plus($addend);
+ }
+
+ public function subtract($minuend, $subtrahend)
+ {
+ return $minuend->reduce($subtrahend);
+ }
+
protected function doProcess($context, $processed)
{
$totalValue = $processed['netValue']->reduce($processed['discounts']);
View
40 lib/Vespolina/Entity/Pricing/PricingSet.php
@@ -19,24 +19,26 @@ class PricingSet implements PricingSetInterface
protected $processedProperties;
protected $processedValues;
protected $processingState = self::PROCESSING_UNPROCESSED;
- protected $returns;
+ protected $returnValues;
protected $pricingElements;
+ protected $valueElement;
const PROCESSING_UNPROCESSED = 0;
const PROCESSING_FINISHED = 1;
- public function __construct(array $customReturns = array(), array $globalPricingElements = array())
+ public function __construct(PricingElementValueInterface $valueElement, array $customValues = array(), array $globalPricingElements = array())
{
- $defaultReturns = array(
+ $defaultValues = array(
'discounts', 'netValue', 'surcharge', 'taxes', 'totalValue'
);
- $this->returns = array_merge($defaultReturns, $customReturns);
+ $this->returnValues = array_merge($defaultValues, $customValues);
- foreach ($this->returns as $return) {
+ foreach ($this->returnValues as $return) {
$this->processedValues[$return] = null;
}
$this->addPricingElements($globalPricingElements);
+ $this->valueElement = $valueElement;
}
public function getId()
@@ -86,8 +88,10 @@ public function get($name)
public function set($name, $value)
{
- if (is_object($value)) {
+ if (array_key_exists($name, $this->processedValues) || (is_object($value))) {
$this->processedValues[$name] = $value;
+
+ return $this;
}
$this->processedProperties[$name] = $value;
@@ -116,8 +120,7 @@ public function process($context = null)
$processed = $this->getProcessed();
}
-
- $newSet = new self();
+ $newSet = new self($this->valueElement);
$newSet->setProcessedProperties($processed['properties']);
$newSet->setProcessedValues($processed['values']);
$newSet->setProcessingState(self::PROCESSING_FINISHED);
@@ -126,24 +129,19 @@ public function process($context = null)
return $newSet;
}
- public function plus($addSet)
+ public function add($addSet)
{
- $newSet = new self();
- foreach ($this->getProcessed() as $key => $value) {
- if (!is_scalar($value)) {
- continue;
- }
- $newSet->set($key, $value);
- }
+ $newSet = new self($this->valueElement);
+ $newSet->setProcessedProperties($this->getProcessedProperties());
if ($addSet instanceof PricingSetInterface) {
- foreach ($addSet->getProcessed() as $key => $value) {
- if (!is_scalar($value)) {
- continue;
- }
- $total = $this->get($key) + $value;
+ foreach ($addSet->getProcessedValues() as $key => $value) {
+
+ $total = $this->valueElement->add($this->get($key), $value);
$newSet->set($key, $total);
}
+ } else {
+ $newSet->setProcessedValues($this->getProcessedValues());
}
$newSet->setProcessingState(self::PROCESSING_FINISHED);
View
16 lib/Vespolina/Entity/Pricing/PricingValueElementInterface.php
@@ -0,0 +1,16 @@
+<?php
+/**
+ * (c) 2013 Vespolina Project http://www.vespolina-project.org
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace Vespolina\Entity\Pricing;
+
+interface PricingElementValueInterface extends PricingElementInterface
+{
+ function add($augend, $addend);
+
+ function subtract($minuend, $subtrahend);
+}
View
82 tests/Entity/Pricing/PricingSetTest.php
@@ -7,7 +7,8 @@ class PricingSetTest extends \PHPUnit_Framework_TestCase
{
public function testAddElements()
{
- $pricingSet = new PricingSet();
+ $valueElement = $this->getMock('Vespolina\Entity\Pricing\PricingElementValueInterface');
+ $pricingSet = new PricingSet($valueElement);
$rp = new \ReflectionProperty($pricingSet, 'pricingElements');
$rp->setAccessible(true);
$rp->setValue($pricingSet, null);
@@ -27,9 +28,37 @@ public function testAddElements()
$this->assertCount(4, $pricingSet->getPricingElements());
}
+ public function testSet()
+ {
+ $valueElement = $this->getMock('Vespolina\Entity\Pricing\PricingElementValueInterface');
+ $pricingSet = new PricingSet($valueElement);
+
+ $pricingSet->set('totalValue', 3);
+ $processedValues = $pricingSet->getProcessedValues();
+ $this->assertSame(3, $processedValues['totalValue'], 'a preset value should return regardless of type');
+ if ($pricingSet->getProcessedProperties()) {
+ $this->assertArrayNotHasKey('totalValue', $pricingSet->getProcessedProperties(), 'an value should not be a property');
+ }
+
+ $object = new stdClass();
+ $pricingSet->set('somethingElse', $object);
+ $processedValues = $pricingSet->getProcessedValues();
+ $this->assertSame($object, $processedValues['somethingElse'], 'an object should be set as a value');
+ if ($pricingSet->getProcessedProperties()) {
+ $this->assertArrayNotHasKey('somethingElse', $pricingSet->getProcessedProperties(), 'an object should not be a property');
+ }
+
+ $pricingSet->set('andNowForSomethingCompletelyDifferent', 43);
+ $processedProperties = $pricingSet->getProcessedProperties();
+ $this->assertSame(43, $processedProperties['andNowForSomethingCompletelyDifferent'], 'a scalar should be set as a property');
+ $this->assertArrayNotHasKey('andNowForSomethingCompletelyDifferent', $pricingSet->getProcessedValues(), 'a scalar should not be a value');
+ }
+
public function testGet()
{
- $pricingSet = new PricingSet();
+ $valueElement = $this->getMock('Vespolina\Entity\Pricing\PricingElementValueInterface');
+ $pricingSet = new PricingSet($valueElement);
+
$object = new stdClass();
$pricingSet->setProcessedProperties(array('thisPropertyExists' => 10));
$pricingSet->setProcessedValues(array('thisValueExists' => $object));
@@ -40,9 +69,14 @@ public function testGet()
$this->assertEquals($object, $pricingSet->get('thisValueExists'));
}
- public function testPlus()
+ public function testAdd()
{
- $pricingSet1 = new PricingSet();
+ $valueElement = $this->getMock('Vespolina\Entity\Pricing\PricingElementValueInterface', array('add'));
+ $valueElement
+ ->expects($this->any())
+ ->method('add')
+ ->will($this->returnValue(5));
+ $pricingSet1 = new PricingSet($valueElement);
$pricingSet1->set('discounts', 5);
$pricingSet1->set('netValue', 5);
$pricingSet1->set('surcharge', 5);
@@ -50,16 +84,21 @@ public function testPlus()
$pricingSet1->set('totalValue', 5);
$pricingSet1->setProcessingState(PricingSet::PROCESSING_FINISHED);
- $sumPricingSet = $pricingSet1->plus(null);
+ $sumPricingSet = $pricingSet1->add(null);
$this->assertInstanceOf('Vespolina\Entity\Pricing\PricingSetInterface', $sumPricingSet, 'a pricing set should be returned when nothing is added');
$this->assertNotSame($pricingSet1, $sumPricingSet, 'the new set should be a new object');
- $this->assertEquals('5', $sumPricingSet->get('discounts'), 'the final value should be 10');
- $this->assertEquals('5', $sumPricingSet->get('netValue'), 'the final value should be 10');
- $this->assertEquals('5', $sumPricingSet->get('surcharge'), 'the final value should be 10');
- $this->assertEquals('5', $sumPricingSet->get('taxes'), 'the final value should be 10');
- $this->assertEquals('5', $sumPricingSet->get('totalValue'), 'the final value should be 10');
-
- $pricingSet2 = new PricingSet();
+ $this->assertEquals('5', $sumPricingSet->get('discounts'), 'the final value should be 5');
+ $this->assertEquals('5', $sumPricingSet->get('netValue'), 'the final value should be 5');
+ $this->assertEquals('5', $sumPricingSet->get('surcharge'), 'the final value should be 5');
+ $this->assertEquals('5', $sumPricingSet->get('taxes'), 'the final value should be 5');
+ $this->assertEquals('5', $sumPricingSet->get('totalValue'), 'the final value should be 5');
+
+ $valueElement = $this->getMock('Vespolina\Entity\Pricing\PricingElementValueInterface', array('add'));
+ $valueElement
+ ->expects($this->any())
+ ->method('add')
+ ->will($this->returnValue(10));
+ $pricingSet2 = new PricingSet($valueElement);
$pricingSet2->set('discounts', 5);
$pricingSet2->set('netValue', 5);
$pricingSet2->set('surcharge', 5);
@@ -67,7 +106,7 @@ public function testPlus()
$pricingSet2->set('totalValue', 5);
$pricingSet2->setProcessingState(PricingSet::PROCESSING_FINISHED);
- $sumPricingSet = $pricingSet1->plus($pricingSet2);
+ $sumPricingSet = $pricingSet2->add($pricingSet1);
$this->assertInstanceOf('Vespolina\Entity\Pricing\PricingSetInterface', $sumPricingSet, 'a pricing set should be returned');
$this->assertNotSame($pricingSet1, $sumPricingSet, 'the new set should be a new object');
$this->assertNotSame($pricingSet2, $sumPricingSet, 'the new set should be a new object');
@@ -77,23 +116,13 @@ public function testPlus()
$this->assertEquals('10', $sumPricingSet->get('taxes'), 'the final value should be 10');
$this->assertEquals('10', $sumPricingSet->get('totalValue'), 'the final value should be 10');
- $pricingSet1 = new PricingSet();
- $pricingSet1->set('scalar', 5);
- $pricingSet1->set('object', new PricingSet());
- $pricingSet1->setProcessingState(PricingSet::PROCESSING_FINISHED);
- $sumPricingSet = $pricingSet1->plus(null);
- $this->assertInstanceOf('Vespolina\Entity\Pricing\PricingSetInterface', $sumPricingSet, 'a pricing set should be returned when nothing is added');
- $this->assertNotSame($pricingSet1, $sumPricingSet, 'the new set should be a new object');
- $this->assertEquals('5', $sumPricingSet->get('scalar'), 'a scalar value is set');
- $this->assertNull($sumPricingSet->get('object'), 'a non scalar is skipped');
-
- $this->markTestIncomplete('implement combining mismatched processed elements');
$this->markTestIncomplete('implement inclusions/exclusions from adding process by passing array (either white or black list?)');
}
public function testGetPricingElements()
{
- $pricingSet = new PricingSet();
+ $valueElement = $this->getMock('Vespolina\Entity\Pricing\PricingElementValueInterface');
+ $pricingSet = new PricingSet($valueElement);
$rp = new \ReflectionProperty($pricingSet, 'pricingElements');
$rp->setAccessible(true);
$rp->setValue($pricingSet, null);
@@ -119,7 +148,8 @@ public function testGetPricingElements()
public function testProcess()
{
- $pricingSet = new PricingSet();
+ $valueElement = $this->getMock('Vespolina\Entity\Pricing\PricingElementValueInterface');
+ $pricingSet = new PricingSet($valueElement);
$elementNetValue = $this->getMock('Vespolina\Entity\Pricing\PricingElement', array('process'));
$elementNetValue->expects($this->any())
Please sign in to comment.
Something went wrong with that request. Please try again.