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() |
|
|
|
|
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() |
|
|
|
|
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
|
|
|
|
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.