Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\Cache;
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Http\Attribute\IsGranted;

class CacheAttributeListenerTest extends AbstractWebTestCase
{
public function testAnonimousUserWithEtag()
{
$client = self::createClient(['test_case' => 'CacheAttributeListener']);

$client->request('GET', '/', server: ['HTTP_IF_NONE_MATCH' => sprintf('"%s"', hash('sha256', '12345'))]);

self::assertTrue($client->getResponse()->isRedirect('http://localhost/login'));
}

public function testAnonimousUserWithoutEtag()
{
$client = self::createClient(['test_case' => 'CacheAttributeListener']);

$client->request('GET', '/');

self::assertTrue($client->getResponse()->isRedirect('http://localhost/login'));
}

public function testLoggedInUserWithEtag()
{
$client = self::createClient(['test_case' => 'CacheAttributeListener']);

$client->loginUser(new InMemoryUser('the-username', 'the-password', ['ROLE_USER']));
$client->request('GET', '/', server: ['HTTP_IF_NONE_MATCH' => sprintf('"%s"', hash('sha256', '12345'))]);

$response = $client->getResponse();

self::assertSame(304, $response->getStatusCode());
self::assertSame('', $response->getContent());
}

public function testLoggedInUserWithoutEtag()
{
$client = self::createClient(['test_case' => 'CacheAttributeListener']);

$client->loginUser(new InMemoryUser('the-username', 'the-password', ['ROLE_USER']));
$client->request('GET', '/');

$response = $client->getResponse();

self::assertSame(200, $response->getStatusCode());
self::assertSame('Hi there!', $response->getContent());
}
}

class TestEntityValueResolver implements ValueResolverInterface
{
public function resolve(Request $request, ArgumentMetadata $argument): iterable
{
return Post::class === $argument->getType() ? [new Post()] : [];
}
}

class Post
{
public function getId(): int
{
return 1;
}

public function getEtag(): string
{
return '12345';
}
}

class WithAttributesController
{
#[IsGranted('ROLE_USER')]
#[Cache(etag: 'post.getEtag()')]
public function __invoke(Post $post): Response
{
return new Response('Hi there!');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\SecurityBundle\SecurityBundle;

return [
new FrameworkBundle(),
new SecurityBundle(),
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
imports:
- { resource: ../config/default.yml }

services:
Symfony\Bundle\FrameworkBundle\Tests\Functional\TestEntityValueResolver:
tags:
- { name: controller.argument_value_resolver, priority: 110 }

Symfony\Bundle\FrameworkBundle\Tests\Functional\WithAttributesController:
public: true

security:
providers:
main:
memory:
users:
the-username: { password: the-password, roles: [ 'ROLE_USER' ] }

firewalls:
main:
pattern: ^/
form_login:
login_path: /login
provider: main
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
with_attributes_controller:
path: /
controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\WithAttributesController
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"symfony/error-handler": "^6.1",
"symfony/event-dispatcher": "^5.4|^6.0",
"symfony/http-foundation": "^6.2",
"symfony/http-kernel": "^6.2",
"symfony/http-kernel": "^6.2.1",
Comment thread
nicolas-grekas marked this conversation as resolved.
"symfony/polyfill-mbstring": "~1.0",
"symfony/filesystem": "^5.4|^6.0",
"symfony/finder": "^5.4|^6.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function onKernelControllerArguments(ControllerArgumentsEvent $event)

public static function getSubscribedEvents(): array
{
return [KernelEvents::CONTROLLER_ARGUMENTS => ['onKernelControllerArguments', 10]];
return [KernelEvents::CONTROLLER_ARGUMENTS => ['onKernelControllerArguments', 20]];
}

private function getIsGrantedSubject(string|Expression $subjectRef, Request $request, array $arguments): mixed
Expand Down