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

CartItemTest::testIncreaseQuantity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.7666
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\Product;
6
use Plane\Shop\CartItem;
7
use Plane\Shop\Exception\QuanityException;
8
use Plane\Shop\Validator\StockQuantityValidator;
9
10
class CartItemTest extends \PHPUnit\Framework\TestCase
11
{
12
    use MoneyTrait;
13
14
    const CURRENCY = 'USD';
15
    
16
    protected $product;
17
    
18
    protected function setUp(): void
19
    {
20
        $this->product = new Product([
21
            'id'            => 'someID',
22
            'name'          => 'Test product',
23
            'price'         => 10.5665,
24
            'stock'         => 4,
25
            'taxRate'       => 0.22,
26
            'weight'        => 5.1,
27
            'imagePath'     => '/path_to_file/file.jpg',
28
        ]);
29
    }
30
31
    public function testCartItem()
32
    {
33
        $quantity = 3;
34
        $cartItem = new CartItem($this->product, $quantity, new StockQuantityValidator);
35
36
        $this->assertSame($cartItem->getProduct(), $this->product);
37
        $this->assertSame($cartItem->getId(), $this->product->getId());
38
        $this->assertSame($cartItem->getName(), $this->product->getName());
39
        $this->assertSame($cartItem->getImagePath(), $this->product->getImagePath());
40
        $this->assertSame($cartItem->getQuantity(), $quantity);
41
        $this->assertSame($cartItem->getWeight(), 5.1);
42
        $this->assertSame($cartItem->getWeightTotal(), 15.3);
43
44
        // money calculations
45
        $this->assertSame($this->getAmount($cartItem->getTax(self::CURRENCY)), '2.33');
46
        $this->assertSame($this->getAmount($cartItem->getTaxTotal(self::CURRENCY)), '6.99');
47
        $this->assertSame($this->getAmount($cartItem->getPrice(self::CURRENCY)), '10.57');
48
        $this->assertSame($this->getAmount($cartItem->getPriceTotal(self::CURRENCY)), '31.71');
49
        $this->assertSame($this->getAmount($cartItem->getPriceWithTax(self::CURRENCY)), '12.90');
50
        $this->assertSame($this->getAmount($cartItem->getPriceTotalWithTax(self::CURRENCY)), '38.70');
51
    }
52
53
    public function testCreateObjectWithStockQuantityValidator()
54
    {
55
        $this->expectException(QuanityException::class);
56
57
        new CartItem($this->product, 5, new StockQuantityValidator);
58
    }
59
60
    public function testToArray()
61
    {
62
        $quantity = 3;
63
        $cartItem = new CartItem($this->product, $quantity);
64
65
        $array = $cartItem->toArray(self::CURRENCY);
66
67
        $this->assertSame($array['quantity'], $quantity);
68
        $this->assertSame($this->getAmount($array['totalTax']), '6.99');
69
        $this->assertSame($this->getAmount($array['priceTotal']), '31.71');
70
        $this->assertSame($this->getAmount($array['priceTotalWithTax']), '38.70');
71
        $this->assertTrue(is_array($array['product']));
72
    }
73
74 View Code Duplication
    public function testSetQuantity()
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...
75
    {
76
        $quantity = 3;
77
        $cartItem = new CartItem($this->product, $quantity);
78
        $cartItem->setQuantity(8);
79
80
        $this->assertSame($cartItem->getQuantity(), 8);
81
82
        // now check with stock validator
83
84
        $this->expectException(QuanityException::class);
85
86
        $cartItem = new CartItem($this->product, $quantity, new StockQuantityValidator);
87
        $cartItem->setQuantity(8);
88
    }
89
90 View Code Duplication
    public function testIncreaseQuantity()
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...
91
    {
92
        $quantity = 3;
93
        $cartItem = new CartItem($this->product, $quantity);
94
        $cartItem->increaseQuantity(5);
95
96
        $this->assertSame($cartItem->getQuantity(), 8);
97
98
        // now check with stock validator
99
100
        $this->expectException(QuanityException::class);
101
102
        $cartItem = new CartItem($this->product, $quantity, new StockQuantityValidator);
103
        $cartItem->increaseQuantity(3);
104
    }
105
106
    public function testDecreaseQuantity()
107
    {
108
        $quantity = 3;
109
        $cartItem = new CartItem($this->product, $quantity);
110
        $cartItem->decreaseQuantity(2);
111
112
        $this->assertSame($cartItem->getQuantity(), 1);
113
114
        $this->expectException(QuanityException::class);
115
116
        $cartItem->decreaseQuantity(1);
117
    }
118
119
    public function testDecreaseQuantityWithStockQuantityValidator()
120
    {
121
        $this->expectException(QuanityException::class);
122
123
        $cartItem = new CartItem($this->product, 3, new StockQuantityValidator);
124
        $cartItem->decreaseQuantity(3);
125
    }
126
}
127