Skip to content

Commit 6e206bc

Browse files
committed
minor #40609 [Security] Use concrete UserInterface and UserProviderInterface classes in the tests (wouterj)
This PR was merged into the 5.2 branch. Discussion ---------- [Security] Use concrete UserInterface and UserProviderInterface classes in the tests | Q | A | ------------- | --- | Branch? | 5.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | n/a Fixes failing deps=high builds in #40403 Commits ------- 89d9de2 Use concrete user related classes in the tests
2 parents d33973b + 89d9de2 commit 6e206bc

11 files changed

+100
-181
lines changed

src/Symfony/Component/Security/Http/Tests/Authenticator/Fixtures/PasswordUpgraderProvider.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@
1111

1212
namespace Symfony\Component\Security\Http\Tests\Authenticator\Fixtures;
1313

14+
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
1415
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
16+
use Symfony\Component\Security\Core\User\UserInterface;
1517
use Symfony\Component\Security\Core\User\UserProviderInterface;
1618

17-
abstract class PasswordUpgraderProvider implements UserProviderInterface, PasswordUpgraderInterface
19+
class PasswordUpgraderProvider extends InMemoryUserProvider implements PasswordUpgraderInterface
1820
{
21+
public function upgradePassword(UserInterface $user, string $newEncodedPassword): void
22+
{
23+
}
1924
}

src/Symfony/Component/Security/Http/Tests/Authenticator/FormLoginAuthenticatorTest.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
1818
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
1919
use Symfony\Component\Security\Core\Security;
20-
use Symfony\Component\Security\Core\User\User;
21-
use Symfony\Component\Security\Core\User\UserProviderInterface;
20+
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
2221
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
2322
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
2423
use Symfony\Component\Security\Http\Authenticator\FormLoginAuthenticator;
@@ -37,8 +36,7 @@ class FormLoginAuthenticatorTest extends TestCase
3736

3837
protected function setUp(): void
3938
{
40-
$this->userProvider = $this->createMock(UserProviderInterface::class);
41-
$this->userProvider->expects($this->any())->method('loadUserByUsername')->willReturn(new User('test', 's$cr$t'));
39+
$this->userProvider = new InMemoryUserProvider(['test' => ['password' => 's$cr$t']]);
4240
$this->successHandler = $this->createMock(AuthenticationSuccessHandlerInterface::class);
4341
$this->failureHandler = $this->createMock(AuthenticationFailureHandlerInterface::class);
4442
}
@@ -149,8 +147,7 @@ public function testUpgradePassword()
149147
$request = Request::create('/login_check', 'POST', ['_username' => 'wouter', '_password' => 's$cr$t']);
150148
$request->setSession($this->createSession());
151149

152-
$this->userProvider = $this->createMock(PasswordUpgraderProvider::class);
153-
$this->userProvider->expects($this->any())->method('loadUserByUsername')->willReturn(new User('test', 's$cr$t'));
150+
$this->userProvider = new PasswordUpgraderProvider(['test' => ['password' => 's$cr$t']]);
154151

155152
$this->setUpAuthenticator();
156153
$passport = $this->authenticator->authenticate($request);

src/Symfony/Component/Security/Http/Tests/Authenticator/HttpBasicAuthenticatorTest.php

+5-10
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
use Symfony\Component\HttpFoundation\Request;
77
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
88
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
9+
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
910
use Symfony\Component\Security\Core\User\User;
10-
use Symfony\Component\Security\Core\User\UserProviderInterface;
1111
use Symfony\Component\Security\Http\Authenticator\HttpBasicAuthenticator;
1212
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordUpgradeBadge;
1313
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
@@ -22,7 +22,7 @@ class HttpBasicAuthenticatorTest extends TestCase
2222

2323
protected function setUp(): void
2424
{
25-
$this->userProvider = $this->createMock(UserProviderInterface::class);
25+
$this->userProvider = new InMemoryUserProvider();
2626
$this->encoderFactory = $this->createMock(EncoderFactoryInterface::class);
2727
$this->encoder = $this->createMock(PasswordEncoderInterface::class);
2828
$this->encoderFactory
@@ -40,16 +40,12 @@ public function testExtractCredentialsAndUserFromRequest()
4040
'PHP_AUTH_PW' => 'ThePassword',
4141
]);
4242

43-
$this->userProvider
44-
->expects($this->any())
45-
->method('loadUserByUsername')
46-
->with('TheUsername')
47-
->willReturn($user = new User('TheUsername', 'ThePassword'));
43+
$this->userProvider->createUser($user = new User('TheUsername', 'ThePassword'));
4844

