Completed
Push — master ( ca6270...ba5ea5 )
by David
42:39 queued 14:41
created

ConfigurationTest::getConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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
                'user_identifier_headers' => array('Cookie', 'Authorization'),
157
                'user_hash_header' => 'FOS-User-Context-Hash',
158
                'role_provider' => true,
159
                'logout_handler' => array(
160
                    'enabled' => 'auto',
161
                ),
162
            ),
163
            'flash_message' => array(
164
                'enabled' => true,
165
                'name' => 'flashtest',
166
                'path' => '/x',
167
                'host' => 'y',
168
                'secure' => true,
169
            ),
170
            'debug' => array(
171
                'enabled' => true,
172
                'header' => 'FOS-Cache-Debug',
173
            ),
174
        );
175
176
        $formats = array_map(function ($path) {
177
            return __DIR__.'/../../Resources/Fixtures/'.$path;
178
        }, array(
179
            'config/full.yml',
180
            'config/full.xml',
181
            'config/full.php',
182
        ));
183
184
        foreach ($formats as $format) {
185
            $this->assertProcessedConfigurationEquals($expectedConfiguration, array($format));
186
        }
187
    }
188
189
    public function testCustomProxyClient()
190
    {
191
        $expectedConfiguration = $this->getEmptyConfig();
192
        $expectedConfiguration['cache_manager'] = array(
193
            'enabled' => true,
194
            'custom_proxy_client' => 'acme.proxy_client',
195
            'generate_url_type' => 'auto',
196
        );
197
        $expectedConfiguration['tags']['enabled'] = 'auto';
198
        $expectedConfiguration['invalidation']['enabled'] = 'auto';
199
200
        $formats = array_map(function ($path) {
201
            return __DIR__.'/../../Resources/Fixtures/'.$path;
202
        }, array(
203
            'config/custom-client.yml',
204
            'config/custom-client.xml',
205
            'config/custom-client.php',
206
        ));
207
208
        foreach ($formats as $format) {
209
            $this->assertProcessedConfigurationEquals($expectedConfiguration, array($format));
210
        }
211
    }
212
213 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...
214
    {
215
        $expectedConfiguration = $this->getEmptyConfig();
216
        $expectedConfiguration['proxy_client'] = array(
217
            'nginx' => array(
218
                'servers' => array('22.22.22.22'),
219
                'base_url' => '/test',
220
                'guzzle_client' => 'acme.guzzle.nginx',
221
                'purge_location' => '/purge',
222
            ),
223
        );
224
        $expectedConfiguration['cache_manager']['enabled'] = 'auto';
225
        $expectedConfiguration['cache_manager']['generate_url_type'] = 'auto';
226
        $expectedConfiguration['tags']['enabled'] = 'auto';
227
        $expectedConfiguration['invalidation']['enabled'] = 'auto';
228
        $expectedConfiguration['user_context']['logout_handler']['enabled'] = 'auto';
229
230
        $formats = array_map(function ($path) {
231
            return __DIR__.'/../../Resources/Fixtures/'.$path;
232
        }, array(
233
            'config/nginx.yml',
234
            'config/nginx.xml',
235
            'config/nginx.php',
236
        ));
237
238
        foreach ($formats as $format) {
239
            $this->assertProcessedConfigurationEquals($expectedConfiguration, array($format));
240
        }
241
    }
242
243 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...
244
    {
245
        $expectedConfiguration = $this->getEmptyConfig();
246
        $expectedConfiguration['proxy_client'] = array(
247
            'symfony' => array(
248
                'servers' => array('22.22.22.22'),
249
                'base_url' => '/test',
250
                'guzzle_client' => 'acme.guzzle.symfony',
251
            ),
252
        );
253
        $expectedConfiguration['cache_manager']['enabled'] = 'auto';
254
        $expectedConfiguration['cache_manager']['generate_url_type'] = 'auto';
255
        $expectedConfiguration['tags']['enabled'] = 'auto';
256
        $expectedConfiguration['invalidation']['enabled'] = 'auto';
257
        $expectedConfiguration['user_context']['logout_handler']['enabled'] = 'auto';
258
259
        $formats = array_map(function ($path) {
260
            return __DIR__.'/../../Resources/Fixtures/'.$path;
261
        }, array(
262
            'config/symfony.yml',
263
            'config/symfony.xml',
264
            'config/symfony.php',
265
        ));
266
267
        foreach ($formats as $format) {
268
            $this->assertProcessedConfigurationEquals($expectedConfiguration, array($format));
269
        }
270
    }
