Skip to content

Commit

Permalink
Improve compatibility with Symfony 6.2+
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd committed Aug 26, 2023
1 parent 0c2d946 commit 16fce5b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
16 changes: 11 additions & 5 deletions tests/Controller/LoginControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -119,7 +120,7 @@ public function testRegistrationRedirect(): void
{
$this->request->attributes = new ParameterBag(
[
Security::AUTHENTICATION_ERROR => new AccountNotLinkedException(),
$this->getSecurityErrorKey() => new AccountNotLinkedException(),
]
);

Expand All @@ -144,7 +145,7 @@ public function testRequestError(): void

$this->request->attributes = new ParameterBag(
[
Security::AUTHENTICATION_ERROR => $authenticationException,
$this->getSecurityErrorKey() => $authenticationException,
]
);

Expand All @@ -164,15 +165,15 @@ public function testSessionError(): void

$this->session->expects($this->once())
->method('has')
->with(Security::AUTHENTICATION_ERROR)
->with($this->getSecurityErrorKey())
->willReturn(true)
;

$authenticationException = new AuthenticationException();

$this->session->expects($this->once())
->method('get')
->with(Security::AUTHENTICATION_ERROR)
->with($this->getSecurityErrorKey())
->willReturn($authenticationException)
;

Expand Down Expand Up @@ -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;

Check failure on line 214 in tests/Controller/LoginControllerTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to constant AUTHENTICATION_ERROR on an unknown class Symfony\Bundle\SecurityBundle\Security.
}
}
18 changes: 7 additions & 11 deletions tests/Functional/Controller/LoginControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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;

Check failure on line 87 in tests/Functional/Controller/LoginControllerTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to constant AUTHENTICATION_ERROR on an unknown class Symfony\Bundle\SecurityBundle\Security.
}
}

0 comments on commit 16fce5b

Please sign in to comment.