Completed
Push — 2.x-dev ( 5f5b4d...48a1b4 )
by Doug
61:10 queued 51:42
created

PackedBoxTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
lcom 0
cbo 5
dl 0
loc 31
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Box packing (3D bin packing, knapsack problem).
4
 *
5
 * @author Doug Wright
6
 */
7
8
namespace DVDoug\BoxPacker;
9
10
use DVDoug\BoxPacker\Test\TestBox;
11
use DVDoug\BoxPacker\Test\TestItem;
12
use PHPUnit\Framework\TestCase;
13
14
/**
15
 * @covers \DVDoug\BoxPacker\PackedBox
16
 */
17
class PackedBoxTest extends TestCase
18
{
19
    /**
20
     * Test various getters work correctly.
21
     */
22
    public function testGetters()
23
    {
24
        $box = new TestBox('Box', 370, 375, 60, 140, 364, 374, 40, 3000);
25
        $item = new TestItem('Item', 230, 330, 6, 320, true);
26
27
        $packedItemList = new ItemList();
28
        $packedItemList->insert($item);
29
30
        $packedBox = new PackedBox($box, $packedItemList, 134, 44, 34, 2540, 0, 0, 0);
31
32
        self::assertEquals($box, $packedBox->getBox());
33
        self::assertEquals($packedItemList, $packedBox->getItems());
34
        self::assertEquals(460, $packedBox->getWeight());
35
        self::assertEquals(134, $packedBox->getRemainingWidth());
36
        self::assertEquals(44, $packedBox->getRemainingLength());
37
        self::assertEquals(34, $packedBox->getRemainingDepth());
38
        self::assertEquals(2540, $packedBox->getRemainingWeight());
39
        self::assertEquals(5445440, $packedBox->getInnerVolume());
40
    }
41
42
    /**
43
     * Test that volume utilisation is calculated correctly.
44
     */
45
    public function testVolumeUtilisation()
46
    {
47
        $box = new TestBox('Box', 10, 10, 20, 10, 10, 10, 20, 10);
48
        $item = new TestItem('Item', 4, 10, 10, 10, true);
49
50
        $boxItems = new ItemList();
51
        $boxItems->insert($item);
52
53
        $packedBox = new PackedBox($box, $boxItems, 0, 0, 0,0,0, 0, 0);
54
55
        self::assertEquals(20, $packedBox->getVolumeUtilisation());
56
    }
57
58
    /**
59
     * Test that caching of weight calculation works correctly.
60
     */
61
    public function testWeightCalcCaching()
62
    {
63
        $box = new TestBox('Box', 10, 10, 20, 10, 10, 10, 20, 10);
64
        $item = new TestItem('Item', 4, 10, 10, 10, true);
65
66
        $boxItems = new ItemList();
67
        $boxItems->insert($item);
68
69
        $packedBox = new PackedBox($box, $boxItems, 0, 0, 0, 0, 0, 0, 0);
70
71
        self::assertEquals(20, $packedBox->getWeight());
72
73
        //inspect cache, then poke at the value and see if it's returned correctly
74
        $cachedValue = new \ReflectionProperty($packedBox, 'weight');
75
        $cachedValue->setAccessible(true);
76
        $cachedValue->getValue($packedBox);
77
        self::assertEquals(20, $cachedValue->getValue($packedBox));
78
79
        $cachedValue->setValue($packedBox, 30);
80
        self::assertEquals(30, $cachedValue->getValue($packedBox));
81
    }
82
}
83