Completed
Push — master ( 0f638d...b58a50 )
by Carlos
02:28
created

LeaderBoardTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 27.03 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 6
c 3
b 1
f 0
lcom 1
cbo 3
dl 20
loc 74
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A getDataWithNoItems() 0 5 1
A canAddSingleItem() 0 8 1
A canAddThreeItemsOrderDescByDefault() 10 10 1
A canAddThreeItemsOrderAsc() 10 10 1
A addItem() 0 8 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 CarlosIO\Geckoboard\Tests\Widgets;
4
5
use CarlosIO\Geckoboard\Data\LeaderBoard\Item;
6
use CarlosIO\Geckoboard\Widgets\LeaderBoard;
7
8
/**
9
 * Class LeaderBoardTest.
10
 */
11
class LeaderBoardTest extends \PHPUnit_Framework_TestCase
12
{
13
    /**
14
     * @var LeaderBoard
15
     */
16
    protected $widget;
17
18
    protected function setUp()
19
    {
20
        $this->widget = new LeaderBoard();
21
    }
22
    /**
23
     * @test
24
     */
25
    public function getDataWithNoItems()
26
    {
27
        $json = json_encode($this->widget->getData());
28
        $this->assertEquals('{"items":[]}', $json);
29
    }
30
31
    /**
32
     * @test
33
     */
34
    public function canAddSingleItem()
35
    {
36
        $this->widget->setId(123);
37
        $this->addItem('Title text', 10, 2);
38
39
        $json = json_encode($this->widget->getData());
40
        $this->assertEquals('{"items":[{"label":"Title text","value":10,"previous_rank":2}]}', $json);
41
    }
42
43
    /**
44
     * @test
45
     */
46 View Code Duplication
    public function canAddThreeItemsOrderDescByDefault()
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...
47
    {
48
        $this->addItem('Title text', 10);
49
        $this->addItem('Title text2', 15);
50
        $this->addItem('Title text3', 7);
51
52
        $json = json_encode($this->widget->getData());
53
        $this->assertEquals('{"items":[{"label":"Title text2","value":15},{"label":"Title text","value":10},'.
54
            '{"label":"Title text3","value":7}]}', $json);
55
    }
56
57
    /**
58
     * @test
59
     */
60 View Code Duplication
    public function canAddThreeItemsOrderAsc()
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->addItem('Title text', 10);
63
        $this->addItem('Title text2', 15);
64
        $this->addItem('Title text3', 7);
65
66
        $json = json_encode($this->widget->getData(LeaderBoard::SORT_ASC));
67
        $this->assertEquals('{"items":[{"label":"Title text3","value":7},{"label":"Title text","value":10},'.
68
            '{"label":"Title text2","value":15}]}', $json);
69
    }
70
71
    /**
72
     * @param $label
73
     * @param $value
74
     * @param null $previousRanking
75
     */
76
    private function addItem($label, $value, $previousRanking = null)
77
    {
78
        $item = new Item();
79
        $item->setLabel($label)
80
            ->setValue($value)
81
            ->setPreviousRank($previousRanking);
82
        $this->widget->addItem($item);
83
    }
84
}
85