4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Validator\Constraint;
16 use Symfony\Component\Validator\Tests\Fixtures\ClassConstraint;
17 use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
18 use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
19 use Symfony\Component\Validator\Tests\Fixtures\ConstraintC;
20 use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithValue;
21 use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithValueAsDefault;
23 class ConstraintTest extends TestCase
25 public function testSetProperties()
27 $constraint = new ConstraintA(array(
32 $this->assertEquals('foo', $constraint->property1);
33 $this->assertEquals('bar', $constraint->property2);
36 public function testSetNotExistingPropertyThrowsException()
38 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\InvalidOptionsException');
40 new ConstraintA(array(
45 public function testMagicPropertiesAreNotAllowed()
47 $constraint = new ConstraintA();
49 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\InvalidOptionsException');
51 $constraint->foo = 'bar';
54 public function testInvalidAndRequiredOptionsPassed()
56 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\InvalidOptionsException');
58 new ConstraintC(array(
59 'option1' => 'default',
64 public function testSetDefaultProperty()
66 $constraint = new ConstraintA('foo');
68 $this->assertEquals('foo', $constraint->property2);
71 public function testSetDefaultPropertyDoctrineStyle()
73 $constraint = new ConstraintA(array('value' => 'foo'));
75 $this->assertEquals('foo', $constraint->property2);
78 public function testSetDefaultPropertyDoctrineStylePlusOtherProperty()
80 $constraint = new ConstraintA(array('value' => 'foo', 'property1' => 'bar'));
82 $this->assertEquals('foo', $constraint->property2);
83 $this->assertEquals('bar', $constraint->property1);
86 public function testSetDefaultPropertyDoctrineStyleWhenDefaultPropertyIsNamedValue()
88 $constraint = new ConstraintWithValueAsDefault(array('value' => 'foo'));
90 $this->assertEquals('foo', $constraint->value);
91 $this->assertNull($constraint->property);
94 public function testDontSetDefaultPropertyIfValuePropertyExists()
96 $constraint = new ConstraintWithValue(array('value' => 'foo'));
98 $this->assertEquals('foo', $constraint->value);
99 $this->assertNull($constraint->property);
102 public function testSetUndefinedDefaultProperty()
104 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
106 new ConstraintB('foo');
109 public function testRequiredOptionsMustBeDefined()
111 $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\MissingOptionsException');
116 public function testRequiredOptionsPassed()
118 $constraint = new ConstraintC(array('option1' => 'default'));
120 $this->assertSame('default', $constraint->option1);
123 public function testGroupsAreConvertedToArray()
125 $constraint = new ConstraintA(array('groups' => 'Foo'));
127 $this->assertEquals(array('Foo'), $constraint->groups);
130 public function testAddDefaultGroupAddsGroup()
132 $constraint = new ConstraintA(array('groups' => 'Default'));
133 $constraint->addImplicitGroupName('Foo');
134 $this->assertEquals(array('Default', 'Foo'), $constraint->groups);
137 public function testAllowsSettingZeroRequiredPropertyValue()
139 $constraint = new ConstraintA(0);
140 $this->assertEquals(0, $constraint->property2);
143 public function testCanCreateConstraintWithNoDefaultOptionAndEmptyArray()
145 $constraint = new ConstraintB(array());
147 $this->assertSame(array(Constraint::PROPERTY_CONSTRAINT, Constraint::CLASS_CONSTRAINT), $constraint->getTargets());
150 public function testGetTargetsCanBeString()
152 $constraint = new ClassConstraint();
154 $this->assertEquals('class', $constraint->getTargets());
157 public function testGetTargetsCanBeArray()
159 $constraint = new ConstraintA();
161 $this->assertEquals(array('property', 'class'), $constraint->getTargets());
164 public function testSerialize()
166 $constraint = new ConstraintA(array(
167 'property1' => 'foo',
168 'property2' => 'bar',
171 $restoredConstraint = unserialize(serialize($constraint));
173 $this->assertEquals($constraint, $restoredConstraint);
176 public function testSerializeInitializesGroupsOptionToDefault()
178 $constraint = new ConstraintA(array(
179 'property1' => 'foo',
180 'property2' => 'bar',
183 $constraint = unserialize(serialize($constraint));
185 $expected = new ConstraintA(array(
186 'property1' => 'foo',
187 'property2' => 'bar',
188 'groups' => 'Default',
191 $this->assertEquals($expected, $constraint);
194 public function testSerializeKeepsCustomGroups()
196 $constraint = new ConstraintA(array(
197 'property1' => 'foo',
198 'property2' => 'bar',
199 'groups' => 'MyGroup',
202 $constraint = unserialize(serialize($constraint));
204 $this->assertSame(array('MyGroup'), $constraint->groups);
208 * @expectedException \Symfony\Component\Validator\Exception\InvalidArgumentException
210 public function testGetErrorNameForUnknownCode()
212 Constraint::getErrorName(1);
215 public function testOptionsAsDefaultOption()
217 $constraint = new ConstraintA($options = array('value1'));
219 $this->assertEquals($options, $constraint->property2);
221 $constraint = new ConstraintA($options = array('value1', 'property1' => 'value2'));
223 $this->assertEquals($options, $constraint->property2);
227 * @expectedException \Symfony\Component\Validator\Exception\InvalidOptionsException
228 * @expectedExceptionMessage The options "0", "5" do not exist
230 public function testInvalidOptions()
232 new ConstraintA(array('property2' => 'foo', 'bar', 5 => 'baz'));
235 public function testOptionsWithInvalidInternalPointer()
237 $options = array('property1' => 'foo');
241 $constraint = new ConstraintA($options);
243 $this->assertEquals('foo', $constraint->property1);