|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FOSHttpCacheBundle 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\HttpCacheBundle\Tests\Unit; |
|
13
|
|
|
|
|
14
|
|
|
use FOS\HttpCacheBundle\CacheManager; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
16
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
17
|
|
|
|
|
18
|
|
|
class CacheManagerTest extends \PHPUnit_Framework_TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
protected $proxyClient; |
|
21
|
|
|
|
|
22
|
|
|
public function setUp() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->proxyClient = \Mockery::mock('\FOS\HttpCache\ProxyClient\ProxyClientInterface'); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function testInvalidateRoute() |
|
28
|
|
|
{ |
|
29
|
|
|
$httpCache = \Mockery::mock('\FOS\HttpCache\ProxyClient\Invalidation\PurgeInterface') |
|
30
|
|
|
->shouldReceive('purge')->once()->with('/my/route', array()) |
|
31
|
|
|
->shouldReceive('purge')->once()->with('/route/with/params/id/123', array()) |
|
32
|
|
|
->shouldReceive('purge')->once()->with('/route/with/params/id/123', array('X-Foo' => 'bar')) |
|
33
|
|
|
->shouldReceive('flush')->once() |
|
34
|
|
|
->getMock(); |
|
35
|
|
|
|
|
36
|
|
|
$router = \Mockery::mock('\Symfony\Component\Routing\Generator\UrlGeneratorInterface') |
|
37
|
|
|
->shouldReceive('generate') |
|
38
|
|
|
->with('my_route', array(), UrlGeneratorInterface::ABSOLUTE_PATH) |
|
39
|
|
|
->andReturn('/my/route') |
|
40
|
|
|
|
|
41
|
|
|
->shouldReceive('generate') |
|
42
|
|
|
->with('route_with_params', array('id' => 123), UrlGeneratorInterface::ABSOLUTE_PATH) |
|
43
|
|
|
->andReturn('/route/with/params/id/123') |
|
44
|
|
|
->getMock(); |
|
45
|
|
|
|
|
46
|
|
|
$cacheManager = new CacheManager($httpCache, $router); |
|
47
|
|
|
|
|
48
|
|
|
$cacheManager->invalidateRoute('my_route') |
|
49
|
|
|
->invalidateRoute('route_with_params', array('id' => 123)) |
|
50
|
|
|
->invalidateRoute('route_with_params', array('id' => 123), array('X-Foo' => 'bar')) |
|
51
|
|
|
->flush(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testRefreshRoute() |
|
55
|
|
|
{ |
|
56
|
|
|
$httpCache = \Mockery::mock('\FOS\HttpCache\ProxyClient\Invalidation\RefreshInterface') |
|
57
|
|
|
->shouldReceive('refresh')->once()->with('/my/route', null) |
|
58
|
|
|
->shouldReceive('refresh')->once()->with('/route/with/params/id/123', null) |
|
59
|
|
|
->shouldReceive('refresh')->once()->with('/route/with/params/id/123', array('X-Foo' => 'bar')) |
|
60
|
|
|
->shouldReceive('flush')->never() |
|
61
|
|
|
->getMock(); |
|
62
|
|
|
|
|
63
|
|
|
$router = \Mockery::mock('\Symfony\Component\Routing\Generator\UrlGeneratorInterface') |
|
64
|
|
|
->shouldReceive('generate') |
|
65
|
|
|
->with('my_route', array(), UrlGeneratorInterface::ABSOLUTE_PATH) |
|
66
|
|
|
->andReturn('/my/route') |
|
67
|
|
|
|
|
68
|
|
|
->shouldReceive('generate') |
|
69
|
|
|
->with('route_with_params', array('id' => 123), UrlGeneratorInterface::ABSOLUTE_PATH) |
|
70
|
|
|
->andReturn('/route/with/params/id/123') |
|
71
|
|
|
->getMock(); |
|
72
|
|
|
|
|
73
|
|
|
$cacheManager = new CacheManager($httpCache, $router); |
|
74
|
|
|
|
|
75
|
|
|
$cacheManager |
|
76
|
|
|
->refreshRoute('my_route') |
|
77
|
|
|
->refreshRoute('route_with_params', array('id' => 123)) |
|
78
|
|
|
->refreshRoute('route_with_params', array('id' => 123), array('X-Foo' => 'bar')) |
|
79
|
|
|
; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @group legacy |
|
84
|
|
|
*/ |
|
85
|
|
|
public function testTagResponse() |
|
86
|
|
|
{ |
|
87
|
|
|
$ban = \Mockery::mock('\FOS\HttpCache\ProxyClient\Invalidation\BanInterface'); |
|
88
|
|
|
$router = \Mockery::mock('\Symfony\Component\Routing\Generator\UrlGeneratorInterface'); |
|
89
|
|
|
|
|
90
|
|
|
$tags1 = array('post-1', 'posts'); |
|
91
|
|
|
$tags2 = array('post-2'); |
|
92
|
|
|
$tags3 = array('different'); |
|
93
|
|
|
|
|
94
|
|
|
$cacheManager = new CacheManager($ban, $router); |
|
95
|
|
|
$response = new Response(); |
|
96
|
|
|
$response->headers->set($cacheManager->getTagsHeader(), ''); |
|
|
|
|
|
|
97
|
|
|
$cacheManager->tagResponse($response, $tags1); |
|
|
|
|
|
|
98
|
|
|
$this->assertTrue($response->headers->has($cacheManager->getTagsHeader())); |
|
|
|
|
|
|
99
|
|
|
$this->assertEquals(implode(',', $tags1), $response->headers->get($cacheManager->getTagsHeader())); |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
$cacheManager->tagResponse($response, $tags2); |
|
|
|
|
|
|
102
|
|
|
$this->assertEquals(implode(',', array_merge($tags1, $tags2)), $response->headers->get($cacheManager->getTagsHeader())); |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
$cacheManager->tagResponse($response, $tags3, true); |
|
|
|
|
|
|
105
|
|
|
$this->assertEquals(implode(',', $tags3), $response->headers->get($cacheManager->getTagsHeader())); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.