1
|
|
|
<?php |
2
|
|
|
namespace Evheniy\HTML5CacheBundle\Tests\Command; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
5
|
|
|
use Symfony\Component\Console\Output\StreamOutput; |
6
|
|
|
use Evheniy\HTML5CacheBundle\Command\DumpCommand; |
7
|
|
|
use Symfony\Component\DependencyInjection\Container; |
8
|
|
|
use Symfony\Bridge\Twig\TwigEngine; |
9
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
10
|
|
|
use Symfony\Component\Finder\Finder; |
11
|
|
|
use Symfony\Component\Templating\TemplateNameParser; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class DumpCommandTest |
16
|
|
|
* |
17
|
|
|
* @package Evheniy\HTML5CacheBundle\Tests\Command |
18
|
|
|
*/ |
19
|
|
|
class DumpCommandTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var DumpCommand |
23
|
|
|
*/ |
24
|
|
|
protected $command; |
25
|
|
|
/** |
26
|
|
|
* @var \ReflectionClass |
27
|
|
|
*/ |
28
|
|
|
protected $reflectionClass; |
29
|
|
|
/** |
30
|
|
|
* @var Container |
31
|
|
|
*/ |
32
|
|
|
protected $container; |
33
|
|
|
/** |
34
|
|
|
* @var \ReflectionProperty |
35
|
|
|
*/ |
36
|
|
|
protected $html5Cache; |
37
|
|
|
/** |
38
|
|
|
* @var \ReflectionProperty |
39
|
|
|
*/ |
40
|
|
|
protected $webDirectory; |
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $webPath; |
45
|
|
|
/** |
46
|
|
|
* @var Filesystem |
47
|
|
|
*/ |
48
|
|
|
protected $filesystem; |
49
|
|
|
/** |
50
|
|
|
* @var \ReflectionProperty |
51
|
|
|
*/ |
52
|
|
|
protected $filesystemField; |
53
|
|
|
/** |
54
|
|
|
* @var \ReflectionProperty |
55
|
|
|
*/ |
56
|
|
|
protected $finder; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* |
60
|
|
|
*/ |
61
|
|
|
protected function setUp() |
62
|
|
|
{ |
63
|
|
|
$this->command = new DumpCommand(); |
64
|
|
|
$this->reflectionClass = new \ReflectionClass('\Evheniy\HTML5CacheBundle\Command\DumpCommand'); |
65
|
|
|
$this->container = new Container(); |
66
|
|
|
$this->container->set('twig', new TwigEngine(new \Twig_Environment(new \Twig_Loader_Array(array('HTML5CacheBundle::cache.html.twig' => file_get_contents(dirname(__FILE__) . '/../../Resources/views/cache.html.twig')))), new TemplateNameParser())); |
|
|
|
|
67
|
|
|
|
68
|
|
|
$this->webPath = dirname(__FILE__) . '/web'; |
69
|
|
|
$this->filesystem = new Filesystem(); |
70
|
|
|
$this->filesystem->mkdir($this->webPath); |
71
|
|
|
$this->filesystem->touch($this->webPath . '/test.png'); |
72
|
|
|
$this->filesystem->touch($this->webPath . '/test.gif'); |
73
|
|
|
|
74
|
|
|
$this->setMockFields(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* |
79
|
|
|
*/ |
80
|
|
|
protected function setMockFields() |
81
|
|
|
{ |
82
|
|
|
$this->html5Cache = $this->reflectionClass->getProperty('html5Cache'); |
83
|
|
|
$this->html5Cache->setAccessible(true); |
84
|
|
|
|
85
|
|
|
$this->webDirectory = $this->reflectionClass->getProperty('webDirectory'); |
86
|
|
|
$this->webDirectory->setAccessible(true); |
87
|
|
|
$this->webDirectory->setValue($this->command, $this->webPath); |
88
|
|
|
|
89
|
|
|
$this->finder = $this->reflectionClass->getProperty('finder'); |
90
|
|
|
$this->finder->setAccessible(true); |
91
|
|
|
|
92
|
|
|
$this->filesystemField = $this->reflectionClass->getProperty('filesystem'); |
93
|
|
|
$this->filesystemField->setAccessible(true); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* |
98
|
|
|
*/ |
99
|
|
|
protected function tearDown() |
100
|
|
|
{ |
101
|
|
|
$this->filesystem->remove($this->webPath); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* |
106
|
|
|
*/ |
107
|
|
|
public function testGetPath() |
108
|
|
|
{ |
109
|
|
|
$method = $this->reflectionClass->getMethod('getPath'); |
110
|
|
|
$method->setAccessible(true); |
111
|
|
|
$this->assertEquals($method->invoke($this->command, 'path/web/test'), 'test'); |
112
|
|
|
$this->expectException('\Evheniy\HTML5CacheBundle\Exception\PathException'); |
113
|
|
|
$method->invoke($this->command, 'path/test'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @throws \Twig_Error_Loader |
118
|
|
|
*/ |
119
|
|
|
public function testRender() |
120
|
|
|
{ |
121
|
|
|
$this->command->setContainer($this->container); |
122
|
|
|
$method = $this->reflectionClass->getMethod('render'); |
123
|
|
|
$method->setAccessible(true); |
124
|
|
|
|
125
|
|
|
$initData = array( |
126
|
|
|
'paths' => array( |
127
|
|
|
'test.png', |
128
|
|
|
'test.gif' |
129
|
|
|
), |
130
|
|
|
'cdn' => '//cdn.site.com', |
131
|
|
|
'http' => true, |
132
|
|
|
'https' => true, |
133
|
|
|
'custom_paths' => array( |
134
|
|
|
'https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js', |
135
|
|
|
) |
136
|
|
|
); |
137
|
|
|
$this->html5Cache->setValue($this->command, $initData); |
138
|
|
|
$data = $method->invoke($this->command); |
139
|
|
|
$this->assertRegExp('/CACHE MANIFEST/', $data); |
140
|
|
|
$this->assertRegExp('/CACHE:/', $data); |
141
|
|
|
$this->assertRegExp('/NETWORK:/', $data); |
142
|
|
|
$this->assertRegExp('/http:\/\/cdn.site.com\/test.png/', $data); |
143
|
|
|
$this->assertRegExp('/https:\/\/cdn.site.com\/test.png/', $data); |
144
|
|
|
$this->assertRegExp('/\/test.png/', $data); |
145
|
|
|
$this->assertRegExp('/http:\/\/cdn.site.com\/test.gif/', $data); |
146
|
|
|
$this->assertRegExp('/https:\/\/cdn.site.com\/test.gif/', $data); |
147
|
|
|
$this->assertRegExp('/\/test.gif/', $data); |
148
|
|
|
$this->assertRegExp('/https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.11.2\/jquery.min.js/', $data); |
149
|
|
|
|
150
|
|
|
$initData = array( |
151
|
|
|
'paths' => array( |
152
|
|
|
'test.png', |
153
|
|
|
'test.gif' |
154
|
|
|
), |
155
|
|
|
'cdn' => '//cdn.site.com', |
156
|
|
|
'http' => false, |
157
|
|
|
'https' => true |
158
|
|
|
); |
159
|
|
|
$this->html5Cache->setValue($this->command, $initData); |
160
|
|
|
$data = $method->invoke($this->command); |
161
|
|
|
$this->assertRegExp('/https:\/\/cdn.site.com\/test.png/', $data); |
162
|
|
|
$this->assertRegExp('/\/test.png/', $data); |
163
|
|
|
$this->assertRegExp('/https:\/\/cdn.site.com\/test.gif/', $data); |
164
|
|
|
$this->assertRegExp('/\/test.gif/', $data); |
165
|
|
|
|
166
|
|
|
$initData = array( |
167
|
|
|
'paths' => array( |
168
|
|
|
'test.png', |
169
|
|
|
'test.gif' |
170
|
|
|
), |
171
|
|
|
'cdn' => '//cdn.site.com', |
172
|
|
|
'http' => true, |
173
|
|
|
'https' => false |
174
|
|
|
); |
175
|
|
|
$this->html5Cache->setValue($this->command, $initData); |
176
|
|
|
$data = $method->invoke($this->command); |
177
|
|
|
$this->assertRegExp('/http:\/\/cdn.site.com\/test.png/', $data); |
178
|
|
|
$this->assertRegExp('/\/test.png/', $data); |
179
|
|
|
$this->assertRegExp('/http:\/\/cdn.site.com\/test.gif/', $data); |
180
|
|
|
$this->assertRegExp('/\/test.gif/', $data); |
181
|
|
|
|
182
|
|
|
$initData = array( |
183
|
|
|
'paths' => array( |
184
|
|
|
'test.png', |
185
|
|
|
'test.gif' |
186
|
|
|
) |
187
|
|
|
); |
188
|
|
|
$this->html5Cache->setValue($this->command, $initData); |
189
|
|
|
$data = $method->invoke($this->command); |
190
|
|
|
$this->assertRegExp('/\/test.png/', $data); |
191
|
|
|
$this->assertRegExp('/\/test.gif/', $data); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* |
196
|
|
|
*/ |
197
|
|
|
public function testSetHtml5Cache() |
198
|
|
|
{ |
199
|
|
|
$initData = array( |
200
|
|
|
'cdn' => '//cdn.site.com', |
201
|
|
|
'http' => true, |
202
|
|
|
'https' => true, |
203
|
|
|
'custom_paths' => array( |
204
|
|
|
'https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js', |
205
|
|
|
) |
206
|
|
|
); |
207
|
|
|
$this->container->setParameter('html5_cache', $initData); |
208
|
|
|
$this->command->setContainer($this->container); |
209
|
|
|
$method = $this->reflectionClass->getMethod('setHtml5Cache'); |
210
|
|
|
$method->setAccessible(true); |
211
|
|
|
$method->invoke($this->command, array('test.png', 'test.gif')); |
212
|
|
|
$data = $this->html5Cache->getValue($this->command); |
213
|
|
|
$initData['paths'] = array('test.png', 'test.gif'); |
214
|
|
|
$this->assertEquals($data, $initData); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* |
219
|
|
|
*/ |
220
|
|
|
public function testGetPaths() |
221
|
|
|
{ |
222
|
|
|
$this->finder->setValue($this->command, new Finder()); |
223
|
|
|
$method = $this->reflectionClass->getMethod('getPaths'); |
224
|
|
|
$method->setAccessible(true); |
225
|
|
|
$paths = $method->invoke($this->command); |
226
|
|
|
$this->assertNotEmpty($paths); |
227
|
|
|
$this->assertTrue(is_array($paths)); |
228
|
|
|
$this->assertCount(2, $paths); |
229
|
|
|
$this->assertTrue(in_array('test.png', $paths)); |
230
|
|
|
$this->assertTrue(in_array('test.gif', $paths)); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @throws \Twig_Error_Loader |
235
|
|
|
*/ |
236
|
|
|
public function testDump() |
237
|
|
|
{ |
238
|
|
|
$this->filesystemField->setValue($this->command, new Filesystem()); |
239
|
|
|
$this->command->setContainer($this->container); |
240
|
|
|
$initData = array( |
241
|
|
|
'paths' => array( |
242
|
|
|
'test.png', |
243
|
|
|
'test.gif' |
244
|
|
|
), |
245
|
|
|
'cdn' => '//cdn.site.com', |
246
|
|
|
'http' => true, |
247
|
|
|
'https' => true, |
248
|
|
|
'custom_paths' => array( |
249
|
|
|
'https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js', |
250
|
|
|
) |
251
|
|
|
); |
252
|
|
|
$this->html5Cache->setValue($this->command, $initData); |
253
|
|
|
$method = $this->reflectionClass->getMethod('dump'); |
254
|
|
|
$method->setAccessible(true); |
255
|
|
|
$method->invoke($this->command); |
256
|
|
|
$this->parseFile(); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @throws \Twig_Error_Loader |
261
|
|
|
*/ |
262
|
|
|
public function testExecute() |
263
|
|
|
{ |
264
|
|
|
$initData = array( |
265
|
|
|
'cdn' => '//cdn.site.com', |
266
|
|
|
'http' => true, |
267
|
|
|
'https' => true, |
268
|
|
|
'custom_paths' => array( |
269
|
|
|
'https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js', |
270
|
|
|
) |
271
|
|
|
); |
272
|
|
|
$this->container->setParameter('html5_cache', $initData); |
273
|
|
|
$kernel = $this->createMock(\Symfony\Component\HttpKernel\KernelInterface::class); |
274
|
|
|
$kernel->method('getRootdir')->willReturn($this->webPath); |
275
|
|
|
$this->container->set('kernel', $kernel); |
276
|
|
|
$this->command->setContainer($this->container); |
277
|
|
|
$method = $this->reflectionClass->getMethod('execute'); |
278
|
|
|
$method->setAccessible(true); |
279
|
|
|
$output = new StreamOutput(fopen('php://memory', 'w', false)); |
280
|
|
|
$method->invoke($this->command, new ArrayInput(array()), $output); |
281
|
|
|
rewind($output->getStream()); |
282
|
|
|
$this->assertRegExp('/Done/', stream_get_contents($output->getStream())); |
283
|
|
|
$this->parseFile(); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* |
288
|
|
|
*/ |
289
|
|
|
private function parseFile() |
290
|
|
|
{ |
291
|
|
|
$this->assertTrue($this->filesystem->exists(array($this->webPath . '/cache.manifest'))); |
|
|
|
|
292
|
|
|
$data = file_get_contents($this->webPath . '/cache.manifest'); |
293
|
|
|
$this->assertRegExp('/CACHE MANIFEST/', $data); |
294
|
|
|
$this->assertRegExp('/CACHE:/', $data); |
295
|
|
|
$this->assertRegExp('/NETWORK:/', $data); |
296
|
|
|
$this->assertRegExp('/http:\/\/cdn.site.com\/test.png/', $data); |
297
|
|
|
$this->assertRegExp('/https:\/\/cdn.site.com\/test.png/', $data); |
298
|
|
|
$this->assertRegExp('/\/test.png/', $data); |
299
|
|
|
$this->assertRegExp('/http:\/\/cdn.site.com\/test.gif/', $data); |
300
|
|
|
$this->assertRegExp('/https:\/\/cdn.site.com\/test.gif/', $data); |
301
|
|
|
$this->assertRegExp('/\/test.gif/', $data); |
302
|
|
|
$this->assertRegExp('/https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.11.2\/jquery.min.js/', $data); |
303
|
|
|
} |
304
|
|
|
} |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.