From: Maxime Bourget Date: Fri, 7 Jun 2013 23:02:22 +0000 (+0100) Subject: Splitter Test written. Doesn't pass. X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=commitdiff_plain;h=514ad9dfb6f8ea147b9cb9ee9b89d2b0b485b607;p=order_line_extra.git Splitter Test written. Doesn't pass. --- diff --git a/includes/splitter.inc b/includes/splitter.inc index c3aff0a..6019760 100644 --- a/includes/splitter.inc +++ b/includes/splitter.inc @@ -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; diff --git a/tests/testSplitter.php b/tests/testSplitter.php index fa42f7a..aa3fa82 100644 --- a/tests/testSplitter.php +++ b/tests/testSplitter.php @@ -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)); + + } } } ?>