Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start to remove legacy things #617

Merged
merged 3 commits into from Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 0 additions & 11 deletions src/EventListener/UserContextListener.php
Expand Up @@ -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.
*
Expand Down
Expand Up @@ -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);
}
}
14 changes: 1 addition & 13 deletions src/UserContext/RoleProvider.php
Expand Up @@ -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.
Expand Down Expand Up @@ -49,21 +47,11 @@ 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
dbu marked this conversation as resolved.
Show resolved Hide resolved
$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);
Expand Down
5 changes: 0 additions & 5 deletions tests/Functional/Command/CommandTestCase.php
Expand Up @@ -12,18 +12,13 @@
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;
use Symfony\Component\Console\Input\StringInput;
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;
Expand Down
9 changes: 0 additions & 9 deletions tests/Functional/EventListener/FlashMessageListenerTest.php
Expand Up @@ -13,7 +13,6 @@

use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpKernel\Kernel;

class FlashMessageListenerTest extends WebTestCase
{
Expand All @@ -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();
Expand Down Expand Up @@ -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));
Expand Down
20 changes: 4 additions & 16 deletions tests/Functional/EventListener/SwitchUserListenerTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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();

Expand All @@ -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
Expand Down
17 changes: 4 additions & 13 deletions tests/Unit/EventListener/CacheControlListenerTest.php
Expand Up @@ -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
{
Expand Down Expand Up @@ -417,25 +410,23 @@ 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);
$response = new Response();
$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();

Expand Down
12 changes: 2 additions & 10 deletions tests/Unit/EventListener/InvalidationListenerTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
18 changes: 4 additions & 14 deletions tests/Unit/EventListener/UserContextListenerTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down