Merge branch '2.3' into 2.6

* 2.3:
  Fixed compatibility with PHP7 and up by introducing new constraints (IsNull, IsTrue, IsFalse) and related validators (IsNullValidator, IsTrueValidator, IsFalseValidator)

Conflicts:
	src/Symfony/Component/Form/composer.json
	src/Symfony/Component/Validator/CHANGELOG.md
	src/Symfony/Component/Validator/Constraints/FalseValidator.php
	src/Symfony/Component/Validator/Constraints/NullValidator.php
	src/Symfony/Component/Validator/Constraints/TrueValidator.php
	src/Symfony/Component/Validator/Tests/Constraints/IsFalseValidatorTest.php
	src/Symfony/Component/Validator/Tests/Constraints/IsNullValidatorTest.php
	src/Symfony/Component/Validator/Tests/Constraints/IsTrueValidatorTest.php
This commit is contained in:
Nicolas Grekas 2015-05-15 11:48:50 +02:00
commit 1688b2ef4f
32 changed files with 280 additions and 137 deletions

View File

@ -158,6 +158,8 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
case 'Symfony\Component\Validator\Constraints\True':
case 'Symfony\Component\Validator\Constraints\False':
case 'Symfony\Component\Validator\Constraints\IsTrue':
case 'Symfony\Component\Validator\Constraints\IsFalse':
return new TypeGuess('checkbox', array(), Guess::MEDIUM_CONFIDENCE);
}
}
@ -175,6 +177,7 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
case 'Symfony\Component\Validator\Constraints\NotNull':
case 'Symfony\Component\Validator\Constraints\NotBlank':
case 'Symfony\Component\Validator\Constraints\True':
case 'Symfony\Component\Validator\Constraints\IsTrue':
return new ValueGuess(true, Guess::HIGH_CONFIDENCE);
}
}

View File

@ -19,7 +19,7 @@ use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\True;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\Type;
use Symfony\Component\Validator\Mapping\ClassMetadata;
@ -64,7 +64,7 @@ class ValidatorTypeGuesserTest extends \PHPUnit_Framework_TestCase
return array(
array(new NotNull(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)),
array(new NotBlank(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)),
array(new True(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)),
array(new IsTrue(), new ValueGuess(true, Guess::HIGH_CONFIDENCE)),
array(new Length(10), new ValueGuess(false, Guess::LOW_CONFIDENCE)),
array(new Range(array('min' => 1, 'max' => 20)), new ValueGuess(false, Guess::LOW_CONFIDENCE)),
);
@ -84,6 +84,18 @@ class ValidatorTypeGuesserTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($guess, $this->guesser->guessRequired(self::TEST_CLASS, self::TEST_PROPERTY));
}
/**
* @group legacy
*/
public function testLegacyGuessRequired()
{
if (PHP_VERSION_ID >= 70000) {
$this->markTestSkipped('Cannot use a class called True on PHP 7 or higher.');
}
$true = 'Symfony\Component\Validator\Constraints\True';
$this->testGuessRequired(new $true(), new ValueGuess(true, Guess::HIGH_CONFIDENCE));
}
public function testGuessRequiredReturnsFalseForUnmappedProperties()
{
$this->assertEquals(new ValueGuess(false, Guess::LOW_CONFIDENCE), $this->guesser->guessRequired(self::TEST_CLASS, self::TEST_PROPERTY));

View File

@ -25,7 +25,7 @@
"require-dev": {
"symfony/phpunit-bridge": "~2.7",
"doctrine/collections": "~1.0",
"symfony/validator": "~2.6",
"symfony/validator": "~2.6,>=2.6.8",
"symfony/http-foundation": "~2.2",
"symfony/http-kernel": "~2.4",
"symfony/security-csrf": "~2.4",

View File

@ -82,6 +82,11 @@ CHANGELOG
* added a constraint the uses the expression language
* added `minRatio`, `maxRatio`, `allowSquare`, `allowLandscape`, and `allowPortrait` to Image validator
2.3.29
------
* fixed compatibility with PHP7 and up by introducing new constraints (IsNull, IsTrue, IsFalse) and related validators (IsNullValidator, IsTrueValidator, IsFalseValidator)
2.3.0
-----

View File

@ -11,8 +11,6 @@
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
@ -21,7 +19,4 @@ use Symfony\Component\Validator\Constraint;
*
* @api
*/
class False extends Constraint
{
public $message = 'This value should be false.';
}
class False extends IsFalse {}

View File

@ -11,32 +11,9 @@
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
class FalseValidator extends ConstraintValidator
{
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof False) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\False');
}
if (null === $value || false === $value || 0 === $value || '0' === $value) {
return;
}
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->addViolation();
}
}
class FalseValidator extends IsFalseValidator {}

