1111
1212namespace Symfony \Bundle \FrameworkBundle \Tests \Functional ;
1313
14+ use Symfony \Component \HttpFoundation \Request ;
1415use Symfony \Component \HttpFoundation \Response ;
1516use Symfony \Component \HttpKernel \Attribute \Cache ;
17+ use Symfony \Component \HttpKernel \Controller \ValueResolverInterface ;
18+ use Symfony \Component \HttpKernel \ControllerMetadata \ArgumentMetadata ;
1619use Symfony \Component \Security \Core \User \InMemoryUser ;
1720use Symfony \Component \Security \Http \Attribute \IsGranted ;
1821
19- class AttributeListenerPriorityTest extends AbstractWebTestCase
22+ class CacheAttributeListenerTest extends AbstractWebTestCase
2023{
2124 public function testAnonimousUserWithEtag ()
2225 {
23- $ client = self ::createClient (['test_case ' => 'AttributeListenerPriority ' ]);
26+ $ client = self ::createClient (['test_case ' => 'CacheAttributeListener ' ]);
2427
25- $ client ->request ('GET ' , '/12345 ' , server: ['HTTP_IF_NONE_MATCH ' => sprintf ('"%s" ' , hash ('sha256 ' , '12345 ' ))]);
28+ $ client ->request ('GET ' , '/ ' , server: ['HTTP_IF_NONE_MATCH ' => sprintf ('"%s" ' , hash ('sha256 ' , '12345 ' ))]);
2629
2730 self ::assertTrue ($ client ->getResponse ()->isRedirect ('http://localhost/login ' ));
2831 }
2932
3033 public function testAnonimousUserWithoutEtag ()
3134 {
32- $ client = self ::createClient (['test_case ' => 'AttributeListenerPriority ' ]);
35+ $ client = self ::createClient (['test_case ' => 'CacheAttributeListener ' ]);
3336
34- $ client ->request ('GET ' , '/12345 ' );
37+ $ client ->request ('GET ' , '/ ' );
3538
3639 self ::assertTrue ($ client ->getResponse ()->isRedirect ('http://localhost/login ' ));
3740 }
3841
3942 public function testLoggedInUserWithEtag ()
4043 {
41- $ client = self ::createClient (['test_case ' => 'AttributeListenerPriority ' ]);
44+ $ client = self ::createClient (['test_case ' => 'CacheAttributeListener ' ]);
4245
4346 $ client ->loginUser (new InMemoryUser ('the-username ' , 'the-password ' , ['ROLE_USER ' ]));
44- $ client ->request ('GET ' , '/12345 ' , server: ['HTTP_IF_NONE_MATCH ' => sprintf ('"%s" ' , hash ('sha256 ' , '12345 ' ))]);
47+ $ client ->request ('GET ' , '/ ' , server: ['HTTP_IF_NONE_MATCH ' => sprintf ('"%s" ' , hash ('sha256 ' , '12345 ' ))]);
4548
4649 $ response = $ client ->getResponse ();
4750
@@ -51,10 +54,10 @@ public function testLoggedInUserWithEtag()
5154
5255 public function testLoggedInUserWithoutEtag ()
5356 {
54- $ client = self ::createClient (['test_case ' => 'AttributeListenerPriority ' ]);
57+ $ client = self ::createClient (['test_case ' => 'CacheAttributeListener ' ]);
5558
5659 $ client ->loginUser (new InMemoryUser ('the-username ' , 'the-password ' , ['ROLE_USER ' ]));
57- $ client ->request ('GET ' , '/12345 ' );
60+ $ client ->request ('GET ' , '/ ' );
5861
5962 $ response = $ client ->getResponse ();
6063
@@ -63,11 +66,32 @@ public function testLoggedInUserWithoutEtag()
6366 }
6467}
6568
69+ class TestEntityValueResolver implements ValueResolverInterface
70+ {
71+ public function resolve (Request $ request , ArgumentMetadata $ argument ): iterable
72+ {
73+ return Post::class === $ argument ->getType () ? [new Post ()] : [];
74+ }
75+ }
76+
77+ class Post
78+ {
79+ public function getId (): int
80+ {
81+ return 1 ;
82+ }
83+
84+ public function getEtag (): string
85+ {
86+ return '12345 ' ;
87+ }
88+ }
89+
6690class WithAttributesController
6791{
6892 #[IsGranted('ROLE_USER ' )]
69- #[Cache(etag: 'etag ' )]
70- public function __invoke (): Response
93+ #[Cache(etag: 'post.getEtag() ' )]
94+ public function __invoke (Post $ post ): Response
7195 {
7296 return new Response ('Hi there! ' );
7397 }
0 commit comments