testMultiMultiMultidimensionalGroupArray()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 161
Code Lines 116

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 116
nc 1
nop 0
dl 0
loc 161
rs 8
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 *       __  ___      ____  _     ___                           _                    __
4
 *      /  |/  /_  __/ / /_(_)___/ (_)___ ___  ___  ____  _____(_)___  ____   ____ _/ /
5
 *     / /|_/ / / / / / __/ / __  / / __ `__ \/ _ \/ __ \/ ___/ / __ \/ __ \ / __ `/ /
6
 *    / /  / / /_/ / / /_/ / /_/ / / / / / / /  __/ / / (__  ) / /_/ / / / // /_/ / /
7
 *   /_/  /_/\__,_/_/\__/_/\__,_/_/_/ /_/ /_/\___/_/ /_/____/_/\____/_/ /_(_)__,_/_/
8
 *
9
 *  Array Sanitization Library
10
 *  Copyright (c) Multidimension.al (http://multidimension.al)
11
 *  Github : https://github.com/multidimension-al/array-sanitization
12
 *
13
 *  Licensed under The MIT License
14
 *  For full copyright and license information, please see the LICENSE file
15
 *  Redistributions of files must retain the above copyright notice.
16
 *
17
 *  @copyright  Copyright © 2017-2019 Multidimension.al (http://multidimension.al)
18
 *  @link       https://github.com/multidimension-al/array-sanitization Github
19
 *  @license    http://www.opensource.org/licenses/mit-license.php MIT License
20
 */
21
22
namespace Multidimensional\ArraySanitization\Test;
23
24
use Multidimensional\ArraySanitization\Sanitization;
25
use PHPUnit\Framework\TestCase;
26
27
class SanitizationTest extends TestCase
28
{
29
    public function testIntegers()
30
    {
31
        $rules = [
32
            'a' => [
33
                'type' => 'integer'
34
            ],
35
            'b' => [
36
                'type' => 'integer'
37
            ],
38
            'c' => [
39
                'type' => 'integer'
40
            ],
41
            'd' => [
42
                'type' => 'integer'
43
            ]
44
        ];
45
        $array = [
46
            'a' => 10,
47
            'b' => '11',
48
            'c' => 12.1,
49
            'd' => '13.9'
50
        ];
51
        $result = Sanitization::sanitize($array, $rules);
52
        $expected = [
53
            'a' => 10,
54
            'b' => 11,
55
            'c' => 12,
56
            'd' => 13
57
        ];
58
        $this->assertEquals($expected, $result);
59
    }
60
61
    public function testDecimal()
62
    {
63
        $rules = [
64
            'a' => [
65
                'type' => 'decimal'
66
            ],
67
            'b' => [
68
                'type' => 'decimal'
69
            ],
70
            'c' => [
71
                'type' => 'decimal'
72
            ],
73
            'd' => [
74
                'type' => 'decimal'
75
            ]
76
        ];
77
        $array = [
78
            'a' => 10,
79
            'b' => '11.5',
80
            'c' => 12.1,
81
            'd' => '13.9'
82
        ];
83
        $result = Sanitization::sanitize($array, $rules);
84
        $expected = [
85
            'a' => 10,
86
            'b' => 11.5,
87
            'c' => 12.1,
88
            'd' => 13.9
89
        ];
90
        $this->assertEquals($expected, $result);
91
    }
92
93
    public function testString()
94
    {
95
        $rules = [
96
            'a' => [
97
                'type' => 'string'
98
            ],
99
            'b' => [
100
                'type' => 'string'
101
            ],
102
            'c' => [
103
                'type' => 'string'
104
            ],
105
            'd' => [
106
                'type' => 'string'
107
            ]
108
        ];
109
        $array = [
110
            'a' => '<a href="http://www.google.com">Hello</a>',
111
            'b' => "This isn't really a good test.",
112
            'c' => "The string filter doesn't really do that <i>much</i>.",
113
            'd' => 'Okay. Maybe it does a little?'
114
        ];
115
        $result = Sanitization::sanitize($array, $rules);
116
        $expected = [
117
            'a' => "Hello",
118
            'b' => "This isn&#39;t really a good test.",
119
            'c' => "The string filter doesn&#39;t really do that much.",
120
            'd' => "Okay. Maybe it does a little?"
121
        ];
122
        $this->assertEquals($expected, $result);
123
    }
124
125
    public function testMultidimensionalArray()
126
    {
127
        $rules = [
128
            'a' => [
129
                'type' => 'integer'
130
            ],
131
            'b' => [
132
                'type' => 'Custom',
133
                'fields' => [
134
                    'a' => [
135
                        'type' => 'decimal'
136
                    ]
137
                ]
138
            ]
139
        ];
140
        $array = [
141
            'a' => 10.1,
142
            'b' => [
143
                'a' => 10.1
144
            ]
145
        ];
146
        $result = Sanitization::sanitize($array, $rules);
147
        $expected = [
148
            'a' => '10',
149
            'b' => [
150
                'a' => 10.1
151
            ]
152
        ];
153
        $this->assertEquals($expected, $result);
154
    }
155
156
    public function testMultidimensionalGroupArray()
157
    {
158
        $rules = [
159
            'Population' => [
160
                'type' => 'array',
161
                'fields' => [
162
                    'People' => [
163
                        'type' => 'group',
164
                        'fields' => [
165
                            'Name' => [
166
                                'type' => 'string'
167
                            ]
168
                        ]
169
                    ]
170
                ]
171
            ]
172
        ];
173
        $array = [
174
            'Population' => [
175
                'People' => [
176
                    0 => [
177
                        'Name' => 'John Smith'
178
                    ],
179
                    1 => [
180
                        'Name' => 'Jane Smith'
181
                    ]
182
                ]
183
            ]
184
        ];
185
        $result = Sanitization::sanitize($array, $rules);
186
        $expected = [
187
            'Population' => [
188
                'People' => [
189
                    0 => [
190
                        'Name' => 'John Smith'
191
                    ],
192
                    1 => [
193
                        'Name' => 'Jane Smith'
194
                    ]
195
                ]
196
            ]
197
        ];
198
        $this->assertEquals($expected, $result);
199
    }
200
201
    public function testMultiMultidimensionalGroupArray()
202
    {
203
        $rules = [
204
            'RateV4Response' => [
205
                'type' => 'array',
206
                'fields' => [
207
                    'Package' => [
208
                        'type' => 'group',
209
                        'fields' => [
210
                            '@ID' => [
211
                                'type' => 'string',
212
                                'required' => true
213
                            ],
214
                            'ZipOrigination' => [
215
                                'type' => 'string',
216
                                'required' => true,
217
                                'pattern' => '\d{5}'
218
                            ],
219
                            'ZipDestination' => [
220
                                'type' => 'string',
221
                                'required' => true,
222
                                'pattern' => '\d{5}'
223
                            ],
224
                            'Pounds' => [
225
                                'type' => 'decimal',
226
                                'required' => true,
227
                            ],
228
                            'Ounces' => [
229
                                'type' => 'decimal',
230
                                'required' => true,
231
                            ],
232
                            'FirstClassMailType' => [
233
                                'type' => 'string'
234
                            ],
235
                            'Container' => [
236
                                'type' => 'string',
237
                            ],
238
                            'Size' => [
239
                                'type' => 'string',
240
                                'required' => true,
241
                                'values' => [
242
                                    'REGULAR',
243
                                    'LARGE'
244
                                ]
245
                            ],
246
                            'Width' => [
247
                                'type' => 'decimal'
248
                            ],
249
                            'Length' => [
250
                                'type' => 'decimal'
251
                            ],
252
                            'Height' => [
253
                                'type' => 'decimal'
254
                            ],
255
                            'Girth' => [
256
                                'type' => 'decimal'
257
                            ],
258
                            'Machinable' => [
259
                                'type' => 'boolean'
260
                            ],
261
                            'Zone' => [
262
                                'type' => 'string'
263
                            ],
264
                            'Postage' => [
265
                                'type' => 'group',
266
                                'required' => true,
267
                                'fields' => [
268
                                    '@CLASSID' => [
269
                                        'type' => 'integer'
270
                                    ],
271
                                    'MailService' => [
272
                                        'type' => 'string'
273
                                    ],
274
                                    'Rate' => [
275
                                        'type' => 'decimal'
276
                                    ]
277
                                ]
278
                            ]
279
                        ]
280
                    ]
281
                ]
282
            ]
283
        ];
284
        $array = ['RateV4Response' => ['Package' => ['@ID' => '123', 'ZipOrigination' => 20500, 'ZipDestination' => 90210, 'Pounds' => '0', 'Ounces' => '32', 'Size' => 'REGULAR', 'Machinable' => 'TRUE', 'Zone' => '8', 'Postage' => [0 => ['@CLASSID' => '1', 'MailService' => 'Priority Mail 2-Day<sup>™</sup>', 'Rate' => '12.75'], 1 => ['@CLASSID' => '22', 'MailService' => 'Priority Mail 2-Day<sup>™</sup> Large Flat Rate Box', 'Rate' => '18.85'], 2 => ['@CLASSID' => '17', 'MailService' => 'Priority Mail 2-Day<sup>™</sup> Medium Flat Rate Box', 'Rate' => '13.60'], 3 => ['@CLASSID' => '28', 'MailService' => 'Priority Mail 2-Day<sup>™</sup> Small Flat Rate Box', 'Rate' => '7.15']]]]];
285
        $result = Sanitization::sanitize($array, $rules);
286
        $expected = [
287
            'RateV4Response' => [
288
                'Package' => [
289
                    '@ID' => '123',
290
                    'ZipOrigination' => '20500',
291
                    'ZipDestination' => '90210',
292
                    'Pounds' => 0.0,
293
                    'Ounces' => 32.0,
294
                    'Size' => 'REGULAR',
295
                    'Machinable' => true,
296
                    'Zone' => '8',
297
                    'Postage' => [
298
                        0 => [
299
                            '@CLASSID' => 1,
300
                            'MailService' => 'Priority Mail 2-Day™',
301
                            'Rate' => 12.75
302
                        ],
303
                        1 => [
304
                            '@CLASSID' => 22,
305
                            'MailService' => 'Priority Mail 2-Day™ Large Flat Rate Box',
306
                            'Rate' => 18.85
307
                        ],
308
                        2 => [
309
                            '@CLASSID' => 17,
310
                            'MailService' => 'Priority Mail 2-Day™ Medium Flat Rate Box',
311
                            'Rate' => 13.60
312
                        ],
313
                        3 => [
314
                            '@CLASSID' => 28,
315
                            'MailService' => 'Priority Mail 2-Day™ Small Flat Rate Box',
316
                            'Rate' => 7.15
317
                        ]
318
                    ]
319
                ]
320
            ]
321
        ];
322
        $this->assertEquals($expected, $result);
323
    }
324
325
    public function testMultiMultiMultidimensionalGroupArray()
326
    {
327
        $rules = [
328
            'RateV4Response' => [
329
                'type' => 'array',
330
                'fields' => [
331
                    'Package' => [
332
                        'type' => 'group',
333
                        'fields' => [
334
                            '@ID' => [
335
                                'type' => 'string',
336
                                'required' => true
337
                            ],
338
                            'ZipOrigination' => [
339
                                'type' => 'string',
340
                                'required' => true,
341
                                'pattern' => '\d{5}'
342
                            ],
343
                            'ZipDestination' => [
344
                                'type' => 'string',
345
                                'required' => true,
346
                                'pattern' => '\d{5}'
347
                            ],
348
                            'Pounds' => [
349
                                'type' => 'decimal',
350
                                'required' => true,
351
                            ],
352
                            'Ounces' => [
353
                                'type' => 'decimal',
354
                                'required' => true,
355
                            ],
356
                            'FirstClassMailType' => [
357
                                'type' => 'string'
358
                            ],
359
                            'Container' => [
360
                                'type' => 'string',
361
                            ],
362
                            'Size' => [
363
                                'type' => 'string',
364
                                'required' => true,
365
                                'values' => [
366
                                    'REGULAR',
367
                                    'LARGE'
368
                                ]
369
                            ],
370
                            'Width' => [
371
                                'type' => 'decimal'
372
                            ],
373
                            'Length' => [
374
                                'type' => 'decimal'
375
                            ],
376
                            'Height' => [
377
                                'type' => 'decimal'
378
                            ],
379
                            'Girth' => [
380
                                'type' => 'decimal'
381
                            ],
382
                            'Machinable' => [
383
                                'type' => 'boolean'
384
                            ],
385
                            'Zone' => [
386
                                'type' => 'string'
387
                            ],
388
                            'Postage' => [
389
                                'type' => 'group',
390
                                'required' => true,
391
                                'fields' => [
392
                                    '@CLASSID' => [
393
                                        'type' => 'integer'
394
                                    ],
395
                                    'MailService' => [
396
                                        'type' => 'string'
397
                                    ],
398
                                    'Rate' => [
399
                                        'type' => 'decimal'
400
                                    ]
401
                                ]
402
                            ]
403
                        ]
404
                    ]
405
                ]
406
            ]
407
        ];
408
        $array = [
409
            'RateV4Response' => [
410
                'Package' => [
411
                    0 => [
412
                        '@ID' => '123',
413
                        'ZipOrigination' => 20500,
414
                        'ZipDestination' => 90210,
415
                        'Pounds' => '0',
416
                        'Ounces' => '32',
417
                        'Size' => 'REGULAR',
418
                        'Machinable' => 'TRUE',
419
                        'Zone' => '8',
420
                        'Postage' => [
421
                            0 => [
422
                                '@CLASSID' => '1',
423
                                'MailService' => 'Priority Mail 2-Day<sup>™</sup>',
424
                                'Rate' => '12.75'
425
                            ],
426
                            1 => [
427
                                '@CLASSID' => '22',
428
                                'MailService' => 'Priority Mail 2-Day<sup>™</sup> Large Flat Rate Box',
429
                                'Rate' => '18.85'
430
                            ],
431
                            2 => [
432
                                '@CLASSID' => '17',
433
                                'MailService' => 'Priority Mail 2-Day<sup>™</sup> Medium Flat Rate Box',
434
                                'Rate' => '13.60'
435
                            ],
436
                            3 => [
437
                                '@CLASSID' => '28',
438
                                'MailService' => 'Priority Mail 2-Day<sup>™</sup> Small Flat Rate Box',
439
                                'Rate' => '7.15'
440
                            ]
441
                        ]
442
                    ]
443
                ]
444
            ]
445
        ];
446
        $result = Sanitization::sanitize($array, $rules);
447
        $expected = [
448
            'RateV4Response' => [
449
                'Package' => [
450
                    0 => [
451
                        '@ID' => '123',
452
                        'ZipOrigination' => '20500',
453
                        'ZipDestination' => '90210',
454
                        'Pounds' => 0.0,
455
                        'Ounces' => 32.0,
456
                        'Size' => 'REGULAR',
457
                        'Machinable' => true,
458
                        'Zone' => '8',
459
                        'Postage' => [
460
                            0 => [
461
                                '@CLASSID' => 1,
462
                                'MailService' => 'Priority Mail 2-Day™',
463
                                'Rate' => 12.75
464
                            ],
465
                            1 => [
466
                                '@CLASSID' => 22,
467
                                'MailService' => 'Priority Mail 2-Day™ Large Flat Rate Box',
468
                                'Rate' => 18.85
469
                            ],
470
                            2 => [
471
                                '@CLASSID' => 17,
472
                                'MailService' => 'Priority Mail 2-Day™ Medium Flat Rate Box',
473
                                'Rate' => 13.6
474
                            ],
475
                            3 => [
476
                                '@CLASSID' => 28,
477
                                'MailService' => 'Priority Mail 2-Day™ Small Flat Rate Box',
478
                                'Rate' => 7.15
479
                            ]
480
                        ]
481
                    ]
482
                ]
483
            ]
484
        ];
485
        $this->assertEquals($expected, $result);
486
    }
487
488
    public function testBoolean()
489
    {
490
        $rules = [
491
            'a' => [
492
                'type' => 'boolean'
493
            ],
494
            'b' => [
495
                'type' => 'boolean'
496
            ],
497
            'c' => [
498
                'type' => 'boolean'
499
            ],
500
            'd' => [
501
                'type' => 'boolean'
502
            ],
503
            'e' => [
504
                'type' => 'boolean'
505
            ]
506
        ];
507
        $array = [
508
            'a' => true,
509
            'b' => 'true',
510
            'c' => false,
511
            'd' => 'false',
512
            'e' => null
513
        ];
514
        $result = Sanitization::sanitize($array, $rules);
515
        $expected = [
516
            'a' => true,
517
            'b' => true,
518
            'c' => false,
519
            'd' => false,
520
            'e' => false
521
        ];
522
        $this->assertEquals($expected, $result);
523
    }
524
    
525
    public function testPattern()
526
    {
527
        $rules = [
528
            'a' => [
529
                'pattern' => '\d{5}'
530
            ],
531
            'b' => [
532
                'pattern' => 'ISO 8601'
533
            ],
534
            'c' => [
535
                'pattern' => '\d{5}'
536
            ],
537
        ];
538
        $array = [
539
            'a' => 'abc12345def',
540
            'b' => '2005-08-15T15:52:01+00:00ABC',
541
            'c' => '12345'
542
        ];
543
        $result = Sanitization::sanitize($array, $rules);
544
        $expected = [
545
            'a' => '12345',
546
            'b' => '2005-08-15T15:52:01+00:00',
547
            'c' => '12345'
548
        ];
549
        $this->assertEquals($expected, $result);
550
    }
551
    
552
    public function testISO8601()
553
    {
554
        $rules = [
555
            'a' => [
556
                'pattern' => 'ISO 8601'
557
            ],
558
            'b' => [
559
                'pattern' => 'ISO 8601'
560
            ],
561
            'c' => [
562
                'pattern' => 'ISO 8601'
563
            ]
564
        ];
565
        $array = [
566
            'a' => '2017-02-18T20:44:48+00:00ABCDEF',
567
            'b' => '2017##$$$-02-18T20:44:48Z',
568
            'c' => '2017-02-18T20:44:48-06:00',
569
        ];
570
        $result = Sanitization::sanitize($array, $rules);
571
        $expected = [
572
            'a' => '2017-02-18T20:44:48+00:00',
573
            'b' => '2017-02-18T20:44:48Z',
574
            'c' => '2017-02-18T20:44:48-06:00'
575
        ];
576
        $this->assertEquals($expected, $result);
577
    }
578
    
579
    public function testValues()
580
    {
581
        $rules = [
582
            'a' => [
583
                'type' => 'integer',
584
                'values' => [
585
                    100,
586
                    101,
587
                    102
588
                ]
589
            ],
590
            'b' => [
591
                'type' => 'string',
592
                'values' => [
593
                    'hello',
594
                    'goodbye'
595
                ]
596
            ],
597
            'c' => [
598
                'type' => 'string',
599
                'values' => 'goodbye'
600
            ]
601
        ];
602
        $array = [
603
            'a' => '101',
604
            'b' => 'hello',
605
            'c' => 'goodbye'
606
        ];
607
        $result = Sanitization::sanitize($array, $rules);
608
        $expected = [
609
            'a' => 101,
610
            'b' => 'hello',
611
            'c' => 'goodbye'
612
        ];
613
        $this->assertEquals($expected, $result);
614
    }
615
616
    public function testValuesFailure()
617
    {
618
        $rules = [
619
            'a' => [
620
                'type' => 'integer',
621
                'values' => [
622
                    100,
623
                    101,
624
                    102
625
                ]
626
            ]
627
        ];
628
        $array = [
629
            'a' => '103',
630
        ];
631
        $result = Sanitization::sanitize($array, $rules);
632
        $expected = [
633
            'a' => 103,
634
        ];
635
        $this->assertEquals($expected, $result);
636
    }
637
}
638