8000 [Validator] GroupSequenceProvider tests improved, configuration changed · symfony/symfony@e0d2828 · GitHub
[go: up one dir, main page]

Skip to content

Commit e0d2828

Browse files
committed
[Validator] GroupSequenceProvider tests improved, configuration changed
1 parent c3b04a3 commit e0d2828

File tree

14 files changed

+110
-35
lines changed

14 files changed

+110
-35
lines changed

src/Symfony/Component/Validator/Constraints/GroupSequenceProvider.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,5 @@
1818
*/
1919
class GroupSequenceProvider
2020
{
21-
/**
22-
* True if the group sequence provider should be used
23-
* @var boolean
24-
*/
25-
public $active;
2621

27-
public function __construct(array $options)
28-
{
29-
$this->active = (bool)$options['value'];
30-
}
3122
}

src/Symfony/Component/Validator/Mapping/ClassMetadata.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public function getReflectionClass()
307307
*/
308308
public function setGroupSequenceProvider($active)
309309
{
310-
if ($this->hasGroupSequenceProvider()) {
310+
if ($this->hasGroupSequence()) {
311311
throw new GroupDefinitionException('Defining a group sequence provider is not allowed with a static group sequence');
312312
}
313313

src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function loadClassMetadata(ClassMetadata $metadata)
4040
if ($constraint instanceof GroupSequence) {
4141
$metadata->setGroupSequence($constraint->groups);
4242
} elseif ($constraint instanceof GroupSequenceProvider) {
43-
$metadata->setGroupSequenceProvider($constraint->active);
43+
$metadata->setGroupSequenceProvider(true);
4444
} elseif ($constraint instanceof Constraint) {
4545
$metadata->addConstraint($constraint);
4646
}

src/Symfony/Component/Validator/Mapping/Loader/XmlFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function loadClassMetadata(ClassMetadata $metadata)
4444
$xml = $this->classes[$metadata->getClassName()];
4545

4646
foreach ($xml->{'group-sequence-provider'} as $provider) {
47-
$metadata->setGroupSequenceProvider((bool)$provider['active']);
47+
$metadata->setGroupSequenceProvider(true);
4848
}
4949

5050
foreach ($this->parseConstraints($xml->constraint) as $constraint) {

src/Symfony/Component/Validator/Mapping/Loader/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
Defines the name of the group sequence provider for a class.
6767
]]></xsd:documentation>
6868
</xsd:annotation>
69-
<xsd:attribute name="active" type="xsd:boolean" use="required" />
7069
</xsd:complexType>
7170

7271
<xsd:complexType name="property">

tests/Symfony/Tests/Component/Validator/Fixtures/Entity.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
/**
1212
* @Symfony\Tests\Component\Validator\Fixtures\ConstraintA
1313
* @Assert\GroupSequence({"Foo", "Entity"})
14-
* @Assert\GroupSequenceProvider("Symfony\Tests\Component\Validator\Fixtures\GroupSequenceProvider")
1514
*/
16-
class Entity extends EntityParent implements EntityInterface, GroupSequenceProviderInterface
15+
class Entity extends EntityParent implements EntityInterface
1716
{
1817
/**
1918
* @Assert\NotNull
@@ -51,14 +50,4 @@ public function getLastName()
5150
{
5251
return $this->lastName;
5352
}
54-
55-
public function setGroups($groups)
56-
{
57-
$this->groups = $groups;
58-
}
59-
60-
public function getValidationGroups()
61-
{
62-
return $this->groups;
63-
}
6453
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Symfony\Tests\Component\Validator\Fixtures;
4+
5+
use Symfony\Component\Validator\Constraints as Assert;
6+
use Symfony\Component\Validator\GroupSequenceProviderInterface;
7+
8+
/**
9+
* @Assert\GroupSequenceProvider
10+
*/
11+
class GroupSequenceProviderEntity implements GroupSequenceProviderInterface
12+
{
13+
public $firstName;
14+
public $lastName;
15+
16+
protected $groups = array();
17+
18+
public function setGroups($groups)
19+
{
20+
$this->groups = $groups;
21+
}
22+
23+
public function getValidationGroups()
24+
{
25+
return $this->groups;
26+
}
27+
}

tests/Symfony/Tests/Component/Validator/Mapping/ClassMetadataTest.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class ClassMetadataTest extends \PHPUnit_Framework_TestCase
3030
{
3131
const CLASSNAME = 'Symfony\Tests\Component\Validator\Fixtures\Entity';
3232
const PARENTCLASS = 'Symfony\Tests\Component\Validator\Fixtures\EntityParent';
33+
const PROVIDERCLASS = 'Symfony\Tests\Component\Validator\Fixtures\GroupSequenceProviderEntity';
3334

3435
protected $metadata;
3536

@@ -192,16 +193,42 @@ public function testGroupSequencesFailIfContainingDefault()
192193
$this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup(), Constraint::DEFAULT_GROUP));
193194
}
194195