4945
$passport = $this->authenticator->authenticate($request);
5046
$this->assertEquals('ThePassword', $passport->getBadge(PasswordCredentials::class)->getPassword());
5147

52-
$this->assertSame($user, $passport->getUser());
48+
$this->assertTrue($user->isEqualTo($passport->getUser()));
5349
}
5450

5551
/**
@@ -77,8 +73,7 @@ public function testUpgradePassword()
7773
'PHP_AUTH_PW' => 'ThePassword',
7874
]);
7975

80-
$this->userProvider = $this->createMock(PasswordUpgraderProvider::class);
81-
$this->userProvider->expects($this->any())->method('loadUserByUsername')->willReturn(new User('test', 's$cr$t'));
76+
$this->userProvider = new PasswordUpgraderProvider(['test' => ['password' => 's$cr$t']]);
8277
$authenticator = new HttpBasicAuthenticator('test', $this->userProvider);
8378

8479
$passport = $authenticator->authenticate($request);

src/Symfony/Component/Security/Http/Tests/Authenticator/RemoteUserAuthenticatorTest.php

+8-11
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
17+
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
1718
use Symfony\Component\Security\Core\User\User;
18-
use Symfony\Component\Security\Core\User\UserProviderInterface;
1919
use Symfony\Component\Security\Http\Authenticator\RemoteUserAuthenticator;
2020

2121
class RemoteUserAuthenticatorTest extends TestCase
2222
{
2323
/**
2424
* @dataProvider provideAuthenticators
2525
*/
26-
public function testSupport(UserProviderInterface $userProvider, RemoteUserAuthenticator $authenticator, $parameterName)
26+
public function testSupport(InMemoryUserProvider $userProvider, RemoteUserAuthenticator $authenticator, $parameterName)
2727
{
2828
$request = $this->createRequest([$parameterName => 'TheUsername']);
2929

@@ -32,35 +32,32 @@ public function testSupport(UserProviderInterface $userProvider, RemoteUserAuthe
3232

3333
public function testSupportNoUser()
3434
{
35-
$authenticator = new RemoteUserAuthenticator($this->createMock(UserProviderInterface::class), new TokenStorage(), 'main');
35+
$authenticator = new RemoteUserAuthenticator(new InMemoryUserProvider(), new TokenStorage(), 'main');
3636

3737
$this->assertFalse($authenticator->supports($this->createRequest([])));
3838
}
3939

4040
/**
4141
* @dataProvider provideAuthenticators
4242
*/
43-
public function testAuthenticate(UserProviderInterface $userProvider, RemoteUserAuthenticator $authenticator, $parameterName)
43+
public function testAuthenticate(InMemoryUserProvider $userProvider, RemoteUserAuthenticator $authenticator, $parameterName)
4444
{
4545
$request = $this->createRequest([$parameterName => 'TheUsername']);
4646

4747
$authenticator->supports($request);
4848

49-
$userProvider->expects($this->once())
50-
->method('loadUserByUsername')
51-
->with('TheUsername')
52-
->willReturn($user = new User('TheUsername', null));
49+
$userProvider->createUser($user = new User('TheUsername', null));
5350

5451
$passport = $authenticator->authenticate($request);
55-
$this->assertEquals($user, $passport->getUser());
52+
$this->assertTrue($user->isEqualTo($passport->getUser()));
5653
}
5754

5855
public function provideAuthenticators()
5956
{
60-
$userProvider = $this->createMock(UserProviderInterface::class);
57+
$userProvider = new InMemoryUserProvider();
6158
yield [$userProvider, new RemoteUserAuthenticator($userProvider, new TokenStorage(), 'main'), 'REMOTE_USER'];
6259

63-
$userProvider = $this->createMock(UserProviderInterface::class);
60+
$userProvider = new InMemoryUserProvider();
6461
yield [$userProvider, new RemoteUserAuthenticator($userProvider, new TokenStorage(), 'main', 'CUSTOM_USER_PARAMETER'), 'CUSTOM_USER_PARAMETER'];
6562
}
6663

src/Symfony/Component/Security/Http/Tests/Authenticator/X509AuthenticatorTest.php

+6-18
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
17+
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
1718
use Symfony\Component\Security\Core\User\User;
18-
use Symfony\Component\Security\Core\User\UserProviderInterface;
1919
use Symfony\Component\Security\Http\Authenticator\X509Authenticator;
2020

2121
class X509AuthenticatorTest extends TestCase
@@ -25,7 +25,7 @@ class X509AuthenticatorTest extends TestCase
2525

2626
protected function setUp(): void
2727
{
28-
$this->userProvider = $this->createMock(UserProviderInterface::class);
28+
$this->userProvider = new InMemoryUserProvider();
2929
$this->authenticator = new X509Authenticator($this->userProvider, new TokenStorage(), 'main');
3030
}
3131

@@ -45,10 +45,7 @@ public function testAuthentication($username, $credentials)
4545
$request = $this->createRequest($serverVars);
4646
$this->assertTrue($this->authenticator->supports($request));
4747

48-
$this->userProvider->expects($this->any())
49-
->method('loadUserByUsername')
50-
->with($username)
51-
->willReturn(new User($username, null));
48+
$this->userProvider->createUser(new User($username, null));
5249

5350
$passport = $this->authenticator->authenticate($request);
5451
$this->assertEquals($username, $passport->getUser()->getUsername());
@@ -69,10 +66,7 @@ public function testAuthenticationNoUser($emailAddress, $credentials)
6966

7067
$this->assertTrue($this->authenticator->supports($request));
7168

72-
$this->userProvider->expects($this->once())
73-
->method('loadUserByUsername')
74-
->with($emailAddress)
75-
->willReturn(new User($emailAddress, null));
69+
$this->userProvider->createUser(new User($emailAddress, null));
7670

7771
$passport = $this->authenticator->authenticate($request);
7872
$this->assertEquals($emailAddress, $passport->getUser()->getUsername());
@@ -105,10 +99,7 @@ public function testAuthenticationCustomUserKey()
10599
]);
106100
$this->assertTrue($authenticator->supports($request));
107101

