Completed
Push — master ( a36c27...010ee5 )
by GBProd
02:47
created

CreateIndexHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 103
Duplicated Lines 36.89 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 0
dl 38
loc 103
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testHandle() 0 22 1
A newIndices() 0 7 1
A newClient() 0 13 1
A newClientRepository() 19 19 2
A newConfigRepository() 19 19 3
A testHandleThrowExceptionIfNoClient() 0 15 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 GBProd\Tests\Units\ElasticsearchExtraBundle\Handler;
4
5
use atoum;
6
use mock\Elasticsearch\Client;
7
use mock\Elasticsearch\Namespaces\IndicesNamespace;
8
use mock\GBProd\ElasticsearchExtraBundle\Repository\ClientRepository;
9
use mock\GBProd\ElasticsearchExtraBundle\Repository\IndexConfigurationRepository;
10
11
/**
12
 * Tests for CreateIndexHandler
13
 * 
14
 * @author gbprod <[email protected]>
15
 */
16
class CreateIndexHandler extends atoum
17
{
18
    public function testHandle()
19
    {
20
        $this
21
            ->given($config = ['my' => ['awesome' => 'config']])
22
                ->and($indices = $this->newIndices())
23
                ->and($client = $this->newClient($indices))
24
                ->and($clientRepository = $this->newClientRepository('my_client', $client))
25
                ->and($configRepository = $this->newConfigRepository('my_client', 'my_index', $config))
26
                ->and($this->newTestedInstance($clientRepository, $configRepository))
27
            ->if($this->testedInstance->handle('my_client', 'my_index'))
28
            ->then
29
                ->mock($indices)
30
                    ->call('create')
31
                        ->withArguments(
32
                            [
33
                                'index' => 'my_index',
34
                                'body'  => $config,
35
                            ]
36
                        )
37
                        ->once()
38
        ;
39
    }
40
    
41
    private function newIndices()
42
    {
43
        $this->mockGenerator->shuntParentClassCalls();
44
        $this->mockGenerator->orphanize('__construct');
45
46
        return new IndicesNamespace();
47
    }
48
    
49
    private function newClient($indices)
50
    {
51
        $this->mockGenerator->shuntParentClassCalls();
52
        $this->mockGenerator->orphanize('__construct');
53
54
        $client = new Client();
55
        
56
        $this->calling($client)->indices = function() use ($indices) {
57
            return $indices;
58
        };
59
        
60
        return $client;
61
    }
62
    
63 View Code Duplication
    private function newClientRepository($clientId, $client)
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...
64
    {
65
        $this->mockGenerator->shuntParentClassCalls();
66
        $this->mockGenerator->orphanize('__construct');
67
68
        $clientRepository = new ClientRepository();
69
        
70
        $this->calling($clientRepository)->get = 
71
            function($id) use ($clientId, $client) {
72
                if ($id == $clientId) {
73
                    return $client;
74
                }
75
                
76
                return null;
77
            }
78
        ;
79
        
80
        return $clientRepository;
81
    }
82
    
83 View Code Duplication
    private function newConfigRepository($clientId, $indexId, $config)
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...
84
    {
85
        $this->mockGenerator->shuntParentClassCalls();
86
        $this->mockGenerator->orphanize('__construct');
87
88
        $configRepository = new IndexConfigurationRepository();
89
        
90
        $this->calling($configRepository)->get = 
91
            function($clientIdParam, $indexIdParam) use ($clientId, $indexId, $config) {
92
                if ($clientId == $clientIdParam && $indexId == $indexIdParam) {
93
                    return $config;
94
                }
95
                
96
                return null;
97
            }
98
        ;
99
        
100
        return $configRepository;
101
    }
102
    
103
    public function testHandleThrowExceptionIfNoClient()
104
    {
105
        $this
106
            ->given($config = ['my' => ['awesome' => 'config']])
107
                ->and($indices = $this->newIndices())
108
                ->and($client = $this->newClient($indices))
109
                ->and($clientRepository = $this->newClientRepository('my_client', null))
110
                ->and($configRepository = $this->newConfigRepository('my_client', 'my_index', $config))
111
                ->and($this->newTestedInstance($clientRepository, $configRepository))
112
            ->exception(function() {
113
                    $this->testedInstance->handle('my_client', 'my_index');
114
                })
115
                ->isInstanceOf(\InvalidArgumentException::class)
116
        ;
117
    }
118
}