Skip to content

Commit

Permalink
Use single-table inheritance for purchase items.
Browse files Browse the repository at this point in the history
Make the promotions package compatible with openbuildings/purchases:0.5
Model_Purchase_Item_Promotion is replacing the "promotion" type
  • Loading branch information
hkdobrev committed Nov 15, 2013
1 parent e5cdadc commit 1fd7e18
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 34 deletions.
3 changes: 2 additions & 1 deletion classes/Kohana/Model/Promotion.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static function initialize(Jam_Meta $meta)
->associations(array(
'purchase_items' => Jam::association('hasmany', array(
'as' => 'reference',
'foreign_model' => 'purchase_item_promotion',
))
))
->fields(array(
Expand Down Expand Up @@ -68,7 +69,7 @@ public function applies_to(Model_Store_Purchase $purchase)
*/
public function build_purchase_item()
{
return $this->purchase_items->build(array(
return $this->purchase_items []= Jam::build('purchase_item_promotion', array(
'type' => 'promotion',
'is_payable' => TRUE,
'is_discount' => TRUE,
Expand Down
25 changes: 25 additions & 0 deletions classes/Kohana/Model/Purchase/Item/Promotion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php defined('SYSPATH') OR die('No direct script access.');

/**
* @package openbuildings\promotions
* @author Haralan Dobrev <hkdobrev@gmail.com>
* @copyright 2013 OpenBuildings, Inc.
* @license http://spdx.org/licenses/BSD-3-Clause
*/
class Kohana_Model_Purchase_Item_Promotion extends Model_Purchase_Item {

/**
* @codeCoverageIgnore
*/
public static function initialize(Jam_Meta $meta)
{
parent::initialize($meta);

$meta->table('purchase_items');
}

public function get_price()
{
return $this->get_reference_paranoid()->price_for_purchase_item($this);
}
}
3 changes: 3 additions & 0 deletions classes/Model/Purchase/Item/Promotion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php defined('SYSPATH') OR die('No direct script access.');

class Model_Purchase_Item_Promotion extends Kohana_Model_Purchase_Item_Promotion {}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
"require": {
"php": ">=5.3.0",
"composer/installers": "*",
"openbuildings/purchases": "0.4.*"
"openbuildings/purchases": "0.5.*"
},
"require-dev": {
"openbuildings/shipping": "0.3.*",
"openbuildings/shipping": "0.4.*",
"openbuildings/kohana-test-bootsrap": "0.1.*"
},
"autoload": {
Expand Down
34 changes: 17 additions & 17 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions tests/test_data/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ DROP TABLE IF EXISTS `purchases`;
CREATE TABLE `purchases` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`creator_id` INT(11) UNSIGNED NOT NULL,
`promo_code_id` INT(11) UNSIGNED NOT NULL,
`promo_code_id` INT(11) UNSIGNED DEFAULT NULL,
`number` VARCHAR(40) NOT NULL,
`promo_code_text` VARCHAR(40) NOT NULL,
`currency` VARCHAR(3) NOT NULL,
Expand Down Expand Up @@ -69,7 +69,7 @@ CREATE TABLE `purchase_items` (
`reference_model` VARCHAR(40) NULL,
`price` DECIMAL(10,2) NULL,
`quantity` INT(11) NULL,
`type` VARCHAR(255) NULL,
`model` VARCHAR(255) NULL,
`is_payable` INT(1) UNSIGNED NOT NULL,
`is_discount` INT(1) UNSIGNED NOT NULL,
`is_deleted` INT(1) UNSIGNED NOT NULL,
Expand Down Expand Up @@ -184,11 +184,11 @@ VALUES
# Dump of table purchase_items
# ------------------------------------------------------------

INSERT INTO `purchase_items` (`id`, `store_purchase_id`, `reference_id`, `reference_model`, `price`, `quantity`, `type`, `is_payable`, `is_discount`, `is_deleted`)
INSERT INTO `purchase_items` (`id`, `store_purchase_id`, `reference_id`, `reference_model`, `price`, `quantity`, `model`, `is_payable`, `is_discount`, `is_deleted`)
VALUES
(1,1,1,'product',200.00,1,'product',1,0,0),
(2,1,1,'variation',200.00,1,'product',1,0,0),
(3,2,1,'product',NULL,1,'product',1,0,0);
(1,1,1,'product',200.00,1,'purchase_item_product',1,0,0),
(2,1,1,'variation',200.00,1,'purchase_item_product',1,0,0),
(3,2,1,'product',NULL,1,'purchase_item_product',1,0,0);

# Dump of table stores
# ------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions tests/tests/Jam/Behavior/Promotable/Store/PurchaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public function test_filter_promotion_items()

$store_purchase = Jam::find('store_purchase', 2);

$store_purchase->items->add(Jam::build('purchase_item', array('id' => 12, 'type' => 'promotion', 'reference' => $promocode_giftcard, 'is_discount' => TRUE, 'is_payable' => TRUE)));
$store_purchase->items->add(Jam::build('purchase_item_promotion', array('id' => 12, 'reference' => $promocode_giftcard, 'is_discount' => TRUE, 'is_payable' => TRUE)));

$store_purchase->items->add(Jam::build('purchase_item', array('id' => 16, 'type' => 'promotion', 'reference' => $promocode_percent, 'is_discount' => TRUE, 'is_payable' => TRUE)));
$store_purchase->items->add(Jam::build('purchase_item_promotion', array('id' => 16, 'reference' => $promocode_percent, 'is_discount' => TRUE, 'is_payable' => TRUE)));

$items = $store_purchase->items(array('promotion' => 'promocode_giftcard'));
$this->assertEquals(array(12), $this->ids($items));
Expand Down Expand Up @@ -113,7 +113,7 @@ public function data_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));
$item = Jam::build('purchase_item_promotion', array('reference_model' => $reference_model));

$this->assertEquals($expected, Jam_Behavior_Promotable_Store_Purchase::purchase_item_is_promotion($item, $promotion));
}
Expand Down
4 changes: 2 additions & 2 deletions tests/tests/Jam/Validator/Rule/Purchase/PromocodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class Jam_Validator_Rule_Purchase_PromocodeTest extends Testcase_Promotions {

/**
* @covers Jam_Validator_Rule_Promocode::valid_promo_code
* @covers Jam_Validator_Rule_Purchase_Promocode::valid_promo_code
*/
public function test_valid_promo_code()
{
Expand Down Expand Up @@ -61,7 +61,7 @@ public function test_valid_promo_code()
}

/**
* @covers Jam_Validator_Rule_Promocode::validate
* @covers Jam_Validator_Rule_Purchase_Promocode::validate
*/
public function test_validate()
{
Expand Down
6 changes: 5 additions & 1 deletion tests/tests/Model/Promotion/Promocode/GitfcardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ public function test_price_for_purchase_item()
{
$monetary = new Monetary(new Source_Static);

$purchase_item = $this->getMock('Model_Purchase_Item', array('monetary'), array('purchase_item'));
$purchase_item = $this->getMock('Model_Purchase_Item_Promotion', array(
'monetary'
), array(
'purchase_item_promotion'
));

$purchase_item
->expects($this->once())
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/Model/Promotion/Promocode/PercentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function test_price_for_purchase_item()
->with($this->equalTo('product'))
->will($this->returnValue($total_price));

$purchase_item = Jam::build('purchase_item', array('store_purchase' => $store_purchase));
$purchase_item = Jam::build('purchase_item_promotion', array('store_purchase' => $store_purchase));

$promotion = Jam::build('promotion_promocode_percent', array(
'amount' => 0.12,
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/Model/PromotionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function test_price_for_purchase_item()
{
$promotion = Jam::build('promotion');

$promotion->price_for_purchase_item(Jam::build('purchase_item'));
$promotion->price_for_purchase_item(Jam::build('purchase_item_promotion'));
}

/**
Expand Down
46 changes: 46 additions & 0 deletions tests/tests/Model/Purchase/Item/PromotionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/**
* @group model
* @group model.purchase_item_promotion
*/
class Model_Purchase_Item_PromotionTest extends Testcase_Promotions {

/**
* @covers Model_Purchase_Item_Promotion::initialize
*/
public function test_initialize()
{
$meta = Jam::meta('purchase_item_promotion');
$this->assertSame('purchase_items', $meta->table());
}

/**
* @covers Model_Purchase_Item_Promotion::get_price
*/
public function test_get_price()
{
$mock = $this->getMock('stdClass', array(
'price_for_purchase_item'
));

$purchase_item = $this->getMock('Model_Purchase_Item_Promotion', array(
'get_reference_paranoid'
), array(
'purchase_item_promotion'
));

$purchase_item
->expects($this->once())
->method('get_reference_paranoid')
->will($this->returnValue($mock));

$mock
->expects($this->once())
->method('price_for_purchase_item')
->with($purchase_item)
->will($this->returnValue(10.25));

$this->assertSame(10.25, $purchase_item->get_price());
}
}

0 comments on commit 1fd7e18

Please sign in to comment.