Skip to content

Commit

Permalink
Merge branch '5.1' into 5.2
Browse files Browse the repository at this point in the history
* 5.1:
  Use createMock() and use import instead of FQCN
  • Loading branch information
nicolas-grekas committed Jan 27, 2021
2 parents b7f721f + 23e2b83 commit a191352
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 51 deletions.
14 changes: 7 additions & 7 deletions Tests/Authenticator/FormLoginAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
namespace Symfony\Component\Security\Guard\Tests\Authenticator;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserInterface;
Expand All @@ -36,7 +38,7 @@ public function testAuthenticationFailureWithoutSession()
{
$failureResponse = $this->authenticator->onAuthenticationFailure($this->requestWithoutSession, new AuthenticationException());

$this->assertInstanceOf(\Symfony\Component\HttpFoundation\RedirectResponse::class, $failureResponse);
$this->assertInstanceOf(RedirectResponse::class, $failureResponse);
$this->assertEquals(self::LOGIN_URL, $failureResponse->getTargetUrl());
}

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

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

$this->assertInstanceOf(\Symfony\Component\HttpFoundation\RedirectResponse::class, $failureResponse);
$this->assertInstanceOf(RedirectResponse::class, $failureResponse);
$this->assertEquals(self::LOGIN_URL, $failureResponse->getTargetUrl());
}

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

$this->assertInstanceOf(\Symfony\Component\HttpFoundation\RedirectResponse::class, $failureResponse);
$this->assertInstanceOf(RedirectResponse::class, $failureResponse);
$this->assertEquals(self::LOGIN_URL, $failureResponse->getTargetUrl());
}

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

$this->assertInstanceOf(\Symfony\Component\HttpFoundation\RedirectResponse::class, $failureResponse);
$this->assertInstanceOf(RedirectResponse::class, $failureResponse);
$this->assertEquals(self::LOGIN_URL, $failureResponse->getTargetUrl());
}

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

$session = $this->getMockBuilder(\Symfony\Component\HttpFoundation\Session\SessionInterface::class)
->disableOriginalConstructor()
->getMock();
$session = $this->createMock(SessionInterface::class);
$this->requestWithSession->setSession($session);

$this->authenticator = new TestFormLoginAuthenticator();
Expand Down
39 changes: 19 additions & 20 deletions Tests/Firewall/GuardAuthenticationListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@
namespace Symfony\Component\Security\Guard\Tests\Firewall;

use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Guard\AuthenticatorInterface;
use Symfony\Component\Security\Guard\Firewall\GuardAuthenticationListener;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
use Symfony\Component\Security\Guard\Token\PreAuthenticationGuardToken;
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;

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

