Skip to content

Commit fbdfa5a

Browse files
committed
do not use PHPUnit mock objects without configured expectations
1 parent dcab5ac commit fbdfa5a

4 files changed

Lines changed: 37 additions & 45 deletions

File tree

Tests/Command/UserPasswordHashCommandTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\Component\PasswordHasher\Command\UserPasswordHashCommand;
1818
use Symfony\Component\PasswordHasher\Hasher\NativePasswordHasher;
1919
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactory;
20-
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
2120
use Symfony\Component\PasswordHasher\Hasher\Pbkdf2PasswordHasher;
2221
use Symfony\Component\PasswordHasher\Hasher\SodiumPasswordHasher;
2322
use Symfony\Component\Security\Core\User\InMemoryUser;
@@ -280,7 +279,7 @@ public function testThrowsExceptionOnNoConfiguredHashers()
280279
$this->expectException(\RuntimeException::class);
281280
$this->expectExceptionMessage('There are no configured password hashers for the "security" extension.');
282281

283-
$tester = new CommandTester(new UserPasswordHashCommand($this->getMockBuilder(PasswordHasherFactoryInterface::class)->getMock(), []));
282+
$tester = new CommandTester(new UserPasswordHashCommand(new PasswordHasherFactory([]), []));
284283
$tester->execute([
285284
'password' => 'password',
286285
], ['interactive' => false]);
@@ -291,7 +290,7 @@ public function testThrowsExceptionOnNoConfiguredHashers()
291290
*/
292291
public function testCompletionSuggestions(array $input, array $expectedSuggestions)
293292
{
294-
$command = new UserPasswordHashCommand($this->createMock(PasswordHasherFactoryInterface::class), ['App\Entity\User']);
293+
$command = new UserPasswordHashCommand(new PasswordHasherFactory([]), ['App\Entity\User']);
295294
$tester = new CommandCompletionTester($command);
296295

297296
$this->assertSame($expectedSuggestions, $tester->complete($input));

Tests/Hasher/MigratingPasswordHasherTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public function testFallback()
4242
{
4343
$bestHasher = new NativePasswordHasher(4, 12000, 4);
4444

45-
$extraHasher1 = $this->createMock(PasswordHasherInterface::class);
46-
$extraHasher1->expects($this->any())
45+
$extraHasher1 = $this->createStub(PasswordHasherInterface::class);
46+
$extraHasher1
4747
->method('verify')
4848
->with('abc', 'foo', 'salt')
4949
->willReturn(true);
@@ -52,8 +52,8 @@ public function testFallback()
5252

5353
$this->assertTrue($hasher->verify('abc', 'foo', 'salt'));
5454

55-
$extraHasher2 = $this->createMock(PasswordHasherInterface::class);
56-
$extraHasher2->expects($this->any())
55+
$extraHasher2 = $this->createStub(PasswordHasherInterface::class);
56+
$extraHasher2
5757
->method('verify')
5858
->willReturn(false);
5959

Tests/Hasher/PasswordHasherFactoryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testGetHasherWithMessageDigestHasher()
3131
'arguments' => ['sha512', true, 5],
3232
]]);
3333

34-
$hasher = $factory->getPasswordHasher($this->createMock(PasswordAuthenticatedUserInterface::class));
34+
$hasher = $factory->getPasswordHasher($this->createStub(PasswordAuthenticatedUserInterface::class));
3535
$expectedHasher = new MessageDigestPasswordHasher('sha512', true, 5);
3636

3737
$this->assertEquals($expectedHasher->hash('foo', 'moo'), $hasher->hash('foo', 'moo'));
@@ -43,7 +43,7 @@ public function testGetHasherWithService()
4343
PasswordAuthenticatedUserInterface::class => new MessageDigestPasswordHasher('sha1'),
4444
]);
4545

46-
$hasher = $factory->getPasswordHasher($this->createMock(PasswordAuthenticatedUserInterface::class));
46+
$hasher = $factory->getPasswordHasher($this->createStub(PasswordAuthenticatedUserInterface::class));
4747
$expectedHasher = new MessageDigestPasswordHasher('sha1');
4848
$this->assertEquals($expectedHasher->hash('foo', ''), $hasher->hash('foo', ''));
4949
}
@@ -54,7 +54,7 @@ public function testGetHasherWithInstance()
5454
PasswordAuthenticatedUserInterface::class => ['instance' => new MessageDigestPasswordHasher('sha1')],
5555
]);
5656

57-
$hasher = $factory->getPasswordHasher($this->createMock(PasswordAuthenticatedUserInterface::class));
57+
$hasher = $factory->getPasswordHasher($this->createStub(PasswordAuthenticatedUserInterface::class));
5858
$expectedHasher = new MessageDigestPasswordHasher('sha1');
5959
$this->assertEquals($expectedHasher->hash('foo', ''), $hasher->hash('foo', ''));
6060
}

Tests/Hasher/UserPasswordHasherTest.php

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\PasswordHasher\Hasher\NativePasswordHasher;
16+
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactory;
1617
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
1718
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasher;
1819
use 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

Comments
 (0)