Skip to content

Commit

Permalink
Fallback to sorting by item description when all other things are equ…
Browse files Browse the repository at this point in the history
…al. Fixes #87
  • Loading branch information
dvdoug committed Oct 23, 2017
1 parent c99e101 commit 55a3c16
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
6 changes: 5 additions & 1 deletion ItemList.php
Expand Up @@ -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;
}
}

Expand Down
40 changes: 27 additions & 13 deletions tests/ItemListTest.php
Expand Up @@ -4,7 +4,6 @@
* @package BoxPacker
* @author Doug Wright
*/

namespace DVDoug\BoxPacker;

use DVDoug\BoxPacker\Test\TestItem;
Expand All @@ -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);
}
}

0 comments on commit 55a3c16

Please sign in to comment.