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 faaf8614..856f63ef 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; @@ -14,21 +13,35 @@ class ItemListTest extends TestCase { function testCompare() + { + $item1 = new TestItem('Small', 20, 20, 2, 100); + $item2 = new TestItem('Large', 200, 200, 20, 1000); + $item3 = new TestItem('Medium', 100, 100, 10, 500); + + $list = new ItemList; + $list->insert($item1); + $list->insert($item2); + $list->insert($item3); + + $sorted = iterator_to_array($list,false); + self::assertEquals(array($item2, $item3, $item1), $sorted); + } + + function testDifferentItemsSameDimensions() { - $box1 = new TestItem('Small', 20, 20, 2, 100); - $box2 = new TestItem('Large', 200, 200, 20, 1000); - $box3 = new TestItem('Medium', 100, 100, 10, 500); + $item1 = new TestItem('Item A', 20, 20, 2, 100); + $item2 = new TestItem('Item B', 20, 20, 2, 100); + $item3 = new TestItem('Item A', 20, 20, 2, 100); + $item4 = new TestItem('Item B', 20, 20, 2, 100); $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); + $list->insert($item4); + + $sorted = iterator_to_array($list,false); + self::assertEquals(array($item1, $item3, $item2, $item4), $sorted); } }