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