ValidationTest::testMinInValid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 29
rs 9.7333
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Cube\SilverStripe\Validation\Tests;
4
5
use Exception;
6
use SilverStripe\Dev\SapphireTest;
7
use Cube\SilverStripe\Validation\Validator;
8
9
/**
10
 * Class ValidationTest
11
 * @package Cube\SilverStripe\Validation\Tests
12
 */
13
class ValidationTest extends SapphireTest
14
{
15
    /**
16
     * Test the validator with a rule that does not exists
17
     */
18
    public function testRuleDoesNotExists()
19
    {
20
        $validator = Validator::make([
21
            'Name' => 'John'
22
        ], ['Name' => 'this_does_not_exist']);
23
24
        $this->expectException(Exception::class);
25
26
        $validator->validate();
27
    }
28
29
    /**
30
     * Test the Validator without settings rules
31
     * @throws Exception
32
     */
33
    public function testValidWithNoRules()
34
    {
35
        $validator = Validator::make([
36
            'Name' => 'John'
37
        ]);
38
39
        $this->assertTrue($validator->validate());
40
    }
41
42
    /**
43
     * Required needs to be valid
44
     * @throws Exception
45
     */
46
    public function testRequiredValid()
47
    {
48
        $validator = Validator::make([
49
            'Name' => 'John'
50
        ], ['Name' => 'required']);
51
52
        $this->assertTrue($validator->validate());
53
    }
54
55
    /**
56
     * Required needs to be invalid
57
     * @throws Exception
58
     */
59
    public function testRequiredInValid()
60
    {
61
        $validator = Validator::make([
62
            'Email' => '[email protected]'
63
        ], ['Name' => 'required']);
64
65
        $this->assertFalse($validator->validate());
66
    }
67
68
    /**
69
     * Required rule needs to be valid
70
     * with a non existing field
71
     * @throws Exception
72
     */
73
    public function testRequiredWithNonExistingField()
74
    {
75
        $validator = Validator::make([
76
            'Name' => 'John'
77
        ], ['DoesNotExist' => 'required']);
78
79
        $this->assertFalse($validator->validate());
80
    }
81
82
    /**
83
     * Min rule needs to be valid
84
     * @throws Exception
85
     */
86
    public function testMinValid()
87
    {
88
        $validator = Validator::make([
89
            'Age' => 22
90
        ], ['Age' => 'min:18']);
91
92
        $this->assertTrue($validator->validate());
93
    }
94
95
    /**
96
     * Min rule needs to be invalid
97
     * @throws Exception
98
     */
99
    public function testMinInValid()
100
    {
101
        // Length needs to be at least 5
102
        $validator = Validator::make([
103
            'Age' => 17
104
        ], ['Age' => 'min:18']);
105
106
        $this->assertFalse($validator->validate());
107
108
        // Value needs to be numeric
109
        $validator = Validator::make([
110
            'Age' => '18'
111
        ], ['Age' => 'min:2']);
112
113
        $this->assertFalse($validator->validate());
114
115
        // Value needs to be a string
116
        $validator = Validator::make([
117
            'Age' => ['key' => 'value']
118
        ], ['Age' => 'min:2']);
119
120
        $this->assertFalse($validator->validate());
121
122
        // Value needs to be a string
123
        $validator = Validator::make([
124
            'Age' => false
125
        ], ['Age' => 'min:2']);
126
127
        $this->assertFalse($validator->validate());
128
    }
129
130
    /**
131
     * Min rule needs to be valid
132
     * @throws Exception
133
     */
134
    public function testMinLengthValid()
135
    {
136
        $validator = Validator::make([
137
            'Name' => 'John'
138
        ], ['Name' => 'min_length:2']);
139
140
        $this->assertTrue($validator->validate());
141
    }
142
143
    /**
144
     * Min rule needs to be invalid
145
     * @throws Exception
146
     */
147
    public function testMinLengthInValid()
148
    {
149
        // Length needs to be at least 5
150
        $validator = Validator::make([
151
            'Name' => 'John'
152
        ], ['Name' => 'min_length:5']);
153
154
        $this->assertFalse($validator->validate());
155
156
        // Value needs to be a string
157
        $validator = Validator::make([
158
            'Name' => 1
159
        ], ['Name' => 'min_length:2']);
160
161
        $this->assertFalse($validator->validate());
162
163
        // Value needs to be a string
164
        $validator = Validator::make([
165
            'Name' => ['key' => 'value']
166
        ], ['Name' => 'min_length:2']);
167
168
        $this->assertFalse($validator->validate());
169
170
        // Value needs to be a string
171
        $validator = Validator::make([
172
            'Name' => false
173
        ], ['Name' => 'min_length:2']);
174
175
        $this->assertFalse($validator->validate());
176
    }
177
178
    /**
179
     * Max needs to be valid
180
     * @throws Exception
181
     */
182
    public function testMaxValid()
183
    {
184
        $validator = Validator::make([
185
            'Age' => 49
186
        ], ['Age' => 'max:50']);
187
188
        $this->assertTrue($validator->validate());
189
    }
190
191
    /**
192
     * Max rule needs to be invalid
193
     * @throws Exception
194
     */
195
    public function testMaxInValid()
196
    {
197
        // Length needs to be at least 5
198
        $validator = Validator::make([
199
            'Age' => 20
200
        ], ['Age' => 'max:18']);
201
202
        $this->assertFalse($validator->validate());
203
204
        // Value needs to be numeric
205
        $validator = Validator::make([
206
            'Age' => '18'
207
        ], ['Age' => 'max:2']);
208
209
        $this->assertFalse($validator->validate());
210
211
        // Value needs to be a string
212
        $validator = Validator::make([
213
            'Age' => ['key' => 'value']
214
        ], ['Age' => 'max:2']);
215
216
        $this->assertFalse($validator->validate());
217
218
        // Value needs to be a string
219
        $validator = Validator::make([
220
            'Age' => false
221
        ], ['Age' => 'max:2']);
222
223
        $this->assertFalse($validator->validate());
224
    }
225
226
    /**
227
     * Max rule needs to be valid
228
     * @throws Exception
229
     */
230
    public function testMaxLengthValid()
231
    {
232
        $validator = Validator::make([
233
            'Name' => 'abc'
234
        ], ['Name' => 'max_length:3']);
235
236
        $this->assertTrue($validator->validate());
237
    }
238
239
    /**
240
     * Min rule needs to be invalid
241
     * @throws Exception
242
     */
243
    public function testMaxLengthInValid()
244
    {
245
        // Length needs to be at least 5
246
        $validator = Validator::make([
247
            'Name' => 'abc'
248
        ], ['Name' => 'max_length:2']);
249
250
        $this->assertFalse($validator->validate());
251
252
        // Value needs to be a integer
253
        $validator = Validator::make([
254
            'Name' => 18
255
        ], ['Name' => 'max_length:2']);
256
257
        $this->assertFalse($validator->validate());
258
259
        // Value needs to be a integer
260
        $validator = Validator::make([
261
            'Name' => ['key' => 'value']
262
        ], ['Name' => 'max_length:2']);
263
264
        $this->assertFalse($validator->validate());
265
266
        // Value needs to be a integer
267
        $validator = Validator::make([
268
            'Name' => false
269
        ], ['Name' => 'max_length:2']);
270
271
        $this->assertFalse($validator->validate());
272
    }
273
274
    /**
275
     * Regex rule needs to be valid
276
     * @throws Exception
277
     */
278
    public function testRegexValid()
279
    {
280
        $validator = Validator::make([
281
            'Test' => 'This is Cube'
282
        ], ['Test' => ['regex:/cube/i']]);
283
284
        $this->assertTrue($validator->validate());
285
    }
286
287
    /**
288
     * Regex rule needs to be invalid
289
     * @throws Exception
290
     */
291
    public function testRegexInValid()
292
    {
293
        $validator = Validator::make([
294
            'Test' => 'Regex does not match'
295
        ], ['Test' => ['regex:/cube/i']]);
296
297
        $this->assertFalse($validator->validate());
298
    }
299
300
    /**
301
     * Email rule needs to be valid
302
     * @throws Exception
303
     */
304
    public function testEmailValid()
305
    {
306
        $validator = Validator::make([
307
            'Email' => '[email protected]'
308
        ], ['Email' => 'email']);
309
310
        $this->assertTrue($validator->validate());
311
    }
312
313
    /**
314
     * Email rule needs to be invalid
315
     * @throws Exception
316
     */
317
    public function testEmailInValid()
318
    {
319
        $validator = Validator::make([
320
            'Email' => 'info@'
321
        ], ['Email' => 'email']);
322
323
        $this->assertFalse($validator->validate());
324
325
        $validator = Validator::make([
326
            'Email' => 'info'
327
        ], ['Email' => 'email']);
328
329
        $this->assertFalse($validator->validate());
330
331
        $validator = Validator::make([
332
            'Email' => '[email protected]'
333
        ], ['Email' => 'email']);
334
335
        $this->assertFalse($validator->validate());
336
    }
337
}
338