1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Telkins\Validation\Tests; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Validator; |
6
|
|
|
use Telkins\Validation\Tests\TestRuleSets\PostRuleSet; |
7
|
|
|
|
8
|
|
|
class ResourceRuleSetTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
protected function getError($key, $attribute, $extra = []) : string |
11
|
|
|
{ |
12
|
|
|
return __($key, array_merge(['attribute' => str_replace('_', ' ', $attribute)], $extra)); |
|
|
|
|
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @test |
17
|
|
|
*/ |
18
|
|
|
public function it_returns_the_expected_rules() |
19
|
|
|
{ |
20
|
|
|
$rules = (new PostRuleSet())->rules(); |
21
|
|
|
|
22
|
|
|
$this->assertIsArray($rules); |
23
|
|
|
$this->assertEquals($rules, [ |
24
|
|
|
'subject' => [ |
25
|
|
|
'string', |
26
|
|
|
'max:255', |
27
|
|
|
], |
28
|
|
|
'body' => [ |
29
|
|
|
'string', |
30
|
|
|
'max:1024', |
31
|
|
|
], |
32
|
|
|
]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @test |
37
|
|
|
*/ |
38
|
|
|
public function it_returns_the_expected_creation_rules() |
39
|
|
|
{ |
40
|
|
|
$rules = (new PostRuleSet())->creationRules(); |
41
|
|
|
|
42
|
|
|
$this->assertIsArray($rules); |
43
|
|
|
$this->assertEquals($rules, [ |
44
|
|
|
'author_id' => [ |
45
|
|
|
'required', |
46
|
|
|
], |
47
|
|
|
'subject' => [ |
48
|
|
|
'string', |
49
|
|
|
'max:255', |
50
|
|
|
'required', |
51
|
|
|
], |
52
|
|
|
'body' => [ |
53
|
|
|
'string', |
54
|
|
|
'max:1024', |
55
|
|
|
], |
56
|
|
|
]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @test |
61
|
|
|
*/ |
62
|
|
|
public function it_returns_the_expected_update_rules() |
63
|
|
|
{ |
64
|
|
|
$rules = (new PostRuleSet())->updateRules(); |
65
|
|
|
|
66
|
|
|
$this->assertIsArray($rules); |
67
|
|
|
$this->assertEquals($rules, [ |
68
|
|
|
'subject' => [ |
69
|
|
|
'string', |
70
|
|
|
'max:255', |
71
|
|
|
], |
72
|
|
|
'body' => [ |
73
|
|
|
'string', |
74
|
|
|
'max:1024', |
75
|
|
|
], |
76
|
|
|
'reason' => [ |
77
|
|
|
'required', |
78
|
|
|
'string', |
79
|
|
|
'max:255', |
80
|
|
|
], |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @test |
86
|
|
|
*/ |
87
|
|
|
public function it_validates_valid_creation_data() |
88
|
|
|
{ |
89
|
|
|
$requestData = [ |
90
|
|
|
'author_id' => 1234, |
91
|
|
|
'subject' => 'My First Post', |
92
|
|
|
'body' => 'This is my first post.', |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
$validator = Validator::make($requestData, (new PostRuleSet())->creationRules()); |
96
|
|
|
|
97
|
|
|
$this->assertTrue($validator->passes()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @test |
102
|
|
|
* @dataProvider providesInvalidCreationData |
103
|
|
|
*/ |
104
|
|
|
public function it_invalidates_invalid_creation_data($requestData, $errorKey, $errorAttribute, $extra = []) |
105
|
|
|
{ |
106
|
|
|
$validator = Validator::make($requestData, (new PostRuleSet())->creationRules()); |
107
|
|
|
|
108
|
|
|
$this->assertFalse($validator->passes()); |
109
|
|
|
$this->assertEquals($this->getError($errorKey, $errorAttribute, $extra), $validator->errors()->first()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function providesInvalidCreationData() |
113
|
|
|
{ |
114
|
|
|
return [ |
115
|
|
|
[ |
116
|
|
|
[ |
117
|
|
|
// 'author_id' => 1234, |
118
|
|
|
'subject' => 'My First Post', |
119
|
|
|
'body' => 'This is my first post.', |
120
|
|
|
], |
121
|
|
|
'validation.required', |
122
|
|
|
'author_id', |
123
|
|
|
], |
124
|
|
|
[ |
125
|
|
|
[ |
126
|
|
|
'author_id' => 1234, |
127
|
|
|
// 'subject' => 'My First Post', |
128
|
|
|
'body' => 'This is my first post.', |
129
|
|
|
], |
130
|
|
|
'validation.required', |
131
|
|
|
'subject', |
132
|
|
|
], |
133
|
|
|
[ |
134
|
|
|
[ |
135
|
|
|
'author_id' => 1234, |
136
|
|
|
'subject' => str_repeat('x', 256), |
137
|
|
|
'body' => 'This is my first post.', |
138
|
|
|
], |
139
|
|
|
'validation.max.string', |
140
|
|
|
'subject', |
141
|
|
|
['max' => 255], |
142
|
|
|
], |
143
|
|
|
[ |
144
|
|
|
[ |
145
|
|
|
'author_id' => 1234, |
146
|
|
|
'subject' => 'My First Post', |
147
|
|
|
'body' => str_repeat('x', 1025), |
148
|
|
|
], |
149
|
|
|
'validation.max.string', |
150
|
|
|
'body', |
151
|
|
|
['max' => 1024], |
152
|
|
|
], |
153
|
|
|
]; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @test |
158
|
|
|
*/ |
159
|
|
|
public function it_validates_valid_update_data() |
160
|
|
|
{ |
161
|
|
|
$requestData = [ |
162
|
|
|
'subject' => 'My Corrected Subject', |
163
|
|
|
'reason' => 'The subject was incorrect.', |
164
|
|
|
]; |
165
|
|
|
|
166
|
|
|
$validator = Validator::make($requestData, (new PostRuleSet())->updateRules()); |
167
|
|
|
|
168
|
|
|
$this->assertTrue($validator->passes()); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @test |
173
|
|
|
* @dataProvider providesInvalidUpdateData |
174
|
|
|
*/ |
175
|
|
|
public function it_invalidates_invalid_update_data($requestData, $errorKey, $errorAttribute, $extra = []) |
176
|
|
|
{ |
177
|
|
|
$validator = Validator::make($requestData, (new PostRuleSet())->updateRules()); |
178
|
|
|
|
179
|
|
|
$this->assertFalse($validator->passes()); |
180
|
|
|
$this->assertEquals($this->getError($errorKey, $errorAttribute, $extra), $validator->errors()->first()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function providesInvalidUpdateData() |
184
|
|
|
{ |
185
|
|
|
return [ |
186
|
|
|
[ |
187
|
|
|
[ |
188
|
|
|
'subject' => str_repeat('x', 256), |
189
|
|
|
'reason' => 'I wanted to make a very long subject.', |
190
|
|
|
], |
191
|
|
|
'validation.max.string', |
192
|
|
|
'subject', |
193
|
|
|
['max' => 255], |
194
|
|
|
], |
195
|
|
|
[ |
196
|
|
|
[ |
197
|
|
|
'body' => str_repeat('x', 1025), |
198
|
|
|
'reason' => 'I wanted to make a very long post.', |
199
|
|
|
], |
200
|
|
|
'validation.max.string', |
201
|
|
|
'body', |
202
|
|
|
['max' => 1024], |
203
|
|
|
], |
204
|
|
|
[ |
205
|
|
|
[ |
206
|
|
|
'subject' => 'My Corrected Subject', |
207
|
|
|
'body' => 'My corrected post.', |
208
|
|
|
// 'reason' => 'This is my first post.', |
209
|
|
|
], |
210
|
|
|
'validation.required', |
211
|
|
|
'reason', |
212
|
|
|
], |
213
|
|
|
[ |
214
|
|
|
[ |
215
|
|
|
'reason' => str_repeat('x', 256), |
216
|
|
|
], |
217
|
|
|
'validation.max.string', |
218
|
|
|
'reason', |
219
|
|
|
['max' => 255], |
220
|
|
|
], |
221
|
|
|
]; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|