Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / validator / Tests / ConstraintTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
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;
13
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;
22
23 class ConstraintTest extends TestCase
24 {
25     public function testSetProperties()
26     {
27         $constraint = new ConstraintA(array(
28             'property1' => 'foo',
29             'property2' => 'bar',
30         ));
31
32         $this->assertEquals('foo', $constraint->property1);
33         $this->assertEquals('bar', $constraint->property2);
34     }
35
36     public function testSetNotExistingPropertyThrowsException()
37     {
38         $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\InvalidOptionsException');
39
40         new ConstraintA(array(
41             'foo' => 'bar',
42         ));
43     }
44
45     public function testMagicPropertiesAreNotAllowed()
46     {
47         $constraint = new ConstraintA();
48
49         $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\InvalidOptionsException');
50
51         $constraint->foo = 'bar';
52     }
53
54     public function testInvalidAndRequiredOptionsPassed()
55     {
56         $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\InvalidOptionsException');
57
58         new ConstraintC(array(
59             'option1' => 'default',
60             'foo' => 'bar',
61         ));
62     }
63
64     public function testSetDefaultProperty()
65     {
66         $constraint = new ConstraintA('foo');
67
68         $this->assertEquals('foo', $constraint->property2);
69     }
70
71     public function testSetDefaultPropertyDoctrineStyle()
72     {
73         $constraint = new ConstraintA(array('value' => 'foo'));
74
75         $this->assertEquals('foo', $constraint->property2);
76     }
77
78     public function testSetDefaultPropertyDoctrineStylePlusOtherProperty()
79     {
80         $constraint = new ConstraintA(array('value' => 'foo', 'property1' => 'bar'));
81
82         $this->assertEquals('foo', $constraint->property2);
83         $this->assertEquals('bar', $constraint->property1);
84     }
85
86     public function testSetDefaultPropertyDoctrineStyleWhenDefaultPropertyIsNamedValue()
87     {
88         $constraint = new ConstraintWithValueAsDefault(array('value' => 'foo'));
89
90         $this->assertEquals('foo', $constraint->value);
91         $this->assertNull($constraint->property);
92     }
93
94     public function testDontSetDefaultPropertyIfValuePropertyExists()
95     {
96         $constraint = new ConstraintWithValue(array('value' => 'foo'));
97
98         $this->assertEquals('foo', $constraint->value);
99         $this->assertNull($constraint->property);
100     }
101
102     public function testSetUndefinedDefaultProperty()
103     {
104         $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
105
106         new ConstraintB('foo');
107     }
108
109     public function testRequiredOptionsMustBeDefined()
110     {
111         $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\MissingOptionsException');
112
113         new ConstraintC();
114     }
115
116     public function testRequiredOptionsPassed()
117     {
118         $constraint = new ConstraintC(array('option1' => 'default'));
119
120         $this->assertSame('default', $constraint->option1);
121     }
122
123     public function testGroupsAreConvertedToArray()
124     {
125         $constraint = new ConstraintA(array('groups' => 'Foo'));
126
127         $this->assertEquals(array('Foo'), $constraint->groups);
128     }
129
130     public function testAddDefaultGroupAddsGroup()
131     {
132         $constraint = new ConstraintA(array('groups' => 'Default'));
133         $constraint->addImplicitGroupName('Foo');
134         $this->assertEquals(array('Default', 'Foo'), $constraint->groups);
135     }
136
137     public function testAllowsSettingZeroRequiredPropertyValue()
138     {
139         $constraint = new ConstraintA(0);
140         $this->assertEquals(0, $constraint->property2);
141     }
142
143     public function testCanCreateConstraintWithNoDefaultOptionAndEmptyArray()
144     {
145         $constraint = new ConstraintB(array());
146
147         $this->assertSame(array(Constraint::PROPERTY_CONSTRAINT, Constraint::CLASS_CONSTRAINT), $constraint->getTargets());
148     }
149
150     public function testGetTargetsCanBeString()
151     {
152         $constraint = new ClassConstraint();
153
154         $this->assertEquals('class', $constraint->getTargets());
155     }
156
157     public function testGetTargetsCanBeArray()
158     {
159         $constraint = new ConstraintA();
160
161         $this->assertEquals(array('property', 'class'), $constraint->getTargets());
162     }
163
164     public function testSerialize()
165     {
166         $constraint = new ConstraintA(array(
167             'property1' => 'foo',
168             'property2' => 'bar',
169         ));
170
171         $restoredConstraint = unserialize(serialize($constraint));
172
173         $this->assertEquals($constraint, $restoredConstraint);
174     }
175
176     public function testSerializeInitializesGroupsOptionToDefault()
177     {
178         $constraint = new ConstraintA(array(
179             'property1' => 'foo',
180             'property2' => 'bar',
181         ));
182
183         $constraint = unserialize(serialize($constraint));
184
185         $expected = new ConstraintA(array(
186             'property1' => 'foo',
187             'property2' => 'bar',
188             'groups' => 'Default',
189         ));
190
191         $this->assertEquals($expected, $constraint);
192     }
193
194     public function testSerializeKeepsCustomGroups()
195     {
196         $constraint = new ConstraintA(array(
197             'property1' => 'foo',
198             'property2' => 'bar',
199             'groups' => 'MyGroup',
200         ));
201
202         $constraint = unserialize(serialize($constraint));
203
204         $this->assertSame(array('MyGroup'), $constraint->groups);
205     }
206
207     /**
208      * @expectedException \Symfony\Component\Validator\Exception\InvalidArgumentException
209      */
210     public function testGetErrorNameForUnknownCode()
211     {
212         Constraint::getErrorName(1);
213     }
214
215     public function testOptionsAsDefaultOption()
216     {
217         $constraint = new ConstraintA($options = array('value1'));
218
219         $this->assertEquals($options, $constraint->property2);
220
221         $constraint = new ConstraintA($options = array('value1', 'property1' => 'value2'));
222
223         $this->assertEquals($options, $constraint->property2);
224     }
225
226     /**
227      * @expectedException \Symfony\Component\Validator\Exception\InvalidOptionsException
228      * @expectedExceptionMessage The options "0", "5" do not exist
229      */
230     public function testInvalidOptions()
231     {
232         new ConstraintA(array('property2' => 'foo', 'bar', 5 => 'baz'));
233     }
234
235     public function testOptionsWithInvalidInternalPointer()
236     {
237         $options = array('property1' => 'foo');
238         next($options);
239         next($options);
240
241         $constraint = new ConstraintA($options);
242
243         $this->assertEquals('foo', $constraint->property1);
244     }
245 }