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\Identifier\Runtime as RuntimeIdentifier; |
24
|
|
|
use Lenius\Basket\Item; |
25
|
|
|
use Lenius\Basket\Storage\Runtime as RuntimeStore; |
26
|
|
|
use PHPUnit\Framework\TestCase; |
27
|
|
|
|
28
|
|
|
class BasketTest extends TestCase |
29
|
|
|
{ |
30
|
|
|
/** @var Basket */ |
31
|
|
|
private Basket $basket; |
32
|
|
|
|
33
|
|
|
public function setUp(): void |
34
|
|
|
{ |
35
|
|
|
$this->basket = new Basket(new RuntimeStore(), new RuntimeIdentifier()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function tearDown(): void |
39
|
|
|
{ |
40
|
|
|
$this->basket->destroy(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testInsert(): void |
44
|
|
|
{ |
45
|
|
|
$actualId = $this->basket->insert(new Item([ |
46
|
|
|
'id' => 'foo', |
47
|
|
|
'name' => 'bar', |
48
|
|
|
'price' => 100, |
49
|
|
|
'quantity' => 1, |
50
|
|
|
'weight' => 200, |
51
|
|
|
])); |
52
|
|
|
|
53
|
|
|
$identifier = md5('foo'.serialize([])); |
54
|
|
|
|
55
|
|
|
$this->assertEquals($identifier, $actualId); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testInsertIncrements(): void |
59
|
|
|
{ |
60
|
|
|
$this->basket->insert(new Item([ |
61
|
|
|
'id' => 'foo', |
62
|
|
|
'name' => 'bar', |
63
|
|
|
'price' => 150, |
64
|
|
|
'quantity' => 1, |
65
|
|
|
'weight' => 200, |
66
|
|
|
])); |
67
|
|
|
|
68
|
|
|
$this->assertEquals(150, $this->basket->total()); |
69
|
|
|
|
70
|
|
|
$this->basket->insert(new Item([ |
71
|
|
|
'id' => 'foo', |
72
|
|
|
'name' => 'bar', |
73
|
|
|
'price' => 150, |
74
|
|
|
'quantity' => 1, |
75
|
|
|
'weight' => 200, |
76
|
|
|
])); |
77
|
|
|
|
78
|
|
|
$this->assertEquals(300, $this->basket->total()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testUpdate(): void |
82
|
|
|
{ |
83
|
|
|
$actualId = $this->basket->insert(new Item([ |
84
|
|
|
'id' => 'foo', |
85
|
|
|
'name' => 'bar', |
86
|
|
|
'price' => 100, |
87
|
|
|
'quantity' => 1, |
88
|
|
|
'weight' => 200, |
89
|
|
|
])); |
90
|
|
|
|
91
|
|
|
$this->basket->update($actualId, 'name', 'baz'); |
92
|
|
|
|
93
|
|
|
$this->assertEquals('baz', $this->basket->item($actualId)->name); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testMagicUpdate(): void |
97
|
|
|
{ |
98
|
|
|
$actualId = $this->basket->insert(new Item([ |
99
|
|
|
'id' => 'zxy', |
100
|
|
|
'name' => 'dolly', |
101
|
|
|
'price' => 120, |
102
|
|
|
'quantity' => 3, |
103
|
|
|
'weight' => 400, |
104
|
|
|
])); |
105
|
|
|
|
106
|
|
|
foreach ($this->basket->contents() as $item) { |
107
|
|
|
$item->name = 'bim'; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->assertEquals('bim', $this->basket->item($actualId)->name); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testWeight(): void |
114
|
|
|
{ |
115
|
|
|
$weight = rand(200, 300); |
116
|
|
|
|
117
|
|
|
$this->basket->insert(new Item([ |
118
|
|
|
'id' => 'foo', |
119
|
|
|
'name' => 'bar', |
120
|
|
|
'price' => 100, |
121
|
|
|
'quantity' => 1, |
122
|
|
|
'weight' => $weight, |
123
|
|
|
'options' => [ |
124
|
|
|
[ |
125
|
|
|
'name' => 'size', |
126
|
|
|
'price' => 50, |
127
|
|
|
], |
128
|
|
|
], |
129
|
|
|
])); |
130
|
|
|
|
131
|
|
|
// test that the total weight is being calculated successfully |
132
|
|
|
$this->assertEquals($weight, $this->basket->weight()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testWeightOption(): void |
136
|
|
|
{ |
137
|
|
|
$weight = rand(200, 300); |
138
|
|
|
$weight_option = rand(50, 800); |
139
|
|
|
$quantity = rand(1, 10); |
140
|
|
|
|
141
|
|
|
$this->basket->insert(new Item([ |
142
|
|
|
'id' => 'foo', |
143
|
|
|
'name' => 'bar', |
144
|
|
|
'price' => 100, |
145
|
|
|
'quantity' => $quantity, |
146
|
|
|
'weight' => $weight, |
147
|
|
|
'options' => [ |
148
|
|
|
[ |
149
|
|
|
'name' => 'size', |
150
|
|
|
'price' => 50, |
151
|
|
|
'weight' => $weight_option, |
152
|
|
|
], |
153
|
|
|
], |
154
|
|
|
])); |
155
|
|
|
|
156
|
|
|
// test that the total weight is being calculated successfully |
157
|
|
|
$this->assertEquals(($weight + $weight_option) * $quantity, $this->basket->weight()); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function testPriceOption(): void |
161
|
|
|
{ |
162
|
|
|
$weight = rand(200, 300); |
163
|
|
|
$weight_option = rand(50, 100); |
164
|
|
|
|
165
|
|
|
$this->basket->insert(new Item([ |
166
|
|
|
'id' => 'foo', |
167
|
|
|
'name' => 'bar', |
168
|
|
|
'price' => 100, |
169
|
|
|
'tax' => 25, |
170
|
|
|
'quantity' => 1, |
171
|
|
|
'weight' => $weight, |
172
|
|
|
'options' => [ |
173
|
|
|
[ |
174
|
|
|
'name' => 'size', |
175
|
|
|
'price' => 50, |
176
|
|
|
'weight' => $weight_option, |
177
|
|
|
], |
178
|
|
|
[ |
179
|
|
|
'name' => 'color', |
180
|
|
|
'price' => 50, |
181
|
|
|
'weight' => $weight_option, |
182
|
|
|
], |
183
|
|
|
], |
184
|
|
|
])); |
185
|
|
|
|
186
|
|
|
// test that the total price is being calculated successfully |
187
|
|
|
$this->assertEquals(250, $this->basket->total(true)); |
188
|
|
|
$this->assertEquals(200, $this->basket->total(false)); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function testPriceDistractOption(): void |
192
|
|
|
{ |
193
|
|
|
$weight = rand(400, 500); |
194
|
|
|
$weight_option = rand(200, 300); |
195
|
|
|
|
196
|
|
|
$this->basket->insert(new Item([ |
197
|
|
|
'id' => 'zxy', |
198
|
|
|
'name' => 'pink', |
199
|
|
|
'price' => 100, |
200
|
|
|
'tax' => 25, |
201
|
|
|
'quantity' => 1, |
202
|
|
|
'weight' => $weight, |
203
|
|
|
'options' => [ |
204
|
|
|
[ |
205
|
|
|
'name' => 'type', |
206
|
|
|
'price' => -20, |
207
|
|
|
'weight' => $weight_option, |
208
|
|
|
], |
209
|
|
|
[ |
210
|
|
|
'name' => 'color', |
211
|
|
|
'price' => 50, |
212
|
|
|
'weight' => $weight_option, |
213
|
|
|
], |
214
|
|
|
], |
215
|
|
|
])); |
216
|
|
|
|
217
|
|
|
// test that the total price is being calculated successfully |
218
|
|
|
$this->assertEquals(162.50, $this->basket->total(true)); |
219
|
|
|
$this->assertEquals(130, $this->basket->total(false)); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function testFind(): void |
223
|
|
|
{ |
224
|
|
|
$this->basket->insert(new Item([ |
225
|
|
|
'id' => 'foo', |
226
|
|
|
'name' => 'bar', |
227
|
|
|
'price' => 100, |
228
|
|
|
'quantity' => 1, |
229
|
|
|
'weight' => 200, |
230
|
|
|
])); |
231
|
|
|
|
232
|
|
|
$this->assertInstanceOf('\Lenius\Basket\Item', $this->basket->find('foo')); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function testTotals(): void |
236
|
|
|
{ |
237
|
|
|
// Generate a random price and quantity |
238
|
|
|
$price = rand(20, 99999); |
239
|
|
|
$quantity = rand(1, 10); |
240
|
|
|
|
241
|
|
|
$this->basket->insert(new Item([ |
242
|
|
|
'id' => 'foo', |
243
|
|
|
'name' => 'bar', |
244
|
|
|
'price' => $price, |
245
|
|
|
'quantity' => $quantity, |
246
|
|
|
'weight' => 200, |
247
|
|
|
])); |
248
|
|
|
|
249
|
|
|
// test that the total is being calculated successfully |
250
|
|
|
$this->assertEquals($price * $quantity, $this->basket->total()); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function testTotalItems(): void |
254
|
|
|
{ |
255
|
|
|
$adding = rand(1, 200); |
256
|
|
|
$actualTotal = 0; |
257
|
|
|
|
258
|
|
|
for ($i = 1; $i <= $adding; $i++) { |
259
|
|
|
$quantity = rand(1, 20); |
260
|
|
|
|
261
|
|
|
$this->basket->insert(new Item([ |
262
|
|
|
'id' => uniqid(), |
263
|
|
|
'name' => 'bar', |
264
|
|
|
'price' => 100, |
265
|
|
|
'quantity' => $quantity, |
266
|
|
|
'weight' => 200, |
267
|
|
|
])); |
268
|
|
|
|
269
|
|
|
$actualTotal += $quantity; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
$this->assertEquals($this->basket->totalItems(), $actualTotal); |
273
|
|
|
$this->assertEquals($this->basket->totalItems(true), $adding); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function testAlternateItemRemoval(): void |
277
|
|
|
{ |
278
|
|
|
$this->basket->insert(new Item([ |
279
|
|
|
'id' => 'foo', |
280
|
|
|
'name' => 'bar', |
281
|
|
|
'price' => 100, |
282
|
|
|
'quantity' => 1, |
283
|
|
|
'weight' => 200, |
284
|
|
|
])); |
285
|
|
|
|
286
|
|
|
$contents = &$this->basket->contents(); |
287
|
|
|
|
288
|
|
|
$this->assertNotEmpty($contents); |
289
|
|
|
|
290
|
|
|
foreach ($contents as $identifier => $item) { |
291
|
|
|
$this->basket->remove($identifier); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
$this->assertEmpty($contents); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function testItemToArray(): void |
298
|
|
|
{ |
299
|
|
|
$actualId = $this->basket->insert(new Item([ |
300
|
|
|
'id' => 'ase', |
301
|
|
|
'name' => 'fly', |
302
|
|
|
'price' => 160, |
303
|
|
|
'quantity' => 12, |
304
|
|
|
'weight' => 123, |
305
|
|
|
])); |
306
|
|
|
|
307
|
|
|
$this->assertTrue(is_array($this->basket->item($actualId)->toArray())); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
public function testbasketToArray(): void |
311
|
|
|
{ |
312
|
|
|
$this->basket->insert(new Item([ |
313
|
|
|
'id' => 'asb', |
314
|
|
|
'name' => 'shuttle', |
315
|
|
|
'price' => 130, |
316
|
|
|
'quantity' => 11, |
317
|
|
|
'weight' => 60, |
318
|
|
|
])); |
319
|
|
|
|
320
|
|
|
foreach ($this->basket->contents(true) as $item) { |
321
|
|
|
$this->assertTrue(is_array($item)); |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
public function testTax(): void |
326
|
|
|
{ |
327
|
|
|
$this->basket->insert(new Item([ |
328
|
|
|
'id' => 'foo', |
329
|
|
|
'name' => 'bar', |
330
|
|
|
'price' => 100, |
331
|
|
|
'quantity' => 1, |
332
|
|
|
'tax' => 20, |
333
|
|
|
'weight' => 200, |
334
|
|
|
])); |
335
|
|
|
|
336
|
|
|
// Test that the tax is being calculated successfully |
337
|
|
|
$this->assertEquals(20, $this->basket->tax()); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
public function testTaxOptions(): void |
341
|
|
|
{ |
342
|
|
|
$this->basket->insert(new Item([ |
343
|
|
|
'id' => 'foo', |
344
|
|
|
'name' => 'bar', |
345
|
|
|
'price' => 100, |
346
|
|
|
'quantity' => 1, |
347
|
|
|
'tax' => 20, |
348
|
|
|
'weight' => 200, |
349
|
|
|
'options' => [ |
350
|
|
|
[ |
351
|
|
|
'name' => 'Size', |
352
|
|
|
'value' => 'L', |
353
|
|
|
'weight' => 50, |
354
|
|
|
'price' => 100, |
355
|
|
|
], |
356
|
|
|
], |
357
|
|
|
])); |
358
|
|
|
|
359
|
|
|
// test that the tax is being calculated successfully |
360
|
|
|
$this->assertEquals(40, $this->basket->tax()); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
public function testTaxMultiply(): void |
364
|
|
|
{ |
365
|
|
|
$this->basket->insert(new Item([ |
366
|
|
|
'id' => 'foo', |
367
|
|
|
'name' => 'bar', |
368
|
|
|
'price' => 100, |
369
|
|
|
'quantity' => 2, |
370
|
|
|
'tax' => 20, |
371
|
|
|
'weight' => 200, |
372
|
|
|
])); |
373
|
|
|
|
374
|
|
|
// test that the tax is being calculated successfully |
375
|
|
|
$this->assertEquals(240, $this->basket->total()); |
376
|
|
|
|
377
|
|
|
// test that the total method can also return the pre-tax price if false is passed |
378
|
|
|
$this->assertEquals(200, $this->basket->total(false)); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
public function testTaxUpdate(): void |
382
|
|
|
{ |
383
|
|
|
$this->basket->insert(new Item([ |
384
|
|
|
'id' => 'foo', |
385
|
|
|
'name' => 'bar', |
386
|
|
|
'price' => 100, |
387
|
|
|
'quantity' => 1, |
388
|
|
|
'tax' => 20, |
389
|
|
|
'weight' => 200, |
390
|
|
|
])); |
391
|
|
|
|
392
|
|
|
$identifier = md5('foo'.serialize([])); |
393
|
|
|
|
394
|
|
|
/** @var Item $item */ |
395
|
|
|
$item = $this->basket->item($identifier); |
396
|
|
|
|
397
|
|
|
// test that the tax is being calculated successfully |
398
|
|
|
$item->update('tax', 0); |
399
|
|
|
$this->assertEquals(100, $item->total()); |
400
|
|
|
$this->assertEquals(100, $item->total(false)); |
401
|
|
|
|
402
|
|
|
$item->update('tax', 20); |
403
|
|
|
$this->assertEquals(120, $item->total()); |
404
|
|
|
$this->assertEquals(100, $item->total(false)); |
405
|
|
|
|
406
|
|
|
$item->update('tax', 0); |
407
|
|
|
$this->assertEquals(100, $item->total()); |
408
|
|
|
$this->assertEquals(100, $item->total(false)); |
409
|
|
|
|
410
|
|
|
$item->update('tax', 20); |
411
|
|
|
$this->assertEquals(120, $item->total()); |
412
|
|
|
$this->assertEquals(100, $item->total(false)); |
413
|
|
|
} |
414
|
|
|
} |
415
|
|
|
|