PutIndexMappingsHandler::newConfigRepository()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 11
nc 1
nop 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