Skip to content

Commit 3f6cda3

Browse files
committed
Fix invalidation not being triggered for some status codes
Fix #224. Invalidation was not triggered when the controller response had status code 204 or 302.
1 parent a716f6c commit 3f6cda3

File tree

7 files changed

+34
-8
lines changed

7 files changed

+34
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
1.3.2
5+
-----
6+
7+
* Fixed some status codes (such as 204 and 302) not triggering invalidation.
8+
49
1.3.1
510
-----
611

EventListener/InvalidationSubscriber.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ public function onKernelTerminate(PostResponseEvent $event)
8989
$response = $event->getResponse();
9090

9191
// Don't invalidate any caches if the request was unsuccessful
92-
if ($response->isSuccessful()) {
92+
if ($response->getStatusCode() >= 200
93+
&& $response->getStatusCode() < 400
94+
) {
9395
$this->handleInvalidation($request, $response);
9496
}
9597

@@ -144,7 +146,8 @@ public static function getSubscribedEvents()
144146
/**
145147
* Handle the invalidation annotations and configured invalidators.
146148
*
147-
* @param Request $request
149+
* @param Request $request
150+
* @param Response $response
148151
*/
149152
private function handleInvalidation(Request $request, Response $response)
150153
{

Http/RuleMatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function matches(Request $request, Response $response)
6969
* headers are already set. As we are about to set them, that would
7070
* always return false.
7171
*/
72-
$status = array(200, 203, 300, 301, 302, 404, 410);
72+
$status = array(200, 203, 204, 300, 301, 302, 404, 410);
7373
if (!empty($this->criteria['additional_cacheable_status'])) {
7474
$status = array_merge($status, $this->criteria['additional_cacheable_status']);
7575
}

Tests/Functional/EventListener/InvalidationSubscriberTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ public function testInvalidateRoute()
3333
$client->request('POST', '/invalidate/route/42');
3434
}
3535

36-
public function testInvalidatePath()
36+
/**
37+
* @dataProvider getStatusCodesThatTriggerInvalidation
38+
*/
39+
public function testInvalidatePath($statusCode)
3740
{
3841
$client = static::createClient();
3942

@@ -43,10 +46,13 @@ public function testInvalidatePath()
4346
)
4447
->shouldReceive('supports')->andReturn(true)
4548
->shouldReceive('invalidatePath')->once()->with('/cached')
49+
->shouldReceive('invalidatePath')->once()->with(
50+
sprintf('/invalidate/path/%s', $statusCode)
51+
)
4652
->shouldReceive('flush')->once()
4753
;
4854

49-
$client->request('POST', '/invalidate/path');
55+
$client->request('POST', sprintf('/invalidate/path/%s', $statusCode));
5056
}
5157

5258
public function testErrorIsNotInvalidated()
@@ -65,6 +71,11 @@ public function testErrorIsNotInvalidated()
6571
$client->request('POST', '/invalidate/error');
6672
}
6773

74+
public function getStatusCodesThatTriggerInvalidation()
75+
{
76+
return array(array(200), array(204), array(302));
77+
}
78+
6879
protected function tearDown()
6980
{
7081
static::createClient()->getContainer()->unmock('fos_http_cache.cache_manager');

Tests/Functional/Fixtures/Controller/InvalidationController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public function itemAction($id)
3131
/**
3232
* @InvalidatePath("/cached")
3333
*/
34-
public function otherAction()
34+
public function otherAction($statusCode)
3535
{
36-
return new Response('Done.');
36+
return new Response('Done.', $statusCode);
3737
}
3838

3939
/**

Tests/Functional/Fixtures/app/config/config.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ fos_http_cache:
2020
varnish:
2121
servers: 127.0.0.1
2222
base_url: localhost:8080
23+
invalidation:
24+
rules:
25+
-
26+
match:
27+
path: ^/invalidate/path.*
28+
routes:
29+
invalidation_path: ~
2330
tags:
2431
rules:
2532
-

Tests/Functional/Fixtures/app/config/routing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ invalidation_route:
2525
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\InvalidationController::itemAction }
2626

2727
invalidation_path:
28-
path: /invalidate/path
28+
path: /invalidate/path/{statusCode}
2929
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\InvalidationController::otherAction }
3030

3131
invalidation_error:

0 commit comments

Comments
 (0)