|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +use Illuminate\Validation\Rule; |
3 | 4 | use Astrotomic\Translatable\Locales; |
| 5 | +use Illuminate\Validation\Rules\RequiredIf; |
4 | 6 | use Astrotomic\Translatable\Validation\RuleFactory; |
5 | 7 |
|
6 | 8 | final class ValidationTest extends TestsBase |
@@ -259,6 +261,212 @@ public function test_it_throws_exception_with_undefined_locales() |
259 | 261 | ]); |
260 | 262 | } |
261 | 263 |
|
| 264 | + public function test_format_array_it_replaces_single_rule() |
| 265 | + { |
| 266 | + $rules = [ |
| 267 | + '%title%' => 'sometimes|string', |
| 268 | + '%content%' => 'required_with:%title%', |
| 269 | + ]; |
| 270 | + |
| 271 | + $this->assertEquals([ |
| 272 | + 'en.title' => 'sometimes|string', |
| 273 | + 'de.title' => 'sometimes|string', |
| 274 | + 'de-DE.title' => 'sometimes|string', |
| 275 | + 'de-AT.title' => 'sometimes|string', |
| 276 | + |
| 277 | + 'en.content' => 'required_with:en.title', |
| 278 | + 'de.content' => 'required_with:de.title', |
| 279 | + 'de-DE.content' => 'required_with:de-DE.title', |
| 280 | + 'de-AT.content' => 'required_with:de-AT.title', |
| 281 | + ], RuleFactory::make($rules, RuleFactory::FORMAT_ARRAY)); |
| 282 | + } |
| 283 | + |
| 284 | + public function test_format_array_it_replaces_imploded_rules() |
| 285 | + { |
| 286 | + $rules = [ |
| 287 | + '%title%' => 'sometimes|string', |
| 288 | + '%content%' => 'required_with:%title%|string', |
| 289 | + ]; |
| 290 | + |
| 291 | + $this->assertEquals([ |
| 292 | + 'en.title' => 'sometimes|string', |
| 293 | + 'de.title' => 'sometimes|string', |
| 294 | + 'de-DE.title' => 'sometimes|string', |
| 295 | + 'de-AT.title' => 'sometimes|string', |
| 296 | + |
| 297 | + 'en.content' => 'required_with:en.title|string', |
| 298 | + 'de.content' => 'required_with:de.title|string', |
| 299 | + 'de-DE.content' => 'required_with:de-DE.title|string', |
| 300 | + 'de-AT.content' => 'required_with:de-AT.title|string', |
| 301 | + ], RuleFactory::make($rules, RuleFactory::FORMAT_ARRAY)); |
| 302 | + } |
| 303 | + |
| 304 | + public function test_format_array_it_replaces_array_of_rules() |
| 305 | + { |
| 306 | + $rules = [ |
| 307 | + '%title%' => 'sometimes|string', |
| 308 | + '%content%' => ['required_with:%title%', 'string'], |
| 309 | + ]; |
| 310 | + |
| 311 | + $this->assertEquals([ |
| 312 | + 'en.title' => 'sometimes|string', |
| 313 | + 'de.title' => 'sometimes|string', |
| 314 | + 'de-DE.title' => 'sometimes|string', |
| 315 | + 'de-AT.title' => 'sometimes|string', |
| 316 | + |
| 317 | + 'en.content' => ['required_with:en.title', 'string'], |
| 318 | + 'de.content' => ['required_with:de.title', 'string'], |
| 319 | + 'de-DE.content' => ['required_with:de-DE.title', 'string'], |
| 320 | + 'de-AT.content' => ['required_with:de-AT.title', 'string'], |
| 321 | + ], RuleFactory::make($rules, RuleFactory::FORMAT_ARRAY)); |
| 322 | + } |
| 323 | + |
| 324 | + public function test_format_array_it_does_not_touch_non_string_rule() |
| 325 | + { |
| 326 | + $rules = [ |
| 327 | + 'title' => 'required', |
| 328 | + '%content%' => Rule::requiredIf(function () { |
| 329 | + return true; |
| 330 | + }), |
| 331 | + ]; |
| 332 | + |
| 333 | + $formattedRules = RuleFactory::make($rules, RuleFactory::FORMAT_ARRAY); |
| 334 | + |
| 335 | + $this->assertEquals('required', $formattedRules['title']); |
| 336 | + $this->assertInstanceOf(RequiredIf::class, $formattedRules['en.content']); |
| 337 | + $this->assertInstanceOf(RequiredIf::class, $formattedRules['de.content']); |
| 338 | + $this->assertInstanceOf(RequiredIf::class, $formattedRules['de-DE.content']); |
| 339 | + $this->assertInstanceOf(RequiredIf::class, $formattedRules['de-AT.content']); |
| 340 | + } |
| 341 | + |
| 342 | + public function test_format_array_it_does_not_touch_non_string_rule_in_array() |
| 343 | + { |
| 344 | + $rules = [ |
| 345 | + 'title' => 'required', |
| 346 | + '%content%' => [ |
| 347 | + 'required_with:%title%', |
| 348 | + Rule::requiredIf(function () { |
| 349 | + return true; |
| 350 | + }), |
| 351 | + ], |
| 352 | + ]; |
| 353 | + |
| 354 | + $formattedRules = RuleFactory::make($rules, RuleFactory::FORMAT_ARRAY); |
| 355 | + |
| 356 | + $this->assertEquals('required', $formattedRules['title']); |
| 357 | + $this->assertEquals('required_with:en.title', $formattedRules['en.content'][0]); |
| 358 | + $this->assertEquals('required_with:de.title', $formattedRules['de.content'][0]); |
| 359 | + $this->assertEquals('required_with:de-DE.title', $formattedRules['de-DE.content'][0]); |
| 360 | + $this->assertEquals('required_with:de-AT.title', $formattedRules['de-AT.content'][0]); |
| 361 | + $this->assertInstanceOf(RequiredIf::class, $formattedRules['en.content'][1]); |
| 362 | + $this->assertInstanceOf(RequiredIf::class, $formattedRules['de.content'][1]); |
| 363 | + $this->assertInstanceOf(RequiredIf::class, $formattedRules['de-DE.content'][1]); |
| 364 | + $this->assertInstanceOf(RequiredIf::class, $formattedRules['de-AT.content'][1]); |
| 365 | + } |
| 366 | + |
| 367 | + public function test_format_key_it_replaces_single_rule() |
| 368 | + { |
| 369 | + $rules = [ |
| 370 | + '%title%' => 'sometimes|string', |
| 371 | + '%content%' => 'required_with:"%title%"', |
| 372 | + ]; |
| 373 | + |
| 374 | + $this->assertEquals([ |
| 375 | + 'title:en' => 'sometimes|string', |
| 376 | + 'title:de' => 'sometimes|string', |
| 377 | + 'title:de-DE' => 'sometimes|string', |
| 378 | + 'title:de-AT' => 'sometimes|string', |
| 379 | + |
| 380 | + 'content:en' => 'required_with:"title:en"', |
| 381 | + 'content:de' => 'required_with:"title:de"', |
| 382 | + 'content:de-DE' => 'required_with:"title:de-DE"', |
| 383 | + 'content:de-AT' => 'required_with:"title:de-AT"', |
| 384 | + ], RuleFactory::make($rules, RuleFactory::FORMAT_KEY)); |
| 385 | + } |
| 386 | + |
| 387 | + public function test_format_key_it_replaces_imploded_rules() |
| 388 | + { |
| 389 | + $rules = [ |
| 390 | + '%title%' => 'sometimes|string', |
| 391 | + '%content%' => 'required_with:"%title%"|string', |
| 392 | + ]; |
| 393 | + |
| 394 | + $this->assertEquals([ |
| 395 | + 'title:en' => 'sometimes|string', |
| 396 | + 'title:de' => 'sometimes|string', |
| 397 | + 'title:de-DE' => 'sometimes|string', |
| 398 | + 'title:de-AT' => 'sometimes|string', |
| 399 | + |
| 400 | + 'content:en' => 'required_with:"title:en"|string', |
| 401 | + 'content:de' => 'required_with:"title:de"|string', |
| 402 | + 'content:de-DE' => 'required_with:"title:de-DE"|string', |
| 403 | + 'content:de-AT' => 'required_with:"title:de-AT"|string', |
| 404 | + ], RuleFactory::make($rules, RuleFactory::FORMAT_KEY)); |
| 405 | + } |
| 406 | + |
| 407 | + public function test_format_key_it_replaces_array_of_rules() |
| 408 | + { |
| 409 | + $rules = [ |
| 410 | + '%title%' => 'sometimes|string', |
| 411 | + '%content%' => ['required_with:"%title%"', 'string'], |
| 412 | + ]; |
| 413 | + |
| 414 | + $this->assertEquals([ |
| 415 | + 'title:en' => 'sometimes|string', |
| 416 | + 'title:de' => 'sometimes|string', |
| 417 | + 'title:de-DE' => 'sometimes|string', |
| 418 | + 'title:de-AT' => 'sometimes|string', |
| 419 | + |
| 420 | + 'content:en' => ['required_with:"title:en"', 'string'], |
| 421 | + 'content:de' => ['required_with:"title:de"', 'string'], |
| 422 | + 'content:de-DE' => ['required_with:"title:de-DE"', 'string'], |
| 423 | + 'content:de-AT' => ['required_with:"title:de-AT"', 'string'], |
| 424 | + ], RuleFactory::make($rules, RuleFactory::FORMAT_KEY)); |
| 425 | + } |
| 426 | + |
| 427 | + public function test_format_key_it_does_not_touch_non_string_rule() |
| 428 | + { |
| 429 | + $rules = [ |
| 430 | + 'title' => 'required', |
| 431 | + '%content%' => Rule::requiredIf(function () { |
| 432 | + return true; |
| 433 | + }), |
| 434 | + ]; |
| 435 | + |
| 436 | + $formattedRules = RuleFactory::make($rules, RuleFactory::FORMAT_KEY); |
| 437 | + |
| 438 | + $this->assertEquals('required', $formattedRules['title']); |
| 439 | + $this->assertInstanceOf(RequiredIf::class, $formattedRules['content:en']); |
| 440 | + $this->assertInstanceOf(RequiredIf::class, $formattedRules['content:de']); |
| 441 | + $this->assertInstanceOf(RequiredIf::class, $formattedRules['content:de-DE']); |
| 442 | + $this->assertInstanceOf(RequiredIf::class, $formattedRules['content:de-AT']); |
| 443 | + } |
| 444 | + |
| 445 | + public function test_format_key_it_does_not_touch_non_string_rule_in_array() |
| 446 | + { |
| 447 | + $rules = [ |
| 448 | + 'title' => 'required', |
| 449 | + '%content%' => [ |
| 450 | + 'required_with:"%title%"', |
| 451 | + Rule::requiredIf(function () { |
| 452 | + return true; |
| 453 | + }), |
| 454 | + ], |
| 455 | + ]; |
| 456 | + |
| 457 | + $formattedRules = RuleFactory::make($rules, RuleFactory::FORMAT_KEY); |
| 458 | + |
| 459 | + $this->assertEquals('required', $formattedRules['title']); |
| 460 | + $this->assertEquals('required_with:"title:en"', $formattedRules['content:en'][0]); |
| 461 | + $this->assertEquals('required_with:"title:de"', $formattedRules['content:de'][0]); |
| 462 | + $this->assertEquals('required_with:"title:de-DE"', $formattedRules['content:de-DE'][0]); |
| 463 | + $this->assertEquals('required_with:"title:de-AT"', $formattedRules['content:de-AT'][0]); |
| 464 | + $this->assertInstanceOf(RequiredIf::class, $formattedRules['content:en'][1]); |
| 465 | + $this->assertInstanceOf(RequiredIf::class, $formattedRules['content:de'][1]); |
| 466 | + $this->assertInstanceOf(RequiredIf::class, $formattedRules['content:de-DE'][1]); |
| 467 | + $this->assertInstanceOf(RequiredIf::class, $formattedRules['content:de-AT'][1]); |
| 468 | + } |
| 469 | + |
262 | 470 | protected function setUp(): void |
263 | 471 | { |
264 | 472 | $this->setUpTheTestEnvironment(); |
|
0 commit comments