|
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\Result; |
|
9
|
|
|
use Yiisoft\Validator\Rule\Callback; |
|
10
|
|
|
|
|
11
|
|
|
class CallbackTest_Object |
|
12
|
|
|
{ |
|
13
|
|
|
private function privateMethod($value): Result |
|
14
|
|
|
{ |
|
15
|
|
|
$result = new Result(); |
|
16
|
|
|
if ($value === 'test') { |
|
17
|
|
|
$result->addError('error message'); |
|
18
|
|
|
} |
|
19
|
|
|
return $result; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
private function protectedMethod($value): Result |
|
23
|
|
|
{ |
|
24
|
|
|
$result = new Result(); |
|
25
|
|
|
if ($value === 'test') { |
|
26
|
|
|
$result->addError('error message'); |
|
27
|
|
|
} |
|
28
|
|
|
return $result; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function publicMethod($value): Result |
|
32
|
|
|
{ |
|
33
|
|
|
$result = new Result(); |
|
34
|
|
|
if ($value === 'test') { |
|
35
|
|
|
$result->addError('error message'); |
|
36
|
|
|
} |
|
37
|
|
|
return $result; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public static function staticMethod($value): Result |
|
41
|
|
|
{ |
|
42
|
|
|
$result = new Result(); |
|
43
|
|
|
if ($value === 'test') { |
|
44
|
|
|
$result->addError('error message'); |
|
45
|
|
|
} |
|
46
|
|
|
return $result; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
private static function privateStaticMethod($value): Result |
|
50
|
|
|
{ |
|
51
|
|
|
$result = new Result(); |
|
52
|
|
|
if ($value === 'test') { |
|
53
|
|
|
$result->addError('error message'); |
|
54
|
|
|
} |
|
55
|
|
|
return $result; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
class CallbackTest extends TestCase |
|
60
|
|
|
{ |
|
61
|
|
|
public function testThrowExceptionInvalidArgument() |
|
62
|
|
|
{ |
|
63
|
|
|
foreach ([null, 'test', []] as $value) { |
|
64
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
65
|
|
|
new Callback($value); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testValidate(): void |
|
70
|
|
|
{ |
|
71
|
|
|
$rule = new Callback( |
|
72
|
|
|
static function ($value): Result { |
|
73
|
|
|
$result = new Result(); |
|
74
|
|
|
if ($value !== 42) { |
|
75
|
|
|
$result->addError('Value should be 42!'); |
|
76
|
|
|
} |
|
77
|
|
|
return $result; |
|
78
|
|
|
} |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
$result = $rule->validate(41); |
|
82
|
|
|
|
|
83
|
|
|
$this->assertFalse($result->isValid()); |
|
84
|
|
|
$this->assertCount(1, $result->getErrors()); |
|
85
|
|
|
$this->assertEquals('Value should be 42!', $result->getErrors()[0]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testValidatePrivateMethod() |
|
89
|
|
|
{ |
|
90
|
|
|
$rule = new Callback([new CallbackTest_Object(), 'privateMethod']); |
|
91
|
|
|
|
|
92
|
|
|
$result = $rule->validate('test'); |
|
93
|
|
|
$this->assertFalse($result->isValid()); |
|
94
|
|
|
$this->assertCount(1, $result->getErrors()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function testValidateProtectedMethod() |
|
98
|
|
|
{ |
|
99
|
|
|
$rule = new Callback([new CallbackTest_Object(), 'protectedMethod']); |
|
100
|
|
|
|
|
101
|
|
|
$result = $rule->validate('test'); |
|
102
|
|
|
$this->assertFalse($result->isValid()); |
|
103
|
|
|
$this->assertCount(1, $result->getErrors()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testValidatePublicMethod() |
|
107
|
|
|
{ |
|
108
|
|
|
$rule = new Callback([new CallbackTest_Object(), 'publicMethod']); |
|
109
|
|
|
|
|
110
|
|
|
$result = $rule->validate('test'); |
|
111
|
|
|
$this->assertFalse($result->isValid()); |
|
112
|
|
|
$this->assertCount(1, $result->getErrors()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function testThrowExceptionIfNoPassedObject() |
|
116
|
|
|
{ |
|
117
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
118
|
|
|
(new Callback([CallbackTest_Object::class, 'protectedMethod']))->validate(null); |
|
119
|
|
|
|
|
120
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
121
|
|
|
(new Callback([CallbackTest_Object::class, 'privateMethod']))->validate(null); |
|
122
|
|
|
|
|
123
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
124
|
|
|
(new Callback([CallbackTest_Object::class, 'publicMethod']))->validate(null); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function testValidatePublicStaticMethod() |
|
128
|
|
|
{ |
|
129
|
|
|
$rule = new Callback([new CallbackTest_Object(), 'staticMethod']); |
|
130
|
|
|
|
|
131
|
|
|
$result = $rule->validate('test'); |
|
132
|
|
|
$this->assertFalse($result->isValid()); |
|
133
|
|
|
$this->assertCount(1, $result->getErrors()); |
|
134
|
|
|
|
|
135
|
|
|
$rule = new Callback([CallbackTest_Object::class, 'staticMethod']); |
|
136
|
|
|
|
|
137
|
|
|
$result = $rule->validate('test'); |
|
138
|
|
|
$this->assertFalse($result->isValid()); |
|
139
|
|
|
$this->assertCount(1, $result->getErrors()); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function testValidatePrivateStaticMethod() |
|
143
|
|
|
{ |
|
144
|
|
|
$rule = new Callback([new CallbackTest_Object(), 'privateStaticMethod']); |
|
145
|
|
|
|
|
146
|
|
|
$result = $rule->validate('test'); |
|
147
|
|
|
$this->assertFalse($result->isValid()); |
|
148
|
|
|
$this->assertCount(1, $result->getErrors()); |
|
149
|
|
|
|
|
150
|
|
|
$rule = new Callback([CallbackTest_Object::class, 'privateStaticMethod']); |
|
151
|
|
|
|
|
152
|
|
|
$result = $rule->validate('test'); |
|
153
|
|
|
$this->assertFalse($result->isValid()); |
|
154
|
|
|
$this->assertCount(1, $result->getErrors()); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|