Permalink
Browse files

allow plus() to work with null set

  • Loading branch information...
1 parent 60565c1 commit d78fd2a402ce570b1e2feb7d0b4b160a4571cbad @iampersistent iampersistent committed Feb 1, 2013
Showing with 16 additions and 4 deletions.
  1. +7 −4 lib/ImmersiveLabs/Pricing/Entity/PricingSet.php
  2. +9 −0 tests/Entity/PricingSetTest.php
@@ -117,13 +117,16 @@ public function process($context = null)
return $newSet;
}
- public function plus(PricingSetInterface $addSet)
+ public function plus($addSet)
{
$newSet = new self();
$newSet->setProcessed($this->processed);
- foreach ($addSet->getProcessed() as $key => $value) {
- $total = $this->get($key) + $value;
- $newSet->set($key, $total);
+
+ if ($addSet instanceof PricingSetInterface) {
+ foreach ($addSet->getProcessed() as $key => $value) {
+ $total = $this->get($key) + $value;
+ $newSet->set($key, $total);
+ }
}
$newSet->setProcessingState(self::PROCESSING_FINISHED);
@@ -65,6 +65,15 @@ public function testPlus()
$pricingSet1->set('totalValue', 5);
$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('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();
$pricingSet2->set('discounts', 5);
$pricingSet2->set('netValue', 5);

0 comments on commit d78fd2a

Please sign in to comment.