Completed
Pull Request — master (#2)
by Jess
01:49
created

RangeFieldTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 249
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 9
Bugs 2 Features 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 249
rs 10
c 9
b 2
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A testDefault() 0 11 1
B testRange() 0 36 1
A testSnap() 0 61 1
A testGetSetMin() 0 8 1
A testGetSetStep() 0 10 1
A testGetSetMax() 0 8 1
A testGetSetRange() 0 22 1
A testGetSetDensity() 0 10 1
A testIsSetPips() 0 10 1
A testIsSetSnap() 0 10 1
A testGetSetStart() 0 14 1
A testGetSetUnit() 0 10 1
A testGetSetdecimalPlaces() 0 10 1
A testSetFormat() 0 13 1
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
        ];
92
93
        $this->assertEquals($expected, $rangeField->getData());
94
        $rangeField->setStep(1);
95
96
        $rangeField->Field([]);
97
98
        $expected = [
99
            'start' => [50],
100
            'snap' => true,
101
            'animate' => true,
102
            'animationDuration' => 300,
103
            'step' => 1,
104
            'range' => [
105
                'min' => 25,
106
                '17%' => 33,
107
                '50%' => 50,
108
                '83%' => 66,
109
                'max' => 75
110
            ],
111
            'pips' => [  // Show a scale with the slider
112
                'mode' => 'steps',
113
                'stepped' => true,
114
                'density' => 4
115
            ]
116
        ];
117
118
        $this->assertEquals($expected, $rangeField->getData());
119
    }
120
121
    public function testGetSetMin()
122
    {
123
        $field = RangeField::create('Test', 'Test');
124
125
        $field->setMin(10);
126
127
        $this->assertEquals(10, $field->getMin());
128
    }
129
130
    public function testGetSetStep()
131
    {
132
        $field = RangeField::create('Test', 'Test');
133
134
        $this->assertNull($field->getStep());
135
136
        $field->setStep(10);
137
138
        $this->assertEquals(10, $field->getStep());
139
    }
140
141
    public function testGetSetMax()
142
    {
143
        $field = RangeField::create('Test', 'Test');
144
145
        $field->setMax(10);
146
147
        $this->assertEquals(10, $field->getMax());
148
    }
149
150
    public function testGetSetRange()
151
    {
152
        $field = RangeField::create('Test', 'Test');
153
154
        $this->assertEquals([], $field->getRange());
155
156
        $field->setRange([
157
            'min' => 25,
158
            '17%' => 33,
159
            '50%' => 50,
160
            '83%' => 66,
161
            'max' => 75
162
        ]);
163
164
        $this->assertEquals([
165
            'min' => 25,
166
            '17%' => 33,
167
            '50%' => 50,
168
            '83%' => 66,
169
            'max' => 75
170
        ], $field->getRange());
171
    }
172
173
    public function testGetSetDensity()
174
    {
175
        $field = RangeField::create('Test', 'Test');
176
177
        $this->assertEquals(4, $field->getDensity());
178
179
        $field->setDensity(10);
180
181
        $this->assertEquals(10, $field->getDensity());
182
    }
183
184
    public function testIsSetPips()
185
    {
186
        $field = RangeField::create('Test', 'Test');
187
188
        $this->assertTrue($field->isShowPips());
189
190
        $field->setShowPips(false);
191
192
        $this->assertFalse($field->isShowPips());
193
    }
194
195
    public function testIsSetSnap()
196
    {
197
        $field = RangeField::create('Test', 'Test');
198
199
        $this->assertFalse($field->isSnap());
200
201
        $field->setSnap(true);
202
203
        $this->assertTrue($field->isSnap());
204
    }
205
206
    public function testGetSetStart()
207
    {
208
        $field = RangeField::create('Test', 'Test', [5, 25]);
209
210
        $this->assertEquals([5, 25], $field->getStart());
211
212
        $field = RangeField::create('Test', 'Test');
213
214
        $this->assertEquals([0], $field->getStart());
215
216
        $field->setStart(10);
217
218
        $this->assertEquals([10], $field->getStart());
219
    }
220
221
    public function testGetSetUnit()
222
    {
223
        $field = RangeField::create('Test', 'Test');
224
225
        $this->assertEquals('', $field->getUnit());
226
227
        $field->setUnit('%');
228
229
        $this->assertEquals('%', $field->getUnit());
230
    }
231
232
    public function testGetSetdecimalPlaces()
233
    {
234
        $field = RangeField::create('Test', 'Test');
235
236
        $this->assertEquals('', $field->getdecimalPlaces());
237
238
        $field->setdecimalPlaces(1);
239
240
        $this->assertEquals(1, $field->getdecimalPlaces());
241
    }
242
243
    public function testSetFormat()
244
    {
245
        $field = RangeField::create('Test', 'Test');
246
247
        $this->assertEquals('', $field->getdecimalPlaces());
248
        $this->assertEquals('', $field->getUnit());
249
250
251
        $field->setFormat('cm', 0);
252
253
        $this->assertEquals(0, $field->getdecimalPlaces());
254
        $this->assertEquals('cm', $field->getUnit());
255
    }
256
}
257