From 2393000f3836e4848a9b6aa347e64c7b00796f3a Mon Sep 17 00:00:00 2001 From: Maxime Bourget Date: Sun, 26 May 2013 11:05:24 +0100 Subject: [PATCH] Add get_default_discount , and test. Not implemented yet. --- includes/textcart_manager.inc | 26 +++++++++--- test/textcartManager/processTest.php | 60 ++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 test/textcartManager/processTest.php diff --git a/includes/textcart_manager.inc b/includes/textcart_manager.inc index 2acbc4f..96db207 100644 --- a/includes/textcart_manager.inc +++ b/includes/textcart_manager.inc @@ -195,7 +195,8 @@ class TextCartManager { $this->get_default_price($cart, $stock_code)); //$price = round2($price, $attributes['price'], user_price_dec()); $discount = $attributes["discount"]; - $discount = expand_template($template_line['discount'], $attributes['discount'], 0); + $discount = expand_template($template_line['discount'], $attributes['discount'], + $this->get_default_discount($cart, $stock_code)); } else { $price = 0; @@ -205,7 +206,11 @@ class TextCartManager { $date = expand_template($template_line['date'], $attributes['date'], $this->get_default_date($cart, $stock_code)); } - $description = expand_template($template_line['description'], $attributes['description'], get_kit_description($stock_code)); + else { + $date = null; + } + + $description = expand_template($template_line['description'], $attributes['description'], $this->get_kit_description($cart, $stock_code)); } else { $stock_code = $attributes["stock_code"]; @@ -213,6 +218,7 @@ class TextCartManager { $quantity = $attributes["quantity"]; $price = $attributes["price"]; $discount = $attributes["discount"]; + $date = $attributes["date"]; $description = $attributes["description"]; } @@ -226,13 +232,10 @@ class TextCartManager { #echo ""; // Checking that product exists, to not process dodgy one - $kit = get_item_kit($stock_code); - $number = db_num_rows($kit); - if ($number == 0) { + if(!$this->check_item_exists($stock_code)) { display_error("Product '$stock_code' doesn't exist"); display_error("Line '$line' skipped"); } - db_free_result($kit); if(!$mode) { $mode = $default_mode; } switch ($mode) { @@ -433,12 +436,23 @@ class TextCartManager { return add_days(Today(), 10); } + function get_kit_description($cart, $stock_code) { + return get_kit_description($stock_code); + } + function unmodifiable_quantity($cart, $line_no) { return 0; } function quantity($cart, $line_no) { return $cart->line_items[$line_no]->quantity; } + + function check_item_exists($stock_code) { + $kit = get_item_kit($stock_code); + $number = db_num_rows($kit); + db_free_result($kit); + return $number != 0; + } } class SalesTextCartManager extends TextCartManager { diff --git a/test/textcartManager/processTest.php b/test/textcartManager/processTest.php new file mode 100644 index 0000000..255a6e4 --- /dev/null +++ b/test/textcartManager/processTest.php @@ -0,0 +1,60 @@ +cart = $this->getMock("Cart"); + $this->mgr = $this->getMock("TextCartManager", array( + 'get_default_price' + ,'get_default_discount' + ,'get_kit_description' + ,'check_item_exists' + ,'add_to_order' + )); + } + + + public function processNormalExamples() { + return array( + array("A", "A", 1, 5.2, 0.02) + ,array("A 3", "A", 3, 5.2, 0.02) + ,array("A $3", "A", 1, 3, 0.02) + ,array(":# $3\nA", "A", 1, 3, 0.02) + ,array(":# $3\nA $7", "A", 1, 7, 0.02) + ,array(":# $(10*#+@)\nA $7", "A", 1, 75.2, 0.02) + ,array(":# 5%\nA", "A", 1, 5.2, 0.05) + ,array("A 0%", "A", 1, 5.2, 0) + ,array("A 10%", "A", 1, 5.2, 0.1) + ); + } + + /** + * @dataProvider processNormalExamples + */ + function testNormalExamples($textcart, $stock_code, $quantity, $price, $discount=null, $description=null) { + // Stubbing mgr + $this->mgr->expects($this->any()) + ->method('get_default_price') + ->will($this->returnValue(5.2)); + $this->mgr->expects($this->any()) + ->method('get_default_discount') + ->will($this->returnValue(.02)); + + // setting up expectation + $this->mgr->expects($this->once()) + ->method('add_to_order') + ->with($this->equalTo($this->cart) + ,$this->equalTo($stock_code) + ,$this->equalTo($quantity) + ,$this->equalTo($price) + ,$this->equalTo($discount) + ,$this->equalTo($description)); + + + $this->mgr->process_textcart($this->cart, $textcart, INSERT_MODE); + } + +} -- 2.30.2