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