Passed
Branch master (168fd2)
by Dariusz
06:34
created

CartTest   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 188
Duplicated Lines 14.89 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 23
lcom 2
cbo 8
dl 28
loc 188
rs 10
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testGetCurrency() 0 4 1
A testGetShipping() 0 4 1
A testGetPayment() 0 4 1
A testFill() 0 16 1
A testAdd() 11 11 1
A testGet() 0 4 1
A testFailedGet() 0 6 1
A testUpdate() 0 15 1
A testRemove() 10 10 1
A testFailedRemove() 0 6 1
A testClear() 7 7 1
A testItemsQuantity() 0 4 1
A testWeight() 0 4 1
A testTotalNet() 0 4 1
A testTotalGross() 0 4 1
A testTax() 0 4 1
A testShippingCost() 0 4 1
A testShippingCostWhenNoShippingSet() 0 6 1
A testPaymentFee() 0 4 1
A testPaymentFeeWhenNoPaymentSet() 0 6 1
A testDiscounts() 0 10 1
A testTotalAfterDiscounts() 0 15 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 Plane\Shop\Tests;
4
5
use Plane\Shop\Cart;
6
use Plane\Shop\CartItem;
7
use OutOfBoundsException;
8
use Plane\Shop\PaymentInterface;
9
use Plane\Shop\ShippingInterface;
10
use Plane\Shop\CartItemCollection;
11
use Plane\Shop\Discount\EverySecondItemFreeDiscount;
12
use Plane\Shop\Discount\TotalPriceThresholdDiscount;
13
14
class CartTest extends \PHPUnit\Framework\TestCase
15
{
16
    use MoneyTrait;
17
    use CartTrait;
18
    
19
    const CURRENCY = 'USD';
20
21
    protected function setUp(): void
22
    {
23
        $this->createFirstCartItem();
24
        $this->createSecondCartItem();
25
        $this->createCart();       
26
    }
27
28
    public function testGetCurrency()
29
    {
30
        $this->assertSame(self::CURRENCY, $this->cart->getCurrency());
31
    }
32
33
    public function testGetShipping()
34
    {
35
        $this->assertInstanceOf(ShippingInterface::class, $this->cart->getShipping());
36
    }
37
38
    public function testGetPayment()
39
    {
40
        $this->assertInstanceOf(PaymentInterface::class, $this->cart->getPayment());
41
    }
42
43
    public function testFill()
44
    {
45
        $cartItemCollection = new CartItemCollection;
46
        $cartItemCollection->addItem($this->firstCartItem);
47
        $cartItemCollection->addItem($this->secondCartItem);
48
        
49
        $cart = new Cart(self::CURRENCY);
50
        $cart->fill($cartItemCollection);
51
52
        $this->assertSame([
53
            1 => $this->firstCartItem,
54
            2 => $this->secondCartItem
55
        ], $cart->items());
56
57
        $this->assertTrue(count($cart->items()) == 2);
58
    }
59
60 View Code Duplication
    public function testAdd()
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...
61
    {
62
        $this->cart->add($this->secondCartItem);
63
64
        $this->assertSame([
65
            1 => $this->firstCartItem,
66
            2 => $this->secondCartItem
67
        ], $this->cart->items());
68
        
69
        $this->assertTrue(count($this->cart->items()) == 2);
70
    }
71
72
    public function testGet()
73
    {
74
        $this->assertSame($this->secondCartItem, $this->cart->get($this->secondCartItem->getId()));
75
    }
76
77
    public function testFailedGet()
78
    {
79
        $this->expectException(OutOfBoundsException::class);
80
81
        $this->cart->get('4');
82
    }
83
84
    public function testUpdate()
85
    {
86
        $firstAltered  = $this->createMock(CartItem::class);
87
        $firstAltered->method('getId')->willReturn(1);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
        $firstAltered->method('getQuantity')->willReturn(5);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
        
90
        $cart = new Cart(self::CURRENCY);
91
        $cart->add($this->firstCartItem);
92
93
        $this->assertTrue($cart->itemsQuantity() == 1);
94
        
95
        $cart->update($firstAltered);
0 ignored issues
show
Documentation introduced by
$firstAltered is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Plane\Shop\CartItemInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96
97
        $this->assertTrue($cart->itemsQuantity() == 5);
98
    }
99
100 View Code Duplication
    public function testRemove()
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...
101
    {
102
        $this->cart->remove($this->firstCartItem->getId());
103
104
        $this->assertSame([
105
            2 => $this->secondCartItem
106
        ], $this->cart->items());
107
108
        $this->assertTrue(count($this->cart->items()) == 1);
109
    }
110
111
    public function testFailedRemove()
112
    {
113
        $this->expectException(OutOfBoundsException::class);
114
115
        $this->cart->remove('3');
116
    }
117
118 View Code Duplication
    public function testClear()
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...
119
    {
120
        $this->cart->clear();
121
122
        $this->assertSame([], $this->cart->items());
123
        $this->assertTrue(count($this->cart->items()) == 0);
124
    }
125
126
    public function testItemsQuantity()
127
    {
128
        $this->assertTrue($this->cart->itemsQuantity() == 3);
129
    }
130
131
    public function testWeight()
132
    {
133
        $this->assertTrue($this->cart->weight() == 9.99);
134
    }
135
136
    public function testTotalNet()
137
    {
138
        $this->assertTrue($this->getAmount($this->cart->totalNet()) == 11.50);
139
    }
140
141
    public function testTotalGross()
142
    {
143
        $this->assertTrue($this->getAmount($this->cart->totalGross()) == 14.03);
144
    }
145
146
    public function testTax()
147
    {
148
        $this->assertTrue($this->getAmount($this->cart->tax()) == 2.53);
149
    }
150
151
    public function testShippingCost()
152
    {
153
        $this->assertTrue($this->getAmount($this->cart->shippingCost()) == 4.00);
154
    }
155
156
    public function testShippingCostWhenNoShippingSet()
157
    {
158
        $cart = new Cart(self::CURRENCY);
159
160
        $this->assertTrue($this->getAmount($cart->shippingCost()) == 0.00);
161
    }
162
163
    public function testPaymentFee()
164
    {
165
        $this->assertTrue($this->getAmount($this->cart->paymentFee()) == 5.50);
166
    }
167
168
    public function testPaymentFeeWhenNoPaymentSet()
169
    {
170
        $cart = new Cart(self::CURRENCY);
171
172
        $this->assertTrue($this->getAmount($cart->paymentFee()) == 0.00);
173
    }
174
175
    public function testDiscounts()
176
    {
177
        $cartDiscount = new TotalPriceThresholdDiscount('Discount description', $this->cart, [
178
            'treshold' => 14,
179
            'discount' => 0.1
180
        ]);
181
        $this->cart->addDiscount($cartDiscount);
182
183
        $this->assertTrue($this->cart->getDiscounts() == [0 => $cartDiscount]);
184
    }
185
186
    public function testTotalAfterDiscounts()
187
    {
188
        $secondFreeDisount = new EverySecondItemFreeDiscount('Every second product is free', $this->cart, [
189
            'items' => $this->cart->items()
190
        ]);
191
        $this->cart->addDiscount($secondFreeDisount);
192
        
193
        $priceTresholdDiscount = new TotalPriceThresholdDiscount('Discount description', $this->cart, [
194
            'treshold' => 4,
195
            'discount' => 0.5
196
        ]);
197
        $this->cart->addDiscount($priceTresholdDiscount);
198
199
        $this->assertTrue($this->getAmount($this->cart->totalAfterDiscounts()) == 2.13);
200
    }
201
}
202