195-
public function testGroupSequenceProvider()
196+
public function testGroupSequenceFailesIfGroupSequenceProviderIsSet()
197+
{
198+
$metadata = new ClassMetadata(self::PROVIDERCLASS);
199+
$metadata->setGroupSequenceProvider(true);
200+
201+
try {
202+
$metadata->setGroupSequence(array('GroupSequenceProviderEntity', 'Foo'));
203+
$this->fail();
204+
} catch(GroupDefinitionException $e) {}
205+
}
206+
207+
public function testGroupSequenceProviderFailesIfGroupSequenceIsSet()
208+
{
209+
$metadata = new ClassMetadata(self::PROVIDERCLASS);
210+
$metadata->setGroupSequence(array('GroupSequenceProviderEntity', 'Foo'));
211+
212+
try {
213+
$metadata->setGroupSequenceProvider(true);
214+
$this->fail();
215+
} catch(GroupDefinitionException $e) {}
216+
}
217+
218+
public function testGroupSequenceProviderFailesIfDomainClassIsInvalid()
196219
{
197220
$metadata = new ClassMetadata('stdClass');
198221

199222
try {
200223
$metadata->setGroupSequenceProvider(true);
201224
$this->fail();
202225
} catch(GroupDefinitionException $e) {}
226+
}
203227

204-
$this->metadata->setGroupSequenceProvider(true);
205-
$this->assertTrue($this->metadata->hasGroupSequenceProvider());
228+
public function testGroupSequenceProvider()
229+
{
230+
$metadata = new ClassMetadata(self::PROVIDERCLASS);
231+
$metadata->setGroupSequenceProvider(true);
232+
$this->assertTrue($metadata->hasGroupSequenceProvider());
206233
}
207234
}

tests/Symfony/Tests/Component/Validator/Mapping/Loader/AnnotationLoaderTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public function testLoadClassMetadata()
7373
'choices' => array('A', 'B'),
7474
)));
7575
$expected->addGetterConstraint('lastName', new NotNull());
76-
$expected->setGroupSequenceProvider(true);
7776

7877
// load reflection class so that the comparison passes
7978
$expected->getReflectionClass();
@@ -138,12 +137,24 @@ public function testLoadClassMetadataAndMerge()
138137
'choices' => array('A', 'B'),
139138
)));
140139
$expected->addGetterConstraint('lastName', new NotNull());
141-
$expected->setGroupSequenceProvider(true);
142140

143141
// load reflection class so that the comparison passes
144142
$expected->getReflectionClass();
145143

146144
$this->assertEquals($expected, $metadata);
147145
}
148146

147+
public function testLoadGroupSequenceProviderAnnotation()
148+
{
149+
$loader = new AnnotationLoader(new AnnotationReader());
150+
151+
$metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\GroupSequenceProviderEntity');
152+
$loader->loadClassMetadata($metadata);
153+
154+
$expected = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\GroupSequenceProviderEntity');
155+
$expected->setGroupSequenceProvider(true);
156+
$expected->getReflectionClass();
157+
158+
$this->assertEquals($expected, $metadata);
159+
}
149160
}

