StringAttributeTest::itShouldCheckStringIsString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 15
rs 9.4286
c 1
b 1
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 9/18/14
5
 * Time: 12:34 AM
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Tests\NilPortugues\Validator\Attribute\String;
12
13
use NilPortugues\Validator\Validator;
14
15
/**
16
 * Class StringAttributeTest
17
 * @package Tests\NilPortugues\Validator\Attribute\StringAttribute
18
 */
19
class StringAttributeTest extends \PHPUnit_Framework_TestCase
20
{
21
    /**
22
     * @return \NilPortugues\Validator\Attribute\String\StringAttribute
23
     */
24
    protected function getValidator()
25
    {
26
        $validator = Validator::create();
27
28
        return $validator->isString('propertyName');
29
    }
30
31
    /**
32
     * @test
33
     */
34
    public function itShouldCheckStringIsString()
35
    {
36
        $value  = 'asdsdadds';
37
        $result = $this->getValidator()->validate($value);
38
        $this->assertTrue($result);
39
40
        $value  = new \StdClass();
41
        $validator = $this->getValidator();
42
43
        $result = $validator->validate($value);
44
        $errors = $validator->getErrors();
45
46
        $this->assertFalse($result);
47
        $this->assertArrayHasKey('string', $errors['propertyName']);
48
    }
49
50
    /**
51
     * @test
52
     */
53
    public function itShouldCheckStringIsAlphanumeric()
54
    {
55
        $value  = 'Qwerty1234';
56
        $result = $this->getValidator()->isAlphanumeric()->validate($value);
57
        $this->assertTrue($result);
58
59
        $value  = '';
60
        $result = $this->getValidator()->isAlphanumeric()->validate($value);
61
        $this->assertFalse($result);
62
    }
63
64
    /**
65
     * @test
66
     */
67
    public function itShouldCheckStringIsAlpha()
68
    {
69
        $value  = 'querty';
70
        $result = $this->getValidator()->isAlpha()->validate($value);
71
        $this->assertTrue($result);
72
73
        $value  = 'querty123';
74
        $result = $this->getValidator()->isAlpha()->validate($value);
75
        $this->assertFalse($result);
76
    }
77
78
    /**
79
     * @test
80
     */
81
    public function itShouldCheckStringIsBetween()
82
    {
83
        $value  = 'Nil';
84
        $result = $this->getValidator()->isBetween(2, 4, false)->validate($value);
85
        $this->assertTrue($result);
86
87
        $value  = 'Nilo';
88
        $result = $this->getValidator()->isBetween(2, 4, true)->validate($value);
89
        $this->assertTrue($result);
90
    }
91
92
    /**
93
     * @test
94
     */
95
    public function itShouldCheckStringIsBetweenException()
96
    {
97
        $this->setExpectedException('\InvalidArgumentException');
98
        $this->getValidator()->isBetween(4, 2, false)->validate('Nil');
99
    }
100
101
    /**
102
     * @test
103
     */
104
    public function itShouldCheckStringIsCharset()
105
    {
106
        $value = 'Portugués';
107
108
        $result = $this->getValidator()->isCharset(['UTF-8'])->validate($value);
109
110
        $this->assertTrue($result);
111
    }
112
113
    /**
114
     * @test
115
     */
116
    public function itShouldCheckStringIsAllConsonants()
117
    {
118
        $value  = 'a';
119
        $result = $this->getValidator()->isAllConsonants()->validate($value);
120
        $this->assertFalse($result);
121
122
        $value  = 'bs';
123
        $result = $this->getValidator()->isAllConsonants()->validate($value);
124
        $this->assertTrue($result);
125
    }
126
127
    /**
128
     * @test
129
     */
130
    public function itShouldCheckStringIsContains()
131
    {
132
        $value     = 'AAAAAAAaaaA';
133
        $contains  = 'aaa';
134
        $identical = true;
135
        $result    = $this->getValidator()->contains($contains, $identical)->validate($value);
136
        $this->assertTrue($result);
137
138
        $value     = 'AAAAAAA123A';
139
        $contains  = 123;
140
        $identical = false;
141
        $result    = $this->getValidator()->contains($contains, $identical)->validate($value);
142
        $this->assertTrue($result);
143
    }
144
145
    /**
146
     * @test
147
     */
148
    public function itShouldCheckStringIsControlCharacters()
149
    {
150
        $value  = "\n\t";
151
        $result = $this->getValidator()->isControlCharacters()->validate($value);
152
        $this->assertTrue($result);
153
154
        $value  = "\nHello\tWorld";
155
        $result = $this->getValidator()->isControlCharacters()->validate($value);
156
        $this->assertFalse($result);
157
    }
158
159
    /**
160
     * @test
161
     */
162
    public function itShouldCheckStringIsDigit()
163
    {
164
        $value  = 'A';
165
        $result = $this->getValidator()->isDigit()->validate($value);
166
        $this->assertFalse($result);
167
168
        $value  = 145.6;
169
        $result = $this->getValidator()->isDigit()->validate($value);
170
        $this->assertFalse($result);
171
    }
172
173
    /**
174
     * @test
175
     */
176
    public function itShouldCheckStringIsEndsWith()
177
    {
178
        $value     = 'AAAAAAAaaaA';
179
        $contains  = 'aaaA';
180
        $identical = true;
181
        $result    = $this->getValidator()->endsWith($contains, $identical)->validate($value);
182
        $this->assertTrue($result);
183
184
        $value     = 'AAAAAAA123';
185
        $contains  = 123;
186
        $identical = false;
187
        $result    = $this->getValidator()->endsWith($contains, $identical)->validate($value);
188
        $this->assertTrue($result);
189
    }
190
191
    /**
192
     * @test
193
     */
194
    public function itShouldCheckStringIsEquals()
195
    {
196
        $value         = 'hello';
197
        $comparedValue = 'hello';
198
        $identical     = true;
199
        $result        = $this->getValidator()->equals($comparedValue, $identical)->validate($value);
200
        $this->assertTrue($result);
201
202
        $value         = '1';
203
        $comparedValue = 1;
204
        $identical     = false;
205
        $result        = $this->getValidator()->equals($comparedValue, $identical)->validate($value);
206
207
        $this->assertTrue($result);
208
    }
209
210
    /**
211
     * @test
212
     */
213
    public function itShouldCheckStringIsIn()
214
    {
215
        $haystack  = 'a12245 asdhsjasd 63-211';
216
        $value     = "122";
217
        $identical = false;
218
        $result    = $this->getValidator()->in($haystack, $identical)->validate($value);
219
        $this->assertTrue($result);
220
221
        $haystack  = 'a12245 asdhsjasd 63-211';
222
        $value     = '5 asd';
223
        $identical = true;
224
        $result    = $this->getValidator()->in($haystack, $identical)->validate($value);
225
        $this->assertTrue($result);
226
    }
227
228
    /**
229
     * @test
230
     */
231
    public function itShouldCheckStringIsGraph()
232
    {
233
        $value  = 'arf12';
234
        $result = $this->getValidator()->hasGraphicalCharsOnly()->validate($value);
235
        $this->assertTrue($result);
236
237
        $value  = "asdf\n\r\t";
238
        $result = $this->getValidator()->hasGraphicalCharsOnly()->validate($value);
239
        $this->assertFalse($result);
240
    }
241
242
    /**
243
     * @test
244
     */
245
    public function itShouldCheckStringIsLength()
246
    {
247
        $value  = 'abcdefgh';
248
        $length = 5;
249
        $result = $this->getValidator()->hasLength($length)->validate($value);
250
        $this->assertFalse($result);
251
252
        $value  = 'abcdefgh';
253
        $length = 8;
254
        $result = $this->getValidator()->hasLength($length)->validate($value);
255
        $this->assertTrue($result);
256
    }
257
258
    /**
259
     * @test
260
     */
261
    public function itShouldCheckStringIsLowercase()
262
    {
263
        $value  = 'strtolower';
264
        $result = $this->getValidator()->isLowercase()->validate($value);
265
        $this->assertTrue($result);
266
    }
267
268
    /**
269
     * @test
270
     */
271
    public function itShouldCheckStringIsNotEmpty()
272
    {
273
        $value  = 'a';
274
        $result = $this->getValidator()->notEmpty()->validate($value);
275
        $this->assertTrue($result);
276
277
        $value  = '';
278
        $result = $this->getValidator()->notEmpty()->validate($value);
279
        $this->assertFalse($result);
280
    }
281
282
    /**
283
     * @test
284
     */
285
    public function itShouldCheckStringIsNoWhitespace()
286
    {
287
        $value  = 'aaaaa';
288
        $result = $this->getValidator()->noWhitespace()->validate($value);
289
        $this->assertTrue($result);
290
291
        $value  = 'lorem ipsum';
292
        $result = $this->getValidator()->noWhitespace()->validate($value);
293
        $this->assertFalse($result);
294
    }
295
296
    /**
297
     * @test
298
     */
299
    public function itShouldCheckStringIsPrintable()
300
    {
301
        $value  = 'LMKA0$% _123';
302
        $result = $this->getValidator()->hasPrintableCharsOnly()->validate($value);
303
        $this->assertTrue($result);
304
305
        $value  = "LMKA0$%\t_123";
306
        $result = $this->getValidator()->hasPrintableCharsOnly()->validate($value);
307
        $this->assertFalse($result);
308
    }
309
310
    /**
311
     * @test
312
     */
313
    public function itShouldCheckStringIsPunctuation()
314
    {
315
        $value  = '&,.;[]';
316
        $result = $this->getValidator()->isPunctuation()->validate($value);
317
        $this->assertTrue($result);
318
319
        $value  = 'a';
320
        $result = $this->getValidator()->isPunctuation()->validate($value);
321
        $this->assertFalse($result);
322
    }
323
324
    /**
325
     * @test
326
     */
327
    public function itShouldCheckStringIsRegex()
328
    {
329
        $value  = 'a';
330
        $regex  = '/[a-z]/';
331
        $result = $this->getValidator()->matchesRegex($regex)->validate($value);
332
        $this->assertTrue($result);
333
334
        $value  = 'A';
335
        $regex  = '/[a-z]/';
336
        $result = $this->getValidator()->matchesRegex($regex)->validate($value);
337
        $this->assertFalse($result);
338
    }
339
340
    /**
341
     * @test
342
     */
343
    public function itShouldCheckStringIsSlug()
344
    {
345
        $value  = 'hello-world-yeah';
346
        $result = $this->getValidator()->isSlug()->validate($value);
347
        $this->assertTrue($result);
348
349
        $value  = '-hello-world-yeah';
350
        $result = $this->getValidator()->isSlug()->validate($value);
351
        $this->assertFalse($result);
352
353
        $value  = 'hello-world-yeah-';
354
        $result = $this->getValidator()->isSlug()->validate($value);
355
        $this->assertFalse($result);
356
357
        $value  = 'hello-world----yeah';
358
        $result = $this->getValidator()->isSlug()->validate($value);
359
        $this->assertFalse($result);
360
    }
361
362
    /**
363
     * @test
364
     */
365
    public function itShouldCheckStringIsSpace()
366
    {
367
        $value  = '    ';
368
        $result = $this->getValidator()->isSpace()->validate($value);
369
        $this->assertTrue($result);
370
371
        $value  = 'e e';
372
        $result = $this->getValidator()->isSpace()->validate($value);
373
        $this->assertFalse($result);
374
    }
375
376
    /**
377
     * @test
378
     */
379
    public function itShouldCheckStringIsStartsWith1()
380
    {
381
        $value     = 'aaaAAAAAAAA';
382
        $contains  = 'aaaA';
383
        $identical = true;
384
        $result    = $this->getValidator()->startsWith($contains, $identical)->validate($value);
385
        $this->assertTrue($result);
386
387
        $value     = '123AAAAAAA';
388
        $contains  = 123;
389
        $identical = false;
390
        $result    = $this->getValidator()->startsWith($contains, $identical)->validate($value);
391
        $this->assertTrue($result);
392
    }
393
394
    /**
395
     * @test
396
     */
397
    public function itShouldCheckStringIsUppercase()
398
    {
399
        $value  = 'AAAAAA';
400
        $result = $this->getValidator()->isUppercase()->validate($value);
401
        $this->assertTrue($result);
402
403
        $value  = 'aaaa';
404
        $result = $this->getValidator()->isUppercase()->validate($value);
405
        $this->assertFalse($result);
406
    }
407
408
    /**
409
     * @test
410
     */
411
    public function itShouldCheckStringIsVersion()
412
    {
413
        $value  = '1.0.2';
414
        $result = $this->getValidator()->isVersion()->validate($value);
415
        $this->assertTrue($result);
416
417
        $value  = '1.0.2-beta';
418
        $result = $this->getValidator()->isVersion()->validate($value);
419
        $this->assertTrue($result);
420
421
        $value  = '1.0';
422
        $result = $this->getValidator()->isVersion()->validate($value);
423
        $this->assertTrue($result);
424
425
        $value  = '1.0.2 beta';
426
        $result = $this->getValidator()->isVersion()->validate($value);
427
        $this->assertFalse($result);
428
    }
429
430
    /**
431
     * @test
432
     */
433
    public function itShouldCheckStringIsVowel()
434
    {
435
        $value  = 'aeA';
436
        $result = $this->getValidator()->isVowel()->validate($value);
437
        $this->assertTrue($result);
438
439
        $value  = 'cds';
440
        $result = $this->getValidator()->isVowel()->validate($value);
441
        $this->assertFalse($result);
442
    }
443
444
    /**
445
     * @test
446
     */
447
    public function itShouldCheckStringIsXdigit()
448
    {
449
        $value  = '100';
450
        $result = $this->getValidator()->isHexDigit()->validate($value);
451
        $this->assertTrue($result);
452
453
        $value  = 'h0000';
454
        $result = $this->getValidator()->isHexDigit()->validate($value);
455
        $this->assertFalse($result);
456
    }
457
458
    /**
459
     * @test
460
     */
461
    public function itShouldStopValidationOnFirstError()
462
    {
463
        $value     = '@aaa';
464
        $validator = $this->getValidator();
465
466
        $result = $validator->isAlphanumeric()->validate($value, true);
467
468
        $this->assertFalse($result);
469
        $this->assertNotEmpty($validator->getErrors());
470
    }
471
472
    /**
473
     * @test
474
     */
475
    public function itShouldCheckIfHasLowercase()
476
    {
477
        $this->assertTrue($this->getValidator()->hasLowercase()->validate('HELLOWOrLD'));
478
        $this->assertTrue($this->getValidator()->hasLowercase(3)->validate('HeLLoWOrLD'));
479
480
        $this->assertFalse($this->getValidator()->hasLowercase()->validate('HELLOWORLD'));
481
        $this->assertFalse($this->getValidator()->hasLowercase(3)->validate('el'));
482
    }
483
484
    /**
485
     * @test
486
     */
487
    public function itShouldCheckIfHasUppercase()
488
    {
489
        $this->assertTrue($this->getValidator()->hasUppercase()->validate('hello World'));
490
        $this->assertTrue($this->getValidator()->hasUppercase(2)->validate('Hello World'));
491
492
        $this->assertFalse($this->getValidator()->hasUppercase()->validate('hello world'));
493
        $this->assertFalse($this->getValidator()->hasUppercase(2)->validate('helloWorld'));
494
    }
495
496
    /**
497
     * @test
498
     */
499
    public function itShouldCheckIfHasNumeric()
500
    {
501
        $this->assertTrue($this->getValidator()->hasNumeric()->validate('hell0 W0rld'));
502
        $this->assertTrue($this->getValidator()->hasNumeric(3)->validate('H3ll0 W0rld'));
503
504
        $this->assertFalse($this->getValidator()->hasNumeric()->validate('hello world'));
505
        $this->assertFalse($this->getValidator()->hasNumeric(2)->validate('h3lloWorld'));
506
    }
507
508
    /**
509
     * @test
510
     */
511
    public function itShouldCheckIfHasSpecialCharacters()
512
    {
513
        $this->assertTrue($this->getValidator()->hasSpecialCharacters()->validate('hell0@W0rld'));
514
        $this->assertTrue($this->getValidator()->hasSpecialCharacters(2)->validate('H3ll0@W0@rld'));
515
516
        $this->assertFalse($this->getValidator()->hasSpecialCharacters()->validate('hello world'));
517
        $this->assertFalse($this->getValidator()->hasSpecialCharacters(2)->validate('h3llo@World'));
518
    }
519
520
    /**
521
     * @test
522
     */
523
    public function itShouldCheckIfIsEmail()
524
    {
525
        $this->assertTrue($this->getValidator()->isEmail()->validate('[email protected]'));
526
        $this->assertTrue($this->getValidator()->isEmail()->validate('[email protected]'));
527
        $this->assertTrue($this->getValidator()->isEmail()->validate('[email protected]'));
528
        $this->assertTrue($this->getValidator()->isEmail()->validate('[email protected]'));
529
        $this->assertTrue($this->getValidator()->isEmail()->validate('[email protected]'));
530
        $this->assertTrue($this->getValidator()->isEmail()->validate('[email protected]'));
531
        $this->assertTrue($this->getValidator()->isEmail()->validate('[email protected]'));
532
533
        $this->assertFalse($this->getValidator()->isEmail()->validate('hello.earth+moon@localhost'));
534
    }
535
536
    /**
537
     * @test
538
     */
539
    public function itShouldCheckIfIsUrl()
540
    {
541
        $this->assertTrue($this->getValidator()->isUrl()->validate('http://google.com'));
542
        $this->assertTrue($this->getValidator()->isUrl()->validate('http://google.com/robots.txt'));
543
        $this->assertTrue($this->getValidator()->isUrl()->validate('https://google.com'));
544
        $this->assertTrue($this->getValidator()->isUrl()->validate('https://google.com/robots.txt'));
545
        $this->assertTrue($this->getValidator()->isUrl()->validate('//google.com'));
546
        $this->assertTrue($this->getValidator()->isUrl()->validate('//google.com/robots.txt'));
547
    }
548
549
    /**
550
     * @test
551
     */
552
    public function itShouldValidateUUID()
553
    {
554
        $this->assertTrue($this->getValidator()->isUUID()->validate('6ba7b810-9dad-11d1-80b4-00c04fd430c8'));
555
        $this->assertTrue($this->getValidator()->isUUID()->validate('6ba7b811-9dad-11d1-80b4-00c04fd430c8'));
556
        $this->assertTrue($this->getValidator()->isUUID()->validate('6ba7b812-9dad-11d1-80b4-00c04fd430c8'));
557
        $this->assertTrue($this->getValidator()->isUUID()->validate('6ba7b814-9dad-11d1-80b4-00c04fd430c8'));
558
        $this->assertTrue($this->getValidator()->isUUID()->validate('00000000-0000-0000-0000-000000000000'));
559
560
        $this->assertTrue($this->getValidator()->isUUID(false)->validate('6ba7b810-9dad-11d1-80b4-00c04fd430c8'));
561
        $this->assertTrue($this->getValidator()->isUUID(false)->validate('6ba7b811-9dad-11d1-80b4-00c04fd430c8'));
562
        $this->assertTrue($this->getValidator()->isUUID(false)->validate('6ba7b812-9dad-11d1-80b4-00c04fd430c8'));
563
        $this->assertTrue($this->getValidator()->isUUID(false)->validate('6ba7b814-9dad-11d1-80b4-00c04fd430c8'));
564
        $this->assertTrue($this->getValidator()->isUUID(false)->validate('00000000-0000-0000-0000-000000000000'));
565
566
        $this->assertFalse($this->getValidator()->isUUID()->validate('{6ba7b810-9dad-11d1-80b4-00c04fd430c8}'));
567
        $this->assertFalse($this->getValidator()->isUUID()->validate('216f-ff40-98d9-11e3-a5e2-0800-200c-9a66'));
568
        $this->assertFalse($this->getValidator()->isUUID()->validate('{216fff40-98d9-11e3-a5e2-0800200c9a66}'));
569
        $this->assertFalse($this->getValidator()->isUUID()->validate('216fff4098d911e3a5e20800200c9a66'));
570
571
        $this->assertTrue($this->getValidator()->isUUID(false)->validate('{6ba7b810-9dad-11d1-80b4-00c04fd430c8}'));
572
        $this->assertTrue($this->getValidator()->isUUID(false)->validate('216f-ff40-98d9-11e3-a5e2-0800-200c-9a66'));
573
        $this->assertTrue($this->getValidator()->isUUID(false)->validate('{216fff40-98d9-11e3-a5e2-0800200c9a66}'));
574
        $this->assertTrue($this->getValidator()->isUUID(false)->validate('216fff4098d911e3a5e20800200c9a66'));
575
    }
576
}
577