|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Validator\Tests\Constraints; |
| 13 | + |
| 14 | +use Symfony\Component\Validator\Constraints\AtLeastOneOf; |
| 15 | +use Symfony\Component\Validator\Constraints\AtLeastOneOfValidator; |
| 16 | +use Symfony\Component\Validator\Constraints\Choice; |
| 17 | +use Symfony\Component\Validator\Constraints\Count; |
| 18 | +use Symfony\Component\Validator\Constraints\Country; |
| 19 | +use Symfony\Component\Validator\Constraints\DivisibleBy; |
| 20 | +use Symfony\Component\Validator\Constraints\EqualTo; |
| 21 | +use Symfony\Component\Validator\Constraints\GreaterThanOrEqual; |
| 22 | +use Symfony\Component\Validator\Constraints\IdenticalTo; |
| 23 | +use Symfony\Component\Validator\Constraints\Language; |
| 24 | +use Symfony\Component\Validator\Constraints\Length; |
| 25 | +use Symfony\Component\Validator\Constraints\LessThan; |
| 26 | +use Symfony\Component\Validator\Constraints\Negative; |
| 27 | +use Symfony\Component\Validator\Constraints\Range; |
| 28 | +use Symfony\Component\Validator\Constraints\Regex; |
| 29 | +use Symfony\Component\Validator\Constraints\Unique; |
| 30 | +use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
| 31 | + |
| 32 | +/** |
| 33 | + * @author Przemysław Bogusz <[email protected]> |
| 34 | + */ |
| 35 | +class AtLeastOneOfValidatorTest extends ConstraintValidatorTestCase |
| 36 | +{ |
| 37 | + protected function createValidator() |
| 38 | + { |
| 39 | + return new AtLeastOneOfValidator(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @dataProvider getValidCombinations |
| 44 | + */ |
| 45 | + public function testValidCombinations($value, $constraints) |
| 46 | + { |
| 47 | + $i = 0; |
| 48 | + |
| 49 | + foreach ($constraints as $constraint) { |
| 50 | + $this->expectViolationsAt($i++, $value, $constraint); |
| 51 | + } |
| 52 | + |
| 53 | + $this->validator->validate($value, new AtLeastOneOf($constraints)); |
| 54 | + |
| 55 | + $this->assertNoViolation(); |
| 56 | + } |
| 57 | + |
| 58 | + public function getValidCombinations() |
| 59 | + { |
| 60 | + return [ |
| 61 | + ['symfony', [ |
| 62 | + new Length(['min' => 10]), |
| 63 | + new EqualTo(['value' => 'symfony']), |
| 64 | + ]], |
| 65 | + [150, [ |
| 66 | + new Range(['min' => 10, 'max' => 20]), |
| 67 | + new GreaterThanOrEqual(['value' => 100]), |
| 68 | + ]], |
| 69 | + [7, [ |
| 70 | + new LessThan(['value' => 5]), |
| 71 | + new IdenticalTo(['value' => 7]), |
| 72 | + ]], |
| 73 | + [-3, [ |
| 74 | + new DivisibleBy(['value' => 4]), |
| 75 | + new Negative(), |
| 76 | + ]], |
| 77 | + ['FOO', [ |
| 78 | + new Choice(['choices' => ['bar', 'BAR']]), |
| 79 | + new Regex(['pattern' => '/foo/i']), |
| 80 | + ]], |
| 81 | + ['fr', [ |
| 82 | + new Country(), |
| 83 | + new Language(), |
| 84 | + ]], |
| 85 | + [[1, 3, 5], [ |
| 86 | + new Count(['min' => 5]), |
| 87 | + new Unique(), |
| 88 | + ]], |
| 89 | + ]; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @dataProvider getInvalidCombinations |
| 94 | + */ |
| 95 | + public function testInvalidCombinationsWithDefaultMessage($value, $constraints) |
| 96 | + { |
| 97 | + $atLeastOneOf = new AtLeastOneOf(['constraints' => $constraints]); |
| 98 | + |
| 99 | + $message = [$atLeastOneOf->message]; |
| 100 | + |
| 101 | + $i = 0; |
| 102 | + |
| 103 | + foreach ($constraints as $constraint) { |
| 104 | + $message[] = ' ['.($i + 1).'] '.$this->expectViolationsAt($i++, $value, $constraint)->get(0)->getMessage(); |
| 105 | + } |
| 106 | + |
| 107 | + $this->validator->validate($value, $atLeastOneOf); |
| 108 | + |
| 109 | + $this->buildViolation(implode('', $message))->setCode(AtLeastOneOf::AT_LEAST_ONE_ERROR)->assertRaised(); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @dataProvider getInvalidCombinations |
| 114 | + */ |
| 115 | + public function testInvalidCombinationsWithCustomMessage($value, $constraints) |
| 116 | + { |
| 117 | + $atLeastOneOf = new AtLeastOneOf(['constraints' => $constraints, 'message' => 'foo', 'includeInternalMessages' => false]); |
| 118 | + |
| 119 | + $i = 0; |
| 120 | + |
| 121 | + foreach ($constraints as $constraint) { |
| 122 | + $this->expectViolationsAt($i++, $value, $constraint); |
| 123 | + } |
| 124 | + |
| 125 | + $this->validator->validate($value, $atLeastOneOf); |
| 126 | + |
| 127 | + $this->buildViolation('foo')->setCode(AtLeastOneOf::AT_LEAST_ONE_ERROR)->assertRaised(); |
| 128 | + } |
| 129 | + |
| 130 | + public function getInvalidCombinations() |
| 131 | + { |
| 132 | + return [ |
| 133 | + ['symphony', [ |
| 134 | + new Length(['min' => 10]), |
| 135 | + new EqualTo(['value' => 'symfony']), |
| 136 | + ]], |
| 137 | + [70, [ |
| 138 | + new Range(['min' => 10, 'max' => 20]), |
| 139 | + new GreaterThanOrEqual(['value' => 100]), |
| 140 | + ]], |
| 141 | + [8, [ |
| 142 | + new LessThan(['value' => 5]), |
| 143 | + new IdenticalTo(['value' => 7]), |
| 144 | + ]], |
| 145 | + [3, [ |
| 146 | + new DivisibleBy(['value' => 4]), |
| 147 | + new Negative(), |
| 148 | + ]], |
| 149 | + ['F_O_O', [ |
| 150 | + new Choice(['choices' => ['bar', 'BAR']]), |
| 151 | + new Regex(['pattern' => '/foo/i']), |
| 152 | + ]], |
| 153 | + ['f_r', [ |
| 154 | + new Country(), |
| 155 | + new Language(), |
| 156 | + ]], |
| 157 | + [[1, 3, 3], [ |
| 158 | + new Count(['min' => 5]), |
| 159 | + new Unique(), |
| 160 | + ]], |
| 161 | + ]; |
| 162 | + } |
| 163 | +} |
0 commit comments