Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
21 | class FOSHttpCacheExtensionTest extends \PHPUnit_Framework_TestCase |
||
22 | { |
||
23 | /** |
||
24 | * @var FOSHttpCacheExtension |
||
25 | */ |
||
26 | protected $extension; |
||
27 | |||
28 | protected function setUp() |
||
29 | { |
||
30 | $this->extension = new FOSHttpCacheExtension(); |
||
31 | } |
||
32 | |||
33 | public function testConfigLoadVarnish() |
||
34 | { |
||
35 | $container = $this->createContainer(); |
||
36 | $this->extension->load(array($this->getBaseConfig()), $container); |
||
37 | |||
38 | $this->assertTrue($container->hasDefinition('fos_http_cache.proxy_client.varnish')); |
||
39 | $this->assertFalse($container->hasDefinition('fos_http_cache.proxy_client.nginx')); |
||
40 | $this->assertTrue($container->hasAlias('fos_http_cache.default_proxy_client')); |
||
41 | $this->assertTrue($container->hasDefinition('fos_http_cache.event_listener.invalidation')); |
||
42 | $this->assertTrue($container->hasDefinition('fos_http_cache.event_listener.tag')); |
||
43 | } |
||
44 | |||
45 | public function testConfigLoadVarnishCustomClient() |
||
46 | { |
||
47 | $container = $this->createContainer(); |
||
48 | |||
49 | $config = $this->getBaseConfig(); |
||
50 | $config['proxy_client']['varnish']['http']['http_client'] = 'my_guzzle'; |
||
51 | $this->extension->load(array($config), $container); |
||
52 | |||
53 | $this->assertTrue($container->hasDefinition('fos_http_cache.proxy_client.varnish.http_dispatcher')); |
||
54 | $def = $container->getDefinition('fos_http_cache.proxy_client.varnish.http_dispatcher'); |
||
55 | $this->assertInstanceOf(Reference::class, $def->getArgument(2)); |
||
56 | $this->assertEquals('my_guzzle', $def->getArgument(2)->__toString()); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
||
61 | */ |
||
62 | public function testConfigLoadVarnishInvalidUrl() |
||
63 | { |
||
64 | $container = $this->createContainer(); |
||
65 | $config = $this->getBaseConfig(); |
||
66 | $config['proxy_client']['varnish']['http']['base_url'] = 'ftp:not a valid url'; |
||
67 | |||
68 | $this->extension->load(array($config), $container); |
||
69 | } |
||
70 | |||
71 | View Code Duplication | public function testConfigLoadNginx() |
|
|
|||
72 | { |
||
73 | $container = $this->createContainer(); |
||
74 | $this->extension->load(array( |
||
75 | array( |
||
76 | 'proxy_client' => array( |
||
77 | 'nginx' => array( |
||
78 | 'http' => [ |
||
79 | 'base_url' => 'my_hostname', |
||
80 | 'servers' => array( |
||
81 | '127.0.0.1', |
||
82 | ), |
||
83 | ], |
||
84 | ), |
||
85 | ), |
||
86 | ), |
||
87 | ), $container); |
||
88 | |||
89 | $this->assertFalse($container->hasDefinition('fos_http_cache.proxy_client.varnish')); |
||
90 | $this->assertTrue($container->hasDefinition('fos_http_cache.proxy_client.nginx')); |
||
91 | $this->assertTrue($container->hasAlias('fos_http_cache.default_proxy_client')); |
||
92 | $this->assertTrue($container->hasDefinition('fos_http_cache.event_listener.invalidation')); |
||
93 | $this->assertFalse($container->hasDefinition('fos_http_cache.http.symfony_response_tagger')); |
||
94 | } |
||
95 | |||
96 | View Code Duplication | public function testConfigLoadSymfony() |
|
97 | { |
||
98 | $container = $this->createContainer(); |
||
99 | $this->extension->load(array( |
||
100 | array( |
||
101 | 'proxy_client' => array( |
||
102 | 'symfony' => array( |
||
103 | 'http' => [ |
||
104 | 'base_url' => 'my_hostname', |
||
105 | 'servers' => array( |
||
106 | '127.0.0.1', |
||
107 | ), |
||
108 | ], |
||
109 | ), |
||
110 | ), |
||
111 | ), |
||
112 | ), $container); |
||
113 | |||
114 | $this->assertFalse($container->hasDefinition('fos_http_cache.proxy_client.varnish')); |
||
115 | $this->assertFalse($container->hasDefinition('fos_http_cache.proxy_client.nginx')); |
||
116 | $this->assertTrue($container->hasDefinition('fos_http_cache.proxy_client.symfony')); |
||
117 | $this->assertTrue($container->hasAlias('fos_http_cache.default_proxy_client')); |
||
118 | $this->assertTrue($container->hasDefinition('fos_http_cache.event_listener.invalidation')); |
||
119 | $this->assertFalse($container->hasDefinition('fos_http_cache.http.symfony_response_tagger')); |
||
120 | } |
||
121 | |||
122 | public function testConfigCustomClient() |
||
140 | |||
141 | public function testEmptyConfig() |
||
150 | |||
151 | /** |
||
152 | * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
||
153 | * @expectedExceptionMessage You can not enable cache tagging with the nginx client |
||
154 | */ |
||
155 | public function testConfigTagNotSupported() |
||
156 | { |
||
176 | |||
177 | public function testConfigLoadTagRules() |
||
204 | |||
205 | public function testConfigLoadInvalidatorRules() |
||
234 | |||
235 | public function testConfigLoadCacheControl() |
||
266 | |||
267 | /** |
||
268 | * Check if comma separated strings are parsed as expected. |
||
269 | */ |
||
270 | public function testConfigLoadCacheControlSplit() |
||
297 | |||
298 | public function testConfigUserContext() |
||
326 | |||
327 | public function testConfigWithoutUserContext() |
||
354 | |||
355 | public function testConfigLoadFlashMessageListener() |
||
365 | |||
366 | protected function createContainer() |
||
380 | |||
381 | View Code Duplication | protected function getBaseConfig() |
|
396 | |||
397 | /** |
||
398 | * @param ContainerBuilder $container |
||
399 | * @param array $attributes |
||
400 | * |
||
401 | * @return DefinitionDecorator |
||
402 | */ |
||
403 | private function assertMatcherCreated(ContainerBuilder $container, array $attributes) |
||
448 | |||
449 | /** |
||
450 | * Assert that the service $id exists and has a method call mapped onto it. |
||
451 | * |
||
452 | * @param ContainerBuilder $container |
||
453 | * @param string $id The service id to investigate |
||
454 | */ |
||
455 | private function assertListenerHasRule(ContainerBuilder $container, $id) |
||
462 | } |
||
463 |
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.