Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / CompositeTest.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\Constraints;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Validator\Constraints\Composite;
16 use Symfony\Component\Validator\Constraints\NotBlank;
17 use Symfony\Component\Validator\Constraints\NotNull;
18 use Symfony\Component\Validator\Constraints\Valid;
19
20 class ConcreteComposite extends Composite
21 {
22     public $constraints;
23
24     protected function getCompositeOption()
25     {
26         return 'constraints';
27     }
28
29     public function getDefaultOption()
30     {
31         return 'constraints';
32     }
33 }
34
35 /**
36  * @author Bernhard Schussek <bschussek@gmail.com>
37  */
38 class CompositeTest extends TestCase
39 {
40     public function testMergeNestedGroupsIfNoExplicitParentGroup()
41     {
42         $constraint = new ConcreteComposite(array(
43             new NotNull(array('groups' => 'Default')),
44             new NotBlank(array('groups' => array('Default', 'Strict'))),
45         ));
46
47         $this->assertEquals(array('Default', 'Strict'), $constraint->groups);
48         $this->assertEquals(array('Default'), $constraint->constraints[0]->groups);
49         $this->assertEquals(array('Default', 'Strict'), $constraint->constraints[1]->groups);
50     }
51
52     public function testSetImplicitNestedGroupsIfExplicitParentGroup()
53     {
54         $constraint = new ConcreteComposite(array(
55             'constraints' => array(
56                 new NotNull(),
57                 new NotBlank(),
58             ),
59             'groups' => array('Default', 'Strict'),
60         ));
61
62         $this->assertEquals(array('Default', 'Strict'), $constraint->groups);
63         $this->assertEquals(array('Default', 'Strict'), $constraint->constraints[0]->groups);
64         $this->assertEquals(array('Default', 'Strict'), $constraint->constraints[1]->groups);
65     }
66
67     public function testExplicitNestedGroupsMustBeSubsetOfExplicitParentGroups()
68     {
69         $constraint = new ConcreteComposite(array(
70             'constraints' => array(
71                 new NotNull(array('groups' => 'Default')),
72                 new NotBlank(array('groups' => 'Strict')),
73             ),
74             'groups' => array('Default', 'Strict'),
75         ));
76
77         $this->assertEquals(array('Default', 'Strict'), $constraint->groups);
78         $this->assertEquals(array('Default'), $constraint->constraints[0]->groups);
79         $this->assertEquals(array('Strict'), $constraint->constraints[1]->groups);
80     }
81
82     /**
83      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
84      */
85     public function testFailIfExplicitNestedGroupsNotSubsetOfExplicitParentGroups()
86     {
87         new ConcreteComposite(array(
88             'constraints' => array(
89                 new NotNull(array('groups' => array('Default', 'Foobar'))),
90             ),
91             'groups' => array('Default', 'Strict'),
92         ));
93     }
94
95     public function testImplicitGroupNamesAreForwarded()
96     {
97         $constraint = new ConcreteComposite(array(
98             new NotNull(array('groups' => 'Default')),
99             new NotBlank(array('groups' => 'Strict')),
100         ));
101
102         $constraint->addImplicitGroupName('ImplicitGroup');
103
104         $this->assertEquals(array('Default', 'Strict', 'ImplicitGroup'), $constraint->groups);
105         $this->assertEquals(array('Default', 'ImplicitGroup'), $constraint->constraints[0]->groups);
106         $this->assertEquals(array('Strict'), $constraint->constraints[1]->groups);
107     }
108
109     public function testSingleConstraintsAccepted()
110     {
111         $nestedConstraint = new NotNull();
112         $constraint = new ConcreteComposite($nestedConstraint);
113
114         $this->assertEquals(array($nestedConstraint), $constraint->constraints);
115     }
116
117     /**
118      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
119      */
120     public function testFailIfNoConstraint()
121     {
122         new ConcreteComposite(array(
123             new NotNull(array('groups' => 'Default')),
124             'NotBlank',
125         ));
126     }
127
128     /**
129      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
130      */
131     public function testFailIfNoConstraintObject()
132     {
133         new ConcreteComposite(array(
134             new NotNull(array('groups' => 'Default')),
135             new \ArrayObject(),
136         ));
137     }
138
139     /**
140      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
141      */
142     public function testValidCantBeNested()
143     {
144         new ConcreteComposite(array(
145             new Valid(),
146         ));
147     }
148 }