|
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; |
|
13
|
|
|
|
|
14
|
|
|
use phpmock\MockBuilder; |
|
15
|
|
|
use Sonata\CacheBundle\Adapter\SymfonyCache; |
|
16
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
17
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class SymfonyCacheTest. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Vincent Composieux <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class SymfonyCacheTest extends \PHPUnit_Framework_TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var SymfonyCache |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $cache; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var RouterInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $router; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var Filesystem |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $filesystem; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Sets up cache adapter. |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function setUp() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->router = $this->getMock('Symfony\Component\Routing\RouterInterface'); |
|
47
|
|
|
$this->filesystem = $this->getMock('Symfony\Component\Filesystem\Filesystem'); |
|
48
|
|
|
|
|
49
|
|
|
$this->cache = new SymfonyCache( |
|
50
|
|
|
$this->router, |
|
51
|
|
|
$this->filesystem, |
|
52
|
|
|
'/cache/dir', |
|
53
|
|
|
'token', |
|
54
|
|
|
false, |
|
55
|
|
|
array('all', 'translations'), |
|
56
|
|
|
array(), |
|
57
|
|
|
array() |
|
|
|
|
|
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Tests cache initialization. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function testInitCache() |
|
65
|
|
|
{ |
|
66
|
|
|
$this->assertTrue($this->cache->flush(array())); |
|
67
|
|
|
$this->assertTrue($this->cache->flushAll()); |
|
68
|
|
|
|
|
69
|
|
|
$this->setExpectedException('Sonata\Cache\Exception\UnsupportedException', 'Symfony cache set() method does not exists'); |
|
70
|
|
|
$this->cache->set(array('id' => 5), 'data'); |
|
71
|
|
|
|
|
72
|
|
|
$this->setExpectedException('Sonata\Cache\Exception\UnsupportedException', 'Symfony cache get() method does not exists'); |
|
73
|
|
|
$this->cache->get(array('id' => 5)); |
|
74
|
|
|
|
|
75
|
|
|
$this->setExpectedException('Sonata\Cache\Exception\UnsupportedException', 'Symfony cache has() method does not exists'); |
|
76
|
|
|
$this->cache->has(array('id' => 5)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Tests cacheAction() method. |
|
81
|
|
|
*/ |
|
82
|
|
|
public function testCacheAction() |
|
83
|
|
|
{ |
|
84
|
|
|
// Given |
|
85
|
|
|
$this->filesystem->expects($this->once())->method('exists')->will($this->returnValue(true)); |
|
|
|
|
|
|
86
|
|
|
$this->filesystem->expects($this->once())->method('remove'); |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
// When |
|
89
|
|
|
$response = $this->cache->cacheAction('token', 'translations'); |
|
90
|
|
|
|
|
91
|
|
|
// Then |
|
92
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertEquals(200, $response->getStatusCode(), 'Response should be 200'); |
|
95
|
|
|
$this->assertEquals('ok', $response->getContent(), 'Response should return "OK"'); |
|
96
|
|
|
|
|
97
|
|
|
$this->assertEquals(2, $response->headers->get('Content-Length')); |
|
98
|
|
|
$this->assertEquals('must-revalidate, no-cache, private', $response->headers->get('Cache-Control')); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Tests cacheAction() method with an invalid token. |
|
103
|
|
|
*/ |
|
104
|
|
|
public function testCacheActionWithInvalidToken() |
|
105
|
|
|
{ |
|
106
|
|
|
// Given |
|
107
|
|
|
// When - Then expect exception |
|
108
|
|
|
$this->setExpectedException('Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException'); |
|
109
|
|
|
|
|
110
|
|
|
$this->cache->cacheAction('invalid-token', 'type'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Tests cacheAction() method with an invalid cache type. |
|
115
|
|
|
*/ |
|
116
|
|
|
public function testCacheActionWithInvalidType() |
|
117
|
|
|
{ |
|
118
|
|
|
// Given |
|
119
|
|
|
// When - Then expect exception |
|
120
|
|
|
$this->setExpectedException('\RuntimeException', 'Type "invalid-type" is not defined, allowed types are: "all, translations"'); |
|
121
|
|
|
|
|
122
|
|
|
$this->cache->cacheAction('token', 'invalid-type'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Asserts the flush method throws an exception if the IP version of the server cannot be detected. |
|
127
|
|
|
*/ |
|
128
|
|
|
public function testFlushThrowsExceptionWithWrongIP() |
|
129
|
|
|
{ |
|
130
|
|
|
$cache = new SymfonyCache( |
|
131
|
|
|
$this->router, |
|
132
|
|
|
$this->filesystem, |
|
133
|
|
|
'/cache/dir', |
|
134
|
|
|
'token', |
|
135
|
|
|
false, |
|
136
|
|
|
array('all', 'translations'), |
|
137
|
|
|
array( |
|
138
|
|
|
array('ip' => 'wrong ip'), |
|
139
|
|
|
), |
|
140
|
|
|
array() |
|
|
|
|
|
|
141
|
|
|
); |
|
142
|
|
|
|
|
143
|
|
|
$this->setExpectedException('\InvalidArgumentException', '"wrong ip" is not a valid ip address'); |
|
144
|
|
|
|
|
145
|
|
|
$cache->flush(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Tests the flush method with IPv4. |
|
150
|
|
|
*/ |
|
151
|
|
|
public function testFlushWithIPv4() |
|
152
|
|
|
{ |
|
153
|
|
|
$cache = new SymfonyCache( |
|
154
|
|
|
$this->router, |
|
155
|
|
|
$this->filesystem, |
|
156
|
|
|
'/cache/dir', |
|
157
|
|
|
'token', |
|
158
|
|
|
false, |
|
159
|
|
|
array('all', 'translations'), |
|
160
|
|
|
array( |
|
161
|
|
|
array('ip' => '213.186.35.9', 'domain' => 'www.example.com', 'basic' => false, 'port' => 80), |
|
162
|
|
|
), |
|
163
|
|
|
array( |
|
|
|
|
|
|
164
|
|
|
'RCV' => array('sec' => 2, 'usec' => 0), |
|
165
|
|
|
'SND' => array('sec' => 2, 'usec' => 0), |
|
166
|
|
|
) |
|
167
|
|
|
); |
|
168
|
|
|
|
|
169
|
|
|
// NEXT_MAJOR: while dropping old versions of php, remove this and simplify the closure below |
|
170
|
|
|
$that = $this; |
|
171
|
|
|
|
|
172
|
|
|
$mocks = array(); |
|
173
|
|
|
|
|
174
|
|
|
$builder = new MockBuilder(); |
|
175
|
|
|
$mock = $builder->setNamespace('Sonata\CacheBundle\Adapter') |
|
176
|
|
|
->setName('socket_create') |
|
177
|
|
|
->setFunction(function () use ($that) { |
|
178
|
|
|
$that->assertSame(array(AF_INET, SOCK_STREAM, SOL_TCP), func_get_args()); |
|
179
|
|
|
}) |
|
180
|
|
|
->build(); |
|
181
|
|
|
$mock->enable(); |
|
182
|
|
|
|
|
183
|
|
|
$mocks[] = $mock; |
|
184
|
|
|
|
|
185
|
|
|
foreach (array('socket_set_option', 'socket_connect', 'socket_write', 'socket_read') as $function) { |
|
186
|
|
|
$builder = new MockBuilder(); |
|
187
|
|
|
$mock = $builder->setNamespace('Sonata\CacheBundle\Adapter') |
|
188
|
|
|
->setName($function) |
|
189
|
|
|
->setFunction(function () { |
|
190
|
|
|
}) |
|
191
|
|
|
->build(); |
|
192
|
|
|
$mock->enable(); |
|
193
|
|
|
|
|
194
|
|
|
$mocks[] = $mock; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$cache->flush(); |
|
198
|
|
|
|
|
199
|
|
|
foreach ($mocks as $mock) { |
|
200
|
|
|
$mock->disable(); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Tests the flush method with IPv6. |
|
206
|
|
|
*/ |
|
207
|
|
|
public function testFlushWithIPv6() |
|
208
|
|
|
{ |
|
209
|
|
|
$cache = new SymfonyCache( |
|
210
|
|
|
$this->router, |
|
211
|
|
|
$this->filesystem, |
|
212
|
|
|
'/cache/dir', |
|
213
|
|
|
'token', |
|
214
|
|
|
false, |
|
215
|
|
|
array('all', 'translations'), |
|
216
|
|
|
array( |
|
217
|
|
|
array('ip' => '2001:41d0:1:209:FF:FF:FF:FF', 'domain' => 'www.example.com', 'basic' => false, 'port' => 80), |
|
218
|
|
|
), |
|
219
|
|
|
array( |
|
|
|
|
|
|
220
|
|
|
'RCV' => array('sec' => 2, 'usec' => 0), |
|
221
|
|
|
'SND' => array('sec' => 2, 'usec' => 0), |
|
222
|
|
|
) |
|
223
|
|
|
); |
|
224
|
|
|
|
|
225
|
|
|
// NEXT_MAJOR: while dropping old versions of php, remove this and simplify the closure below |
|
226
|
|
|
$that = $this; |
|
227
|
|
|
|
|
228
|
|
|
$mocks = array(); |
|
229
|
|
|
|
|
230
|
|
|
$builder = new MockBuilder(); |
|
231
|
|
|
$mock = $builder->setNamespace('Sonata\CacheBundle\Adapter') |
|
232
|
|
|
->setName('socket_create') |
|
233
|
|
|
->setFunction(function () use ($that) { |
|
234
|
|
|
$that->assertSame(array(AF_INET6, SOCK_STREAM, SOL_TCP), func_get_args()); |
|
235
|
|
|
}) |
|
236
|
|
|
->build(); |
|
237
|
|
|
$mock->enable(); |
|
238
|
|
|
|
|
239
|
|
|
$mocks[] = $mock; |
|
240
|
|
|
|
|
241
|
|
|
foreach (array('socket_set_option', 'socket_connect', 'socket_write', 'socket_read') as $function) { |
|
242
|
|
|
$builder = new MockBuilder(); |
|
243
|
|
|
$mock = $builder->setNamespace('Sonata\CacheBundle\Adapter') |
|
244
|
|
|
->setName($function) |
|
245
|
|
|
->setFunction(function () { |
|
246
|
|
|
}) |
|
247
|
|
|
->build(); |
|
248
|
|
|
$mock->enable(); |
|
249
|
|
|
|
|
250
|
|
|
$mocks[] = $mock; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
$cache->flush(); |
|
254
|
|
|
|
|
255
|
|
|
foreach ($mocks as $mock) { |
|
256
|
|
|
$mock->disable(); |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.