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 ;
@@ -27,19 +28,17 @@ public function testHashWithLegacyUser()
2728 {
2829 $ user = new TestLegacyPasswordAuthenticatedUser ('name ' , null , 'userSalt ' );
2930
30- $ mockHasher = $ this ->createMock (PasswordHasherInterface::class);
31- $ mockHasher -> expects ( $ this -> any ())
31+ $ passwordHasher = $ this ->createStub (PasswordHasherInterface::class);
32+ $ passwordHasher
3233 ->method ('hash ' )
3334 ->with ($ this ->equalTo ('plainPassword ' ), $ this ->equalTo ('userSalt ' ))
3435 ->willReturn ('hash ' );
3536
36- $ mockPasswordHasherFactory = $ this ->createMock (PasswordHasherFactoryInterface::class);
37- $ mockPasswordHasherFactory ->expects ($ this ->any ())
38- ->method ('getPasswordHasher ' )
39- ->with ($ user )
40- ->willReturn ($ mockHasher );
37+ $ passwordHasherFactory = new PasswordHasherFactory ([
38+ $ user ::class => $ passwordHasher ,
39+ ]);
4140
42- $ passwordHasher = new UserPasswordHasher ($ mockPasswordHasherFactory );
41+ $ passwordHasher = new UserPasswordHasher ($ passwordHasherFactory );
4342
4443 $ encoded = $ passwordHasher ->hashPassword ($ user , 'plainPassword ' );
4544 $ this ->assertEquals ('hash ' , $ encoded );
@@ -49,19 +48,17 @@ public function testHashWithPasswordAuthenticatedUser()
4948 {
5049 $ user = new TestPasswordAuthenticatedUser ();
5150
52- $ mockHasher = $ this ->createMock (PasswordHasherInterface::class);
53- $ mockHasher -> expects ( $ this -> any ())
51+ $ passwordHasher = $ this ->createStub (PasswordHasherInterface::class);
52+ $ passwordHasher
5453 ->method ('hash ' )
5554 ->with ($ this ->equalTo ('plainPassword ' ), $ this ->equalTo (null ))
5655 ->willReturn ('hash ' );
5756
58- $ mockPasswordHasherFactory = $ this ->createMock (PasswordHasherFactoryInterface::class);
59- $ mockPasswordHasherFactory ->expects ($ this ->any ())
60- ->method ('getPasswordHasher ' )
61- ->with ($ user )
62- ->willReturn ($ mockHasher );
57+ $ passwordHasherFactory = new PasswordHasherFactory ([
58+ $ user ::class => $ passwordHasher ,
59+ ]);
6360
64- $ passwordHasher = new UserPasswordHasher ($ mockPasswordHasherFactory );
61+ $ passwordHasher = new UserPasswordHasher ($ passwordHasherFactory );
6562
6663 $ hashedPassword = $ passwordHasher ->hashPassword ($ user , 'plainPassword ' );
6764
@@ -72,19 +69,17 @@ public function testVerifyWithLegacyUser()
7269 {
7370 $ user = new TestLegacyPasswordAuthenticatedUser ('user ' , 'hash ' , 'userSalt ' );
7471
75- $ mockHasher = $ this ->createMock (PasswordHasherInterface::class);
76- $ mockHasher -> expects ( $ this -> any ())
72+ $ passwordHasher = $ this ->createStub (PasswordHasherInterface::class);
73+ $ passwordHasher
7774 ->method ('verify ' )
7875 ->with ($ this ->equalTo ('hash ' ), $ this ->equalTo ('plainPassword ' ), $ this ->equalTo ('userSalt ' ))
7976 ->willReturn (true );
8077
81- $ mockPasswordHasherFactory = $ this ->createMock (PasswordHasherFactoryInterface::class);
82- $ mockPasswordHasherFactory ->expects ($ this ->any ())
83- ->method ('getPasswordHasher ' )
84- ->with ($ user )
85- ->willReturn ($ mockHasher );
78+ $ passwordHasherFactory = new PasswordHasherFactory ([
79+ $ user ::class => $ passwordHasher ,
80+ ]);
8681
87- $ passwordHasher = new UserPasswordHasher ($ mockPasswordHasherFactory );
82+ $ passwordHasher = new UserPasswordHasher ($ passwordHasherFactory );
8883
8984 $ isValid = $ passwordHasher ->isPasswordValid ($ user , 'plainPassword ' );
9085 $ this ->assertTrue ($ isValid );
@@ -94,19 +89,17 @@ public function testVerify()
9489 {
9590 $ user = new TestPasswordAuthenticatedUser ('hash ' );
9691
97- $ mockHasher = $ this ->createMock (PasswordHasherInterface::class);
98- $ mockHasher -> expects ( $ this -> any ())
92+ $ passwordHasher = $ this ->createStub (PasswordHasherInterface::class);
93+ $ passwordHasher
9994 ->method ('verify ' )
10095 ->with ($ this ->equalTo ('hash ' ), $ this ->equalTo ('plainPassword ' ), $ this ->equalTo (null ))
10196 ->willReturn (true );
10297
103- $ mockPasswordHasherFactory = $ this ->createMock (PasswordHasherFactoryInterface::class);
104- $ mockPasswordHasherFactory ->expects ($ this ->any ())
105- ->method ('getPasswordHasher ' )
106- ->with ($ user )
107- ->willReturn ($ mockHasher );
98+ $ passwordHasherFactory = new PasswordHasherFactory ([
99+ $ user ::class => $ passwordHasher ,
100+ ]);
108101
109- $ passwordHasher = new UserPasswordHasher ($ mockPasswordHasherFactory );
102+ $ passwordHasher = new UserPasswordHasher ($ passwordHasherFactory );
110103
111104 $ isValid = $ passwordHasher ->isPasswordValid ($ user , 'plainPassword ' );
112105 $ this ->assertTrue ($ isValid );
@@ -117,13 +110,13 @@ public function testNeedsRehash()
117110 $ user = new InMemoryUser ('username ' , null );
118111 $ hasher = new NativePasswordHasher (4 , 20000 , 4 );
119112
120- $ mockPasswordHasherFactory = $ this ->createMock (PasswordHasherFactoryInterface::class);
121- $ mockPasswordHasherFactory -> expects ( $ this -> any ())
113+ $ passwordHasherFactory = $ this ->createStub (PasswordHasherFactoryInterface::class);
114+ $ passwordHasherFactory
122115 ->method ('getPasswordHasher ' )
123116 ->with ($ user )
124117 ->willReturn ($ hasher , $ hasher , new NativePasswordHasher (5 , 20000 , 5 ), $ hasher );
125118
126- $ passwordHasher = new UserPasswordHasher ($ mockPasswordHasherFactory );
119+ $ passwordHasher = new UserPasswordHasher ($ passwordHasherFactory );
127120
128121 \Closure::bind (function () use ($ passwordHasher ) { $ this ->password = $ passwordHasher ->hashPassword ($ this , 'foo ' , 'salt ' ); }, $ user , class_exists (User::class) ? User::class : InMemoryUser::class)();
129122 $ this ->assertFalse ($ passwordHasher ->needsRehash ($ user ));
0 commit comments