Skip to content

Commit 16801a4

Browse files
committed
Added functional tests for tag invalidation with single and multiple headers
1 parent bec93f3 commit 16801a4

File tree

10 files changed

+94
-5
lines changed

10 files changed

+94
-5
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ cache:
77
env:
88
global:
99
- VARNISH_VERSION=5.1
10-
- DEPENDENCIES="toflar/psr6-symfony-http-cache-store:^1.0"
10+
- DEPENDENCIES="toflar/psr6-symfony-http-cache-store:^1.1.2"
1111

1212
matrix:
1313
fast_finish: true
@@ -30,7 +30,7 @@ matrix:
3030
- php: 7.2
3131
env: DEPENDENCIES="symfony/lts:^2"
3232
- php: 7.2
33-
env: DEPENDENCIES="symfony/lts:^3 toflar/psr6-symfony-http-cache-store:^1.0"
33+
env: DEPENDENCIES="symfony/lts:^3 toflar/psr6-symfony-http-cache-store:^1.1.2"
3434

3535
# Latest commit to master
3636
- php: 7.2

src/ProxyClient/Symfony.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@ protected function configureOptions()
5858
'purge_method' => PurgeListener::DEFAULT_PURGE_METHOD,
5959
'tags_method' => PurgeTagsListener::DEFAULT_TAGS_METHOD,
6060
'tags_header' => PurgeTagsListener::DEFAULT_TAGS_HEADER,
61+
'tags_path' => '/',
6162
'header_length' => 7500,
6263
]);
6364
$resolver->setAllowedTypes('purge_method', 'string');
6465
$resolver->setAllowedTypes('tags_method', 'string');
6566
$resolver->setAllowedTypes('tags_header', 'string');
67+
$resolver->setAllowedTypes('tags_path', 'string');
6668
$resolver->setAllowedTypes('header_length', 'int');
6769

6870
return $resolver;
@@ -84,7 +86,7 @@ public function invalidateTags(array $tags)
8486
foreach (array_chunk($escapedTags, $chunkSize) as $tagchunk) {
8587
$this->queueRequest(
8688
$this->options['tags_method'],
87-
'/',
89+
$this->options['tags_path'],
8890
[$this->options['tags_header'] => implode(',', $tagchunk)],
8991
false
9092
);

src/SymfonyCache/PurgeTagsListener.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,13 @@ public function handlePurgeTags(CacheEvent $event)
117117
return;
118118
}
119119

120-
$tags = explode(',', $request->headers->get($this->tagsHeader));
120+
$tags = [];
121+
122+
foreach ($request->headers->get($this->tagsHeader, '', false) as $v) {
123+
foreach (explode(',', $v) as $tag) {
124+
$tags[] = $tag;
125+
}
126+
}
121127

122128
if ($store->invalidateTags($tags)) {
123129
$response->setStatusCode(200, 'Purged');

src/Test/SymfonyTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ protected function getProxyClient()
123123

124124
$this->proxyClient = new Symfony($httpDispatcher, [
125125
'purge_method' => 'NOTIFY',
126+
'tags_method' => 'UNSUBSCRIBE',
127+
'tags_path' => '/symfony.php/',
126128
]
127129
);
128130
}

