Skip to content

Commit

Permalink
[Validator] Group sequences must now always contain the group "<Class…
Browse files Browse the repository at this point in the history
…Name>" and never the group "Default" since that group is redefined by the group sequence
  • Loading branch information
Bernhard Schussek authored and fabpot committed Nov 19, 2010
1 parent a71cad4 commit 68cebd6
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 24 deletions.
@@ -0,0 +1,16 @@
<?php

namespace Symfony\Component\Validator\Exception;

/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

class GroupDefinitionException extends ValidatorException
{
}
42 changes: 31 additions & 11 deletions src/Symfony/Component/Validator/Mapping/ClassMetadata.php
Expand Up @@ -14,11 +14,12 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\GroupDefinitionException;

class ClassMetadata extends ElementMetadata
{
public $name;
public $shortName;
public $defaultGroup;
public $members = array();
public $properties = array();
public $getters = array();
Expand All @@ -33,7 +34,8 @@ class ClassMetadata extends ElementMetadata
public function __construct($class)
{
$this->name = $class;
$this->shortName = substr($class, strrpos($class, '\\') + 1);
// class name without namespace
$this->defaultGroup = substr($class, strrpos($class, '\\') + 1);
}

/**
Expand All @@ -49,7 +51,7 @@ public function __sleep()
'members',
'name',
'properties',
'shortName'
'defaultGroup'
));
}

Expand All @@ -64,13 +66,23 @@ public function getClassName()
}

/**
* Returns the class name without namespace
* Returns the name of the default group for this class
*
* @return string The local class name in the namespace
* For each class, the group "Default" is an alias for the group
* "<ClassName>", where <ClassName> is the non-namespaced name of the
* class. All constraints implicitely or explicitely assigned to group
* "Default" belong to both of these groups, unless the class defines
* a group sequence.
*
* If a class defines a group sequence, validating the class in "Default"
* will validate the group sequence. The constraints assinged to "Default"
* can still be validated by validating the class in "<ClassName>".
*
* @return string The name of the default group
*/
public function getShortClassName()
public function getDefaultGroup()
{
return $this->shortName;
return $this->defaultGroup;
}

/**
Expand All @@ -82,7 +94,7 @@ public function addConstraint(Constraint $constraint)
throw new ConstraintDefinitionException('The constraint Valid can only be put on properties or getters');
}

$constraint->addImplicitGroupName($this->getShortClassName());
$constraint->addImplicitGroupName($this->getDefaultGroup());

parent::addConstraint($constraint);
}
Expand All @@ -103,7 +115,7 @@ public function addPropertyConstraint($property, Constraint $constraint)
$this->addMemberMetadata($this->properties[$property]);
}

$constraint->addImplicitGroupName($this->getShortClassName());
$constraint->addImplicitGroupName($this->getDefaultGroup());

$this->properties[$property]->addConstraint($constraint);

Expand All @@ -129,7 +141,7 @@ public function addGetterConstraint($property, Constraint $constraint)
$this->addMemberMetadata($this->getters[$property]);
}

$constraint->addImplicitGroupName($this->getShortClassName());
$constraint->addImplicitGroupName($this->getDefaultGroup());

$this->getters[$property]->addConstraint($constraint);

Expand All @@ -152,7 +164,7 @@ public function mergeConstraints(ClassMetadata $source)
$member = clone $member;

foreach ($member->getConstraints() as $constraint) {
$constraint->addImplicitGroupName($this->getShortClassName());
$constraint->addImplicitGroupName($this->getDefaultGroup());
}

$this->addMemberMetadata($member);
Expand Down Expand Up @@ -213,6 +225,14 @@ public function getConstrainedProperties()
*/
public function setGroupSequence(array $groups)
{
if (in_array(Constraint::DEFAULT_GROUP, $groups, true)) {
throw new GroupDefinitionException(sprintf('The group "%s" is not allowed in group sequences', Constraint::DEFAULT_GROUP));
}

if (!in_array($this->getDefaultGroup(), $groups, true)) {
throw new GroupDefinitionException(sprintf('The group "%s" is missing in the group sequence', $this->getDefaultGroup()));
}

$this->groupSequence = $groups;

return $this;
Expand Down
12 changes: 6 additions & 6 deletions tests/Symfony/Tests/Component/Validator/GraphWalkerTest.php
Expand Up @@ -72,9 +72,9 @@ public function testWalkClassInDefaultGroupTraversesGroupSequence()
'groups' => 'First',
)));
$this->metadata->addGetterConstraint('lastName', new FailingConstraint(array(
'groups' => 'Second',
'groups' => 'Default',
)));
$this->metadata->setGroupSequence(array('First', 'Second'));
$this->metadata->setGroupSequence(array('First', $this->metadata->getDefaultGroup()));

