parse line test setup correctly.
authorMaxime Bourget <bmx007@gmail.com>
Sat, 25 May 2013 15:40:21 +0000 (16:40 +0100)
committerMaxime Bourget <bmx007@gmail.com>
Sat, 25 May 2013 15:40:21 +0000 (16:40 +0100)
includes/textcart_manager.inc
test/textcartManagerTest.php [new file with mode: 0644]
test/textcart_manager.php [deleted file]

index 81678f704bffea01c61d0f99ec520d60e186277b..2acbc4f74500bf409c76e7e14e65a7a532d18b30 100644 (file)
@@ -323,6 +323,7 @@ class TextCartManager {
     display_error("Can't find line details with ".join(", ",array($stock_code, $quantity, $price, $discount, $description)));
     return null;
   }
+
   function parse_line($line) {
     global $mode_map;
     #echo "parsing : $line<br/>";
@@ -349,7 +350,7 @@ class TextCartManager {
       $mode = $this->mode_map[$matches[1]];
       $stock_code =  $matches[2];
       $fields_str = $matches[3];
-      $description = trim($matches[4], '" ');
+      $description = trim(@$matches[4], '" ');
 
       # Hack to allow spaces between attribute qualifier
       $fields_str = preg_replace('/([+$^])\s*/', '\1' , $fields_str);
@@ -379,7 +380,7 @@ class TextCartManager {
           display_error("price already set for line '$line'");
           return;
         };
-        $price = $matches[1] . $matches[2];//  ack to get first match or the second one
+        $price = $matches[1] . @$matches[2];//  ack to get first match or the second one
       }
       elseif (preg_match('/^(\d+(?:.\d+)?'.PARAM_REG.')%$/', $field, $matches)) {
         if($discount) {
diff --git a/test/textcartManagerTest.php b/test/textcartManagerTest.php
new file mode 100644 (file)
index 0000000..b31b497
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+
+$path_to_root = '../../';
+require_once 'includes/textcart_manager.inc';
+
+
+class TextcartManagerTest extends PHPUnit_Framework_TestCase {
+       protected $cart;
+       protected $mgr;
+
+       protected  function setUp() {
+               $this->cart = array("mock");
+               $this->mgr = new TextCartManager();
+       }
+
+
+       public function parseExamples() {
+               return array(
+                                                       array("A 10 5.0 3% | hello", NORMAL_LINE, "A", '10', '5.0', 0.03, "hello")
+                                                       ,array("A", "A", 1, 0.0, 0, "")
+               );
+       }
+/**
+ *      * @dataProvider parseExamples
+ *      */
+    public function testAdd($line, $mode,  $stock_code, $quantity, $price, $discount=null, $description=null, $date=null)
+    {
+                       $data = $this->mgr->parse_line($line);
+                       $this->assertEquals($data, array(
+      "mode" => $mode
+      ,"stock_code" => $stock_code
+      ,"quantity" => $quantity
+      ,"price" => $price
+      ,"discount" => $discount
+      ,"description" => $description
+      ,"date" => $date
+                               )
+                       );
+    }
+} 
diff --git a/test/textcart_manager.php b/test/textcart_manager.php
deleted file mode 100644 (file)
index 608da26..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-<?php
-
-$path_to_root = '../../';
-require_once 'includes/textcart_manager.inc';
-//require_once 'PHPUnit.php';
-
-class TextcartManagerTest extends PHPUnit_Framework_TestCase {
-       function test_titi() {
-}
-}