108-
$this->userProvider->expects($this->once())
109-
->method('loadUserByUsername')
110-
->with('TheUser')
111-
->willReturn(new User('TheUser', null));
102+
$this->userProvider->createUser(new User('TheUser', null));
112103

113104
$passport = $this->authenticator->authenticate($request);
114105
$this->assertEquals('TheUser', $passport->getUser()->getUsername());
@@ -123,10 +114,7 @@ public function testAuthenticationCustomCredentialsKey()
123114
]);
124115
$this->assertTrue($authenticator->supports($request));
125116

126-
$this->userProvider->expects($this->once())
127-
->method('loadUserByUsername')
128-
->with('cert@example.com')
129-
->willReturn(new User('cert@example.com', null));
117+
$this->userProvider->createUser(new User('cert@example.com', null));
130118

131119
$passport = $authenticator->authenticate($request);
132120
$this->assertEquals('cert@example.com', $passport->getUser()->getUsername());

src/Symfony/Component/Security/Http/Tests/EventListener/PasswordMigratingListenerTest.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1717
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
1818
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
19+
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
1920
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
21+
use Symfony\Component\Security\Core\User\User;
2022
use Symfony\Component\Security\Core\User\UserInterface;
21-
use Symfony\Component\Security\Core\User\UserProviderInterface;
2223
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
2324
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordUpgradeBadge;
2425
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
@@ -36,13 +37,12 @@ class PasswordMigratingListenerTest extends TestCase
3637

3738
protected function setUp(): void
3839
{
39-
$this->user = $this->createMock(UserInterface::class);
40-
$this->user->expects($this->any())->method('getPassword')->willReturn('old-encoded-password');
40+
$this->user = new User('test', 'old-encoded-password');
4141
$encoder = $this->createMock(PasswordEncoderInterface::class);
4242
$encoder->expects($this->any())->method('needsRehash')->willReturn(true);
4343
$encoder->expects($this->any())->method('encodePassword')->with('pa$$word', null)->willReturn('new-encoded-password');
4444
$this->encoderFactory = $this->createMock(EncoderFactoryInterface::class);
45-
$this->encoderFactory->expects($this->any())->method('getEncoder')->with($this->user)->willReturn($encoder);
45+
$this->encoderFactory->expects($this->any())->method('getEncoder')->with($this->callback(function ($user) { return $this->user->isEqualTo($user); }))->willReturn($encoder);
4646
$this->listener = new PasswordMigratingListener($this->encoderFactory);
4747
}
4848

@@ -96,12 +96,12 @@ public function testUpgradeWithUpgrader()
9696

