|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
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 Sonata\CacheBundle\Tests\Adapter\Cache; |
|
13
|
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
use Sonata\Cache\CacheElement; |
|
16
|
|
|
use Sonata\CacheBundle\Adapter\VarnishCache; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
19
|
|
|
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface; |
|
20
|
|
|
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface; |
|
21
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
22
|
|
|
|
|
23
|
|
|
class VarnishCacheTest extends TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
private $router; |
|
26
|
|
|
private $controllerResolver; |
|
27
|
|
|
private $argumentResolver; |
|
28
|
|
|
private $cache; |
|
29
|
|
|
|
|
30
|
|
|
protected function setUp() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->router = $this->createMock(RouterInterface::class); |
|
33
|
|
|
$this->controllerResolver = $this->createMock(ControllerResolverInterface::class); |
|
34
|
|
|
if (interface_exists(ArgumentResolverInterface::class)) { |
|
35
|
|
|
$this->argumentResolver = $this->createMock(ArgumentResolverInterface::class); |
|
36
|
|
|
} |
|
37
|
|
|
$this->cache = new VarnishCache( |
|
38
|
|
|
'token', |
|
39
|
|
|
[], |
|
40
|
|
|
$this->router, |
|
41
|
|
|
'ban', |
|
42
|
|
|
$this->controllerResolver, |
|
43
|
|
|
interface_exists(ArgumentResolverInterface::class) ? |
|
44
|
|
|
$this->argumentResolver : |
|
45
|
|
|
null |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testInitCache() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->router->expects($this->any()) |
|
52
|
|
|
->method('generate') |
|
53
|
|
|
->will($this->returnValue( |
|
54
|
|
|
'https://sonata-project.org/cache/esi/TOKEN?controller=asdsad' |
|
55
|
|
|
)); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertTrue($this->cache->flush([])); |
|
58
|
|
|
$this->assertTrue($this->cache->flushAll()); |
|
59
|
|
|
|
|
60
|
|
|
$cacheElement = $this->cache->set(['id' => 7], 'data'); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertInstanceOf(CacheElement::class, $cacheElement); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertTrue($this->cache->has(['id' => 7])); |
|
65
|
|
|
|
|
66
|
|
|
$cacheElement = $this->cache->get([ |
|
67
|
|
|
'id' => 7, |
|
68
|
|
|
'controller' => 'foo.service::runAction', |
|
69
|
|
|
'parameters' => [], |
|
70
|
|
|
]); |
|
71
|
|
|
|
|
72
|
|
|
$this->assertInstanceOf(CacheElement::class, $cacheElement); |
|
73
|
|
|
|
|
74
|
|
|
$this->assertEquals( |
|
75
|
|
|
'<esi:include src="https://sonata-project.org/cache/esi/TOKEN?controller=asdsad"/>', |
|
76
|
|
|
$cacheElement->getData()->getContent() |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException |
|
82
|
|
|
*/ |
|
83
|
|
|
public function testActionInvalidToken(): void |
|
84
|
|
|
{ |
|
85
|
|
|
$this->router->expects($this->any()) |
|
86
|
|
|
->method('generate') |
|
87
|
|
|
->will($this->returnValue( |
|
88
|
|
|
'http://sonata-project.orf/cache/esi/TOKEN?controller=asdsad' |
|
89
|
|
|
)); |
|
90
|
|
|
|
|
91
|
|
|
$request = Request::create('cache/esi/TOKEN?controller=asdsad', 'get', [ |
|
92
|
|
|
'token' => 'wrong', |
|
93
|
|
|
]); |
|
94
|
|
|
|
|
95
|
|
|
$this->cache->cacheAction($request); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function testValidToken(): void |
|
99
|
|
|
{ |
|
100
|
|
|
$this->controllerResolver->expects($this->any()) |
|
101
|
|
|
->method('getController') |
|
102
|
|
|
->will($this->returnValue(function () { |
|
103
|
|
|
return new Response(); |
|
104
|
|
|
})); |
|
105
|
|
|
$resolver = interface_exists(ArgumentResolverInterface::class) ? |
|
106
|
|
|
$this->argumentResolver : |
|
107
|
|
|
$this->controllerResolver; |
|
108
|
|
|
|
|
109
|
|
|
$resolver->expects($this->any()) |
|
110
|
|
|
->method('getArguments') |
|
111
|
|
|
->will($this->returnValue([])); |
|
112
|
|
|
|
|
113
|
|
|
$request = Request::create('cache/esi/TOKEN', 'get', [ |
|
114
|
|
|
'token' => '44befdbd93f304ea693023aa6587729bed76a206ecdacfd9bbd9b43fcf2e1664', |
|
115
|
|
|
'parameters' => [ |
|
116
|
|
|
'controller' => 'asfsat', |
|
117
|
|
|
'parameters' => [], |
|
118
|
|
|
], |
|
119
|
|
|
]); |
|
120
|
|
|
|
|
121
|
|
|
$this->cache->cacheAction($request); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function testRunCommand(): void |
|
125
|
|
|
{ |
|
126
|
|
|
$tmpFile = tempnam(sys_get_temp_dir(), 'sonata-cache'); |
|
127
|
|
|
|
|
128
|
|
|
$cache = new VarnishCache( |
|
129
|
|
|
'token', |
|
130
|
|
|
[ |
|
131
|
|
|
sprintf("echo \"varnishadm -T 10.4.1.62:6082 -S /etc/varnish/secret {{ COMMAND }} '{{ EXPRESSION }}'\" >> %s", $tmpFile), |
|
132
|
|
|
sprintf("echo \"varnishadm -T 10.4.1.66:6082 -S /etc/varnish/secret {{ COMMAND }} '{{ EXPRESSION }}'\" >> %s", $tmpFile), |
|
133
|
|
|
], |
|
134
|
|
|
$this->router, |
|
135
|
|
|
'ban', |
|
136
|
|
|
$this->controllerResolver, |
|
137
|
|
|
$this->argumentResolver |
|
138
|
|
|
); |
|
139
|
|
|
|
|
140
|
|
|
$method = new \ReflectionMethod($cache, 'runCommand'); |
|
141
|
|
|
$method->setAccessible(true); |
|
142
|
|
|
|
|
143
|
|
|
$this->assertTrue($method->invoke($cache, 'ban', 'req.url ~ \'.*\'')); |
|
144
|
|
|
|
|
145
|
|
|
$this->assertSame(<<<'CMD' |
|
146
|
|
|
varnishadm -T 10.4.1.62:6082 -S /etc/varnish/secret ban 'req.url ~ '.*'' |
|
147
|
|
|
varnishadm -T 10.4.1.66:6082 -S /etc/varnish/secret ban 'req.url ~ '.*'' |
|
148
|
|
|
|
|
149
|
|
|
CMD |
|
150
|
|
|
, file_get_contents($tmpFile)); |
|
151
|
|
|
|
|
152
|
|
|
unlink($tmpFile); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @group legacy |
|
157
|
|
|
* @expectedDeprecation Not providing a "Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface" instance to "Sonata\CacheBundle\Adapter\VarnishCache::__construct" is deprecated since 3.x and will not be possible in 4.0 |
|
158
|
|
|
*/ |
|
159
|
|
|
public function testConstructorLegacy() |
|
160
|
|
|
{ |
|
161
|
|
|
if (!interface_exists(ArgumentResolverInterface::class)) { |
|
162
|
|
|
$this->markTestSkipped( |
|
163
|
|
|
'Running Symfony < 3.1' |
|
164
|
|
|
); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
new VarnishCache('token', [], $this->router, 'ban'); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|