Completed
Push — master ( a7a684...a86091 )
by GBProd
02:32
created

testHandleThrowExceptionIfNoClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
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 PutIndexSettingsHandler
13
 * 
14
 * @author gbprod <[email protected]>
15
 */
16
class PutIndexSettingsHandler extends atoum
17
{
18
    public function testHandle()
19
    {
20
        $config = [
21
            'settings' => [
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('putSettings')
38
                        ->withArguments(
39
                            [
40
                                'index' => 'my_index',
41
                                'body'  => [
42
                                    'settings' => ['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
}