Skip to content

Commit

Permalink
Add additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdoug committed Jul 4, 2021
1 parent 33350c8 commit 23810c1
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 11 deletions.
32 changes: 32 additions & 0 deletions tests/InfalliblePackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,45 @@

namespace DVDoug\BoxPacker;

use DVDoug\BoxPacker\Test\LimitedSupplyTestBox;
use DVDoug\BoxPacker\Test\TestBox;
use DVDoug\BoxPacker\Test\TestItem;
use function iterator_to_array;
use PHPUnit\Framework\TestCase;

class InfalliblePackerTest extends TestCase
{
public function testTooLargeItemsHandled(): void
{
$packer = new InfalliblePacker();
$packer->addBox(new TestBox('Le petite box', 300, 300, 10, 10, 296, 296, 8, 1000));
$packer->addBox(new TestBox('Le grande box', 3000, 3000, 100, 100, 2960, 2960, 80, 10000));
$packer->addItem(new TestItem('Item 1', 2500, 2500, 20, 2000, true));
$packer->addItem(new TestItem('Item 2', 25000, 2500, 20, 2000, true));
$packer->addItem(new TestItem('Item 3', 2500, 2500, 20, 2000, true));

$packedBoxes = $packer->pack();

self::assertCount(1, $packedBoxes);
self::assertCount(2, $packedBoxes->top()->getItems());
self::assertCount(1, $packer->getUnpackedItems());
}

public function testUnpackableItemsHandled(): void
{
$packer = new InfalliblePacker();
$packer->addBox(new TestBox('Le petite box', 300, 300, 10, 10, 296, 296, 8, 1000));
$packer->addBox(new LimitedSupplyTestBox('Le grande box', 3000, 3000, 100, 100, 2960, 2960, 80, 10000, 0));
$packer->addItem(new TestItem('Item 1', 2500, 2500, 20, 2000, true));
$packer->addItem(new TestItem('Item 2', 25000, 2500, 20, 2000, true));
$packer->addItem(new TestItem('Item 3', 2500, 2500, 20, 2000, true));

$packedBoxes = $packer->pack();

self::assertCount(0, $packedBoxes);
self::assertCount(3, $packer->getUnpackedItems());
}

/**
* From issue #182.
* @group efficiency
Expand Down
3 changes: 3 additions & 0 deletions tests/ItemListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public function testCount(): void
$item3 = new TestItem('Item C', 20, 20, 2, 100, true);
$itemList->insert($item3);
self::assertCount(3, $itemList);

$itemList->remove($item2);
self::assertCount(2, $itemList);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
use DVDoug\BoxPacker\Test\TestItem;
use PHPUnit\Framework\TestCase;

/**
* @covers \DVDoug\BoxPacker\ItemTooLargeException
*/
class ItemTooLargeExceptionTest extends TestCase
class NoBoxesAvailableExceptionTest extends TestCase
{
/**
* Test that the offending item can be retrieved from the object.
Expand All @@ -23,7 +20,7 @@ public function testCanGetItem(): void
{
$item = new TestItem('Item 1', 2500, 2500, 20, 2000, true);

$exception = new ItemTooLargeException('Just testing...', $item);
$exception = new NoBoxesAvailableException('Just testing...', $item);
self::assertEquals($item, $exception->getItem());
}
}
20 changes: 20 additions & 0 deletions tests/PackedBoxListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use DVDoug\BoxPacker\Test\TestBox;
use DVDoug\BoxPacker\Test\TestItem;
use function json_encode;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -171,4 +172,23 @@ public function testMeanWeight(): void

self::assertEquals(15, $packedBoxList->getMeanWeight());
}

/**
* Test JSON representation.
*/
public function testJsonSerialize(): void
{
$box = new TestBox('Box', 10, 10, 20, 10, 10, 10, 20, 10);
$item = new OrientatedItem(new TestItem('Item', 4, 10, 10, 10, true), 4, 10, 10);

$boxItems = new PackedItemList();
$boxItems->insert(PackedItem::fromOrientatedItem($item, 0, 0, 0));

$packedBox = new PackedBox($box, $boxItems);

$packedBoxList = new PackedBoxList();
$packedBoxList->insert($packedBox);

self::assertJsonStringEqualsJsonString('[{"box":{"reference":"Box","innerWidth":10,"innerLength":10,"innerDepth":20},"items":[{"x":0,"y":0,"z":0,"width":4,"length":10,"depth":10,"item":{"description":"Item","width":4,"length":10,"depth":10,"keepFlat":true}}]}]', json_encode($packedBoxList));
}
}
18 changes: 17 additions & 1 deletion tests/PackedBoxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use DVDoug\BoxPacker\Test\TestBox;
use DVDoug\BoxPacker\Test\TestItem;
use function json_encode;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;

Expand Down Expand Up @@ -77,10 +78,25 @@ public function testWeightCalcCaching(): void
//inspect cache, then poke at the value and see if it's returned correctly
$cachedValue = new ReflectionProperty($packedBox, 'itemWeight');
$cachedValue->setAccessible(true);
$cachedValue->getValue($packedBox);
self::assertEquals(10, $cachedValue->getValue($packedBox));

$cachedValue->setValue($packedBox, 30);
self::assertEquals(30, $cachedValue->getValue($packedBox));
}