tests/Functional/Fixtures/Symfony/AppCache.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use FOS\HttpCache\SymfonyCache\DebugListener;
1717
use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache;
1818
use FOS\HttpCache\SymfonyCache\PurgeListener;
19+
use FOS\HttpCache\SymfonyCache\PurgeTagsListener;
1920
use FOS\HttpCache\SymfonyCache\RefreshListener;
2021
use FOS\HttpCache\SymfonyCache\UserContextListener;
2122
use Symfony\Component\HttpFoundation\Request;
@@ -34,6 +35,7 @@ public function __construct(HttpKernelInterface $kernel, StoreInterface $store,
3435

3536
$this->addSubscriber(new CustomTtlListener());
3637
$this->addSubscriber(new PurgeListener(['purge_method' => 'NOTIFY']));
38+
$this->addSubscriber(new PurgeTagsListener(['tags_method' => 'UNSUBSCRIBE']));
3739
$this->addSubscriber(new RefreshListener());
3840
$this->addSubscriber(new UserContextListener());
3941
if (isset($options['debug']) && $options['debug']) {

tests/Functional/Fixtures/Symfony/AppKernel.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
3232
$response = new Response(microtime(true));
3333
$response->setCache(['max_age' => 3600, 'public' => true]);
3434

35+
return $response;
36+
case '/tags':
37+
$response = new Response(microtime(true));
38+
$response->setCache(['max_age' => 3600, 'public' => true]);
39+
$response->headers->set('X-Cache-Tags', 'tag1,tag2');
40+
41+
return $response;
42+
case '/tags_multi_header':
43+
$response = new Response(microtime(true));
44+
$response->setCache(['max_age' => 3600, 'public' => true]);
45+
$response->headers->set('X-Cache-Tags', ['tag1', 'tag2']);
46+
3547
return $response;
3648
case '/negotiation':
3749
$response = new Response(microtime(true));

tests/Functional/Fixtures/web/symfony.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
use FOS\HttpCache\Tests\Functional\Fixtures\Symfony\AppKernel;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpKernel\HttpCache\Store;
17+
use Toflar\Psr6HttpCacheStore\Psr6Store;
1718

1819
$loader = require_once __DIR__.'/../../../../vendor/autoload.php';
1920

2021
$symfonyProxy = new SymfonyProxy();
2122

2223
$kernel = new AppKernel();
23-
$kernel = new AppCache($kernel, new Store($symfonyProxy->getCacheDir()), null, ['debug' => true]);
24+
$kernel = new AppCache($kernel, new Psr6Store(['cache_directory' => $symfonyProxy->getCacheDir(), 'cache_tags_header' => 'X-Cache-Tags']), null, ['debug' => true]);
2425
$request = Request::createFromGlobals();
2526
$response = $kernel->handle($request);
2627
$response->send();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCache package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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+
header('Cache-Control: max-age=3600');
13+
header('Content-Type: text/html');
14+
header('X-Cache-Tags: tag1');
15+
header('X-Cache-Tags: tag2');
16+
header('X-Cache-Debug: 1');
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCache package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\HttpCache\Tests\Functional\ProxyClient;
13+
14+
use FOS\HttpCache\ProxyClient\Invalidation\PurgeCapable;
15+
use FOS\HttpCache\ProxyClient\Invalidation\TagCapable;
16+
17+
/**
18+
* Assertions that do the cache tag invalidation operations.
19+
*/
20+
trait InvalidateTagsAssertions
21+
{
22+
/**
23+
* Asserting that purging cache tags leads to invalidated content.
24+
*
25+
* @param PurgeCapable $proxyClient The client to send purge instructions to the cache
26+
* @param array $tags The cache tags to invalidate
27+
* @param string $path The path to get and purge, defaults to /tags.php
28+
*/
29+
protected function assertInvalidateTags(TagCapable $proxyClient, array $cacheTags, $path = '/tags.php')
30+
{
31+
$this->assertMiss($this->getResponse($path));
32+
$this->assertHit($this->getResponse($path));
33+
34+
$proxyClient->invalidateTags($cacheTags)->flush();
35+
$this->assertMiss($this->getResponse($path));
36+
}
37+
}

tests/Functional/ProxyClient/SymfonyProxyClientTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class SymfonyProxyClientTest extends SymfonyTestCase
2121
{
2222
use RefreshAssertions;
2323
use PurgeAssertions;
24+
use InvalidateTagsAssertions;
2425

2526
public function testPurge()
2627
{
@@ -46,4 +47,14 @@ public function testRefreshContentType()
4647
{
4748
$this->assertRefresh($this->getProxyClient(), '/symfony.php/negotiation');
4849
}
50+
51+
public function testInvalidateTags()
52+
{
53+
$this->assertInvalidateTags($this->getProxyClient(), ['tag1'], '/symfony.php/tags');
54+
}
55+
56+
public function testInvalidateTagsMultiHeader()
57+
{
58+
$this->assertInvalidateTags($this->getProxyClient(), ['tag2'], '/symfony.php/tags_multi_header');
59+
}
4960
}

0 commit comments

Comments
 (0)