Add get_default_discount , and test. Not implemented yet.
authorMaxime Bourget <bmx007@gmail.com>
Sun, 26 May 2013 10:05:24 +0000 (11:05 +0100)
committerMaxime Bourget <bmx007@gmail.com>
Sun, 26 May 2013 14:04:45 +0000 (15:04 +0100)
includes/textcart_manager.inc
test/textcartManager/processTest.php [new file with mode: 0644]

index 2acbc4f74500bf409c76e7e14e65a7a532d18b30..96db2075e2aeebae17c7edbe01a0c3bca100b765 100644 (file)
@@ -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 "</ul>";
 
       // 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 (file)
index 0000000..255a6e4
--- /dev/null
@@ -0,0 +1,60 @@
+<?php
+require_once('test/helper.php');
+
+class ProcessTest extends PHPUnit_Framework_TestCase {
+       protected $cart;
+       protected $mgr;
+
+       protected  function setUp() {
+               $this->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);
+       }
+
+}