|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ValueValidators\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use ValueValidators\Error; |
|
7
|
|
|
use ValueValidators\StringValidator; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @covers ValueValidators\StringValidator |
|
11
|
|
|
* |
|
12
|
|
|
* @group ValueValidators |
|
13
|
|
|
* @group DataValueExtensions |
|
14
|
|
|
* |
|
15
|
|
|
* @license GPL-2.0-or-later |
|
16
|
|
|
* @author Thiemo Kreuz |
|
17
|
|
|
*/ |
|
18
|
|
|
class StringValidatorTest extends TestCase { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @dataProvider stringProvider |
|
22
|
|
|
*/ |
|
23
|
|
|
public function testValidate( $value, array $options, $expectedError ) { |
|
24
|
|
|
$validator = new StringValidator(); |
|
25
|
|
|
$validator->setOptions( $options ); |
|
26
|
|
|
$result = $validator->validate( $value ); |
|
27
|
|
|
|
|
28
|
|
|
$this->assertEquals( |
|
29
|
|
|
$expectedError === null ? [] : [ $expectedError ], |
|
30
|
|
|
$result->getErrors() |
|
31
|
|
|
); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function stringProvider() { |
|
35
|
|
|
return [ |
|
36
|
|
|
[ |
|
37
|
|
|
'value' => null, |
|
38
|
|
|
'options' => [], |
|
39
|
|
|
'expectedErrors' => Error::newError( 'Not a string' ) |
|
40
|
|
|
], |
|
41
|
|
|
[ |
|
42
|
|
|
'value' => '', |
|
43
|
|
|
'options' => [], |
|
44
|
|
|
'expectedErrors' => null |
|
45
|
|
|
], |
|
46
|
|
|
[ |
|
47
|
|
|
'value' => '', |
|
48
|
|
|
'options' => [ 'length' => 1 ], |
|
49
|
|
|
'expectedErrors' => Error::newError( 'Value exceeding lower bound', 'length' ) |
|
50
|
|
|
], |
|
51
|
|
|
[ |
|
52
|
|
|
'value' => '1', |
|
53
|
|
|
'options' => [ 'length' => 1 ], |
|
54
|
|
|
'expectedErrors' => null |
|
55
|
|
|
], |
|
56
|
|
|
[ |
|
57
|
|
|
'value' => '1', |
|
58
|
|
|
'options' => [ 'length' => 0 ], |
|
59
|
|
|
'expectedErrors' => Error::newError( 'Value exceeding upper bound', 'length' ) |
|
60
|
|
|
], |
|
61
|
|
|
[ |
|
62
|
|
|
'value' => '', |
|
63
|
|
|
'options' => [ 'length' => 0 ], |
|
64
|
|
|
'expectedErrors' => null |
|
65
|
|
|
], |
|
66
|
|
|
[ |
|
67
|
|
|
'value' => '', |
|
68
|
|
|
'options' => [ 'minlength' => 1 ], |
|
69
|
|
|
'expectedErrors' => Error::newError( 'Value exceeding lower bound', 'length' ) |
|
70
|
|
|
], |
|
71
|
|
|
[ |
|
72
|
|
|
'value' => '1', |
|
73
|
|
|
'options' => [ 'minlength' => 1 ], |
|
74
|
|
|
'expectedErrors' => null |
|
75
|
|
|
], |
|
76
|
|
|
[ |
|
77
|
|
|
'value' => '1', |
|
78
|
|
|
'options' => [ 'maxlength' => 0 ], |
|
79
|
|
|
'expectedErrors' => Error::newError( 'Value exceeding upper bound', 'length' ) |
|
80
|
|
|
], |
|
81
|
|
|
[ |
|
82
|
|
|
'value' => '', |
|
83
|
|
|
'options' => [ 'maxlength' => 0 ], |
|
84
|
|
|
'expectedErrors' => null |
|
85
|
|
|
], |
|
86
|
|
|
[ |
|
87
|
|
|
'value' => '1', |
|
88
|
|
|
'options' => [ 'regex' => '/^$/' ], |
|
89
|
|
|
'expectedErrors' => Error::newError( 'String does not match the regular expression /^$/' ) |
|
90
|
|
|
], |
|
91
|
|
|
[ |
|
92
|
|
|
'value' => '', |
|
93
|
|
|
'options' => [ 'regex' => '/^$/' ], |
|
94
|
|
|
'expectedErrors' => null |
|
95
|
|
|
], |
|
96
|
|
|
]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|