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