/
CacheManagerTest.php
129 lines (104 loc) · 4.74 KB
/
CacheManagerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/*
* This file is part of the FOSHttpCacheBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\HttpCacheBundle\Tests\Unit;
use FOS\HttpCache\ProxyClient\HttpProxyClient;
use FOS\HttpCache\ProxyClient\Invalidation\PurgeCapable;
use FOS\HttpCache\ProxyClient\Invalidation\RefreshCapable;
use FOS\HttpCache\ProxyClient\ProxyClient;
use FOS\HttpCacheBundle\CacheManager;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\VarExporter\LazyObjectInterface;
class CacheManagerTest extends TestCase
{
use MockeryPHPUnitIntegration;
protected $proxyClient;
public function setUp(): void
{
$this->proxyClient = \Mockery::mock(ProxyClient::class);
}
public function testInvalidateRoute()
{
$httpCache = \Mockery::mock(PurgeCapable::class)
->shouldReceive('purge')->once()->with('/my/route', [])
->shouldReceive('purge')->once()->with('/route/with/params/id/123', [])
->shouldReceive('purge')->once()->with('/route/with/params/id/123', ['X-Foo' => 'bar'])
->shouldReceive('flush')->once()->andReturn(2)
->getMock();
$router = \Mockery::mock(UrlGeneratorInterface::class)
->shouldReceive('generate')
->with('my_route', [], UrlGeneratorInterface::ABSOLUTE_PATH)
->andReturn('/my/route')
->shouldReceive('generate')
->with('route_with_params', ['id' => 123], UrlGeneratorInterface::ABSOLUTE_PATH)
->andReturn('/route/with/params/id/123')
->getMock();
$cacheManager = new CacheManager($httpCache, $router);
$cacheManager->invalidateRoute('my_route')
->invalidateRoute('route_with_params', ['id' => 123])
->invalidateRoute('route_with_params', ['id' => 123], ['X-Foo' => 'bar'])
->flush();
}
public function testRefreshRoute()
{
$httpCache = \Mockery::mock(RefreshCapable::class)
->shouldReceive('refresh')->once()->with('/my/route', null)
->shouldReceive('refresh')->once()->with('/route/with/params/id/123', null)
->shouldReceive('refresh')->once()->with('/route/with/params/id/123', ['X-Foo' => 'bar'])
->shouldReceive('flush')->never()
->getMock();
$router = \Mockery::mock(UrlGeneratorInterface::class)
->shouldReceive('generate')
->with('my_route', [], UrlGeneratorInterface::ABSOLUTE_PATH)
->andReturn('/my/route')
->shouldReceive('generate')
->with('route_with_params', ['id' => 123], UrlGeneratorInterface::ABSOLUTE_PATH)
->andReturn('/route/with/params/id/123')
->getMock();
$cacheManager = new CacheManager($httpCache, $router);
$cacheManager
->refreshRoute('my_route')
->refreshRoute('route_with_params', ['id' => 123])
->refreshRoute('route_with_params', ['id' => 123], ['X-Foo' => 'bar'])
;
}
public function testSkipFlushOnEmptyInvalidationsAndLazyLoaded()
{
$proxyClient = \Mockery::mock(HttpProxyClient::class, LazyObjectInterface::class)
->shouldNotReceive('flush')
->shouldReceive('isLazyObjectInitialized')->andReturn(false)
->getMock();
$router = \Mockery::mock(UrlGeneratorInterface::class);
$cacheInvalidator = new CacheManager($proxyClient, $router);
$this->assertEquals(0, $cacheInvalidator->flush());
}
public function testFlushOnNotLazyLoaded()
{
$proxyClient = \Mockery::mock(HttpProxyClient::class)
->shouldReceive('flush')->andReturn(0)
->shouldNotReceive('isLazyObjectInitialized')
->getMock();
$router = \Mockery::mock(UrlGeneratorInterface::class);
$cacheInvalidator = new CacheManager($proxyClient, $router);
$this->assertEquals(0, $cacheInvalidator->flush());
}
public function testFlushOnLazyLoaded()
{
$proxyClient = \Mockery::mock(HttpProxyClient::class, LazyObjectInterface::class, PurgeCapable::class);
$proxyClient->shouldReceive('flush')->andReturn(1);
$proxyClient->shouldReceive('purge');
$proxyClient->shouldReceive('isLazyObjectInitialized')->andReturn(true);
$router = \Mockery::mock(UrlGeneratorInterface::class);
$cacheInvalidator = new CacheManager($proxyClient, $router);
$cacheInvalidator->invalidatePath('/foo');
$this->assertEquals(1, $cacheInvalidator->flush());
}
}