|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Validation; |
| 4 | + |
| 5 | +use Illuminate\Translation\ArrayLoader; |
| 6 | +use Illuminate\Translation\Translator; |
| 7 | +use Illuminate\Validation\Validator; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +class ValidationInArrayKeysTest extends TestCase |
| 11 | +{ |
| 12 | + public function testInArrayKeysValidation() |
| 13 | + { |
| 14 | + $trans = $this->getIlluminateArrayTranslator(); |
| 15 | + |
| 16 | + // Test passes when array has at least one of the specified keys |
| 17 | + $v = new Validator($trans, ['foo' => ['first_key' => 'bar', 'second_key' => 'baz']], ['foo' => 'in_array_keys:first_key,third_key']); |
| 18 | + $this->assertTrue($v->passes()); |
| 19 | + |
| 20 | + // Test passes when array has multiple of the specified keys |
| 21 | + $v = new Validator($trans, ['foo' => ['first_key' => 'bar', 'second_key' => 'baz']], ['foo' => 'in_array_keys:first_key,second_key']); |
| 22 | + $this->assertTrue($v->passes()); |
| 23 | + |
| 24 | + // Test fails when array doesn't have any of the specified keys |
| 25 | + $v = new Validator($trans, ['foo' => ['first_key' => 'bar', 'second_key' => 'baz']], ['foo' => 'in_array_keys:third_key,fourth_key']); |
| 26 | + $this->assertTrue($v->fails()); |
| 27 | + |
| 28 | + // Test fails when value is not an array |
| 29 | + $v = new Validator($trans, ['foo' => 'not-an-array'], ['foo' => 'in_array_keys:first_key']); |
| 30 | + $this->assertTrue($v->fails()); |
| 31 | + |
| 32 | + // Test fails when no keys are specified |
| 33 | + $v = new Validator($trans, ['foo' => ['first_key' => 'bar']], ['foo' => 'in_array_keys:']); |
| 34 | + $this->assertTrue($v->fails()); |
| 35 | + } |
| 36 | + |
| 37 | + public function testInArrayKeysValidationWithNestedArrays() |
| 38 | + { |
| 39 | + $trans = $this->getIlluminateArrayTranslator(); |
| 40 | + |
| 41 | + // Test passes with nested arrays |
| 42 | + $v = new Validator($trans, [ |
| 43 | + 'foo' => [ |
| 44 | + 'first_key' => ['nested' => 'value'], |
| 45 | + 'second_key' => 'baz', |
| 46 | + ], |
| 47 | + ], ['foo' => 'in_array_keys:first_key,third_key']); |
| 48 | + $this->assertTrue($v->passes()); |
| 49 | + |
| 50 | + // Test with dot notation for nested arrays |
| 51 | + $v = new Validator($trans, [ |
| 52 | + 'foo' => [ |
| 53 | + 'first' => [ |
| 54 | + 'nested_key' => 'value', |
| 55 | + ], |
| 56 | + ], |
| 57 | + ], ['foo.first' => 'in_array_keys:nested_key']); |
| 58 | + $this->assertTrue($v->passes()); |
| 59 | + } |
| 60 | + |
| 61 | + public function testInArrayKeysValidationErrorMessage() |
| 62 | + { |
| 63 | + $trans = $this->getIlluminateArrayTranslator(); |
| 64 | + $trans->addLines([ |
| 65 | + 'validation.in_array_keys' => 'The :attribute field must contain at least one of the following keys: :values.', |
| 66 | + ], 'en'); |
| 67 | + |
| 68 | + $v = new Validator($trans, ['foo' => ['wrong_key' => 'bar']], ['foo' => 'in_array_keys:first_key,second_key']); |
| 69 | + $this->assertFalse($v->passes()); |
| 70 | + $this->assertEquals( |
| 71 | + 'The foo field must contain at least one of the following keys: first_key, second_key.', |
| 72 | + $v->messages()->first('foo') |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + protected function getIlluminateArrayTranslator() |
| 77 | + { |
| 78 | + return new Translator(new ArrayLoader, 'en'); |
| 79 | + } |
| 80 | +} |
0 commit comments