/**
* Test JSON representation.
*/
public function testJsonSerialize(): void
{
$box = new TestBox('Box', 10, 10, 20, 10, 10, 10, 20, 10);
$item = new OrientatedItem(new TestItem('Item', 4, 10, 10, 10, true), 4, 10, 10);

$boxItems = new PackedItemList();
$boxItems->insert(PackedItem::fromOrientatedItem($item, 0, 0, 0));

$packedBox = new PackedBox($box, $boxItems);

self::assertJsonStringEqualsJsonString('{"box":{"reference":"Box","innerWidth":10,"innerLength":10,"innerDepth":20},"items":[{"x":0,"y":0,"z":0,"width":4,"length":10,"depth":10,"item":{"description":"Item","width":4,"length":10,"depth":10,"keepFlat":true}}]}', json_encode($packedBox));
}
}
10 changes: 10 additions & 0 deletions tests/PackedItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace DVDoug\BoxPacker;

use DVDoug\BoxPacker\Test\TestItem;
use function json_encode;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -24,4 +25,13 @@ public function testVolumeCalculation(): void
$packedItem = new PackedItem(new TestItem('Item', 1, 1, 0, 0, false), 0, 0, 0, 3, 5, 7);
self::assertSame(105, $packedItem->getVolume());
}

/**
* Test JSON representation.
*/
public function testJsonSerialize(): void
{
$packedItem = new PackedItem(new TestItem('Item', 1, 2, 3, 10, false), 100, 20, 300, 3, 5, 7);
self::assertJsonStringEqualsJsonString('{"x":100,"y":20,"z":300,"width":3,"length":5,"depth":7,"item":{"description":"Item","width":1,"length":2,"depth":3,"keepFlat":false}}', json_encode($packedItem));
}
}
12 changes: 7 additions & 5 deletions tests/PackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -564,11 +564,13 @@ public function testIssue231(): void
$packer->setMaxBoxesToBalanceWeight(0);
$packer->addBox(new TestBox('Box 2.5-1', 30, 20, 20, 2, 30, 20, 20, 1000));

$packer->addItem(new TestItem('Item 1', 20, 20, 2, 0, false), 4);
$packer->addItem(new TestItem('Item 2', 8, 3, 2, 0, false), 5);
$packer->addItem(new TestItem('Item 3', 10, 10, 10, 0, false), 4);
$packer->addItem(new TestItem('Item 4', 12, 12, 10, 0, false), 2);
$packer->addItem(new TestItem('Item 5', 6, 4, 2, 0, false), 2);
$itemList = new ItemList();
$itemList->insert(new TestItem('Item 1', 20, 20, 2, 0, false), 4);
$itemList->insert(new TestItem('Item 2', 8, 3, 2, 0, false), 5);
$itemList->insert(new TestItem('Item 3', 10, 10, 10, 0, false), 4);
$itemList->insert(new TestItem('Item 4', 12, 12, 10, 0, false), 2);
$itemList->insert(new TestItem('Item 5', 6, 4, 2, 0, false), 2);
$packer->setItems($itemList);
$packedBoxes = $packer->pack();

self::assertCount(1, $packedBoxes);
Expand Down

0 comments on commit 23810c1

Please sign in to comment.