-
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathFieldAttributesValidationTest.php
More file actions
609 lines (516 loc) · 15 KB
/
FieldAttributesValidationTest.php
File metadata and controls
609 lines (516 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
<?php
namespace Styde\Html\Tests;
use Illuminate\Contracts\Validation\Rule;
use Styde\Html\Facades\Field;
use Styde\Html\Fields\FieldBuilder;
use Illuminate\Validation\Rules\In;
use Illuminate\Validation\Rules\NotIn;
class FieldAttributesValidationTest extends TestCase
{
/** @test */
function the_required_attribute_generates_the_required_rule()
{
$this->assertHasRules(['required'], Field::text('name', ['required']));
}
/** @test */
function it_returns_the_email_rule_for_email_fields()
{
$this->assertHasRules(
['email', 'required'],
Field::email('email', ['required' => true])
);
}
/** @test */
function it_returns_the_url_rule_when_the_type_of_the_field_is_url()
{
$this->assertHasRules(['url'], Field::url('link'));
}
/** @test */
function it_builds_the_in_rule_when_the_field_includes_static_options()
{
$builder = Field::select('visibility')->required()->options([
'public' => 'Everyone',
'admin' => 'Admin only',
'auth' => 'Authenticated users only',
'guest' => 'Guest users only',
]);
$this->assertHasRules(
['required', 'in:"public","admin","auth","guest"'],
$builder->getValidationRules()
);
}
/** @test */
function it_builds_the_exists_rule_when_options_come_from_a_table()
{
$field = Field::select('parent_id')
->from('table_name', 'label', 'id', function ($q) {
$q->whereNull('parent_id')
->orderBy('label', 'ASC');
});
$this->assertHasRules(['exists:table_name,id'], $field);
}
/** @test */
function it_adds_the_max_rule()
{
$this->assertHasRules(['max:10'], Field::text('name')->max(10));
$this->assertHasRules(['max:10'], Field::text('name')->maxlength(10));
}
/** @test */
function it_adds_the_file_rule_to_file_fields()
{
$this->assertHasRules(['file'], Field::file('avatar'));
}
/** @test */
function it_adds_the_date_rule_to_date_fields()
{
$this->assertHasRules(['date'], Field::date('time'));
}
/** @test */
function it_adds_the_numeric_rule_to_number_fields()
{
$this->assertHasRules(['numeric'], Field::number('field'));
}
/** @test */
function it_adds_the_nullable_rule()
{
$field = Field::text('name')->nullable();
$this->assertHasRules(['nullable'], Field::text('name')->nullable());
}
/** @test */
function it_adds_the_required_rule()
{
$this->assertHasRules(['required'], Field::text('name')->required());
}
/** @test */
function it_adds_the_min_rule()
{
$this->assertHasRules(['min:10'], Field::text('name')->min(10));
$this->assertHasRules(['min:10'], Field::text('name')->minlength(10));
}
/** @test */
function it_adds_the_regex_rule()
{
$this->assertHasRules(['regex:/.{6,}/'], Field::text('name')->regex('.{6,}'));
}
/** @test */
function it_adds_the_pattern_rule()
{
$field = Field::text('name')->pattern('.{6,}')->getField();
$this->assertHasRules(['regex:/.{6,}/'], $field);
$this->assertTrue($field->hasAttribute('pattern'));
}
/** @test */
function it_adds_the_placeholder_attribute()
{
$field = Field::text('name')->placeholder('This is a placeholder')->getField();
$this->assertEquals(['placeholder' => 'This is a placeholder'], $field->attributes);
}
/** @test */
function it_adds_the_value_attribute()
{
$field = Field::text('name')->value('This is the value')->getField();
$this->assertSame('This is the value', $field->value);
}
/** @test */
function it_adds_the_required_if_rule()
{
$this->assertHasRules(
['required_if:status,1'],
Field::text('name')->requiredIf('status', true)
);
}
/** @test */
function it_adds_the_required_unless_rule()
{
$this->assertHasRules(
['required_unless:price,>=,500'],
Field::text('offer_code')->requiredUnless('price', '>=', 500)
);
$this->assertHasRules(
['required_unless:price,500'],
Field::text('offer_code')->requiredUnless('price', 500)
);
}
/** @test */
function it_adds_the_required_with_rule()
{
$this->assertHasRules(
['required_with:foo,bar,john,doe'],
Field::text('name')->requiredWith('foo', 'bar', 'john', 'doe')
);
}
/** @test */
function it_adds_the_required_with_all_rule()
{
$this->assertHasRules(
['required_with_all:foo,bar'],
Field::text('name')->requiredWithAll('foo', 'bar')
);
}
/** @test */
function it_adds_the_required_without_rule()
{
$this->assertHasRules(
['required_without:John,Doe'],
Field::text('name')->requiredWithout('John', 'Doe')
);
}
/** @test */
function it_adds_the_required_without_all_rule()
{
$this->assertHasRules(
['required_without_all:foo,bar'],
Field::text('name')->requiredWithoutAll('foo', 'bar')
);
}
/** @test */
function it_adds_the_same_rule()
{
$this->assertHasRules(
['numeric', 'same:phone2'],
Field::number('phone')->same('phone2')
);
}
/** @test */
function it_adds_the_size_rule()
{
$this->assertHasRules(['file', 'size:1000'], Field::file('image')->size(1000));
}
/** @test */
function it_adds_the_image_rule()
{
$this->assertHasRules(['file', 'image'], Field::file('image')->image());
}
/** @test */
function it_adds_the_accepted_rule()
{
$this->assertHasRules(['accepted'], Field::text('name')->accepted());
}
/** @test */
function it_adds_the_active_url_rule()
{
$this->assertHasRules(['active_url'], Field::text('name')->activeUrl());
}
/** @test */
function it_adds_the_after_rule()
{
$this->assertHasRules(['after:tomorrow'], Field::text('name')->after('tomorrow'));
}
/** @test */
function it_adds_the_after_or_equal_rule()
{
$this->assertHasRules(
['after_or_equal:tomorrow'],
Field::text('name')->afterOrEqual('tomorrow')
);
}
/** @test */
function it_adds_the_alpha_rule()
{
$this->assertHasRules(['alpha'], Field::text('name')->alpha());
}
/** @test */
function it_adds_the_alpha_dash_rule()
{
$this->assertHasRules(['alpha_dash'], Field::text('name')->alphaDash());
}
/** @test */
function it_adds_the_alpha_num_rule()
{
$this->assertHasRules(['alpha_num'], Field::text('name')->alphaNum());
}
/** @test */
function it_adds_the_array_rule()
{
$this->assertHasRules(['array'], Field::text('name')->array());
}
/** @test */
function it_adds_the_bail_rule()
{
$this->assertHasRules(['bail'], Field::text('name')->bail());
}
/** @test */
function it_adds_the_before_rule()
{
$this->assertHasRules(
['before:tomorrow'],
Field::text('name')->before('tomorrow')
);
}
/** @test */
function it_adds_the_before_or_equal_rule()
{
$this->assertHasRules(
['before_or_equal:tomorrow'],
Field::text('name')->beforeOrEqual('tomorrow')
);
}
/** @test */
function it_adds_the_between_rule()
{
$this->assertHasRules(['between:1,10'], Field::text('name')->between(1, 10));
}
/** @test */
function it_adds_the_boolean_rule()
{
$this->assertHasRules(['boolean'], Field::text('value')->boolean());
}
/** @test */
function it_adds_the_confirmed_rule()
{
$this->assertHasRules(['email', 'confirmed'], Field::email('email')->confirmed());
}
/** @test */
function it_adds_the_date_rule()
{
$this->assertHasRules(['date'], Field::text('date')->date());
}
/** @test */
function it_adds_the_date_equals_rule()
{
$this->assertHasRules(
['date_equals:tomorrow'],
Field::text('date')->dateEquals('tomorrow')
);
}
/** @test */
function it_adds_the_date_format_rule()
{
$this->assertHasRules(
['date_format:d-m-Y'],
Field::text('date')->dateFormat('d-m-Y')
);
}
/** @test */
function it_adds_the_different_rule()
{
$this->assertHasRules(
['different:first_name'],
Field::text('last_name')->different('first_name')
);
}
/** @test */
function it_adds_the_digits_rule()
{
$this->assertHasRules(['numeric', 'digits:2'], Field::number('age')->digits(2));
}
/** @test */
function it_adds_the_digits_between_rule()
{
$this->assertHasRules(
['numeric', 'digits_between:1,2'],
Field::number('age')->digitsBetween(1, 2)
);
}
/** @test */
function it_adds_the_dimensions_rule()
{
$this->assertHasRules(
['file', 'dimensions:min_width=100,max_height=100'],
Field::file('avatar')->dimensions(['min_width' => 100, 'max_height' => 100])
);
}
/** @test */
function it_adds_the_distinct_rule()
{
$this->assertHasRules(['distinct'], Field::text('name')->distinct());
}
/** @test */
function it_adds_the_email_rule()
{
$this->assertHasRules(['email'], Field::text('email')->email());
}
/** @test */
function it_adds_the_email_rule_with_custom_strategies()
{
$this->assertHasRules(['email'], Field::text('email')->email());
}
/** @test */
function it_adds_the_exists_rule()
{
$this->assertHasRules(
['exists:table,column'],
Field::text('foo')->exists('table', 'column')
);
$this->assertHasRules(
['exists:table'],
Field::text('foo')->exists('table')
);
}
/** @test */
function it_adds_the_file_rule()
{
$this->assertHasRules(['file'], Field::text('foobar')->file());
}
/** @test */
function it_adds_the_filled_rule()
{
$this->assertHasRules(['filled'], Field::text('name')->filled());
}
/** @test */
function it_adds_the_gt_rule()
{
$this->assertHasRules(['gt:field'], Field::text('name')->gt('field'));
}
/** @test */
function it_adds_the_gte_rule()
{
$this->assertHasRules(['gte:field'], Field::text('name')->gte('field'));
}
/** @test */
function it_adds_the_in_rule()
{
$this->assertHasRules(
['in:first-zone,second-zone'],
Field::text('name')->in('first-zone', 'second-zone')
);
}
/** @test */
function it_adds_the_in_rule_class()
{
$this->assertHasRules(
[new In(['first-zone','second-zone'])],
Field::text('name')->in(['first-zone', 'second-zone'])
);
}
/** @test */
function it_adds_the_in_array_rule()
{
$this->assertHasRules(
['in_array:value'],
Field::text('name')->inArray('value')
);
}
/** @test */
function it_adds_the_integer_rule()
{
$this->assertHasRules(['integer'], Field::text('dni')->integer());
}
/** @test */
function it_adds_the_ip_rule()
{
$this->assertHasRules(['ip'], Field::text('ip')->ip());
}
/** @test */
function it_adds_the_ipv4_rule()
{
$this->assertHasRules(['ipv4'], Field::text('ip')->ipv4());
}
/** @test */
function it_adds_the_ipv6_rule()
{
$this->assertHasRules(['ipv6'], Field::text('ip')->ipv6());
}
/** @test */
function it_adds_the_json_rule()
{
$this->assertHasRules(['json'], Field::text('data')->json());
}
/** @test */
function it_adds_the_lt_rule()
{
$this->assertHasRules(['lt:field'], Field::text('data')->lt('field'));
}
/** @test */
function it_adds_the_lte_rule()
{
$this->assertHasRules(['lte:field'], Field::text('data')->lte('field'));
}
/** @test */
function it_adds_the_mimetypes_rule()
{
$this->assertHasRules(
['mimetypes:video/avi,video/mpeg'],
Field::text('data')->mimetypes('video/avi', 'video/mpeg')
);
}
/** @test */
function it_adds_the_mimes_rule()
{
$this->assertHasRules(
['mimes:mp3,mp4,avi'],
Field::text('data')->mimes('mp3', 'mp4', 'avi')
);
}
/** @test */
function it_adds_the_not_in_rule_string()
{
$this->assertHasRules(
['not_in:first-zone,second-zone'],
Field::text('name')->notIn('first-zone', 'second-zone')
);
}
/** @test */
function it_adds_the_not_in_rule_class()
{
$this->assertHasRules(
[new NotIn(['first-zone','second-zone'])],
Field::text('name')->notIn(['first-zone', 'second-zone'])
);
}
/** @test */
function it_adds_the_not_regex_rule()
{
$this->assertHasRules(
['not_regex:/.{6,}/'],
Field::text('name')->notRegex('.{6,}')
);
}
/** @test */
function it_adds_the_numeric_rule()
{
$this->assertHasRules(['numeric'], Field::text('number')->numeric());
}
/** @test */
function it_adds_the_present_rule()
{
$this->assertHasRules(['present'], Field::text('name')->present());
}
/** @test */
function it_adds_the_string_rule()
{
$this->assertHasRules(['string'], Field::text('name')->string());
}
/** @test */
function it_adds_the_timezone_rule()
{
$this->assertHasRules(['timezone'], Field::text('date')->timezone());
}
/** @test */
function it_adds_the_unique_rule()
{
$this->assertHasRules(
['unique:users,name,NULL,id'],
Field::text('name')->unique('users', 'name')->getField()
);
}
/** @test */
function it_adds_ignore_in_unique_rule()
{
$this->assertHasRules(
['unique:users,name,"1",user_id'],
Field::text('name')->unique('users', 'name')->ignore(1, 'user_id')
);
}
/** @test */
function it_cannot_use_method_ignore_without_calling_unique_first()
{
$this->expectException('Exception');
Field::text('name')->ignore(1, 'user_id');
}
/** @test */
function it_adds_the_url_rule()
{
$this->assertHasRules(['url'], Field::text('page')->url());
}
protected function assertHasRules(array $rules, $field)
{
if (method_exists($field, 'getParent')) {
$field = $field->getParent();
}
if ($field instanceof FieldBuilder) {
$field = $field->getField();
}
$this->assertEquals($rules, $field->getValidationRules());
}
}