Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f650709

Browse files
committed
Use concrete user related classes in the tests
This allows the tests to automatically adapt for changes in the UserInterface and UserProviderInterface in 5.x
1 parent d33973b commit f650709

11 files changed

+97
-175
lines changed

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

Lines changed: 6 additions & 1 deletion
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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
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\InMemoryUserProvider;
2021
use Symfony\Component\Security\Core\User\User;
21-
use Symfony\Component\Security\Core\User\UserProviderInterface;
2222
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
2323
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
2424
use Symfony\Component\Security\Http\Authenticator\FormLoginAuthenticator;
@@ -37,8 +37,7 @@ class FormLoginAuthenticatorTest extends TestCase
3737

3838
protected function setUp(): void
3939
{
40-
$this->userProvider = $this->createMock(UserProviderInterface::class);
41-
$this->userProvider->expects($this->any())->method('loadUserByUsername')->willReturn(new User('test', 's$cr$t'));
40+
$this->userProvider = new InMemoryUserProvider(['test' => ['password' => 's$cr$t']]);
4241
$this->successHandler = $this->createMock(AuthenticationSuccessHandlerInterface::class);
4342
$this->failureHandler = $this->createMock(AuthenticationFailureHandlerInterface::class);
4443
}
@@ -149,8 +148,7 @@ public function testUpgradePassword()
149148
$request = Request::create('/login_check', 'POST', ['_username' => 'wouter', '_password' => 's$cr$t']);
150149
$request->setSession($this->createSession());
151150

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

155153
$this->setUpAuthenticator();
156154
$passport = $this->authenticator->authenticate($request);

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

Lines changed: 5 additions & 10 deletions
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->assertEquals($user, $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

Lines changed: 7 additions & 10 deletions
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);
5552
$this->assertEquals($user, $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

Lines changed: 6 additions & 18 deletions
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-
129-
->willReturn(new User('[email protected]', null));
117+
$this->userProvider->createUser(new User('[email protected]', null));
130118

131119
$passport = $authenticator->authenticate($request);
132120
$this->assertEquals('[email protected]', $passport->getUser()->getUsername());

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

Lines changed: 6 additions & 6 deletions
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,8 +37,7 @@ 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');
@@ -96,8 +96,8 @@ 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')
@@ -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

Lines changed: 4 additions & 4 deletions
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->assertEquals($user, $passport->getUser());
4747
}
4848

4949
/**

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

Lines changed: 6 additions & 7 deletions
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)