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