Skip to content

Commit 4ca502f

Browse files
Merge branch '4.4' into 5.1
* 4.4: Use createMock() and use import instead of FQCN
2 parents a4bd75c + 02da7f5 commit 4ca502f

33 files changed

+246
-188
lines changed

Tests/Authentication/AuthenticationProviderManagerTest.php

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
namespace Symfony\Component\Security\Core\Tests\Authentication;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1516
use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
17+
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
1618
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1719
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
1820
use Symfony\Component\Security\Core\AuthenticationEvents;
@@ -35,7 +37,7 @@ public function testAuthenticateWithProvidersWithIncorrectInterface()
3537
$this->expectException(\InvalidArgumentException::class);
3638
(new AuthenticationProviderManager([
3739
new \stdClass(),
38-
]))->authenticate($this->getMockBuilder(TokenInterface::class)->getMock());
40+
]))->authenticate($this->createMock(TokenInterface::class));
3941
}
4042

4143
public function testAuthenticateWhenNoProviderSupportsToken()
@@ -45,7 +47,7 @@ public function testAuthenticateWhenNoProviderSupportsToken()
4547
]);
4648

4749
try {
48-
$manager->authenticate($token = $this->getMockBuilder(TokenInterface::class)->getMock());
50+
$manager->authenticate($token = $this->createMock(TokenInterface::class));
4951
$this->fail();
5052
} catch (ProviderNotFoundException $e) {
5153
$this->assertSame($token, $e->getToken());
@@ -54,18 +56,18 @@ public function testAuthenticateWhenNoProviderSupportsToken()
5456

5557
public function testAuthenticateWhenProviderReturnsAccountStatusException()
5658
{
57-
$secondAuthenticationProvider = $this->getMockBuilder(\Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface::class)->getMock();
59+
$secondAuthenticationProvider = $this->createMock(AuthenticationProviderInterface::class);
5860

5961
$manager = new AuthenticationProviderManager([
60-
$this->getAuthenticationProvider(true, null, 'Symfony\Component\Security\Core\Exception\AccountStatusException'),
62+
$this->getAuthenticationProvider(true, null, AccountStatusException::class),
6163
$secondAuthenticationProvider,
6264
]);
6365

6466
// AccountStatusException stops authentication
6567
$secondAuthenticationProvider->expects($this->never())->method('supports');
6668

6769
try {
68-
$manager->authenticate($token = $this->getMockBuilder(TokenInterface::class)->getMock());
70+
$manager->authenticate($token = $this->createMock(TokenInterface::class));
6971
$this->fail();
7072
} catch (AccountStatusException $e) {
7173
$this->assertSame($token, $e->getToken());
@@ -75,11 +77,11 @@ public function testAuthenticateWhenProviderReturnsAccountStatusException()
7577
public function testAuthenticateWhenProviderReturnsAuthenticationException()
7678
{
7779
$manager = new AuthenticationProviderManager([
78-
$this->getAuthenticationProvider(true, null, 'Symfony\Component\Security\Core\Exception\AuthenticationException'),
80+
$this->getAuthenticationProvider(true, null, AuthenticationException::class),
7981
]);
8082

8183
try {
82-
$manager->authenticate($token = $this->getMockBuilder(TokenInterface::class)->getMock());
84+
$manager->authenticate($token = $this->createMock(TokenInterface::class));
8385
$this->fail();
8486
} catch (AuthenticationException $e) {
8587
$this->assertSame($token, $e->getToken());
@@ -89,27 +91,27 @@ public function testAuthenticateWhenProviderReturnsAuthenticationException()
8991
public function testAuthenticateWhenOneReturnsAuthenticationExceptionButNotAll()
9092
{
9193
$manager = new AuthenticationProviderManager([
92-
$this->getAuthenticationProvider(true, null, 'Symfony\Component\Security\Core\Exception\AuthenticationException'),
93-
$this->getAuthenticationProvider(true, $expected = $this->getMockBuilder(TokenInterface::class)->getMock()),
94+
$this->getAuthenticationProvider(true, null, AuthenticationException::class),
95+
$this->getAuthenticationProvider(true, $expected = $this->createMock(TokenInterface::class)),
9496
]);
9597

96-
$token = $manager->authenticate($this->getMockBuilder(TokenInterface::class)->getMock());
98+
$token = $manager->authenticate($this->createMock(TokenInterface::class));
9799
$this->assertSame($expected, $token);
98100
}
99101

100102
public function testAuthenticateReturnsTokenOfTheFirstMatchingProvider()
101103
{
102-
$second = $this->getMockBuilder(\Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface::class)->getMock();
104+
$second = $this->createMock(AuthenticationProviderInterface::class);
103105
$second
104106
->expects($this->never())
105107
->method('supports')
106108
;
107109
$manager = new AuthenticationProviderManager([
108-
$this->getAuthenticationProvider(true, $expected = $this->getMockBuilder(TokenInterface::class)->getMock()),
110+
$this->getAuthenticationProvider(true, $expected = $this->createMock(TokenInterface::class)),
109111
$second,
110112
]);
111113

112-
$token = $manager->authenticate($this->getMockBuilder(TokenInterface::class)->getMock());
114+
$token = $manager->authenticate($this->createMock(TokenInterface::class));
113115
$this->assertSame($expected, $token);
114116
}
115117

@@ -119,25 +121,25 @@ public function testEraseCredentialFlag()
119121
$this->getAuthenticationProvider(true, $token = new UsernamePasswordToken('foo', 'bar', 'key')),
120122
]);
121123

122-
$token = $manager->authenticate($this->getMockBuilder(TokenInterface::class)->getMock());
124+
$token = $manager->authenticate($this->createMock(TokenInterface::class));
123125
$this->assertEquals('', $token->getCredentials());
124126

125127
$manager = new AuthenticationProviderManager([
126128
$this->getAuthenticationProvider(true, $token = new UsernamePasswordToken('foo', 'bar', 'key')),
127129
], false);
128130

129-
$token = $manager->authenticate($this->getMockBuilder(TokenInterface::class)->getMock());
131+
$token = $manager->authenticate($this->createMock(TokenInterface::class));
130132
$this->assertEquals('bar', $token->getCredentials());
131133
}
132134

133135
public function testAuthenticateDispatchesAuthenticationFailureEvent()
134136
{
135137
$token = new UsernamePasswordToken('foo', 'bar', 'key');
136-
$provider = $this->getMockBuilder(\Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface::class)->getMock();
138+
$provider = $this->createMock(AuthenticationProviderInterface::class);
137139
$provider->expects($this->once())->method('supports')->willReturn(true);
138140
$provider->expects($this->once())->method('authenticate')->willThrowException($exception = new AuthenticationException());
139141

140-
$dispatcher = $this->getMockBuilder(\Symfony\Component\EventDispatcher\EventDispatcherInterface::class)->getMock();
142+
$dispatcher = $this->createMock(EventDispatcherInterface::class);
141143
$dispatcher
142144
->expects($this->once())
143145
->method('dispatch')
@@ -158,11 +160,11 @@ public function testAuthenticateDispatchesAuthenticationSuccessEvent()
158160
{
159161
$token = new UsernamePasswordToken('foo', 'bar', 'key');
160162

161-
$provider = $this->getMockBuilder(\Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface::class)->getMock();
163+
$provider = $this->createMock(AuthenticationProviderInterface::class);
162164
$provider->expects($this->once())->method('supports')->willReturn(true);
163165
$provider->expects($this->once())->method('authenticate')->willReturn($token);
164166

165-
$dispatcher = $this->getMockBuilder(\Symfony\Component\EventDispatcher\EventDispatcherInterface::class)->getMock();
167+
$dispatcher = $this->createMock(EventDispatcherInterface::class);
166168
$dispatcher
167169
->expects($this->once())
168170
->method('dispatch')
@@ -176,7 +178,7 @@ public function testAuthenticateDispatchesAuthenticationSuccessEvent()
176178

177179
protected function getAuthenticationProvider($supports, $token = null, $exception = null)
178180
{
179-
$provider = $this->getMockBuilder(\Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface::class)->getMock();
181+
$provider = $this->createMock(AuthenticationProviderInterface::class);
180182
$provider->expects($this->once())
181183
->method('supports')
182184
->willReturn($supports)

Tests/Authentication/AuthenticationTrustResolverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function testisFullFledgedWithClassAsConstructorButStillExtending()
9191

9292
protected function getToken()
9393
{
94-
return $this->getMockBuilder(TokenInterface::class)->getMock();
94+
return $this->createMock(TokenInterface::class);
9595
}
9696

9797
protected function getAnonymousToken()

Tests/Authentication/Provider/AnonymousAuthenticationProviderTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Security\Core\Authentication\Provider\AnonymousAuthenticationProvider;
16+
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
17+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
18+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
19+
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
1620

1721
class AnonymousAuthenticationProviderTest extends TestCase
1822
{
@@ -21,21 +25,21 @@ public function testSupports()
2125
$provider = $this->getProvider('foo');
2226

2327
$this->assertTrue($provider->supports($this->getSupportedToken('foo')));
24-
$this->assertFalse($provider->supports($this->getMockBuilder(\Symfony\Component\Security\Core\Authentication\Token\TokenInterface::class)->getMock()));
28+
$this->assertFalse($provider->supports($this->createMock(TokenInterface::class)));
2529
}
2630

2731
public function testAuthenticateWhenTokenIsNotSupported()
2832
{
29-
$this->expectException(\Symfony\Component\Security\Core\Exception\AuthenticationException::class);
33+
$this->expectException(AuthenticationException::class);
3034
$this->expectExceptionMessage('The token is not supported by this authentication provider.');
3135
$provider = $this->getProvider('foo');
3236

33-
$provider->authenticate($this->getMockBuilder(\Symfony\Component\Security\Core\Authentication\Token\TokenInterface::class)->getMock());
37+
$provider->authenticate($this->createMock(TokenInterface::class));
3438
}
3539

3640
public function testAuthenticateWhenSecretIsNotValid()
3741
{
38-
$this->expectException(\Symfony\Component\Security\Core\Exception\BadCredentialsException::class);
42+
$this->expectException(BadCredentialsException::class);
3943
$provider = $this->getProvider('foo');
4044

4145
$provider->authenticate($this->getSupportedToken('bar'));
@@ -51,7 +55,7 @@ public function testAuthenticate()
5155

5256
protected function getSupportedToken($secret)
5357
{
54-
$token = $this->getMockBuilder(\Symfony\Component\Security\Core\Authentication\Token\AnonymousToken::class)->setMethods(['getSecret'])->disableOriginalConstructor()->getMock();
58+
$token = $this->getMockBuilder(AnonymousToken::class)->setMethods(['getSecret'])->disableOriginalConstructor()->getMock();
5559
$token->expects($this->any())
5660
->method('getSecret')
5761
->willReturn($secret)

0 commit comments

Comments
 (0)