Completed
Push — master ( a86091...f009d3 )
by GBProd
02:31
created

PutIndexMappingsHandler::newConfigRepository()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 21
Code Lines 11

Duplication

Lines 21
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 21
loc 21
rs 9.3142
cc 3
eloc 11
nc 1
nop 3
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 PutIndexMappingssHandler
13
 * 
14
 * @author gbprod <[email protected]>
15
 */
16 View Code Duplication
class PutIndexMappingsHandler 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
        $config = [
21
            'mappings' => [
22
                'awesome' => 'config'
23
            ], 
24
            'foo' => 'bar',
25
        ];
26
        
27
        $this
28
            ->given($config)
29
                ->and($indices = $this->newIndices())
30
                ->and($client = $this->newClient($indices))
31
                ->and($clientRepository = $this->newClientRepository('my_client', $client))
32
                ->and($configRepository = $this->newConfigRepository('my_client', 'my_index', $config))
33
                ->and($this->newTestedInstance($clientRepository, $configRepository))
34
            ->if($this->testedInstance->handle('my_client', 'my_index'))
35
            ->then
36
                ->mock($indices)
37
                    ->call('putMapping')
38
                        ->withArguments(
39
                            [
40
                                'index' => 'my_index',
41
                                'body'  => [
42
                                    'awesome' => 'config',
43
                                ],
44
                            ]
45
                        )
46
                        ->once()
47
        ;
48
    }
49
    
50
    private function newIndices()
51
    {
52
        $this->mockGenerator->shuntParentClassCalls();
53
        $this->mockGenerator->orphanize('__construct');
54
55
        return new IndicesNamespace();
56
    }
57
    
58
    private function newClient($indices)
59
    {
60
        $this->mockGenerator->shuntParentClassCalls();
61
        $this->mockGenerator->orphanize('__construct');
62
63
        $client = new Client();
64
        
65
        $this->calling($client)->indices = function() use ($indices) {
66
            return $indices;
67
        };
68
        
69
        return $client;
70
    }
71
    
72
    private function newClientRepository($clientId, $client)
73
    {
74
        $this->mockGenerator->shuntParentClassCalls();
75
        $this->mockGenerator->orphanize('__construct');
76
77
        $clientRepository = new ClientRepository();
78
        
79
        $this->calling($clientRepository)->get = 
80
            function($id) use ($clientId, $client) {
81
                if ($id == $clientId) {
82
                    return $client;
83
                }
84
                
85
                return null;
86
            }
87
        ;
88
89
        $this->mockGenerator->unshuntParentClassCalls();
90
91
        return $clientRepository;
92
    }
93
    
94
    private function newConfigRepository($clientId, $indexId, $config)
95
    {
96
        $this->mockGenerator->shuntParentClassCalls();
97
        $this->mockGenerator->orphanize('__construct');
98
99
        $configRepository = new IndexConfigurationRepository();
100
        
101
        $this->calling($configRepository)->get = 
102
            function($clientIdParam, $indexIdParam) use ($clientId, $indexId, $config) {
103
                if ($clientId == $clientIdParam && $indexId == $indexIdParam) {
104
                    return $config;
105
                }
106
                
107
                return null;
108
            }
109
        ;
110
111
        $this->mockGenerator->unshuntParentClassCalls();
112
        
113
        return $configRepository;
114
    }
115
    
116
    public function testHandleThrowExceptionIfNoClient()
117
    {
118
        $this
119
            ->given($config = ['my' => ['awesome' => 'config']])
120
                ->and($indices = $this->newIndices())
121
                ->and($client = $this->newClient($indices))
122
                ->and($clientRepository = $this->newClientRepository('my_client', null))
123
                ->and($configRepository = $this->newConfigRepository('my_client', 'my_index', $config))
124
                ->and($this->newTestedInstance($clientRepository, $configRepository))
125
            ->exception(function() {
126
                    $this->testedInstance->handle('my_client', 'my_index');
127
                })
128
                ->isInstanceOf(\InvalidArgumentException::class)
129
        ;
130
    }
131
}