Skip to content

Commit

Permalink
Update update_purchase
Browse files Browse the repository at this point in the history
Make it more resilient so if there is bad data there it will update
purchase_items properly
  • Loading branch information
ivank committed Jan 27, 2014
1 parent 1b67d83 commit 62f119c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
20 changes: 11 additions & 9 deletions classes/Kohana/Model/Promotion.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function applies_to(Model_Store_Purchase $purchase)
*/
public function build_purchase_item()
{
return $this->purchase_items []= Jam::build('purchase_item_promotion');
return $this->purchase_items->build(array('model' => 'purchase_item_promotion'));
}

public function currency()
Expand All @@ -79,20 +79,22 @@ public function currency()

/**
* If the promotion applies to the store_purchase - add a purchase_item for this promotion, otherwise remove the associated purchase item
* @param Model_Store_Purchase $store_purchase
* @param Model_Store_Purchase $store_purchase
*/
public function update_store_purchase(Model_Store_Purchase $store_purchase)
{
$items = $store_purchase->items->as_array();
$promo_item = $this->build_purchase_item();
$item_offset = $store_purchase->search_same_item($promo_item);

if ($this->applies_to($store_purchase))
{
$store_purchase->items[$item_offset] = $promo_item;
}
elseif ($item_offset !== NULL)
$items = array_filter($items, function($item) use ($promo_item) {
return ! $item->is_same($promo_item);
});

if ($this->applies_to($store_purchase))
{
unset($store_purchase->items[$item_offset]);
$items []= $promo_item;
}

$store_purchase->items = $items;
}
}
17 changes: 7 additions & 10 deletions tests/tests/Model/PromotionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function test_applies_to()
*/
public function test_build_purchase_item()
{
$promotion = Jam::build('promotion');
$promotion = Jam::build('promotion')->load_fields(array('id' => 10));

$purchase_item = $promotion->build_purchase_item();

Expand All @@ -69,14 +69,9 @@ public function test_update_store_purchase()
'store_purchase'
));

$store_purchase
->expects($this->exactly(4))
->method('search_same_item')
->will($this->onConsecutiveCalls(0, NULL, 1, NULL));

$store_purchase->items = array(
array('id' => 10, 'model' => 'product'),
array('id' => 15, 'model' => 'product'),
array('id' => 10, 'model' => 'purchase_item_product'),
array('id' => 15, 'model' => 'purchase_item_product'),
);

$promotion = $this->getMock('Model_Promotion', array(
Expand All @@ -85,6 +80,8 @@ public function test_update_store_purchase()
'promotion'
));

$promotion->load_fields(array('id' => 10));

$promotion
->expects($this->exactly(4))
->method('applies_to')
Expand All @@ -93,8 +90,8 @@ public function test_update_store_purchase()
// applies_to = TRUE, offset = 0
$promotion->update_store_purchase($store_purchase);

$this->assertCount(2, $store_purchase->items);
$this->assertSame($promotion, $store_purchase->items[0]->reference);
$this->assertCount(3, $store_purchase->items);
$this->assertSame($promotion, $store_purchase->items[2]->reference);

// applies_to = TRUE, offset = NULL
$promotion->update_store_purchase($store_purchase);
Expand Down

0 comments on commit 62f119c

Please sign in to comment.