|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\FrameworkBundle\Tests\Functional; |
| 13 | + |
| 14 | +use Symfony\Component\HttpFoundation\Request; |
| 15 | +use Symfony\Component\HttpFoundation\Response; |
| 16 | +use Symfony\Component\HttpKernel\Attribute\Cache; |
| 17 | +use Symfony\Component\HttpKernel\Controller\ValueResolverInterface; |
| 18 | +use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; |
| 19 | +use Symfony\Component\Security\Core\User\InMemoryUser; |
| 20 | +use Symfony\Component\Security\Http\Attribute\IsGranted; |
| 21 | + |
| 22 | +class CacheAttributeListenerTest extends AbstractWebTestCase |
| 23 | +{ |
| 24 | + public function testAnonimousUserWithEtag() |
| 25 | + { |
| 26 | + $client = self::createClient(['test_case' => 'CacheAttributeListener']); |
| 27 | + |
| 28 | + $client->request('GET', '/', server: ['HTTP_IF_NONE_MATCH' => sprintf('"%s"', hash('sha256', '12345'))]); |
| 29 | + |
| 30 | + self::assertTrue($client->getResponse()->isRedirect('http://localhost/login')); |
| 31 | + } |
| 32 | + |
| 33 | + public function testAnonimousUserWithoutEtag() |
| 34 | + { |
| 35 | + $client = self::createClient(['test_case' => 'CacheAttributeListener']); |
| 36 | + |
| 37 | + $client->request('GET', '/'); |
| 38 | + |
| 39 | + self::assertTrue($client->getResponse()->isRedirect('http://localhost/login')); |
| 40 | + } |
| 41 | + |
| 42 | + public function testLoggedInUserWithEtag() |
| 43 | + { |
| 44 | + $client = self::createClient(['test_case' => 'CacheAttributeListener']); |
| 45 | + |
| 46 | + $client->loginUser(new InMemoryUser('the-username', 'the-password', ['ROLE_USER'])); |
| 47 | + $client->request('GET', '/', server: ['HTTP_IF_NONE_MATCH' => sprintf('"%s"', hash('sha256', '12345'))]); |
| 48 | + |
| 49 | + $response = $client->getResponse(); |
| 50 | + |
| 51 | + self::assertSame(304, $response->getStatusCode()); |
| 52 | + self::assertSame('', $response->getContent()); |
| 53 | + } |
| 54 | + |
| 55 | + public function testLoggedInUserWithoutEtag() |
| 56 | + { |
| 57 | + $client = self::createClient(['test_case' => 'CacheAttributeListener']); |
| 58 | + |
| 59 | + $client->loginUser(new InMemoryUser('the-username', 'the-password', ['ROLE_USER'])); |
| 60 | + $client->request('GET', '/'); |
| 61 | + |
| 62 | + $response = $client->getResponse(); |
| 63 | + |
| 64 | + self::assertSame(200, $response->getStatusCode()); |
| 65 | + self::assertSame('Hi there!', $response->getContent()); |
| 66 | + } |
| 67 | +} |
| 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 | + |
| 90 | +class WithAttributesController |
| 91 | +{ |
| 92 | + #[IsGranted('ROLE_USER')] |
| 93 | + #[Cache(etag: 'post.getEtag()')] |
| 94 | + public function __invoke(Post $post): Response |
| 95 | + { |
| 96 | + return new Response('Hi there!'); |
| 97 | + } |
| 98 | +} |
0 commit comments