Skip to content

Commit a191352

Browse files
Merge branch '5.1' into 5.2
* 5.1: Use createMock() and use import instead of FQCN
2 parents b7f721f + 23e2b83 commit a191352

File tree

4 files changed

+53
-51
lines changed

4 files changed

+53
-51
lines changed

Tests/Authenticator/FormLoginAuthenticatorTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
namespace Symfony\Component\Security\Guard\Tests\Authenticator;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\RedirectResponse;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\HttpFoundation\Response;
18+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
1719
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1820
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1921
use Symfony\Component\Security\Core\User\UserInterface;
@@ -36,7 +38,7 @@ public function testAuthenticationFailureWithoutSession()
3638
{
3739
$failureResponse = $this->authenticator->onAuthenticationFailure($this->requestWithoutSession, new AuthenticationException());
3840

39-
$this->assertInstanceOf(\Symfony\Component\HttpFoundation\RedirectResponse::class, $failureResponse);
41+
$this->assertInstanceOf(RedirectResponse::class, $failureResponse);
4042
$this->assertEquals(self::LOGIN_URL, $failureResponse->getTargetUrl());
4143
}
4244

@@ -48,7 +50,7 @@ public function testAuthenticationFailureWithSession()
4850

4951
$failureResponse = $this->authenticator->onAuthenticationFailure($this->requestWithSession, new AuthenticationException());
5052

51-
$this->assertInstanceOf(\Symfony\Component\HttpFoundation\RedirectResponse::class, $failureResponse);
53+
$this->assertInstanceOf(RedirectResponse::class, $failureResponse);
5254
$this->assertEquals(self::LOGIN_URL, $failureResponse->getTargetUrl());
5355
}
5456

@@ -63,15 +65,15 @@ public function testStartWithoutSession()
6365
{
6466
$failureResponse = $this->authenticator->start($this->requestWithoutSession, new AuthenticationException());
6567

66-
$this->assertInstanceOf(\Symfony\Component\HttpFoundation\RedirectResponse::class, $failureResponse);
68+
$this->assertInstanceOf(RedirectResponse::class, $failureResponse);
6769
$this->assertEquals(self::LOGIN_URL, $failureResponse->getTargetUrl());
6870
}
6971

7072
public function testStartWithSession()
7173
{
7274
$failureResponse = $this->authenticator->start($this->requestWithSession, new AuthenticationException());
7375

74-
$this->assertInstanceOf(\Symfony\Component\HttpFoundation\RedirectResponse::class, $failureResponse);
76+
$this->assertInstanceOf(RedirectResponse::class, $failureResponse);
7577
$this->assertEquals(self::LOGIN_URL, $failureResponse->getTargetUrl());
7678
}
7779

@@ -80,9 +82,7 @@ protected function setUp(): void
8082
$this->requestWithoutSession = new Request([], [], [], [], [], []);
8183
$this->requestWithSession = new Request([], [], [], [], [], []);
8284

83-
$session = $this->getMockBuilder(\Symfony\Component\HttpFoundation\Session\SessionInterface::class)
84-
->disableOriginalConstructor()
85-
->getMock();
85+
$session = $this->createMock(SessionInterface::class);
8686
$this->requestWithSession->setSession($session);
8787

8888
$this->authenticator = new TestFormLoginAuthenticator();

Tests/Firewall/GuardAuthenticationListenerTest.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@
1212
namespace Symfony\Component\Security\Guard\Tests\Firewall;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Psr\Log\LoggerInterface;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\HttpFoundation\Response;
18+
use Symfony\Component\HttpKernel\Event\RequestEvent;
19+
use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
1720
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1821
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1922
use Symfony\Component\Security\Guard\AuthenticatorInterface;
2023
use Symfony\Component\Security\Guard\Firewall\GuardAuthenticationListener;
24+
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
2125
use Symfony\Component\Security\Guard\Token\PreAuthenticationGuardToken;
26+
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
2227

