1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\CacheBundle\Tests\Adapter\Cache; |
15
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Sonata\Cache\CacheElement; |
18
|
|
|
use Sonata\CacheBundle\Adapter\VarnishCache; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
21
|
|
|
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface; |
22
|
|
|
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface; |
23
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
24
|
|
|
use Symfony\Component\Routing\RouterInterface; |
25
|
|
|
|
26
|
|
|
class VarnishCacheTest extends TestCase |
27
|
|
|
{ |
28
|
|
|
private $router; |
29
|
|
|
private $controllerResolver; |
30
|
|
|
private $argumentResolver; |
31
|
|
|
private $cache; |
32
|
|
|
|
33
|
|
|
protected function setUp(): void |
34
|
|
|
{ |
35
|
|
|
$this->router = $this->createMock(RouterInterface::class); |
36
|
|
|
$this->controllerResolver = $this->createMock(ControllerResolverInterface::class); |
37
|
|
|
$this->argumentResolver = $this->createMock(ArgumentResolverInterface::class); |
38
|
|
|
|
39
|
|
|
$this->cache = new VarnishCache( |
40
|
|
|
'token', |
41
|
|
|
[], |
42
|
|
|
$this->router, |
43
|
|
|
'ban', |
44
|
|
|
$this->controllerResolver, |
45
|
|
|
$this->argumentResolver |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testInitCache(): void |
50
|
|
|
{ |
51
|
|
|
$this->router->expects($this->any()) |
52
|
|
|
->method('generate') |
53
|
|
|
->willReturn( |
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->assertSame( |
75
|
|
|
'<esi:include src="https://sonata-project.org/cache/esi/TOKEN?controller=asdsad"/>', |
76
|
|
|
$cacheElement->getData()->getContent() |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testActionInvalidToken(): void |
81
|
|
|
{ |
82
|
|
|
$this->expectException(AccessDeniedHttpException::class); |
83
|
|
|
|
84
|
|
|
$this->router->expects($this->any()) |
85
|
|
|
->method('generate') |
86
|
|
|
->willReturn( |
87
|
|
|
'http://sonata-project.orf/cache/esi/TOKEN?controller=asdsad' |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$request = Request::create('cache/esi/TOKEN?controller=asdsad', 'get', [ |
91
|
|
|
'token' => 'wrong', |
92
|
|
|
]); |
93
|
|
|
|
94
|
|
|
$this->cache->cacheAction($request); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @doesNotPerformAssertions |
99
|
|
|
*/ |
100
|
|
|
public function testValidToken(): void |
101
|
|
|
{ |
102
|
|
|
$this->controllerResolver->expects($this->any()) |
103
|
|
|
->method('getController') |
104
|
|
|
->willReturn(static function () { |
105
|
|
|
return new Response(); |
106
|
|
|
}); |
107
|
|
|
|
108
|
|
|
$this->argumentResolver->expects($this->any()) |
109
|
|
|
->method('getArguments') |
110
|
|
|
->willReturn([]); |
111
|
|
|
|
112
|
|
|
$request = Request::create('cache/esi/TOKEN', 'get', [ |
113
|
|
|
'token' => '44befdbd93f304ea693023aa6587729bed76a206ecdacfd9bbd9b43fcf2e1664', |
114
|
|
|
'parameters' => [ |
115
|
|
|
'controller' => 'asfsat', |
116
|
|
|
'parameters' => [], |
117
|
|
|
], |
118
|
|
|
]); |
119
|
|
|
|
120
|
|
|
$this->cache->cacheAction($request); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testRunCommand(): void |
124
|
|
|
{ |
125
|
|
|
$tmpFile = tempnam(sys_get_temp_dir(), 'sonata-cache'); |
126
|
|
|
|
127
|
|
|
$cache = new VarnishCache( |
128
|
|
|
'token', |
129
|
|
|
[ |
130
|
|
|
sprintf("echo \"varnishadm -T 10.4.1.62:6082 -S /etc/varnish/secret {{ COMMAND }} '{{ EXPRESSION }}'\" >> %s", $tmpFile), |
131
|
|
|
sprintf("echo \"varnishadm -T 10.4.1.66:6082 -S /etc/varnish/secret {{ COMMAND }} '{{ EXPRESSION }}'\" >> %s", $tmpFile), |
132
|
|
|
], |
133
|
|
|
$this->router, |
134
|
|
|
'ban', |
135
|
|
|
$this->controllerResolver, |
136
|
|
|
$this->argumentResolver |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
$method = new \ReflectionMethod($cache, 'runCommand'); |
140
|
|
|
$method->setAccessible(true); |
141
|
|
|
|
142
|
|
|
$this->assertTrue($method->invoke($cache, 'ban', 'req.url ~ \'.*\'')); |
143
|
|
|
|
144
|
|
|
$this->assertSame(<<<'CMD' |
145
|
|
|
varnishadm -T 10.4.1.62:6082 -S /etc/varnish/secret ban 'req.url ~ '.*'' |
146
|
|
|
varnishadm -T 10.4.1.66:6082 -S /etc/varnish/secret ban 'req.url ~ '.*'' |
147
|
|
|
|
148
|
|
|
CMD |
149
|
|
|
, file_get_contents($tmpFile)); |
150
|
|
|
|
151
|
|
|
unlink($tmpFile); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|