Passed
Push — master ( 660106...a2ed0f )
by Alexander
01:37
created

HasLengthTest::testValidateMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 12
rs 9.9666
c 1
b 0
f 0
1
<?php
2
3
namespace Yiisoft\Validator\Tests\Rule;
4
5
use PHPUnit\Framework\TestCase;
6
use Yiisoft\Validator\Rule\HasLength;
7
8
/**
9
 * @group validators
10
 */
11
class HasLengthTest extends TestCase
12
{
13
    public function testValidate(): void
14
    {
15
        $val = new HasLength();
16
        $this->assertFalse($val->validate(['not a string'])->isValid());
17
        $this->assertFalse($val->validate(new \stdClass())->isValid());
18
        $this->assertTrue($val->validate('Just some string')->isValid());
19
        $this->assertFalse($val->validate(true)->isValid());
20
        $this->assertFalse($val->validate(false)->isValid());
21
    }
22
23
    public function testValidateLength(): void
24
    {
25
        $val = (new HasLength())
26
            ->min(25)
27
            ->max(25);
28
        $this->assertTrue($val->validate(str_repeat('x', 25))->isValid());
29
        $this->assertTrue($val->validate(str_repeat('€', 25))->isValid());
30
        $this->assertFalse($val->validate(str_repeat('x', 125))->isValid());
31
        $this->assertFalse($val->validate('')->isValid());
32
33
        $val = (new HasLength())
34
            ->min(25);
35
        $this->assertTrue($val->validate(str_repeat('x', 125))->isValid());
36
        $this->assertTrue($val->validate(str_repeat('€', 25))->isValid());
37
        $this->assertFalse($val->validate(str_repeat('x', 13))->isValid());
38
        $this->assertFalse($val->validate('')->isValid());
39
40
        $val = (new HasLength())
41
            ->max(25);
42
        $this->assertTrue($val->validate(str_repeat('x', 25))->isValid());
43
        $this->assertTrue($val->validate(str_repeat('Ä', 24))->isValid());
44
        $this->assertfalse($val->validate(str_repeat('x', 1250))->isValid());
45
        $this->assertTrue($val->validate('')->isValid());
46
47
        $val = (new HasLength())
48
            ->min(10)
49
            ->max(25);
50
        $this->assertTrue($val->validate(str_repeat('x', 15))->isValid());
51
        $this->assertTrue($val->validate(str_repeat('x', 10))->isValid());
52
        $this->assertTrue($val->validate(str_repeat('x', 20))->isValid());
53
        $this->assertTrue($val->validate(str_repeat('x', 25))->isValid());
54
        $this->assertFalse($val->validate(str_repeat('x', 5))->isValid());
55
        $this->assertFalse($val->validate('')->isValid());
56
    }
57
58
    public function testValidateMin(): void
59
    {
60
        $rule = (new HasLength())
61
            ->min(1);
62
63
        $result = $rule->validate('');
64
        $this->assertFalse($result->isValid());
65
        $this->assertStringContainsString('{attribute} should contain at least {min, number} {min, plural, one{character} other{characters}}.', $result->getErrors()[0]);
66
        $this->assertTrue($rule->validate(str_repeat('x', 5))->isValid());
67
    }
68
69
70
    public function testValidateMax(): void
71
    {
72
        $rule = (new HasLength())
73
            ->max(100);
74
75
        $this->assertTrue($rule->validate(str_repeat('x', 5))->isValid());
76
77
        $result = $rule->validate(str_repeat('x', 1230));
78
        $this->assertFalse($result->isValid());
79
        $this->assertStringContainsString('{attribute} should contain at most {max, number} {max, plural, one{character} other{characters}}.', $result->getErrors()[0]);
80
    }
81
82
    public function testValidateMessages()
83
    {
84
        $rule = (new HasLength())
85
            ->message('is not string error')
86
            ->tooShortMessage('is to short test')
87
            ->tooLongMessage('is to long test')
88
            ->min(3)
89
            ->max(5);
90
91
        $this->assertEquals('is not string error', $rule->validate(null)->getErrors()[0]);
92
        $this->assertEquals('is to short test', $rule->validate(str_repeat('x', 1))->getErrors()[0]);
93
        $this->assertEquals('is to long test', $rule->validate(str_repeat('x', 6))->getErrors()[0]);
94
    }
95
}
96