11
11
12
12
namespace Symfony \Bundle \FrameworkBundle \Tests \Functional ;
13
13
14
+ use Symfony \Component \HttpFoundation \Request ;
14
15
use Symfony \Component \HttpFoundation \Response ;
15
16
use Symfony \Component \HttpKernel \Attribute \Cache ;
17
+ use Symfony \Component \HttpKernel \Controller \ValueResolverInterface ;
18
+ use Symfony \Component \HttpKernel \ControllerMetadata \ArgumentMetadata ;
16
19
use Symfony \Component \Security \Core \User \InMemoryUser ;
17
20
use Symfony \Component \Security \Http \Attribute \IsGranted ;
18
21
19
- class AttributeListenerPriorityTest extends AbstractWebTestCase
22
+ class CacheAttributeListenerTest extends AbstractWebTestCase
20
23
{
21
24
public function testAnonimousUserWithEtag ()
22
25
{
23
- $ client = self ::createClient (['test_case ' => 'AttributeListenerPriority ' ]);
26
+ $ client = self ::createClient (['test_case ' => 'CacheAttributeListener ' ]);
24
27
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 ' ))]);
26
29
27
30
self ::assertTrue ($ client ->getResponse ()->isRedirect ('http://localhost/login ' ));
28
31
}
29
32
30
33
public function testAnonimousUserWithoutEtag ()
31
34
{
32
- $ client = self ::createClient (['test_case ' => 'AttributeListenerPriority ' ]);
35
+ $ client = self ::createClient (['test_case ' => 'CacheAttributeListener ' ]);
33
36
34
- $ client ->request ('GET ' , '/12345 ' );
37
+ $ client ->request ('GET ' , '/ ' );
35
38
36
39
self ::assertTrue ($ client ->getResponse ()->isRedirect ('http://localhost/login ' ));
37
40
}
38
41
39
42
public function testLoggedInUserWithEtag ()
40
43
{
41
- $ client = self ::createClient (['test_case ' => 'AttributeListenerPriority ' ]);
44
+ $ client = self ::createClient (['test_case ' => 'CacheAttributeListener ' ]);
42
45
43
46
$ 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 ' ))]);
45
48
46
49
$ response = $ client ->getResponse ();
47
50
@@ -51,10 +54,10 @@ public function testLoggedInUserWithEtag()
51
54
52
55
public function testLoggedInUserWithoutEtag ()
53
56
{
54
- $ client = self ::createClient (['test_case ' => 'AttributeListenerPriority ' ]);
57
+ $ client = self ::createClient (['test_case ' => 'CacheAttributeListener ' ]);
55
58
56
59
$ client ->loginUser (new InMemoryUser ('the-username ' , 'the-password ' , ['ROLE_USER ' ]));
57
- $ client ->request ('GET ' , '/12345 ' );
60
+ $ client ->request ('GET ' , '/ ' );
58
61
59
62
$ response = $ client ->getResponse ();
60
63
@@ -63,11 +66,32 @@ public function testLoggedInUserWithoutEtag()
63
66
}
64
67
}
65
68
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
+
66
90
class WithAttributesController
67
91
{
68
92
#[IsGranted('ROLE_USER ' )]
69
- #[Cache(etag: 'etag ' )]
70
- public function __invoke (): Response
93
+ #[Cache(etag: 'post.getEtag() ' )]
94
+ public function __invoke (Post $ post ): Response
71
95
{
72
96
return new Response ('Hi there! ' );
73
97
}
0 commit comments