View File

@ -0,0 +1,27 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
class IsFalse extends Constraint
{
public $message = 'This value should be false.';
}

View File

@ -0,0 +1,42 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
class IsFalseValidator extends ConstraintValidator
{
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof IsFalse) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\IsFalse');
}
if (null === $value || false === $value || 0 === $value || '0' === $value) {
return;
}
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->addViolation();
}
}

View File

@ -0,0 +1,27 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
class IsNull extends Constraint
{
public $message = 'This value should be null.';
}

View File

@ -0,0 +1,40 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
class IsNullValidator extends ConstraintValidator
{
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof IsNull) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\IsNull');
}
if (null !== $value) {
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->addViolation();
}
}
}

View File

@ -0,0 +1,27 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
class IsTrue extends Constraint
{
public $message = 'This value should be true.';
}

View File

@ -0,0 +1,44 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
class IsTrueValidator extends ConstraintValidator
{
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof IsTrue) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\IsTrue');
}
if (null === $value) {
return;
}
if (true !== $value && 1 !== $value && '1' !== $value) {
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->addViolation();
}
}
}

View File

@ -11,8 +11,6 @@
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
@ -21,7 +19,4 @@ use Symfony\Component\Validator\Constraint;
*
* @api
*/
class Null extends Constraint
{
public $message = 'This value should be null.';
}
class Null extends IsNull {}

View File

@ -11,30 +11,9 @@
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
class NullValidator extends ConstraintValidator
{
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Null) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Null');
}
if (null !== $value) {
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->addViolation();
}
}
}
class NullValidator extends IsNullValidator {}

View File

@ -11,8 +11,6 @@
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
@ -21,7 +19,4 @@ use Symfony\Component\Validator\Constraint;
*
* @api
*/
class True extends Constraint
{
public $message = 'This value should be true.';
}
class True extends IsTrue {}

View File

@ -11,34 +11,9 @@
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
class TrueValidator extends ConstraintValidator
{
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof True) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\True');
}
if (null === $value) {
return;
}
if (true !== $value && 1 !== $value && '1' !== $value) {
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->addViolation();
}
}
}
class TrueValidator extends IsTrueValidator {}

View File

