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
|
|
|
$this->assertEquals(2, $field->getdecimalPlaces()); |
19
|
|
|
$this->assertEquals('', $field->getUnit()); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testRange() |
23
|
|
|
{ |
24
|
|
|
$rangeField = RangeField::create( |
25
|
|
|
'TestInt', |
26
|
|
|
'Test', |
27
|
|
|
50, |
28
|
|
|
25, |
29
|
|
|
75, |
30
|
|
|
['min' => 25, '17%' => 33, '50%' => 50, '83%' => 66, 'max' => 75] |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
$rangeField->Field([]); |
34
|
|
|
|
35
|
|
|
$expected = [ |
36
|
|
|
'start' => [50], |
37
|
|
|
'snap' => false, |
38
|
|
|
'animate' => true, |
39
|
|
|
'animationDuration' => 300, |
40
|
|
|
'range' => [ |
41
|
|
|
'min' => 25, |
42
|
|
|
'17%' => 33, |
43
|
|
|
'50%' => 50, |
44
|
|
|
'83%' => 66, |
45
|
|
|
'max' => 75 |
46
|
|
|
], |
47
|
|
|
'unit' => '', |
48
|
|
|
'decimalPlaces' => 2, |
49
|
|
|
'pips' => [ // Show a scale with the slider |
50
|
|
|
'mode' => 'steps', |
51
|
|
|
'stepped' => true, |
52
|
|
|
'density' => 4 |
53
|
|
|
] |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
$this->assertEquals($expected, $rangeField->getData()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testSnap() |
60
|
|
|
{ |
61
|
|
|
$rangeField = RangeField::create( |
62
|
|
|
'TestInt', |
63
|
|
|
'Test', |
64
|
|
|
50, |
65
|
|
|
25, |
66
|
|
|
75, |
67
|
|
|
['min' => 25, '17%' => 33, '50%' => 50, '83%' => 66, 'max' => 75] |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$rangeField->setSnap(true); |
71
|
|
|
|
72
|
|
|
$rangeField->Field([]); |
73
|
|
|
|
74
|
|
|
$expected = [ |
75
|
|
|
'start' => [50], |
76
|
|
|
'snap' => true, |
77
|
|
|
'animate' => true, |
78
|
|
|
'animationDuration' => 300, |
79
|
|
|
'range' => [ |
80
|
|
|
'min' => 25, |
81
|
|
|
'17%' => 33, |
82
|
|
|
'50%' => 50, |
83
|
|
|
'83%' => 66, |
84
|
|
|
'max' => 75 |
85
|
|
|
], |
86
|
|
|
'pips' => [ // Show a scale with the slider |
87
|
|
|
'mode' => 'steps', |
88
|
|
|
'stepped' => true, |
89
|
|
|
'density' => 4 |
90
|
|
|
], |
91
|
|
|
'unit' => '', |
92
|
|
|
'decimalPlaces' => 2, |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
$this->assertEquals($expected, $rangeField->getData()); |
96
|
|
|
$rangeField->setStep(1); |
97
|
|
|
|
98
|
|
|
$rangeField->Field([]); |
99
|
|
|
|
100
|
|
|
$expected = [ |
101
|
|
|
'start' => [50], |
102
|
|
|
'snap' => true, |
103
|
|
|
'animate' => true, |
104
|
|
|
'animationDuration' => 300, |
105
|
|
|
'step' => 1, |
106
|
|
|
'range' => [ |
107
|
|
|
'min' => 25, |
108
|
|
|
'17%' => 33, |
109
|
|
|
'50%' => 50, |
110
|
|
|
'83%' => 66, |
111
|
|
|
'max' => 75 |
112
|
|
|
], |
113
|
|
|
'pips' => [ // Show a scale with the slider |
114
|
|
|
'mode' => 'steps', |
115
|
|
|
'stepped' => true, |
116
|
|
|
'density' => 4 |
117
|
|
|
], |
118
|
|
|
'unit' => '', |
119
|
|
|
'decimalPlaces' => 2, |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
$this->assertEquals($expected, $rangeField->getData()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testGetSetMin() |
126
|
|
|
{ |
127
|
|
|
$field = RangeField::create('Test', 'Test'); |
128
|
|
|
|
129
|
|
|
$field->setMin(10); |
130
|
|
|
|
131
|
|
|
$this->assertEquals(10, $field->getMin()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testGetSetStep() |
135
|
|
|
{ |
136
|
|
|
$field = RangeField::create('Test', 'Test'); |
137
|
|
|
|
138
|
|
|
$this->assertNull($field->getStep()); |
139
|
|
|
|
140
|
|
|
$field->setStep(10); |
141
|
|
|
|
142
|
|
|
$this->assertEquals(10, $field->getStep()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testGetSetMax() |
146
|
|
|
{ |
147
|
|
|
$field = RangeField::create('Test', 'Test'); |
148
|
|
|
|
149
|
|
|
$field->setMax(10); |
150
|
|
|
|
151
|
|
|
$this->assertEquals(10, $field->getMax()); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testGetSetRange() |
155
|
|
|
{ |
156
|
|
|
$field = RangeField::create('Test', 'Test'); |
157
|
|
|
|
158
|
|
|
$this->assertEquals([], $field->getRange()); |
159
|
|
|
|
160
|
|
|
$field->setRange([ |
161
|
|
|
'min' => 25, |
162
|
|
|
'17%' => 33, |
163
|
|
|
'50%' => 50, |
164
|
|
|
'83%' => 66, |
165
|
|
|
'max' => 75 |
166
|
|
|
]); |
167
|
|
|
|
168
|
|
|
$this->assertEquals([ |
169
|
|
|
'min' => 25, |
170
|
|
|
'17%' => 33, |
171
|
|
|
'50%' => 50, |
172
|
|
|
'83%' => 66, |
173
|
|
|
'max' => 75 |
174
|
|
|
], $field->getRange()); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function testGetSetDensity() |
178
|
|
|
{ |
179
|
|
|
$field = RangeField::create('Test', 'Test'); |
180
|
|
|
|
181
|
|
|
$this->assertEquals(4, $field->getDensity()); |
182
|
|
|
|
183
|
|
|
$field->setDensity(10); |
184
|
|
|
|
185
|
|
|
$this->assertEquals(10, $field->getDensity()); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function testIsSetPips() |
189
|
|
|
{ |
190
|
|
|
$field = RangeField::create('Test', 'Test'); |
191
|
|
|
|
192
|
|
|
$this->assertTrue($field->isShowPips()); |
193
|
|
|
|
194
|
|
|
$field->setShowPips(false); |
195
|
|
|
|
196
|
|
|
$this->assertFalse($field->isShowPips()); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function testIsSetSnap() |
200
|
|
|
{ |
201
|
|
|
$field = RangeField::create('Test', 'Test'); |
202
|
|
|
|
203
|
|
|
$this->assertFalse($field->isSnap()); |
204
|
|
|
|
205
|
|
|
$field->setSnap(true); |
206
|
|
|
|
207
|
|
|
$this->assertTrue($field->isSnap()); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function testGetSetStart() |
211
|
|
|
{ |
212
|
|
|
$field = RangeField::create('Test', 'Test', [5, 25]); |
213
|
|
|
|
214
|
|
|
$this->assertEquals([5, 25], $field->getStart()); |
215
|
|
|
|
216
|
|
|
$field = RangeField::create('Test', 'Test'); |
217
|
|
|
|
218
|
|
|
$this->assertEquals([0], $field->getStart()); |
219
|
|
|
|
220
|
|
|
$field->setStart(10); |
221
|
|
|
|
222
|
|
|
$this->assertEquals([10], $field->getStart()); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function testGetSetUnit() |
226
|
|
|
{ |
227
|
|
|
$field = RangeField::create('Test', 'Test'); |
228
|
|
|
|
229
|
|
|
$this->assertEquals('', $field->getUnit()); |
230
|
|
|
|
231
|
|
|
$field->setUnit('%'); |
232
|
|
|
|
233
|
|
|
$this->assertEquals('%', $field->getUnit()); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public function testGetSetdecimalPlaces() |
237
|
|
|
{ |
238
|
|
|
$field = RangeField::create('Test', 'Test'); |
239
|
|
|
|
240
|
|
|
$this->assertEquals(2, $field->getDecimalPlaces()); |
241
|
|
|
|
242
|
|
|
$field->setDecimalPlaces(1); |
243
|
|
|
|
244
|
|
|
$this->assertEquals(1, $field->getDecimalPlaces()); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function testSetFormat() |
248
|
|
|
{ |
249
|
|
|
$field = RangeField::create('Test', 'Test'); |
250
|
|
|
|
251
|
|
|
$this->assertEquals(2, $field->getDecimalPlaces()); |
252
|
|
|
$this->assertEquals('', $field->getUnit()); |
253
|
|
|
|
254
|
|
|
|
255
|
|
|
$field->setFormat('cm', 0); |
256
|
|
|
|
257
|
|
|
$this->assertEquals(0, $field->getDecimalPlaces()); |
258
|
|
|
$this->assertEquals('cm', $field->getUnit()); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|