Completed
Push — master ( f1ac10...99c02a )
by Carsten
04:49
created

ItemTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 166
Duplicated Lines 29.52 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 49
loc 166
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A tearDown() 0 4 1
A testTaxUpdate() 0 22 1
A testWeight() 0 18 1
A testWeightWithOption() 0 34 1
A testHasOption() 0 23 1
A testHasNoOption() 15 15 1
A testItemTotalPrice() 16 16 1
A testItemSetTax() 18 18 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Item;
24
use Lenius\Basket\Storage\Runtime as RuntimeStore;
25
use PHPUnit\Framework\TestCase;
26
27
class ItemTest extends TestCase
28
{
29
    private $item;
30
31
    public function setUp()
32
    {
33
    }
34
35
    public function tearDown()
36
    {
37
        $this->item = null;
38
    }
39
40
    public function testTaxUpdate()
41
    {
42
        $identifier = 1;
43
44
        $item = [
45
            'id'       => 'foo',
46
            'name'     => 'bar',
47
            'price'    => 100,
48
            'quantity' => 1,
49
            'weight'   => 200,
50
        ];
51
52
        $this->item = new Item($identifier, $item, new RuntimeStore());
53
54
        $this->item->update('tax', 0);
55
        $this->assertEquals(100, $this->item->total());
56
        $this->assertEquals(100, $this->item->total(false));
57
58
        $this->item->update('tax', 20);
59
        $this->assertEquals(120, $this->item->total());
60
        $this->assertEquals(100, $this->item->total(false));
61
    }
62
63
    public function testWeight()
64
    {
65
        $identifier = 1;
66
67
        $weight = rand(200, 300);
68
        $quantity = rand(1, 10);
69
70
        $item = [
71
            'id'       => 'foo',
72
            'name'     => 'bar',
73
            'price'    => 100,
74
            'quantity' => $quantity,
75
            'weight'   => $weight,
76
        ];
77
78
        $this->item = new Item($identifier, $item, new RuntimeStore());
79
        $this->assertEquals(($weight * $quantity), $this->item->weight());
80
    }
81
82
    public function testWeightWithOption()
83
    {
84
        $identifier = 1;
85
86
        $weight = rand(200, 300);
87
        $weight_option_one = rand(50, 100);
88
        $weight_option_two = rand(150, 200);
89
        $quantity = rand(1, 10);
90
91
        $item = [
92
            'id'       => 'foo',
93
            'name'     => 'bar',
94
            'price'    => 100,
95
            'quantity' => $quantity,
96
            'weight'   => $weight,
97
            'options'  => [
98
                [
99
                    'name'   => 'Size',
100
                    'value'  => 'L',
101
                    'weight' => $weight_option_one,
102
                    'price'  => 100,
103
                ],
104
                [
105
                    'name'   => 'Color',
106
                    'value'  => 'Black',
107
                    'weight' => $weight_option_two,
108
                    'price'  => 100,
109
                ],
110
            ],
111
        ];
112
113
        $this->item = new Item($identifier, $item, new RuntimeStore());
114
        $this->assertEquals(($weight + ($weight_option_one + $weight_option_two)) * $quantity, $this->item->weight());
115
    }
116
117
    public function testHasOption()
118
    {
119
        $identifier = 1;
120
121
        $item = [
122
            'id'       => 'foo',
123
            'name'     => 'bar',
124
            'price'    => 100,
125
            'quantity' => 1,
126
            'weight'   => 200,
127
            'options'  => [
128
                [
129
                    'name'   => 'Size',
130
                    'value'  => 'L',
131
                    'weight' => 50,
132
                    'price'  => 100,
133
                ],
134
            ],
135
        ];
136
137
        $this->item = new Item($identifier, $item, new RuntimeStore());
138
        $this->assertTrue($this->item->hasOptions());
139
    }
140
141 View Code Duplication
    public function testHasNoOption()
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...
142
    {
143
        $identifier = 1;
144
145
        $item = [
146
            'id'       => 'foo',
147
            'name'     => 'bar',
148
            'price'    => 100,
149
            'quantity' => 1,
150
            'weight'   => 200,
151
        ];
152
153
        $this->item = new Item($identifier, $item, new RuntimeStore());
154
        $this->assertFalse($this->item->hasOptions());
155
    }
156
157 View Code Duplication
    public function testItemTotalPrice()
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...
158
    {
159
        $identifier = 1;
160
161
        $item = [
162
            'id'       => 'foo',
163
            'name'     => 'bar',
164
            'price'    => 100,
165
            'quantity' => 1,
166
            'weight'   => 200,
167
        ];
168
169
        $this->item = new Item($identifier, $item, new RuntimeStore());
170
171
        $this->assertEquals(100, $this->item->single());
172
    }
173
174 View Code Duplication
    public function testItemSetTax()
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...
175
    {
176
        $identifier = 1;
177
178
        $item = [
179
            'id'       => 'foo',
180
            'name'     => 'bar',
181
            'price'    => 100,
182
            'quantity' => 1,
183
            'weight'   => 200,
184
        ];
185
186
        $this->item = new Item($identifier, $item, new RuntimeStore());
187
188
        $this->item->tax = 25;
0 ignored issues
show
Documentation introduced by
The property $tax is declared protected in Lenius\Basket\Item. Since you implemented __set(), maybe consider adding a @property or @property-write annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
189
190
        $this->assertEquals(25, $this->item->tax());
191
    }
192
}
193