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

PackedBoxListTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 88.24 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
lcom 0
cbo 6
dl 30
loc 34
rs 10
c 1
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\PackedBoxList
16
 */
17
class PackedBoxListTest extends TestCase
18
{
19
    /**
20
     * Test that inserting individually correctly works.
21
     */
22
    public function testInsertAndCount()
23
    {
24
        $box = new TestBox('Box', 10, 10, 10, 0, 10, 10, 10, 100);
25
        $itemA = new TestItem('Item A', 5, 10, 10, 10, true);
26
        $itemB = new TestItem('Item B', 5, 10, 10, 20, true);
27
28
        $packedItemListA = new ItemList();
29
        $packedItemListA->insert($itemA);
30
        $packedBoxA = new PackedBox($box, $packedItemListA, 0, 0, 0,0 ,0, 0, 0);
31
32
        $packedItemListB = new ItemList();
33
        $packedItemListB->insert($itemB);
34
        $packedBoxB = new PackedBox($box, $packedItemListB, 0, 0, 0,0 ,0, 0, 0);
35
36
        $packedBoxList = new PackedBoxList();
37
        $packedBoxList->insert($packedBoxA);
38
        $packedBoxList->insert($packedBoxB);
39
40
        self::assertEquals(2, $packedBoxList->count());
41
    }
42
43
    /**
44
     * Test that inserting in bulk correctly works.
45
     */
46
    public function testInsertFromArrayAndCount()
47
    {
48
        $box = new TestBox('Box', 10, 10, 10, 0, 10, 10, 10, 100);
49
        $itemA = new TestItem('Item A', 5, 10, 10, 10, true);
50
        $itemB = new TestItem('Item B', 5, 10, 10, 20, true);
51
52
        $packedItemListA = new ItemList();
53
        $packedItemListA->insert($itemA);
54
        $packedBoxA = new PackedBox($box, $packedItemListA, 0, 0, 0,0 ,0, 0, 0);
55
56
        $packedItemListB = new ItemList();
57
        $packedItemListB->insert($itemB);
58
        $packedBoxB = new PackedBox($box, $packedItemListB, 0, 0, 0,0 ,0, 0, 0);
59
60
        $packedBoxList = new PackedBoxList();
61
        $packedBoxList->insertFromArray([$packedBoxA, $packedBoxB]);
62
63
        self::assertEquals(2, $packedBoxList->count());
64
    }
65
66
    /**
67
     * Test we can peek at the "top" (next) item in the list.
68
     */
69
    public function testTop()
70
    {
71
        $box = new TestBox('Box', 10, 10, 10, 0, 10, 10, 10, 100);
72
        $itemA = new TestItem('Item A', 5, 10, 10, 10, true);
73
        $itemB = new TestItem('Item B', 5, 10, 10, 20, true);
74
75
        $packedItemListA = new ItemList();
76
        $packedItemListA->insert($itemA);
77
        $packedBoxA = new PackedBox($box, $packedItemListA, 0, 0, 0,0 ,0, 0, 0);
78
79
        $packedItemListB = new ItemList();
80
        $packedItemListB->insert($itemB);
81
        $packedBoxB = new PackedBox($box, $packedItemListB, 0, 0, 0,0 ,0, 0, 0);
82
83
        $packedBoxList = new PackedBoxList();
84
        $packedBoxList->insert($packedBoxA);
85
        $packedBoxList->insert($packedBoxB);
86
87
        self::assertEquals($packedBoxB, $packedBoxList->top());
88
    }
89
90
    /**
91
     * Test that volume utilisation is correctly calculated.
92
     */
93
    public function testVolumeUtilisation()
94
    {
95
        $box = new TestBox('Box', 10, 10, 10, 0, 10, 10, 10, 10);
96
        $item = new TestItem('Item', 5, 10, 10, 10, true);
97
98
        $packedItemList = new ItemList();
99
        $packedItemList->insert($item);
100
101
        $packedBox = new PackedBox($box, $packedItemList, 0, 0, 0,0 ,0, 0, 0);
102
103
        $packedBoxList = new PackedBoxList();
104
        $packedBoxList->insert($packedBox);
105
106
        self::assertEquals(50, $packedBoxList->getVolumeUtilisation());
107
    }
108
109
    /**
110
     * Test that weight variance is correctly calculated.
111
     */
112
    public function testWeightVariance()
113
    {
114
        $box = new TestBox('Box', 10, 10, 10, 0, 10, 10, 10, 100);
115
        $itemA = new TestItem('Item A', 5, 10, 10, 10, true);
116
        $itemB = new TestItem('Item B', 5, 10, 10, 20, true);
117
118
        $packedItemListA = new ItemList();
119
        $packedItemListA->insert($itemA);
120
        $packedBoxA = new PackedBox($box, $packedItemListA, 0, 0, 0,0 ,0, 0, 0);
121
122
        $packedItemListB = new ItemList();
123
        $packedItemListB->insert($itemB);
124
        $packedBoxB = new PackedBox($box, $packedItemListB, 0, 0, 0,0 ,0, 0, 0);
125
126
        $packedBoxList = new PackedBoxList();
127
        $packedBoxList->insert($packedBoxA);
128
        $packedBoxList->insert($packedBoxB);
129
130
        self::assertEquals(25, $packedBoxList->getWeightVariance());
131
    }
132
133
    /**
134
     * Test that mean weight is correctly calculated.
135
     */
136
    public function testMeanWeight()
137
    {
138
        $box = new TestBox('Box', 10, 10, 10, 0, 10, 10, 10, 100);
139
        $itemA = new TestItem('Item A', 5, 10, 10, 10, true);
140
        $itemB = new TestItem('Item B', 5, 10, 10, 20, true);
141
142
        $packedItemListA = new ItemList();
143
        $packedItemListA->insert($itemA);
144
        $packedBoxA = new PackedBox($box, $packedItemListA, 0, 0, 0,0 ,0, 0, 0);
145
146
        $packedItemListB = new ItemList();
147
        $packedItemListB->insert($itemB);
148
        $packedBoxB = new PackedBox($box, $packedItemListB, 0, 0, 0,0 ,0, 0, 0);
149
150
        $packedBoxList = new PackedBoxList();
151
        $packedBoxList->insert($packedBoxA);
152
        $packedBoxList->insert($packedBoxB);
153
154
        self::assertEquals(15, $packedBoxList->getMeanWeight());
155
    }
156
}
157