tests/Symfony/Tests/Component/Validator/Mapping/Loader/XmlFileLoaderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ public function testLoadClassMetadata()
6767
'choices' => array('A', 'B'),
6868
)));
6969
$expected->addGetterConstraint('lastName', new NotNull());
70+
71+
$this->assertEquals($expected, $metadata);
72+
}
73+
74+
public function testLoadGroupSequenceProvider()
75+
{
76+
$loader = new XmlFileLoader(__DIR__.'/constraint-mapping.xml');
77+
$metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\GroupSequenceProviderEntity');
78+
79+
$loader->loadClassMetadata($metadata);
80+
81+
$expected = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\GroupSequenceProviderEntity');
7082
$expected->setGroupSequenceProvider(true);
7183

7284
$this->assertEquals($expected, $metadata);

tests/Symfony/Tests/Component/Validator/Mapping/Loader/YamlFileLoaderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ public function testLoadClassMetadata()
8585
'choices' => array('A', 'B'),
8686
)));
8787
$expected->addGetterConstraint('lastName', new NotNull());
88+
89+
$this->assertEquals($expected, $metadata);
90+
}
91+
92+
public function testLoadGroupSequenceProvider()
93+
{
94+
$loader = new YamlFileLoader(__DIR__.'/constraint-mapping.yml');
95+
$metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\GroupSequenceProviderEntity');
96+
97+
$loader->loadClassMetadata($metadata);
98+
99+
$expected = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\GroupSequenceProviderEntity');
88100
$expected->setGroupSequenceProvider(true);
89101

90102
$this->assertEquals($expected, $metadata);

tests/Symfony/Tests/Component/Validator/Mapping/Loader/constraint-mapping.xml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88

99
<class name="Symfony\Tests\Component\Validator\Fixtures\Entity">
1010

11-
<!-- GROUP SEQUENCE PROVIDER -->
12-
<group-sequence-provider active="true" />
13-
1411
<!-- CLASS CONSTRAINTS -->
1512

1613
<!-- Custom constraint -->
@@ -80,4 +77,11 @@
8077
<constraint name="NotNull" />
8178
</getter>
8279
</class>
80+
81+
<class name="Symfony\Tests\Component\Validator\Fixtures\GroupSequenceProviderEntity">
82+
83+
<!-- GROUP SEQUENCE PROVIDER -->
84+
<group-sequence-provider />
85+
86+
</class>
8387
</constraint-mapping>

tests/Symfony/Tests/Component/Validator/Mapping/Loader/constraint-mapping.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ namespaces:
22
custom: Symfony\Tests\Component\Validator\Fixtures\
33

44
Symfony\Tests\Component\Validator\Fixtures\Entity:
5-
group_sequence_provider: true
65
constraints:
76
# Custom constraint
87
- Symfony\Tests\Component\Validator\Fixtures\ConstraintA: ~
@@ -40,3 +39,6 @@ Symfony\Tests\Component\Validator\Fixtures\Entity:
4039
getters:
4140
lastName:
4241
- NotNull: ~
42+
43+
Symfony\Tests\Component\Validator\Fixtures\GroupSequenceProviderEntity:
44+
group_sequence_provider: true

tests/Symfony/Tests/Component/Validator/ValidatorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
require_once __DIR__.'/Fixtures/FakeClassMetadataFactory.php';
1818

1919
use Symfony\Tests\Component\Validator\Fixtures\Entity;
20+
use Symfony\Tests\Component\Validator\Fixtures\GroupSequenceProviderEntity;
2021
use Symfony\Tests\Component\Validator\Fixtures\FakeClassMetadataFactory;
2122
use Symfony\Tests\Component\Validator\Fixtures\FailingConstraint;
2223
use Symfony\Component\Validator\Validator;
@@ -124,7 +125,7 @@ public function testValidate_multipleGroups()
124125

125126
public function testValidate_groupSequenceProvider()
126127
{
127-
$entity = new Entity();
128+
$entity = new GroupSequenceProviderEntity();
128129
$metadata = new ClassMetadata(get_class($entity));
129130
$metadata->addPropertyConstraint('firstName', new FailingConstraint(array(
130131
'groups' => 'First',

0 commit comments

Comments
 (0)
0