Passed
Pull Request — master (#4)
by Carsten
09:22
created

BasketTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
 * 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;
32
33
    public function setUp()
34
    {
35
        $this->basket = new Basket(new RuntimeStore(), new RuntimeIdentifier());
36
    }
37
38
    public function tearDown()
39
    {
40
        $this->basket->destroy();
41
    }
42
43
    public function testInsert()
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()
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 View Code Duplication
    public function testUpdate()
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 View Code Duplication
    public function testMagicUpdate()
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()
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()
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 View Code Duplication
    public function testPriceOption()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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 View Code Duplication
    public function testPriceDistractOption()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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 View Code Duplication
    public function testFind()
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 View Code Duplication
    public function testTotals()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
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()
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 View Code Duplication
    public function testItemToArray()
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 View Code Duplication
    public function testbasketToArray()
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 View Code Duplication
    public function testTax()
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()
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()
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()
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