|
| 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\Unique; |
| 15 | +use Symfony\Component\Validator\Constraints\UniqueValidator; |
| 16 | +use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
| 17 | + |
| 18 | +class UniqueValidatorTest extends ConstraintValidatorTestCase |
| 19 | +{ |
| 20 | + protected function createValidator() |
| 21 | + { |
| 22 | + return new UniqueValidator(); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException |
| 27 | + */ |
| 28 | + public function testExpectsUniqueConstraintCompatibleType() |
| 29 | + { |
| 30 | + $this->validator->validate('', new Unique()); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @dataProvider getValidValues |
| 35 | + */ |
| 36 | + public function testValidValues($value) |
| 37 | + { |
| 38 | + $this->validator->validate($value, new Unique()); |
| 39 | + |
| 40 | + $this->assertNoViolation(); |
| 41 | + } |
| 42 | + |
| 43 | + public function getValidValues() |
| 44 | + { |
| 45 | + return array( |
| 46 | + yield 'null' => array(array(null)), |
| 47 | + yield 'empty array' => array(array()), |
| 48 | + yield 'single integer' => array(array(5)), |
| 49 | + yield 'single string' => array(array('a')), |
| 50 | + yield 'single object' => array(array(new \stdClass())), |
| 51 | + yield 'unique booleans' => array(array(true, false)), |
| 52 | + yield 'unique integers' => array(array(1, 2, 3, 4, 5, 6)), |
| 53 | + yield 'unique floats' => array(array(0.1, 0.2, 0.3)), |
| 54 | + yield 'unique strings' => array(array('a', 'b', 'c')), |
| 55 | + yield 'unique arrays' => array(array(array(1, 2), array(2, 4), array(4, 6))), |
| 56 | + yield 'unique objects' => array(array(new \stdClass(), new \stdClass())), |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @dataProvider getInvalidValues |
| 62 | + */ |
| 63 | + public function testInvalidValues($value) |
| 64 | + { |
| 65 | + $constraint = new Unique(array( |
| 66 | + 'message' => 'myMessage', |
| 67 | + )); |
| 68 | + $this->validator->validate($value, $constraint); |
| 69 | + |
| 70 | + $this->buildViolation('myMessage') |
| 71 | + ->setParameter('{{ value }}', 'array') |
| 72 | + ->setCode(Unique::IS_NOT_UNIQUE) |
| 73 | + ->assertRaised(); |
| 74 | + } |
| 75 | + |
| 76 | + public function getInvalidValues() |
| 77 | + { |
| 78 | + $object = new \stdClass(); |
| 79 | + |
| 80 | + return array( |
| 81 | + yield 'not unique booleans' => array(array(true, true)), |
| 82 | + yield 'not unique integers' => array(array(1, 2, 3, 3)), |
| 83 | + yield 'not unique floats' => array(array(0.1, 0.2, 0.1)), |
| 84 | + yield 'not unique string' => array(array('a', 'b', 'a')), |
| 85 | + yield 'not unique arrays' => array(array(array(1, 1), array(2, 3), array(1, 1))), |
| 86 | + yield 'not unique objects' => array(array($object, $object)), |
| 87 | + ); |
| 88 | + } |
| 89 | +} |
0 commit comments