271
272
    public function testSplitOptions()
273
    {
274
        $expectedConfiguration = $this->getEmptyConfig();
275
        $expectedConfiguration['cache_control'] = array(
276
            'rules' => array(
277
                array(
278
                    'match' => array(
279
                        'path' => null,
280
                        'host' => null,
281
                        'attributes' => array(),
282
                        'additional_cacheable_status' => array(),
283
                        'match_response' => null,
284
                        'methods' => array('GET', 'POST'),
285
                        'ips' => array('1.2.3.4', '1.1.1.1'),
286
                    ),
287
                    'headers' => array(
288
                        'reverse_proxy_ttl' => null,
289
                        'vary' => array('Cookie', 'Authorization'),
290
                        'overwrite' => 'default',
291
                        'etag' => false,
292
                    ),
293
                ),
294
            ),
295
            'defaults' => array(
296
                'overwrite' => false,
297
            ),
298
        );
299
        $expectedConfiguration['proxy_client'] = array(
300
            'varnish' => array(
301
                'base_url' => null,
302
                'guzzle_client' => null,
303
                'servers' => array('1.1.1.1:80', '2.2.2.2:80'),
304
            ),
305
            'nginx' => array(
306
                'base_url' => null,
307
                'guzzle_client' => null,
308
                'purge_location' => '',
309
                'servers' => array('1.1.1.1:81', '2.2.2.2:81'),
310
            ),
311
        );
312
        $expectedConfiguration['cache_manager']['enabled'] = 'auto';
313
        $expectedConfiguration['cache_manager']['generate_url_type'] = 'auto';
314
        $expectedConfiguration['tags']['enabled'] = 'auto';
315
        $expectedConfiguration['invalidation']['enabled'] = 'auto';
316
        $expectedConfiguration['user_context']['logout_handler']['enabled'] = 'auto';
317
318
        $formats = array_map(function ($path) {
319
            return __DIR__.'/../../Resources/Fixtures/'.$path;
320
        }, array(
321
            'config/split.yml',
322
            'config/split.xml',
323
            'config/split.php',
324
        ));
325
326
        foreach ($formats as $format) {
327
            $this->assertProcessedConfigurationEquals($expectedConfiguration, array($format));
328
        }
329
    }
330
331 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...
332
    {
333
        $formats = array_map(function ($path) {
334
            return __DIR__.'/../../Resources/Fixtures/'.$path;
335
        }, array(
336
            'config/cachemanager_noclient.yml',
337
            'config/cachemanager_noclient.xml',
338
            'config/cachemanager_noclient.php',
339
        ));
340
341
        foreach ($formats as $format) {
342
            try {
343
                $this->assertProcessedConfigurationEquals(array(), array($format));
344
                $this->fail('No exception thrown on invalid configuration');
345
            } catch (InvalidConfigurationException $e) {
346
                $this->assertContains('need to configure a proxy_client', $e->getMessage());
347
            }
348
        }
349
    }
350
351 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...
352
    {
353
        $formats = array_map(function ($path) {
354
            return __DIR__.'/../../Resources/Fixtures/'.$path;
355
        }, array(
356
            'config/tags_nocachemanager.yml',
357
            'config/tags_nocachemanager.xml',
358
            'config/tags_nocachemanager.php',
359
        ));
360
361
        foreach ($formats as $format) {
362
            try {
363
                $this->assertProcessedConfigurationEquals(array(), array($format));
364
                $this->fail('No exception thrown on invalid configuration');
365
            } catch (InvalidConfigurationException $e) {
366
                $this->assertContains('cache_manager needed for tag handling', $e->getMessage());
367
            }
368
        }
369
    }
