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_one = rand(50, 100); |
88
|
|
|
$weight_option_two = rand(150, 200); |
89
|
|
|
$quantity = rand(1, 10); |
90
|
|
|
|
91
|
|
|
$item = [ |
92
|
|
|
'id' => 'foo', |
93
|
|
|
'name' => 'bar', |
94
|
|
|
'price' => 100, |
95
|
|
|
'quantity' => $quantity, |
96
|
|
|
'weight' => $weight, |
97
|
|
|
'options' => [ |
98
|
|
|
[ |
99
|
|
|
'name' => 'Size', |
100
|
|
|
'value' => 'L', |
101
|
|
|
'weight' => $weight_option_one, |
102
|
|
|
'price' => 100, |
103
|
|
|
], |
104
|
|
|
[ |
105
|
|
|
'name' => 'Color', |
106
|
|
|
'value' => 'Black', |
107
|
|
|
'weight' => $weight_option_two, |
108
|
|
|
'price' => 100, |
109
|
|
|
], |
110
|
|
|
], |
111
|
|
|
]; |
112
|
|
|
|
113
|
|
|
$this->item = new Item($identifier, $item, new RuntimeStore()); |
114
|
|
|
$this->assertEquals(($weight + ($weight_option_one + $weight_option_two)) * $quantity, $this->item->weight()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testHasOption() |
118
|
|
|
{ |
119
|
|
|
$identifier = 1; |
120
|
|
|
|
121
|
|
|
$item = [ |
122
|
|
|
'id' => 'foo', |
123
|
|
|
'name' => 'bar', |
124
|
|
|
'price' => 100, |
125
|
|
|
'quantity' => 1, |
126
|
|
|
'weight' => 200, |
127
|
|
|
'options' => [ |
128
|
|
|
[ |
129
|
|
|
'name' => 'Size', |
130
|
|
|
'value' => 'L', |
131
|
|
|
'weight' => 50, |
132
|
|
|
'price' => 100, |
133
|
|
|
], |
134
|
|
|
], |
135
|
|
|
]; |
136
|
|
|
|
137
|
|
|
$this->item = new Item($identifier, $item, new RuntimeStore()); |
138
|
|
|
$this->assertTrue($this->item->hasOptions()); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testHasNoOption() |
142
|
|
|
{ |
143
|
|
|
$identifier = 1; |
144
|
|
|
|
145
|
|
|
$item = [ |
146
|
|
|
'id' => 'foo', |
147
|
|
|
'name' => 'bar', |
148
|
|
|
'price' => 100, |
149
|
|
|
'quantity' => 1, |
150
|
|
|
'weight' => 200, |
151
|
|
|
]; |
152
|
|
|
|
153
|
|
|
$this->item = new Item($identifier, $item, new RuntimeStore()); |
154
|
|
|
$this->assertFalse($this->item->hasOptions()); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|