|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lenius\Basket\Tests; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Lenius Basket, a PHP package to handle |
|
7
|
|
|
* your shopping basket. |
|
8
|
|
|
* |
|
9
|
|
|
* Copyright (c) 2017 Lenius. |
|
10
|
|
|
* https://github.com/lenius/basket |
|
11
|
|
|
* |
|
12
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
13
|
|
|
* file that was distributed with this source code. |
|
14
|
|
|
* |
|
15
|
|
|
* @author Carsten Jonstrup<[email protected]> |
|
16
|
|
|
* @copyright 2017 Lenius. |
|
17
|
|
|
* |
|
18
|
|
|
* @version production |
|
19
|
|
|
* |
|
20
|
|
|
* @link https://github.com/lenius/basket |
|
21
|
|
|
*/ |
|
22
|
|
|
use Lenius\Basket\Basket; |
|
23
|
|
|
use Lenius\Basket\Item; |
|
24
|
|
|
use PHPUnit\Framework\TestCase; |
|
25
|
|
|
|
|
26
|
|
|
class ItemTest extends TestCase |
|
27
|
|
|
{ |
|
28
|
|
|
private $item; |
|
29
|
|
|
|
|
30
|
|
|
public function setUp(): void |
|
31
|
|
|
{ |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function tearDown(): void |
|
35
|
|
|
{ |
|
36
|
|
|
$this->item = null; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testTaxUpdate(): void |
|
40
|
|
|
{ |
|
41
|
|
|
$item = [ |
|
42
|
|
|
'id' => 'foo', |
|
43
|
|
|
'name' => 'bar', |
|
44
|
|
|
'price' => 100, |
|
45
|
|
|
'quantity' => 1, |
|
46
|
|
|
'weight' => 200, |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
$this->item = new Item($item); |
|
50
|
|
|
|
|
51
|
|
|
$this->item->update('tax', 0); |
|
52
|
|
|
$this->assertEquals(100, $this->item->total()); |
|
53
|
|
|
$this->assertEquals(100, $this->item->total(false)); |
|
54
|
|
|
|
|
55
|
|
|
$this->item->update('tax', 20); |
|
56
|
|
|
$this->assertEquals(120, $this->item->total()); |
|
57
|
|
|
$this->assertEquals(100, $this->item->total(false)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
View Code Duplication |
public function testWeight(): void |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$weight = rand(200, 300); |
|
63
|
|
|
$quantity = rand(1, 10); |
|
64
|
|
|
|
|
65
|
|
|
$item = [ |
|
66
|
|
|
'id' => 'foo', |
|
67
|
|
|
'name' => 'bar', |
|
68
|
|
|
'price' => 100, |
|
69
|
|
|
'quantity' => $quantity, |
|
70
|
|
|
'weight' => $weight, |
|
71
|
|
|
]; |
|
72
|
|
|
|
|
73
|
|
|
$this->item = new Item($item); |
|
74
|
|
|
$this->assertEquals(($weight * $quantity), $this->item->weight()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testWeightWithOption(): void |
|
78
|
|
|
{ |
|
79
|
|
|
$weight = rand(200, 300); |
|
80
|
|
|
$weight_option_one = rand(50, 100); |
|
81
|
|
|
$weight_option_two = rand(150, 200); |
|
82
|
|
|
$quantity = rand(1, 10); |
|
83
|
|
|
|
|
84
|
|
|
$item = [ |
|
85
|
|
|
'id' => 'foo', |
|
86
|
|
|
'name' => 'bar', |
|
87
|
|
|
'price' => 100, |
|
88
|
|
|
'quantity' => $quantity, |
|
89
|
|
|
'weight' => $weight, |
|
90
|
|
|
'options' => [ |
|
91
|
|
|
[ |
|
92
|
|
|
'name' => 'Size', |
|
93
|
|
|
'value' => 'L', |
|
94
|
|
|
'weight' => $weight_option_one, |
|
95
|
|
|
'price' => 100, |
|
96
|
|
|
], |
|
97
|
|
|
[ |
|
98
|
|
|
'name' => 'Color', |
|
99
|
|
|
'value' => 'Black', |
|
100
|
|
|
'weight' => $weight_option_two, |
|
101
|
|
|
'price' => 100, |
|
102
|
|
|
], |
|
103
|
|
|
], |
|
104
|
|
|
]; |
|
105
|
|
|
|
|
106
|
|
|
$this->item = new Item($item); |
|
107
|
|
|
$this->assertEquals(($weight + ($weight_option_one + $weight_option_two)) * $quantity, $this->item->weight()); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function testHasOption(): void |
|
111
|
|
|
{ |
|
112
|
|
|
$item = [ |
|
113
|
|
|
'id' => 'foo', |
|
114
|
|
|
'name' => 'bar', |
|
115
|
|
|
'price' => 100, |
|
116
|
|
|
'quantity' => 1, |
|
117
|
|
|
'weight' => 200, |
|
118
|
|
|
'options' => [ |
|
119
|
|
|
[ |
|
120
|
|
|
'name' => 'Size', |
|
121
|
|
|
'value' => 'L', |
|
122
|
|
|
'weight' => 50, |
|
123
|
|
|
'price' => 100, |
|
124
|
|
|
], |
|
125
|
|
|
], |
|
126
|
|
|
]; |
|
127
|
|
|
|
|
128
|
|
|
$this->item = new Item($item); |
|
129
|
|
|
$this->assertTrue($this->item->hasOptions()); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function testHasNoOption(): void |
|
133
|
|
|
{ |
|
134
|
|
|
$item = [ |
|
135
|
|
|
'id' => 'foo', |
|
136
|
|
|
'name' => 'bar', |
|
137
|
|
|
'price' => 100, |
|
138
|
|
|
'quantity' => 1, |
|
139
|
|
|
'weight' => 200, |
|
140
|
|
|
]; |
|
141
|
|
|
|
|
142
|
|
|
$this->item = new Item($item); |
|
143
|
|
|
$this->assertFalse($this->item->hasOptions()); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
View Code Duplication |
public function testItemTotalPrice(): void |
|
|
|
|
|
|
147
|
|
|
{ |
|
148
|
|
|
$item = [ |
|
149
|
|
|
'id' => 'foo', |
|
150
|
|
|
'name' => 'bar', |
|
151
|
|
|
'price' => 100, |
|
152
|
|
|
'quantity' => 1, |
|
153
|
|
|
'weight' => 200, |
|
154
|
|
|
]; |
|
155
|
|
|
|
|
156
|
|
|
$this->item = new Item($item); |
|
157
|
|
|
|
|
158
|
|
|
$this->assertEquals(100, $this->item->single()); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
View Code Duplication |
public function testItemSetTax(): void |
|
|
|
|
|
|
162
|
|
|
{ |
|
163
|
|
|
$item = [ |
|
164
|
|
|
'id' => 'foo', |
|
165
|
|
|
'name' => 'bar', |
|
166
|
|
|
'price' => 100, |
|
167
|
|
|
'quantity' => 1, |
|
168
|
|
|
'weight' => 200, |
|
169
|
|
|
]; |
|
170
|
|
|
|
|
171
|
|
|
$this->item = new Item($item); |
|
172
|
|
|
|
|
173
|
|
|
$this->item->tax = 25; |
|
174
|
|
|
|
|
175
|
|
|
$this->assertEquals(25, $this->item->tax()); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.