9797
public function testUpgradeWithoutUpgrader()
9898
{
99-
$userLoader = $this->createMock(MigratingUserProvider::class);
100-
$userLoader->expects($this->any())->method('loadUserByUsername')->willReturn($this->user);
99+
$userLoader = $this->getMockBuilder(MigratingUserProvider::class)->setMethods(['upgradePassword'])->getMock();
100+
$userLoader->createUser($this->user);
101101

102102
$userLoader->expects($this->once())
103103
->method('upgradePassword')
104-
->with($this->user, 'new-encoded-password')
104+
->with($this->callback(function ($user) { return $this->user->isEqualTo($user); }), 'new-encoded-password')
105105
;
106106

107107
$event = $this->createEvent(new SelfValidatingPassport(new UserBadge('test', [$userLoader, 'loadUserByUsername']), [new PasswordUpgradeBadge('pa$$word')]));
@@ -119,7 +119,7 @@ private function createEvent(PassportInterface $passport)
119119
}
120120
}
121121

122-
abstract class MigratingUserProvider implements UserProviderInterface, PasswordUpgraderInterface
122+
class MigratingUserProvider extends InMemoryUserProvider implements PasswordUpgraderInterface
123123
{
124124
public function upgradePassword(UserInterface $user, string $newEncodedPassword): void
125125
{

src/Symfony/Component/Security/Http/Tests/EventListener/UserProviderListenerTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
namespace Symfony\Component\Security\Http\Tests\EventListener;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
1516
use Symfony\Component\Security\Core\User\User;
1617
use Symfony\Component\Security\Core\User\UserInterface;
17-
use Symfony\Component\Security\Core\User\UserProviderInterface;
1818
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
1919
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
2020
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
@@ -28,7 +28,7 @@ class UserProviderListenerTest extends TestCase
2828

2929
protected function setUp(): void
3030
{
31-
$this->userProvider = $this->createMock(UserProviderInterface::class);
31+
$this->userProvider = new InMemoryUserProvider();
3232
$this->listener = new UserProviderListener($this->userProvider);
3333
}
3434

@@ -42,8 +42,8 @@ public function testSetUserProvider()
4242
$this->assertEquals([$this->userProvider, 'loadUserByUsername'], $badge->getUserLoader());
4343

4444
$user = new User('wouter', null);
45-
$this->userProvider->expects($this->once())->method('loadUserByUsername')->with('wouter')->willReturn($user);
46-
$this->assertSame($user, $passport->getUser());
45+
$this->userProvider->createUser($user);
46+
$this->assertTrue($user->isEqualTo($passport->getUser()));
4747
}
4848

4949
/**

src/Symfony/Component/Security/Http/Tests/Firewall/SwitchUserListenerTest.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
2626
use Symfony\Component\Security\Core\User\User;
2727
use Symfony\Component\Security\Core\User\UserCheckerInterface;
28-
use Symfony\Component\Security\Core\User\UserInterface;
2928
use Symfony\Component\Security\Core\User\UserProviderInterface;
3029
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
3130
use Symfony\Component\Security\Http\Firewall\SwitchUserListener;
@@ -112,13 +111,13 @@ public function testExitUserUpdatesToken()
112111

113112
public function testExitUserDispatchesEventWithRefreshedUser()
114113
{
115-
$originalUser = $this->createMock(UserInterface::class);
116-
$refreshedUser = $this->createMock(UserInterface::class);
114+
$originalUser = new User('username', null);
115+
$refreshedUser = new User('username', null);
117116
$this
118117
->userProvider
119118
->expects($this->any())
120119
->method('refreshUser')
121-
->with($originalUser)
120+
->with($this->identicalTo($originalUser))
122121
->willReturn($refreshedUser);
123122
$originalToken = new UsernamePasswordToken($originalUser, '', 'key');
124123
$this->tokenStorage->setToken(new SwitchUserToken('username', '', 'key', ['ROLE_USER'], $originalToken));
@@ -399,13 +398,13 @@ public function testSwitchUserStateless()
399398

400399
public function testSwitchUserRefreshesOriginalToken()
401400
{
402-
$originalUser = $this->createMock(UserInterface::class);
403-
$refreshedOriginalUser = $this->createMock(UserInterface::class);
401+
$originalUser = new User('username', null);
402+
$refreshedOriginalUser = new User('username', null);
404403
$this
405404
->userProvider
406405
->expects($this->any())
407406
->method('refreshUser')
408-
->with($originalUser)
407+
->with($this->identicalTo($originalUser))
409408
->willReturn($refreshedOriginalUser);
410409
$originalToken = new UsernamePasswordToken($originalUser, '', 'key');
411410
$this->tokenStorage->setToken(new SwitchUserToken('username', '', 'key', ['ROLE_USER'], $originalToken));

0 commit comments

Comments
 (0)