CartTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 22
loc 66
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testEmptyCart() 0 4 1
A testAddItem() 0 8 1
A testRemoveItem() 11 11 1
A testClearCart() 11 11 1
A testGetAttributeTotalValue() 0 8 1
A testGetItems() 0 11 1
A testGetStorage() 0 4 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 yii2mod\cart\tests;
4
5
use Yii;
6
use yii2mod\cart\storage\StorageInterface;
7
use yii2mod\cart\tests\data\Product;
8
9
/**
10
 * Class CartTest
11
 *
12
 * @package yii2mod\cart\tests
13
 */
14
class CartTest extends TestCase
15
{
16
    public function testEmptyCart()
17
    {
18
        $this->assertEmpty(Yii::$app->cart->getItems());
19
    }
20
21
    public function testAddItem()
22
    {
23
        $cart = Yii::$app->cart;
24
        $product = Product::findOne(1);
25
        $cart->add($product);
26
27
        $this->assertContains($product, $cart->getItems());
28
    }
29
30 View Code Duplication
    public function testRemoveItem()
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...
31
    {
32
        $cart = Yii::$app->cart;
33
        $product = Product::findOne(1);
34
        $cart->add($product);
35
36
        $this->assertContains($product, $cart->getItems());
37
38
        $cart->remove($product->getUniqueId());
39
        $this->assertEmpty($cart->getItems());
40
    }
41
42 View Code Duplication
    public function testClearCart()
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...
43
    {
44
        $cart = Yii::$app->cart;
45
        $product = Product::findOne(1);
46
        $cart->add($product);
47
48
        $this->assertEquals(1, $cart->getCount());
49
50
        $cart->clear();
51
        $this->assertEmpty($cart->getItems());
52
    }
53
54
    public function testGetAttributeTotalValue()
55
    {
56
        $cart = Yii::$app->cart;
57
        $product = Product::findOne(1);
58
        $cart->add($product);
59
60
        $this->assertEquals(100, $cart->getAttributeTotal('price'));
61
    }
62
63
    public function testGetItems()
64
    {
65
        $cart = Yii::$app->cart;
66
        $product = Product::findOne(1);
67
        $cart->add($product);
68
69
        $items = $cart->getItems();
70
71
        $this->assertCount(1, $items);
72
        $this->assertEquals('Amazon Kindle', $items[1]['name']);
73
    }
74
75
    public function testGetStorage()
76
    {
77
        $this->assertInstanceOf(StorageInterface::class, Yii::$app->cart->getStorage());
78
    }
79
}
80