Permalink
Browse files

pass global Elements and related fixes

  • Loading branch information...
1 parent 6ece735 commit e9fdf7d3b9db3507dc62cb89c9bc6949da15db7a @iampersistent iampersistent committed Mar 9, 2013
@@ -13,7 +13,8 @@
class PricingElement implements PricingElementInterface
{
protected $attributes;
- protected $position;
+ protected $position = 0;
+ protected $pricingSet;
protected $type;
public function __construct()
@@ -16,7 +16,44 @@ class PricingSet implements PricingSetInterface
{
protected $pricingElements;
- public function __construct()
+ const PROCESSING_UNPROCESSED = 0;
+ const PROCESSING_FINISHED = 1;
+
+ public function __construct(array $customReturns = array(), array $globalPricingElements = array())
+ {
+ $defaultReturns = array(
+ 'discounts', 'netValue', 'surcharge', 'taxes', 'totalValue'
+ );
+ $this->returns = array_merge($defaultReturns, $customReturns);
+
+ foreach ($this->returns as $return) {
+ $this->processed[$return] = null;
+ }
+
+ $this->addPricingElements($globalPricingElements);
+ }
+
+ public function getId()
+ {
+ return $this->id;
+ }
+
+ public function getDiscounts()
+ {
+ return $this->get('discounts');
+ }
+
+ public function getNetValue()
+ {
+ return $this->get('netValue');
+ }
+
+ public function getSurcharge()
+ {
+ return $this->get('surcharge');
+ }
+
+ public function getTaxes()
{
$this->pricingElements = array();
}
@@ -42,7 +79,29 @@ public function set($name, $value)
$this->pricingElements[$name] = $value;
}
- public function setAll($pricingElements)
+ public function getContext()
+ {
+ return $this->context;
+ }
+
+ public function addPricingElement(PricingElementInterface $element)
+ {
+ $this->pricingElements[] = $element;
+ $element->setPricingSet($this);
+
+ return $this;
+ }
+
+ public function addPricingElements(array $elements)
+ {
+ foreach ($elements as $element) {
+ $this->addPricingElement($element);
+ }
+
+ return $this;
+ }
+
+ public function setPricingElements($pricingElements)
{
$this->pricingElements = $pricingElements;
}
@@ -52,7 +111,40 @@ public function setAll($pricingElements)
*/
public function all()
{
- return $this->pricingElements;
+ if (!$this->pricingElements) {
+ return array();
+ }
+ $elements = $this->pricingElements;
+ $returnElements = array();
+ foreach ($elements as $element) {
+ $position = $element->getPosition();
+ while (isset($returnElements[$position])) {
+ $position ++;
+ }
+ $returnElements[$position] = $element;
+ }
+ ksort($returnElements);
+
+ return $returnElements;
+ }
+
+ public function setProcessed($processed)
+ {
+ $this->processed = $processed;
+
+ return $this;
+ }
+
+ public function getProcessed()
+ {
+ return $this->processed;
+ }
+
+ public function setProcessingState($processingState)
+ {
+ $this->processingState = $processingState;
+
+ return $this;
}
function getNetValue()
@@ -8,18 +8,49 @@ class PricingSetTest extends \PHPUnit_Framework_TestCase
public function testAddElements()
{
$pricingSet = new PricingSet();
- $pricingElementsCount = count($pricingSet->getPricingElements());
-
- $this->assertGreaterThan(0, $pricingElementsCount, 'there should be at least one default element');
+ $rp = new \ReflectionProperty($pricingSet, 'pricingElements');
+ $rp->setAccessible(true);
+ $rp->setValue($pricingSet, null);
$element1 = new PricingElement();
$pricingSet->addPricingElement($element1);
- $pricingElementsCount++;
- $this->assertEquals($pricingElementsCount, count($pricingSet->getPricingElements()));
+ $this->assertCount(1, $pricingSet->getPricingElements());
+ $this->assertSame($pricingSet, $element1->getPricingSet());
$element2 = new PricingElement();
$pricingSet->addPricingElement($element2);
- $this->assertEquals($pricingElementsCount, count($pricingSet->getPricingElements()), 'should be still two');
+ $this->assertCount(2, $pricingSet->getPricingElements());
+
+ $elements[] = new PricingElement();
+ $elements[] = new PricingElement();
+ $pricingSet->addPricingElements($elements);
+ $this->assertCount(4, $pricingSet->getPricingElements());
+ }
+
+ public function testGetPricingElements()
+ {
+ $pricingSet = new PricingSet();
+ $rp = new \ReflectionProperty($pricingSet, 'pricingElements');
+ $rp->setAccessible(true);
+ $rp->setValue($pricingSet, null);
+
+ $element1 = new PricingElement();
+ $element1->setPosition(100);
+ $element2 = new PricingElement();
+ $element2->setPosition(1000);
+ $element3 = new PricingElement();
+ $element4 = new PricingElement();
+ $elements[] = $element1;
+ $elements[] = $element2;
+ $elements[] = $element3;
+ $elements[] = $element4;
+ $pricingSet->addPricingElements($elements);
+ $pricingElements = $pricingSet->getPricingElements();
+ $this->assertCount(4, $pricingElements);
+ $this->assertArrayHasKey(0, $pricingElements);
+ $this->assertArrayHasKey(1, $pricingElements);
+ $this->assertArrayHasKey(100, $pricingElements);
+ $this->assertArrayHasKey(1000, $pricingElements);
}
public function testProcess()

0 comments on commit e9fdf7d

Please sign in to comment.