diff --git a/ItemList.php b/ItemList.php index af3f9fbc..c67a5e75 100644 --- a/ItemList.php +++ b/ItemList.php @@ -30,8 +30,12 @@ public function compare($itemA, $itemB) return 1; } elseif ($itemA->getVolume() < $itemB->getVolume()) { return -1; - } else { + } elseif ($itemA->getWeight() !== $itemB->getWeight()) { return $itemA->getWeight() - $itemB->getWeight(); + } else if ($itemA->getDescription() < $itemB->getDescription()) { + return 1; + } else { + return -1; } } diff --git a/tests/ItemListTest.php b/tests/ItemListTest.php index ee332ca0..b3198c7f 100644 --- a/tests/ItemListTest.php +++ b/tests/ItemListTest.php @@ -4,7 +4,6 @@ * @package BoxPacker * @author Doug Wright */ - namespace DVDoug\BoxPacker; use DVDoug\BoxPacker\Test\TestItem; @@ -16,19 +15,34 @@ class ItemListTest extends TestCase function testCompare() { - $box1 = new TestItem('Small', 20, 20, 2, 100, true); - $box2 = new TestItem('Large', 200, 200, 20, 1000, true); - $box3 = new TestItem('Medium', 100, 100, 10, 500, true); + $item1 = new TestItem('Small', 20, 20, 2, 100, true); + $item2 = new TestItem('Large', 200, 200, 20, 1000, true); + $item3 = new TestItem('Medium', 100, 100, 10, 500, true); $list = new ItemList; - $list->insert($box1); - $list->insert($box2); - $list->insert($box3); - - $sorted = []; - while (!$list->isEmpty()) { - $sorted[] = $list->extract(); - } - self::assertEquals(array($box2, $box3, $box1), $sorted); + $list->insert($item1); + $list->insert($item2); + $list->insert($item3); + + $sorted = iterator_to_array($list,false); + self::assertEquals(array($item2, $item3, $item1), $sorted); + } + + function testDifferentItemsSameDimensions() + { + + $item1 = new TestItem('Item A', 20, 20, 2, 100, true); + $item2 = new TestItem('Item B', 20, 20, 2, 100, true); + $item3 = new TestItem('Item A', 20, 20, 2, 100, true); + $item4 = new TestItem('Item B', 20, 20, 2, 100, true); + + $list = new ItemList; + $list->insert($item1); + $list->insert($item2); + $list->insert($item3); + $list->insert($item4); + + $sorted = iterator_to_array($list,false); + self::assertEquals(array($item1, $item3, $item2, $item4), $sorted); } }