diff --git a/Tests/Authenticator/FormLoginAuthenticatorTest.php b/Tests/Authenticator/FormLoginAuthenticatorTest.php index 129df28..c54fa67 100644 --- a/Tests/Authenticator/FormLoginAuthenticatorTest.php +++ b/Tests/Authenticator/FormLoginAuthenticatorTest.php @@ -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; @@ -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()); } @@ -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()); } @@ -63,7 +65,7 @@ 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()); } @@ -71,7 +73,7 @@ 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()); } @@ -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(); diff --git a/Tests/Firewall/GuardAuthenticationListenerTest.php b/Tests/Firewall/GuardAuthenticationListenerTest.php index 7167ba3..7d1c8e7 100644 --- a/Tests/Firewall/GuardAuthenticationListenerTest.php +++ b/Tests/Firewall/GuardAuthenticationListenerTest.php @@ -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 @@ -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']; @@ -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 @@ -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 @@ -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!'); @@ -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 @@ -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 @@ -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(); @@ -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 diff --git a/Tests/GuardAuthenticatorHandlerTest.php b/Tests/GuardAuthenticatorHandlerTest.php index 6309462..58784ba 100644 --- a/Tests/GuardAuthenticatorHandlerTest.php +++ b/Tests/GuardAuthenticatorHandlerTest.php @@ -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; @@ -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 @@ -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'); diff --git a/Tests/Provider/GuardAuthenticationProviderTest.php b/Tests/Provider/GuardAuthenticationProviderTest.php index 1c810ec..1205e23 100644 --- a/Tests/Provider/GuardAuthenticationProviderTest.php +++ b/Tests/Provider/GuardAuthenticationProviderTest.php @@ -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; @@ -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) @@ -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) @@ -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) @@ -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); @@ -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'); @@ -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'); @@ -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