$this->walker->walkClass($this->metadata, $entity, 'Default', '');

Expand All @@ -98,7 +98,7 @@ public function testWalkClassInGroupSequencePropagatesDefaultGroup()
$entity->reference = new Reference();

$this->metadata->addPropertyConstraint('reference', new Valid());
$this->metadata->setGroupSequence(array('First'));
$this->metadata->setGroupSequence(array($this->metadata->getDefaultGroup()));

$referenceMetadata = new ClassMetadata(get_class($entity->reference));
$referenceMetadata->addConstraint(new FailingConstraint(array(
Expand Down Expand Up @@ -132,11 +132,11 @@ public function testWalkClassInOtherGroupTraversesNoGroupSequence()
'groups' => 'First',
)));
$this->metadata->addGetterConstraint('lastName', new FailingConstraint(array(
'groups' => 'Second',
'groups' => $this->metadata->getDefaultGroup(),
)));
$this->metadata->setGroupSequence(array('First', 'Second'));
$this->metadata->setGroupSequence(array('First', $this->metadata->getDefaultGroup()));

$this->walker->walkClass($this->metadata, $entity, 'Second', '');
$this->walker->walkClass($this->metadata, $entity, $this->metadata->getDefaultGroup(), '');

// Only group "Second" was validated
$violations = new ConstraintViolationList();
Expand Down
Expand Up @@ -2,16 +2,17 @@

namespace Symfony\Tests\Component\Validator\Mapping;

require_once __DIR__.'/../Fixtures/Entity.php';
require_once __DIR__.'/../Fixtures/ConstraintA.php';
require_once __DIR__.'/../Fixtures/ConstraintB.php';

use Symfony\Tests\Component\Validator\Fixtures\Entity;
use Symfony\Tests\Component\Validator\Fixtures\ConstraintA;
use Symfony\Tests\Component\Validator\Fixtures\ConstraintB;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\PropertyMetadata;
use Symfony\Tests\Component\Validator\Fixtures\Entity;
use Symfony\Tests\Component\Validator\Fixtures\ConstraintA;
use Symfony\Tests\Component\Validator\Fixtures\ConstraintB;

require_once __DIR__.'/../Fixtures/Entity.php';
require_once __DIR__.'/../Fixtures/ConstraintA.php';
require_once __DIR__.'/../Fixtures/ConstraintB.php';

class ClassMetadataTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -139,5 +140,24 @@ public function testSerialize()

$this->assertEquals($this->metadata, $metadata);
}

public function testGroupSequencesWorkIfContainingDefaultGroup()
{
$this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup()));
}

public function testGroupSequencesFailIfNotContainingDefaultGroup()
{
$this->setExpectedException('Symfony\Component\Validator\Exception\GroupDefinitionException');

$this->metadata->setGroupSequence(array('Foo', 'Bar'));
}

public function testGroupSequencesFailIfContainingDefault()
{
$this->setExpectedException('Symfony\Component\Validator\Exception\GroupDefinitionException');

$this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup(), Constraint::DEFAULT_GROUP));
}
}

0 comments on commit 68cebd6

Please sign in to comment.