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

CartPresenterTest::testGetPayment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
}