From 49c88321cd45b3837fc9b6702b807571837a3b46 Mon Sep 17 00:00:00 2001 From: toxicity1985 Date: Mon, 25 Mar 2024 10:57:11 +0100 Subject: [PATCH] remove symfony 5 legacy code (#617) --- src/EventListener/UserContextListener.php | 11 ----- ...ontextInvalidationSessionLogoutHandler.php | 45 +++++-------------- src/UserContext/RoleProvider.php | 15 +------ tests/Functional/Command/CommandTestCase.php | 5 --- .../FlashMessageListenerTest.php | 9 ---- .../EventListener/SwitchUserListenerTest.php | 20 ++------- .../CacheControlListenerTest.php | 17 ++----- .../InvalidationListenerTest.php | 12 +---- .../EventListener/UserContextListenerTest.php | 18 ++------ tests/Unit/UserContext/RoleProviderTest.php | 35 ++------------- 10 files changed, 29 insertions(+), 158 deletions(-) diff --git a/src/EventListener/UserContextListener.php b/src/EventListener/UserContextListener.php index 3f222a26..e49d9db5 100644 --- a/src/EventListener/UserContextListener.php +++ b/src/EventListener/UserContextListener.php @@ -18,24 +18,13 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestMatcherInterface; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpKernel\Event\FilterResponseEvent; -use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\Event\ResponseEvent; use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener; use Symfony\Component\HttpKernel\HttpKernelInterface; -use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\OptionsResolver\OptionsResolver; -if (Kernel::MAJOR_VERSION >= 5) { - class_alias(RequestEvent::class, 'FOS\HttpCacheBundle\EventListener\UserContextRequestEvent'); - class_alias(ResponseEvent::class, 'FOS\HttpCacheBundle\EventListener\UserContextResponseEvent'); -} else { - class_alias(GetResponseEvent::class, 'FOS\HttpCacheBundle\EventListener\UserContextRequestEvent'); - class_alias(FilterResponseEvent::class, 'FOS\HttpCacheBundle\EventListener\UserContextResponseEvent'); -} - /** * Check requests and responses with the matcher. * diff --git a/src/Security/Http/Logout/ContextInvalidationSessionLogoutHandler.php b/src/Security/Http/Logout/ContextInvalidationSessionLogoutHandler.php index 6ad6a352..5c48d6b7 100644 --- a/src/Security/Http/Logout/ContextInvalidationSessionLogoutHandler.php +++ b/src/Security/Http/Logout/ContextInvalidationSessionLogoutHandler.php @@ -12,47 +12,24 @@ namespace FOS\HttpCacheBundle\Security\Http\Logout; use FOS\HttpCacheBundle\UserContextInvalidator; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpKernel\Kernel; -use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Http\Event\LogoutEvent; use Symfony\Component\Security\Http\EventListener\SessionLogoutListener; -use Symfony\Component\Security\Http\Logout\SessionLogoutHandler; -if (Kernel::MAJOR_VERSION >= 6) { - final class ContextInvalidationSessionLogoutHandler extends SessionLogoutListener - { - private $invalidator; - - public function __construct(UserContextInvalidator $invalidator) - { - $this->invalidator = $invalidator; - } +final class ContextInvalidationSessionLogoutHandler extends SessionLogoutListener +{ + private $invalidator; - public function onLogout(LogoutEvent $event): void - { - if ($event->getRequest()->hasSession()) { - $this->invalidator->invalidateContext($event->getRequest()->getSession()->getId()); - } - - parent::onLogout($event); - } - } -} else { - final class ContextInvalidationSessionLogoutHandler extends SessionLogoutHandler + public function __construct(UserContextInvalidator $invalidator) { - private $invalidator; + $this->invalidator = $invalidator; + } - public function __construct(UserContextInvalidator $invalidator) - { - $this->invalidator = $invalidator; + public function onLogout(LogoutEvent $event): void + { + if ($event->getRequest()->hasSession()) { + $this->invalidator->invalidateContext($event->getRequest()->getSession()->getId()); } - public function logout(Request $request, Response $response, TokenInterface $token) - { - $this->invalidator->invalidateContext($request->getSession()->getId()); - parent::logout($request, $response, $token); - } + parent::onLogout($event); } } diff --git a/src/UserContext/RoleProvider.php b/src/UserContext/RoleProvider.php index f621e039..d5dfd4ec 100644 --- a/src/UserContext/RoleProvider.php +++ b/src/UserContext/RoleProvider.php @@ -14,10 +14,8 @@ use FOS\HttpCache\UserContext\ContextProvider; use FOS\HttpCache\UserContext\UserContext; use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; -use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Security\Core\Authentication\Token\NullToken; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; -use Symfony\Component\Security\Core\Role\Role; /** * The RoleProvider adds roles to the UserContext for the hash generation. @@ -49,21 +47,10 @@ public function updateUserContext(UserContext $context): void $token = $this->tokenStorage->getToken(); if (null === $token) { - if (Kernel::MAJOR_VERSION < 6) { - return; - } - - // Symfony 6 no longer provides the AnonymousToken, use the NullToken to generate the same hash for non-logged in users as before $token = new NullToken(); } - if (method_exists($token, 'getRoleNames')) { - $roles = $token->getRoleNames(); - } else { - $roles = array_map(static function (Role $role) { - return $role->getRole(); - }, $token->getRoles()); - } + $roles = $token->getRoleNames(); // Order is not important for roles and should not change hash. sort($roles); diff --git a/tests/Functional/Command/CommandTestCase.php b/tests/Functional/Command/CommandTestCase.php index c22dd359..62d306aa 100644 --- a/tests/Functional/Command/CommandTestCase.php +++ b/tests/Functional/Command/CommandTestCase.php @@ -12,7 +12,6 @@ namespace FOS\HttpCacheBundle\Tests\Functional\Command; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; -use Symfony\Bundle\FrameworkBundle\Client; use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Bundle\FrameworkBundle\KernelBrowser; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; @@ -20,10 +19,6 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\StreamOutput; -if (!\class_exists(KernelBrowser::class)) { - \class_alias(Client::class, KernelBrowser::class); -} - abstract class CommandTestCase extends WebTestCase { use MockeryPHPUnitIntegration; diff --git a/tests/Functional/EventListener/FlashMessageListenerTest.php b/tests/Functional/EventListener/FlashMessageListenerTest.php index 781fc1e3..788768ac 100644 --- a/tests/Functional/EventListener/FlashMessageListenerTest.php +++ b/tests/Functional/EventListener/FlashMessageListenerTest.php @@ -13,7 +13,6 @@ use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; -use Symfony\Component\HttpKernel\Kernel; class FlashMessageListenerTest extends WebTestCase { @@ -24,10 +23,6 @@ public function testFlashMessageCookieIsSet() $client = static::createClient(); $client->request('GET', '/flash'); - if (Kernel::MAJOR_VERSION < 6) { - $session = static::$kernel->getContainer()->get('session'); - $this->assertFalse($session->isStarted()); - } $response = $client->getResponse(); $this->assertEquals('flash', $response->getContent()); $cookies = $response->headers->getCookies(); @@ -56,10 +51,6 @@ public function testFlashMessageCookieIsSetOnRedirect() $client->setMaxRedirects(2); $client->request('GET', '/flash-redirect'); - if (Kernel::MAJOR_VERSION < 6) { - $session = static::$kernel->getContainer()->get('session'); - $this->assertFalse($session->isStarted()); - } $response = $client->getResponse(); $cookies = $response->headers->getCookies(); $this->assertGreaterThanOrEqual(1, $cookies, implode(',', $cookies)); diff --git a/tests/Functional/EventListener/SwitchUserListenerTest.php b/tests/Functional/EventListener/SwitchUserListenerTest.php index 735d3f12..d8131da7 100644 --- a/tests/Functional/EventListener/SwitchUserListenerTest.php +++ b/tests/Functional/EventListener/SwitchUserListenerTest.php @@ -14,21 +14,14 @@ use FOS\HttpCache\ProxyClient\Varnish; use FOS\HttpCacheBundle\Tests\Functional\SessionHelperTrait; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; -use Symfony\Bundle\FrameworkBundle\Client; use Symfony\Bundle\FrameworkBundle\KernelBrowser; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\BrowserKit\Cookie; -use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\Security\Core\User\InMemoryUser; -use Symfony\Component\Security\Core\User\User; use Symfony\Component\Security\Core\User\UserInterface; -if (!\class_exists(KernelBrowser::class)) { - \class_alias(Client::class, KernelBrowser::class); -} - class SwitchUserListenerTest extends WebTestCase { use MockeryPHPUnitIntegration; @@ -69,7 +62,7 @@ public function testInvalidateContext() ; $client = static::createClient(); - $container = method_exists($this, 'getContainer') ? self::getContainer() : (property_exists($this, 'container') ? self::$container : $client->getContainer()); + $container = $client->getContainer(); $container->set('fos_http_cache.proxy_client.varnish', $mock); $this->loginAsAdmin($client); @@ -85,12 +78,11 @@ public function loginAsAdmin(KernelBrowser $client) return; } - $container = method_exists($this, 'getContainer') ? self::getContainer() : (property_exists($this, 'container') ? self::$container : $client->getContainer()); - $session = $container->get('session'); + $session = $client->getContainer()->get('session'); $user = $this->createAdminUser(); - $token = new UsernamePasswordToken($user, null, self::FIREWALL_NAME, $user->getRoles()); + $token = new UsernamePasswordToken($user, self::FIREWALL_NAME, $user->getRoles()); $session->set('_security_'.self::FIREWALL_NAME, serialize($token)); $session->save(); @@ -100,11 +92,7 @@ public function loginAsAdmin(KernelBrowser $client) private function createAdminUser(): UserInterface { - if (Kernel::MAJOR_VERSION >= 6) { - return new InMemoryUser('admin', 'admin', ['ROLE_ADMIN', 'ROLE_ALLOWED_TO_SWITCH']); - } - - return new User('admin', 'admin', ['ROLE_ADMIN', 'ROLE_ALLOWED_TO_SWITCH']); + return new InMemoryUser('admin', 'admin', ['ROLE_ADMIN', 'ROLE_ALLOWED_TO_SWITCH']); } protected static function createKernel(array $options = []): KernelInterface diff --git a/tests/Unit/EventListener/CacheControlListenerTest.php b/tests/Unit/EventListener/CacheControlListenerTest.php index d1078fed..b56fd556 100755 --- a/tests/Unit/EventListener/CacheControlListenerTest.php +++ b/tests/Unit/EventListener/CacheControlListenerTest.php @@ -14,19 +14,12 @@ use FOS\HttpCacheBundle\EventListener\CacheControlListener; use FOS\HttpCacheBundle\Http\RuleMatcherInterface; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpKernel\Event\FilterResponseEvent; use Symfony\Component\HttpKernel\Event\ResponseEvent; use Symfony\Component\HttpKernel\HttpKernelInterface; -use Symfony\Component\HttpKernel\Kernel; - -if (Kernel::MAJOR_VERSION >= 5) { - class_alias(ResponseEvent::class, 'FOS\HttpCacheBundle\Tests\Unit\EventListener\CacheControlResponseEvent'); -} else { - class_alias(FilterResponseEvent::class, 'FOS\HttpCacheBundle\Tests\Unit\EventListener\CacheControlResponseEvent'); -} class CacheControlListenerTest extends TestCase { @@ -417,7 +410,7 @@ public function testUnsafeMethod() * Build the filter response event with a mock kernel and default request * and response objects. */ - protected function buildEvent(string $method = 'GET'): CacheControlResponseEvent + protected function buildEvent(string $method = 'GET'): ResponseEvent { /** @var HttpKernelInterface $kernel */ $kernel = \Mockery::mock(HttpKernelInterface::class); @@ -425,17 +418,15 @@ protected function buildEvent(string $method = 'GET'): CacheControlResponseEvent $request = new Request(); $request->setMethod($method); - return new CacheControlResponseEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response); + return new ResponseEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response); } /** * We mock a rule matcher for tests about applying the rules. * * @param array $headers The headers to return from the matcher - * - * @return \PHPUnit_Framework_MockObject_MockObject|CacheControlListener */ - protected function getCacheControl(array $headers) + protected function getCacheControl(array $headers): CacheControlListener|MockObject { $listener = new CacheControlListener(); diff --git a/tests/Unit/EventListener/InvalidationListenerTest.php b/tests/Unit/EventListener/InvalidationListenerTest.php index 0623bb80..d863c7f1 100644 --- a/tests/Unit/EventListener/InvalidationListenerTest.php +++ b/tests/Unit/EventListener/InvalidationListenerTest.php @@ -24,20 +24,12 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestMatcher\AttributesRequestMatcher; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpKernel\Event\PostResponseEvent; use Symfony\Component\HttpKernel\Event\TerminateEvent; use Symfony\Component\HttpKernel\HttpKernelInterface; -use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; -if (Kernel::MAJOR_VERSION >= 5) { - class_alias(TerminateEvent::class, 'FOS\HttpCacheBundle\Tests\Unit\EventListener\InvalidationTerminateEvent'); -} else { - class_alias(PostResponseEvent::class, 'FOS\HttpCacheBundle\Tests\Unit\EventListener\InvalidationTerminateEvent'); -} - class InvalidationListenerTest extends TestCase { use MockeryPHPUnitIntegration; @@ -194,9 +186,9 @@ public function testOnConsoleTerminate() $this->listener->onConsoleTerminate($event); } - protected function getEvent(Request $request, Response $response = null): InvalidationTerminateEvent + protected function getEvent(Request $request, Response $response = null): TerminateEvent { - return new InvalidationTerminateEvent( + return new TerminateEvent( \Mockery::mock(HttpKernelInterface::class), $request, null !== $response ? $response : new Response() diff --git a/tests/Unit/EventListener/UserContextListenerTest.php b/tests/Unit/EventListener/UserContextListenerTest.php index e25b036e..97532d29 100644 --- a/tests/Unit/EventListener/UserContextListenerTest.php +++ b/tests/Unit/EventListener/UserContextListenerTest.php @@ -19,22 +19,12 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestMatcherInterface; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpKernel\Event\FilterResponseEvent; -use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\Event\ResponseEvent; use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\Kernel; -if (Kernel::MAJOR_VERSION >= 5) { - class_alias(RequestEvent::class, 'FOS\HttpCacheBundle\Tests\Unit\EventListener\UserContextRequestEvent'); - class_alias(ResponseEvent::class, 'FOS\HttpCacheBundle\Tests\Unit\EventListener\UserContextResponseEvent'); -} else { - class_alias(GetResponseEvent::class, 'FOS\HttpCacheBundle\Tests\Unit\EventListener\UserContextRequestEvent'); - class_alias(FilterResponseEvent::class, 'FOS\HttpCacheBundle\Tests\Unit\EventListener\UserContextResponseEvent'); -} - class UserContextListenerTest extends TestCase { use MockeryPHPUnitIntegration; @@ -565,18 +555,18 @@ public function testFullRequestHashChanged() $this->assertEquals('max-age=0, no-cache, no-store, private, s-maxage=0', $event->getResponse()->headers->get('Cache-Control')); } - protected function getKernelRequestEvent(Request $request, $type = HttpKernelInterface::MAIN_REQUEST): UserContextRequestEvent + protected function getKernelRequestEvent(Request $request, $type = HttpKernelInterface::MAIN_REQUEST): RequestEvent { - return new UserContextRequestEvent( + return new RequestEvent( \Mockery::mock(HttpKernelInterface::class), $request, $type ); } - protected function getKernelResponseEvent(Request $request, Response $response = null, $type = HttpKernelInterface::MAIN_REQUEST): UserContextResponseEvent + protected function getKernelResponseEvent(Request $request, Response $response = null, $type = HttpKernelInterface::MAIN_REQUEST): ResponseEvent { - return new UserContextResponseEvent( + return new ResponseEvent( \Mockery::mock(HttpKernelInterface::class), $request, $type, diff --git a/tests/Unit/UserContext/RoleProviderTest.php b/tests/Unit/UserContext/RoleProviderTest.php index 0e21f904..1cf6796f 100644 --- a/tests/Unit/UserContext/RoleProviderTest.php +++ b/tests/Unit/UserContext/RoleProviderTest.php @@ -16,11 +16,8 @@ use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use PHPUnit\Framework\TestCase; use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; -use Symfony\Component\HttpKernel\Kernel; -use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; -use Symfony\Component\Security\Core\Role\Role; class RoleProviderTest extends TestCase { @@ -28,19 +25,9 @@ class RoleProviderTest extends TestCase public function testProvider() { - if (Kernel::MAJOR_VERSION >= 6) { - $token = \Mockery::mock(TokenInterface::class); - $token->shouldReceive('getRoleNames')->andReturn(['ROLE_USER']); - $token->shouldNotReceive('getRoles'); - } elseif (method_exists(AnonymousToken::class, 'getRoleNames')) { - $token = \Mockery::mock(AnonymousToken::class); - $token->shouldReceive('getRoleNames')->andReturn(['ROLE_USER']); - $token->shouldNotReceive('getRoles'); - } else { - $token = \Mockery::mock(TokenInterface::class); - $token->shouldReceive('getRoles')->andReturn([new Role('ROLE_USER')]); - $token->shouldNotReceive('getRoleNames'); - } + $token = \Mockery::mock(TokenInterface::class); + $token->shouldReceive('getRoleNames')->andReturn(['ROLE_USER']); + $token->shouldNotReceive('getRoles'); $securityContext = $this->getTokenStorageMock(); $securityContext->shouldReceive('getToken')->andReturn($token); @@ -55,22 +42,6 @@ public function testProvider() ], $userContext->getParameters()); } - public function testProviderWithoutToken() - { - if (Kernel::MAJOR_VERSION >= 6) { - $this->markTestSkipped('No longer applicable with Symfony 6'); - } - $securityContext = $this->getTokenStorageMock(); - $securityContext->shouldReceive('getToken')->andReturn(null); - - $userContext = new UserContext(); - $provider = new RoleProvider($securityContext); - - $provider->updateUserContext($userContext); - - $this->assertEmpty($userContext->getParameters()); - } - public function testNotUnderFirewall() { $this->expectException(InvalidConfigurationException::class);