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