1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace 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
|
|
|
* http://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 http://github.com/lenius/basket |
21
|
|
|
*/ |
22
|
|
|
use Lenius\Basket\Basket; |
23
|
|
|
use Lenius\Basket\Item; |
24
|
|
|
use Lenius\Basket\Storage\Runtime as RuntimeStore; |
25
|
|
|
use PHPUnit\Framework\TestCase; |
26
|
|
|
|
27
|
|
|
class ItemTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
private $item; |
30
|
|
|
|
31
|
|
|
public function setUp() |
32
|
|
|
{ |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function tearDown() |
36
|
|
|
{ |
37
|
|
|
$this->item = null; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testTaxUpdate() |
41
|
|
|
{ |
42
|
|
|
$identifier = 1; |
43
|
|
|
|
44
|
|
|
$item = [ |
45
|
|
|
'id' => 'foo', |
46
|
|
|
'name' => 'bar', |
47
|
|
|
'price' => 100, |
48
|
|
|
'quantity' => 1, |
49
|
|
|
'weight' => 200, |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
$this->item = new Item($identifier, $item, new RuntimeStore()); |
53
|
|
|
|
54
|
|
|
$this->item->update('tax', 0); |
55
|
|
|
$this->assertEquals(100, $this->item->total()); |
56
|
|
|
$this->assertEquals(100, $this->item->total(false)); |
57
|
|
|
|
58
|
|
|
$this->item->update('tax', 20); |
59
|
|
|
$this->assertEquals(120, $this->item->total()); |
60
|
|
|
$this->assertEquals(100, $this->item->total(false)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testWeight() |
64
|
|
|
{ |
65
|
|
|
$identifier = 1; |
66
|
|
|
|
67
|
|
|
$weight = rand(200, 300); |
68
|
|
|
$quantity = rand(1, 10); |
69
|
|
|
|
70
|
|
|
$item = [ |
71
|
|
|
'id' => 'foo', |
72
|
|
|
'name' => 'bar', |
73
|
|
|
'price' => 100, |
74
|
|
|
'quantity' => $quantity, |
75
|
|
|
'weight' => $weight, |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
$this->item = new Item($identifier, $item, new RuntimeStore()); |
79
|
|
|
$this->assertEquals(($weight * $quantity), $this->item->weight()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testWeightWithOption() |
83
|
|
|
{ |
84
|
|
|
$identifier = 1; |
85
|
|
|
|
86
|
|
|
$weight = rand(200, 300); |
87
|
|
|
$weight_option = rand(50, 800); |
88
|
|
|
$quantity = rand(1, 10); |
89
|
|
|
|
90
|
|
|
$item = [ |
91
|
|
|
'id' => 'foo', |
92
|
|
|
'name' => 'bar', |
93
|
|
|
'price' => 100, |
94
|
|
|
'quantity' => $quantity, |
95
|
|
|
'weight' => $weight, |
96
|
|
|
'options' => [ |
97
|
|
|
[ |
98
|
|
|
'name' => 'Size', |
99
|
|
|
'value' => 'L', |
100
|
|
|
'weight' => $weight_option, |
101
|
|
|
'price' => 100, |
102
|
|
|
], |
103
|
|
|
], |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
$this->item = new Item($identifier, $item, new RuntimeStore()); |
107
|
|
|
$this->assertEquals(($weight + $weight_option) * $quantity, $this->item->weight()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
View Code Duplication |
public function testHasOption() |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$identifier = 1; |
113
|
|
|
|
114
|
|
|
$item = [ |
115
|
|
|
'id' => 'foo', |
116
|
|
|
'name' => 'bar', |
117
|
|
|
'price' => 100, |
118
|
|
|
'quantity' => 1, |
119
|
|
|
'weight' => 200, |
120
|
|
|
'options' => [ |
121
|
|
|
[ |
122
|
|
|
'name' => 'Size', |
123
|
|
|
'value' => 'L', |
124
|
|
|
'weight' => 50, |
125
|
|
|
'price' => 100, |
126
|
|
|
], |
127
|
|
|
], |
128
|
|
|
]; |
129
|
|
|
|
130
|
|
|
$this->item = new Item($identifier, $item, new RuntimeStore()); |
131
|
|
|
$this->assertTrue($this->item->hasOptions()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
View Code Duplication |
public function testHasNotOption() |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$identifier = 1; |
137
|
|
|
|
138
|
|
|
$item = [ |
139
|
|
|
'id' => 'foo', |
140
|
|
|
'name' => 'bar', |
141
|
|
|
'price' => 100, |
142
|
|
|
'quantity' => 1, |
143
|
|
|
'weight' => 200, |
144
|
|
|
'options' => [ |
145
|
|
|
[ |
146
|
|
|
'name' => 'Size', |
147
|
|
|
'value' => 'L', |
148
|
|
|
'weight' => 50, |
149
|
|
|
'price' => 100, |
150
|
|
|
], |
151
|
|
|
], |
152
|
|
|
]; |
153
|
|
|
|
154
|
|
|
$this->item = new Item($identifier, $item, new RuntimeStore()); |
155
|
|
|
$this->assertEquals(250, $this->item->weight()); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
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.