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

CartPresenterTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 121
Duplicated Lines 14.05 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 6
dl 17
loc 121
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A testGetCurrency() 0 4 1
A testGetShipping() 0 4 1
A testGetPayment() 0 4 1
A testGet() 0 4 1
A testFailedGet() 0 6 1
A testHas() 0 4 1
A testFailedHas() 0 4 1
A testUpdate() 0 12 1
A testRemove() 10 10 1
A testFailedRemove() 0 6 1
A testClear() 7 7 1
A testToArray() 0 12 1
A testDecoratorMethods() 0 11 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\CartItem;
6
use OutOfBoundsException;
7
use Plane\Shop\CartPresenter;
8
use Plane\Shop\PaymentInterface;
9
use Plane\Shop\ShippingInterface;
10
use Plane\Shop\Discount\TotalPriceThresholdDiscount;
11
12
class CartPresenterTest extends \PHPUnit\Framework\TestCase
13
{
14
    use MoneyTrait;
15
    use CartTrait;
16
17
    const CURRENCY = 'USD';
18
19
    protected $cartPresenter;
20
21
    protected function setUp(): void
22
    {
23
        $this->createFirstCartItem();
24
        $this->createSecondCartItem();
25
        $this->createCart();
26
27
        $this->cartPresenter = new CartPresenter($this->cart);
28
        $this->cartPresenter->setShipping($this->getShippingMock());
29
        $this->cartPresenter->setPayment($this->getPaymentMock());
30
    }
31
32
    public function testGetCurrency()
33
    {
34
        $this->assertSame(self::CURRENCY, $this->cartPresenter->getCurrency());
35
    }
36
37
    public function testGetShipping()
38
    {
39
        $this->assertInstanceOf(ShippingInterface::class, $this->cartPresenter->getShipping());
40
    }
41
42
    public function testGetPayment()
43
    {
44
        $this->assertInstanceOf(PaymentInterface::class, $this->cartPresenter->getPayment());
45
    }
46
47
    public function testGet()
48
    {
49
        $this->assertSame($this->secondCartItem, $this->cartPresenter->get($this->secondCartItem->getId()));
50
    }
51
52
    public function testFailedGet()
53
    {
54
        $this->expectException(OutOfBoundsException::class);
55
56
        $this->cartPresenter->get('4');
57
    }
58
59
    public function testHas()
60
    {
61
        $this->assertTrue($this->cartPresenter->has('1'));
62
    }
63
64
    public function testFailedHas()
65
    {
66
        $this->assertFalse($this->cartPresenter->has('4'));
67
    }
68
69
    public function testUpdate()
70
    {
71
        $firstAltered  = $this->createMock(CartItem::class);
72
        $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...
73
        $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...
74
        
75
        $this->assertTrue($this->cartPresenter->itemsQuantity() == 3);
76
        
77
        $this->cartPresenter->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...
78
79
        $this->assertTrue($this->cartPresenter->itemsQuantity() == 7);
80
    }
81
82 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...
83
    {
84
        $this->cartPresenter->remove($this->firstCartItem->getId());
85
        
86
        $this->assertSame([
87
            2 => $this->secondCartItem
88
        ], $this->cartPresenter->items());
89
90
        $this->assertTrue(count($this->cartPresenter->items()) == 1);
91
    }
92
93
    public function testFailedRemove()
94
    {
95
        $this->expectException(OutOfBoundsException::class);
96
97
        $this->cartPresenter->remove('3');
98
    }
99
100 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...
101
    {
102
        $this->cartPresenter->clear();
103
104
        $this->assertSame([], $this->cartPresenter->items());
105
        $this->assertTrue(count($this->cartPresenter->items()) == 0);
106
    }
107
    
108
    public function testToArray()
109
    {
110
        $discount = $this->createMock(TotalPriceThresholdDiscount::class);
111
        $discount->method('getDescription')->willReturn('Test discount');
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...
112
        $discount->method('getPriceAfterDiscount')->willReturn($this->getMoney('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...
113
114
        $this->cart->addDiscount($discount);
0 ignored issues
show
Documentation introduced by
$discount is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Plane\Shop\CartDiscountInterface>.

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...
115
116
        $cartPresenter = new CartPresenter($this->cart);
117
118
        $this->assertTrue(is_array($cartPresenter->toArray()));
119
    }
120
121
    public function testDecoratorMethods()
122
    {
123
        $cartPresenter = new CartPresenter($this->cart);
124
125
        $this->assertSame($cartPresenter->totalNet(), $this->getAmount($this->cart->totalNet()));
126
        $this->assertSame($cartPresenter->totalGross(), $this->getAmount($this->cart->totalGross()));
127
        $this->assertSame($cartPresenter->tax(), $this->getAmount($this->cart->tax()));
128
        $this->assertSame($cartPresenter->totalAfterDiscounts(), $this->getAmount($this->cart->totalAfterDiscounts()));
129
        $this->assertSame($cartPresenter->shippingCost(), $this->getAmount($this->cart->shippingCost()));
130
        $this->assertSame($cartPresenter->paymentFee(), $this->getAmount($this->cart->paymentFee()));
131
    }
132
}