Skip to content

Commit

Permalink
[Validator] Backported constraint validator tests from 2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
webmozart committed Aug 14, 2014
1 parent 48b150a commit 87a47ea
Show file tree
Hide file tree
Showing 51 changed files with 2,134 additions and 2,159 deletions.

Large diffs are not rendered by default.

This file was deleted.

Large diffs are not rendered by default.

@@ -0,0 +1,168 @@
<?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\Security\Core\Tests\Validator\Constraints;

use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
use Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator;
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class UserPasswordValidatorTest extends AbstractConstraintValidatorTest
{
const PASSWORD = 's3Cr3t';

const SALT = '^S4lt$';

/**
* @var SecurityContextInterface
*/
protected $securityContext;

/**
* @var PasswordEncoderInterface
*/
protected $encoder;

/**
* @var EncoderFactoryInterface
*/
protected $encoderFactory;

protected function createValidator()
{
return new UserPasswordValidator($this->securityContext, $this->encoderFactory);
}

protected function setUp()
{
$user = $this->createUser();
$this->securityContext = $this->createSecurityContext($user);
$this->encoder = $this->createPasswordEncoder();
$this->encoderFactory = $this->createEncoderFactory($this->encoder);

parent::setUp();
}

public function testPasswordIsValid()
{
$constraint = new UserPassword(array(
'message' => 'myMessage',
));

$this->encoder->expects($this->once())
->method('isPasswordValid')
->with(static::PASSWORD, 'secret', static::SALT)
->will($this->returnValue(true));

$this->validator->validate('secret', $constraint);

$this->assertNoViolation();
}

public function testPasswordIsNotValid()
{
$constraint = new UserPassword(array(
'message' => 'myMessage',
));

$this->encoder->expects($this->once())
->method('isPasswordValid')
->with(static::PASSWORD, 'secret', static::SALT)
->will($this->returnValue(false));

$this->validator->validate('secret', $constraint);

$this->assertViolation('myMessage');
}

/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
public function testUserIsNotValid()
{
$user = $this->getMock('Foo\Bar\User');

$this->securityContext = $this->createSecurityContext($user);
$this->validator = $this->createValidator();
$this->validator->initialize($this->context);

$this->validator->validate('secret', new UserPassword());
}

protected function createUser()
{
$mock = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');

$mock
->expects($this->any())
->method('getPassword')
->will($this->returnValue(static::PASSWORD))
;

$mock
->expects($this->any())
->method('getSalt')
->will($this->returnValue(static::SALT))
;

return $mock;
}

protected function createPasswordEncoder($isPasswordValid = true)
{
return $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface');
}

protected function createEncoderFactory($encoder = null)
{
$mock = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface');

$mock
->expects($this->any())
->method('getEncoder')
->will($this->returnValue($encoder))
;

return $mock;
}

protected function createSecurityContext($user = null)
{
$token = $this->createAuthenticationToken($user);

$mock = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
$mock
->expects($this->any())
->method('getToken')
->will($this->returnValue($token))
;

return $mock;
}

protected function createAuthenticationToken($user = null)
{
$mock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
$mock
->expects($this->any())
->method('getUser')
->will($this->returnValue($user))
;

return $mock;
}
}
4 changes: 1 addition & 3 deletions src/Symfony/Component/Validator/Constraints/AllValidator.php
Expand Up @@ -38,9 +38,7 @@ public function validate($value, Constraint $constraint)
$group = $this->context->getGroup();

foreach ($value as $key => $element) {
foreach ($constraint->constraints as $constr) {
$this->context->validateValue($element, $constr, '['.$key.']', $group);
}
$this->context->validateValue($element, $constraint->constraints, '['.$key.']', $group);
}
}
}
Expand Up @@ -70,15 +70,15 @@ public function validate($value, Constraint $constraint)
if ($constraint->min !== null && $count < $constraint->min) {
$this->context->addViolation($constraint->minMessage, array(
'{{ limit }}' => $constraint->min
), null, (int) $constraint->min);
), $value, (int) $constraint->min);

return;
}

if ($constraint->max !== null && $count > $constraint->max) {
$this->context->addViolation($constraint->maxMessage, array(
'{{ limit }}' => $constraint->max
), null, (int) $constraint->max);
), $value, (int) $constraint->max);

return;
}
Expand Down
Expand Up @@ -43,9 +43,7 @@ public function validate($value, Constraint $constraint)
(is_array($value) && array_key_exists($field, $value)) ||
($value instanceof \ArrayAccess && $value->offsetExists($field))
) {
foreach ($fieldConstraint->constraints as $constr) {
$this->context->validateValue($value[$field], $constr, '['.$field.']', $group);
}
$this->context->validateValue($value[$field], $fieldConstraint->constraints, '['.$field.']', $group);
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
$this->context->addViolationAt('['.$field.']', $constraint->missingFieldsMessage, array(
'{{ field }}' => $this->formatValue($field)
Expand Down
Expand Up @@ -13,7 +13,6 @@

use Symfony\Component\Intl\Util\IntlTestHelper;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\AbstractComparisonValidator;

class ComparisonTest_Class
{
Expand All @@ -33,32 +32,15 @@ public function __toString()
/**
* @author Daniel Holmes <daniel@danielholmes.org>
*/
abstract class AbstractComparisonValidatorTestCase extends \PHPUnit_Framework_TestCase
abstract class AbstractComparisonValidatorTestCase extends AbstractConstraintValidatorTest
{
private $validator;
private $context;

protected function setUp()
{
$this->validator = $this->createValidator();
$this->context = $this->getMockBuilder('Symfony\Component\Validator\ExecutionContext')
->disableOriginalConstructor()
->getMock();
$this->validator->initialize($this->context);

\Locale::setDefault('en');
}

/**
* @return AbstractComparisonValidator
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
abstract protected function createValidator();

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

$comparison = $this->createConstraint(array());

$this->validator->validate('some value', $comparison);
}

Expand All @@ -69,16 +51,11 @@ public function testThrowsConstraintExceptionIfNoValueOrProperty()
*/
public function testValidComparisonToValue($dirtyValue, $comparisonValue)
{
$this->context->expects($this->never())
->method('addViolation');

$constraint = $this->createConstraint(array('value' => $comparisonValue));

$this->context->expects($this->any())
->method('getPropertyPath')
->will($this->returnValue('property1'));

$this->validator->validate($dirtyValue, $constraint);

$this->assertNoViolation();
}

/**
Expand All @@ -105,19 +82,13 @@ public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $
$constraint = $this->createConstraint(array('value' => $comparedValue));
$constraint->message = 'Constraint Message';

$this->context->expects($this->any())
->method('getPropertyPath')
->will($this->returnValue('property1'));

$this->context->expects($this->once())
->method('addViolation')
->with('Constraint Message', array(
'{{ value }}' => $dirtyValueAsString,
'{{ compared_value }}' => $comparedValueString,
'{{ compared_value_type }}' => $comparedValueType
));

$this->validator->validate($dirtyValue, $constraint);

$this->assertViolation('Constraint Message', array(
'{{ value }}' => $dirtyValueAsString,
'{{ compared_value }}' => $comparedValueString,
'{{ compared_value_type }}' => $comparedValueType
));
}

/**
Expand Down

0 comments on commit 87a47ea

Please sign in to comment.