Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 87a47ea

Browse files
committed
[Validator] Backported constraint validator tests from 2.5
1 parent 48b150a commit 87a47ea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2134
-2159
lines changed

src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php

Lines changed: 414 additions & 0 deletions
Large diffs are not rendered by default.

src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueValidatorTest.php

Lines changed: 0 additions & 437 deletions
This file was deleted.

src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php

Lines changed: 105 additions & 189 deletions
Large diffs are not rendered by default.
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Security\Core\Tests\Validator\Constraints;
13+
14+
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
15+
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
16+
use Symfony\Component\Security\Core\SecurityContextInterface;
17+
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
18+
use Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator;
19+
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
20+
21+
/**
22+
* @author Bernhard Schussek <[email protected]>
23+
*/
24+
class UserPasswordValidatorTest extends AbstractConstraintValidatorTest
25+
{
26+
const PASSWORD = 's3Cr3t';
27+
28+
const SALT = '^S4lt$';
29+
30+
/**
31+
* @var SecurityContextInterface
32+
*/
33+
protected $securityContext;
34+
35+
/**
36+
* @var PasswordEncoderInterface
37+
*/
38+
protected $encoder;
39+
40+
/**
41+
* @var EncoderFactoryInterface
42+
*/
43+
protected $encoderFactory;
44+
45+
protected function createValidator()
46+
{
47+
return new UserPasswordValidator($this->securityContext, $this->encoderFactory);
48+
}
49+
50+
protected function setUp()
51+
{
52+
$user = $this->createUser();
53+
$this->securityContext = $this->createSecurityContext($user);
54+
$this->encoder = $this->createPasswordEncoder();
55+
$this->encoderFactory = $this->createEncoderFactory($this->encoder);
56+
57+
parent::setUp();
58+
}
59+
60+
public function testPasswordIsValid()
61+
{
62+
$constraint = new UserPassword(array(
63+
'message' => 'myMessage',
64+
));
65+
66+
$this->encoder->expects($this->once())
67+
->method('isPasswordValid')
68+
->with(static::PASSWORD, 'secret', static::SALT)
69+
->will($this->returnValue(true));
70+
71+
$this->validator->validate('secret', $constraint);
72+
73+
$this->assertNoViolation();
74+
}
75+
76+
public function testPasswordIsNotValid()
77+
{
78+
$constraint = new UserPassword(array(
79+
'message' => 'myMessage',
80+
));
81+
82+
$this->encoder->expects($this->once())
83+
->method('isPasswordValid')
84+
->with(static::PASSWORD, 'secret', static::SALT)
85+
->will($this->returnValue(false));
86+
87+
$this->validator->validate('secret', $constraint);
88+
89+
$this->assertViolation('myMessage');
90+
}
91+
92+
/**
93+
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
94+
*/
95+
public function testUserIsNotValid()
96+
{
97+
$user = $this->getMock('Foo\Bar\User');
98+
99+
$this->securityContext = $this->createSecurityContext($user);
100+
$this->validator = $this->createValidator();
101+
$this->validator->initialize($this->context);
102+
103+
$this->validator->validate('secret', new UserPassword());
104+
}
105+
106+
protected function createUser()
107+
{
108+
$mock = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
109+
110+
$mock
111+
->expects($this->any())
112+
->method('getPassword')
113+
->will($this->returnValue(static::PASSWORD))
114+
;
115+
116+
$mock
117+
->expects($this->any())
118+
->method('getSalt')
119+
->will($this->returnValue(static::SALT))
120+
;
121+
122+
return $mock;
123+
}
124+
125+
protected function createPasswordEncoder($isPasswordValid = true)
126+
{
127+
return $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface');
128+
}
129+
130+
protected function createEncoderFactory($encoder = null)
131+
{
132+
$mock = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface');
133+
134+
$mock
135+
->expects($this->any())
136+
->method('getEncoder')
137+
->will($this->returnValue($encoder))
138+
;
139+
140+
return $mock;
141+
}
142+
143+
protected function createSecurityContext($user = null)
144+
{
145+
$token = $this->createAuthenticationToken($user);
146+
147+
$mock = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
148+
$mock
149+
->expects($this->any())
150+
->method('getToken')
151+
->will($this->returnValue($token))
152+
;
153+
154+
return $mock;
155+
}
156+
157+
protected function createAuthenticationToken($user = null)
158+
{
159+
$mock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
160+
$mock
161+
->expects($this->any())
162+
->method('getUser')
163+
->will($this->returnValue($user))
164+
;
165+
166+
return $mock;
167+
}
168+
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ public function validate($value, Constraint $constraint)
3838
$group = $this->context->getGroup();
3939

4040
foreach ($value as $key => $element) {
41-
foreach ($constraint->constraints as $constr) {
42-
$this->context->validateValue($element, $constr, '['.$key.']', $group);
43-
}
41+
$this->context->validateValue($element, $constraint->constraints, '['.$key.']', $group);
4442
}
4543
}
4644
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ public function validate($value, Constraint $constraint)
7070
if ($constraint->min !== null && $count < $constraint->min) {
7171
$this->context->addViolation($constraint->minMessage, array(
7272
'{{ limit }}' => $constraint->min
73-
), null, (int) $constraint->min);
73+
), $value, (int) $constraint->min);
7474

