diff --git a/tests/Controller/LoginControllerTest.php b/tests/Controller/LoginControllerTest.php index 6cde6fc8c..0ccc04dc3 100644 --- a/tests/Controller/LoginControllerTest.php +++ b/tests/Controller/LoginControllerTest.php @@ -15,6 +15,7 @@ use HWI\Bundle\OAuthBundle\Security\Core\Exception\AccountNotLinkedException; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use Symfony\Bundle\SecurityBundle\Security; use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; @@ -23,7 +24,7 @@ use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException; use Symfony\Component\Security\Core\Exception\AuthenticationException; -use Symfony\Component\Security\Core\Security; +use Symfony\Component\Security\Core\Security as DeprecatedSecurity; use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; use Twig\Environment; @@ -119,7 +120,7 @@ public function testRegistrationRedirect(): void { $this->request->attributes = new ParameterBag( [ - Security::AUTHENTICATION_ERROR => new AccountNotLinkedException(), + $this->getSecurityErrorKey() => new AccountNotLinkedException(), ] ); @@ -144,7 +145,7 @@ public function testRequestError(): void $this->request->attributes = new ParameterBag( [ - Security::AUTHENTICATION_ERROR => $authenticationException, + $this->getSecurityErrorKey() => $authenticationException, ] ); @@ -164,7 +165,7 @@ public function testSessionError(): void $this->session->expects($this->once()) ->method('has') - ->with(Security::AUTHENTICATION_ERROR) + ->with($this->getSecurityErrorKey()) ->willReturn(true) ; @@ -172,7 +173,7 @@ public function testSessionError(): void $this->session->expects($this->once()) ->method('get') - ->with(Security::AUTHENTICATION_ERROR) + ->with($this->getSecurityErrorKey()) ->willReturn($authenticationException) ; @@ -207,4 +208,9 @@ private function createController(): LoginController 'IS_AUTHENTICATED_REMEMBERED' ); } + + private function getSecurityErrorKey(): string + { + return class_exists(DeprecatedSecurity::class) ? DeprecatedSecurity::AUTHENTICATION_ERROR : Security::AUTHENTICATION_ERROR; + } } diff --git a/tests/Functional/Controller/LoginControllerTest.php b/tests/Functional/Controller/LoginControllerTest.php index 87e788e22..ddba5774c 100644 --- a/tests/Functional/Controller/LoginControllerTest.php +++ b/tests/Functional/Controller/LoginControllerTest.php @@ -16,10 +16,10 @@ use HWI\Bundle\OAuthBundle\Security\Core\Exception\AccountNotLinkedException; use HWI\Bundle\OAuthBundle\Tests\Functional\AuthenticationHelperTrait; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use Symfony\Bundle\SecurityBundle\Security; use Symfony\Component\HttpClient\MockHttpClient; -use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; use Symfony\Component\Security\Core\Exception\UserNotFoundException; -use Symfony\Component\Security\Core\Security; +use Symfony\Component\Security\Core\Security as DeprecatedSecurity; use Symfony\Contracts\HttpClient\HttpClientInterface; final class LoginControllerTest extends WebTestCase @@ -48,7 +48,7 @@ public function testRedirectingToRegistrationFormWithError(): void $client = self::createClient(); $session = $this->getSession($client); - $session->set(Security::AUTHENTICATION_ERROR, new AccountNotLinkedException()); + $session->set($this->getSecurityErrorKey(), new AccountNotLinkedException()); $this->saveSession($client, $session); @@ -67,10 +67,10 @@ public function testLoginPageWithError(): void $client = self::createClient(); $client->getContainer()->set('hwi_oauth.http_client', $httpClient); - $exception = $this->createUserNotFoundException(); + $exception = new UserNotFoundException(); $session = $this->getSession($client); - $session->set(Security::AUTHENTICATION_ERROR, $exception); + $session->set($this->getSecurityErrorKey(), $exception); $this->logIn($client, $session); @@ -82,12 +82,8 @@ public function testLoginPageWithError(): void $this->assertSame($exception->getMessageKey(), $crawler->filter('span')->text(), $response->getContent()); } - private function createUserNotFoundException() + private function getSecurityErrorKey(): string { - if (class_exists(UserNotFoundException::class)) { - return new UserNotFoundException(); - } - - return new UsernameNotFoundException(); + return class_exists(DeprecatedSecurity::class) ? DeprecatedSecurity::AUTHENTICATION_ERROR : Security::AUTHENTICATION_ERROR; } }