|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Validation; |
| 4 | + |
| 5 | +use Illuminate\Translation\ArrayLoader; |
| 6 | +use Illuminate\Translation\Translator; |
| 7 | +use Illuminate\Validation\Rule; |
| 8 | +use Illuminate\Validation\Validator; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +include_once 'Enums.php'; |
| 12 | + |
| 13 | +class ValidationRuleContainsTest extends TestCase |
| 14 | +{ |
| 15 | + public function testItCorrectlyFormatsAStringVersionOfTheRule() |
| 16 | + { |
| 17 | + $rule = Rule::contains('Taylor'); |
| 18 | + $this->assertSame('contains:"Taylor"', (string) $rule); |
| 19 | + |
| 20 | + $rule = Rule::contains('Taylor', 'Abigail'); |
| 21 | + $this->assertSame('contains:"Taylor","Abigail"', (string) $rule); |
| 22 | + |
| 23 | + $rule = Rule::contains(['Taylor', 'Abigail']); |
| 24 | + $this->assertSame('contains:"Taylor","Abigail"', (string) $rule); |
| 25 | + |
| 26 | + $rule = Rule::contains(collect(['Taylor', 'Abigail'])); |
| 27 | + $this->assertSame('contains:"Taylor","Abigail"', (string) $rule); |
| 28 | + |
| 29 | + $rule = Rule::contains([ArrayKeys::key_1, ArrayKeys::key_2]); |
| 30 | + $this->assertSame('contains:"key_1","key_2"', (string) $rule); |
| 31 | + |
| 32 | + $rule = Rule::contains([ArrayKeysBacked::key_1, ArrayKeysBacked::key_2]); |
| 33 | + $this->assertSame('contains:"key_1","key_2"', (string) $rule); |
| 34 | + |
| 35 | + $rule = Rule::contains(['Taylor', 'Taylor']); |
| 36 | + $this->assertSame('contains:"Taylor","Taylor"', (string) $rule); |
| 37 | + |
| 38 | + $rule = Rule::contains([1, 2, 3]); |
| 39 | + $this->assertSame('contains:"1","2","3"', (string) $rule); |
| 40 | + |
| 41 | + $rule = Rule::contains(['"foo"', '"bar"', '"baz"']); |
| 42 | + $this->assertSame('contains:"""foo""","""bar""","""baz"""', (string) $rule); |
| 43 | + } |
| 44 | + |
| 45 | + public function testContainsValidation() |
| 46 | + { |
| 47 | + $trans = new Translator(new ArrayLoader, 'en'); |
| 48 | + |
| 49 | + // Test fails when value is string |
| 50 | + $v = new Validator($trans, ['roles' => 'admin'], ['roles' => Rule::contains('editor')]); |
| 51 | + $this->assertTrue($v->fails()); |
| 52 | + |
| 53 | + // Test passes when array contains the value |
| 54 | + $v = new Validator($trans, ['roles' => ['admin', 'user']], ['roles' => Rule::contains('admin')]); |
| 55 | + $this->assertTrue($v->passes()); |
| 56 | + |
| 57 | + // Test fails when array doesn't contain all the values |
| 58 | + $v = new Validator($trans, ['roles' => ['admin', 'user']], ['roles' => Rule::contains(['admin', 'editor'])]); |
| 59 | + $this->assertTrue($v->fails()); |
| 60 | + |
| 61 | + // Test fails when array doesn't contain all the values (using multiple arguments) |
| 62 | + $v = new Validator($trans, ['roles' => ['admin', 'user']], ['roles' => Rule::contains('admin', 'editor')]); |
| 63 | + $this->assertTrue($v->fails()); |
| 64 | + |
| 65 | + // Test passes when array contains all the values |
| 66 | + $v = new Validator($trans, ['roles' => ['admin', 'user', 'editor']], ['roles' => Rule::contains(['admin', 'editor'])]); |
| 67 | + $this->assertTrue($v->passes()); |
| 68 | + |
| 69 | + // Test passes when array contains all the values (using multiple arguments) |
| 70 | + $v = new Validator($trans, ['roles' => ['admin', 'user', 'editor']], ['roles' => Rule::contains('admin', 'editor')]); |
| 71 | + $this->assertTrue($v->passes()); |
| 72 | + |
| 73 | + // Test fails when array doesn't contain the value |
| 74 | + $v = new Validator($trans, ['roles' => ['admin', 'user']], ['roles' => Rule::contains('editor')]); |
| 75 | + $this->assertTrue($v->fails()); |
| 76 | + |
| 77 | + // Test fails when array doesn't contain any of the values |
| 78 | + $v = new Validator($trans, ['roles' => ['admin', 'user']], ['roles' => Rule::contains(['editor', 'manager'])]); |
| 79 | + $this->assertTrue($v->fails()); |
| 80 | + |
| 81 | + // Test with empty array |
| 82 | + $v = new Validator($trans, ['roles' => []], ['roles' => Rule::contains('admin')]); |
| 83 | + $this->assertTrue($v->fails()); |
| 84 | + |
| 85 | + // Test with nullable field |
| 86 | + $v = new Validator($trans, ['roles' => null], ['roles' => ['nullable', Rule::contains('admin')]]); |
| 87 | + $this->assertTrue($v->passes()); |
| 88 | + } |
| 89 | +} |
0 commit comments