Completed
Pull Request — master (#284)
by
unknown
07:57
created

ConfigurationTest::testCacheManagerNoClient()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 13

Duplication

Lines 19
Ratio 100 %
Metric Value
dl 19
loc 19
rs 9.4285
cc 3
eloc 13
nc 4
nop 0
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCacheBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\HttpCacheBundle\Tests\Unit\DependencyInjection;
13
14
use FOS\HttpCacheBundle\DependencyInjection\Configuration;
15
use FOS\HttpCacheBundle\DependencyInjection\FOSHttpCacheExtension;
16
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionConfigurationTestCase;
17
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
18
19
class ConfigurationTest extends AbstractExtensionConfigurationTestCase
20
{
21
    protected function getContainerExtension()
22
    {
23
        return new FOSHttpCacheExtension();
24
    }
25
26
    protected function getConfiguration()
27
    {
28
        return new Configuration(false);
29
    }
30
31
    public function testEmptyConfiguration()
32
    {
33
        $expectedConfiguration = $this->getEmptyConfig();
34
35
        $formats = array_map(function ($path) {
36
            return __DIR__.'/../../Resources/Fixtures/'.$path;
37
        }, array(
38
            'config/empty.yml',
39
            'config/empty.xml',
40
            'config/empty.php',
41
        ));
42
43
        foreach ($formats as $format) {
44
            $this->assertProcessedConfigurationEquals($expectedConfiguration, array($format));
45
        }
46
    }
47
48
    public function testSupportsAllConfigFormats()
49
    {
50
        $expectedConfiguration = array(
51
            'cache_control' => array(
52
                'defaults' => array(
53
                    'overwrite' => true,
54
                ),
55
                'rules' => array(
56
                    array(
57
                        'match' => array(
58
                            'path' => '/abc',
59
                            'host' => 'fos',
60
                            'methods' => array('GET', 'POST'),
61
                            'ips' => array('1.2.3.4', '1.1.1.1'),
62
                            'attributes' => array('_controller' => 'fos.user_bundle.*'),
63
                            'additional_cacheable_status' => array(100, 500),
64
                            'match_response' => '',
65
                            // TODO 'match_response' => '',
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
66
                        ),
67
                        'headers' => array(
68
                            'overwrite' => false,
69
                            'cache_control' => array(
70
                                'max_age' => 1,
71
                                's_maxage' => 2,
72
                                'public' => true,
73
                                'must_revalidate' => true,
74
                                'proxy_revalidate' => false,
75
                                'no_transform' => true,
76
                                'no_cache' => false,
77
                                'stale_if_error' => 3,
78
                                'stale_while_revalidate' => 4,
79
                            ),
80
                            'etag' => true,
81
                            'last_modified' => '-1 hour',
82
                            'reverse_proxy_ttl' => 42,
83
                            'vary' => array('Cookie', 'Authorization'),
84
                        ),
85
                    ),
86
                ),
87
            ),
88
            'proxy_client' => array(
89
                'varnish' => array(
90
                    'servers' => array('22.22.22.22'),
91
                    'base_url' => '/test',
92
                    'guzzle_client' => 'acme.guzzle.varnish',
93
                ),
94
            ),
95
            'cache_manager' => array(
96
                'enabled' => true,
97
                'custom_proxy_client' => 'acme.proxy_client',
98
                'generate_url_type' => 'auto',
99
            ),
100
            'tags' => array(
101
                'enabled' => 'auto',
102
                'header' => 'FOS-Tags',
103
                'expression_language' => 'acme.expression_language',
104
                'rules' => array(
105
                    array(
106
                        'match' => array(
107
                            'path' => '/def',
108
                            'host' => 'friends',
109
                            'methods' => array('PUT', 'DELETE'),
110
                            'ips' => array('99.99.99.99'),
111
                            'attributes' => array(
112
                                '_foo' => 'bar',
113
                            ),
114
                            'additional_cacheable_status' => array(501, 502),
115
                            'match_response' => '',
116
                            // TODO match_response
117
                        ),
118
                        'tags' => array('a', 'b'),
119
                        'tag_expressions' => array('"a"', '"b"'),
120
                    ),
121
                ),
122
            ),
123
            'invalidation' => array(
124
                'enabled' => 'auto',
125
                'expression_language' => 'acme.expression_language',
126
                'rules' => array(
127
                    array(
128
                        'match' => array(
129
                            'path' => '/hij',
130
                            'host' => 'symfony',
131
                            'methods' => array('PATCH'),
132
                            'ips' => array('42.42.42.42'),
133
                            'attributes' => array(
134
                                '_format' => 'json',
135
                            ),
136
                            'additional_cacheable_status' => array(404, 403),
137
                            'match_response' => '',
138
                            // TODO match_response
139
                        ),
140
                        'routes' => array(
141
                            'invalidate_route1' => array(
142
                                'ignore_extra_params' => false,
143
                            ),
144
                        ),
145
                    ),
146
                ),
147
            ),
148
            'user_context' => array(
149
                'enabled' => true,
150
                'match' => array(
151
                    'matcher_service' => 'fos_http_cache.user_context.request_matcher',
152
                    'accept' => 'application/vnd.fos.user-context-hash',
153
                    'method' => 'GET',
154
                ),
155
                'hash_cache_ttl' => 300,
156
                'always_vary_on_context_hash' => true,
157
                'user_identifier_headers' => array('Cookie', 'Authorization'),
158
                'user_hash_header' => 'FOS-User-Context-Hash',
159
                'role_provider' => true,
160
                'logout_handler' => array(
161
                    'enabled' => 'auto',
162
                ),
163
            ),
164
            'flash_message' => array(
165
                'enabled' => true,
166
                'name' => 'flashtest',
167
                'path' => '/x',
168
                'host' => 'y',
169
                'secure' => true,
170
            ),
171
            'debug' => array(
172
                'enabled' => true,
173
                'header' => 'FOS-Cache-Debug',
174
            ),
175
        );
176
177
        $formats = array_map(function ($path) {
178
            return __DIR__.'/../../Resources/Fixtures/'.$path;
179
        }, array(
180
            'config/full.yml',
181
            'config/full.xml',
182
            'config/full.php',
183
        ));
184
185
        foreach ($formats as $format) {
186
            $this->assertProcessedConfigurationEquals($expectedConfiguration, array($format));
187
        }
188
    }
189
190
    public function testCustomProxyClient()
191
    {
192
        $expectedConfiguration = $this->getEmptyConfig();
193
        $expectedConfiguration['cache_manager'] = array(
194
            'enabled' => true,
195
            'custom_proxy_client' => 'acme.proxy_client',
196
            'generate_url_type' => 'auto',
197
        );
198
        $expectedConfiguration['tags']['enabled'] = 'auto';
199
        $expectedConfiguration['invalidation']['enabled'] = 'auto';
200
201
        $formats = array_map(function ($path) {
202
            return __DIR__.'/../../Resources/Fixtures/'.$path;
203
        }, array(
204
            'config/custom-client.yml',
205
            'config/custom-client.xml',
206
            'config/custom-client.php',
207
        ));
208
209
        foreach ($formats as $format) {
210
            $this->assertProcessedConfigurationEquals($expectedConfiguration, array($format));
211
        }
212
    }
213
214 View Code Duplication
    public function testSupportsNginx()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
215
    {
216
        $expectedConfiguration = $this->getEmptyConfig();
217
        $expectedConfiguration['proxy_client'] = array(
218
            'nginx' => array(
219
                'servers' => array('22.22.22.22'),
220
                'base_url' => '/test',
221
                'guzzle_client' => 'acme.guzzle.nginx',
222
                'purge_location' => '/purge',
223
            ),
224
        );
225
        $expectedConfiguration['cache_manager']['enabled'] = 'auto';
226
        $expectedConfiguration['cache_manager']['generate_url_type'] = 'auto';
227
        $expectedConfiguration['tags']['enabled'] = 'auto';
228
        $expectedConfiguration['invalidation']['enabled'] = 'auto';
229
        $expectedConfiguration['user_context']['logout_handler']['enabled'] = 'auto';
230
231
        $formats = array_map(function ($path) {
232
            return __DIR__.'/../../Resources/Fixtures/'.$path;
233
        }, array(
234
            'config/nginx.yml',
235
            'config/nginx.xml',
236
            'config/nginx.php',
237
        ));
238
239
        foreach ($formats as $format) {
240
            $this->assertProcessedConfigurationEquals($expectedConfiguration, array($format));
241
        }
242
    }
243
244 View Code Duplication
    public function testSupportsSymfony()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
245
    {
246
        $expectedConfiguration = $this->getEmptyConfig();
247
        $expectedConfiguration['proxy_client'] = array(
248
            'symfony' => array(
249
                'servers' => array('22.22.22.22'),
250
                'base_url' => '/test',
251
                'guzzle_client' => 'acme.guzzle.symfony',
252
            ),
253
        );
254
        $expectedConfiguration['cache_manager']['enabled'] = 'auto';
255
        $expectedConfiguration['cache_manager']['generate_url_type'] = 'auto';
256
        $expectedConfiguration['tags']['enabled'] = 'auto';
257
        $expectedConfiguration['invalidation']['enabled'] = 'auto';
258
        $expectedConfiguration['user_context']['logout_handler']['enabled'] = 'auto';
259
260
        $formats = array_map(function ($path) {
261
            return __DIR__.'/../../Resources/Fixtures/'.$path;
262
        }, array(
263
            'config/symfony.yml',
264
            'config/symfony.xml',
265
            'config/symfony.php',
266
        ));
267
268
        foreach ($formats as $format) {
269
            $this->assertProcessedConfigurationEquals($expectedConfiguration, array($format));
270
        }
271
    }
272
273
    public function testSplitOptions()
274
    {
275
        $expectedConfiguration = $this->getEmptyConfig();
276
        $expectedConfiguration['cache_control'] = array(
277
            'rules' => array(
278
                array(
279
                    'match' => array(
280
                        'path' => null,
281
                        'host' => null,
282
                        'attributes' => array(),
283
                        'additional_cacheable_status' => array(),
284
                        'match_response' => null,
285
                        'methods' => array('GET', 'POST'),
286
                        'ips' => array('1.2.3.4', '1.1.1.1'),
287
                    ),
288
                    'headers' => array(
289
                        'reverse_proxy_ttl' => null,
290
                        'vary' => array('Cookie', 'Authorization'),
291
                        'overwrite' => 'default',
292
                        'etag' => false,
293
                    ),
294
                ),
295
            ),
296
            'defaults' => array(
297
                'overwrite' => false,
298
            ),
299
        );
300
        $expectedConfiguration['proxy_client'] = array(
301
            'varnish' => array(
302
                'base_url' => null,
303
                'guzzle_client' => null,
304
                'servers' => array('1.1.1.1:80', '2.2.2.2:80'),
305
            ),
306
            'nginx' => array(
307
                'base_url' => null,
308
                'guzzle_client' => null,
309
                'purge_location' => '',
310
                'servers' => array('1.1.1.1:81', '2.2.2.2:81'),
311
            ),
312
        );
313
        $expectedConfiguration['cache_manager']['enabled'] = 'auto';
314
        $expectedConfiguration['cache_manager']['generate_url_type'] = 'auto';
315
        $expectedConfiguration['tags']['enabled'] = 'auto';
316
        $expectedConfiguration['invalidation']['enabled'] = 'auto';
317
        $expectedConfiguration['user_context']['logout_handler']['enabled'] = 'auto';
318
319
        $formats = array_map(function ($path) {
320
            return __DIR__.'/../../Resources/Fixtures/'.$path;
321
        }, array(
322
            'config/split.yml',
323
            'config/split.xml',
324
            'config/split.php',
325
        ));
326
327
        foreach ($formats as $format) {
328
            $this->assertProcessedConfigurationEquals($expectedConfiguration, array($format));
329
        }
330
    }
331
332 View Code Duplication
    public function testCacheManagerNoClient()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
333
    {
334
        $formats = array_map(function ($path) {
335
            return __DIR__.'/../../Resources/Fixtures/'.$path;
336
        }, array(
337
            'config/cachemanager_noclient.yml',
338
            'config/cachemanager_noclient.xml',
339
            'config/cachemanager_noclient.php',
340
        ));
341
342
        foreach ($formats as $format) {
343
            try {
344
                $this->assertProcessedConfigurationEquals(array(), array($format));
345
                $this->fail('No exception thrown on invalid configuration');
346
            } catch (InvalidConfigurationException $e) {
347
                $this->assertContains('need to configure a proxy_client', $e->getMessage());
348
            }
349
        }
350
    }
351
352 View Code Duplication
    public function testTagsNoCacheManager()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
353
    {
354
        $formats = array_map(function ($path) {
355
            return __DIR__.'/../../Resources/Fixtures/'.$path;
356
        }, array(
357
            'config/tags_nocachemanager.yml',
358
            'config/tags_nocachemanager.xml',
359
            'config/tags_nocachemanager.php',
360
        ));
361
362
        foreach ($formats as $format) {
363
            try {
364
                $this->assertProcessedConfigurationEquals(array(), array($format));
365
                $this->fail('No exception thrown on invalid configuration');
366
            } catch (InvalidConfigurationException $e) {
367
                $this->assertContains('cache_manager needed for tag handling', $e->getMessage());
368
            }
369
        }
370
    }
371
372 View Code Duplication
    public function testInvalidationNoCacheManager()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
373
    {
374
        $formats = array_map(function ($path) {
375
            return __DIR__.'/../../Resources/Fixtures/'.$path;
376
        }, array(
377
            'config/invalidation_nocachemanager.yml',
378
            'config/invalidation_nocachemanager.xml',
379
            'config/invalidation_nocachemanager.php',
380
        ));
381
382
        foreach ($formats as $format) {
383
            try {
384
                $this->assertProcessedConfigurationEquals(array(), array($format));
385
                $this->fail('No exception thrown on invalid configuration');
386
            } catch (InvalidConfigurationException $e) {
387
                $this->assertContains('cache_manager needed for invalidation handling', $e->getMessage());
388
            }
389
        }
390
    }
391
392 View Code Duplication
    public function testInvalidDate()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
393
    {
394
        $formats = array_map(function ($path) {
395
            return __DIR__.'/../../Resources/Fixtures/'.$path;
396
        }, array(
397
            'config/invalid_date.yml',
398
            'config/invalid_date.xml',
399
            'config/invalid_date.php',
400
        ));
401
402
        foreach ($formats as $format) {
403
            try {
404
                $this->assertProcessedConfigurationEquals(array(), array($format));
405
                $this->fail('No exception thrown on invalid configuration');
406
            } catch (InvalidConfigurationException $e) {
407
                $this->assertContains('Failed to parse time string', $e->getMessage());
408
            }
409
        }
410
    }
411
412
    /**
413
     * The configuration is reused, we only need to test this once.
414
     */
415 View Code Duplication
    public function testRulesBothStatusAndExpression()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
416
    {
417
        $formats = array_map(function ($path) {
418
            return __DIR__.'/../../Resources/Fixtures/'.$path;
419
        }, array(
420
            'config/rules_matchstatusandexpression.yml',
421
            'config/rules_matchstatusandexpression.xml',
422
            'config/rules_matchstatusandexpression.php',
423
        ));
424
425
        foreach ($formats as $format) {
426
            try {
427
                $this->assertProcessedConfigurationEquals(array(), array($format));
428
                $this->fail('No exception thrown on invalid configuration');
429
            } catch (InvalidConfigurationException $e) {
430
                $this->assertContains('may not set both additional_cacheable_status and match_response', $e->getMessage());
431
            }
432
        }
433
    }
434
435
    /**
436
     * @return array The configuration when nothing is specified.
437
     */
438
    private function getEmptyConfig()
439
    {
440
        return array(
441
            'cache_manager' => array(
442
                'enabled' => false,
443
                'generate_url_type' => 'auto',
444
            ),
445
            'tags' => array(
446
                'enabled' => false,
447
                'header' => 'X-Cache-Tags',
448
                'expression_language' => null,
449
                'rules' => array(),
450
            ),
451
            'invalidation' => array(
452
                'enabled' => false,
453
                'expression_language' => null,
454
                'rules' => array(),
455
            ),
456
            'user_context' => array(
457
                'enabled' => false,
458
                'match' => array(
459
                    'matcher_service' => 'fos_http_cache.user_context.request_matcher',
460
                    'accept' => 'application/vnd.fos.user-context-hash',
461
                    'method' => null,
462
                ),
463
                'hash_cache_ttl' => 0,
464
                'always_vary_on_context_hash' => true,
465
                'user_identifier_headers' => array('Cookie', 'Authorization'),
466
                'user_hash_header' => 'X-User-Context-Hash',
467
                'role_provider' => false,
468
                'logout_handler' => array(
469
                    'enabled' => false,
470
                ),
471
            ),
472
            'flash_message' => array(
473
                'enabled' => false,
474
                'name' => 'flashes',
475
                'path' => '/',
476
                'host' => null,
477
                'secure' => false,
478
            ),
479
            'debug' => array(
480
                'enabled' => false,
481
                'header' => 'X-Cache-Debug',
482
            ),
483
        );
484
    }
485
}
486