Skip to content

Commit

Permalink
Use createMock() and use import instead of FQCN
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarStark authored and nicolas-grekas committed Jan 27, 2021
1 parent bb858fc commit 02da7f5
Show file tree
Hide file tree
Showing 38 changed files with 331 additions and 259 deletions.
42 changes: 22 additions & 20 deletions Tests/Authentication/AuthenticationProviderManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
namespace Symfony\Component\Security\Core\Tests\Authentication;

use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\AuthenticationEvents;
Expand All @@ -35,7 +37,7 @@ public function testAuthenticateWithProvidersWithIncorrectInterface()
$this->expectException(\InvalidArgumentException::class);
(new AuthenticationProviderManager([
new \stdClass(),
]))->authenticate($this->getMockBuilder(TokenInterface::class)->getMock());
]))->authenticate($this->createMock(TokenInterface::class));
}

public function testAuthenticateWhenNoProviderSupportsToken()
Expand All @@ -45,7 +47,7 @@ public function testAuthenticateWhenNoProviderSupportsToken()
]);

try {
$manager->authenticate($token = $this->getMockBuilder(TokenInterface::class)->getMock());
$manager->authenticate($token = $this->createMock(TokenInterface::class));
$this->fail();
} catch (ProviderNotFoundException $e) {
$this->assertSame($token, $e->getToken());
Expand All @@ -54,18 +56,18 @@ public function testAuthenticateWhenNoProviderSupportsToken()

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

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

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

try {
$manager->authenticate($token = $this->getMockBuilder(TokenInterface::class)->getMock());
$manager->authenticate($token = $this->createMock(TokenInterface::class));
$this->fail();
} catch (AccountStatusException $e) {
$this->assertSame($token, $e->getToken());
Expand All @@ -75,11 +77,11 @@ public function testAuthenticateWhenProviderReturnsAccountStatusException()
public function testAuthenticateWhenProviderReturnsAuthenticationException()
{
$manager = new AuthenticationProviderManager([
$this->getAuthenticationProvider(true, null, 'Symfony\Component\Security\Core\Exception\AuthenticationException'),
$this->getAuthenticationProvider(true, null, AuthenticationException::class),
]);

try {
$manager->authenticate($token = $this->getMockBuilder(TokenInterface::class)->getMock());
$manager->authenticate($token = $this->createMock(TokenInterface::class));
$this->fail();
} catch (AuthenticationException $e) {
$this->assertSame($token, $e->getToken());
Expand All @@ -89,27 +91,27 @@ public function testAuthenticateWhenProviderReturnsAuthenticationException()
public function testAuthenticateWhenOneReturnsAuthenticationExceptionButNotAll()
{
$manager = new AuthenticationProviderManager([
$this->getAuthenticationProvider(true, null, 'Symfony\Component\Security\Core\Exception\AuthenticationException'),
$this->getAuthenticationProvider(true, $expected = $this->getMockBuilder(TokenInterface::class)->getMock()),
$this->getAuthenticationProvider(true, null, AuthenticationException::class),
$this->getAuthenticationProvider(true, $expected = $this->createMock(TokenInterface::class)),
]);

$token = $manager->authenticate($this->getMockBuilder(TokenInterface::class)->getMock());
$token = $manager->authenticate($this->createMock(TokenInterface::class));
$this->assertSame($expected, $token);
}

public function testAuthenticateReturnsTokenOfTheFirstMatchingProvider()
{
$second = $this->getMockBuilder(\Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface::class)->getMock();
$second = $this->createMock(AuthenticationProviderInterface::class);
$second
->expects($this->never())
->method('supports')
;
$manager = new AuthenticationProviderManager([
$this->getAuthenticationProvider(true, $expected = $this->getMockBuilder(TokenInterface::class)->getMock()),
$this->getAuthenticationProvider(true, $expected = $this->createMock(TokenInterface::class)),
$second,
]);

$token = $manager->authenticate($this->getMockBuilder(TokenInterface::class)->getMock());
$token = $manager->authenticate($this->createMock(TokenInterface::class));
$this->assertSame($expected, $token);
}

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

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

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

$token = $manager->authenticate($this->getMockBuilder(TokenInterface::class)->getMock());
$token = $manager->authenticate($this->createMock(TokenInterface::class));
$this->assertEquals('bar', $token->getCredentials());
}

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

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

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

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

protected function getAuthenticationProvider($supports, $token = null, $exception = null)
{
$provider = $this->getMockBuilder(\Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface::class)->getMock();
$provider = $this->createMock(AuthenticationProviderInterface::class);
$provider->expects($this->once())
->method('supports')
->willReturn($supports)
Expand Down
2 changes: 1 addition & 1 deletion Tests/Authentication/AuthenticationTrustResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function testisFullFledgedWithClassAsConstructorButStillExtending()

protected function getToken()
{
return $this->getMockBuilder(TokenInterface::class)->getMock();
return $this->createMock(TokenInterface::class);
}

protected function getAnonymousToken()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Provider\AnonymousAuthenticationProvider;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;

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

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

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

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

public function testAuthenticateWhenSecretIsNotValid()
{
$this->expectException(\Symfony\Component\Security\Core\Exception\BadCredentialsException::class);
$this->expectException(BadCredentialsException::class);
$provider = $this->getProvider('foo');

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

protected function getSupportedToken($secret)
{
$token = $this->getMockBuilder(\Symfony\Component\Security\Core\Authentication\Token\AnonymousToken::class)->setMethods(['getSecret'])->disableOriginalConstructor()->getMock();
$token = $this->getMockBuilder(AnonymousToken::class)->setMethods(['getSecret'])->disableOriginalConstructor()->getMock();
$token->expects($this->any())
->method('getSecret')
->willReturn($secret)
Expand Down
Loading

0 comments on commit 02da7f5

Please sign in to comment.