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