Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Changelog

## [4.x - Unreleased] - 2026-xx-xx

### Fixed
- In some rare cases `WeightRedistributor` would silently drop items

## [4.2.0] - 2026-07-05
### Added
- A mechanism to allow forcing certain items to be packed together [RubenKluft]
Expand Down
7 changes: 6 additions & 1 deletion src/WeightRedistributor.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ private function doVolumeRepack(iterable $items, Box $currentBox): PackedBoxList
$packer->setBoxQuantity($currentBox, $this->boxQuantitiesAvailable[$currentBox] + 1);
$packer->setItems($items);

return $packer->doBasicPacking(true);
$packedBoxes = $packer->doBasicPacking(true);
if ($packedBoxes->count() !== 1 || $packer->getUnpackedItems()->count() !== 0) {
return new PackedBoxList($this->packedBoxSorter);
}

return $packedBoxes;
}

/**
Expand Down
32 changes: 32 additions & 0 deletions tests/WeightRedistributorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace DVDoug\BoxPacker;

use DVDoug\BoxPacker\Test\ConstrainedPlacementNoStackingTestItem;
use DVDoug\BoxPacker\Test\LimitedSupplyTestBox;
use DVDoug\BoxPacker\Test\TestBox;
use DVDoug\BoxPacker\Test\TestItem;
use PHPUnit\Framework\Attributes\CoversClass;
Expand Down Expand Up @@ -67,4 +68,35 @@ public function testWeightDistributionWorks(): void

self::assertEquals(0, $packedBoxes->getWeightVariance());
}

/**
* Test to ensure no items are silently dropped during weight redistribution.
*/
public function testWeightRedistributionDoesNotSilentlyDropItems(): void
{
$packer = new Packer();
$packer->addBox(new LimitedSupplyTestBox('Box', 29, 29, 29, 0, 29, 29, 29, 68, 2));

$packer->addItem(new TestItem('Item 0', 10, 10, 10, 2, Rotation::BestFit), 2);
$packer->addItem(new TestItem('Item 1', 10, 10, 10, 3, Rotation::BestFit));
$packer->addItem(new TestItem('Item 2', 10, 10, 10, 4, Rotation::BestFit));
$packer->addItem(new TestItem('Item 3', 10, 10, 10, 8, Rotation::BestFit), 5);
$packer->addItem(new TestItem('Item 4', 10, 10, 10, 18, Rotation::BestFit), 3);

// packer initially packs 6 items into each box,
// the imbalance in weights will be attempted to be corrected by the WeightRedistributor
$packedBoxes = $packer->pack();
self::assertCount(2, $packedBoxes);

$packedItemCount = 0;
foreach ($packedBoxes as $packedBox) {
$packedItemCount += $packedBox->items->count();
}

self::assertSame(
12,
$packedItemCount + $packer->getUnpackedItems()->count(),
'No items should be lost during weight redistribution'
);
}
}
Loading