Completed
Push — master ( 4ef090...a7a684 )
by GBProd
02:22
created

DeleteIndexHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 105
Duplicated Lines 100 %

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 105
loc 105
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testHandle() 20 20 1
A newIndices() 7 7 1
A newClient() 13 13 1
A newClientRepository() 21 21 2
A newConfigRepository() 21 21 3
A testHandleThrowExceptionIfNoClient() 15 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 DeleteIndexHandler
13
 * 
14
 * @author gbprod <[email protected]>
15
 */
16 View Code Duplication
class DeleteIndexHandler extends atoum
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...
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('delete')
31
                        ->withArguments([
32
                                'index' => 'my_index',
33
                            ]
34
                        )
35
                        ->once()
36
        ;
37
    }
38
    
39
    private function newIndices()
40
    {
41
        $this->mockGenerator->shuntParentClassCalls();
42
        $this->mockGenerator->orphanize('__construct');
43
44
        return new IndicesNamespace();
45
    }
46
    
47
    private function newClient($indices)
48
    {
49
        $this->mockGenerator->shuntParentClassCalls();
50
        $this->mockGenerator->orphanize('__construct');
51
52
        $client = new Client();
53
        
54
        $this->calling($client)->indices = function() use ($indices) {
55
            return $indices;
56
        };
57
        
58
        return $client;
59
    }
60
    
61
    private function newClientRepository($clientId, $client)
62
    {
63
        $this->mockGenerator->shuntParentClassCalls();
64
        $this->mockGenerator->orphanize('__construct');
65
66
        $clientRepository = new ClientRepository();
67
        
68
        $this->calling($clientRepository)->get = 
69
            function($id) use ($clientId, $client) {
70
                if ($id == $clientId) {
71
                    return $client;
72
                }
73
                
74
                return null;
75
            }
76
        ;
77
78
        $this->mockGenerator->unshuntParentClassCalls();
79
80
        return $clientRepository;
81
    }
82
    
83
    private function newConfigRepository($clientId, $indexId, $config)
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
        $this->mockGenerator->unshuntParentClassCalls();
101
        
102
        return $configRepository;
103
    }
104
    
105
    public function testHandleThrowExceptionIfNoClient()
106
    {
107
        $this
108
            ->given($config = ['my' => ['awesome' => 'config']])
109
                ->and($indices = $this->newIndices())
110
                ->and($client = $this->newClient($indices))
111
                ->and($clientRepository = $this->newClientRepository('my_client', null))
112
                ->and($configRepository = $this->newConfigRepository('my_client', 'my_index', $config))
113
                ->and($this->newTestedInstance($clientRepository, $configRepository))
114
            ->exception(function() {
115
                    $this->testedInstance->handle('my_client', 'my_index');
116
                })
117
                ->isInstanceOf(\InvalidArgumentException::class)
118
        ;
119
    }
120
}