@ -81,7 +81,7 @@ class User
}
/**
* @Assert\True(message = "The user should have a Google Mail account")
* @Assert\IsTrue(message = "The user should have a Google Mail account")
*/
public function isGmailUser()
{

View File

@ -11,11 +11,11 @@
namespace Symfony\Component\Validator\Tests\Constraints;
use Symfony\Component\Validator\Constraints\False;
use Symfony\Component\Validator\Constraints\FalseValidator;
use Symfony\Component\Validator\Constraints\IsFalse;
use Symfony\Component\Validator\Constraints\IsFalseValidator;
use Symfony\Component\Validator\Validation;
class FalseValidatorTest extends AbstractConstraintValidatorTest
class IsFalseValidatorTest extends AbstractConstraintValidatorTest
{
protected function getApiVersion()
{
@ -24,26 +24,26 @@ class FalseValidatorTest extends AbstractConstraintValidatorTest
protected function createValidator()
{
return new FalseValidator();
return new IsFalseValidator();
}
public function testNullIsValid()
{
$this->validator->validate(null, new False());
$this->validator->validate(null, new IsFalse());
$this->assertNoViolation();
}
public function testFalseIsValid()
{
$this->validator->validate(false, new False());
$this->validator->validate(false, new IsFalse());
$this->assertNoViolation();
}
public function testTrueIsInvalid()
{
$constraint = new False(array(
$constraint = new IsFalse(array(
'message' => 'myMessage',
));

View File

@ -11,11 +11,11 @@
namespace Symfony\Component\Validator\Tests\Constraints;
use Symfony\Component\Validator\Constraints\Null;
use Symfony\Component\Validator\Constraints\NullValidator;
use Symfony\Component\Validator\Constraints\IsNull;
use Symfony\Component\Validator\Constraints\IsNullValidator;
use Symfony\Component\Validator\Validation;
class NullValidatorTest extends AbstractConstraintValidatorTest
class IsNullValidatorTest extends AbstractConstraintValidatorTest
{
protected function getApiVersion()
{
@ -24,12 +24,12 @@ class NullValidatorTest extends AbstractConstraintValidatorTest
protected function createValidator()
{
return new NullValidator();
return new IsNullValidator();
}
public function testNullIsValid()
{
$this->validator->validate(null, new Null());
$this->validator->validate(null, new IsNull());
$this->assertNoViolation();
}
@ -39,7 +39,7 @@ class NullValidatorTest extends AbstractConstraintValidatorTest
*/
public function testInvalidValues($value, $valueAsString)
{
$constraint = new Null(array(
$constraint = new IsNull(array(
'message' => 'myMessage',
));

View File

@ -11,11 +11,11 @@
namespace Symfony\Component\Validator\Tests\Constraints;
use Symfony\Component\Validator\Constraints\True;
use Symfony\Component\Validator\Constraints\TrueValidator;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\IsTrueValidator;
use Symfony\Component\Validator\Validation;
class TrueValidatorTest extends AbstractConstraintValidatorTest
class IsTrueValidatorTest extends AbstractConstraintValidatorTest
{
protected function getApiVersion()
{
@ -24,26 +24,26 @@ class TrueValidatorTest extends AbstractConstraintValidatorTest
protected function createValidator()
{
return new TrueValidator();
return new IsTrueValidator();
}
public function testNullIsValid()
{
$this->validator->validate(null, new True());
$this->validator->validate(null, new IsTrue());
$this->assertNoViolation();
}
public function testTrueIsValid()
{
$this->validator->validate(true, new True());
$this->validator->validate(true, new IsTrue());
$this->assertNoViolation();
}
public function testFalseIsInvalid()
{
$constraint = new True(array(
$constraint = new IsTrue(array(
'message' => 'myMessage',
));

View File

@ -18,7 +18,7 @@ use Symfony\Component\Validator\Validation;
* @author Bernhard Schussek <bschussek@gmail.com>
* @group legacy
*/
class LegacyFalseValidator2Dot4ApiTest extends FalseValidatorTest
class LegacyFalseValidator2Dot4ApiTest extends IsFalseValidatorTest
{
protected function getApiVersion()
{

View File

@ -18,7 +18,7 @@ use Symfony\Component\Validator\Validation;
* @author Bernhard Schussek <bschussek@gmail.com>
* @group legacy
*/
class LegacyFalseValidatorLegacyApiTest extends FalseValidatorTest
class LegacyFalseValidatorLegacyApiTest extends IsFalseValidatorTest
{
protected function getApiVersion()
{

View File

@ -18,7 +18,7 @@ use Symfony\Component\Validator\Validation;
* @author Bernhard Schussek <bschussek@gmail.com>
* @group legacy
*/
class LegacyNullValidator2Dot4ApiTest extends NullValidatorTest
class LegacyNullValidator2Dot4ApiTest extends IsNullValidatorTest
{
protected function getApiVersion()
{

View File

@ -18,7 +18,7 @@ use Symfony\Component\Validator\Validation;
* @author Bernhard Schussek <bschussek@gmail.com>
* @group legacy
*/
class LegacyNullValidatorLegacyApiTest extends NullValidatorTest
class LegacyNullValidatorLegacyApiTest extends IsNullValidatorTest
{
protected function getApiVersion()
{

View File

@ -18,7 +18,7 @@ use Symfony\Component\Validator\Validation;
* @author Bernhard Schussek <bschussek@gmail.com>
* @group legacy
*/
class LegacyTrueValidator2Dot4ApiTest extends TrueValidatorTest
class LegacyTrueValidator2Dot4ApiTest extends IsTrueValidatorTest
{
protected function getApiVersion()
{

View File

@ -18,7 +18,7 @@ use Symfony\Component\Validator\Validation;
* @author Bernhard Schussek <bschussek@gmail.com>
* @group legacy
*/
class LegacyTrueValidatorLegacyApiTest extends TrueValidatorTest
class LegacyTrueValidatorLegacyApiTest extends IsTrueValidatorTest
{
protected function getApiVersion()
{

View File

@ -64,7 +64,7 @@ class Entity extends EntityParent implements EntityInterface
}
/**
* @Assert\True
* @Assert\IsTrue
*/
public function isValid()
{
@ -72,7 +72,7 @@ class Entity extends EntityParent implements EntityInterface
}
/**
* @Assert\True
* @Assert\IsTrue
*/
public function hasPermissions()
{

View File

@ -18,7 +18,7 @@ use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\True;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
@ -78,8 +78,8 @@ class AnnotationLoaderTest extends \PHPUnit_Framework_TestCase
'choices' => array('A', 'B'),
)));
$expected->addGetterConstraint('lastName', new NotNull());
$expected->addGetterConstraint('valid', new True());
$expected->addGetterConstraint('permissions', new True());
$expected->addGetterConstraint('valid', new IsTrue());
$expected->addGetterConstraint('permissions', new IsTrue());
// load reflection class so that the comparison passes
$expected->getReflectionClass();
@ -147,8 +147,8 @@ class AnnotationLoaderTest extends \PHPUnit_Framework_TestCase
'choices' => array('A', 'B'),
)));
$expected->addGetterConstraint('lastName', new NotNull());
$expected->addGetterConstraint('valid', new True());
$expected->addGetterConstraint('permissions', new True());
$expected->addGetterConstraint('valid', new IsTrue());
$expected->addGetterConstraint('permissions', new IsTrue());
// load reflection class so that the comparison passes
$expected->getReflectionClass();

View File

@ -18,7 +18,7 @@ use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Constraints\True;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Exception\MappingException;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
@ -71,8 +71,8 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
'choices' => array('A', 'B'),
)));
$expected->addGetterConstraint('lastName', new NotNull());
$expected->addGetterConstraint('valid', new True());
$expected->addGetterConstraint('permissions', new True());
$expected->addGetterConstraint('valid', new IsTrue());
$expected->addGetterConstraint('permissions', new IsTrue());
$this->assertEquals($expected, $metadata);
}

View File

@ -17,7 +17,7 @@ use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\True;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
@ -102,8 +102,8 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
'choices' => array('A', 'B'),
)));
$expected->addGetterConstraint('lastName', new NotNull());
$expected->addGetterConstraint('valid', new True());
$expected->addGetterConstraint('permissions', new True());
$expected->addGetterConstraint('valid', new IsTrue());
$expected->addGetterConstraint('permissions', new IsTrue());
$this->assertEquals($expected, $metadata);
}

View File

@ -103,10 +103,10 @@
<constraint name="NotNull" />
</getter>
<getter property="valid">
<constraint name="True" />
<constraint name="IsTrue" />
</getter>
<getter property="permissions">
<constraint name="True" />
<constraint name="IsTrue" />
</getter>
</class>

View File

@ -54,9 +54,9 @@ Symfony\Component\Validator\Tests\Fixtures\Entity:
lastName:
- NotNull: ~
valid:
- "True": ~
- "IsTrue": ~
permissions:
- "True": ~
- "IsTrue": ~
Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity:
group_sequence_provider: true