Completed
Push — master ( 81cf23...9b1850 )
by Carsten
04:22
created

BasketTest::testWeight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
26
{
27
    public function setUp()
28
    {
29
        $this->basket = new Basket(new RuntimeStore(), new RuntimeIdentifier());
0 ignored issues
show
Bug introduced by
The property basket does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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()
1 ignored issue
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...
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()
1 ignored issue
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...
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 = 200;
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
151
    public function testWeightOption()
152
    {
153
        $weight = 200;
154
        $weight_option = 50;
155
156
        $this->basket->insert([
157
            'id'       => 'foo',
158
            'name'     => 'bar',
159
            'price'    => 100,
160
            'quantity' => 1,
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);
170
171
    }
172
173
    public function testFind()
174
    {
175
        $this->basket->insert([
176
            'id'       => 'foo',
177
            'name'     => 'bar',
178
            'price'    => 100,
179
            'quantity' => 1,
180
            'weight'   => 200,
181
        ]);
182
183
        $this->assertInstanceOf('\Lenius\Basket\Item', $this->basket->find('foo'));
184
    }
185
186
    public function testTotals()
187
    {
188
        // Generate a random price and quantity
189
        $price = rand(20, 99999);
190
        $quantity = rand(1, 10);
191
192
        $this->basket->insert([
193
            'id'       => 'foo',
194
            'name'     => 'bar',
195
            'price'    => $price,
196
            'quantity' => $quantity,
197
            'weight'   => 200,
198
        ]);
199
200
        // Test that the total is being calculated successfully
201
        $this->assertEquals($this->basket->total(), $price * $quantity);
202
    }
203
204
    public function testTotalItems()
205
    {
206
        $adding = rand(1, 200);
207
        $actualTotal = 0;
208
209
        for ($i = 1; $i <= $adding; $i++) {
210
            $quantity = rand(1, 20);
211
212
            $this->basket->insert([
213
                'id'       => uniqid(),
214
                'name'     => 'bar',
215
                'price'    => 100,
216
                'quantity' => $quantity,
217
                'weight'   => 200,
218
            ]);
219
220
            $actualTotal += $quantity;
221
        }
222
223
        $this->assertEquals($this->basket->totalItems(), $actualTotal);
224
        $this->assertEquals($this->basket->totalItems(true), $adding);
225
    }
226
227 View Code Duplication
    public function testItemRemoval()
1 ignored issue
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...
228
    {
229
        $this->basket->insert([
230
            'id'       => 'foo',
231
            'name'     => 'bar',
232
            'price'    => 100,
233
            'quantity' => 1,
234
            'weight'   => 200,
235
        ]);
236
237
        $contents = &$this->basket->contents();
238
239
        $this->assertNotEmpty($contents);
240
241
        foreach ($contents as $item) {
242
            $item->remove();
243
        }
244
245
        $this->assertEmpty($contents);
246
    }
247
248 View Code Duplication
    public function testAlternateItemRemoval()
1 ignored issue
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...
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 $identifier => $item) {
263
            $this->basket->remove($identifier);
264
        }
265
266
        $this->assertEmpty($contents);
267
    }
268
269 View Code Duplication
    public function testItemToArray()
1 ignored issue
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...
270
    {
271
        $actualId = $this->basket->insert([
272
            'id'       => 'foo',
273
            'name'     => 'bar',
274
            'price'    => 100,
275
            'quantity' => 1,
276
            'weight'   => 200,
277
        ]);
278
279
        $this->assertTrue(is_array($this->basket->item($actualId)->toArray()));
280
    }
281
282 View Code Duplication
    public function testbasketToArray()
1 ignored issue
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...
283
    {
284
        $this->basket->insert([
285
            'id'       => 'foo',
286
            'name'     => 'bar',
287
            'price'    => 100,
288
            'quantity' => 1,
289
            'weight'   => 200,
290
        ]);
291
292
        foreach ($this->basket->contents(true) as $item) {
293
            $this->assertTrue(is_array($item));
294
        }
295
    }
296
297
    public function testTax()
298
    {
299
        $this->basket->insert([
300
            'id'       => 'foo',
301
            'name'     => 'bar',
302
            'price'    => 100,
303
            'quantity' => 1,
304
            'tax'      => 20,
305
            'weight'   => 200,
306
        ]);
307
308
        // Test that the tax is being calculated successfully
309
        $this->assertEquals($this->basket->total(), 120);
310
311
        // Test that the total method can also return the pre-tax price if false is passed
312
        $this->assertEquals($this->basket->total(false), 100);
313
    }
314
315
    public function testTaxUpdate()
316
    {
317
        $this->basket->insert([
318
            'id'       => 'foo',
319
            'name'     => 'bar',
320
            'price'    => 100,
321
            'quantity' => 1,
322
            'tax'      => 20,
323
            'weight'   => 200,
324
        ]);
325
326
        $identifier = md5('foo'.serialize([]));
327
328
        $item = $this->basket->item($identifier);
329
330
        // Test that the tax is being calculated successfully
331
        $item->tax = 0;
332
        $this->assertEquals($item->total(), 100);
333
        $this->assertEquals($item->total(false), 100);
334
335
        $item->tax = 20;
336
        $this->assertEquals($item->total(), 120);
337
        $this->assertEquals($item->total(false), 100);
338
339
        $item->update('tax', 0);
340
        $this->assertEquals($item->total(), 100);
341
        $this->assertEquals($item->total(false), 100);
342
343
        $item->update('tax', 20);
344
        $this->assertEquals($item->total(), 120);
345
        $this->assertEquals($item->total(false), 100);
346
    }
347
}
348