Completed
Push — master ( d72a06...ba0ad0 )
by GBProd
04:38
created

PutIndexMappingsHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 5
c 3
b 1
f 1
lcom 1
cbo 0
dl 0
loc 79
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B testHandle() 0 32 1
A newIndices() 0 7 1
A newClient() 0 13 1
A newConfigRepository() 0 21 2
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
class PutIndexMappingsHandler extends atoum
17
{
18
    public function testHandle()
19
    {
20
        $config = [
21
            'mappings' => [
22
                'my_type' => ['config'],
23
                'my_type_2' => ['config'],
24
            ],
25
            'foo' => 'bar',
26
        ];
27
28
        $this
29
            ->given($config)
30
                ->and($indices = $this->newIndices())
31
                ->and($client = $this->newClient($indices))
32
                ->and($configRepository = $this->newConfigRepository('my_index', $config))
33
                ->and($this->newTestedInstance($configRepository))
34
            ->if($this->testedInstance->handle($client, 'my_index', 'my_type'))
35
            ->then
36
                ->mock($indices)
37
                    ->call('putMapping')
38
                        ->withArguments(
39
                            [
40
                                'index' => 'my_index',
41
                                'type'  => 'my_type',
42
                                'body'  => [
43
                                    'my_type' => ['config'],
44
                                ],
45
                            ]
46
                        )
47
                        ->once()
48
        ;
49
    }
50
51
    private function newIndices()
52
    {
53
        $this->mockGenerator->shuntParentClassCalls();
54
        $this->mockGenerator->orphanize('__construct');
55
56
        return new IndicesNamespace();
57
    }
58
59
    private function newClient($indices)
60
    {
61
        $this->mockGenerator->shuntParentClassCalls();
62
        $this->mockGenerator->orphanize('__construct');
63
64
        $client = new Client();
65
66
        $this->calling($client)->indices = function() use ($indices) {
67
            return $indices;
68
        };
69
70
        return $client;
71
    }
72
73
    private function newConfigRepository($index, $config)
74
    {
75
        $this->mockGenerator->shuntParentClassCalls();
76
        $this->mockGenerator->orphanize('__construct');
77
78
        $configRepository = new IndexConfigurationRepository();
79
80
        $this->calling($configRepository)->get =
81
            function($indexParam) use ($index, $config) {
82
                if ($index == $indexParam) {
83
                    return $config;
84
                }
85
86
                return null;
87
            }
88
        ;
89
90
        $this->mockGenerator->unshuntParentClassCalls();
91
92
        return $configRepository;
93
    }
94
}
95