Permalink
Browse files

get the tests for pricing sets working

  • Loading branch information...
1 parent 334bfcb commit 4a35b06c55ea24f03f27167f621a116a3e9da3d7 @iampersistent iampersistent committed Jan 23, 2013
@@ -6,7 +6,7 @@
class TotalValueElement extends PricingElement
{
- protected $order = 100000;
+ protected $position = 100000;
protected function doProcess($context, $processed)
{
@@ -9,7 +9,7 @@ class PricingElement implements PricingElementInterface
{
protected $id;
protected $attributes;
- protected $order;
+ protected $position;
protected $type;
public function __construct()
@@ -27,24 +27,6 @@ public function setNetValue($netValue)
/**
* @inheritdoc
*/
- public function setOrder($order)
- {
- $this->order = $order;
-
- return $this;
- }
-
- /**
- * @inheritdoc
- */
- public function getOrder()
- {
- return $this->order;
- }
-
- /**
- * @inheritdoc
- */
public function process($context, $processed)
{
return $this->doProcess($context, $processed);
@@ -63,7 +45,9 @@ protected function doProcess($context, $processed)
*/
function setPosition($position)
{
- // TODO: Implement setPosition() method.
+ $this->position = $position;
+
+ return $this;
}
/**
@@ -73,7 +57,7 @@ function setPosition($position)
*/
function getPosition()
{
- // TODO: Implement getPosition() method.
+ return $this->position;
}
public function getId()
@@ -93,38 +93,57 @@ public function process($context = null)
// create empty array with keys from $this->processed
$processed = array();
/** @var \Vespolina\Entity\Pricing\PricingElementInterface $element */
- foreach ($this->elements as $element) {
+ foreach ($this->getPricingElements() as $element) {
$processed = array_merge($this->processed, $element->process($context, $processed));
}
$this->processed = $processed;
+
+ $newSet = new self();
+ $newSet->setProcessed($processed);
+ $newSet->setProcessingState(self::PROCESSING_FINISHED);
$this->processingState = self::PROCESSING_FINISHED;
- }
- public function addElement(PricingElementInterface $element)
- {
- $this->pricingElements->add($element);
+ return $newSet;
}
-
public function setContext($context)
{
$this->context = $context;
+
+ return $this;
}
public function getContext()
{
return $this->context;
}
+ public function addPricingElement(PricingElementInterface $element)
+ {
+ $this->pricingElements->add($element);
+
+ return $this;
+ }
+
public function setPricingElements($pricingElements)
{
$this->pricingElements = $pricingElements;
+
+ return $this;
}
public function getPricingElements()
{
- return $this->pricingElements;
+ $elements = $this->pricingElements->toArray();
+ $returnElements = array();
+ foreach ($elements as $element) {
+ $position = $element->getPosition();
+ $returnElements[$position] = $element;
+ }
+ ksort($returnElements);
+
+ return $returnElements;
}
public function setProcessed($processed)
@@ -64,10 +64,9 @@ function addOrderHandler(OrderHandlerInterface $handler)
function determineOrderItemPrices(ItemInterface $item, PricingContextInterface $pricingContext)
{
- $pricing = array();
$productPricing = $item->getProduct()->getPricingSet();
- $pricing['totalNet'] = $item->getQuantity() * $productPricing['netValue'];
+ $itemPricing = $productPricing->process($pricingContext);
- $item->setPricing($pricing);
+ $item->setPricing($itemPricing);
}
}
@@ -1,46 +1,47 @@
<?php
-use Pricing\Entity\PricingElement;
-use Pricing\Entity\PricingSet;
+use ImmersiveLabs\Pricing\Entity\PricingElement;
+use ImmersiveLabs\Pricing\Entity\PricingSet;
class PricingSetTest extends \PHPUnit_Framework_TestCase
{
public function testAddElements()
{
$pricingSet = new PricingSet();
- $this->assertCount(1, $pricingSet->getElements(), 'the elements should start with the total value element');
+ $this->assertCount(1, $pricingSet->getPricingElements(), 'the elements should start with the total value element');
$element1 = new PricingElement();
$element1->setPosition(100);
- $pricingSet->addElement($element1);
- $this->assertCount(2, $pricingSet->getElements());
+ $pricingSet->addPricingElement($element1);
+ $this->assertCount(2, $pricingSet->getPricingElements());
$element2 = new PricingElement();
$element2->setPosition(10);
- $pricingSet->addElement($element2);
- $elements = $pricingSet->getElements();
+ $pricingSet->addPricingElement($element2);
+ $elements = $pricingSet->getPricingElements();
$this->assertCount(3, $elements);
- $this->assertSame($element2, $elements[0], 'the second element should be first since the position is lower');
- $this->assertSame($element1, $elements[1], 'the first element should be next since the position is higher');
- $this->assertInstanceOf('', $elements[2], 'the preset element should be last');
-
+ $this->assertSame($element2, array_shift($elements), 'the second element should be first since the position is lower');
+ $this->assertSame($element1, array_shift($elements), 'the first element should be next since the position is higher');
+ $this->assertInstanceOf('ImmersiveLabs\Pricing\Entity\Element\TotalValueElement', array_shift($elements), 'the preset element should be last');
+ $this->markTestIncomplete('deal with multiple elements with the same position');
}
public function testProcess()
{
$pricingSet = new PricingSet();
- $elementNetValue = $this->getMock('Vespolina\Entity\Pricing\PricingElement', array('process'));
+ $elementNetValue = $this->getMock('ImmersiveLabs\Pricing\Entity\PricingElement', array('process'));
$elementNetValue->expects($this->any())
->method('process')
->will($this->returnValue(array('netValue' => '9.99')));
+ $pricingSet->addPricingElement($elementNetValue);
- $pricingSet->process();
-
- $this->assertEqual('9.99', $pricingSet->getNetValue(), 'the final value should be 9.99');
- $this->assertEqual('9.99', $pricingSet->getValue(), 'the final value should be 9.00');
-
+ $newSet = $pricingSet->process();
+ $this->assertInstanceOf('Vespolina\Entity\Pricing\PricingSetInterface', $newSet, 'a pricing set should be returned');
+ $this->assertNotSame($newSet, $pricingSet, 'the new set should be a new object');
+ $this->assertEquals('9.99', $newSet->getNetValue(), 'the final value should be 9.99');
+ $this->assertEquals('9.99', $newSet->getTotalValue(), 'the final value should be 9.00');
}
}

0 comments on commit 4a35b06

Please sign in to comment.