Skip to content

Commit 0b769fc

Browse files
committed
Merge branch 'fix-cache-provider'
2 parents 67e749c + 4e1be3d commit 0b769fc

File tree

5 files changed

+53
-17
lines changed

5 files changed

+53
-17
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ Changelog
33

44
See also the [GitHub releases page](https://github.com/FriendsOfSymfony/FOSHttpCache/releases).
55

6+
2.5.3
7+
-----
8+
9+
### Symfony HttpCache
10+
11+
* Fixed: Handle HttpCache not available in KernelDispatcher and fix return
12+
type annotations - if HttpCache is not set, it can't be returned.
13+
614
2.5.2
715
-----
816

src/SymfonyCache/HttpCacheAware.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
trait HttpCacheAware
2020
{
2121
/**
22-
* @var HttpKernelInterface
22+
* @var HttpKernelInterface|null
2323
*/
2424
private $httpCache;
2525

2626
/**
27-
* @return HttpKernelInterface
27+
* @return HttpKernelInterface|null
2828
*/
2929
public function getHttpCache()
3030
{

src/SymfonyCache/HttpCacheProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
interface HttpCacheProvider
1717
{
1818
/**
19-
* @return HttpKernelInterface
19+
* @return HttpKernelInterface|null
2020
*/
2121
public function getHttpCache();
2222
}

src/SymfonyCache/KernelDispatcher.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace FOS\HttpCache\SymfonyCache;
1313

1414
use FOS\HttpCache\Exception\ExceptionCollection;
15+
use FOS\HttpCache\Exception\ProxyUnreachableException;
1516
use FOS\HttpCache\ProxyClient\Dispatcher;
1617
use Psr\Http\Message\RequestInterface;
1718
use Symfony\Component\HttpFoundation\Request;
@@ -96,6 +97,10 @@ public function flush()
9697

9798
$httpCache = $this->httpCacheProvider->getHttpCache();
9899

100+
if (null === $httpCache) {
101+
throw new ProxyUnreachableException('Kernel did not return a HttpCache instance. Did you forget $kernel->setHttpCache($cacheKernel) in your front controller?');
102+
}
103+
99104
foreach ($queue as $request) {
100105
try {
101106
$httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, false);

tests/Unit/SymfonyCache/KernelDispatcherTest.php

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace FOS\HttpCache\Tests\Unit\SymfonyCache;
1313

14+
use FOS\HttpCache\Exception\ProxyUnreachableException;
1415
use FOS\HttpCache\SymfonyCache\HttpCacheProvider;
1516
use FOS\HttpCache\SymfonyCache\KernelDispatcher;
1617
use GuzzleHttp\Psr7\Request as Psr7Request;
@@ -26,19 +27,6 @@ class KernelDispatcherTest extends TestCase
2627
{
2728
public function testFlush()
2829
{
29-
$headers = [
30-
'Content-Type' => 'foobar',
31-
'Cookie' => 'foo=bar; foscacheis=awesome',
32-
'X-Cache-Tags' => 'foo,bar,stuff',
33-
];
34-
35-
$psr7Request = new Psr7Request(
36-
'PURGETAGS',
37-
'http://127.0.0.1/foobar?query=string&more=stuff',
38-
$headers,
39-
'super content'
40-
);
41-
4230
$httpCache = $this->createMock(HttpCache::class);
4331
$httpCache->expects($this->once())
4432
->method('handle')
@@ -64,8 +52,43 @@ public function testFlush()
6452
->willReturn($httpCache);
6553

6654
$dispatcher = new KernelDispatcher($kernel);
67-
$dispatcher->invalidate($psr7Request);
55+
$dispatcher->invalidate($this->getRequest());
6856

6957
$dispatcher->flush();
7058
}
59+
60+
public function testFlushWithoutHttpCache()
61+
{
62+
$this->expectException(ProxyUnreachableException::class);
63+
$this->expectExceptionMessage('Kernel did not return a HttpCache instance. Did you forget $kernel->setHttpCache($cacheKernel) in your front controller?');
64+
65+
$kernel = $this->createMock(HttpCacheProvider::class);
66+
$kernel->expects($this->once())
67+
->method('getHttpCache')
68+
->willReturn(null);
69+
70+
$dispatcher = new KernelDispatcher($kernel);
71+
$dispatcher->invalidate($this->getRequest());
72+
73+
$dispatcher->flush();
74+
}
75+
76+
/**
77+
* @return Psr7Request
78+
*/
79+
private function getRequest()
80+
{
81+
$headers = [
82+
'Content-Type' => 'foobar',
83+
'Cookie' => 'foo=bar; foscacheis=awesome',
84+
'X-Cache-Tags' => 'foo,bar,stuff',
85+
];
86+
87+
return new Psr7Request(
88+
'PURGETAGS',
89+
'http://127.0.0.1/foobar?query=string&more=stuff',
90+
$headers,
91+
'super content'
92+
);
93+
}
7194
}

0 commit comments

Comments
 (0)