Completed
Push — master ( 9de1be...100987 )
by David
8s
created

HashGeneratorPassTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 97
Duplicated Lines 13.4 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 6
c 4
b 2
f 0
lcom 1
cbo 5
dl 13
loc 97
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testConfigNoContext() 0 8 1
A testConfigNoProviders() 0 8 1
B testConfigPrioritisedProviders() 0 29 1
A createContainer() 0 6 1
A getBaseConfig() 13 13 1

How to fix   Duplicated Code   

Duplicated Code

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
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\Compiler;
13
14
use FOS\HttpCacheBundle\DependencyInjection\Compiler\HashGeneratorPass;
15
use FOS\HttpCacheBundle\DependencyInjection\FOSHttpCacheExtension;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
19
20
class HashGeneratorPassTest extends \PHPUnit_Framework_TestCase
21
{
22
    /**
23
     * @var FOSHttpCacheExtension
24
     */
25
    private $extension;
26
27
    /**
28
     * @var HashGeneratorPass
29
     */
30
    private $userContextListenerPass;
31
32
    protected function setUp()
33
    {
34
        $this->extension = new FOSHttpCacheExtension();
35
        $this->userContextListenerPass = new HashGeneratorPass();
36
    }
37
38
    /**
39
     * Nothing happens when user_context.hash_generator is not enabled.
40
     */
41
    public function testConfigNoContext()
42
    {
43
        $container = $this->createContainer();
44
        $config = $this->getBaseConfig();
45
        $this->extension->load(array($config), $container);
46
        $this->userContextListenerPass->process($container);
47
        $this->assertCount(13, $container->getDefinitions());
48
    }
49
50
    /**
51
     * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
52
     * @expectedExceptionMessage No user context providers found
53
     */
54
    public function testConfigNoProviders()
55
    {
56
        $container = $this->createContainer();
57
        $config = $this->getBaseConfig();
58
        $config['user_context']['enabled'] = true;
59
        $this->extension->load(array($config), $container);
60
        $this->userContextListenerPass->process($container);
61
    }
62
63
    /**
64
     * Test that priorities allow for us to re-arrange services.
65
     */
66
    public function testConfigPrioritisedProviders()
67
    {
68
        $container = $this->createContainer();
69
70
        $definitions = array(
71
            'test.provider' => new Definition('\\stdClass'),
72
            'foo.provider' => new Definition('\\stdClass'),
73
            'bar.provider' => new Definition('\\stdClass'),
74
        );
75
        $definitions['test.provider']->addTag(HashGeneratorPass::TAG_NAME, array('priority' => 10));
76
        $definitions['foo.provider']->addTag(HashGeneratorPass::TAG_NAME);
77
        $definitions['bar.provider']->addTag(HashGeneratorPass::TAG_NAME, array('priority' => 5));
78
79
        $container->setDefinitions($definitions);
80
81
        $config = $this->getBaseConfig();
82
        $config['user_context']['enabled'] = true;
83
        $this->extension->load(array($config), $container);
84
        $this->userContextListenerPass->process($container);
85
86
        $arguments = $container->getDefinition('fos_http_cache.user_context.hash_generator')->getArguments();
87
        $services = array_map(
88
            function ($argument) {
89
                return (string) $argument;
90
            }, array_shift($arguments)
91
        );
92
93
        $this->assertEquals($services, array('test.provider', 'bar.provider', 'foo.provider'));
94
    }
95
96
    protected function createContainer()
97
    {
98
        return new ContainerBuilder(new ParameterBag(array(
99
            'kernel.debug' => false,
100
        )));
101
    }
102
103 View Code Duplication
    protected function getBaseConfig()
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...
104
    {
105
        return array(
106
            'proxy_client' => array(
107
                'varnish' => array(
108
                    'base_url' => 'my_hostname',
109
                    'servers' => array(
110
                        '127.0.0.1',
111
                    ),
112
                ),
113
            ),
114
        );
115
    }
116
}
117