1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\RangeField\Tests; |
4
|
|
|
|
5
|
|
|
use Firesphere\RangeField\RangeField; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
|
8
|
|
|
class RangeFieldTest extends SapphireTest |
9
|
|
|
{ |
10
|
|
|
public function testDefault() |
11
|
|
|
{ |
12
|
|
|
$field = RangeField::create('Test', 'Test'); |
13
|
|
|
$this->assertEquals([0], $field->getStart()); |
14
|
|
|
$this->assertEquals(0, $field->getMin()); |
15
|
|
|
$this->assertEquals(100, $field->getMax()); |
16
|
|
|
$this->assertFalse($field->isSnap()); |
17
|
|
|
$this->assertEquals([], $field->getRange()); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
View Code Duplication |
public function testRange() |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$rangeField = RangeField::create( |
23
|
|
|
'TestInt', |
24
|
|
|
'Test', |
25
|
|
|
50, |
26
|
|
|
25, |
27
|
|
|
75, |
28
|
|
|
['min' => 25, '17%' => 33, '50%' => 50, '83%' => 66, 'max' => 75] |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
$rangeField->Field([]); |
32
|
|
|
|
33
|
|
|
$expected = [ |
34
|
|
|
'start' => [50], |
35
|
|
|
'snap' => false, |
36
|
|
|
'range' => [ |
37
|
|
|
'min' => 25, |
38
|
|
|
'17%' => 33, |
39
|
|
|
'50%' => 50, |
40
|
|
|
'83%' => 66, |
41
|
|
|
'max' => 75 |
42
|
|
|
], |
43
|
|
|
'pips' => [ // Show a scale with the slider |
44
|
|
|
'mode' => 'steps', |
45
|
|
|
'stepped' => true, |
46
|
|
|
'density' => 4 |
47
|
|
|
] |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
$this->assertEquals($expected, $rangeField->getData()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
View Code Duplication |
public function testSnap() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$rangeField = RangeField::create( |
56
|
|
|
'TestInt', |
57
|
|
|
'Test', |
58
|
|
|
50, |
59
|
|
|
25, |
60
|
|
|
75, |
61
|
|
|
['min' => 25, '17%' => 33, '50%' => 50, '83%' => 66, 'max' => 75] |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$rangeField->setSnap(true); |
65
|
|
|
|
66
|
|
|
$rangeField->Field([]); |
67
|
|
|
|
68
|
|
|
$expected = [ |
69
|
|
|
'start' => [50], |
70
|
|
|
'snap' => true, |
71
|
|
|
'range' => [ |
72
|
|
|
'min' => 25, |
73
|
|
|
'17%' => 33, |
74
|
|
|
'50%' => 50, |
75
|
|
|
'83%' => 66, |
76
|
|
|
'max' => 75 |
77
|
|
|
], |
78
|
|
|
'pips' => [ // Show a scale with the slider |
79
|
|
|
'mode' => 'steps', |
80
|
|
|
'stepped' => true, |
81
|
|
|
'density' => 4 |
82
|
|
|
] |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
$this->assertEquals($expected, $rangeField->getData()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testGetSetMin() |
89
|
|
|
{ |
90
|
|
|
$field = RangeField::create('Test', 'Test'); |
91
|
|
|
|
92
|
|
|
$field->setMin(10); |
93
|
|
|
|
94
|
|
|
$this->assertEquals([10], $field->getMin()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testGetSetMax() |
98
|
|
|
{ |
99
|
|
|
$field = RangeField::create('Test', 'Test'); |
100
|
|
|
|
101
|
|
|
$field->setMax(10); |
102
|
|
|
|
103
|
|
|
$this->assertEquals(10, $field->getMax()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testGetSetRange() |
107
|
|
|
{ |
108
|
|
|
$field = RangeField::create('Test', 'Test'); |
109
|
|
|
|
110
|
|
|
$this->assertEquals([], $field->getRange()); |
111
|
|
|
|
112
|
|
|
$field->setRange([ |
113
|
|
|
'min' => 25, |
114
|
|
|
'17%' => 33, |
115
|
|
|
'50%' => 50, |
116
|
|
|
'83%' => 66, |
117
|
|
|
'max' => 75 |
118
|
|
|
]); |
119
|
|
|
|
120
|
|
|
$this->assertEquals([ |
121
|
|
|
'min' => 25, |
122
|
|
|
'17%' => 33, |
123
|
|
|
'50%' => 50, |
124
|
|
|
'83%' => 66, |
125
|
|
|
'max' => 75 |
126
|
|
|
], $field->getRange()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testGetSetDensity() |
130
|
|
|
{ |
131
|
|
|
$field = RangeField::create('Test', 'Test'); |
132
|
|
|
|
133
|
|
|
$this->assertEquals(4, $field->getDensity()); |
134
|
|
|
|
135
|
|
|
$field->setDensity(10); |
136
|
|
|
|
137
|
|
|
$this->assertEquals(10, $field->getDensity()); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testIsSetPips() |
141
|
|
|
{ |
142
|
|
|
$field = RangeField::create('Test', 'Test'); |
143
|
|
|
|
144
|
|
|
$this->assertTrue($field->isShowPips()); |
145
|
|
|
|
146
|
|
|
$field->setShowPips(false); |
147
|
|
|
|
148
|
|
|
$this->assertFalse($field->isShowPips()); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function testIsSetSnap() |
152
|
|
|
{ |
153
|
|
|
$field = RangeField::create('Test', 'Test'); |
154
|
|
|
|
155
|
|
|
$this->assertTrue($field->isSnap()); |
156
|
|
|
|
157
|
|
|
$field->setSnap(false); |
158
|
|
|
|
159
|
|
|
$this->assertFalse($field->isSnap()); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testGetSetStart() |
163
|
|
|
{ |
164
|
|
|
$field = RangeField::create('Test', 'Test', [5, 25]); |
165
|
|
|
|
166
|
|
|
$this->assertEquals([5, 25], $field->getStart()); |
167
|
|
|
|
168
|
|
|
$field = RangeField::create('Test', 'Test'); |
169
|
|
|
|
170
|
|
|
$this->assertEquals([0], $field->getStart()); |
171
|
|
|
|
172
|
|
|
$field->setStart(10); |
173
|
|
|
|
174
|
|
|
$this->assertEquals([10], $field->getStart()); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
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.