Splitter Test written. Doesn't pass.
authorMaxime Bourget <bmx007@gmail.com>
Fri, 7 Jun 2013 23:02:22 +0000 (00:02 +0100)
committerMaxime Bourget <bmx007@gmail.com>
Fri, 7 Jun 2013 23:02:22 +0000 (00:02 +0100)
includes/splitter.inc
tests/testSplitter.php

index c3aff0ad894d1cba21c04e3db8d1a0e13f7b0fc2..6019760212382821eb8766163749b0ee36ea86b0 100644 (file)
@@ -3,6 +3,9 @@ require_once($path_to_root.'/'.'includes/date_functions.inc');
 class Split {
        public $start_date;
        public $end_date;
+       public $quantity=0;
+       public $quantity_dispatched=0;
+       
 
        function __construct($start_date, $period=null) {
                        $this->start_date = $start_date;
@@ -14,12 +17,13 @@ class Split {
                        $this->end_date = add_days($this->end_date, $days);
        }
 }
+
 class Splitter {
        public $detail_ids = array() ;
        public $start_date;
        public $end_date;
        protected $days;
-       public $quantity_max;
+       public $max_quantity;
 
        public function __construct(array $data) {
                $this->details_id = array();
@@ -29,6 +33,8 @@ class Splitter {
                $this->start_date = $data['start_date'];
                $this->end_date = $data['end_date'];
                $this->days = date_diff2($this->end_date, $this->start_date, 'd');
+
+               $this->max_quantity = $data['max_quantity'];
        }
 
        protected function loadDetail($detail_id) {
@@ -59,33 +65,33 @@ class Splitter {
  * The 'splitting' split the whole quantity not only the quantity left to dispatch
  * However, fully dispatched split won't be split in real, but merged with the next one
  */
-       protected function split($row) {
+       public function split($row) {
                $splits = array();
                $quantity = $row['quantity'];
                $quantity_sent = $row['qty_sent'];
 
-               if($quantity >= $quantity_sent) return;
+               if($quantity >= $quantity_sent) return $splits;
                if(($quantity-$quantity_sent) % $this->max_quantity < $this->max_quantity) return;
                
                // determine the number of split needed. This will give us the lenght of a split.
                $nsplit = ceiling($initial_quantity/$this->max_quantity);
-               if($nsplit == 1) return;
+               if($nsplit == 1) return $splits;
 
                $period = $this->days/$nsplit;
 
                array_push($splits, $split = new Split($start_date, $period));
+               $split->quantity_dispatched = $quantity_dispatched;
                $split_quantity = min($quantity, $this->max_quantity);
                while($split_quantity > 0) {
                                        $split->quantity += $split_quantity;
                                // Check if the split has been entirely dispatch or not.
-                               if($quantity_dispatched > $split->quantity) {
+                               if($split->$quantity_dispatched > $split->quantity) {
                                        //extend the split
                                        $split->extend($period);
                                }
                                else {
                                        // create a new split
                                        array_push($splits, $split = new Split($split->end_date, $period));
-                                       $quantity_dispatched = 0; // we don't need to check anymore.
                                        
                                }
                                $quantity -= $split_quantity;
index fa42f7a32439c83cc6cd480fcb3ab52ae8211e96..aa3fa8277e20121ae16ed3a8ab5e7a17bdf7f63e 100644 (file)
@@ -5,15 +5,47 @@ class splitterTest extends PHPUnit_Framework_TestCase {
        public function testConstruct() {
                $start_date = '2013/05/01';
                $end_date = '2013/05/31';
+               $max_quantity = 6;
                $details = array('1' => 'details 1', '2' => 'details', '3' => 'd3');
 
                $splitter = new Splitter(array('detail' => $details
-                       , 'start_date'=> $start_date
-                       , 'end_date' => $end_date));
+                       ,'start_date'=> $start_date
+                       ,'end_date' => $end_date
+                       ,'max_quantity' => $max_quantity));
                $this->assertEquals($start_date, $splitter->start_date);
                $this->assertEquals($end_date, $splitter->end_date);
+               $this->assertEquals($max_quantity, $splitter->max_quantity);
                $this->assertEquals($splitter->days(), 30);
 
+               return $splitter;
+       }
+
+       /**
+ * @depends testConstruct
+ */
+       public function testSplit($splitter) {
+               $this->assertSplit($splitter, 5,  0, array());
+               $this->assertSplit($splitter, 15,  11, array());
+               $this->assertSplit($splitter, 30,  24, array());
+               $this->assertSplit($splitter, 10,  0
+                               ,array("6 0  2013/05/01 2013/05/16"
+                               ,"4 0  2013/05/16 2013/05/31"));
+               $this->assertSplit($splitter, 18,  10
+                               ,array(" 12 10  2013/05/01 2013/05/21"
+                               ,"6 0  2013/05/21 2013/05/31"));
+       }
+
+       public function assertSplit(Splitter $splitter, $quantity, $quantity_dispatched, array $expected_splits) {
+                       $splits = $splitter->split(array('quantity' => $quantity, 'qty_sent' => $quantity_dispatched));
+
+                       if(empty($expected_splits)) return $this->assertEmpty($splits);
+                       foreach(array_combine($expected_splits, $splits) as $ex => $split) {
+                               $this->assertEquals(explode(" ", $ex), array($split->quantity
+                                       ,$split->quantity_dispatched
+                                       ,$split->start_date
+                                       ,$split->end_date));
+                       
+                       }
        }
 }
 ?>