Skip to content

Commit 5a32031

Browse files
committed
Add functional test for tags
Simplify config with canBeEnabled() and canBeDisabled()
1 parent 6b0f4eb commit 5a32031

File tree

6 files changed

+113
-25
lines changed

6 files changed

+113
-25
lines changed

DependencyInjection/Configuration.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,7 @@ private function addTagListenerSection(ArrayNodeDefinition $rootNode)
122122
$rootNode
123123
->children()
124124
->arrayNode('tag_listener')
125-
->addDefaultsIfNotSet()
126-
->treatFalseLike(array('enabled' => false))
127-
->treatTrueLike(array('enabled' => true))
128-
->treatNullLike(array('enabled' => true))
129-
->children()
130-
->scalarNode('enabled')
131-
->defaultTrue()
132-
->info('Whether to enable the listener. True by default.')
133-
->end()
134-
->end()
125+
->canBeDisabled()
135126
->end()
136127
->end();
137128
}
@@ -142,14 +133,8 @@ private function addFlashMessageListenerSection(ArrayNodeDefinition $rootNode)
142133
->children()
143134
->arrayNode('flash_message_listener')
144135
->canBeUnset()
145-
->treatFalseLike(array('enabled' => false))
146-
->treatTrueLike(array('enabled' => true))
147-
->treatNullLike(array('enabled' => true))
136+
->canBeEnabled()
148137
->children()
149-
->scalarNode('enabled')
150-
->defaultTrue()
151-
->info('Whether to enable the listener. Automatically set if the flash_message_listener is configured.')
152-
->end()
153138
->scalarNode('name')
154139
->defaultValue('flashes')
155140
->info('Name of the cookie to set for flashes.')
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace FOS\HttpCacheBundle\Tests\Functional\EventListener;
4+
5+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6+
7+
class TagListenerTest extends WebTestCase
8+
{
9+
public function testTagsAreSet()
10+
{
11+
$client = static::createClient();
12+
13+
$client->request('GET', '/test/list');
14+
$response = $client->getResponse();
15+
$this->assertEquals('all-items,item-123', $response->headers->get('X-Cache-Tags'));
16+
17+
$client->request('GET', '/test/123');
18+
$response = $client->getResponse();
19+
$this->assertEquals('item-123', $response->headers->get('X-Cache-Tags'));
20+
}
21+
22+
public function testTagsAreInvalidated()
23+
{
24+
$client = static::createClient();
25+
26+
$client->getContainer()->mock(
27+
'fos_http_cache.cache_manager',
28+
'\FOS\HttpCacheBundle\CacheManager'
29+
)
30+
->shouldReceive('invalidateTags')->once()->with(array('all-items'))
31+
->shouldReceive('invalidateTags')->once()->with(array('item-123'))
32+
->shouldReceive('flush')->once()
33+
;
34+
35+
$client->request('POST', '/test/123');
36+
}
37+
38+
public function testErrorIsNotInvalidated()
39+
{
40+
$client = static::createClient();
41+
42+
$client->getContainer()->mock(
43+
'fos_http_cache.cache_manager',
44+
'\FOS\HttpCacheBundle\CacheManager'
45+
)
46+
->shouldReceive('invalidateTags')->never()
47+
->shouldReceive('flush')->once()
48+
;
49+
50+
$client->request('POST', '/test/error');
51+
}
52+
53+
protected function tearDown()
54+
{
55+
static::createClient()->getContainer()->unmock('fos_http_cache.cache_manager');
56+
}
57+
}

Tests/Functional/Fixtures/Controller/TestController.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,38 @@
33
namespace FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller;
44

55
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6+
use Symfony\Component\HttpFoundation\Request;
67
use Symfony\Component\HttpFoundation\Response;
8+
use FOS\HttpCacheBundle\Configuration\Tag;
79

810
class TestController extends Controller
911
{
10-
public function testAction()
12+
/**
13+
* @Tag("all-items")
14+
* @Tag("item-123")
15+
*/
16+
public function listAction()
1117
{
12-
return new Response('hello');
18+
return new Response('All items including 123');
19+
}
20+
21+
/**
22+
* @Tag(expression="'item-'~id")
23+
*/
24+
public function itemAction(Request $request, $id)
25+
{
26+
if (!$request->isMethodSafe()) {
27+
$this->container->get('fos_http_cache.cache_manager')->invalidateTags(array('all-items'));
28+
}
29+
30+
return new Response('Item ' . $id . ' invalidated');
31+
}
32+
33+
/**
34+
* @Tag("items")
35+
*/
36+
public function errorAction()
37+
{
38+
return new Response('Forbidden', 403);
1339
}
1440
}

Tests/Functional/Fixtures/app/AppKernel.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public function registerBundles()
1212
{
1313
return array(
1414
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
15+
new \Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
1516
new \Symfony\Bundle\MonologBundle\MonologBundle(),
1617
new \FOS\HttpCacheBundle\FOSHttpCacheBundle(),
1718
);
@@ -30,14 +31,22 @@ public function registerContainerConfiguration(LoaderInterface $loader)
3031
*/
3132
public function getCacheDir()
3233
{
33-
return sys_get_temp_dir().'/fos/cache';
34+
return sys_get_temp_dir().'/fos-http-cache-bundle/cache';
3435
}
3536

3637
/**
3738
* {@inheritdoc}
3839
*/
3940
public function getLogDir()
4041
{
41-
return sys_get_temp_dir().'/fos/logs';
42+
return sys_get_temp_dir().'/fos-http-cache-bundle/logs';
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
protected function getContainerBaseClass()
49+
{
50+
return '\PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer';
4251
}
4352
}
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
test:
2-
pattern: /test
3-
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::testAction }
1+
test_list:
2+
pattern: /test/list
3+
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::listAction }
4+
methods: [GET]
5+
6+
test_error:
7+
pattern: /test/error
8+
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::errorAction }
9+
10+
test_one:
11+
pattern: /test/{id}
12+
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::itemAction }
13+
methods: [GET,POST]

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"symfony/symfony": "~2.3",
3535
"symfony/security": "~2.3",
3636
"symfony/expression-language": "~2.4",
37-
"symfony/monolog-bundle": "~2.3"
37+
"symfony/monolog-bundle": "~2.3",
38+
"polishsymfonycommunity/symfony-mocker-container": "~1.0"
3839
},
3940
"suggest": {
4041
"guzzle/http": "For sending invalidation requests",

0 commit comments

Comments
 (0)