From 57f35c9a6c71342cc7ba824122333d5559e2eab9 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 15 Jul 2021 16:19:38 +0200 Subject: [PATCH] Avoid deprecation warnings in Symfony 5.3 --- .../Provider/AuthenticationProviderDecorator.php | 7 ++++++- .../TwoFactor/Event/AuthenticationTokenListener.php | 7 ++++++- .../TwoFactor/Trusted/TrustedDeviceTokenStorage.php | 7 ++++++- .../Provider/AuthenticationProviderDecoratorTest.php | 8 +++++++- .../TwoFactor/Event/AuthenticationTokenListenerTest.php | 8 +++++++- .../TwoFactor/Trusted/TrustedDeviceTokenStorageTest.php | 8 +++++++- 6 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/bundle/Security/Authentication/Provider/AuthenticationProviderDecorator.php b/src/bundle/Security/Authentication/Provider/AuthenticationProviderDecorator.php index 10c227b3..65d06369 100644 --- a/src/bundle/Security/Authentication/Provider/AuthenticationProviderDecorator.php +++ b/src/bundle/Security/Authentication/Provider/AuthenticationProviderDecorator.php @@ -105,7 +105,12 @@ public function __call(string $method, array $arguments) private function getRequest(): Request { - $request = $this->requestStack->getMasterRequest(); + // Compatibility for Symfony >= 5.3 + if (method_exists(RequestStack::class, 'getMainRequest')) { + $request = $this->requestStack->getMainRequest(); + } else { + $request = $this->requestStack->getMasterRequest(); + } if (null === $request) { throw new \RuntimeException('No request available'); } diff --git a/src/bundle/Security/TwoFactor/Event/AuthenticationTokenListener.php b/src/bundle/Security/TwoFactor/Event/AuthenticationTokenListener.php index 2d79dca3..4c939549 100644 --- a/src/bundle/Security/TwoFactor/Event/AuthenticationTokenListener.php +++ b/src/bundle/Security/TwoFactor/Event/AuthenticationTokenListener.php @@ -75,7 +75,12 @@ public function onAuthenticationTokenCreated(AuthenticationTokenCreatedEvent $ev private function getRequest(): Request { - $request = $this->requestStack->getMasterRequest(); + // Compatibility for Symfony >= 5.3 + if (method_exists(RequestStack::class, 'getMainRequest')) { + $request = $this->requestStack->getMainRequest(); + } else { + $request = $this->requestStack->getMasterRequest(); + } if (null === $request) { throw new \RuntimeException('No request available'); } diff --git a/src/trusted-device/Security/TwoFactor/Trusted/TrustedDeviceTokenStorage.php b/src/trusted-device/Security/TwoFactor/Trusted/TrustedDeviceTokenStorage.php index 2180f02b..45ecb126 100644 --- a/src/trusted-device/Security/TwoFactor/Trusted/TrustedDeviceTokenStorage.php +++ b/src/trusted-device/Security/TwoFactor/Trusted/TrustedDeviceTokenStorage.php @@ -135,7 +135,12 @@ private function readCookieValue(): ?string private function getRequest(): Request { - $request = $this->requestStack->getMasterRequest(); + // Compatibility for Symfony >= 5.3 + if (method_exists(RequestStack::class, 'getMainRequest')) { + $request = $this->requestStack->getMainRequest(); + } else { + $request = $this->requestStack->getMasterRequest(); + } if (null === $request) { throw new \RuntimeException('No request available'); } diff --git a/tests/Security/Authentication/Provider/AuthenticationProviderDecoratorTest.php b/tests/Security/Authentication/Provider/AuthenticationProviderDecoratorTest.php index f0c6c0b2..610b9c22 100644 --- a/tests/Security/Authentication/Provider/AuthenticationProviderDecoratorTest.php +++ b/tests/Security/Authentication/Provider/AuthenticationProviderDecoratorTest.php @@ -59,9 +59,15 @@ protected function setUp(): void $this->firewallMap = $this->createMock(FirewallMap::class); $this->requestStack = $this->createMock(RequestStack::class); + // Compatibility for Symfony >= 5.3 + if (method_exists(RequestStack::class, 'getMainRequest')) { + $method = 'getMainRequest'; + } else { + $method = 'getMasterRequest'; + } $this->requestStack ->expects($this->any()) - ->method('getMasterRequest') + ->method($method) ->willReturn($this->createMock(Request::class)); $this->decorator = new AuthenticationProviderDecorator( diff --git a/tests/Security/TwoFactor/Event/AuthenticationTokenListenerTest.php b/tests/Security/TwoFactor/Event/AuthenticationTokenListenerTest.php index 447cb8e8..d8984be8 100644 --- a/tests/Security/TwoFactor/Event/AuthenticationTokenListenerTest.php +++ b/tests/Security/TwoFactor/Event/AuthenticationTokenListenerTest.php @@ -48,9 +48,15 @@ protected function setUp(): void $this->authenticationContextFactory = $this->createMock(AuthenticationContextFactoryInterface::class); $this->requestStack = $this->createMock(RequestStack::class); + // Compatibility for Symfony >= 5.3 + if (method_exists(RequestStack::class, 'getMainRequest')) { + $method = 'getMainRequest'; + } else { + $method = 'getMasterRequest'; + } $this->requestStack ->expects($this->any()) - ->method('getMasterRequest') + ->method($method) ->willReturn($this->createMock(Request::class)); $this->listener = new AuthenticationTokenListener( diff --git a/tests/Security/TwoFactor/Trusted/TrustedDeviceTokenStorageTest.php b/tests/Security/TwoFactor/Trusted/TrustedDeviceTokenStorageTest.php index 0ea59e3b..31211d7e 100644 --- a/tests/Security/TwoFactor/Trusted/TrustedDeviceTokenStorageTest.php +++ b/tests/Security/TwoFactor/Trusted/TrustedDeviceTokenStorageTest.php @@ -35,9 +35,15 @@ protected function setUp(): void $this->tokenEncoder = $this->createMock(TrustedDeviceTokenEncoder::class); $this->request = new Request(); $requestStack = $this->createMock(RequestStack::class); + // Compatibility for Symfony >= 5.3 + if (method_exists(RequestStack::class, 'getMainRequest')) { + $method = 'getMainRequest'; + } else { + $method = 'getMasterRequest'; + } $requestStack ->expects($this->any()) - ->method('getMasterRequest') + ->method($method) ->willReturn($this->request); $this->tokenStorage = new TrustedDeviceTokenStorage($requestStack, $this->tokenEncoder, 'cookieName');