1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Lenius Basket, a PHP package to handle |
5
|
|
|
* your shopping basket. |
6
|
|
|
* |
7
|
|
|
* Copyright (c) 2017 Lenius. |
8
|
|
|
* http://github.com/lenius/basket |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @author Carsten Jonstrup<[email protected]> |
14
|
|
|
* @copyright 2017 Lenius. |
15
|
|
|
* |
16
|
|
|
* @version dev |
17
|
|
|
* |
18
|
|
|
* @link http://github.com/lenius/basket |
19
|
|
|
*/ |
20
|
|
|
use Lenius\Basket\Basket; |
21
|
|
|
use Lenius\Basket\Identifier\Runtime as RuntimeIdentifier; |
22
|
|
|
use Lenius\Basket\Storage\Runtime as RuntimeStore; |
23
|
|
|
use PHPUnit\Framework\TestCase; |
24
|
|
|
|
25
|
|
|
class BasketTest extends TestCase |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
public function setUp() |
28
|
|
|
{ |
29
|
|
|
$this->basket = new Basket(new RuntimeStore(), new RuntimeIdentifier()); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function tearDown() |
33
|
|
|
{ |
34
|
|
|
$this->basket->destroy(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testInsert() |
38
|
|
|
{ |
39
|
|
|
$actualId = $this->basket->insert([ |
40
|
|
|
'id' => 'foo', |
41
|
|
|
'name' => 'bar', |
42
|
|
|
'price' => 100, |
43
|
|
|
'quantity' => 1, |
44
|
|
|
'weight' => 200, |
45
|
|
|
]); |
46
|
|
|
|
47
|
|
|
$identifier = md5('foo'.serialize([])); |
48
|
|
|
|
49
|
|
|
$this->assertEquals($identifier, $actualId); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testInsertIncrements() |
53
|
|
|
{ |
54
|
|
|
$this->basket->insert([ |
55
|
|
|
'id' => 'foo', |
56
|
|
|
'name' => 'bar', |
57
|
|
|
'price' => 150, |
58
|
|
|
'quantity' => 1, |
59
|
|
|
'weight' => 200, |
60
|
|
|
]); |
61
|
|
|
|
62
|
|
|
$this->assertEquals($this->basket->total(), 150); |
63
|
|
|
|
64
|
|
|
$this->basket->insert([ |
65
|
|
|
'id' => 'foo', |
66
|
|
|
'name' => 'bar', |
67
|
|
|
'price' => 150, |
68
|
|
|
'quantity' => 1, |
69
|
|
|
'weight' => 200, |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
$this->assertEquals($this->basket->total(), 300); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
public function testUpdate() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$actualId = $this->basket->insert([ |
78
|
|
|
'id' => 'foo', |
79
|
|
|
'name' => 'bar', |
80
|
|
|
'price' => 100, |
81
|
|
|
'quantity' => 1, |
82
|
|
|
'weight' => 200, |
83
|
|
|
]); |
84
|
|
|
|
85
|
|
|
$this->basket->update($actualId, 'name', 'baz'); |
86
|
|
|
|
87
|
|
|
$this->assertEquals($this->basket->item($actualId)->name, 'baz'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
View Code Duplication |
public function testMagicUpdate() |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$actualId = $this->basket->insert([ |
93
|
|
|
'id' => 'foo', |
94
|
|
|
'name' => 'bar', |
95
|
|
|
'price' => 100, |
96
|
|
|
'quantity' => 1, |
97
|
|
|
'weight' => 200, |
98
|
|
|
]); |
99
|
|
|
|
100
|
|
|
foreach ($this->basket->contents() as $item) { |
101
|
|
|
$item->name = 'bim'; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$this->assertEquals($this->basket->item($actualId)->name, 'bim'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testOptions() |
108
|
|
|
{ |
109
|
|
|
$actualId = $this->basket->insert([ |
110
|
|
|
'id' => 'foo', |
111
|
|
|
'name' => 'bar', |
112
|
|
|
'price' => 100, |
113
|
|
|
'quantity' => 1, |
114
|
|
|
'weight' => 200, |
115
|
|
|
'options' => [ |
116
|
|
|
'size' => 'L', |
117
|
|
|
], |
118
|
|
|
]); |
119
|
|
|
|
120
|
|
|
$item = $this->basket->item($actualId); |
121
|
|
|
|
122
|
|
|
$this->assertTrue($item->hasOptions()); |
123
|
|
|
$this->assertNotEmpty($item->options); |
124
|
|
|
|
125
|
|
|
$item->options = []; |
126
|
|
|
|
127
|
|
|
$this->assertFalse($item->hasOptions()); |
128
|
|
|
$this->assertEmpty($item->options); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testWeight() |
132
|
|
|
{ |
133
|
|
|
$weight = rand(200, 300); |
134
|
|
|
|
135
|
|
|
$this->basket->insert([ |
136
|
|
|
'id' => 'foo', |
137
|
|
|
'name' => 'bar', |
138
|
|
|
'price' => 100, |
139
|
|
|
'quantity' => 1, |
140
|
|
|
'weight' => $weight, |
141
|
|
|
'options' => [ |
142
|
|
|
'size' => 'L', |
143
|
|
|
], |
144
|
|
|
]); |
145
|
|
|
|
146
|
|
|
// Test that the total weight is being calculated successfully |
147
|
|
|
$this->assertEquals($this->basket->weight(), $weight); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
View Code Duplication |
public function testWeightOption() |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$weight = rand(200, 300); |
153
|
|
|
$weight_option = rand(50, 800); |
154
|
|
|
$quantity = rand(1, 10); |
155
|
|
|
|
156
|
|
|
$this->basket->insert([ |
157
|
|
|
'id' => 'foo', |
158
|
|
|
'name' => 'bar', |
159
|
|
|
'price' => 100, |
160
|
|
|
'quantity' => $quantity , |
161
|
|
|
'weight' => $weight, |
162
|
|
|
'options' => [ |
163
|
|
|
'size' => 'L', |
164
|
|
|
'weight' => $weight_option, |
165
|
|
|
], |
166
|
|
|
]); |
167
|
|
|
|
168
|
|
|
// Test that the total weight is being calculated successfully |
169
|
|
|
$this->assertEquals($this->basket->weight(), ($weight + $weight_option) * $quantity ); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
View Code Duplication |
public function testPriceOption() |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
$weight = rand(200, 300); |
175
|
|
|
$weight_option = rand(50, 800); |
176
|
|
|
$quantity = rand(1, 10); |
177
|
|
|
|
178
|
|
|
$this->basket->insert([ |
179
|
|
|
'id' => 'foo', |
180
|
|
|
'name' => 'bar', |
181
|
|
|
'price' => 100, |
182
|
|
|
'quantity' => $quantity , |
183
|
|
|
'weight' => $weight, |
184
|
|
|
'options' => [ |
185
|
|
|
'size' => 'L', |
186
|
|
|
'weight' => $weight_option, |
187
|
|
|
], |
188
|
|
|
]); |
189
|
|
|
|
190
|
|
|
// Test that the total weight is being calculated successfully |
191
|
|
|
$this->assertEquals($this->basket->weight(), ($weight + $weight_option) * $quantity ); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function testFind() |
195
|
|
|
{ |
196
|
|
|
$this->basket->insert([ |
197
|
|
|
'id' => 'foo', |
198
|
|
|
'name' => 'bar', |
199
|
|
|
'price' => 100, |
200
|
|
|
'quantity' => 1, |
201
|
|
|
'weight' => 200, |
202
|
|
|
]); |
203
|
|
|
|
204
|
|
|
$this->assertInstanceOf('\Lenius\Basket\Item', $this->basket->find('foo')); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function testTotals() |
208
|
|
|
{ |
209
|
|
|
// Generate a random price and quantity |
210
|
|
|
$price = rand(20, 99999); |
211
|
|
|
$quantity = rand(1, 10); |
212
|
|
|
|
213
|
|
|
$this->basket->insert([ |
214
|
|
|
'id' => 'foo', |
215
|
|
|
'name' => 'bar', |
216
|
|
|
'price' => $price, |
217
|
|
|
'quantity' => $quantity, |
218
|
|
|
'weight' => 200, |
219
|
|
|
]); |
220
|
|
|
|
221
|
|
|
// Test that the total is being calculated successfully |
222
|
|
|
$this->assertEquals($this->basket->total(), $price * $quantity); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function testTotalItems() |
226
|
|
|
{ |
227
|
|
|
$adding = rand(1, 200); |
228
|
|
|
$actualTotal = 0; |
229
|
|
|
|
230
|
|
|
for ($i = 1; $i <= $adding; $i++) { |
231
|
|
|
$quantity = rand(1, 20); |
232
|
|
|
|
233
|
|
|
$this->basket->insert([ |
234
|
|
|
'id' => uniqid(), |
235
|
|
|
'name' => 'bar', |
236
|
|
|
'price' => 100, |
237
|
|
|
'quantity' => $quantity, |
238
|
|
|
'weight' => 200, |
239
|
|
|
]); |
240
|
|
|
|
241
|
|
|
$actualTotal += $quantity; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$this->assertEquals($this->basket->totalItems(), $actualTotal); |
245
|
|
|
$this->assertEquals($this->basket->totalItems(true), $adding); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
View Code Duplication |
public function testItemRemoval() |
|
|
|
|
249
|
|
|
{ |
250
|
|
|
$this->basket->insert([ |
251
|
|
|
'id' => 'foo', |
252
|
|
|
'name' => 'bar', |
253
|
|
|
'price' => 100, |
254
|
|
|
'quantity' => 1, |
255
|
|
|
'weight' => 200, |
256
|
|
|
]); |
257
|
|
|
|
258
|
|
|
$contents = &$this->basket->contents(); |
259
|
|
|
|
260
|
|
|
$this->assertNotEmpty($contents); |
261
|
|
|
|
262
|
|
|
foreach ($contents as $item) { |
263
|
|
|
$item->remove(); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
$this->assertEmpty($contents); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
View Code Duplication |
public function testAlternateItemRemoval() |
|
|
|
|
270
|
|
|
{ |
271
|
|
|
$this->basket->insert([ |
272
|
|
|
'id' => 'foo', |
273
|
|
|
'name' => 'bar', |
274
|
|
|
'price' => 100, |
275
|
|
|
'quantity' => 1, |
276
|
|
|
'weight' => 200, |
277
|
|
|
]); |
278
|
|
|
|
279
|
|
|
$contents = &$this->basket->contents(); |
280
|
|
|
|
281
|
|
|
$this->assertNotEmpty($contents); |
282
|
|
|
|
283
|
|
|
foreach ($contents as $identifier => $item) { |
284
|
|
|
$this->basket->remove($identifier); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
$this->assertEmpty($contents); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
View Code Duplication |
public function testItemToArray() |
|
|
|
|
291
|
|
|
{ |
292
|
|
|
$actualId = $this->basket->insert([ |
293
|
|
|
'id' => 'foo', |
294
|
|
|
'name' => 'bar', |
295
|
|
|
'price' => 100, |
296
|
|
|
'quantity' => 1, |
297
|
|
|
'weight' => 200, |
298
|
|
|
]); |
299
|
|
|
|
300
|
|
|
$this->assertTrue(is_array($this->basket->item($actualId)->toArray())); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
View Code Duplication |
public function testbasketToArray() |
|
|
|
|
304
|
|
|
{ |
305
|
|
|
$this->basket->insert([ |
306
|
|
|
'id' => 'foo', |
307
|
|
|
'name' => 'bar', |
308
|
|
|
'price' => 100, |
309
|
|
|
'quantity' => 1, |
310
|
|
|
'weight' => 200, |
311
|
|
|
]); |
312
|
|
|
|
313
|
|
|
foreach ($this->basket->contents(true) as $item) { |
314
|
|
|
$this->assertTrue(is_array($item)); |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|
318
|
|
View Code Duplication |
public function testTax() |
|
|
|
|
319
|
|
|
{ |
320
|
|
|
$this->basket->insert([ |
321
|
|
|
'id' => 'foo', |
322
|
|
|
'name' => 'bar', |
323
|
|
|
'price' => 100, |
324
|
|
|
'quantity' => 1, |
325
|
|
|
'tax' => 20, |
326
|
|
|
'weight' => 200, |
327
|
|
|
]); |
328
|
|
|
|
329
|
|
|
// Test that the tax is being calculated successfully |
330
|
|
|
$this->assertEquals($this->basket->total(), 120); |
331
|
|
|
|
332
|
|
|
// Test that the total method can also return the pre-tax price if false is passed |
333
|
|
|
$this->assertEquals($this->basket->total(false), 100); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
View Code Duplication |
public function testTaxMultiply() |
|
|
|
|
337
|
|
|
{ |
338
|
|
|
$this->basket->insert([ |
339
|
|
|
'id' => 'foo', |
340
|
|
|
'name' => 'bar', |
341
|
|
|
'price' => 100, |
342
|
|
|
'quantity' => 2, |
343
|
|
|
'tax' => 20, |
344
|
|
|
'weight' => 200, |
345
|
|
|
]); |
346
|
|
|
|
347
|
|
|
// Test that the tax is being calculated successfully |
348
|
|
|
$this->assertEquals($this->basket->total(), 240); |
349
|
|
|
|
350
|
|
|
// Test that the total method can also return the pre-tax price if false is passed |
351
|
|
|
$this->assertEquals($this->basket->total(false), 200); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
public function testTaxUpdate() |
355
|
|
|
{ |
356
|
|
|
$this->basket->insert([ |
357
|
|
|
'id' => 'foo', |
358
|
|
|
'name' => 'bar', |
359
|
|
|
'price' => 100, |
360
|
|
|
'quantity' => 1, |
361
|
|
|
'tax' => 20, |
362
|
|
|
'weight' => 200, |
363
|
|
|
]); |
364
|
|
|
|
365
|
|
|
$identifier = md5('foo'.serialize([])); |
366
|
|
|
|
367
|
|
|
$item = $this->basket->item($identifier); |
368
|
|
|
|
369
|
|
|
// Test that the tax is being calculated successfully |
370
|
|
|
$item->tax = 0; |
371
|
|
|
$this->assertEquals($item->total(), 100); |
372
|
|
|
$this->assertEquals($item->total(false), 100); |
373
|
|
|
|
374
|
|
|
$item->tax = 20; |
375
|
|
|
$this->assertEquals($item->total(), 120); |
376
|
|
|
$this->assertEquals($item->total(false), 100); |
377
|
|
|
|
378
|
|
|
$item->update('tax', 0); |
379
|
|
|
$this->assertEquals($item->total(), 100); |
380
|
|
|
$this->assertEquals($item->total(false), 100); |
381
|
|
|
|
382
|
|
|
$item->update('tax', 20); |
383
|
|
|
$this->assertEquals($item->total(), 120); |
384
|
|
|
$this->assertEquals($item->total(false), 100); |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.