Completed
Push — master ( 4e4cd8...063296 )
by Maksim
16s
created

RegisterPagerProvidersPassTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 137
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 137
loc 137
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testShouldImplementCompilerPassInterface() 6 6 1
A testCouldBeConstructedWithoutAnyArguments() 4 4 1
A testShouldDoNothingIfPagerProviderRegistryServiceIsMissing() 11 11 1
B testShouldRegisterTaggedPagerProviders() 27 27 1
A testThrowsIfTagMissesTypeAttribute() 14 14 1
A testThrowsIfProviderForSuchIndexTypeHasBeenAlreadyRegistered() 14 14 1
A testThrowsIfProviderServiceDoesNotImplementPagerProviderInterface() 17 17 1
A testShouldSkipClassCheckIfDefinitionHasFactory() 21 21 1
A createProviderDefinition() 7 7 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
namespace FOS\ElasticaBundle\Tests\DependencyInjection\Compiler;
4
5
use FOS\ElasticaBundle\DependencyInjection\Compiler\RegisterPagerProvidersPass;
6
use FOS\ElasticaBundle\Provider\PagerProviderInterface;
7
use FOS\ElasticaBundle\Provider\PagerProviderRegistry;
8
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
9
use Symfony\Component\DependencyInjection\Definition;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
12 View Code Duplication
class RegisterPagerProvidersPassTest extends \PHPUnit_Framework_TestCase
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
    public function testShouldImplementCompilerPassInterface()
15
    {
16
        $rc = new \ReflectionClass(RegisterPagerProvidersPass::class);
17
18
        $this->assertTrue($rc->implementsInterface(CompilerPassInterface::class));
19
    }
20
21
    public function testCouldBeConstructedWithoutAnyArguments()
22
    {
23
        new RegisterPagerProvidersPass();
24
    }
25
26
    public function testShouldDoNothingIfPagerProviderRegistryServiceIsMissing()
27
    {
28
        $container = new ContainerBuilder();
29
        $pass = new RegisterPagerProvidersPass();
30
31
        $container->setDefinition('foo_provider', $this->createProviderDefinition(array('type' => 'a')));
32
        $container->setDefinition('bar_provider', $this->createProviderDefinition(array('index' => 'foo', 'type' => 'b')));
33
        $container->setDefinition('baz_provider', $this->createProviderDefinition(array('index' => 'bar', 'type' => 'a')));
34
35
        $pass->process($container);
36
    }
37
38
    public function testShouldRegisterTaggedPagerProviders()
39
    {
40
        $container = new ContainerBuilder();
41
        $pass = new RegisterPagerProvidersPass();
42
43
        $registry = new Definition(PagerProviderRegistry::class);
44
        $registry->addArgument([]);
45
46
        $container->setParameter('fos_elastica.default_index', 'foo');
47
        $container->setDefinition('fos_elastica.pager_provider_registry', $registry);
48
49
        $container->setDefinition('foo_provider', $this->createProviderDefinition(array('type' => 'a')));
50
        $container->setDefinition('bar_provider', $this->createProviderDefinition(array('index' => 'foo', 'type' => 'b')));
51
        $container->setDefinition('baz_provider', $this->createProviderDefinition(array('index' => 'bar', 'type' => 'a')));
52
53
        $pass->process($container);
54
55
        $this->assertEquals([
56
            'foo' => [
57
                'a' => 'foo_provider',
58
                'b' => 'bar_provider',
59
            ],
60
            'bar' => [
61
                'a' => 'baz_provider'
62
            ],
63
        ], $registry->getArgument(0));
64
    }
65
66
    public function testThrowsIfTagMissesTypeAttribute()
67
    {
68
        $container = new ContainerBuilder();
69
        $pass = new RegisterPagerProvidersPass();
70
71
        $container->setDefinition('fos_elastica.pager_provider_registry', new Definition());
72
        $container->setParameter('fos_elastica.default_index', 'foo');
73
74
        $container->setDefinition('a_provider', $this->createProviderDefinition([]));
75
76
        $this->setExpectedException(\InvalidArgumentException::class, 'Elastica provider "a_provider" must specify the "type" attribute.');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
77
78
        $pass->process($container);
79
    }
80
81
    public function testThrowsIfProviderForSuchIndexTypeHasBeenAlreadyRegistered()
82
    {
83
        $container = new ContainerBuilder();
84
        $pass = new RegisterPagerProvidersPass();
85
86
        $container->setDefinition('fos_elastica.pager_provider_registry', new Definition());
87
        $container->setParameter('fos_elastica.default_index', 'foo');
88
89
        $container->setDefinition('a_foo_provider', $this->createProviderDefinition(['index' => 'foo', 'type' => 'bar']));
90
        $container->setDefinition('a_bar_provider', $this->createProviderDefinition(['index' => 'foo', 'type' => 'bar']));
91
92
        $this->setExpectedException(\InvalidArgumentException::class, 'Cannot register provider "a_bar_provider". The provider "a_foo_provider" has been registered for same index "foo" and type "bar"');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
93
        $pass->process($container);
94
    }
95
96
    public function testThrowsIfProviderServiceDoesNotImplementPagerProviderInterface()
97
    {
98
        $container = new ContainerBuilder();
99
        $pass = new RegisterPagerProvidersPass();
100
101
        $container->setDefinition('fos_elastica.pager_provider_registry', new Definition());
102
        $container->setParameter('fos_elastica.default_index', 'foo');
103
104
        $provider = $this->createProviderDefinition(['index' => 'foo', 'type' => 'bar']);
105
        $provider->setClass(\stdClass::class);
106
107
        $container->setDefinition('a_foo_provider', $provider);
108
109
        $this->setExpectedException(\InvalidArgumentException::class, 'Elastica provider "a_foo_provider" with class "stdClass" must implement "FOS\ElasticaBundle\Provider\PagerProviderInterface".');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
110
111
        $pass->process($container);
112
    }
113
114
    public function testShouldSkipClassCheckIfDefinitionHasFactory()
115
    {
116
        $container = new ContainerBuilder();
117
        $pass = new RegisterPagerProvidersPass();
118
119
        $registry = new Definition(PagerProviderRegistry::class);
120
        $registry->addArgument([]);
121
122
        $container->setParameter('fos_elastica.default_index', 'foo');
123
        $container->setDefinition('fos_elastica.pager_provider_registry', $registry);
124
125
        $provider = $this->createProviderDefinition(['index' => 'foo', 'type' => 'bar']);
126
        $provider->setClass(\stdClass::class);
127
        $provider->setFactory('a_factory_function');
128
129
        $container->setDefinition('a_foo_provider', $provider);
130
131
        $pass->process($container);
132
133
        $this->assertEquals(['foo' => ['bar' => 'a_foo_provider']], $registry->getArgument(0));
134
    }
135
136
    /**
137
     * @param array $attributes
138
     * 
139
     * @return Definition
140
     */
141
    private function createProviderDefinition(array $attributes = array())
142
    {
143
        $definition = new Definition(PagerProviderInterface::class);
144
        $definition->addTag('fos_elastica.pager_provider', $attributes);
145
146
        return $definition;
147
    }
148
}
149