ItemTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 97
c 1
b 0
f 0
dl 0
loc 170
rs 10
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 3 1
A testWeight() 0 15 1
A setUp() 0 2 1
A testTaxUpdate() 0 19 1
A testItemSetTax() 0 15 1
A testQuantity() 0 18 1
A testWeightWithOption() 0 31 1
A testHasNoOption() 0 12 1
A testItemTotalPrice() 0 13 1
A testHasOption() 0 20 1
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) 2023 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 2023 Lenius.
17
 *
18
 * @version production
19
 *
20
 * @link https://github.com/lenius/basket
21
 */
22
use Lenius\Basket\Basket;
23
use Lenius\Basket\Item;
24
use PHPUnit\Framework\TestCase;
25
26
class ItemTest extends TestCase
27
{
28
    private $item;
29
30
    public function setUp(): void
31
    {
32
    }
33
34
    public function tearDown(): void
35
    {
36
        $this->item = null;
37
    }
38
39
    public function testTaxUpdate(): void
40
    {
41
        $item = [
42
            'id'       => 'foo',
43
            'name'     => 'bar',
44
            'price'    => 100,
45
            'quantity' => 1,
46
            'weight'   => 200,
47
        ];
48
49
        $this->item = new Item($item);
50
51
        $this->item->update('tax', 0);
52
        $this->assertEquals(100, $this->item->total());
53
        $this->assertEquals(100, $this->item->total(false));
54
55
        $this->item->update('tax', 20);
56
        $this->assertEquals(120, $this->item->total());
57
        $this->assertEquals(100, $this->item->total(false));
58
    }
59
60
    public function testWeight(): void
61
    {
62
        $weight = rand(200, 300);
63
        $quantity = rand(1, 10);
64
65
        $item = [
66
            'id'       => 'foo',
67
            'name'     => 'bar',
68
            'price'    => 100,
69
            'quantity' => $quantity,
70
            'weight'   => $weight,
71
        ];
72
73
        $this->item = new Item($item);
74
        $this->assertEquals(($weight * $quantity), $this->item->weight());
75
    }
76
77
    public function testQuantity(): void
78
    {
79
        $weight = rand(200, 300);
80
        $quantity = rand(1, 10);
81
82
        $item = [
83
            'id'       => 'foo',
84
            'name'     => 'bar',
85
            'price'    => 100,
86
            'quantity' => $quantity,
87
            'weight'   => $weight,
88
        ];
89
90
        $this->item = new Item($item);
91
        $this->assertEquals($quantity, $this->item->getQuantity());
92
93
        $this->item->setQuantity(12);
94
        $this->assertEquals(12, $this->item->getQuantity());
95
    }
96
97
    public function testWeightWithOption(): void
98
    {
99
        $weight = rand(200, 300);
100
        $weight_option_one = rand(50, 100);
101
        $weight_option_two = rand(150, 200);
102
        $quantity = rand(1, 10);
103
104
        $item = [
105
            'id'       => 'foo',
106
            'name'     => 'bar',
107
            'price'    => 100,
108
            'quantity' => $quantity,
109
            'weight'   => $weight,
110
            'options'  => [
111
                [
112
                    'name'   => 'Size',
113
                    'value'  => 'L',
114
                    'weight' => $weight_option_one,
115
                    'price'  => 100,
116
                ],
117
                [
118
                    'name'   => 'Color',
119
                    'value'  => 'Black',
120
                    'weight' => $weight_option_two,
121
                    'price'  => 100,
122
                ],
123
            ],
124
        ];
125
126
        $this->item = new Item($item);
127
        $this->assertEquals(($weight + ($weight_option_one + $weight_option_two)) * $quantity, $this->item->weight());
128
    }
129
130
    public function testHasOption(): void
131
    {
132
        $item = [
133
            'id'       => 'foo',
134
            'name'     => 'bar',
135
            'price'    => 100,
136
            'quantity' => 1,
137
            'weight'   => 200,
138
            'options'  => [
139
                [
140
                    'name'   => 'Size',
141
                    'value'  => 'L',
142
                    'weight' => 50,
143
                    'price'  => 100,
144
                ],
145
            ],
146
        ];
147
148
        $this->item = new Item($item);
149
        $this->assertTrue($this->item->hasOptions());
150
    }
151
152
    public function testHasNoOption(): void
153
    {
154
        $item = [
155
            'id'       => 'foo',
156
            'name'     => 'bar',
157
            'price'    => 100,
158
            'quantity' => 1,
159
            'weight'   => 200,
160
        ];
161
162
        $this->item = new Item($item);
163
        $this->assertFalse($this->item->hasOptions());
164
    }
165
166
    public function testItemTotalPrice(): void
167
    {
168
        $item = [
169
            'id'       => 'foo',
170
            'name'     => 'bar',
171
            'price'    => 100,
172
            'quantity' => 1,
173
            'weight'   => 200,
174
        ];
175
176
        $this->item = new Item($item);
177
178
        $this->assertEquals(100, $this->item->single());
179
    }
180
181
    public function testItemSetTax(): void
182
    {
183
        $item = [
184
            'id'       => 'foo',
185
            'name'     => 'bar',
186
            'price'    => 100,
187
            'quantity' => 1,
188
            'weight'   => 200,
189
        ];
190
191
        $this->item = new Item($item);
192
193
        $this->item->tax = 25;
0 ignored issues
show
Bug Best Practice introduced by
The property $tax is declared protected in Lenius\Basket\Item. Since you implement __set, consider adding a @property or @property-write.
Loading history...
Documentation Bug introduced by
It seems like 25 of type integer is incompatible with the declared type Lenius\Basket\Tax of property $tax.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
194
195
        $this->assertEquals(25, $this->item->tax());
196
    }
197
}
198