Skip to content

Commit

Permalink
add "not_promotion" filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Kerin committed Nov 1, 2013
1 parent 9ef19d7 commit ee76df3
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 7 deletions.
38 changes: 31 additions & 7 deletions classes/Kohana/Jam/Behavior/Promotable/Store/Purchase.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,13 @@ public function filter_promotion_items(Model_Store_Purchase $store_purchase, Jam
{
if (array_key_exists('promotion', $filter))
{
if ($item->type !== 'promotion')
if ( ! Jam_Behavior_Promotable_Store_Purchase::purchase_item_is_promotion($item, $filter['promotion']))
continue;
}

$model_names = array_map(function($name) {
return 'promotion_'.$name;
}, (array) $filter['promotion']);

if ( ! in_array($item->get_insist('reference')->model, $model_names))
if (array_key_exists('not_promotion', $filter))
{
if (Jam_Behavior_Promotable_Store_Purchase::purchase_item_is_promotion($item, $filter['not_promotion']))
continue;
}

Expand All @@ -61,7 +60,32 @@ public function filter_promotion_items(Model_Store_Purchase $store_purchase, Jam

$data->return = $filtered;
}


/**
* Convert promotion name to model names
* @param string|array $name
* @return array
*/
public static function promotion_model_names($name)
{
return array_map(function($name) {
return 'promotion_'.$name;
}, (array) $name);
}

/**
* Check if purchase item's reference is one of the given promotions
* @param Model_Purchase_Item $item
* @param string|array $promotion
* @return boolean
*/
public static function purchase_item_is_promotion(Model_Purchase_Item $item, $promotion)
{
$model_names = Jam_Behavior_Promotable_Store_Purchase::promotion_model_names($promotion);

return in_array($item->reference_model, $model_names);
}

/**
* Iterate through available promotions and update its status on the store purchase (does it apply or not)
*
Expand Down
42 changes: 42 additions & 0 deletions tests/tests/Jam/Behavior/Promotable/Store/PurchaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,47 @@ public function test_filter_promotion_items()

$items = $store_purchase->items(array('promotion' => array('promocode_percent', 'promocode_giftcard')));
$this->assertEquals(array(12, 16), $this->ids($items));

$items = $store_purchase->items(array('promotion', 'not_promotion' => 'promocode_giftcard'));
$this->assertEquals(array(16), $this->ids($items));
}

public function data_promotion_model_names()
{
return array(
array('promocode', array('promotion_promocode')),
array(array('promocode'), array('promotion_promocode')),
array(array('promocode', 'promocode_giftcard'), array('promotion_promocode', 'promotion_promocode_giftcard')),
);
}

/**
* @dataProvider data_promotion_model_names
* @covers Jam_Behavior_Promotable_Store_Purchase::promotion_model_names
*/
public function test_promotion_model_names($promotion, $expected)
{
$this->assertEquals($expected, Jam_Behavior_Promotable_Store_Purchase::promotion_model_names($promotion));
}

public function data_purchase_item_is_promotion()
{
return array(
array('product', array('promocode'), FALSE),
array('promotion', array('promocode'), FALSE),
array('promotion_promocode', array('promocode'), TRUE),
array('promotion_promocode', array('promocode_giftcard'), FALSE),
);
}

/**
* @dataProvider data_purchase_item_is_promotion
* @covers Jam_Behavior_Promotable_Store_Purchase::purchase_item_is_promotion
*/
public function test_purchase_item_is_promotion($reference_model, $promotion, $expected)
{
$item = Jam::build('purchase_item', array('reference_model' => $reference_model));

$this->assertEquals($expected, Jam_Behavior_Promotable_Store_Purchase::purchase_item_is_promotion($item, $promotion));
}
}

0 comments on commit ee76df3

Please sign in to comment.