1313
1414use PHPUnit \Framework \TestCase ;
1515use Symfony \Component \PasswordHasher \Hasher \NativePasswordHasher ;
16+ use Symfony \Component \PasswordHasher \Hasher \PasswordHasherFactory ;
1617use Symfony \Component \PasswordHasher \Hasher \PasswordHasherFactoryInterface ;
1718use Symfony \Component \PasswordHasher \Hasher \UserPasswordHasher ;
1819use Symfony \Component \PasswordHasher \PasswordHasherInterface ;
@@ -26,19 +27,17 @@ public function testHashWithLegacyUser()
2627 {
2728 $ user = new TestLegacyPasswordAuthenticatedUser ('name ' , null , 'userSalt ' );
2829
29- $ mockHasher = $ this ->createMock (PasswordHasherInterface::class);
30- $ mockHasher -> expects ( $ this -> any ())
30+ $ passwordHasher = $ this ->createStub (PasswordHasherInterface::class);
31+ $ passwordHasher
3132 ->method ('hash ' )
3233 ->with ($ this ->equalTo ('plainPassword ' ), $ this ->equalTo ('userSalt ' ))
3334 ->willReturn ('hash ' );
3435
35- $ mockPasswordHasherFactory = $ this ->createMock (PasswordHasherFactoryInterface::class);
36- $ mockPasswordHasherFactory ->expects ($ this ->any ())
37- ->method ('getPasswordHasher ' )
38- ->with ($ user )
39- ->willReturn ($ mockHasher );
36+ $ passwordHasherFactory = new PasswordHasherFactory ([
37+ $ user ::class => $ passwordHasher ,
38+ ]);
4039
41- $ passwordHasher = new UserPasswordHasher ($ mockPasswordHasherFactory );
40+ $ passwordHasher = new UserPasswordHasher ($ passwordHasherFactory );
4241
4342 $ encoded = $ passwordHasher ->hashPassword ($ user , 'plainPassword ' );
4443 $ this ->assertEquals ('hash ' , $ encoded );
@@ -48,19 +47,17 @@ public function testHashWithPasswordAuthenticatedUser()
4847 {
4948 $ user = new TestPasswordAuthenticatedUser ();
5049
51- $ mockHasher = $ this ->createMock (PasswordHasherInterface::class);
52- $ mockHasher -> expects ( $ this -> any ())
50+ $ passwordHasher = $ this ->createStub (PasswordHasherInterface::class);
51+ $ passwordHasher
5352 ->method ('hash ' )
5453 ->with ($ this ->equalTo ('plainPassword ' ), $ this ->equalTo (null ))
5554 ->willReturn ('hash ' );
5655
57- $ mockPasswordHasherFactory = $ this ->createMock (PasswordHasherFactoryInterface::class);
58- $ mockPasswordHasherFactory ->expects ($ this ->any ())
59- ->method ('getPasswordHasher ' )
60- ->with ($ user )
61- ->willReturn ($ mockHasher );
56+ $ passwordHasherFactory = new PasswordHasherFactory ([
57+ $ user ::class => $ passwordHasher ,
58+ ]);
6259
63- $ passwordHasher = new UserPasswordHasher ($ mockPasswordHasherFactory );
60+ $ passwordHasher = new UserPasswordHasher ($ passwordHasherFactory );
6461
6562 $ hashedPassword = $ passwordHasher ->hashPassword ($ user , 'plainPassword ' );
6663
@@ -71,19 +68,17 @@ public function testVerifyWithLegacyUser()
7168 {
7269 $ user = new TestLegacyPasswordAuthenticatedUser ('user ' , 'hash ' , 'userSalt ' );
7370
74- $ mockHasher = $ this ->createMock (PasswordHasherInterface::class);
75- $ mockHasher -> expects ( $ this -> any ())
71+ $ passwordHasher = $ this ->createStub (PasswordHasherInterface::class);
72+ $ passwordHasher
7673 ->method ('verify ' )
7774 ->with ($ this ->equalTo ('hash ' ), $ this ->equalTo ('plainPassword ' ), $ this ->equalTo ('userSalt ' ))
7875 ->willReturn (true );
7976
80- $ mockPasswordHasherFactory = $ this ->createMock (PasswordHasherFactoryInterface::class);
81- $ mockPasswordHasherFactory ->expects ($ this ->any ())
82- ->method ('getPasswordHasher ' )
83- ->with ($ user )
84- ->willReturn ($ mockHasher );
77+ $ passwordHasherFactory = new PasswordHasherFactory ([
78+ $ user ::class => $ passwordHasher ,
79+ ]);
8580
86- $ passwordHasher = new UserPasswordHasher ($ mockPasswordHasherFactory );
81+ $ passwordHasher = new UserPasswordHasher ($ passwordHasherFactory );
8782
8883 $ isValid = $ passwordHasher ->isPasswordValid ($ user , 'plainPassword ' );
8984 $ this ->assertTrue ($ isValid );
@@ -93,19 +88,17 @@ public function testVerify()
9388 {
9489 $ user = new TestPasswordAuthenticatedUser ('hash ' );
9590
96- $ mockHasher = $ this ->createMock (PasswordHasherInterface::class);
97- $ mockHasher -> expects ( $ this -> any ())
91+ $ passwordHasher = $ this ->createStub (PasswordHasherInterface::class);
92+ $ passwordHasher
9893 ->method ('verify ' )
9994 ->with ($ this ->equalTo ('hash ' ), $ this ->equalTo ('plainPassword ' ), $ this ->equalTo (null ))
10095 ->willReturn (true );
10196
102- $ mockPasswordHasherFactory = $ this ->createMock (PasswordHasherFactoryInterface::class);
103- $ mockPasswordHasherFactory ->expects ($ this ->any ())
104- ->method ('getPasswordHasher ' )
105- ->with ($ user )
106- ->willReturn ($ mockHasher );
97+ $ passwordHasherFactory = new PasswordHasherFactory ([
98+ $ user ::class => $ passwordHasher ,
99+ ]);
107100
108- $ passwordHasher = new UserPasswordHasher ($ mockPasswordHasherFactory );
101+ $ passwordHasher = new UserPasswordHasher ($ passwordHasherFactory );
109102
110103 $ isValid = $ passwordHasher ->isPasswordValid ($ user , 'plainPassword ' );
111104 $ this ->assertTrue ($ isValid );
@@ -116,13 +109,13 @@ public function testNeedsRehash()
116109 $ user = new InMemoryUser ('username ' , null );
117110 $ hasher = new NativePasswordHasher (4 , 20000 , 4 );
118111
119- $ mockPasswordHasherFactory = $ this ->createMock (PasswordHasherFactoryInterface::class);
120- $ mockPasswordHasherFactory -> expects ( $ this -> any ())
112+ $ passwordHasherFactory = $ this ->createStub (PasswordHasherFactoryInterface::class);
113+ $ passwordHasherFactory
121114 ->method ('getPasswordHasher ' )
122115 ->with ($ user )
123116 ->willReturn ($ hasher , $ hasher , new NativePasswordHasher (5 , 20000 , 5 ), $ hasher );
124117
125- $ passwordHasher = new UserPasswordHasher ($ mockPasswordHasherFactory );
118+ $ passwordHasher = new UserPasswordHasher ($ passwordHasherFactory );
126119
127120 \Closure::bind (function () use ($ passwordHasher ) { $ this ->password = $ passwordHasher ->hashPassword ($ this , 'foo ' , 'salt ' ); }, $ user , InMemoryUser::class)();
128121 $ this ->assertFalse ($ passwordHasher ->needsRehash ($ user ));
0 commit comments