2328
/**
2429
* @author Ryan Weaver <weaverryan@gmail.com>
@@ -35,8 +40,8 @@ class GuardAuthenticationListenerTest extends TestCase
3540

3641
public function testHandleSuccess()
3742
{
38-
$authenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
39-
$authenticateToken = $this->getMockBuilder(TokenInterface::class)->getMock();
43+
$authenticator = $this->createMock(AuthenticatorInterface::class);
44+
$authenticateToken = $this->createMock(TokenInterface::class);
4045
$providerKey = 'my_firewall';
4146

4247
$credentials = ['username' => 'weaverryan', 'password' => 'all_your_base'];
@@ -90,8 +95,8 @@ public function testHandleSuccess()
9095

9196
public function testHandleSuccessStopsAfterResponseIsSet()
9297
{
93-
$authenticator1 = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
94-
$authenticator2 = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
98+
$authenticator1 = $this->createMock(AuthenticatorInterface::class);
99+
$authenticator2 = $this->createMock(AuthenticatorInterface::class);
95100

96101
// mock the first authenticator to fail, and set a Response
97102
$authenticator1
@@ -124,8 +129,8 @@ public function testHandleSuccessStopsAfterResponseIsSet()
124129

125130
public function testHandleSuccessWithRememberMe()
126131
{
127-
$authenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
128-
$authenticateToken = $this->getMockBuilder(TokenInterface::class)->getMock();
132+
$authenticator = $this->createMock(AuthenticatorInterface::class);
133+
$authenticateToken = $this->createMock(TokenInterface::class);
129134
$providerKey = 'my_firewall_with_rememberme';
130135

131136
$authenticator
@@ -172,7 +177,7 @@ public function testHandleSuccessWithRememberMe()
172177

173178
public function testHandleCatchesAuthenticationException()
174179
{
175-
$authenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
180+
$authenticator = $this->createMock(AuthenticatorInterface::class);
176181
$providerKey = 'my_firewall2';
177182

178183
$authException = new AuthenticationException('Get outta here crazy user with a bad password!');
@@ -208,7 +213,7 @@ public function testHandleCatchesAuthenticationException()
208213

209214
public function testSupportsReturnFalseSkipAuth()
210215
{
211-
$authenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
216+
$authenticator = $this->createMock(AuthenticatorInterface::class);
212217
$providerKey = 'my_firewall4';
213218

214219
$authenticator
@@ -235,7 +240,7 @@ public function testSupportsReturnFalseSkipAuth()
235240
public function testReturnNullFromGetCredentials()
236241
{
237242
$this->expectException(\UnexpectedValueException::class);
238-
$authenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
243+
$authenticator = $this->createMock(AuthenticatorInterface::class);
239244
$providerKey = 'my_firewall4';
240245

241246
$authenticator
@@ -262,17 +267,11 @@ public function testReturnNullFromGetCredentials()
262267

263268
protected function setUp(): void
264269
{
265-
$this->authenticationManager = $this->getMockBuilder(\Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager::class)
266-
->disableOriginalConstructor()
267-
->getMock();
268-
269-
$this->guardAuthenticatorHandler = $this->getMockBuilder(\Symfony\Component\Security\Guard\GuardAuthenticatorHandler::class)
270-
->disableOriginalConstructor()
271-
->getMock();
272-
270+
$this->authenticationManager = $this->createMock(AuthenticationProviderManager::class);
271+
$this->guardAuthenticatorHandler = $this->createMock(GuardAuthenticatorHandler::class);
273272
$this->request = new Request([], [], [], [], [], []);
274273

275-
$this->event = $this->getMockBuilder(\Symfony\Component\HttpKernel\Event\RequestEvent::class)
274+
$this->event = $this->getMockBuilder(RequestEvent::class)
276275
->disableOriginalConstructor()
277276
->setMethods(['getRequest'])
278277
->getMock();
@@ -281,8 +280,8 @@ protected function setUp(): void
281280
->method('getRequest')
282281
->willReturn($this->request);
283282

284-
$this->logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock();
285-
$this->rememberMeServices = $this->getMockBuilder(\Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface::class)->getMock();
283+
$this->logger = $this->createMock(LoggerInterface::class);
284+
$this->rememberMeServices = $this->createMock(RememberMeServicesInterface::class);
286285
}
287286

288287
protected function tearDown(): void

Tests/GuardAuthenticatorHandlerTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
1718
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1819
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1920
use Symfony\Component\Security\Core\Exception\AuthenticationException;
@@ -171,12 +172,12 @@ public function testSessionIsNotInstantiatedOnStatelessFirewall()
171172

172173
protected function setUp(): void
173174
{
174-
$this->tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock();
175-
$this->dispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
176-
$this->token = $this->getMockBuilder(TokenInterface::class)->getMock();
175+
$this->tokenStorage = $this->createMock(TokenStorageInterface::class);
176+
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
177+
$this->token = $this->createMock(TokenInterface::class);
177178
$this->request = new Request([], [], [], [], [], []);
178-
$this->sessionStrategy = $this->getMockBuilder(SessionAuthenticationStrategyInterface::class)->getMock();
179-
$this->guardAuthenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
179+
$this->sessionStrategy = $this->createMock(SessionAuthenticationStrategyInterface::class);
180+
$this->guardAuthenticator = $this->createMock(AuthenticatorInterface::class);
180181
}
181182

182183
protected function tearDown(): void
@@ -190,7 +191,7 @@ protected function tearDown(): void
190191

191192
private function configurePreviousSession()
192193
{
193-
$session = $this->getMockBuilder(\Symfony\Component\HttpFoundation\Session\SessionInterface::class)->getMock();
194+
$session = $this->createMock(SessionInterface::class);
194195
$session->expects($this->any())
195196
->method('getName')
196197
->willReturn('test_session_name');

Tests/Provider/GuardAuthenticationProviderTest.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
namespace Symfony\Component\Security\Guard\Tests\Provider;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
16+
use Symfony\Component\Security\Core\Exception\AuthenticationExpiredException;
1517
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
18+
use Symfony\Component\Security\Core\User\UserCheckerInterface;
1619
use Symfony\Component\Security\Core\User\UserInterface;
20+
use Symfony\Component\Security\Core\User\UserProviderInterface;
1721
use Symfony\Component\Security\Guard\AuthenticatorInterface;
1822
use Symfony\Component\Security\Guard\Provider\GuardAuthenticationProvider;
1923
use Symfony\Component\Security\Guard\Token\GuardTokenInterface;
@@ -33,9 +37,9 @@ public function testAuthenticate()
3337
{
3438
$providerKey = 'my_cool_firewall';
3539

36-
$authenticatorA = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
37-
$authenticatorB = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
38-
$authenticatorC = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
40+
$authenticatorA = $this->createMock(AuthenticatorInterface::class);
41+
$authenticatorB = $this->createMock(AuthenticatorInterface::class);
42+
$authenticatorC = $this->createMock(AuthenticatorInterface::class);
3943
$authenticators = [$authenticatorA, $authenticatorB, $authenticatorC];
4044

4145
// called 2 times - for authenticator A and B (stops on B because of match)
@@ -58,7 +62,7 @@ public function testAuthenticate()
5862
$authenticatorC->expects($this->never())
5963
->method('getUser');
6064

61-
$mockedUser = $this->getMockBuilder(UserInterface::class)->getMock();
65+
$mockedUser = $this->createMock(UserInterface::class);
6266
$authenticatorB->expects($this->once())
6367
->method('getUser')
6468
->with($enteredCredentials, $this->userProvider)
@@ -69,7 +73,7 @@ public function testAuthenticate()
6973
->with($enteredCredentials, $mockedUser)
7074
// authentication works!
7175
->willReturn(true);
72-
$authedToken = $this->getMockBuilder(GuardTokenInterface::class)->getMock();
76+
$authedToken = $this->createMock(GuardTokenInterface::class);
7377
$authenticatorB->expects($this->once())
7478
->method('createAuthenticatedToken')
7579
->with($mockedUser, $providerKey)
@@ -121,12 +125,12 @@ public function testCheckCredentialsReturningFalseFailsAuthentication()
121125

122126
public function testGuardWithNoLongerAuthenticatedTriggersLogout()
123127
{
124-
$this->expectException(\Symfony\Component\Security\Core\Exception\AuthenticationExpiredException::class);
128+
$this->expectException(AuthenticationExpiredException::class);
125129
$providerKey = 'my_firewall_abc';
126130

127131
// create a token and mark it as NOT authenticated anymore
128132
// this mimics what would happen if a user "changed" between request
129-
$mockedUser = $this->getMockBuilder(UserInterface::class)->getMock();
133+
$mockedUser = $this->createMock(UserInterface::class);
130134
$token = new PostAuthenticationGuardToken($mockedUser, $providerKey, ['ROLE_USER']);
131135
$token->setAuthenticated(false);
132136

@@ -136,11 +140,11 @@ public function testGuardWithNoLongerAuthenticatedTriggersLogout()
136140

137141
public function testSupportsChecksGuardAuthenticatorsTokenOrigin()
138142
{
139-
$authenticatorA = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
140-
$authenticatorB = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
143+
$authenticatorA = $this->createMock(AuthenticatorInterface::class);
144+
$authenticatorB = $this->createMock(AuthenticatorInterface::class);
141145
$authenticators = [$authenticatorA, $authenticatorB];
142146

143-
$mockedUser = $this->getMockBuilder(UserInterface::class)->getMock();
147+
$mockedUser = $this->createMock(UserInterface::class);
144148
$provider = new GuardAuthenticationProvider($authenticators, $this->userProvider, 'first_firewall', $this->userChecker);
145149

146150
$token = new PreAuthenticationGuardToken($mockedUser, 'first_firewall_1');
@@ -154,12 +158,12 @@ public function testSupportsChecksGuardAuthenticatorsTokenOrigin()
154158

155159
public function testAuthenticateFailsOnNonOriginatingToken()
156160
{
157-
$this->expectException(\Symfony\Component\Security\Core\Exception\AuthenticationException::class);
161+
$this->expectException(AuthenticationException::class);
158162
$this->expectExceptionMessageMatches('/second_firewall_0/');
159-
$authenticatorA = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
163+
$authenticatorA = $this->createMock(AuthenticatorInterface::class);
160164
$authenticators = [$authenticatorA];
161165

162-
$mockedUser = $this->getMockBuilder(UserInterface::class)->getMock();
166+
$mockedUser = $this->createMock(UserInterface::class);
163167
$provider = new GuardAuthenticationProvider($authenticators, $this->userProvider, 'first_firewall', $this->userChecker);
164168

165169
$token = new PreAuthenticationGuardToken($mockedUser, 'second_firewall_0');
@@ -168,11 +172,9 @@ public function testAuthenticateFailsOnNonOriginatingToken()
168172

169173
protected function setUp(): void
170174
{
171-
$this->userProvider = $this->getMockBuilder(\Symfony\Component\Security\Core\User\UserProviderInterface::class)->getMock();
172-
$this->userChecker = $this->getMockBuilder(\Symfony\Component\Security\Core\User\UserCheckerInterface::class)->getMock();
173-
$this->preAuthenticationToken = $this->getMockBuilder(PreAuthenticationGuardToken::class)
174-
->disableOriginalConstructor()
175-
->getMock();
175+
$this->userProvider = $this->createMock(UserProviderInterface::class);
176+
$this->userChecker = $this->createMock(UserCheckerInterface::class);
177+
$this->preAuthenticationToken = $this->createMock(PreAuthenticationGuardToken::class);
176178
}
177179

178180
protected function tearDown(): void

0 commit comments

Comments
 (0)