7575
return;
7676
}
7777

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

8383
return;
8484
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ public function validate($value, Constraint $constraint)
4343
(is_array($value) && array_key_exists($field, $value)) ||
4444
($value instanceof \ArrayAccess && $value->offsetExists($field))
4545
) {
46-
foreach ($fieldConstraint->constraints as $constr) {
47-
$this->context->validateValue($value[$field], $constr, '['.$field.']', $group);
48-
}
46+
$this->context->validateValue($value[$field], $fieldConstraint->constraints, '['.$field.']', $group);
4947
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
5048
$this->context->addViolationAt('['.$field.']', $constraint->missingFieldsMessage, array(
5149
'{{ field }}' => $this->formatValue($field)

src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\Intl\Util\IntlTestHelper;
1515
use Symfony\Component\Validator\Constraint;
16-
use Symfony\Component\Validator\Constraints\AbstractComparisonValidator;
1716

1817
class ComparisonTest_Class
1918
{
@@ -33,32 +32,15 @@ public function __toString()
3332
/**
3433
* @author Daniel Holmes <[email protected]>
3534
*/
36-
abstract class AbstractComparisonValidatorTestCase extends \PHPUnit_Framework_TestCase
35+
abstract class AbstractComparisonValidatorTestCase extends AbstractConstraintValidatorTest
3736
{
38-
private $validator;
39-
private $context;
40-
41-
protected function setUp()
42-
{
43-
$this->validator = $this->createValidator();
44-
$this->context = $this->getMockBuilder('Symfony\Component\Validator\ExecutionContext')
45-
->disableOriginalConstructor()
46-
->getMock();
47-
$this->validator->initialize($this->context);
48-
49-
\Locale::setDefault('en');
50-
}
51-
5237
/**
53-
* @return AbstractComparisonValidator
38+
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
5439
*/
55-
abstract protected function createValidator();
56-
5740
public function testThrowsConstraintExceptionIfNoValueOrProperty()
5841
{
59-
$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
60-
6142
$comparison = $this->createConstraint(array());
43+
6244
$this->validator->validate('some value', $comparison);
6345
}
6446

@@ -69,16 +51,11 @@ public function testThrowsConstraintExceptionIfNoValueOrProperty()
6951
*/
7052
public function testValidComparisonToValue($dirtyValue, $comparisonValue)
7153
{
72-
$this->context->expects($this->never())
73-
->method('addViolation');
74-
7554
$constraint = $this->createConstraint(array('value' => $comparisonValue));
7655

77-
$this->context->expects($this->any())
78-
->method('getPropertyPath')
79-
->will($this->returnValue('property1'));
80-
8156
$this->validator->validate($dirtyValue, $constraint);
57+
58+
$this->assertNoViolation();
8259
}
8360

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

108-
$this->context->expects($this->any())
109-
->method('getPropertyPath')
110-
->will($this->returnValue('property1'));
111-
112-
$this->context->expects($this->once())
113-
->method('addViolation')
114-
->with('Constraint Message', array(
115-
'{{ value }}' => $dirtyValueAsString,
116-
'{{ compared_value }}' => $comparedValueString,
117-
'{{ compared_value_type }}' => $comparedValueType
118-
));
119-
12085
$this->validator->validate($dirtyValue, $constraint);
86+
87+
$this->assertViolation('Constraint Message', array(
88+
'{{ value }}' => $dirtyValueAsString,
89+
'{{ compared_value }}' => $comparedValueString,
90+
'{{ compared_value_type }}' => $comparedValueType
91+
));
12192
}
12293

12394
/**

0 commit comments

Comments
 (0)