public function testHandleSuccess()
{
$authenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$authenticateToken = $this->getMockBuilder(TokenInterface::class)->getMock();
$authenticator = $this->createMock(AuthenticatorInterface::class);
$authenticateToken = $this->createMock(TokenInterface::class);
$providerKey = 'my_firewall';

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

public function testHandleSuccessStopsAfterResponseIsSet()
{
$authenticator1 = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$authenticator2 = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$authenticator1 = $this->createMock(AuthenticatorInterface::class);
$authenticator2 = $this->createMock(AuthenticatorInterface::class);

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

public function testHandleSuccessWithRememberMe()
{
$authenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$authenticateToken = $this->getMockBuilder(TokenInterface::class)->getMock();
$authenticator = $this->createMock(AuthenticatorInterface::class);
$authenticateToken = $this->createMock(TokenInterface::class);
$providerKey = 'my_firewall_with_rememberme';

$authenticator
Expand Down Expand Up @@ -172,7 +177,7 @@ public function testHandleSuccessWithRememberMe()

public function testHandleCatchesAuthenticationException()
{
$authenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$authenticator = $this->createMock(AuthenticatorInterface::class);
$providerKey = 'my_firewall2';

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

public function testSupportsReturnFalseSkipAuth()
{
$authenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$authenticator = $this->createMock(AuthenticatorInterface::class);
$providerKey = 'my_firewall4';

$authenticator
Expand All @@ -235,7 +240,7 @@ public function testSupportsReturnFalseSkipAuth()
public function testReturnNullFromGetCredentials()
{
$this->expectException(\UnexpectedValueException::class);
$authenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$authenticator = $this->createMock(AuthenticatorInterface::class);
$providerKey = 'my_firewall4';

$authenticator
Expand All @@ -262,17 +267,11 @@ public function testReturnNullFromGetCredentials()

protected function setUp(): void
{
$this->authenticationManager = $this->getMockBuilder(\Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager::class)
->disableOriginalConstructor()
->getMock();

$this->guardAuthenticatorHandler = $this->getMockBuilder(\Symfony\Component\Security\Guard\GuardAuthenticatorHandler::class)
->disableOriginalConstructor()
->getMock();

$this->authenticationManager = $this->createMock(AuthenticationProviderManager::class);
$this->guardAuthenticatorHandler = $this->createMock(GuardAuthenticatorHandler::class);
$this->request = new Request([], [], [], [], [], []);

$this->event = $this->getMockBuilder(\Symfony\Component\HttpKernel\Event\RequestEvent::class)
$this->event = $this->getMockBuilder(RequestEvent::class)
->disableOriginalConstructor()
->setMethods(['getRequest'])
->getMock();
Expand All @@ -281,8 +280,8 @@ protected function setUp(): void
->method('getRequest')
->willReturn($this->request);

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

protected function tearDown(): void
Expand Down
13 changes: 7 additions & 6 deletions Tests/GuardAuthenticatorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
Expand Down Expand Up @@ -171,12 +172,12 @@ public function testSessionIsNotInstantiatedOnStatelessFirewall()

protected function setUp(): void
{
$this->tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock();
$this->dispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
$this->token = $this->getMockBuilder(TokenInterface::class)->getMock();
$this->tokenStorage = $this->createMock(TokenStorageInterface::class);
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
$this->token = $this->createMock(TokenInterface::class);
$this->request = new Request([], [], [], [], [], []);
$this->sessionStrategy = $this->getMockBuilder(SessionAuthenticationStrategyInterface::class)->getMock();
$this->guardAuthenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$this->sessionStrategy = $this->createMock(SessionAuthenticationStrategyInterface::class);
$this->guardAuthenticator = $this->createMock(AuthenticatorInterface::class);
}

protected function tearDown(): void
Expand All @@ -190,7 +191,7 @@ protected function tearDown(): void

private function configurePreviousSession()
{
$session = $this->getMockBuilder(\Symfony\Component\HttpFoundation\Session\SessionInterface::class)->getMock();
$session = $this->createMock(SessionInterface::class);
$session->expects($this->any())
->method('getName')
->willReturn('test_session_name');
Expand Down
38 changes: 20 additions & 18 deletions Tests/Provider/GuardAuthenticationProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
namespace Symfony\Component\Security\Guard\Tests\Provider;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\AuthenticationExpiredException;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Guard\AuthenticatorInterface;
use Symfony\Component\Security\Guard\Provider\GuardAuthenticationProvider;
use Symfony\Component\Security\Guard\Token\GuardTokenInterface;
Expand All @@ -33,9 +37,9 @@ public function testAuthenticate()
{
$providerKey = 'my_cool_firewall';

$authenticatorA = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$authenticatorB = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$authenticatorC = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$authenticatorA = $this->createMock(AuthenticatorInterface::class);
$authenticatorB = $this->createMock(AuthenticatorInterface::class);
$authenticatorC = $this->createMock(AuthenticatorInterface::class);
$authenticators = [$authenticatorA, $authenticatorB, $authenticatorC];

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

$mockedUser = $this->getMockBuilder(UserInterface::class)->getMock();
$mockedUser = $this->createMock(UserInterface::class);
$authenticatorB->expects($this->once())
->method('getUser')
->with($enteredCredentials, $this->userProvider)
Expand All @@ -69,7 +73,7 @@ public function testAuthenticate()
->with($enteredCredentials, $mockedUser)
// authentication works!
->willReturn(true);
$authedToken = $this->getMockBuilder(GuardTokenInterface::class)->getMock();
$authedToken = $this->createMock(GuardTokenInterface::class);
$authenticatorB->expects($this->once())
->method('createAuthenticatedToken')
->with($mockedUser, $providerKey)
Expand Down Expand Up @@ -121,12 +125,12 @@ public function testCheckCredentialsReturningFalseFailsAuthentication()

public function testGuardWithNoLongerAuthenticatedTriggersLogout()
{
$this->expectException(\Symfony\Component\Security\Core\Exception\AuthenticationExpiredException::class);
$this->expectException(AuthenticationExpiredException::class);
$providerKey = 'my_firewall_abc';

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

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

public function testSupportsChecksGuardAuthenticatorsTokenOrigin()
{
$authenticatorA = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$authenticatorB = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$authenticatorA = $this->createMock(AuthenticatorInterface::class);
$authenticatorB = $this->createMock(AuthenticatorInterface::class);
$authenticators = [$authenticatorA, $authenticatorB];

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

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

public function testAuthenticateFailsOnNonOriginatingToken()
{
$this->expectException(\Symfony\Component\Security\Core\Exception\AuthenticationException::class);
$this->expectException(AuthenticationException::class);
$this->expectExceptionMessageMatches('/second_firewall_0/');
$authenticatorA = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
$authenticatorA = $this->createMock(AuthenticatorInterface::class);
$authenticators = [$authenticatorA];

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

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

protected function setUp(): void
{
$this->userProvider = $this->getMockBuilder(\Symfony\Component\Security\Core\User\UserProviderInterface::class)->getMock();
$this->userChecker = $this->getMockBuilder(\Symfony\Component\Security\Core\User\UserCheckerInterface::class)->getMock();
$this->preAuthenticationToken = $this->getMockBuilder(PreAuthenticationGuardToken::class)
->disableOriginalConstructor()
->getMock();
$this->userProvider = $this->createMock(UserProviderInterface::class);
$this->userChecker = $this->createMock(UserCheckerInterface::class);
$this->preAuthenticationToken = $this->createMock(PreAuthenticationGuardToken::class);
}

protected function tearDown(): void
Expand Down

0 comments on commit a191352

Please sign in to comment.