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

Skip to content

Commit 845c33c

Browse files
committed
[Validator] Backported constraint validator tests from 2.5
1 parent 054f1b5 commit 845c33c

File tree

3 files changed

+160
-162
lines changed

3 files changed

+160
-162
lines changed

src/Symfony/Component/Security/Core/Tests/Validator/Constraints/UserPasswordValidatorTest.php

Lines changed: 68 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,117 +11,128 @@
1111

1212
namespace Symfony\Component\Security\Core\Tests\Validator\Constraints;
1313

14+
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
15+
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
16+
use Symfony\Component\Security\Core\SecurityContextInterface;
1417
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
1518
use Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator;
19+
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
1620

17-
class UserPasswordValidatorTest extends \PHPUnit_Framework_TestCase
21+
/**
22+
* @author Bernhard Schussek <[email protected]>
23+
*/
24+
class UserPasswordValidatorTest extends AbstractConstraintValidatorTest
1825
{
19-
const PASSWORD_VALID = true;
20-
const PASSWORD_INVALID = false;
26+
const PASSWORD = 's3Cr3t';
2127

22-
protected $context;
28+
const SALT = '^S4lt$';
2329

24-
protected function setUp()
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()
2546
{
26-
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
47+
return new UserPasswordValidator($this->securityContext, $this->encoderFactory);
2748
}
2849

29-
protected function tearDown()
50+
protected function setUp()
3051
{
31-
$this->context = null;
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();
3258
}
3359

3460
public function testPasswordIsValid()
3561
{
36-
$user = $this->createUser();
37-
$securityContext = $this->createSecurityContext($user);
62+
$constraint = new UserPassword(array(
63+
'message' => 'myMessage',
64+
));
3865

39-
$encoder = $this->createPasswordEncoder(static::PASSWORD_VALID);
40-
$encoderFactory = $this->createEncoderFactory($encoder);
41-
42-
$validator = new UserPasswordValidator($securityContext, $encoderFactory);
43-
$validator->initialize($this->context);
66+
$this->encoder->expects($this->once())
67+
->method('isPasswordValid')
68+
->with(static::PASSWORD, 'secret', static::SALT)
69+
->will($this->returnValue(true));
4470

45-
$this
46-
->context
47-
->expects($this->never())
48-
->method('addViolation')
49-
;
71+
$this->validator->validate('secret', $constraint);
5072

51-
$validator->validate('secret', new UserPassword());
73+
$this->assertNoViolation();
5274
}
5375

5476
public function testPasswordIsNotValid()
5577
{
56-
$user = $this->createUser();
57-
$securityContext = $this->createSecurityContext($user);
58-
59-
$encoder = $this->createPasswordEncoder(static::PASSWORD_INVALID);
60-
$encoderFactory = $this->createEncoderFactory($encoder);
78+
$constraint = new UserPassword(array(
79+
'message' => 'myMessage',
80+
));
6181

62-
$validator = new UserPasswordValidator($securityContext, $encoderFactory);
63-
$validator->initialize($this->context);
82+
$this->encoder->expects($this->once())
83+
->method('isPasswordValid')
84+
->with(static::PASSWORD, 'secret', static::SALT)
85+
->will($this->returnValue(false));
6486

65-
$this
66-
->context
67-
->expects($this->once())
68-
->method('addViolation')
69-
;
87+
$this->validator->validate('secret', $constraint);
7088

71-
$validator->validate('secret', new UserPassword());
89+
$this->assertViolation('myMessage');
7290
}
7391

92+
/**
93+
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
94+
*/
7495
public function testUserIsNotValid()
7596
{
76-
$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
77-
7897
$user = $this->getMock('Foo\Bar\User');
79-
$encoderFactory = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface');
80-
$securityContext = $this->createSecurityContext($user);
8198

82-
$validator = new UserPasswordValidator($securityContext, $encoderFactory);
83-
$validator->initialize($this->context);
84-
$validator->validate('secret', new UserPassword());
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());
85104
}
86105

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

91110
$mock
92-
->expects($this->once())
111+
->expects($this->any())
93112
->method('getPassword')
94-
->will($this->returnValue('s3Cr3t'))
113+
->will($this->returnValue(static::PASSWORD))
95114
;
96115

97116
$mock
98-
->expects($this->once())
117+
->expects($this->any())
99118
->method('getSalt')
100-
->will($this->returnValue('^S4lt$'))
119+
->will($this->returnValue(static::SALT))
101120
;
102121

103122
return $mock;
104123
}
105124

106125
protected function createPasswordEncoder($isPasswordValid = true)
107126
{
108-
$mock = $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface');
109-
110-
$mock
111-
->expects($this->once())
112-
->method('isPasswordValid')
113-
->will($this->returnValue($isPasswordValid))
114-
;
115-
116-
return $mock;
127+
return $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface');
117128
}
118129

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

123134
$mock
124-
->expects($this->once())
135+
->expects($this->any())
125136
->method('getEncoder')
126137
->will($this->returnValue($encoder))
127138
;
@@ -135,7 +146,7 @@ protected function createSecurityContext($user = null)
135146

136147
$mock = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
137148
$mock
138-
->expects($this->once())
149+
->expects($this->any())
139150
->method('getToken')
140151
->will($this->returnValue($token))
141152
;
@@ -147,7 +158,7 @@ protected function createAuthenticationToken($user = null)
147158
{
148159
$mock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
149160
$mock
150-
->expects($this->once())
161+
->expects($this->any())
151162
->method('getUser')
152163
->will($this->returnValue($user))
153164
;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function validate($value, Constraint $constraint)
5151
$variables = array();
5252

5353
if (null === $this->context->getPropertyName()) {
54+
$variables['value'] = $value;
5455
$variables['this'] = $value;
5556
} else {
5657
// Extract the object that the property belongs to from the object

0 commit comments

Comments
 (0)