-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathCacheAttributeListenerTest.php
98 lines (77 loc) · 2.86 KB
/
CacheAttributeListenerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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!');
}
}