370
371 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...
372
    {
373
        $formats = array_map(function ($path) {
374
            return __DIR__.'/../../Resources/Fixtures/'.$path;
375
        }, array(
376
            'config/invalidation_nocachemanager.yml',
377
            'config/invalidation_nocachemanager.xml',
378
            'config/invalidation_nocachemanager.php',
379
        ));
380
381
        foreach ($formats as $format) {
382
            try {
383
                $this->assertProcessedConfigurationEquals(array(), array($format));
384
                $this->fail('No exception thrown on invalid configuration');
385
            } catch (InvalidConfigurationException $e) {
386
                $this->assertContains('cache_manager needed for invalidation handling', $e->getMessage());
387
            }
388
        }
389
    }
390
391 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...
392
    {
393
        $formats = array_map(function ($path) {
394
            return __DIR__.'/../../Resources/Fixtures/'.$path;
395
        }, array(
396
            'config/invalid_date.yml',
397
            'config/invalid_date.xml',
398
            'config/invalid_date.php',
399
        ));
400
401
        foreach ($formats as $format) {
402
            try {
403
                $this->assertProcessedConfigurationEquals(array(), array($format));
404
                $this->fail('No exception thrown on invalid configuration');
405
            } catch (InvalidConfigurationException $e) {
406
                $this->assertContains('Failed to parse time string', $e->getMessage());
407
            }
408
        }
409
    }
410
411
    /**
412
     * The configuration is reused, we only need to test this once.
413
     */
414 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...
415
    {
416
        $formats = array_map(function ($path) {
417
            return __DIR__.'/../../Resources/Fixtures/'.$path;
418
        }, array(
419
            'config/rules_matchstatusandexpression.yml',
420
            'config/rules_matchstatusandexpression.xml',
421
            'config/rules_matchstatusandexpression.php',
422
        ));
423
424
        foreach ($formats as $format) {
425
            try {
426
                $this->assertProcessedConfigurationEquals(array(), array($format));
427
                $this->fail('No exception thrown on invalid configuration');
428
            } catch (InvalidConfigurationException $e) {
429
                $this->assertContains('may not set both additional_cacheable_status and match_response', $e->getMessage());
430
            }
431
        }
432
    }
433
434
    /**
435
     * @return array The configuration when nothing is specified.
436
     */
437
    private function getEmptyConfig()
438
    {
439
        return array(
440
            'cache_manager' => array(
441
                'enabled' => false,
442
                'generate_url_type' => 'auto',
443
            ),
444
            'tags' => array(
445
                'enabled' => false,
446
                'header' => 'X-Cache-Tags',
447
                'expression_language' => null,
448
                'rules' => array(),
449
            ),
450
            'invalidation' => array(
451
                'enabled' => false,
452
                'expression_language' => null,
453
                'rules' => array(),
454
            ),
455
            'user_context' => array(
456
                'enabled' => false,
457
                'match' => array(
458
                    'matcher_service' => 'fos_http_cache.user_context.request_matcher',
459
                    'accept' => 'application/vnd.fos.user-context-hash',
460
                    'method' => null,
461
                ),
462
                'hash_cache_ttl' => 0,
463
                'user_identifier_headers' => array('Cookie', 'Authorization'),
464
                'user_hash_header' => 'X-User-Context-Hash',
465
                'role_provider' => false,
466
                'logout_handler' => array(
467
                    'enabled' => false,
468
                ),
469
            ),
470
            'flash_message' => array(
471
                'enabled' => false,
472
                'name' => 'flashes',
473
                'path' => '/',
474
                'host' => null,
475
                'secure' => false,
476
            ),
477
            'debug' => array(
478
                'enabled' => false,
479
                'header' => 'X-Cache-Debug',
480
            ),
481
        );
482
    }
483
}
484